plsync 0.2.2

Parallel local only `rsync` implementation in Rust
Documentation
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use filetime::FileTime;
use glob_match::glob_match;
use indicatif::{
    FormattedDuration, HumanBytes, HumanFloatCount, ParallelProgressIterator, ProgressBar,
};
use log::{debug, error, info, warn};
use rayon::prelude::*;
use std::any::Any;
use std::fs::Metadata;
use std::io::Error;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::path::PathBuf;

#[derive(Clone, Default, Debug)]
pub struct SyncStatus {
    pub dirs_copied: u64,
    pub dirs_total: u64,
    pub dirs_errors: u64,
    pub dirs_deleted: u64,
    pub files_copied: u64,
    pub files_total: u64,
    pub files_errors: u64,
    pub files_deleted: u64,
    pub links_copied: u64,
    pub links_total: u64,
    pub links_errors: u64,
    pub links_deleted: u64,
    pub permissions_errors: u64,
    pub bytes_copied: u64,
    pub bytes_total: u64,
}

impl SyncStatus {
    pub fn entries_total(&self) -> u64 {
        self.dirs_total + self.files_total + self.links_total
    }

    pub fn errors_total(&self) -> u64 {
        self.dirs_errors + self.files_errors + self.links_errors + self.permissions_errors
    }

    pub fn copied_total(&self) -> u64 {
        self.dirs_copied + self.files_copied + self.links_copied
    }

    pub fn deleted_total(&self) -> u64 {
        self.dirs_deleted + self.files_deleted + self.links_deleted
    }

    pub fn skipped_total(&self) -> u64 {
        self.entries_total() - self.copied_total()
    }

    pub fn dirs_skipped(&self) -> u64 {
        self.dirs_total - self.dirs_copied
    }

    pub fn files_skipped(&self) -> u64 {
        self.files_total - self.files_copied
    }

    pub fn links_skipped(&self) -> u64 {
        self.links_total - self.links_copied
    }

    pub fn bytes_skipped(&self) -> u64 {
        self.bytes_total - self.bytes_copied
    }

    pub fn bandwidth_total(&self, elapsed: &std::time::Duration) -> u64 {
        let elapsed = elapsed.as_secs_f64();
        if elapsed == 0.0 {
            return 0;
        }
        (self.bytes_total as f64 / elapsed) as u64
    }

    pub fn bandwidth_copied(&self, elapsed: &std::time::Duration) -> u64 {
        let elapsed = elapsed.as_secs_f64();
        if elapsed == 0.0 {
            return 0;
        }
        (self.bytes_copied as f64 / elapsed) as u64
    }

    pub fn bandwidth_skipped(&self, elapsed: &std::time::Duration) -> u64 {
        let elapsed = elapsed.as_secs_f64();
        if elapsed == 0.0 {
            return 0;
        }
        (self.bytes_skipped() as f64 / elapsed) as u64
    }

    pub fn merge(&self, other: &Self) -> Self {
        SyncStatus {
            dirs_copied: self.dirs_copied + other.dirs_copied,
            dirs_total: self.dirs_total + other.dirs_total,
            dirs_errors: self.dirs_errors + other.dirs_errors,
            dirs_deleted: self.dirs_deleted + other.dirs_deleted,
            files_copied: self.files_copied + other.files_copied,
            files_total: self.files_total + other.files_total,
            files_errors: self.files_errors + other.files_errors,
            files_deleted: self.files_deleted + other.files_deleted,
            links_copied: self.links_copied + other.links_copied,
            links_total: self.links_total + other.links_total,
            links_errors: self.links_errors + other.links_errors,
            links_deleted: self.links_deleted + other.links_deleted,
            permissions_errors: self.permissions_errors + other.permissions_errors,
            bytes_copied: self.bytes_copied + other.bytes_copied,
            bytes_total: self.bytes_total + other.bytes_total,
        }
    }

    pub fn print(&self) {
        println!(
            "Entries total: {}, copied: {}, skipped: {}, deleted: {}, errors: {}, permission errors: {}",
            HumanFloatCount(self.entries_total() as f64),
            HumanFloatCount(self.copied_total() as f64),
            HumanFloatCount(self.skipped_total() as f64),
            HumanFloatCount(self.deleted_total() as f64),
            HumanFloatCount(self.errors_total() as f64),
            HumanFloatCount(self.permissions_errors as f64)
        );
        println!(
            "Directories total: {}, copied: {}, skipped: {}, deleted: {}, errors: {}",
            HumanFloatCount(self.dirs_total as f64),
            HumanFloatCount(self.dirs_copied as f64),
            HumanFloatCount(self.dirs_skipped() as f64),
            HumanFloatCount(self.dirs_deleted as f64),
            HumanFloatCount(self.dirs_errors as f64)
        );
        println!(
            "Symbolic links total: {}, copied: {}, skipped: {}, deleted: {}, errors: {}",
            HumanFloatCount(self.links_total as f64),
            HumanFloatCount(self.links_copied as f64),
            HumanFloatCount(self.links_skipped() as f64),
            HumanFloatCount(self.links_deleted as f64),
            HumanFloatCount(self.links_errors as f64)
        );
        println!(
            "Files total: {}, copied: {}, skipped: {}, deleted: {}, errors: {}",
            HumanFloatCount(self.files_total as f64),
            HumanFloatCount(self.files_copied as f64),
            HumanFloatCount(self.files_skipped() as f64),
            HumanFloatCount(self.files_deleted as f64),
            HumanFloatCount(self.files_errors as f64)
        );
        println!(
            "Transfered toal: {}, copied {}, skipped: {}",
            HumanBytes(self.bytes_total),
            HumanBytes(self.bytes_copied),
            HumanBytes(self.bytes_skipped()),
        );
    }

    pub fn print_elapsed(&self, start_time: &std::time::Instant) {
        let elapsed = start_time.elapsed();
        println!("Elapsed time: {}", FormattedDuration(elapsed));
        self.print();
        println!(
            "bandwidth toal: {}/s, copied {}/s, skipped: {}/s",
            HumanBytes(self.bandwidth_total(&elapsed)),
            HumanBytes(self.bandwidth_copied(&elapsed)),
            HumanBytes(self.bandwidth_skipped(&elapsed)),
        );
    }
}

pub struct SyncOptions {
    pub preserve_permissions: bool,
    pub perform_dry_run: bool,
    pub delete: bool,
    pub delete_before: bool,
    pub delete_after: bool,
    pub exclude: Vec<String>,
}

impl Default for SyncOptions {
    fn default() -> Self {
        Self {
            preserve_permissions: true,
            perform_dry_run: false,
            delete: false,
            delete_before: false,
            delete_after: false,
            exclude: Vec::new(),
        }
    }
}

pub fn sync(
    source_path: &Path,
    destination_path: &Path,
    options: &SyncOptions,
    progress_bar: &ProgressBar,
) -> SyncStatus {
    if !source_path.is_dir() {
        error!("Source path is not a directory: {}", source_path.display());
        return SyncStatus { dirs_errors: 1, ..Default::default() };
    }
    if !destination_path.is_dir() && !options.perform_dry_run {
        if let Err(e) = std::fs::create_dir_all(destination_path) {
            error!(
                "Failed to create directory: {}, {}",
                destination_path.display(),
                e
            );
            return SyncStatus { dirs_errors: 1, ..Default::default() };
        }
    }
    let mut status = SyncStatus::default();
    if options.delete_before {
        progress_bar.set_message("Delete before phase");
        status = delete_path(destination_path, source_path, options, progress_bar).merge(&status);
        progress_bar.set_message("Copy phase");
        progress_bar.set_position(0);
    }
    if options.delete {
        progress_bar.set_message("Copy and delete phase");
    }
    status = sync_path(source_path, destination_path, options, progress_bar).merge(&status);
    if options.delete_after {
        progress_bar.set_message("Delete after phase");
        progress_bar.set_position(0);
        status = delete_path(destination_path, source_path, options, progress_bar).merge(&status);
    }
    status
}

fn sync_path(
    source_base: &Path,
    destination_base: &Path,
    options: &SyncOptions,
    progress_bar: &ProgressBar,
) -> SyncStatus {
    let source_dir = std::fs::read_dir(source_base);
    if source_dir.is_err() {
        return SyncStatus { permissions_errors: 1, ..Default::default() };
    }
    source_dir
        .unwrap()
        .par_bridge()
        .progress_with(progress_bar.clone())
        .map(|entry| {
            if entry.is_err() {
                return SyncStatus {
                    permissions_errors: 1,
                    ..Default::default()
                };
            }
            let entry = entry.unwrap();
            let source_path = entry.path();
            let metadata = entry.metadata();
            if metadata.is_err() {
                return SyncStatus {
                    dirs_errors: 1,
                    ..Default::default()
                };
            }
            let metadata = metadata.unwrap();
            let source_relative = source_path.strip_prefix(source_base).unwrap();
            let destination_path = destination_base.join(source_relative);
            if skip_path(&metadata, &source_path, &options.exclude) {
                let mut status = SyncStatus::default();
                match metadata.file_type() {
                    file_type if file_type.is_dir() => status.dirs_total = 1,
                    file_type if file_type.is_symlink() => status.links_total = 1,
                    _ => status.files_total = 1,
                }
                return status;
            }
            if metadata.is_dir() {
                debug!(
                    "Syncing directory: {} -> {}",
                    source_path.display(),
                    destination_path.display()
                );
                let status = sync_dir(&source_path, &destination_path, options, &metadata);
                sync_path(&source_path, &destination_path, options, progress_bar).merge(&status)
            } else if metadata.is_symlink() {
                debug!(
                    "Syncing symlink: {} -> {}",
                    source_path.display(),
                    destination_path.display()
                );
                sync_symlink(&source_path, &destination_path, options, &metadata)
            } else {
                debug!(
                    "Syncing file: {} -> {}",
                    source_path.display(),
                    destination_path.display()
                );
                sync_file(&source_path, &destination_path, options, &metadata)
            }
        })
        .reduce(SyncStatus::default, |a, b| a.merge(&b))
}

fn delete_path(
    source_base: &Path,
    destination_base: &Path,
    options: &SyncOptions,
    progress_bar: &ProgressBar,
) -> SyncStatus {
    let source_dir = std::fs::read_dir(source_base);
    if source_dir.is_err() {
        return SyncStatus {
            permissions_errors: 1,
            ..Default::default()
        };
    }
    source_dir
        .unwrap()
        .par_bridge()
        .progress_with(progress_bar.clone())
        .map(|entry| {
            if entry.is_err() {
                return SyncStatus {
                    permissions_errors: 1,
                    ..Default::default()
                };
            }
            let entry = entry.unwrap();
            let source_path = entry.path();
            let metadata = entry.metadata();
            if metadata.is_err() {
                return SyncStatus {
                    dirs_errors: 1,
                    ..Default::default()
                };
            }
            let metadata = metadata.unwrap();
            let source_relative = source_path.strip_prefix(source_base).unwrap();
            let destination_path = destination_base.join(source_relative);
            if skip_path(&metadata, &source_path, &options.exclude) {
                return SyncStatus::default();
            }
            if metadata.is_dir() {
                debug!(
                    "Syncing absent directory: {} -> {}",
                    source_path.display(),
                    destination_path.display()
                );
                let mut status =
                    delete_path(&source_path, &destination_path, options, progress_bar);
                if !destination_path.exists() {
                    status = delete_dir(&source_path, options).merge(&status);
                }
                status
            } else {
                debug!(
                    "Syncing absent file: {} -> {}",
                    source_path.display(),
                    destination_path.display()
                );
                if !destination_path.exists() {
                    delete_file_or_link(&source_path, options, &metadata)
                } else {
                    SyncStatus::default()
                }
            }
        })
        .reduce(SyncStatus::default, |a, b| a.merge(&b))
}

fn skip_path(metadata: &Metadata, source_path: &Path, excluded: &Vec<String>) -> bool {
    let source = source_path.file_name().unwrap().to_string_lossy();

    for pattern in excluded {
        if pattern.is_empty() {
            continue;
        }
        if pattern.as_str() == source {
            debug!(
                "Skipping {} because {} == {}",
                source_path.display(),
                pattern,
                source
            );
            return true;
        }
        if pattern.ends_with('/') && metadata.is_dir() && pattern[..pattern.len() - 1] == source {
            debug!(
                "Skipping dir {} because {} == {}/",
                source_path.display(),
                pattern,
                source
            );
            return true;
        }
        if glob_match(pattern, source_path.to_string_lossy().as_ref())
            || glob_match(pattern, &source)
        {
            debug!(
                "Skipping {} because {} matches {}",
                source_path.display(),
                source,
                pattern
            );
            return true;
        }
    }
    false
}

#[cfg(unix)]
fn copy_permissions(metadata: &Metadata, destination: &PathBuf) -> Result<(), Error> {
    let permissions = metadata.permissions();
    debug!(
        "Setting permissions {:o} on {}",
        permissions.mode(),
        destination.display()
    );
    std::fs::set_permissions(destination, permissions)
}

fn sync_dir(
    _source: &Path,
    destination: &PathBuf,
    options: &SyncOptions,
    #[cfg(unix)] metadata: &Metadata,
    #[cfg(windows)] _metadata: &Metadata,
) -> SyncStatus {
    let mut status = SyncStatus {
        dirs_total: 1,
        ..Default::default()
    };
    if !destination.exists() {
        if options.perform_dry_run {
            debug!("Would create directory: {}", destination.display());
        } else {
            // assert_parent_exists(&destination)?;
            debug!("Creating directory: {}", destination.display());
            if let Err(e) = std::fs::create_dir_all(destination) {
                error!(
                    "Failed to create directory: {}, {}",
                    destination.display(),
                    e
                );
                status.dirs_errors = 1;
                return status;
            }
        }
        info!("Created directory: {}", destination.display());
        status.dirs_copied = 1;
    }
    #[cfg(unix)]
    {
        if !options.perform_dry_run && options.preserve_permissions {
            if let Err(e) = copy_permissions(metadata, destination) {
                error!(
                    "Failed to set permissions on directory: {}, {}",
                    destination.display(),
                    e
                );
                status.permissions_errors = 1;
            }
        }
    }
    status
}

fn delete_dir(source: &PathBuf, options: &SyncOptions) -> SyncStatus {
    let mut status = SyncStatus {
        dirs_deleted: 1,
        ..Default::default()
    };
    if options.perform_dry_run {
        debug!("Would delete directory: {}", source.display());
    } else {
        debug!("Deleting directory: {}", source.display());
        if let Err(e) = std::fs::remove_dir(source) {
            status.dirs_errors = 1;
            error!("Failed to delete directory: {}, {}", source.display(), e);
            return status;
        }
    }
    info!("Deleted directory: {}", source.display());
    status
}

fn sync_symlink(
    source: &PathBuf,
    destination: &PathBuf,
    options: &SyncOptions,
    _metadata: &Metadata,
) -> SyncStatus {
    let mut status = SyncStatus {
        links_total: 1,
        ..Default::default()
    };
    let link = std::fs::read_link(source);
    if link.is_err() {
        error!(
            "Failed to read symlink: {}, {}",
            source.display(),
            link.unwrap_err()
        );
        status.links_errors = 1;
        return status;
    }
    let link = link.unwrap();
    let destination_metadata = destination.symlink_metadata();
    if destination_metadata.is_ok() {
        let destination_metadata = destination_metadata.unwrap();
        if destination_metadata.file_type().is_symlink() {
            let destination_link = std::fs::read_link(destination).unwrap();
            if destination_link == link {
                return status;
            }
        }
        if options.perform_dry_run {
            debug!("Would delete existing file: {}", destination.display());
        } else {
            debug!("Deleting existing file: {}", destination.display());
            if let Err(e) = std::fs::remove_file(destination) {
                status.links_errors = 1;
                error!(
                    "Failed to delete existing file: {}, {}",
                    destination.display(),
                    e
                );
                return status;
            }
        }
    }
    if options.perform_dry_run {
        debug!(
            "Would create symlink: {} -> {}",
            destination.display(),
            link.display()
        );
    } else {
        if let Err(e) = ensure_parent_exists(destination) {
            error!(
                "Failed to create parent directory: {}, {}",
                destination.display(),
                e
            );
            status.links_errors = 1;
            return status;
        }
        debug!(
            "Creating symlink: {} -> {}",
            destination.display(),
            link.display()
        );
        #[cfg(unix)]
        {
            if let Err(e) = std::os::unix::fs::symlink(&link, destination) {
                error!("Failed to create symlink: {}, {}", destination.display(), e);
                status.links_errors = 1;
                return status;
            }
        }
    }
    info!(
        "Created symlink: {} -> {}",
        destination.display(),
        link.display()
    );
    status
}

fn sync_file(
    source: &PathBuf,
    destination: &PathBuf,
    options: &SyncOptions,
    metadata: &Metadata,
) -> SyncStatus {
    let mut status = SyncStatus::default();
    let files_differs = files_differs(destination, metadata).unwrap();
    let source_length = metadata.len();
    status.files_total = 1;
    status.bytes_total = source_length;
    if destination.exists() {
        if !destination.is_file() {
            error!(
                "Failed to change: {} of type {:?}",
                destination.display(),
                destination.type_id()
            );
            status.files_errors = 1;
            return status;
        }
        if !files_differs {
            debug!(
                "File up to date, no need to copy: {} -> {}",
                source.display(),
                destination.display()
            );
            return status;
        }
    }
    debug!(
        "Copying file: {} -> {}",
        source.display(),
        destination.display()
    );
    let bytes_copied = if options.perform_dry_run {
        debug!(
            "Would copy: {} -> {}",
            source.display(),
            destination.display()
        );
        source_length
    } else {
        if let Err(e) = ensure_parent_exists(destination) {
            error!(
                "Failed to create parent directory: {}, {}",
                destination.display(),
                e
            );
            status.files_errors = 1;
            return status;
        }

        let copy_outcome = std::fs::copy(source, destination);
        if copy_outcome.is_err() {
            error!(
                "Failed to copy file: {} -> {}, {}",
                source.display(),
                destination.display(),
                copy_outcome.unwrap_err()
            );
            status.files_errors = 1;
            return status;
        }
        info!(
            "Copied file: {} -> {}",
            source.display(),
            destination.display()
        );
        copy_outcome.unwrap()
    };
    status.bytes_copied = bytes_copied;
    status.files_copied = 1;
    status
}

fn delete_file_or_link(source: &PathBuf, options: &SyncOptions, metadata: &Metadata) -> SyncStatus {
    let mut status = SyncStatus::default();
    let file_type = if metadata.is_symlink() {
        "symlink"
    } else {
        "file"
    };
    if metadata.is_symlink() {
        status.links_deleted = 1;
    } else {
        status.files_deleted = 1;
    }
    if options.perform_dry_run {
        debug!("Would delete {}: {}", file_type, source.display());
    } else {
        debug!("Deleting {}: {}", file_type, source.display());
        if let Err(e) = std::fs::remove_file(source) {
            status.links_errors = 1;
            error!(
                "Failed to delete {}: {}, {}",
                file_type,
                source.display(),
                e
            );
            return status;
        }
    }
    info!("Deleted {}: {}", file_type, source.display());
    status
}

fn ensure_parent_exists(path: &Path) -> Result<(), Error> {
    let path_parent = path.parent().unwrap();
    if !path_parent.exists() {
        warn!("Creating parent directory: {}", path_parent.display());
        std::fs::create_dir_all(path_parent)?;
    }
    Ok(())
}

fn files_differs(destination: &Path, src_meta: &Metadata) -> Result<bool, Error> {
    if !destination.exists() {
        return Ok(true);
    }

    let dest_meta = destination.metadata()?;

    let src_mtime = FileTime::from_last_modification_time(src_meta);
    let dest_mtime = FileTime::from_last_modification_time(&dest_meta);

    let src_size = src_meta.len();
    let dest_size = dest_meta.len();

    Ok(src_mtime > dest_mtime || src_size != dest_size)
}