From c60123093ddf725af7228494182fed697ff8b021 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 27 Dec 2023 15:27:45 +0200 Subject: [PATCH] fix(fs): fix panic due to unwrap & truncate by default (#847) * fix(fs): fix panic due to unwrap & truncate by default closes #846 * fmt and change file --- .changes/fs-create-new.md | 6 ++++++ .changes/fs-trunacte-non-append.md | 6 ++++++ .changes/fs-write-panic.md | 6 ++++++ plugins/fs/guest-js/index.ts | 4 +++- plugins/fs/src/commands.rs | 32 ++++++++++++++++++++---------- 5 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 .changes/fs-create-new.md create mode 100644 .changes/fs-trunacte-non-append.md create mode 100644 .changes/fs-write-panic.md diff --git a/.changes/fs-create-new.md b/.changes/fs-create-new.md new file mode 100644 index 00000000..6ad2ce15 --- /dev/null +++ b/.changes/fs-create-new.md @@ -0,0 +1,6 @@ +--- +"fs": "patch" +"fs-js": "patch" +--- + +Add `createNew` option for `writeFile` and `writeTextFile` to create the file if doesn't exist and fail if it does. diff --git a/.changes/fs-trunacte-non-append.md b/.changes/fs-trunacte-non-append.md new file mode 100644 index 00000000..4d49407b --- /dev/null +++ b/.changes/fs-trunacte-non-append.md @@ -0,0 +1,6 @@ +--- +"fs": "patch" +"fs-js": "patch" +--- + +Truncate files when using `writeFile` and `writeTextFile` with `append: false`. diff --git a/.changes/fs-write-panic.md b/.changes/fs-write-panic.md new file mode 100644 index 00000000..96983f00 --- /dev/null +++ b/.changes/fs-write-panic.md @@ -0,0 +1,6 @@ +--- +"fs": "patch" +"fs-js": "patch" +--- + +Fix panic when using `writeFile` or `writeTextFile` without passing an option object. diff --git a/plugins/fs/guest-js/index.ts b/plugins/fs/guest-js/index.ts index 545e800c..1e1c0e60 100644 --- a/plugins/fs/guest-js/index.ts +++ b/plugins/fs/guest-js/index.ts @@ -980,6 +980,8 @@ interface WriteFileOptions { append?: boolean; /** Sets the option to allow creating a new file, if one doesn't already exist at the specified path (defaults to `true`). */ create?: boolean; + /** Sets the option to create a new file, failing if it already exists. */ + createNew?: boolean; /** File permissions. Ignored on Windows. */ mode?: number; /** Base directory for `path` */ @@ -1023,7 +1025,7 @@ async function writeFile( * * await writeTextFile('file.txt', "Hello world", { dir: BaseDirectory.App }); * ``` - * + * * @since 2.0.0 */ async function writeTextFile( diff --git a/plugins/fs/src/commands.rs b/plugins/fs/src/commands.rs index bde9a9ea..d8e1f1fc 100644 --- a/plugins/fs/src/commands.rs +++ b/plugins/fs/src/commands.rs @@ -579,11 +579,16 @@ pub fn write( } #[derive(Debug, Clone, Deserialize)] +#[serde(rename_all = "camelCase")] pub struct WriteFileOptions { #[serde(flatten)] base: BaseOptions, - append: Option, - create: Option, + #[serde(default)] + append: bool, + #[serde(default)] + create: bool, + #[serde(default)] + create_new: bool, #[allow(unused)] mode: Option, } @@ -597,18 +602,25 @@ fn write_file_inner( let resolved_path = resolve_path(&app, path, options.as_ref().and_then(|o| o.base.base_dir))?; let mut opts = std::fs::OpenOptions::new(); - opts.append(options.as_ref().map(|o| o.append.unwrap_or(false)).unwrap()); - opts.create(options.as_ref().map(|o| o.create.unwrap_or(true)).unwrap()); + // defaults + opts.read(false).write(true).truncate(true).create(true); - #[cfg(unix)] - { - use std::os::unix::fs::OpenOptionsExt; - if let Some(Some(mode)) = options.map(|o| o.mode) { - opts.mode(mode & 0o777); + if let Some(options) = options { + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + if let Some(mode) = options.mode { + opts.mode(mode); + } } + + opts.create(options.create) + .append(options.append) + .truncate(!options.append) + .create_new(options.create_new); } - let mut file = opts.write(true).open(&resolved_path).map_err(|e| { + let mut file = opts.open(&resolved_path).map_err(|e| { format!( "failed to open file at path: {} with error: {e}", resolved_path.display()