rusync2 0.10.0

Maintained fork of rusync: adds --include/--exclude filters and a --sleep-interval watch mode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! console_info
//!
//! Display transfer progress to the command line

use crate::progress::{Progress, ProgressInfo, WalkInfo};
use crate::sync;
use anyhow::{Context, Error};
use colored::Colorize;
use humansize::{file_size_opts as options, FileSize};
use std::fs::OpenOptions;
use std::io;
use std::io::Write;
use std::path::Path;
use std::thread;
use std::time::Duration;
use std::time::Instant;
use terminal_size::{terminal_size, Width};

/// Minimum delay between two rendered progress lines. Rendering on every
/// event is both slow (terminal writes) and flickery; for trees with many
/// small files the events come in thousands per second.
const MIN_RENDER_INTERVAL: Duration = Duration::from_millis(100);

#[derive(Debug)]
pub struct ConsoleProgressInfo {
    err_file: Option<std::fs::File>,
    last_render: Option<Instant>,
    last_scan_render: Option<Instant>,
    line_dirty: bool,
}

impl ConsoleProgressInfo {
    pub fn new() -> Self {
        Self {
            err_file: None,
            last_render: None,
            last_scan_render: None,
            line_dirty: false,
        }
    }

    pub fn with_error_list_path(error_list_path: &Path) -> Result<Self, Error> {
        let err_file = OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(false)
            .open(error_list_path)
            .with_context(|| {
                format!("Could not open errfile at '{}'", error_list_path.display())
            })?;
        Ok(Self {
            err_file: Some(err_file),
            last_render: None,
            last_scan_render: None,
            line_dirty: false,
        })
    }

    /// Whether a transfer progress line should be rendered now; also
    /// updates the render clock. The first line is always rendered.
    fn should_render(&mut self) -> bool {
        let ready = match self.last_render {
            None => true,
            Some(last) => last.elapsed() >= MIN_RENDER_INTERVAL,
        };
        if ready {
            self.last_render = Some(Instant::now());
        }
        ready
    }

    /// Same as `should_render`, but for the scanning line, so that a busy
    /// transfer phase cannot starve the scanning display (and the first
    /// scanning line is always rendered).
    fn should_render_scan(&mut self) -> bool {
        let ready = match self.last_scan_render {
            None => true,
            Some(last) => last.elapsed() >= MIN_RENDER_INTERVAL,
        };
        if ready {
            self.last_scan_render = Some(Instant::now());
        }
        ready
    }

    fn erase_line_if_dirty(&mut self) {
        if self.line_dirty {
            erase_line();
            self.line_dirty = false;
        }
    }
}

impl ProgressInfo for ConsoleProgressInfo {
    fn done_syncing(&mut self) {
        self.erase_line_if_dirty();
    }

    fn start(&mut self, source: &str, destination: &str) {
        println!(
            "{} Syncing from {} to {}",
            "::".color("blue"),
            source.bold(),
            destination.bold()
        )
    }

    fn new_file(&mut self, _name: &str) {}

    fn scanning(&mut self, info: &WalkInfo) {
        if info.finished {
            self.erase_line_if_dirty();
            return;
        }
        if !self.should_render_scan() {
            return;
        }
        // Pad to the terminal width so that a previous, longer progress
        // line is fully overwritten:
        let line = format!(
            "{:<width$}",
            scanning_line(info),
            width = get_terminal_width()
        );
        print!("{}\r", line);
        let _ = io::stdout().flush();
        self.line_dirty = true;
    }

    fn progress(&mut self, progress: &Progress) {
        if !self.should_render() {
            return;
        }
        let eta_str = human_seconds(progress.eta);
        let speed_str = human_speed(progress.current_speed);
        let avg_str = format!("avg {}", human_speed(progress.average_speed));
        let percent_width = 3;
        let eta_width = eta_str.len();
        let speed_width = speed_str.len();
        let avg_width = avg_str.len();
        let index = progress.index;
        let index_width = index.to_string().len();
        let num_files = progress.num_files;
        let num_files_width = num_files.to_string().len();
        let widgets_width =
            percent_width + index_width + num_files_width + eta_width + speed_width + avg_width;
        let num_separators = 7;
        let line_width = get_terminal_width();
        let file_width = line_width
            .saturating_sub(widgets_width + num_separators + 1)
            .max(8);
        let current_file = progress.current_file.clone();
        let current_file = truncate_lossy(&current_file, file_width);
        let current_file = format!(
            "{filename:<pad$}",
            pad = file_width,
            filename = current_file
        );
        let file_percent = (progress.file_done * 100) / progress.file_size.max(1);
        print!(
            "{:>3}% {}/{} {} {} {} {}\r",
            file_percent, index, num_files, current_file, speed_str, avg_str, eta_str
        );
        let _ = io::stdout().flush();
        self.line_dirty = true;
    }

    fn error(&mut self, entry: &str, desc: &str) {
        eprintln!("Errror: {}", desc);
        if let Some(err_file) = &mut self.err_file {
            // Ignoring errrors when trying to log errors ...
            let _ = err_file.write(entry.as_bytes());
            let _ = err_file.write(b"\n");
            let _ = err_file.flush();
        }
    }

    fn end(&mut self, stats: &sync::Stats) {
        println!(
            "{} Synced {} files ({} up to date)",
            "".color("green"),
            stats.num_synced,
            stats.up_to_date
        );
        println!(
            "{} files copied, {} symlinks created, {} symlinks updated",
            stats.copied, stats.symlink_created, stats.symlink_updated
        );
        if stats.excluded_files > 0 || stats.excluded_dirs > 0 {
            println!(
                "{} files skipped by filters ({} directories pruned)",
                stats.excluded_files, stats.excluded_dirs
            );
        }
        let transfered = stats.total_transfered;
        // We know transfered cannot be negative
        let transfered = transfered.file_size(options::DECIMAL).unwrap();
        let duration = stats.duration();
        let average = average_speed(stats.total_transfered, duration);
        // Truncate below 1 second
        let duration = std::time::Duration::from_secs(duration.as_secs());
        let duration = humantime::format_duration(duration);
        println!(
            "{} copied in {} (avg {})",
            transfered,
            duration,
            human_speed(average)
        );
        if stats.errors != 0 {
            eprintln!("{} errors occurred", stats.errors);
        }
    }
}

impl Default for ConsoleProgressInfo {
    fn default() -> Self {
        Self::new()
    }
}

/// Print the time the sync completed and the time of the next sync, in watch
/// mode (see the `--sleep-interval` option).
pub fn announce_next_sync(finished_at: &str, next_sync_at: &str) {
    println!(
        "{} Sync completed at {}, next sync at {}",
        "::".color("blue"),
        finished_at.bold(),
        next_sync_at.bold()
    );
}

/// One line of the progress bar displayed while sleeping between two syncs.
pub fn sleep_progress_line(
    elapsed_secs: u64,
    total_secs: u64,
    next_sync_at: &str,
    bar_width: usize,
) -> String {
    // The interval is at least one second, so this is never zero:
    let total = total_secs.max(1) as u128;
    let elapsed = elapsed_secs as u128;
    let percent = (elapsed * 100 / total) as u64;
    let filled = ((elapsed * bar_width as u128 / total) as usize).min(bar_width);
    let mut bar = String::new();
    for i in 0..bar_width {
        if i < filled {
            bar.push('#');
        } else {
            bar.push('.');
        }
    }
    format!(
        "waiting [{}] {:>3}% ({}/{}) next sync at {}",
        bar,
        percent,
        human_seconds(elapsed_secs as usize),
        human_seconds(total_secs as usize),
        next_sync_at
    )
}

/// Sleep for `interval`, refreshing `sleep_progress_line` while waiting.
pub fn sleep_with_progress(interval: Duration, next_sync_at: &str) {
    let start = Instant::now();
    let total = interval.as_secs().max(1);
    loop {
        let elapsed = start.elapsed();
        if elapsed >= interval {
            break;
        }
        let line = sleep_progress_line(elapsed.as_secs(), total, next_sync_at, 10);
        print!("{}\r", line);
        let _ = io::stdout().flush();
        thread::sleep(Duration::from_secs(1));
    }
    erase_line();
}

/// The line displayed while the source tree is being scanned.
fn scanning_line(info: &WalkInfo) -> String {
    let mut line = format!(
        "scanning … {} entries, {} to sync",
        info.seen, info.num_files
    );
    if info.excluded_files > 0 || info.excluded_dirs > 0 {
        line.push_str(&format!(
            ", {} skipped by filters",
            info.excluded_files + info.excluded_dirs
        ));
    }
    line
}

/// Format a speed in bytes per second.
fn human_speed(bytes_per_second: usize) -> String {
    format!(
        "{}/s",
        bytes_per_second
            .file_size(options::DECIMAL)
            .unwrap_or_default()
    )
}

/// Average speed in bytes per second over the given duration.
fn average_speed(total_done: u64, elapsed: std::time::Duration) -> usize {
    let secs = elapsed.as_secs_f64();
    if secs > 0.0 {
        (total_done as f64 / secs) as usize
    } else {
        0
    }
}

fn get_terminal_width() -> usize {
    if let Some((Width(w), _)) = terminal_size() {
        return w as usize;
    }
    // We're likely not a tty here, so this is a good enough default:
    80
}

fn erase_line() {
    let line_width = get_terminal_width();
    let line = vec![32_u8; line_width];
    // We're calling from_utf8 on a string containing only spaces,
    // so calling unwrap() is safe
    print!("{}\r", String::from_utf8(line).unwrap());
}

fn human_seconds(s: usize) -> String {
    let hours = s / 3600;
    let minutes = (s / 60) % 60;
    let seconds = s % 60;
    format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
}

fn truncate_lossy(text: &str, maxsize: usize) -> String {
    // Our goal here is to make sure the text can be written
    // in the terminal without going over the `maxsize` length
    // Our approach is to first convert to bytes, then truncate
    // the vector of bytes, then convert to a lossy string
    // This way we *know* we won't cut at a char boundary
    let mut as_bytes = text.to_string().into_bytes();
    as_bytes.truncate(maxsize);
    String::from_utf8_lossy(&as_bytes).to_string()
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn test_truncate_string() {
        let new_text = truncate_lossy("ééé", 2);
        assert_eq!(new_text, "é");
    }

    #[test]
    fn scanning_line_without_filters() {
        let info = WalkInfo {
            seen: 12,
            num_files: 10,
            excluded_files: 0,
            excluded_dirs: 0,
            finished: false,
        };
        assert_eq!(scanning_line(&info), "scanning … 12 entries, 10 to sync");
    }

    #[test]
    fn scanning_line_with_filters() {
        let info = WalkInfo {
            seen: 15,
            num_files: 10,
            excluded_files: 2,
            excluded_dirs: 1,
            finished: false,
        };
        assert_eq!(
            scanning_line(&info),
            "scanning … 15 entries, 10 to sync, 3 skipped by filters"
        );
    }

    #[test]
    fn test_human_seconds() {
        assert_eq!("00:00:05", human_seconds(5));
        assert_eq!("00:00:42", human_seconds(42));
        assert_eq!("00:03:05", human_seconds(185));
        assert_eq!("02:04:05", human_seconds(7445));
        assert_eq!("200:00:02", human_seconds(720_002));
    }

    #[test]
    fn test_human_speed() {
        assert_eq!(human_speed(0), "0 B/s");
        assert_eq!(human_speed(1024), "1.02 KB/s");
        assert_eq!(human_speed(12500000), "12.50 MB/s");
    }

    #[test]
    fn sleep_progress_line_at_start() {
        let line = sleep_progress_line(0, 60, "2026-08-20 17:32:00", 10);
        assert_eq!(
            line,
            "waiting [..........]   0% (00:00:00/00:01:00) next sync at 2026-08-20 17:32:00"
        );
    }

    #[test]
    fn sleep_progress_line_half_way() {
        let line = sleep_progress_line(30, 60, "2026-08-20 17:32:00", 10);
        assert_eq!(
            line,
            "waiting [#####.....]  50% (00:00:30/00:01:00) next sync at 2026-08-20 17:32:00"
        );
    }

    #[test]
    fn sleep_progress_line_rounds_the_bar_down() {
        let line = sleep_progress_line(45, 120, "2026-08-20 17:32:00", 4);
        assert_eq!(
            line,
            "waiting [#...]  37% (00:00:45/00:02:00) next sync at 2026-08-20 17:32:00"
        );
    }

    #[test]
    fn sleep_progress_line_complete() {
        let line = sleep_progress_line(60, 60, "2026-08-20 17:32:00", 10);
        assert_eq!(
            line,
            "waiting [##########] 100% (00:01:00/00:01:00) next sync at 2026-08-20 17:32:00"
        );
    }

    #[test]
    fn sleep_progress_line_uses_hours() {
        let line = sleep_progress_line(0, 24 * 3600, "2026-08-21 17:32:00", 10);
        assert_eq!(
            line,
            "waiting [..........]   0% (00:00:00/24:00:00) next sync at 2026-08-21 17:32:00"
        );
    }
}