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
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::path::{Path, PathBuf};

use ftp::types::FileType;
use ftp::FtpStream;
use indicatif::{ProgressBar, ProgressStyle};
use url::Url;

use crate::index::Index;

pub trait Sync {
    fn synchronize(
        &mut self,
        current_index: &Index,
        previous_index: &mut Index,
        assume_directories: bool,
    ) -> Result<bool, Box<dyn Error>>;
}

/// A synchronizer which save by FTP.
pub struct FtpSync {
    // the FTP session
    // if none it means that we are running with --skip-upload
    ftp_session: Option<FtpStream>,
    remote_dir: String,
    // create a local cache of existing directories
    // so that we won't waste time trying to create them again
    existing_directories: HashMap<String, bool>,
}

impl Sync for FtpSync {
    fn synchronize(
        &mut self,
        current_index: &Index,
        previous_index: &mut Index,
        assume_directories: bool,
    ) -> Result<bool, Box<dyn Error>> {
        // compute diff
        let (changed_files, deleted_files) = previous_index.diff(current_index);
        println!("-> {} files changed", changed_files.len());
        println!("-> {} files deleted", deleted_files.len());

        // If set to true, use the local cache to determinate existing directories
        // this will greatly reduce upload duration since we do not need to try to create ALL directories.
        if assume_directories {
            for path in previous_index.files().keys() {
                let path = Path::new(&path);

                // remove last component from path (the file)
                if path.parent().is_none() {
                    continue;
                }
                let path = path.parent().unwrap().to_str().unwrap();

                let mut current_dir = self.remote_dir.clone();
                for folder in path.split('/').filter(|f| !f.is_empty()) {
                    current_dir = format!("{}/{}", current_dir, folder);
                    self.existing_directories
                        .insert(current_dir.to_string(), true);
                }
            }
        }

        if self.ftp_session.is_some() {
            // create progress bar
            let pb = ProgressBar::new((changed_files.len() + deleted_files.len()) as u64);
            pb.set_style(ProgressStyle::default_bar().template(
                "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] ({pos}/{len}, ETA {eta})",
            ));

            self.process_changed_files(&pb, previous_index, &changed_files)?;
            self.process_deleted_files(&pb, previous_index, &deleted_files)?;
        }

        // everything is fine, save index to file
        current_index.save()?;

        Ok(self.ftp_session.is_none())
    }
}

impl FtpSync {
    pub fn new(dst: &Option<Url>) -> Result<FtpSync, Box<dyn Error>> {
        let mut ftp_session = None;
        let mut remote_dir = "";

        // If an URL is provided
        if let Some(dst) = dst {
            // open FTP connection
            let address = format!(
                "{}:{}",
                dst.host_str().expect("missing address"),
                dst.port().unwrap_or(21)
            );

            let mut session = FtpStream::connect(address)?;

            // authenticate if required
            if dst.username() != "" {
                session.login(dst.username(), dst.password().unwrap_or(""))?;
            }

            // set transfer mode to binary
            session.transfer_type(FileType::Binary)?;

            ftp_session = Some(session);

            // setup custom root directory if required
            remote_dir = if dst.path() != "" { dst.path() } else { "/" };
        }

        Ok(FtpSync {
            ftp_session,
            remote_dir: remote_dir.to_string(),
            existing_directories: HashMap::new(),
        })
    }

    fn process_changed_files(
        &mut self,
        progress_bar: &ProgressBar,
        previous_index: &mut Index,
        files: &[String],
    ) -> Result<(), Box<dyn Error>> {
        for path in files {
            // extract parent directory
            let p = PathBuf::from(path.clone());
            let parent = p.parent().unwrap().to_str().unwrap();

            // create any missing directories (recursively)
            self.make_directories(&format!("{}/{}", &self.remote_dir, parent))?;

            // store the file on the server
            let mut content = File::open(previous_index.path().join(path))?;
            self.ftp_session
                .as_mut()
                .unwrap()
                .put(&format!("{}/{}", &self.remote_dir, path), &mut content)?;
            previous_index.update(&path)?;
            previous_index.save()?;

            progress_bar.println(format!("[+] {}", path));
            progress_bar.inc(1);
        }

        Ok(())
    }
    fn process_deleted_files(
        &mut self,
        progress_bar: &ProgressBar,
        previous_index: &mut Index,
        files: &[String],
    ) -> Result<(), Box<dyn Error>> {
        for path in files {
            self.ftp_session
                .as_mut()
                .unwrap()
                .rm(&format!("{}/{}", &self.remote_dir, path))?;
            previous_index.remove(path)?;
            previous_index.save()?;

            progress_bar.println(format!("[-] {}", path));
            progress_bar.inc(1);
        }

        // TODO: it could be great to delete empty directory too

        Ok(())
    }

    fn make_directories(&mut self, path: &str) -> Result<(), Box<dyn Error>> {
        let mut current_dir = String::new();

        for folder in path.split('/').filter(|f| !f.is_empty()) {
            let next_dir = format!("{}/{}", current_dir, folder);

            // if the directory is not yet in the cache
            if !self.existing_directories.contains_key(&next_dir) {
                // create directory if not already exist
                if !self.directory_exist(&current_dir, &folder)? {
                    self.ftp_session.as_mut().unwrap().mkdir(&next_dir)?;
                }

                // insert directory into cache
                self.existing_directories.insert(next_dir.to_string(), true);
            }

            current_dir = next_dir;
        }

        Ok(())
    }

    fn directory_exist(&mut self, haystack: &str, needle: &str) -> Result<bool, Box<dyn Error>> {
        for f in self.ftp_session.as_mut().unwrap().list(Some(haystack))? {
            let parts: Vec<&str> = f.split_whitespace().collect();
            let perm = parts[0];
            let name = parts[parts.len() - 1];
            let is_dir = perm.starts_with('d');

            if is_dir && name == needle {
                return Ok(true);
            }
        }

        Ok(false)
    }
}