Function cap_primitives::fs::open
source · pub fn open(start: &File, path: &Path, options: &OpenOptions) -> Result<File>
Expand description
Perform an openat
-like operation, ensuring that the resolution of the
path never escapes the directory tree rooted at start
.
Examples found in repository?
src/fs/open_dir.rs (line 16)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
pub fn open_dir(start: &fs::File, path: &Path) -> io::Result<fs::File> {
open(start, path, &dir_options())
}
/// Like `open_dir`, but additionally request the ability to read the directory
/// entries.
#[cfg(not(windows))]
#[inline]
pub(crate) fn open_dir_for_reading(
start: &fs::File,
path: &Path,
follow: FollowSymlinks,
) -> io::Result<fs::File> {
open(start, path, readdir_options().follow(follow))
}
/// Similar to `open_dir`, but fails if the path names a symlink.
#[inline]
pub fn open_dir_nofollow(start: &fs::File, path: &Path) -> io::Result<fs::File> {
open(start, path, dir_options().follow(FollowSymlinks::No))
}
More 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/set_times_impl.rs (lines 39-43)
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),
)
}
src/windows/fs/dir_entry_inner.rs (lines 35-39)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
pub(crate) fn open(&self, options: &OpenOptions) -> io::Result<fs::File> {
match options.follow {
FollowSymlinks::No => {
let (opts, manually_trunc) = open_options_to_std(options);
let file = opts.open(self.std.path())?;
if manually_trunc {
// Unwrap is ok because 0 never overflows, and we'll only
// have `manually_trunc` set when the file is opened for
// writing.
file.set_len(0).unwrap();
}
Ok(file)
}
FollowSymlinks::Yes => {
let path = self.std.path();
open(
&open_ambient_dir(path.parent().unwrap(), ambient_authority())?,
path.file_name().unwrap().as_ref(),
options,
)
}
}
}