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

adding file.temp_file to eldritch #797

Merged
merged 8 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ The <b>file.replace</b> method finds the first string matching a regex pattern i

The <b>file.replace_all</b> method finds all strings matching a regex pattern in the specified file and replaces them with the value. Please consult the [Rust Regex Docs](https://rust-lang-nursery.github.io/rust-cookbook/text/regex.html) for more information on pattern matching.

### file.tmp_file

`file.tmp_file(name: str) -> str`
adm1nPanda marked this conversation as resolved.
Show resolved Hide resolved

The <b> file.temp</b> method returns the path of a new temporary file with the name provided as the argument.

### file.template

`file.template(template_path: str, dst: str, args: Dict<String, Value>, autoescape: bool) -> None`
Expand Down
7 changes: 7 additions & 0 deletions implants/lib/eldritch/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod read_impl;
mod remove_impl;
mod replace_all_impl;
mod replace_impl;
mod temp_file_impl;
mod template_impl;
mod timestomp_impl;
mod write_impl;
Expand Down Expand Up @@ -176,4 +177,10 @@ fn methods(builder: &mut MethodsBuilder) {
follow_impl::follow(path, f, eval)?;
Ok(NoneType{})
}

#[allow(unused_variables)]
fn temp_file(this: &FileLibrary, name: String) -> anyhow::Result<String> {
temp_file_impl::temp_file(name)
}

}
46 changes: 46 additions & 0 deletions implants/lib/eldritch/src/file/temp_file_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use anyhow::Result;
use std::fs;
use std::path::PathBuf;
use tempfile::NamedTempFile;

pub fn temp_file(name: String) -> Result<String> {
adm1nPanda marked this conversation as resolved.
Show resolved Hide resolved
adm1nPanda marked this conversation as resolved.
Show resolved Hide resolved
//create a file in temp folder
let tmp_file = NamedTempFile::new()?;
let tdir: PathBuf = tmp_file.path().parent().unwrap().into();
let new_path = tdir.join(name);
adm1nPanda marked this conversation as resolved.
Show resolved Hide resolved
let (_tf, tpath) = tmp_file.keep()?;

fs::rename(&tpath, &new_path)?;

Ok(String::from(new_path.to_str().unwrap()).clone())
}

#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;

#[test]
fn test_temp_file() -> anyhow::Result<()> {
// Create file
let p = temp_file("foo".to_string())?;
// check if file exists
assert!(Path::new(&p).exists());

Ok(())
}
#[test]
adm1nPanda marked this conversation as resolved.
Show resolved Hide resolved
fn test_temp_no_file() -> anyhow::Result<()> {
// Create file and then delete it (so we know it doesnt exist)
let p = temp_file("foo".to_string())?;
if Path::new(&p).exists() {
// delete the file
fs::remove_file(&p)?;
}

// check if file exists
adm1nPanda marked this conversation as resolved.
Show resolved Hide resolved
assert!(!Path::new(&p).exists());

Ok(())
}
}
2 changes: 1 addition & 1 deletion implants/lib/eldritch/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod tests {
parameters: HashMap::new(),
file_names: Vec::new(),
},
want_text: format!("{}\n", r#"["append", "compress", "copy", "exists", "find", "follow", "is_dir", "is_file", "list", "mkdir", "moveto", "parent_dir", "read", "remove", "replace", "replace_all", "template", "timestomp", "write"]"#),
want_text: format!("{}\n", r#"["append", "compress", "copy", "exists", "find", "follow", "is_dir", "is_file", "list", "mkdir", "moveto", "parent_dir", "read", "remove", "replace", "replace_all", "temp_file", "template", "timestomp", "write"]"#),
want_error: None,
},
process_bindings: TestCase {
Expand Down
1 change: 1 addition & 0 deletions implants/lib/pb/src/generated/c2.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions implants/lib/pb/src/generated/eldritch.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading