From d2de3f2e7b4b7a6b574540c96c82618884995b88 Mon Sep 17 00:00:00 2001 From: Hulto Date: Tue, 15 Mar 2022 02:14:31 +0000 Subject: [PATCH 1/2] File.Remove Added docs, testing, and impl. --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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/** From d420d3910f37baa85420ae3dd5ac2ff79563fe8c Mon Sep 17 00:00:00 2001 From: Hulto Date: Tue, 15 Mar 2022 02:15:14 +0000 Subject: [PATCH 2/2] File.Remove add docs, tests, and impl. --- docs/_docs/user-guide/eldritch.md | 2 +- implants/eldritch/src/file/remove_impl.rs | 50 +++++++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/docs/_docs/user-guide/eldritch.md b/docs/_docs/user-guide/eldritch.md index 2a22115ee..29964ef59 100644 --- a/docs/_docs/user-guide/eldritch.md +++ b/docs/_docs/user-guide/eldritch.md @@ -57,7 +57,7 @@ The file.read method is very cool, and will be even cooler when Nick docu ### 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(()) + } +}