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
use std::fs;
use std::{
    io,
    path::{Path, PathBuf},
};

use crate::{copy_dir, DirectoryOperation, RollbackableOperation};

/// Copies a file to destination
pub struct CopyFile {
    source: PathBuf,
    dest: PathBuf,
}

impl CopyFile {
    /// Constructs a new CopyFile operation
    pub fn new<S: AsRef<Path>, T: AsRef<Path>>(source: S, dest: T) -> Self {
        Self {
            source: source.as_ref().into(),
            dest: dest.as_ref().into(),
        }
    }
}

impl RollbackableOperation for CopyFile {
    fn execute(&mut self) -> io::Result<()> {
        match fs::copy(&self.source, &self.dest) {
            Ok(_v) => Ok(()),
            Err(e) => Err(e),
        }
    }

    fn rollback(&self) -> io::Result<()> {
        fs::remove_file(&self.dest)
    }
}

/// Copies a directory to destination
pub struct CopyDirectory {
    source: PathBuf,
    dest: PathBuf,
    backup_path: PathBuf,
    temp_dir: PathBuf,
}

impl CopyDirectory {
    /// Constructs a new CopyDirectory operation
    pub fn new<S: AsRef<Path>, T: AsRef<Path>, U: AsRef<Path>>(
        source: S,
        dest: T,
        temp_dir: U,
    ) -> Self {
        Self {
            source: source.as_ref().into(),
            dest: dest.as_ref().into(),
            temp_dir: temp_dir.as_ref().into(),
            backup_path: PathBuf::new(),
        }
    }
}

impl RollbackableOperation for CopyDirectory {
    fn execute(&mut self) -> io::Result<()> {
        self.create_backup_folder()?;
        copy_dir(&self.source, &self.dest)
    }

    fn rollback(&self) -> io::Result<()> {
        fs::remove_dir_all(&self.dest)
    }
}

impl DirectoryOperation for CopyDirectory {
    fn get_path(&self) -> &Path {
        &self.source
    }

    fn get_backup_path(&self) -> &Path {
        &self.backup_path
    }

    fn set_backup_path<S: AsRef<Path>>(&mut self, uuid: S) {
        self.backup_path = uuid.as_ref().into();
    }

    fn get_temp_dir(&self) -> &Path {
        &self.temp_dir
    }
}

impl Drop for CopyDirectory {
    fn drop(&mut self) {
        match self.dispose() {
            Err(e) => eprintln!("{}", e),
            _ => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::{self, File};
    use std::path::Path;

    const FILE_SOURCE: &str = "./copy_file_source.txt";
    const DEST_DIR: &str = "./copy_file_dir";
    const FILE_DEST: &str = "./copy_file_dir/copy_file_source.txt";

    fn file_setup() -> std::io::Result<()> {
        File::create(FILE_SOURCE)?;
        fs::create_dir(DEST_DIR)
    }

    #[test]
    #[allow(unused_must_use)]
    fn copy_file_works() {
        assert_eq!((), file_setup().unwrap());

        let mut op = CopyFile::new(FILE_SOURCE, FILE_DEST);

        assert_eq!(false, Path::new(FILE_DEST).exists());
        assert_eq!((), op.execute().unwrap());
        assert_eq!(true, Path::new(FILE_SOURCE).exists());
        assert_eq!(true, Path::new(FILE_DEST).exists());

        assert_eq!((), op.rollback().unwrap());
        assert_eq!(true, Path::new(FILE_SOURCE).exists());
        assert_eq!(false, Path::new(FILE_DEST).exists());

        fs::remove_file(FILE_SOURCE);
        fs::remove_dir_all(DEST_DIR);
    }

    const DIR_SOURCE: &str = "./copy_dir_source";
    const DIR_DIR: &str = "./copy_dest_dir";
    const DIR_DEST: &str = "./copy_dest_dir/copy_dir_source";
    const DIR_TEMP: &str = "./tmp";

    fn folder_setup() -> std::io::Result<()> {
        fs::create_dir(DIR_SOURCE)?;
        fs::create_dir(DIR_DIR)
    }

    #[test]
    #[allow(unused_must_use)]
    fn copy_dir_works() {
        assert_eq!((), folder_setup().unwrap());

        let mut op = CopyDirectory::new(DIR_SOURCE, DIR_DEST, DIR_TEMP);

        assert_eq!((), op.execute().unwrap());
        assert_eq!(true, Path::new(DIR_SOURCE).exists());
        assert_eq!(true, Path::new(DIR_DEST).exists());

        assert_eq!((), op.rollback().unwrap());
        assert_eq!(true, Path::new(DIR_SOURCE).exists());
        assert_eq!(false, Path::new(DIR_DEST).exists());

        fs::remove_dir_all(DIR_SOURCE);
        fs::remove_dir(DIR_DIR);
    }
}