From 55c66a333a65a71ee4e82e2a4beb75c88cd0f54c Mon Sep 17 00:00:00 2001 From: Thomas Gebert Date: Fri, 3 Jan 2025 10:09:10 +0100 Subject: [PATCH] Improve loops Eliminating for loops and using pipe for input the while loops shall improve the memory footprint and performance --- retention-helper/retention-helper.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/retention-helper/retention-helper.sh b/retention-helper/retention-helper.sh index 83bfc04..6579108 100644 --- a/retention-helper/retention-helper.sh +++ b/retention-helper/retention-helper.sh @@ -50,7 +50,7 @@ get_xattr_of_file() { } find_files_with_atime() { - while read -r FILE; do + find "${DIRECTORY}" -type f | while read -r FILE; do ATIME_FILE=$(get_atime_of_file "${FILE}") if [ "${ATIME_FILE}" == "" ]; then continue @@ -58,12 +58,12 @@ find_files_with_atime() { if [ ${ATIME_FILE} -ge ${ATIME_SEARCH} ]; then echo "${FILE}" fi - done <<< $(find "${DIRECTORY}" -type f) + done } find_files_with_retention() { - while read -r FILE; do + find "${DIRECTORY}" -type f | while read -r FILE; do RETENTION_FILE=$(get_xattr_of_file "${FILE}" "trusted.worm.attr" | grep -o '[0-9]\+$') if [ "${RETENTION_FILE}" == "" ]; then continue @@ -72,12 +72,12 @@ find_files_with_retention() { if [ ${RETENTION_FILE} -eq ${RETENTION_SEARCH} ]; then echo "${FILE}" fi - done <<< $(find "${DIRECTORY}" -type f) + done } set_atime() { - for FILE in $(find_files_with_retention "${DIRECTORY}" "${RETENTION_SEARCH}"); do + find_files_with_retention "${DIRECTORY}" "${RETENTION_SEARCH}" | while read -r FILE; do if [ -f "${FILE}" ]; then ARCHIVE_TIME=$(get_xattr_of_file "${FILE}" "trusted.archive_time") NEW_ATIME=$((ARCHIVE_TIME+NEW_RETENTION)) @@ -89,7 +89,7 @@ set_atime() { show_atime_retention() { - while read -r FILE; do + find "${DIRECTORY}" -type f | while read -r FILE; do ATIME=$(get_atime_of_file "${FILE}") if [ "${ATIME}" == "" ]; then continue @@ -98,7 +98,7 @@ show_atime_retention() { echo "${FILE}" echo " atime: $(date --date=@${ATIME}) || ${ATIME}" echo " trusted.worm.attr: ${TRUSTED_WORM_ATTR}" - done <<< $(find "${DIRECTORY}" -type f) + done } ################################################################################ -- 2.39.5