aboutsummaryrefslogtreecommitdiff
path: root/src/tftpc.rs
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2019-03-08 18:41:04 +0100
committerReiner Herrmann <reiner@reiner-h.de>2019-03-08 18:41:04 +0100
commit6694875f8cd850ae0a09c88790921c47d7bd2bfe (patch)
tree277c340924a166d242179c9195f3fc51d381a1bc /src/tftpc.rs
parent66ef7d94f59c91618acf2e757307385551a35846 (diff)
Display transfer progress in percentage
Diffstat (limited to 'src/tftpc.rs')
-rw-r--r--src/tftpc.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/tftpc.rs b/src/tftpc.rs
index c6b4c0f..689d055 100644
--- a/src/tftpc.rs
+++ b/src/tftpc.rs
@@ -5,7 +5,7 @@
use std::env;
use std::fs::File;
-use std::io;
+use std::io::{self, Write};
use std::net::{SocketAddr, ToSocketAddrs, UdpSocket};
use std::path::{Path, PathBuf};
use std::time::Duration;
@@ -32,6 +32,26 @@ struct Tftpc {
conf: Configuration,
}
+fn update_progress(current: u64, total: u64, last: u64) -> u64 {
+ if total == 0 {
+ /* unknown; remote does not support tsize */
+ return 0;
+ }
+ let onepercent = total / 100;
+ if current < total && current < last + onepercent {
+ /* not enough progress to warrant an update */
+ return last;
+ }
+
+ let percent = 100 * current / total;
+ print!("\r {}% ", percent);
+ io::stdout().flush().expect("flushing stdout failed");
+ if current == total {
+ print!("\r");
+ }
+ current
+}
+
impl Tftpc {
pub fn new(conf: Configuration) -> Tftpc {
Tftpc {
@@ -207,6 +227,7 @@ impl Tftpc {
}
pub fn start(&mut self) {
+ self.tftp.set_progress_callback(update_progress);
let socket = UdpSocket::bind("[::]:0").expect("binding failed");
socket.set_read_timeout(Some(Duration::from_secs(5))).expect("setting socket timeout failed");