aboutsummaryrefslogtreecommitdiff
path: root/src/tftpc.rs
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2019-03-25 01:40:14 +0100
committerReiner Herrmann <reiner@reiner-h.de>2019-03-25 01:40:14 +0100
commitc790e9b6013cfe86c81f5d8068feefff4339abf7 (patch)
tree2ff554b692128779cf0bd9f3fdd8aaed0984130d /src/tftpc.rs
parent01838bd4e8a208ef0d76181e1b5875421b7803a2 (diff)
Simplify error handling in some places
Diffstat (limited to 'src/tftpc.rs')
-rw-r--r--src/tftpc.rs60
1 files changed, 15 insertions, 45 deletions
diff --git a/src/tftpc.rs b/src/tftpc.rs
index 24073d2..240ef2c 100644
--- a/src/tftpc.rs
+++ b/src/tftpc.rs
@@ -65,35 +65,23 @@ impl Tftpc {
fn wait_for_option_ack(&mut self, sock: &UdpSocket) -> Option<SocketAddr> {
let mut buf = [0; 512];
- match sock.peek_from(&mut buf) {
- Ok(_) => (),
- Err(_) => return None,
- };
+ sock.peek_from(&mut buf).ok()?;
let opcode = u16::from_be_bytes([buf[0], buf[1]]);
if opcode != rtftp::Opcode::OACK as u16 {
return None;
}
- let (len, remote) = match sock.recv_from(&mut buf) {
- Ok(args) => args,
- Err(_) => return None,
- };
+ let (len, remote) = sock.recv_from(&mut buf).ok()?;
let mut options = self.tftp.parse_options(&buf[2..len]);
- match self.tftp.init_tftp_options(&sock, &mut options) {
- Ok(_) => {}
- Err(_) => return None,
- }
+ self.tftp.init_tftp_options(&sock, &mut options).ok()?;
Some(remote)
}
fn wait_for_response(&self, sock: &UdpSocket, expected_opcode: rtftp::Opcode, expected_block: u16, expected_remote: Option<SocketAddr>) -> Result<Option<SocketAddr>, std::io::Error> {
let mut buf = [0; 4];
- let (len, remote) = match sock.peek_from(&mut buf) {
- Ok(args) => args,
- Err(err) => return Err(err),
- };
+ let (len, remote) = sock.peek_from(&mut buf)?;
if let Some(rem) = expected_remote {
/* verify we got a response from the same client that sent
@@ -140,24 +128,14 @@ impl Tftpc {
}
fn handle_wrq(&mut self, sock: &UdpSocket) -> Result<String, io::Error> {
- let mut file = match File::open(self.conf.filename.as_path()) {
- Ok(f) => f,
- Err(err) => return Err(err),
- };
- let err_invalidpath = io::Error::new(io::ErrorKind::InvalidInput, "Invalid path/filename");
- let filename = match self.conf.filename.file_name() {
- Some(f) => match f.to_str() {
- Some(s) => s,
- None => return Err(err_invalidpath),
- },
- None => return Err(err_invalidpath),
- };
- let metadata = match file.metadata() {
- Ok(m) => m,
- Err(_) => return Err(err_invalidpath),
- };
+ let mut file = File::open(self.conf.filename.as_path())?;
+ let err_invalidpath = || io::Error::new(io::ErrorKind::InvalidInput, "Invalid path/filename");
+
+ let filename = self.conf.filename.file_name().ok_or(err_invalidpath())?
+ .to_str().ok_or(err_invalidpath())?;
+ let metadata = file.metadata().map_err(|_| err_invalidpath())?;
if !metadata.is_file() {
- return Err(err_invalidpath);
+ return Err(err_invalidpath());
}
let tsize = self.tftp.transfersize(&mut file)?;
@@ -192,19 +170,11 @@ impl Tftpc {
}
fn handle_rrq(&mut self, sock: &UdpSocket) -> Result<String, io::Error> {
- let filename = match self.conf.filename.file_name() {
- Some(f) => f,
- None => return Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid path/filename")),
- };
+ let err_invalidpath = || io::Error::new(io::ErrorKind::InvalidInput, "Invalid path/filename");
+ let filename = self.conf.filename.file_name().ok_or(err_invalidpath())?;
let outpath = env::current_dir().expect("Can't get current directory").join(filename);
- let mut file = match File::create(outpath) {
- Ok(f) => f,
- Err(err) => return Err(err),
- };
- let filename = match self.conf.filename.to_str() {
- Some(f) => f,
- None => return Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid path/filename")),
- };
+ let mut file = File::create(outpath)?;
+ let filename = self.conf.filename.to_str().ok_or(err_invalidpath())?;
let buf = self.init_req(rtftp::Opcode::RRQ, filename, 0);