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
use std::path::Path;
use std::path::PathBuf;
use std::sync::mpsc::channel;
use std::thread;

use anyhow::{anyhow, Error};

use crate::entry::Entry;
use crate::fsops;
use crate::fsops::SyncOutcome::*;
use crate::progress::{ProgressInfo, ProgressMessage};
use crate::workers::ProgressWorker;
use crate::workers::SyncWorker;
use crate::workers::WalkWorker;

#[derive(Debug)]
pub struct Stats {
    /// Number of files in the source
    pub num_files: u64,
    /// Sum of the sizes of all the files in the source
    pub total_size: usize,
    /// Sum of the sizes of all the files that were synced
    pub total_transfered: u64,

    /// Number of files transfered (should match `num_files`
    /// if no error)
    pub num_synced: u64,
    /// Number of files for which the copy was skipped
    pub up_to_date: u64,
    /// Number of files that were copied
    pub copied: u64,
    /// Number of errors
    pub errors: u64,

    /// Number of symlink created in the destination folder
    pub symlink_created: u64,
    /// Number of symlinks updated in the destination folder
    pub symlink_updated: u64,

    /// Duration of the transfer
    pub duration: std::time::Duration,

    start: std::time::Instant,
}

impl Stats {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Stats {
        Stats {
            num_files: 0,
            total_size: 0,
            total_transfered: 0,

            num_synced: 0,
            up_to_date: 0,
            copied: 0,
            errors: 0,

            symlink_created: 0,
            symlink_updated: 0,
            start: std::time::Instant::now(),
            duration: std::time::Duration::new(0, 0),
        }
    }

    pub fn start(&mut self) {
        self.start = std::time::Instant::now();
    }

    pub fn stop(&mut self) {
        let end = std::time::Instant::now();
        self.duration = end - self.start;
    }

    pub fn duration(&self) -> std::time::Duration {
        self.duration
    }

    pub fn add_error(&mut self) {
        self.errors += 1;
    }

    #[doc(hidden)]
    pub fn add_outcome(&mut self, outcome: &fsops::SyncOutcome) {
        self.num_synced += 1;
        match outcome {
            FileCopied { size } => {
                self.copied += 1;
                self.total_transfered += size;
            }
            UpToDate => self.up_to_date += 1,
            SymlinkUpdated => self.symlink_updated += 1,
            SymlinkCreated => self.symlink_created += 1,
        }
    }
}

#[derive(Copy, Clone)]
pub struct SyncOptions {
    /// Wether to preserve permissions of the source file after the destination is written.
    pub preserve_permissions: bool,
}

impl Default for SyncOptions {
    fn default() -> Self {
        Self {
            preserve_permissions: true,
        }
    }
}

pub struct Syncer {
    source: PathBuf,
    destination: PathBuf,
    options: SyncOptions,
    progress_info: Box<dyn ProgressInfo + Send>,
}

impl Syncer {
    pub fn new(
        source: &Path,
        destination: &Path,
        options: SyncOptions,
        progress_info: Box<dyn ProgressInfo + Send>,
    ) -> Syncer {
        Syncer {
            source: source.to_path_buf(),
            destination: destination.to_path_buf(),
            progress_info,
            options,
        }
    }

    pub fn sync(self) -> Result<Stats, Error> {
        let (walker_entry_output, syncer_input) = channel::<Entry>();
        let (walker_stats_output, progress_input) = channel::<ProgressMessage>();
        let progress_output = walker_stats_output.clone();

        let walk_worker = WalkWorker::new(&self.source, walker_entry_output, walker_stats_output);
        let sync_worker = SyncWorker::new(
            &self.source,
            &self.destination,
            syncer_input,
            progress_output,
        );
        let progress_worker = ProgressWorker::new(progress_input, self.progress_info);
        let options = self.options;

        let walker_thread = thread::spawn(move || walk_worker.start());
        let syncer_thread = thread::spawn(move || sync_worker.start(options));
        let progress_thread = thread::spawn(|| progress_worker.start());

        walker_thread
            .join()
            .map_err(|e| anyhow!("Could not join walker thread: {:?}", e))?;

        let syncer_result = syncer_thread
            .join()
            .map_err(|e| anyhow!("Could not join syncer thread: {:?}", e))?;

        let progress_result = progress_thread
            .join()
            .map_err(|e| anyhow!("Could not join progress thread: {:?}", e))?;

        syncer_result?;

        Ok(progress_result)
    }
}