Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eldritch File.Remove #21

Merged
merged 5 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -29,4 +32,4 @@ build/**
.stats/**

# Credentials
.creds/**
.creds/**
2 changes: 1 addition & 1 deletion docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The <b>file.read</b> method will read the contents of a file. If the file or dir
### file.remove
`file.remove(path: str) -> None`

The <b>file.remove</b> method is very cool, and will be even cooler when Nick documents it.
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`
Expand Down
50 changes: 47 additions & 3 deletions implants/eldritch/src/file/remove_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
use anyhow::Result;
use std::path::Path;
use std::fs;

pub fn remove(_path: String) -> Result<()> {
unimplemented!("Method unimplemented")
}
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(())
}
}