+ done
+
+ # Collect ancestor directories of all included paths (for traversal rules)
+ declare -A TRAVERSAL_DIRS
+ for INC in "${DRIVE_INCLUDES[@]}"; do
+ dir="$INC"
+ while [[ "$dir" != "/" ]]; do
+ dir="$(dirname "$dir")"
+ [[ "$dir" != "/" ]] && TRAVERSAL_DIRS["$dir"]=1
+ done
+ done
+
+ # Sort traversal dirs shallowest first so rules are ordered correctly
+ SORTED_TRAVERSAL=()
+ while IFS= read -r dir; do
+ SORTED_TRAVERSAL+=("$dir")
+ done < <(printf '%s\n' "${!TRAVERSAL_DIRS[@]}" | awk '{ print length, $0 }' | sort -n | cut -d' ' -f2-)
+ unset TRAVERSAL_DIRS
+
+ # Build rsync args array
+ FLAGS="-Par"
+ [[ $TEST -eq 1 ]] && FLAGS+="vn"
+ [[ $DEBUG -eq 0 ]] && FLAGS+="q"
+
+ RSYNC_ARGS=("$FLAGS" --delete --delete-excluded)
+
+ # Traversal rules: allow rsync to enter ancestor directories
+ for dir in "${SORTED_TRAVERSAL[@]}"; do
+ RSYNC_ARGS+=(--include="/${dir#/}/")
+ done
+
+ # Special includes: paths inside excluded dirs must appear before their exclusion rule
+ for INC in "${SPECIAL_INCLUDES[@]}"; do
+ rel="${INC#/}"
+ RSYNC_ARGS+=(--include="/$rel")
+ [[ -d "$INC" ]] && RSYNC_ARGS+=(--include="/$rel/**")
+ done
+
+ # Exclusion rules — only for paths that fall within this drive's includes
+ for EXC in "${!EXCLUDE_PATHS[@]}"; do
+ for INC in "${DRIVE_INCLUDES[@]}"; do
+ if [[ "$EXC" == "$INC"/* ]]; then
+ RSYNC_ARGS+=(--exclude="/${EXC#/}/**")
+ break
+ fi
+ done
+ done
+
+ # Regular includes
+ for INC in "${REGULAR_INCLUDES[@]}"; do
+ rel="${INC#/}"
+ if [[ -d "$INC" ]]; then
+ RSYNC_ARGS+=(--include="/$rel/") # the directory itself (needed for traversal)
+ RSYNC_ARGS+=(--include="/$rel/**")
+ else
+ RSYNC_ARGS+=(--include="/$rel")
+ fi
+ done
+
+ # Exclude everything not explicitly included, source, destination
+ RSYNC_ARGS+=(--exclude='*' / "$MOUNT_POINT/")