From 804229a9bc6493f9e7e34f9cf4d31130db77e873 Mon Sep 17 00:00:00 2001 From: Mahmoud Ashraf <182176867+SNO7E-G@users.noreply.github.com> Date: Thu, 23 Jul 2026 22:24:49 +0500 Subject: [PATCH] Fix translation count and find precedence in download Two issues in download_translations' extraction step, same block: - moved_count was incremented inside a `find ... | while read` pipeline, which runs in a subshell, so the parent counter never changed and the "Moved N files" log always reported 0. Feed the loop with process substitution so the count survives. - `find -type f -name "*.json" -o -name "*.po" ...` bound -type f only to the first -name, so a directory named like a match could slip through. Group the -name alternatives with \( \) so -type f applies to all of them. Success/failure and cleanup behaviour is preserved via a move_failed flag. --- ptc-cli.sh | 55 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/ptc-cli.sh b/ptc-cli.sh index 747db3b..92d87f1 100755 --- a/ptc-cli.sh +++ b/ptc-cli.sh @@ -2496,11 +2496,17 @@ download_translations() { fi log_debug "Moving translation files to target directory..." local moved_count=0 - if find "$temp_extract_dir" -type f -name "*.json" -o -name "*.po" -o -name "*.pot" -o -name "*.mo" -o -name "*.yml" -o -name "*.yaml" 2>/dev/null | while read -r file; do + local move_failed=false + # The -name alternatives are grouped with \( \) so -type f applies + # to every one of them (without the group it binds only to the + # first). The loop is fed by process substitution rather than a + # `find | while` pipe: a pipe runs the loop in a subshell, so + # moved_count would never survive and always read 0. + while IFS= read -r file; do local filename=$(basename "$file") local target_file="$target_dir/$filename" log_debug "Moving: $filename → $target_file" - + # Check if target file already exists if [[ -f "$target_file" ]]; then log_debug "Overwriting existing file: $target_file" @@ -2508,39 +2514,36 @@ download_translations() { log_info "Overwriting: $filename" fi fi - - if mv "$file" "$target_file" 2>/dev/null; then - # Verify the move was successful - if [[ -f "$target_file" ]]; then - local final_size=$(stat -f%z "$target_file" 2>/dev/null || stat -c%s "$target_file" 2>/dev/null || echo "unknown") - log_debug "Successfully moved $filename ($final_size bytes)" - if [[ "$PTC_VERBOSE" == "true" ]]; then - log_info " ✓ $filename ($final_size bytes)" - fi - moved_count=$((moved_count + 1)) - else - log_warning "File move reported success but target file not found: $target_file" - return 1 + + if mv "$file" "$target_file" 2>/dev/null && [[ -f "$target_file" ]]; then + local final_size=$(stat -f%z "$target_file" 2>/dev/null || stat -c%s "$target_file" 2>/dev/null || echo "unknown") + log_debug "Successfully moved $filename ($final_size bytes)" + if [[ "$PTC_VERBOSE" == "true" ]]; then + log_info " ✓ $filename ($final_size bytes)" fi + moved_count=$((moved_count + 1)) else log_warning "Failed to move $filename to $target_file" - return 1 + move_failed=true + break fi - done; then - log_debug "Moved $moved_count translation files successfully" - if [[ "$PTC_VERBOSE" == "true" ]]; then - log_info "Successfully moved $moved_count files" - fi - log_success "Translations unpacked successfully to $target_dir" - log_debug "Cleaning up temporary files..." - rm -rf "$temp_extract_dir" "$temp_zip" - return 0 - else + done < <(find "$temp_extract_dir" -type f \( -name "*.json" -o -name "*.po" -o -name "*.pot" -o -name "*.mo" -o -name "*.yml" -o -name "*.yaml" \) 2>/dev/null) + + if [[ "$move_failed" == "true" ]]; then log_error "Failed to move translation files to target directory" log_debug "Cleaning up temporary files after failure..." rm -rf "$temp_extract_dir" "$temp_zip" return 1 fi + + log_debug "Moved $moved_count translation files successfully" + if [[ "$PTC_VERBOSE" == "true" ]]; then + log_info "Successfully moved $moved_count files" + fi + log_success "Translations unpacked successfully to $target_dir" + log_debug "Cleaning up temporary files..." + rm -rf "$temp_extract_dir" "$temp_zip" + return 0 else log_error "Failed to extract translations ZIP" rm -rf "$temp_extract_dir" "$temp_zip"