Skip to content
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ processTheMoves() {
REMAINING_UNATTENDED_FOLDERS=$TOTALUNATTENDEDFOLDERS

# Dry mode info:
if [ $TESTMODE = "yes" ]; then
if [ "$TESTMODE" = "yes" ]; then
mvlogger "Warning: Test Mode: yes, running $MOVE_FILES_TOOL in dry-mode for moving $([ "$MOVE_FILES_TOOL" = "rsync" ] && echo 'and syncing')"
fi

Expand Down Expand Up @@ -1874,6 +1874,34 @@ processTheMoves() {
fi
fi

# Clean moved empty folders
if [[ "$ACTION" =~ "move" && "$FILETYPE" == "d" ]]; then
DIR="${SOURCE}/${FILEPATH}"

if [ -d "$DIR" ]; then
mvDebuglogger "Current folder: $DIR" "Clean Empty Moved Folder - Start Loop"

if [ -e "${DIR}/.placeholder" ]; then
file_count=".placeholder"
else
file_count=$(find "$DIR" -type f | wc -l)
fi
# Count subdirectories
dir_count=$(find "$DIR" -mindepth 1 -type d | wc -l)

if [ "$file_count" -eq 0 ] && [ "$dir_count" -eq 0 ] && [ "$file_count" != ".placeholder" ]; then
if [ "$TESTMODE" = "no" ]; then
mvlogger "Deleting moved empty folder: ${DIR}"
rmdir "$DIR"
else
mvlogger "TEST MODE: Would delete moved empty folder: ${DIR}"
fi
else
mvlogger "Not deleting folder: contains $file_count file(s), $dir_count subfolder(s): $DIR"
fi
Comment on lines 1898 to 1916

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The logic for handling .placeholder files is buggy and will cause the script to fail. When a .placeholder file is found, file_count is set to the string ".placeholder". The subsequent condition [ "$file_count" -eq 0 ] then attempts an arithmetic comparison on a string, which results in an integer expression expected error. The logic should be refactored to correctly handle the .placeholder case separately to avoid this runtime error.

Suggested change
if [ -e "${DIR}/.placeholder" ]; then
file_count=".placeholder"
else
file_count=$(find "$DIR" -type f | wc -l)
fi
# Count subdirectories
dir_count=$(find "$DIR" -mindepth 1 -type d | wc -l)
if [ "$file_count" -eq 0 ] && [ "$dir_count" -eq 0 ] && [ "$file_count" != ".placeholder" ]; then
if [ "$TESTMODE" = "no" ]; then
mvlogger "Deleting moved empty folder: ${DIR}"
rmdir "$DIR"
else
mvlogger "TEST MODE: Would delete moved empty folder: ${DIR}"
fi
else
mvlogger "Not deleting folder: contains $file_count file(s), $dir_count subfolder(s): $DIR"
fi
if [ -e "${DIR}/.placeholder" ]; then
mvlogger "Not deleting folder: contains .placeholder file: $DIR"
else
file_count=$(find "$DIR" -type f | wc -l)
# Count subdirectories
dir_count=$(find "$DIR" -mindepth 1 -type d | wc -l)
if [ "$file_count" -eq 0 ] && [ "$dir_count" -eq 0 ]; then
if [ "$TESTMODE" = "no" ]; then
mvlogger "Deleting moved empty folder: ${DIR}"
rmdir "$DIR"
else
mvlogger "TEST MODE: Would delete moved empty folder: ${DIR}"
fi
else
mvlogger "Not deleting folder: contains $file_count file(s), $dir_count subfolder(s): $DIR"
fi
fi

fi
fi

# Setting mover status
if [[ "$ACTION" =~ "unattended" ]]; then
REMAINING_UNATTENDED_SIZE=$((REMAINING_UNATTENDED_SIZE - FILESIZE))
Expand All @@ -1885,29 +1913,41 @@ processTheMoves() {
fi

if [[ "$ACTION" =~ "unattended to secondary" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to secondary ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
# Check if there are any files or folders left
if [[ "$FILETYPE" = "f" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to secondary ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
else
mvlogger "$REMAINING_UNATTENDED_FOLDERS folders remaining from unattended to secondary"
fi
elif [[ "$ACTION" =~ "unattended to primary" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to primary ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
# Check if there are any files or folders left
if [[ "$FILETYPE" = "f" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to primary ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
else
mvlogger "$REMAINING_UNATTENDED_FOLDERS folders remaining from unattended to primary"
fi
fi
Comment on lines 1930 to 1944

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This block of code contains duplicated logic for logging messages based on FILETYPE. This can be simplified by extracting the common parts and using a variable for the differing part (primary vs secondary), which improves maintainability and readability.

Suggested change
if [[ "$ACTION" =~ "unattended to secondary" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to secondary ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
# Check if there are any files or folders left
if [[ "$FILETYPE" = "f" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to secondary ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
else
mvlogger "$REMAINING_UNATTENDED_FOLDERS folders remaining from unattended to secondary"
fi
elif [[ "$ACTION" =~ "unattended to primary" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to primary ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
# Check if there are any files or folders left
if [[ "$FILETYPE" = "f" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to primary ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
else
mvlogger "$REMAINING_UNATTENDED_FOLDERS folders remaining from unattended to primary"
fi
fi
local direction
if [[ "$ACTION" =~ "unattended to secondary" ]]; then
direction="secondary"
elif [[ "$ACTION" =~ "unattended to primary" ]]; then
direction="primary"
fi
if [[ -n "$direction" ]]; then
# Check if there are any files or folders left
if [[ "$FILETYPE" = "f" ]]; then
mvlogger "$REMAINING_UNATTENDED_FILES files remaining from unattended to $direction ($(bytes_to_human "$REMAINING_UNATTENDED_SIZE"))"
else
mvlogger "$REMAINING_UNATTENDED_FOLDERS folders remaining from unattended to $direction"
fi
fi

else
if [[ "$SOURCE" =~ $PRIMARYSTORAGENAME ]]; then
REMAINING_CACHE_SIZE=$((REMAINING_CACHE_SIZE - FILESIZE))
# Check if there are any files or folders left
if [[ "$FILETYPE" = "f" ]]; then
REMAINING_CACHE_FILES=$((REMAINING_CACHE_FILES - NBLINKS))
mvlogger "$REMAINING_CACHE_FILES files remaining from caches to array ($(bytes_to_human "$REMAINING_CACHE_SIZE"))"
else
REMAINING_CACHE_FOLDERS=$((REMAINING_CACHE_FOLDERS - 1))
mvlogger "$REMAINING_CACHE_FOLDERS folders remaining from caches to array"
fi
mvlogger "$REMAINING_CACHE_FILES files remaining from caches to array ($(bytes_to_human "$REMAINING_CACHE_SIZE"))"
else
REMAINING_ARRAY_SIZE=$((REMAINING_ARRAY_SIZE - FILESIZE))
# Check if there are any files or folders left
if [[ "$FILETYPE" = "f" ]]; then
REMAINING_ARRAY_FILES=$((REMAINING_ARRAY_FILES - NBLINKS))
mvlogger "$REMAINING_ARRAY_FILES files remaining from array to caches ($(bytes_to_human "$REMAINING_ARRAY_SIZE"))"
else
REMAINING_ARRAY_FOLDERS=$((REMAINING_ARRAY_FOLDERS - 1))
mvlogger "$REMAINING_ARRAY_FOLDERS folders remaining from array to caches"
fi
mvlogger "$REMAINING_ARRAY_FILES files remaining from array to caches ($(bytes_to_human "$REMAINING_ARRAY_SIZE"))"
fi
Comment on lines 1946 to 1966

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There is significant code duplication in this if/else block. The logic for decrementing counters and logging is repeated for both cache-to-array and array-to-cache movements. This can be refactored by using local variables to hold the context-specific values and then having a single block for logging. This will make the code more concise and easier to maintain.

                local log_msg
                if [[ "$SOURCE" =~ $PRIMARYSTORAGENAME ]]; then
                    REMAINING_CACHE_SIZE=$((REMAINING_CACHE_SIZE - FILESIZE))
                    # Check if there are any files or folders left
                    if [[ "$FILETYPE" = "f" ]]; then
                        REMAINING_CACHE_FILES=$((REMAINING_CACHE_FILES - NBLINKS))
                        log_msg="$REMAINING_CACHE_FILES files remaining from caches to array ($(bytes_to_human "$REMAINING_CACHE_SIZE"))"
                    else
                        REMAINING_CACHE_FOLDERS=$((REMAINING_CACHE_FOLDERS - 1))
                        log_msg="$REMAINING_CACHE_FOLDERS folders remaining from caches to array"
                    fi
                else
                    REMAINING_ARRAY_SIZE=$((REMAINING_ARRAY_SIZE - FILESIZE))
                    # Check if there are any files or folders left
                    if [[ "$FILETYPE" = "f" ]]; then
                        REMAINING_ARRAY_FILES=$((REMAINING_ARRAY_FILES - NBLINKS))
                        log_msg="$REMAINING_ARRAY_FILES files remaining from array to caches ($(bytes_to_human "$REMAINING_ARRAY_SIZE"))"
                    else
                        REMAINING_ARRAY_FOLDERS=$((REMAINING_ARRAY_FOLDERS - 1))
                        log_msg="$REMAINING_ARRAY_FOLDERS folders remaining from array to caches"
                    fi
                fi
                mvlogger "$log_msg"

fi
moverStatusWrite
Expand Down