Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 
File.Moveto method implemented, tested, and doc'd (#25)
  • Loading branch information
hulto authored Jun 21, 2022
1 parent b02cb63 commit 24d220b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 10 deletions.
6 changes: 3 additions & 3 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ The <b>file.read</b> method will read the contents of a file. If the file or dir

The <b>file.remove</b> method will delete a file or directory (and it's contents) specified by path.

### file.rename
`file.rename(src: str, dst: str) -> None`
### file.moveto
`file.moveto(src: str, dst: str) -> None`

The <b>file.rename</b> method is very cool, and will be even cooler when Nick documents it.
The <b>file.moveto</b> method will move a file or directory from src to dst. If the dst directory or file exists it will be deleted before being replaced. To ensure consistency across systems.

### file.replace
`file.replace(path: str, pattern: str, value: str) -> None`
Expand Down
4 changes: 2 additions & 2 deletions implants/eldritch/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod is_dir_impl;
mod mkdir_impl;
mod read_impl;
mod remove_impl;
mod rename_impl;
mod moveto_impl;
mod replace_all_impl;
mod replace_impl;
mod timestomp_impl;
Expand Down Expand Up @@ -80,7 +80,7 @@ fn methods(builder: &mut MethodsBuilder) {
Ok(NoneType{})
}
fn rename(_this: FileLibrary, old: String, new: String) -> NoneType {
rename_impl::rename(old, new)?;
moveto_impl::moveto(old, new)?;
Ok(NoneType{})
}
fn replace_all(_this: FileLibrary, path: String, pattern: String, value: String) -> NoneType {
Expand Down
102 changes: 102 additions & 0 deletions implants/eldritch/src/file/moveto_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use anyhow::Result;
use std::fs;
use std::path::Path;

pub fn moveto(old: String, new: String) -> Result<()> {
// If path is a dir delete it.
// This will help unify behavior across systems.
// https://doc.rust-lang.org/std/fs/fn.rename.html#platform-specific-behavior
if Path::new(&new.clone()).is_dir() {
fs::remove_dir_all(new.clone())?;
} else if Path::new(&new.clone()).is_file() {
fs::remove_file(new.clone())?;
}

fs::rename(old, new)?;
Ok(())
}


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

#[test]
fn test_moveto_dir() -> anyhow::Result<()>{
let tmp_dir = tempdir()?;
let path_old = String::from(tmp_dir.path().to_str().unwrap()).clone();
let tmp_dir_new = tempdir()?;
let path_new = String::from(tmp_dir_new.path().to_str().unwrap()).clone();
tmp_dir_new.close()?;


moveto(path_old.clone(), path_new.clone())?;

assert_eq!(Path::new(&path_old).is_dir(), false);
assert_eq!(Path::new(&path_new).is_dir(), true);
Ok(())
}
#[test]
fn test_moveto_file() -> anyhow::Result<()>{
let tmp_file = NamedTempFile::new()?;
let path_old = String::from(tmp_file.path().to_str().unwrap()).clone();
let tmp_file_new = NamedTempFile::new()?;
let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone();
tmp_file_new.close()?;
// Run our code

moveto(path_old.clone(), path_new.clone())?;

assert_eq!(Path::new(&path_old).is_file(), false);
assert_eq!(Path::new(&path_new).is_file(), true);
Ok(())
}
#[test]
fn test_moveto_file_overwrites() -> anyhow::Result<()>{
let tmp_file = NamedTempFile::new()?;
let path_old = String::from(tmp_file.path().to_str().unwrap()).clone();
let tmp_file_new = NamedTempFile::new()?;
let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone();
// Run our code

moveto(path_old.clone(), path_new.clone())?;

assert_eq!(Path::new(&path_old).is_file(), false);
assert_eq!(Path::new(&path_new).is_file(), true);
Ok(())
}
#[test]
fn test_moveto_dir_overwrites() -> anyhow::Result<()>{
// Create initial directory and file.
let tmp_dir = tempdir()?;
let tmp_dir_path = tmp_dir.path();

let path_old = String::from(tmp_dir_path.to_str().unwrap()).clone();
let path_old_file = String::from(tmp_dir_path.join("myfile2").to_str().unwrap()).clone();
let _ = File::create(path_old_file.clone())?;
let _ = fs::write(path_old_file.clone(),"Hello");

// Create destination directory and the file path we expect after moveto "win"
let tmp_dir_new = tempdir()?;
let path_new = String::from(tmp_dir_new.path().to_str().unwrap()).clone();
let path_new_file = String::from(tmp_dir_new.path().join("myfile").to_str().unwrap()).clone();
let path_new_file_win = String::from(tmp_dir_new.path().join("myfile2").to_str().unwrap()).clone();

let _ = File::create(path_new_file.clone())?;


// Run our code
moveto(path_old.clone(), path_new.clone())?;

// Assert
assert_eq!(Path::new(&path_old).is_dir(), false);
assert_eq!(Path::new(&path_new_file_win).is_file(), true);
assert_eq!(Path::new(&path_new_file).is_file(), false);
assert_eq!(Path::new(&path_new).is_dir(), true);
Ok(())
}

}
5 changes: 0 additions & 5 deletions implants/eldritch/src/file/rename_impl.rs

This file was deleted.

0 comments on commit 24d220b

Please sign in to comment.