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

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

/// Deletes a file
pub struct DeleteFile {
    source: PathBuf,
    temp_dir: PathBuf,
    backup_path: PathBuf,
}

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

impl RollbackableOperation for DeleteFile {
    fn execute(&mut self) -> io::Result<()> {
        self.create_backup_file()?;

        fs::remove_file(self.get_path())
    }

    fn rollback(&self) -> io::Result<()> {
        match fs::copy(self.get_backup_path(), self.get_path()) {
            Ok(_v) => Ok(()),
            Err(e) => Err(e),
        }
    }
}

impl SingleFileOperation for DeleteFile {
    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 DeleteFile {
    fn drop(&mut self) {
        match self.dispose() {
            Err(e) => eprintln!("{}", e),
            _ => {}
        }
    }
}

/// Deletes a directory
pub struct DeleteDirectory {
    source: PathBuf,
    backup_path: PathBuf,
    temp_dir: PathBuf,
}

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

impl RollbackableOperation for DeleteDirectory {
    fn execute(&mut self) -> io::Result<()> {
        self.create_backup_folder()?;
        fs::remove_dir_all(&self.source)
    }

    fn rollback(&self) -> io::Result<()> {
        fs::rename(self.get_backup_path(), &self.source)
    }
}

impl DirectoryOperation for DeleteDirectory {
    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 DeleteDirectory {
    fn drop(&mut self) {
        match self.dispose() {
            Err(e) => eprintln!("{}", e),
            _ => {}
        }
    }
}

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

    const FILE_SOURCE: &str = "./delete_file_source";
    const TEMP_DIR: &str = "./tmp/";

    fn file_setup() -> std::io::Result<()> {
        match File::create(FILE_SOURCE) {
            Ok(_f) => Ok(()),
            Err(e) => Err(e),
        }
    }

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

        let mut op = DeleteFile::new(FILE_SOURCE, TEMP_DIR);

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

        fs::remove_file(FILE_SOURCE);
    }

    const DIR_SOURCE: &str = "./delete_dir_source";

    fn dir_setup() -> std::io::Result<()> {
        fs::create_dir(DIR_SOURCE)
    }

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

        let mut op = DeleteDirectory::new(DIR_SOURCE, TEMP_DIR);

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

        fs::remove_dir_all(DIR_SOURCE);
    }
}