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

Write files using create_new option to avoid accidental overwrites #855

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 3 deletions components/utils/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs::{copy, create_dir_all, read_dir, File};
use std::fs::{copy, create_dir_all, read_dir, File, OpenOptions};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
Expand All @@ -19,8 +19,7 @@ pub fn is_path_in_directory(parent: &Path, path: &Path) -> Result<bool> {

/// Create a file with the content given
pub fn create_file(path: &Path, content: &str) -> Result<()> {
let mut file =
File::create(&path).map_err(|e| Error::chain(format!("Failed to create {:?}", path), e))?;
let mut file = OpenOptions::new().write(true).create_new(true).open(&path).map_err(|e| Error::chain(format!("Failed to create {:?}", path), e))?;
file.write_all(content.as_bytes())?;
Ok(())
}
Expand Down