From a3dd7fecb4c9fb747933c71b85a86757e9f40228 Mon Sep 17 00:00:00 2001 From: altugbakan Date: Sat, 11 Mar 2023 20:47:52 +0300 Subject: [PATCH] Refactor file path --- src/server.rs | 29 ++++++----------------------- src/worker.rs | 32 ++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/server.rs b/src/server.rs index 1c1e72f..852fddb 100644 --- a/src/server.rs +++ b/src/server.rs @@ -65,7 +65,8 @@ impl Server { options: &mut Vec, to: &SocketAddr, ) -> Result<(), Box> { - match check_file_exists(&get_full_path(&filename, &self.directory), &self.directory) { + let file_path = &self.directory.join(&filename); + match check_file_exists(&file_path, &self.directory) { ErrorCode::FileNotFound => { return Message::send_error_to( &self.socket, @@ -85,7 +86,7 @@ impl Server { ErrorCode::FileExists => Worker::send( self.socket.local_addr().unwrap(), *to, - filename, + file_path.to_path_buf(), options.to_vec(), ), _ => {} @@ -100,7 +101,8 @@ impl Server { options: &mut Vec, to: &SocketAddr, ) -> Result<(), Box> { - match check_file_exists(&get_full_path(&filename, &self.directory), &self.directory) { + let file_path = &self.directory.join(&filename); + match check_file_exists(&file_path, &self.directory) { ErrorCode::FileExists => { return Message::send_error_to( &self.socket, @@ -120,7 +122,7 @@ impl Server { ErrorCode::FileNotFound => Worker::receive( self.socket.local_addr().unwrap(), *to, - filename, + file_path.to_path_buf(), options.to_vec(), ), _ => {} @@ -146,29 +148,10 @@ fn validate_file_path(file: &PathBuf, directory: &PathBuf) -> bool { !file.to_str().unwrap().contains("..") && file.ancestors().any(|a| a == directory) } -fn get_full_path(filename: &str, directory: &PathBuf) -> PathBuf { - let mut file = directory.clone(); - file.push(PathBuf::from(filename)); - file -} - #[cfg(test)] mod tests { use super::*; - #[test] - fn gets_full_path() { - assert_eq!( - get_full_path("test.txt", &PathBuf::from("/dir/test")), - PathBuf::from("/dir/test/test.txt") - ); - - assert_eq!( - get_full_path("some_dir/test.txt", &PathBuf::from("/dir/test")), - PathBuf::from("/dir/test/some_dir/test.txt") - ); - } - #[test] fn validates_file_path() { assert!(validate_file_path( diff --git a/src/worker.rs b/src/worker.rs index 60b6260..e8e617c 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -3,7 +3,7 @@ use std::{ fs::File, io::{Read, Write}, net::{SocketAddr, UdpSocket}, - path::Path, + path::{Path, PathBuf}, thread, time::Duration, }; @@ -35,16 +35,16 @@ impl Worker { pub fn send( addr: SocketAddr, remote: SocketAddr, - filename: String, + file_path: PathBuf, mut options: Vec, ) { thread::spawn(move || { let mut handle_send = || -> Result<(), Box> { let socket = setup_socket(&addr, &remote)?; - let work_type = WorkType::Send(Path::new(&filename).metadata().unwrap().len()); + let work_type = WorkType::Send(Path::new(&file_path).metadata().unwrap().len()); accept_request(&socket, &options, &work_type)?; check_response(&socket)?; - send_file(&socket, &filename, &mut options)?; + send_file(&socket, &file_path, &mut options)?; Ok(()) }; @@ -58,7 +58,7 @@ impl Worker { pub fn receive( addr: SocketAddr, remote: SocketAddr, - filename: String, + file_path: PathBuf, mut options: Vec, ) { thread::spawn(move || { @@ -66,7 +66,7 @@ impl Worker { let socket = setup_socket(&addr, &remote)?; let work_type = WorkType::Receive; accept_request(&socket, &options, &work_type)?; - receive_file(&socket, &filename, &mut options)?; + receive_file(&socket, &file_path, &mut options)?; Ok(()) }; @@ -80,10 +80,10 @@ impl Worker { fn send_file( socket: &UdpSocket, - filename: &String, + file_path: &PathBuf, options: &mut Vec, ) -> Result<(), Box> { - let mut file = File::open(filename).unwrap(); + let mut file = File::open(file_path).unwrap(); let worker_options = parse_options(options, &WorkType::Send(file.metadata().unwrap().len()))?; let mut block_number = 1; @@ -119,16 +119,20 @@ fn send_file( }; } - println!("Sent {filename} to {}", socket.peer_addr().unwrap()); + println!( + "Sent {} to {}", + file_path.display(), + socket.peer_addr().unwrap() + ); Ok(()) } fn receive_file( socket: &UdpSocket, - filename: &String, + file_path: &PathBuf, options: &mut Vec, ) -> Result<(), Box> { - let mut file = File::create(filename).unwrap(); + let mut file = File::create(file_path).unwrap(); let worker_options = parse_options(options, &WorkType::Receive)?; let mut block_number: u16 = 0; @@ -170,7 +174,11 @@ fn receive_file( }; } - println!("Received {filename} from {}", socket.peer_addr().unwrap()); + println!( + "Received {} from {}", + file_path.display(), + socket.peer_addr().unwrap() + ); Ok(()) }