diff --git a/crates/pfs-tftp/src/bin/tftp.rs b/crates/pfs-tftp/src/bin/tftp.rs index 955add2..1f0f082 100644 --- a/crates/pfs-tftp/src/bin/tftp.rs +++ b/crates/pfs-tftp/src/bin/tftp.rs @@ -248,30 +248,28 @@ fn main() -> ExitCode { } }; - // Create output file - let mut file = match File::create(&local_file) { - Ok(f) => f, + // Download to memory first - only create local file on success + let data = match client.get(&remote_file, args.mode) { + Ok(data) => data, Err(e) => { - eprintln!("Error creating file '{local_file}': {e}"); + eprintln!("Error: {e}"); return ExitCode::FAILURE; } }; - // Download - match client.get_to_writer(&remote_file, args.mode, &mut file) { - Ok(bytes) => { - if args.verbose { - eprintln!("Received {bytes} bytes"); - } - println!("Downloaded '{remote_file}' -> '{local_file}' ({bytes} bytes)"); - } - Err(e) => { - // Clean up partial file - let _ = std::fs::remove_file(&local_file); - eprintln!("Error: {e}"); - return ExitCode::FAILURE; - } + // Write to local file + if let Err(e) = std::fs::write(&local_file, &data) { + eprintln!("Error writing file '{local_file}': {e}"); + return ExitCode::FAILURE; } + + if args.verbose { + eprintln!("Received {} bytes", data.len()); + } + println!( + "Downloaded '{remote_file}' -> '{local_file}' ({} bytes)", + data.len() + ); } Command::Put {