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 ...@@ -314,30 +314,54 @@ exit
last_error = None last_error = None
# Try each directory until we find one that works # Try each directory until we find one that works
for temp_dir in candidate_dirs: self.status.emit("Attempting to create diskpart script...")
if not temp_dir or not os.path.exists(temp_dir): 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 continue
try: try:
candidate_path = os.path.join(temp_dir, script_filename) 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 # Try to write script file with explicit encoding
with open(candidate_path, 'w') as script_file: with open(candidate_path, 'w', encoding='utf-8') as script_file:
script_file.write(diskpart_script) script_file.write(diskpart_script)
# Verify file exists and is readable # Verify file exists and is readable
if os.path.exists(candidate_path): if os.path.exists(candidate_path) and os.path.isfile(candidate_path):
script_path = candidate_path # Verify we can read the file back
self.status.emit(f"Created diskpart script: {script_path}") with open(candidate_path, 'r', encoding='utf-8') as verify_file:
break 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: except Exception as e:
self.status.emit(f"Directory {i+1}: Failed - {e}")
last_error = e last_error = e
continue continue
if not script_path: if not script_path:
raise Exception(f"Failed to create diskpart script in any accessible directory. Last error: {last_error}") 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: try:
# Run diskpart to format the USB drive # Run diskpart to format the USB drive
self.status.emit("Formatting 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