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
//! TFIO is a library that provides a Transaction-like interface that are traditionally used in databases on FileIO operations. It gives the flexibility to execute and rollback singlular operations as well as transactions on the fly. The library also provides a builder-pattern interface to chain operations and execute them in one go.
//!
//! # Examples
//!
//! Create a transaction and execute it. If any `Error` is encountered, rollback the entire transaction:
//! Note: The paths should only use forward slashes (`/`) and can either begin with disks or relative to the present working directory ie. begin with `./`
//! ```
//! use std::io;
//! use tfio::*;
//!
//! fn main() -> io::Result<()> {
//! 	let temp_dir = "./PATH_TO_TEMP_DIR";
//! 	let mut tr = Transaction::new()
//! 				.create_file("./foo.txt")
//! 				.create_dir("./bar")
//! 				.write_file("./foo.txt", temp_dir, b"Hello World".to_vec())
//! 				.move_file("./foo.txt", "./bar/foo.txt")
//! 				.append_file("./bar/foo.txt", temp_dir, b"dlroW olleH".to_vec());
//!
//! 	// Execute the transaction
//! 	if let Err(e) = tr.execute() {
//! 		eprintln!("Error during execution: {}", e);
//!
//! 		// All operations can be reverted in reverse-order if `Error` is encountered
//! 		if let Err(ee) = tr.rollback() {
//! 			panic!("Error during transaction rollback: {}", ee);
//! 		}
//! 	}
//! 	Ok(())
//! }
//! ```
//!
//! You can also import single operations to use:
//! ```
//! use std::io;
//! use std::fs;
//! use tfio::{CopyFile, RollbackableOperation};
//!
//! fn main() -> io::Result<()> {
//! 	fs::File::create("./foo.txt")?;
//! 	fs::create_dir_all("./bar/baz")?;
//!
//! 	let mut copy_operation = CopyFile::new("./foo.txt", "./bar/baz/foo.txt");
//!
//! 	// Execute the operation
//! 	if let Err(e) = copy_operation.execute() {
//! 		eprintln!("Error during execution: {}", e);
//!
//! 		// Rollback the operation
//! 		if let Err(ee) = copy_operation.rollback() {
//! 			panic!("Error during rollback: {}", ee);
//! 		}
//! 	}
//! 	Ok(())
//! }
//! ```

#![deny(missing_docs)]

mod append;
mod copy;
mod create;
mod delete;
mod r#move;
mod write;

use std::fs::{self, OpenOptions};
use std::io::{self, Error, ErrorKind, Read, Write};
use std::path::{Path, PathBuf};

use uuid::Uuid;

pub use append::AppendFile;
pub use copy::{CopyDirectory, CopyFile};
pub use create::{CreateDirectory, CreateFile};
pub use delete::{DeleteDirectory, DeleteFile};
pub use r#move::{MoveDirectory, MoveFile, MoveOperation};
pub use write::WriteFile;

/// Trait that represents a Rollbackable operation
pub trait RollbackableOperation {
    /// Executes the operation
    fn execute(&mut self) -> io::Result<()>;

    /// Rollbacks the operation
    fn rollback(&self) -> io::Result<()>;
}

/// Trait that represents a Directory operation
pub trait DirectoryOperation: RollbackableOperation + Drop {
    /// Returns path to source directory
    fn get_path(&self) -> &Path;

    /// Returns path to backup directory
    ///
    /// Defaults to ""
    fn get_backup_path(&self) -> &Path;

    /// Sets the backup path
    fn set_backup_path<S: AsRef<Path>>(&mut self, uuid: S);

    /// Returns path to temp dir
    fn get_temp_dir(&self) -> &Path;

    /// Dispose off resources used by the operation
    ///
    /// It should be called inside [Drop](std::ops::Drop)
    fn dispose(&self) -> io::Result<()> {
        fs::remove_dir(self.get_backup_path())
    }

    /// Creates a backup of the source directory
    ///
    /// If backup file is successfully created, method should call [set_backup_path](#method.set_backup_path)
    fn create_backup_folder(&mut self) -> io::Result<()> {
        fs::create_dir_all(&self.get_temp_dir())?;

        let uuid = Uuid::new_v4();
        let mut buffer = [b' '; 36];

        uuid.to_hyphenated().encode_lower(&mut buffer);

        let uuid_str = String::from_utf8(buffer.to_vec())
            .expect(format!("Could not convert buffer to String").as_str());
        let backup_path = Path::new(&self.get_temp_dir())
            .join(uuid_str)
            .to_str()
            .unwrap()
            .to_string();

        copy_dir(self.get_path(), &backup_path)?;

        self.set_backup_path(&backup_path);

        Ok(())
    }
}

/// Trait that represents a single file operation
pub trait SingleFileOperation: RollbackableOperation + Drop {
    /// Returns path to source file
    fn get_path(&self) -> &Path;

    /// Returns path to backup file
    ///
    /// Defaults to ""
    fn get_backup_path(&self) -> &Path;

    /// Sets the backup path
    fn set_backup_path<S: AsRef<Path>>(&mut self, uuid: S);

    /// Returns path to temp dir
    fn get_temp_dir(&self) -> &Path;

    /// Dispose off resources used by the operation
    ///
    /// It should be called inside [Drop](std::ops::Drop)
    fn dispose(&self) -> io::Result<()> {
        fs::remove_file(self.get_backup_path())
    }

    /// Creates a backup of the source file
    ///
    /// If backup file is successfully created, method should call [set_backup_path](#method.set_backup_path)
    fn create_backup_file(&mut self) -> io::Result<()> {
        fs::create_dir_all(&self.get_temp_dir())?;

        let uuid = Uuid::new_v4();
        let mut buffer = [b' '; 36];

        uuid.to_hyphenated().encode_lower(&mut buffer);

        let uuid_str = String::from_utf8(buffer.to_vec())
            .expect(format!("Could not convert buffer to String").as_str());
        let backup_path = Path::new(&self.get_temp_dir())
            .join(uuid_str)
            .to_str()
            .unwrap()
            .to_string();

        let mut buffer = Vec::new();
        let mut dest_file = OpenOptions::new()
            .write(true)
            .create(true)
            .open(&backup_path)?;
        let mut source_file = OpenOptions::new().read(true).open(&self.get_path())?;

        source_file.read_to_end(&mut buffer)?;
        dest_file.write_all(&buffer)?;
        self.set_backup_path(&backup_path);

        Ok(())
    }
}

fn copy_dir<U: AsRef<Path>, V: AsRef<Path>>(from: U, to: V) -> io::Result<()> {
    let mut stack = Vec::new();
    stack.push(PathBuf::from(from.as_ref()));

    let output_root = PathBuf::from(to.as_ref());
    let input_root = PathBuf::from(from.as_ref()).components().count();

    while let Some(working_path) = stack.pop() {
        let src: PathBuf = working_path.components().skip(input_root).collect();

        let dest = if src.components().count() == 0 {
            output_root.clone()
        } else {
            output_root.join(&src)
        };

        if fs::metadata(&dest).is_err() {
            fs::create_dir_all(&dest)?;
        }

        for entry in fs::read_dir(working_path)? {
            let path = entry?.path();

            if path.is_dir() {
                stack.push(path);
            } else {
                match path.file_name() {
                    Some(filename) => {
                        let dest_path = dest.join(filename);
                        fs::copy(&path, &dest_path)?;
                    }
                    None => {
                        return Err(Error::new(
                            ErrorKind::Other,
                            "Could not extract filename from path",
                        ))
                    }
                }
            }
        }
    }

    Ok(())
}

/// A rollbackable Transaction
pub struct Transaction {
    ops: Vec<Box<dyn RollbackableOperation>>,
    execution_count: usize,
}

impl Transaction {
    /// Constructs a new, empty Transaction
    pub fn new() -> Self {
        Self {
            ops: vec![],
            execution_count: 0,
        }
    }

    /// Adds a [CreateFile](struct.CreateFile.html) operation to the transaction
    pub fn create_file<S: AsRef<Path>>(mut self, path: S) -> Transaction {
        self.ops.push(Box::new(CreateFile::new(path)));
        self
    }

    /// Adds a [CreateDirectory](struct.CreateDirectory.html) operation to the transaction
    pub fn create_dir<S: AsRef<Path>>(mut self, path: S) -> Transaction {
        self.ops.push(Box::new(CreateDirectory::new(path)));
        self
    }

    /// Adds a [AppendFile](struct.AppendFile.html) operation to the transaction
    pub fn append_file<S: AsRef<Path>>(
        mut self,
        source: S,
        temp_dir: S,
        data: Vec<u8>,
    ) -> Transaction {
        self.ops
            .push(Box::new(AppendFile::new(source, temp_dir, data)));
        self
    }

    /// Adds a [CopyFile](struct.CopyFile.html) operation to the transaction
    pub fn copy_file<S: AsRef<Path>>(mut self, source: S, dest: S) -> Transaction {
        self.ops.push(Box::new(CopyFile::new(source, dest)));
        self
    }

    /// Adds a [CopyDirectory](struct.CopyDirectory.html) operation to the transaction
    pub fn copy_dir<S: AsRef<Path>>(mut self, source: S, dest: S, temp_dir: S) -> Transaction {
        self.ops
            .push(Box::new(CopyDirectory::new(source, dest, temp_dir)));
        self
    }

    /// Adds a [DeleteFile](struct.DeleteFile.html) operation to the transaction
    pub fn delete_file<S: AsRef<Path>>(mut self, source: S, temp_dir: S) -> Transaction {
        self.ops.push(Box::new(DeleteFile::new(source, temp_dir)));
        self
    }

    /// Adds a [DeleteDirectory](struct.DeleteDirectory.html) operation to the transaction
    pub fn delete_dir<S: AsRef<Path>>(mut self, source: S, temp_dir: S) -> Transaction {
        self.ops
            .push(Box::new(DeleteDirectory::new(source, temp_dir)));
        self
    }

    /// Adds a [MoveFile](type.MoveFile.html) operation to the transaction
    pub fn move_file<S: AsRef<Path>>(mut self, source: S, dest: S) -> Transaction {
        self.ops.push(Box::new(MoveFile::new(source, dest)));
        self
    }

    /// Adds a [MoveDirectory](type.MoveDirectory.html) operation to the transaction
    pub fn move_dir<S: AsRef<Path>>(mut self, source: S, dest: S) -> Transaction {
        self.ops.push(Box::new(MoveDirectory::new(source, dest)));
        self
    }

    /// Adds a [WriteFile](struct.WriteFile.html) operation to the transaction
    pub fn write_file<S: AsRef<Path>>(
        mut self,
        source: S,
        temp_dir: S,
        data: Vec<u8>,
    ) -> Transaction {
        self.ops
            .push(Box::new(WriteFile::new(source, temp_dir, data)));
        self
    }
}

impl RollbackableOperation for Transaction {
    /// Executes the transaction
    fn execute(&mut self) -> io::Result<()> {
        for i in 0..self.ops.len() {
            self.execution_count += 1;
            if let Err(e) = self.ops[i].execute() {
                return Err(e);
            }
        }

        Ok(())
    }

    /// Performs rollback on the transaction
    ///
    /// Only the operations that were executed will be rollbacked
    fn rollback(&self) -> io::Result<()> {
        for i in (0..self.execution_count).rev() {
            if let Err(e) = self.ops[i].rollback() {
                return Err(e);
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[allow(unused)]
    fn transaction_works() {
        let temp_dir = "./tmp";
        let mut tr = Transaction::new()
            .create_file("./created_file_transaction.txt")
            .create_file("./for_delete.txt")
            .create_dir("./inner/create_dir_transaction")
            .create_dir("./for_delete_dir")
            .create_dir("./magic_dir")
            .write_file(
                "./created_file_transaction.txt",
                temp_dir,
                b"Hello World".to_vec(),
            )
            .append_file(
                "./created_file_transaction.txt",
                temp_dir,
                b"Hello World".to_vec(),
            )
            .copy_file(
                "./created_file_transaction.txt",
                "./inner/created_file_transaction.txt",
            )
            .copy_dir("./magic_dir", "./inner/magic_dir", temp_dir)
            .delete_file("./for_delete.txt", temp_dir)
            .delete_dir("./for_delete_dir", temp_dir)
            .move_file(
                "./inner/created_file_transaction.txt",
                "./inner/magic_dir/created_file_transaction.txt",
            )
            .create_dir("./for_moving")
            .move_dir("./for_moving", "./inner/magic_dir/for_moving");

        assert_eq!((), tr.execute().expect("Cannot execute"));
        assert_eq!((), tr.rollback().expect("Cannot Rollback"));
    }
}