From 21cc9cb7a76d805fbb7b53583448aa101c294e71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sabiniarz?= <31597105+mhvsa@users.noreply.github.com> Date: Tue, 21 Jan 2020 10:49:42 +0100 Subject: Implemented alternative open mode in files (#3119) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bartek IwaƄczuk --- cli/ops/files.rs | 125 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 82 insertions(+), 43 deletions(-) (limited to 'cli/ops/files.rs') 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, filename: String, - mode: String, + options: Option, + mode: Option, +} + +#[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 { 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(); -- cgit v1.2.3