From 66f479e8bc282b1626636f585b404ab7e91a1e83 Mon Sep 17 00:00:00 2001 From: altugbakan Date: Mon, 13 Mar 2023 20:05:24 +0300 Subject: [PATCH] Add better error handling and messages --- Cargo.lock | 2 +- Cargo.toml | 4 ++-- src/packet.rs | 10 ++++++++-- src/server.rs | 6 +++--- src/worker.rs | 12 +++++++++--- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec4a50b..41f82d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "tftpd" -version = "0.1.3" +version = "0.1.4" diff --git a/Cargo.toml b/Cargo.toml index b919529..fa6af94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,10 @@ [package] name = "tftpd" -version = "0.1.3" +version = "0.1.4" authors = ["Altuğ Bakan "] edition = "2021" description = "Multithreaded TFTP server daemon" repository = "https://github.com/altugbakan/rs-tftpd" license = "MIT" keywords = ["tftp", "server"] -categories = ["command-line-utilities"] \ No newline at end of file +categories = ["command-line-utilities"] diff --git a/src/packet.rs b/src/packet.rs index 0e96915..d551af2 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -333,8 +333,14 @@ fn parse_ack(buf: &[u8]) -> Result> { fn parse_error(buf: &[u8]) -> Result> { let code = ErrorCode::from_u16(Convert::to_u16(&buf[2..])?)?; - let (msg, _) = Convert::to_string(buf, 4)?; - Ok(Packet::Error { code, msg }) + if let Ok((msg, _)) = Convert::to_string(buf, 4) { + Ok(Packet::Error { code, msg }) + } else { + Ok(Packet::Error { + code, + msg: "(no message)".to_string(), + }) + } } fn serialize_data(block_num: &u16, data: &Vec) -> Vec { diff --git a/src/server.rs b/src/server.rs index 6af716a..9d359e4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -49,7 +49,7 @@ impl Server { } => { println!("Sending {filename} to {from}"); if let Err(err) = self.handle_rrq(filename.clone(), &mut options, &from) { - eprintln!("{err}") + eprintln!("Error while sending file: {err}") } } Packet::Wrq { @@ -59,7 +59,7 @@ impl Server { } => { println!("Receiving {filename} from {from}"); if let Err(err) = self.handle_wrq(filename.clone(), &mut options, &from) { - eprintln!("{err}") + eprintln!("Error while receiving file: {err}") } } _ => { @@ -69,7 +69,7 @@ impl Server { ErrorCode::IllegalOperation, "invalid request".to_string(), ) - .unwrap_or_else(|err| eprintln!("{err}")); + .unwrap_or_else(|_| eprintln!("Received invalid request")); } }; } diff --git a/src/worker.rs b/src/worker.rs index a324492..cc07884 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -1,6 +1,6 @@ use std::{ error::Error, - fs::File, + fs::{self, File}, io::{Read, Write}, net::{SocketAddr, UdpSocket}, path::{Path, PathBuf}, @@ -95,6 +95,12 @@ impl Worker { if let Err(err) = handle_receive() { eprintln!("{err}"); + if fs::remove_file(&file_path).is_err() { + eprintln!( + "Error while cleaning {}", + file_path.file_name().unwrap().to_str().unwrap() + ); + } } }); } @@ -143,7 +149,7 @@ fn send_file( println!( "Sent {} to {}", - file_path.display(), + file_path.file_name().unwrap().to_str().unwrap(), socket.peer_addr().unwrap() ); Ok(()) @@ -198,7 +204,7 @@ fn receive_file( println!( "Received {} from {}", - file_path.display(), + file_path.file_name().unwrap().to_str().unwrap(), socket.peer_addr().unwrap() ); Ok(())