Add comprehensive debugging for diskpart script creation issues

- Add detailed status messages for each directory attempt
- Explicit UTF-8 encoding for script file creation
- Verify file exists and has content after creation
- Enhanced error reporting with specific failure reasons
- Better handling of permission errors vs other exceptions
- Final verification before using script path
- Will help identify exactly where script creation fails
parent f4d916ba
......@@ -314,30 +314,54 @@ exit
last_error = None
# Try each directory until we find one that works
for temp_dir in candidate_dirs:
if not temp_dir or not os.path.exists(temp_dir):
self.status.emit("Attempting to create diskpart script...")
for i, temp_dir in enumerate(candidate_dirs):
if not temp_dir:
self.status.emit(f"Directory {i+1}: Empty directory path, skipping")
continue
if not os.path.exists(temp_dir):
self.status.emit(f"Directory {i+1}: {temp_dir} does not exist, skipping")
continue
try:
candidate_path = os.path.join(temp_dir, script_filename)
self.status.emit(f"Directory {i+1}: Trying to create script at {candidate_path}")
# Try to write script file
with open(candidate_path, 'w') as script_file:
# Try to write script file with explicit encoding
with open(candidate_path, 'w', encoding='utf-8') as script_file:
script_file.write(diskpart_script)
# Verify file exists and is readable
if os.path.exists(candidate_path):
script_path = candidate_path
self.status.emit(f"Created diskpart script: {script_path}")
break
if os.path.exists(candidate_path) and os.path.isfile(candidate_path):
# Verify we can read the file back
with open(candidate_path, 'r', encoding='utf-8') as verify_file:
content = verify_file.read()
if content.strip(): # File has content
script_path = candidate_path
self.status.emit(f"SUCCESS: Created diskpart script at {script_path} (Size: {len(content)} chars)")
break
else:
self.status.emit(f"Directory {i+1}: File created but empty, trying next...")
else:
self.status.emit(f"Directory {i+1}: File creation appeared successful but file not found")
except PermissionError as e:
self.status.emit(f"Directory {i+1}: Permission denied - {e}")
last_error = e
continue
except Exception as e:
self.status.emit(f"Directory {i+1}: Failed - {e}")
last_error = e
continue
if not script_path:
raise Exception(f"Failed to create diskpart script in any accessible directory. Last error: {last_error}")
# Final verification before using the script
if not os.path.exists(script_path):
raise Exception(f"Diskpart script was created but is no longer accessible at {script_path}")
try:
# Run diskpart to format the USB drive
self.status.emit("Formatting USB drive...")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment