diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-06-20 13:04:08 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-06-20 13:04:08 +0200 |
commit | 9d31c921b3d83e67d8eb03458b43aeb81c4d0b8c (patch) | |
tree | f69727b8c94e4876ad4135ce431d554b1ca5c49d /src | |
parent | 3a51c2ce4abedcc2c8e1670b6c2c6ad1243a303a (diff) | |
download | rebel-9d31c921b3d83e67d8eb03458b43aeb81c4d0b8c.tar rebel-9d31c921b3d83e67d8eb03458b43aeb81c4d0b8c.zip |
util/unix: avoid fdopendir/closedir on O_PATH fd
Diffstat (limited to 'src')
-rw-r--r-- | src/util/unix.rs | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/util/unix.rs b/src/util/unix.rs index ee0507c..b5e4d33 100644 --- a/src/util/unix.rs +++ b/src/util/unix.rs @@ -1,6 +1,10 @@ -use std::{fs::File, io::Result, os::unix::prelude::AsRawFd, path::Path}; +use std::{fs::File, io::Result, os::unix::prelude::*, path::Path}; -use nix::{dir::Dir, fcntl::OFlag, sys::stat::Mode, unistd}; +use nix::{ + fcntl::{self, OFlag}, + sys::stat::Mode, + unistd, +}; use super::ToIOResult; @@ -43,7 +47,7 @@ pub fn create_as<P: AsRef<Path>>( File::create(path) } -pub struct Chdir(Dir); +pub struct Chdir(File); impl Drop for Chdir { fn drop(&mut self) { @@ -52,14 +56,15 @@ impl Drop for Chdir { } pub fn chdir<P: AsRef<Path>>(path: P) -> Result<Chdir> { - let dir = Dir::open( + let fd = fcntl::open( ".", OFlag::O_PATH | OFlag::O_CLOEXEC | OFlag::O_NOFOLLOW, Mode::empty(), ) .to_io_result()?; + let file = unsafe { File::from_raw_fd(fd) }; unistd::chdir(path.as_ref()).to_io_result()?; - Ok(Chdir(dir)) + Ok(Chdir(file)) } |