diff --git a/usr/local/bin/torrent-mover b/usr/local/bin/torrent-mover index 30ce6b1..5e5fceb 100755 --- a/usr/local/bin/torrent-mover +++ b/usr/local/bin/torrent-mover @@ -120,7 +120,10 @@ main() { torrent_ids=$(get_torrents) log_debug "Found $(echo "$torrent_ids" | wc -l) torrents" - echo "$torrent_ids" | while read -r id; do + # Use a regular for loop instead of a pipe to while + # to avoid the subshell issue that causes processed_source_dirs to be lost + readarray -t torrent_ids_array <<< "$torrent_ids" + for id in "${torrent_ids_array[@]}"; do # Skip empty IDs if [[ -z "$id" ]]; then log_debug "Skipping empty torrent ID" @@ -244,6 +247,14 @@ main() { fi done + # Print count of processed directories + if [[ "${DEBUG}" -eq 1 ]]; then + log_debug "Processed source directories count: ${#processed_source_dirs[@]}" + for dir in "${!processed_source_dirs[@]}"; do + log_debug "Processed directory: $dir" + done + fi + # Check disk usage for all directories for dir in "${REQUIRED_DIRS[@]}"; do check_disk_usage "${dir}" diff --git a/usr/local/lib/torrent-mover/transmission_handler.sh b/usr/local/lib/torrent-mover/transmission_handler.sh index 04cae11..711c792 100644 --- a/usr/local/lib/torrent-mover/transmission_handler.sh +++ b/usr/local/lib/torrent-mover/transmission_handler.sh @@ -13,18 +13,36 @@ get_destination() { # Check if path is already in the cache if [[ -n "${PATH_CACHE["${source_path}"]+x}" ]]; then - echo "${PATH_CACHE["${source_path}"]}" + local cached_destination="${PATH_CACHE["${source_path}"]}" + log_debug "Using cached destination for ${source_path}: ${cached_destination}" + echo "${cached_destination}" return fi # Skip recursive path analysis - only log once if [[ "${source_path}" =~ ^/mnt/dsnas1/ ]]; then # Already in destination format, return as is + log_debug "Path already in destination format: ${source_path}" PATH_CACHE["${source_path}"]="${source_path}" echo "${source_path}" return fi + # For paths in dsnas2, check if they map to same structure in dsnas1 + if [[ "${source_path}" =~ ^/mnt/dsnas2/ ]]; then + local dir_suffix="${source_path#/mnt/dsnas2/}" + local potential_dest="/mnt/dsnas1/${dir_suffix}" + + # If the directories match exactly in structure, only on different mounts, + # return the source to avoid needless copying + if [[ -d "${potential_dest}" ]]; then + log_debug "Path maps to same structure on different mount: ${source_path} -> ${source_path}" + PATH_CACHE["${source_path}"]="${source_path}" + echo "${source_path}" + return + fi + fi + log_info "Analyzing path: ${source_path}" local destination="${DEFAULT_DST}"