diff --git a/Cargo.lock b/Cargo.lock index 72b7b00..662de4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "qft" -version = "0.1.11" +version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 631bb3d..4e984b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "qft" -version = "0.1.11" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index dafcc92..0d4a38a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::{ collections::HashMap, - fs::File, - io::{stdout, Error, Read, Write}, + fs::{File, OpenOptions}, + io::{stdout, Error, Read, Write, Seek, SeekFrom}, net::*, str::FromStr, thread, @@ -67,7 +67,7 @@ impl SafeReadWrite { panic!("bad buf length") } } - Err(x) => return Err(x), + Err(_) => {} } let mut buf = [0, 0]; match self.socket.recv(&mut buf).ok() { @@ -147,7 +147,7 @@ impl SafeReadWrite { panic!("internet down") } } - Err(_) => return self.socket, + Err(_) => {}, } let mut buf = [0, 0]; match self.socket.recv(&mut buf).ok() { @@ -227,6 +227,7 @@ fn sender(args: &Vec) { let connection = holepunch(args); let br = u32::from_str_radix(args.get(5).unwrap_or(&"256".to_string()), 10) .expect("This is not a correct number"); + let begin = args.get(6).map(|s| u64::from_str_radix(s.as_str(), 10)).unwrap_or(Ok(0)).expect("bad begin operand"); let mut buf: Vec = Vec::new(); buf.resize(br as usize, 0); let mut buf = buf.leak(); @@ -236,6 +237,12 @@ fn sender(args: &Vec) { })) .expect("file not readable"); + if begin != 0 { + println!("Skipping to {}...", begin); + file.seek(SeekFrom::Start(begin)).expect("unable to skip"); + println!("Done."); + } + let mut sc = SafeReadWrite::new(connection); let mut bytes_sent: u64 = 0; loop { @@ -261,15 +268,22 @@ fn receiver(args: &Vec) { let connection = holepunch(args); let br = u32::from_str_radix(args.get(5).unwrap_or(&"256".to_string()), 10) .expect("This is not a correct number"); + let begin = args.get(6).map(|s| u64::from_str_radix(s.as_str(), 10)).unwrap_or(Ok(0)).expect("bad begin operand"); let mut buf: Vec = Vec::new(); buf.resize(br as usize, 0); let mut buf: &[u8] = buf.leak(); - let mut file = File::create(args.get(4).unwrap_or_else(|| { + let mut file = OpenOptions::new().truncate(false).open(args.get(4).unwrap_or_else(|| { print_args(args); panic!("unreachable") })) .expect("file not writable"); + if begin != 0 { + println!("Skipping to {}...", begin); + file.seek(SeekFrom::Start(begin)).expect("unable to skip"); + println!("Done."); + } + let mut sc = SafeReadWrite::new(connection); let mut bytes_received: u64 = 0; loop { @@ -358,8 +372,8 @@ fn print_args(args: &Vec) { println!( "No arguments. Needed: \n\ | {} helper \n\ - | {} sender : [bitrate]\n\ - | {} receiver : [bitrate]\n", + | {} sender : [bitrate] [skip]\n\ + | {} receiver : [bitrate] [skip]\n", f, f, f ); panic!("No arguments");