pub struct OpenOptions { /* private fields */ }
Expand description

Options and flags which can be used to configure how a file is opened.

This corresponds to std::fs::OpenOptions.

This OpenOptions has no open method. To open a file with an OptionOptions, first obtain a Dir containing the path, and then call Dir::open_with.

We need to define our own version because the libstd `OpenOptions` doesn't have public accessors that we can use.

Implementations§

Creates a blank new set of options ready for configuration.

This corresponds to std::fs::OpenOptions::new.

Examples found in repository?
src/fs/dir_entry.rs (line 31)
30
31
32
    pub fn open(&self) -> io::Result<fs::File> {
        self.open_with(OpenOptions::new().read(true))
    }
More examples
Hide additional examples
src/windows/fs/copy.rs (line 12)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pub(crate) fn copy_impl(
    from_start: &fs::File,
    from_path: &Path,
    to_start: &fs::File,
    to_path: &Path,
) -> io::Result<u64> {
    let from = open(from_start, from_path, OpenOptions::new().read(true))?;
    let to = open(
        to_start,
        to_path,
        OpenOptions::new().create(true).truncate(true).write(true),
    )?;
    fs::copy(get_path(&from)?, get_path(&to)?)
}
src/windows/fs/dir_utils.rs (line 66)
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
pub(crate) fn dir_options() -> OpenOptions {
    // Set `FILE_FLAG_BACKUP_SEMANTICS` so that we can open directories. Unset
    // `FILE_SHARE_DELETE` so that directories can't be renamed or deleted
    // underneath us, since we use paths to implement many directory operations.
    OpenOptions::new()
        .read(true)
        .dir_required(true)
        .custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
        .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE)
        .clone()
}

/// Like `dir_options`, but additionally request the ability to read the
/// directory entries.
pub(crate) fn readdir_options() -> OpenOptions {
    dir_options().readdir_required(true).clone()
}

/// Return an `OpenOptions` for canonicalizing paths.
pub(crate) fn canonicalize_options() -> OpenOptions {
    OpenOptions::new()
        .read(true)
        .custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
        .clone()
}
src/windows/fs/dir_entry_inner.rs (line 54)
50
51
52
53
54
55
56
57
58
59
60
    pub(crate) fn full_metadata(&self) -> io::Result<Metadata> {
        // If we can open the file, we can get a more complete Metadata which
        // includes `file_index`, `volume_serial_number`, and
        // `number_of_links`.
        let mut opts = OpenOptions::new();
        opts.access_mode(0);
        opts.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS);
        opts.follow(FollowSymlinks::No);
        let opened = self.open(&opts)?;
        Metadata::from_file(&opened)
    }
src/windows/fs/read_link_impl.rs (line 15)
11
12
13
14
15
16
17
18
19
20
21
pub(crate) fn read_link_impl(start: &fs::File, path: &Path) -> io::Result<PathBuf> {
    // Open the link with no access mode, instead of generic read.
    // By default FILE_LIST_DIRECTORY is denied for the junction "C:\Documents and
    // Settings", so this is needed for a common case.
    let mut opts = OpenOptions::new();
    opts.access_mode(0);
    opts.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS);
    opts.follow(FollowSymlinks::No);
    let file = open(start, path, &opts)?;
    winx::file::read_link(&file)
}
src/windows/fs/set_times_impl.rs (line 42)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
fn set_times_inner(
    start: &fs::File,
    path: &Path,
    atime: Option<SystemTimeSpec>,
    mtime: Option<SystemTimeSpec>,
    custom_flags: u32,
) -> io::Result<()> {
    let custom_flags = custom_flags | FILE_FLAG_BACKUP_SEMANTICS;

    // On Windows, `set_times` requires write permissions.
    let file = open(
        start,
        path,
        OpenOptions::new().write(true).custom_flags(custom_flags),
    )?;
    fs_set_times::SetTimes::set_times(
        &file,
        atime.map(SystemTimeSpec::into_std),
        mtime.map(SystemTimeSpec::into_std),
    )
}

Sets the option for read access.

This corresponds to std::fs::OpenOptions::read.

Examples found in repository?
src/fs/dir_entry.rs (line 31)
30
31
32
    pub fn open(&self) -> io::Result<fs::File> {
        self.open_with(OpenOptions::new().read(true))
    }
More examples
Hide additional examples
src/windows/fs/copy.rs (line 12)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pub(crate) fn copy_impl(
    from_start: &fs::File,
    from_path: &Path,
    to_start: &fs::File,
    to_path: &Path,
) -> io::Result<u64> {
    let from = open(from_start, from_path, OpenOptions::new().read(true))?;
    let to = open(
        to_start,
        to_path,
        OpenOptions::new().create(true).truncate(true).write(true),
    )?;
    fs::copy(get_path(&from)?, get_path(&to)?)
}
src/windows/fs/dir_utils.rs (line 67)
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
pub(crate) fn dir_options() -> OpenOptions {
    // Set `FILE_FLAG_BACKUP_SEMANTICS` so that we can open directories. Unset
    // `FILE_SHARE_DELETE` so that directories can't be renamed or deleted
    // underneath us, since we use paths to implement many directory operations.
    OpenOptions::new()
        .read(true)
        .dir_required(true)
        .custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
        .share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE)
        .clone()
}

/// Like `dir_options`, but additionally request the ability to read the
/// directory entries.
pub(crate) fn readdir_options() -> OpenOptions {
    dir_options().readdir_required(true).clone()
}

/// Return an `OpenOptions` for canonicalizing paths.
pub(crate) fn canonicalize_options() -> OpenOptions {
    OpenOptions::new()
        .read(true)
        .custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
        .clone()
}

Sets the option for write access.

This corresponds to std::fs::OpenOptions::write.

Examples found in repository?
src/windows/fs/copy.rs (line 16)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pub(crate) fn copy_impl(
    from_start: &fs::File,
    from_path: &Path,
    to_start: &fs::File,
    to_path: &Path,
) -> io::Result<u64> {
    let from = open(from_start, from_path, OpenOptions::new().read(true))?;
    let to = open(
        to_start,
        to_path,
        OpenOptions::new().create(true).truncate(true).write(true),
    )?;
    fs::copy(get_path(&from)?, get_path(&to)?)
}
More examples
Hide additional examples
src/windows/fs/set_times_impl.rs (line 42)
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
fn set_times_inner(
    start: &fs::File,
    path: &Path,
    atime: Option<SystemTimeSpec>,
    mtime: Option<SystemTimeSpec>,
    custom_flags: u32,
) -> io::Result<()> {
    let custom_flags = custom_flags | FILE_FLAG_BACKUP_SEMANTICS;

    // On Windows, `set_times` requires write permissions.
    let file = open(
        start,
        path,
        OpenOptions::new().write(true).custom_flags(custom_flags),
    )?;
    fs_set_times::SetTimes::set_times(
        &file,
        atime.map(SystemTimeSpec::into_std),
        mtime.map(SystemTimeSpec::into_std),
    )
}

Sets the option for the append mode.

This corresponds to std::fs::OpenOptions::append.

Sets the option for truncating a previous file.

This corresponds to std::fs::OpenOptions::truncate.

Examples found in repository?
src/windows/fs/copy.rs (line 16)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pub(crate) fn copy_impl(
    from_start: &fs::File,
    from_path: &Path,
    to_start: &fs::File,
    to_path: &Path,
) -> io::Result<u64> {
    let from = open(from_start, from_path, OpenOptions::new().read(true))?;
    let to = open(
        to_start,
        to_path,
        OpenOptions::new().create(true).truncate(true).write(true),
    )?;
    fs::copy(get_path(&from)?, get_path(&to)?)
}

Sets the option to create a new file.

This corresponds to std::fs::OpenOptions::create.

Examples found in repository?
src/windows/fs/copy.rs (line 16)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pub(crate) fn copy_impl(
    from_start: &fs::File,
    from_path: &Path,
    to_start: &fs::File,
    to_path: &Path,
) -> io::Result<u64> {
    let from = open(from_start, from_path, OpenOptions::new().read(true))?;
    let to = open(
        to_start,
        to_path,
        OpenOptions::new().create(true).truncate(true).write(true),
    )?;
    fs::copy(get_path(&from)?, get_path(&to)?)
}

Sets the option to always create a new file.

This corresponds to std::fs::OpenOptions::create_new.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

To prevent race conditions on Windows, handles for directories must be opened without FILE_SHARE_DELETE.

Overrides the dwDesiredAccess argument to the call to CreateFile with the specified value. Read more
Sets extra flags for the dwFileFlags argument to the call to CreateFile2 to the specified value (or combines it with attributes and security_qos_flags to set the dwFlagsAndAttributes for CreateFile). Read more
Sets the dwFileAttributes argument to the call to CreateFile2 to the specified value (or combines it with custom_flags and security_qos_flags to set the dwFlagsAndAttributes for CreateFile). Read more
Sets the dwSecurityQosFlags argument to the call to CreateFile2 to the specified value (or combines it with custom_flags and attributes to set the dwFlagsAndAttributes for CreateFile). Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.