Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions gen-pack
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#
# Open-CMSIS-Pack gen-pack Bash library
#
# Copyright (c) 2022-2025 Arm Limited. All rights reserved.
# Copyright (c) 2022-2026 Arm Limited. All rights reserved.
#
# Provided as-is without warranty under Apache 2.0 License
# SPDX-License-Identifier: Apache-2.0
#

GEN_PACK_LIB_VERSION="0.12.1"
GEN_PACK_LIB_VERSION="0.13.0"
GEN_PACK_LIB_SOURCE="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
GEN_PACK_SCRIPT_SOURCE="$(dirname "$(readlink -f "$0")")"

SCRIPT="${SCRIPT:-$(basename "$0")}"
LOG_PREFIX="${LOG_PREFIX:-"$(tput setaf 4 2>/dev/null)${SCRIPT}>$(tput sgr0 2>/dev/null) "}"

shopt -s expand_aliases
shopt -s expand_aliases globstar

if [ "${BASH_VERSION%%.*}" -lt 5 ]; then
echo "${LOG_PREFIX}gen-pack requires Bash version 5 or later." >&2
Expand Down Expand Up @@ -97,12 +97,12 @@ function add_files {
echo_log Adding files to pack:
echo_log "${PACK_BASE_FILES}"
echo_log " "
for f in ${PACK_BASE_FILES}; do
d=$(realpath --relative-to="$(pwd)" "$f")
if [[ $f = ../* ]]; then
cp -r "$f" "$1"
for pattern in ${PACK_BASE_FILES}; do
d=$(realpath --relative-to="$(pwd)" "$pattern")
if [[ $pattern = ../* ]]; then
cp -r "$pattern" "$1"
else
cp -f --parents "$f" "$1/"
cp -f --parents "$pattern" "$1/"
fi
done
else
Expand All @@ -117,13 +117,31 @@ function add_files {
# <build> The temporary build folder, typically PACK_BUILD
#
function delete_files {
local build="$1"
# Delete files
if [ -n "${PACK_DELETE_FILES}" ]; then
echo_log Deleting files from pack:
echo_log "${PACK_DELETE_FILES}"
echo_log " "
for f in ${PACK_DELETE_FILES}; do
find "$(dirname "$1/$f")" -maxdepth 1 -name "$(basename "$1/$f")" -exec rm -rf "{}" +

local patterns
split_and_trim patterns "${PACK_DELETE_FILES}"

for pattern in "${patterns[@]}"; do
if ! is_subpath_of "${build}" "${build}/${pattern}"; then
echo_err "Skipping '${pattern}' as it resolves to outside the build folder."
continue
fi
# shellcheck disable=SC2206
local files=("${build}/"${pattern})
for file in "${files[@]}"; do
if is_subpath_of "${build}" "${file}"; then
echo_v "Removing '${file}' ..."
rm -rf "${file}"
else
echo_err "Skipping '${file}' as it resolves to outside the build folder."
fi
done
done
else
echo_log "PACK_DELETE_FILES is empty, no files to be removed."
Expand Down
42 changes: 41 additions & 1 deletion lib/helper
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Open-CMSIS-Pack gen-pack Bash library
#
# Copyright (c) 2022-2024 Arm Limited. All rights reserved.
# Copyright (c) 2022-2026 Arm Limited. All rights reserved.
#
# Provided as-is without warranty under Apache 2.0 License
# SPDX-License-Identifier: Apache-2.0
Expand Down Expand Up @@ -73,6 +73,46 @@ function check_placeholder {
fi
}

#
# Convert multiline input into an array
# with trimmed leading/trailing whitespaces and non-empty lines
#
# Usage: split_and_trim <result_array> <input_string>
# <result_array> Name of the array variable to store the result
# <input_string> Multiline string input
#
function split_and_trim {
local -n result=$1
local input=$2
local line

result=()
while IFS= read -r line; do
# Trim whitespace
line="${line#"${line%%[![:space:]]*}"}"
line="${line%"${line##*[![:space:]]}"}"
# Add non-empty lines
[[ -n "$line" ]] && result+=("$line")
done <<< "$input"
}

#
# Check if a path is a sibling of another path
#
# Usage: is_subpath_of <base_dir> <target_dir>
# <base_dir> Base directory
# <target_dir> Target directory to check
#
function is_subpath_of {
local base_dir target_dir
base_dir=$(realpath -m "$1")
target_dir=$(realpath -m "$2")
case "${target_dir}" in
"${base_dir}"/*) return 0;;
esac
return 1
}

#
# Get filesystem permissions
# Returns the octal representation for Unix file permissions.
Expand Down
30 changes: 28 additions & 2 deletions test/tests_gen_pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Open-CMSIS-Pack gen-pack Bash library
#
# Copyright (c) 2022-2025 Arm Limited. All rights reserved.
# Copyright (c) 2022-2026 Arm Limited. All rights reserved.
#
# Provided as-is without warranty under Apache 2.0 License
# SPDX-License-Identifier: Apache-2.0
Expand Down Expand Up @@ -179,12 +179,15 @@ test_delete_files() {
cp -r --parents "input3" "${PACK_BUILD}"

PACK_DELETE_FILES="
../**/file*.txt
input1/file11.txt
input2/file22.txt
input3/file33.txt
"
delete_files "${PACK_BUILD}"
OUTPUT=$(delete_files "${PACK_BUILD}" 2>&1)
echo "${OUTPUT}"

assertContains "${OUTPUT}" "Skipping '../**/file*.txt'"
assertFalse "[ -f \"${PACK_BUILD}/input1/file11.txt\" ]"
assertTrue "[ -f \"${PACK_BUILD}/input1/file12.txt\" ]"
assertTrue "[ -f \"${PACK_BUILD}/input1/file13.txt\" ]"
Expand Down Expand Up @@ -219,6 +222,29 @@ test_delete_files_non_recursive() {
assertTrue "File 'examples/rtos/README.md' unexpectedly deleted" "[ -f \"${PACK_BUILD}/examples/rtos/README.md\" ]"
}

test_delete_files_recursive() {
mkdir -p "${PACK_BUILD}/examples/blinky"
mkdir -p "${PACK_BUILD}/examples/rtos"

touch "${PACK_BUILD}/README.md"
touch "${PACK_BUILD}/CONTRIBUTE.md"
touch "${PACK_BUILD}/examples/README.md"
touch "${PACK_BUILD}/examples/blinky/README.md"
touch "${PACK_BUILD}/examples/rtos/README.md"

PACK_DELETE_FILES="
**/*.md
"

delete_files "${PACK_BUILD}"

assertFalse "File 'README.md' not deleted" "[ -f \"${PACK_BUILD}/README.md\" ]"
assertFalse "File 'CONTRIBUTE.md' not deleted" "[ -f \"${PACK_BUILD}/CONTRIBUTE.md\" ]"
assertFalse "File 'examples/README.md' not deleted" "[ -f \"${PACK_BUILD}/examples/README.md\" ]"
assertFalse "File 'examples/blinky/README.md' not deleted" "[ -f \"${PACK_BUILD}/examples/blinky/README.md\" ]"
assertFalse "File 'examples/rtos/README.md' not deleted" "[ -f \"${PACK_BUILD}/examples/rtos/README.md\" ]"
}

test_apply_patches() {
createTestData

Expand Down
33 changes: 32 additions & 1 deletion test/tests_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Open-CMSIS-Pack gen-pack Bash library
#
# Copyright (c) 2022-2025 Arm Limited. All rights reserved.
# Copyright (c) 2022-2026 Arm Limited. All rights reserved.
#
# Provided as-is without warranty under Apache 2.0 License
# SPDX-License-Identifier: Apache-2.0
Expand Down Expand Up @@ -80,6 +80,37 @@ test_check_placeholder() {
assertContains "${output}" "Remove placeholders from gen-pack settings!"
}

test_split_and_trim() {
MULTILINE_VAR="
some text
**/glob/pattern.txt
path/to/fileName.dat
"

split_and_trim RESULT_ARRAY "${MULTILINE_VAR}"
assertEquals 3 "${#RESULT_ARRAY[@]}"
assertEquals "some text" "${RESULT_ARRAY[0]}"
assertEquals "**/glob/pattern.txt" "${RESULT_ARRAY[1]}"
assertEquals "path/to/fileName.dat" "${RESULT_ARRAY[2]}"
}

test_is_subpath_of() {
output=$(is_subpath_of "/base/dir" "/base/dir/sub/dir/file.txt")
assertTrue $?

output=$(is_subpath_of "/base/dir" "/base/dir/file.txt")
assertTrue $?

output=$(is_subpath_of "/base/dir" "/base/dir/../file.txt")
assertFalse $?

output=$(is_subpath_of "/base/dir" "/base/otherdir/file.txt")
assertFalse $?

output=$(is_subpath_of "/base/dir" "/other/base/dir/file.txt")
assertFalse $?
}

test_is_url() {
output=$(is_url "http://url.to/file")
assertTrue $?
Expand Down
Loading