diff --git a/base/lnls-prune-artifacts.sh b/base/lnls-prune-artifacts.sh index 4ce99dd..4a4408e 100755 --- a/base/lnls-prune-artifacts.sh +++ b/base/lnls-prune-artifacts.sh @@ -77,6 +77,34 @@ get_used_epics_modules() { filter_out_paths "$all_modules" "$unused_modules" } +# Traverse ancestor directories of each provided path, and concatenate all +# their .lnls-keep-paths defined entries as absolute paths. +get_defined_paths_to_keep() { + for path; do + if [ "${path:0:1}" != "/" ]; then + >&2 echo "error: get_defined_paths_to_keep() expects absolute paths, but got '$path'" + exit 1 + fi + + while true; do + keep_path_file="$path/.lnls-keep-paths" + + if [ -f "$keep_path_file" ]; then + keep_paths=$(cat "$keep_path_file") + + for keep_path in $keep_paths; do + # output it as an absolute path + realpath "$path"/$keep_path + done + fi + + [ "$path" == "/" ] && break + + path=$(dirname $path) + done + done | sort -u +} + prune_module_dirs() { module=$1 @@ -84,6 +112,7 @@ prune_module_dirs() { keep_paths=$(cat << EOF $(find_shared_libraries $module) $(find $module -type f -regex ".*\.\(cmd\|db\|template\|req\|substitutions\)" -printf "%h\n" | sort -u) +$(get_defined_paths_to_keep $module) EOF ) @@ -154,11 +183,16 @@ remove_unused_shared_libraries() { remove_libs=$(echo "$remove_libs" | grep -vx $lib) done + keep_paths=$(get_defined_paths_to_keep $remove_libs) + for lib in $remove_libs; do - size=$(du -hs $lib | cut -f 1) + # if library is not found inside any $keep_dirs, remove it + if find $keep_paths -path "$lib" -exec false {} +; then + size=$(du -hs $lib | cut -f 1) - echo "Removing shared library '$lib' ($size)" - rm -f ${lib%.so*}.so* + echo "Removing shared library '$lib' ($size)" + rm -f ${lib%.so*}.so* + fi done }