Skip to content

Commit

Permalink
fix(fs): fix panic due to unwrap & truncate by default (#847)
Browse files Browse the repository at this point in the history
* fix(fs): fix panic due to unwrap & truncate by default

closes #846

* fmt and change file
  • Loading branch information
amrbashir authored Dec 27, 2023
1 parent 38b5d37 commit c601230
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changes/fs-create-new.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 6 additions & 0 deletions .changes/fs-trunacte-non-append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"fs": "patch"
"fs-js": "patch"
---

Truncate files when using `writeFile` and `writeTextFile` with `append: false`.
6 changes: 6 additions & 0 deletions .changes/fs-write-panic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"fs": "patch"
"fs-js": "patch"
---

Fix panic when using `writeFile` or `writeTextFile` without passing an option object.
4 changes: 3 additions & 1 deletion plugins/fs/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` */
Expand Down Expand Up @@ -1023,7 +1025,7 @@ async function writeFile(
*
* await writeTextFile('file.txt', "Hello world", { dir: BaseDirectory.App });
* ```
*
*
* @since 2.0.0
*/
async function writeTextFile(
Expand Down
32 changes: 22 additions & 10 deletions plugins/fs/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,16 @@ pub fn write<R: Runtime>(
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WriteFileOptions {
#[serde(flatten)]
base: BaseOptions,
append: Option<bool>,
create: Option<bool>,
#[serde(default)]
append: bool,
#[serde(default)]
create: bool,
#[serde(default)]
create_new: bool,
#[allow(unused)]
mode: Option<u32>,
}
Expand All @@ -597,18 +602,25 @@ fn write_file_inner<R: Runtime>(
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()
Expand Down

0 comments on commit c601230

Please sign in to comment.