diff options
author | Michał Sabiniarz <31597105+mhvsa@users.noreply.github.com> | 2020-01-21 10:49:42 +0100 |
---|---|---|
committer | Bartek Iwańczuk <biwanczuk@gmail.com> | 2020-01-21 10:49:42 +0100 |
commit | 21cc9cb7a76d805fbb7b53583448aa101c294e71 (patch) | |
tree | be8cb74bc49c06d299e8c79ec406c484a64bd88f /cli/ops/files.rs | |
parent | 7966bf14c062a05b1606a62c996890571454ecc8 (diff) |
Implemented alternative open mode in files (#3119)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/ops/files.rs')
-rw-r--r-- | cli/ops/files.rs | 125 |
1 files changed, 82 insertions, 43 deletions
diff --git a/cli/ops/files.rs b/cli/ops/files.rs index 8bb3c8acb..3c17a607e 100644 --- a/cli/ops/files.rs +++ b/cli/ops/files.rs @@ -26,7 +26,20 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) { struct OpenArgs { promise_id: Option<u64>, filename: String, - mode: String, + options: Option<OpenOptions>, + mode: Option<String>, +} + +#[derive(Deserialize, Default, Debug)] +#[serde(rename_all = "camelCase")] +#[serde(default)] +struct OpenOptions { + read: bool, + write: bool, + create: bool, + truncate: bool, + append: bool, + create_new: bool, } fn op_open( @@ -36,56 +49,82 @@ fn op_open( ) -> Result<JsonOp, ErrBox> { let args: OpenArgs = serde_json::from_value(args)?; let filename = deno_fs::resolve_from_cwd(Path::new(&args.filename))?; - let mode = args.mode.as_ref(); let state_ = state.clone(); let mut open_options = tokio::fs::OpenOptions::new(); - match mode { - "r" => { - open_options.read(true); - } - "r+" => { - open_options.read(true).write(true); - } - "w" => { - open_options.create(true).write(true).truncate(true); - } - "w+" => { - open_options - .read(true) - .create(true) - .write(true) - .truncate(true); - } - "a" => { - open_options.create(true).append(true); - } - "a+" => { - open_options.read(true).create(true).append(true); - } - "x" => { - open_options.create_new(true).write(true); - } - "x+" => { - open_options.create_new(true).read(true).write(true); - } - &_ => { - panic!("Unknown file open mode."); - } - } - - match mode { - "r" => { + if let Some(options) = args.options { + if options.read { state.check_read(&filename)?; } - "w" | "a" | "x" => { + + if options.write || options.append { state.check_write(&filename)?; } - &_ => { - state.check_read(&filename)?; - state.check_write(&filename)?; + + open_options + .read(options.read) + .create(options.create) + .write(options.write) + .truncate(options.truncate) + .append(options.append) + .create_new(options.create_new); + } else if let Some(mode) = args.mode { + let mode = mode.as_ref(); + match mode { + "r" => { + state.check_read(&filename)?; + } + "w" | "a" | "x" => { + state.check_write(&filename)?; + } + &_ => { + state.check_read(&filename)?; + state.check_write(&filename)?; + } + }; + + match mode { + "r" => { + open_options.read(true); + } + "r+" => { + open_options.read(true).write(true); + } + "w" => { + open_options.create(true).write(true).truncate(true); + } + "w+" => { + open_options + .read(true) + .create(true) + .write(true) + .truncate(true); + } + "a" => { + open_options.create(true).append(true); + } + "a+" => { + open_options.read(true).create(true).append(true); + } + "x" => { + open_options.create_new(true).write(true); + } + "x+" => { + open_options.create_new(true).read(true).write(true); + } + &_ => { + return Err(ErrBox::from(DenoError::new( + ErrorKind::Other, + "Unknown open mode.".to_string(), + ))); + } } - } + } else { + return Err(ErrBox::from(DenoError::new( + ErrorKind::Other, + "Open requires either mode or options.".to_string(), + ))); + }; let is_sync = args.promise_id.is_none(); |