-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Implementation of create file #1282
Conversation
File open modes in Python (taken from https://docs.python.org/3/library/io.html#io.FileIO) The mode can be 'r', 'w', 'x' or 'a' for reading (default), writing, exclusive creation or appending. The file will be created if it doesn’t exist when opened for writing or appending; it will be truncated when opened for writing. FileExistsError will be raised if it already exists when opened for creating. Opening a file for creating implies writing, so this mode behaves in a similar way to 'w'. Add a '+' to the mode to allow simultaneous reading and writing. Which translates to roughly this:
Opinions welcome. CC: @ry @kitsonk @piscisaureus |
@bartlomieju I would totally defer to Ryan or @piscisaureus on this one. I am not really good at having well informed opinions about OS APIs. |
Sure, thanks for letting know. |
Go's and Ruby's file mode for reference: const (
// Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
O_RDONLY int = syscall.O_RDONLY // open the file read-only.
O_WRONLY int = syscall.O_WRONLY // open the file write-only.
O_RDWR int = syscall.O_RDWR // open the file read-write.
// The remaining values may be or'ed in to control behavior.
O_APPEND int = syscall.O_APPEND // append data to the file when writing.
O_CREATE int = syscall.O_CREAT // create a new file if none exists.
O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist.
O_SYNC int = syscall.O_SYNC // open for synchronous I/O.
O_TRUNC int = syscall.O_TRUNC // if possible, truncate file when opened.
) https://ruby-doc.org/core-2.2.2/IO.html#method-c-new-label-IO+Open+Mode
|
This looks great! r w x a with optional plus is the time tested interface. Can you add some more tests? Add TODOs for the missing ones. |
@ry I added Please check how OpenOptions::append behaves. Do we need to care about position of reading? I will write full tests for each mode tomorrow morning. I expect to find some quirky behavior along the way that will have to be pinpointed. EDIT: grammar |
panic!("Unknown file open mode."); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I move this whole operation somewhere else?
Probably some comments explaining why each option is used would be appropriate as well.
I got a bit confused, I found that Deno already has cc @ry |
@bartlomieju Regarding |
@ry I resolved your remarks. Please let know if To finish tests we need to have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - thanks for seeing this through!
- console.assert should not throw error (denoland#1335) - Support more modes in deno.open (denoland#1282, denoland#1336) - Simplify code fetch logic (denoland#1322) - readDir entry mode (denoland#1326) - Use stderr for exceptions (denoland#1303) - console.log formatting improvements (denoland#1327, denoland#1299) - Expose TooLarge error code for buffers (denoland#1298)
Trying to implement simple logger in deno I found that creating a file is still impossible.
I saw work done in #908, but I turns out we can simply use
tokio::fs::OpenOptions
to configure what mode we want to open file in.This is very rough minimal implementation that still needs a lot of work.
TODO:
OpenMode
s and implement asenum
infiles.ts
tokio::fs::OpenOptions
inops.ts