mirror of
https://github.com/denoland/deno.git
synced 2025-08-03 10:33:54 +00:00
Implemented alternative open mode in files (#3119)
Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
parent
7966bf14c0
commit
21cc9cb7a7
5 changed files with 298 additions and 55 deletions
125
cli/ops/files.rs
125
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);
|
||||
if let Some(options) = args.options {
|
||||
if options.read {
|
||||
state.check_read(&filename)?;
|
||||
}
|
||||
"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" => {
|
||||
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();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue