Skip to content
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
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: Option<str>) -> str`

The <b> file.temp</b> method returns the path of a new temporary file with a random filename or the optional filename provided as an 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: Option<String>) -> anyhow::Result<String> {
temp_file_impl::temp_file(name)
}

}
51 changes: 51 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,51 @@
use anyhow::Result;
use std::env;
use std::fs::File;
use tempfile::NamedTempFile;

pub fn temp_file(name: Option<String>) -> Result<String> {
let mut temp_path;

match name {
None => {
// Generate a random file name if name is not provided
let tfile = NamedTempFile::new()?;
(_, temp_path) = tfile.keep()?;
}
Some(n) => {
temp_path = env::temp_dir();
temp_path.push(n);
_ = File::create(&temp_path)?;
}
}
// Create the file in the temporary directory

Ok(temp_path.display().to_string())
}

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

#[test]
fn test_temp_file_w_name() -> anyhow::Result<()> {
// Create file with a name
let p = temp_file(Some("foo".to_string()))?;
// check if file exists
let t = Path::new(&p);
assert!(t.exists());
assert!(t.file_name().unwrap() == "foo");

Ok(())
}
#[test]
fn test_temp_file_r_name() -> anyhow::Result<()> {
// Create file with a random name
let p = temp_file(None)?;
// check if file exists
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.