Skip to content

Commit

Permalink
Add a create_file_raw method to Jail
Browse files Browse the repository at this point in the history
  • Loading branch information
nitnelave committed Dec 29, 2023
1 parent e33a99b commit 2995789
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/jail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,19 @@ impl Jail {
/// });
/// ```
pub fn create_file<P: AsRef<Path>>(&self, path: P, contents: &str) -> Result<File> {
create_file_raw(path, contents.as_bytes())
}

/// Same as [`create_file`], but with a non-utf8 content.
pub fn create_file_raw<P: AsRef<Path>>(&self, path: P, contents: &[u8]) -> Result<File> {
let path = path.as_ref();
if !path.is_relative() {
return Err("Jail::create_file(): file path is absolute".to_string().into());
}

let file = File::create(self.directory().join(path)).map_err(as_string)?;
let mut writer = BufWriter::new(file);
writer.write_all(contents.as_bytes()).map_err(as_string)?;
writer.write_all(contents).map_err(as_string)?;
Ok(writer.into_inner().map_err(as_string)?)
}

Expand Down

0 comments on commit 2995789

Please sign in to comment.