Fix replace-file to use cp for all file operations

- Write to /home/cyberpanel temp first (no user permission issues)
- Copy from /home/cyberpanel to user directory using executioner
- Use cp instead of mv for final file replacement (more reliable)
- Clean up temp files after successful operations
- Fixes: File corruption due to failed mv command via lscpd
This commit is contained in:
usmannasir 2025-10-26 15:35:38 +05:00
parent 993c68441a
commit 8ef95418a2
1 changed files with 12 additions and 5 deletions

View File

@ -1297,17 +1297,24 @@ def scanner_replace_file(request):
chmod_cmd = f'chmod {permissions} "{user_temp_path}"' chmod_cmd = f'chmod {permissions} "{user_temp_path}"'
ProcessUtilities.executioner(chmod_cmd, user=user) ProcessUtilities.executioner(chmod_cmd, user=user)
# Atomic rename # Replace file using cp instead of mv (more reliable)
mv_cmd = f'mv "{user_temp_path}" "{full_path}"' # cp preserves the temp file in case of issues
mv_result = ProcessUtilities.executioner(mv_cmd, user=user) cp_cmd = f'cp "{user_temp_path}" "{full_path}"'
cp_result = ProcessUtilities.executioner(cp_cmd, user=user)
# executioner returns 1 for success, 0 for failure # executioner returns 1 for success, 0 for failure
if mv_result != 1: if cp_result != 1:
error_msg = 'Failed to replace file with cp command'
logging.writeToFile(f'[API] {error_msg}, cp_result={cp_result}')
# Cleanup temp file # Cleanup temp file
ProcessUtilities.executioner(f'rm -f "{user_temp_path}"', user=user) ProcessUtilities.executioner(f'rm -f "{user_temp_path}"', user=user)
log_file_operation(scan_id, 'replace', file_path, False, 'Failed to replace file', backup_path=backup_path, request=request) log_file_operation(scan_id, 'replace', file_path, False, error_msg, backup_path=backup_path, request=request)
return JsonResponse({'success': False, 'error': 'Failed to replace file', 'error_code': 'REPLACE_FAILED'}, status=500) return JsonResponse({'success': False, 'error': 'Failed to replace file', 'error_code': 'REPLACE_FAILED'}, status=500)
# Clean up temp file after successful copy
ProcessUtilities.executioner(f'rm -f "{user_temp_path}"', user=user)
logging.writeToFile(f'[API] Replaced {full_path} with new content')
# Calculate new hash # Calculate new hash
cat_cmd = f'cat "{full_path}"' cat_cmd = f'cat "{full_path}"'
result = ProcessUtilities.outputExecutioner(cat_cmd, user=user, retRequired=True) result = ProcessUtilities.outputExecutioner(cat_cmd, user=user, retRequired=True)