Struct compio_fs::OpenOptions
source · pub struct OpenOptions(/* private fields */);
Expand description
Options and flags which can be used to configure how a file is opened.
This builder exposes the ability to configure how a File
is opened and
what operations are permitted on the open file. The File::open
and
File::create
methods are aliases for commonly used options using this
builder.
Generally speaking, when using OpenOptions
, you’ll first call
OpenOptions::new
, then chain calls to methods to set each option, then
call OpenOptions::open
, passing the path of the file you’re trying to
open. This will give you a std::io::Result
with a File
inside that
you can further operate on.
Examples
Opening a file to read:
use compio_fs::OpenOptions;
let file = OpenOptions::new().read(true).open("foo.txt").unwrap();
Opening a file for both reading and writing, as well as creating it if it doesn’t exist:
use compio_fs::OpenOptions;
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open("foo.txt")
.unwrap();
Implementations§
source§impl OpenOptions
impl OpenOptions
sourcepub fn read(self, read: bool) -> Self
pub fn read(self, read: bool) -> Self
Sets the option for read access.
This option, when true, will indicate that the file should be
read
-able if opened.
sourcepub fn write(self, write: bool) -> Self
pub fn write(self, write: bool) -> Self
Sets the option for write access.
This option, when true, will indicate that the file should be
write
-able if opened.
sourcepub fn truncate(self, truncate: bool) -> Self
pub fn truncate(self, truncate: bool) -> Self
Sets the option for truncating a previous file.
If a file is successfully opened with this option set it will truncate the file to 0 length if it already exists.
The file must be opened with write access for truncate to work.
sourcepub fn create(self, create: bool) -> Self
pub fn create(self, create: bool) -> Self
Sets the option to create a new file, or open it if it already exists.
In order for the file to be created, OpenOptions::write
access must
be used.
sourcepub fn create_new(self, create_new: bool) -> Self
pub fn create_new(self, create_new: bool) -> Self
Sets the option to create a new file, failing if it already exists.
No file is allowed to exist at the target location, also no (dangling) symlink. In this way, if the call succeeds, the file returned is guaranteed to be new.
This option is useful because it is atomic. Otherwise between checking whether a file exists and creating a new one, the file may have been created by another process (a TOCTOU race condition / attack).
If .create_new(true)
is set, .create()
and .truncate()
are
ignored.
The file must be opened with write or append access in order to create a new file.
Trait Implementations§
source§impl Clone for OpenOptions
impl Clone for OpenOptions
source§fn clone(&self) -> OpenOptions
fn clone(&self) -> OpenOptions
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more