Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c601230

Browse files
authored
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
1 parent 38b5d37 commit c601230

5 files changed

Lines changed: 43 additions & 11 deletions

File tree

.changes/fs-create-new.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"fs": "patch"
3+
"fs-js": "patch"
4+
---
5+
6+
Add `createNew` option for `writeFile` and `writeTextFile` to create the file if doesn't exist and fail if it does.

.changes/fs-trunacte-non-append.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"fs": "patch"
3+
"fs-js": "patch"
4+
---
5+
6+
Truncate files when using `writeFile` and `writeTextFile` with `append: false`.

.changes/fs-write-panic.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"fs": "patch"
3+
"fs-js": "patch"
4+
---
5+
6+
Fix panic when using `writeFile` or `writeTextFile` without passing an option object.

plugins/fs/guest-js/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,8 @@ interface WriteFileOptions {
980980
append?: boolean;
981981
/** Sets the option to allow creating a new file, if one doesn't already exist at the specified path (defaults to `true`). */
982982
create?: boolean;
983+
/** Sets the option to create a new file, failing if it already exists. */
984+
createNew?: boolean;
983985
/** File permissions. Ignored on Windows. */
984986
mode?: number;
985987
/** Base directory for `path` */
@@ -1023,7 +1025,7 @@ async function writeFile(
10231025
*
10241026
* await writeTextFile('file.txt', "Hello world", { dir: BaseDirectory.App });
10251027
* ```
1026-
*
1028+
*
10271029
* @since 2.0.0
10281030
*/
10291031
async function writeTextFile(

plugins/fs/src/commands.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,16 @@ pub fn write<R: Runtime>(
579579
}
580580

581581
#[derive(Debug, Clone, Deserialize)]
582+
#[serde(rename_all = "camelCase")]
582583
pub struct WriteFileOptions {
583584
#[serde(flatten)]
584585
base: BaseOptions,
585-
append: Option<bool>,
586-
create: Option<bool>,
586+
#[serde(default)]
587+
append: bool,
588+
#[serde(default)]
589+
create: bool,
590+
#[serde(default)]
591+
create_new: bool,
587592
#[allow(unused)]
588593
mode: Option<u32>,
589594
}
@@ -597,18 +602,25 @@ fn write_file_inner<R: Runtime>(
597602
let resolved_path = resolve_path(&app, path, options.as_ref().and_then(|o| o.base.base_dir))?;
598603

599604
let mut opts = std::fs::OpenOptions::new();
600-
opts.append(options.as_ref().map(|o| o.append.unwrap_or(false)).unwrap());
601-
opts.create(options.as_ref().map(|o| o.create.unwrap_or(true)).unwrap());
605+
// defaults
606+
opts.read(false).write(true).truncate(true).create(true);
602607

603-
#[cfg(unix)]
604-
{
605-
use std::os::unix::fs::OpenOptionsExt;
606-
if let Some(Some(mode)) = options.map(|o| o.mode) {
607-
opts.mode(mode & 0o777);
608+
if let Some(options) = options {
609+
#[cfg(unix)]
610+
{
611+
use std::os::unix::fs::OpenOptionsExt;
612+
if let Some(mode) = options.mode {
613+
opts.mode(mode);
614+
}
608615
}
616+
617+
opts.create(options.create)
618+
.append(options.append)
619+
.truncate(!options.append)
620+
.create_new(options.create_new);
609621
}
610622

611-
let mut file = opts.write(true).open(&resolved_path).map_err(|e| {
623+
let mut file = opts.open(&resolved_path).map_err(|e| {
612624
format!(
613625
"failed to open file at path: {} with error: {e}",
614626
resolved_path.display()

0 commit comments

Comments
 (0)