From e22c50877d6d9d3ce2784ea91b22a871bba68815 Mon Sep 17 00:00:00 2001 From: Hulto Date: Sat, 2 Apr 2022 11:11:29 -0400 Subject: [PATCH] Eldritch File.Remove (#21) * File.Remove Added docs, testing, and impl. --- .gitignore | 5 ++- docs/_docs/user-guide/eldritch.md | 2 +- implants/eldritch/src/file/remove_impl.rs | 50 +++++++++++++++++++++-- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 98f21086a..c743a498d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Generated by Cargo # will have compiled files and executables /target/ +cmd/implants/eldritch/target/ +cmd/implants/target/ +cmd/implants/Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk @@ -29,4 +32,4 @@ build/** .stats/** # Credentials -.creds/** \ No newline at end of file +.creds/** diff --git a/docs/_docs/user-guide/eldritch.md b/docs/_docs/user-guide/eldritch.md index 65fc23c3d..fcfb5fa7e 100644 --- a/docs/_docs/user-guide/eldritch.md +++ b/docs/_docs/user-guide/eldritch.md @@ -57,7 +57,7 @@ The file.read method will read the contents of a file. If the file or dir ### file.remove `file.remove(path: str) -> None` -The file.remove method is very cool, and will be even cooler when Nick documents it. +The file.remove method will delete a file or directory (and it's contents) specified by path. ### file.rename `file.rename(src: str, dst: str) -> None` diff --git a/implants/eldritch/src/file/remove_impl.rs b/implants/eldritch/src/file/remove_impl.rs index 3d112914e..7ee08a4ca 100644 --- a/implants/eldritch/src/file/remove_impl.rs +++ b/implants/eldritch/src/file/remove_impl.rs @@ -1,5 +1,49 @@ use anyhow::Result; +use std::path::Path; +use std::fs; -pub fn remove(_path: String) -> Result<()> { - unimplemented!("Method unimplemented") -} \ No newline at end of file +pub fn remove(path: String) -> Result<()> { + let res = Path::new(&path); + if res.is_file() { + fs::remove_file(path)?; + } else if res.is_dir() { + fs::remove_dir_all(path)?; + } + Ok(()) +} + + +#[cfg(test)] +mod tests { + use super::*; + use tempfile::{NamedTempFile,tempdir}; + + #[test] + fn remove_file() -> anyhow::Result<()> { + // Create file + let tmp_file = NamedTempFile::new()?; + let path = String::from(tmp_file.path().to_str().unwrap()).clone(); + + // Run our code + remove(path.clone())?; + + // Verify that file has been removed + let res = Path::new(&path).exists(); + assert_eq!(res, false); + Ok(()) + } + #[test] + fn remove_dir() -> anyhow::Result<()> { + // Create dir + let tmp_dir = tempdir()?; + let path = String::from(tmp_dir.path().to_str().unwrap()); + + // Run our code + remove(path.clone())?; + + // Verify that file has been removed + let res = Path::new(&path).exists(); + assert_eq!(res, false); + Ok(()) + } +}