Skip to content

Commit

Permalink
Implement undo(mrl) file (closes #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Aug 4, 2020
1 parent e2bb718 commit 7fbc9cc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
**/*.rs.bk
/mrl
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
pub mod libgen;

pub mod mrl {

use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::LineWriter;

pub fn create(l: &Vec<String>) -> Result<(), io::Error> {
if l.get(0).is_some() {
let file = File::create("./mrl")?;
let mut file = LineWriter::new(file);
for i in l {
file.write_all(i.as_bytes())?;
file.write_all(b"\n")?;
}
file.flush()?
}
Ok(())
}
}

pub mod waste {
pub struct WasteFolderProperties {
pub parent: String,
Expand Down
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ fn main() -> Result<(), io::Error> {
let date_now = Local::now();
let deletion_date = date_now.format("%Y-%m-%dT%H:%M:%S").to_string();

let mut renamed_list: Vec<String> = Vec::new();

// This will be changed later; the subscript number for waste_list depends on whether or not
// the file being rmw'ed is
// on the same filesystem as the WASTE folder.
Expand All @@ -64,6 +66,7 @@ fn main() -> Result<(), io::Error> {
// Trash specification<https://specifications.freedesktop.org/trash-spec/trashspec-latest.html>.
for file in &opt.files {
let basename = libgen::get_basename(&file).to_str().unwrap();

let file_absolute = file.canonicalize().unwrap().display().to_string();

// Will need more error-checking to prevent overwriting existing destination files.
Expand All @@ -72,9 +75,17 @@ fn main() -> Result<(), io::Error> {
println!("'{}' -> '{}'", file.display(), destination);
rename(file, &destination).expect("Error renaming file");

renamed_list.push(file_absolute.clone());

let contents = trashinfo::create_contents(&file_absolute, &deletion_date);
trashinfo::create(&basename, &waste.info, contents).expect("Error writing trashinfo file");
}

// I don't think we need a unit test for mrl file creation; when there's a restore
// and undo function,
// it can be tested easily using the bin script test.
oxi_rmw::mrl::create(&renamed_list)?;

Ok(())
}

Expand Down

0 comments on commit 7fbc9cc

Please sign in to comment.