Struct obnth::Dir [−][src]
A wrapper around a directory file descriptor that allows opening files within that directory.
Implementations
impl Dir
[src]
pub fn open<P: AsPath>(path: P) -> Result<Self>
[src]
Open the specified directory.
pub fn parent_unchecked(&self) -> Result<Self>
[src]
Open the parent directory of this directory, without checking if it's open to the root directory.
If this directory is open to the root directory, this method will return a new directory
open to the same directory (similarly to try_clone()
).
pub fn parent(&self) -> Result<Option<Self>>
[src]
Open the parent directory of this directory.
This returns Ok(None)
if this directory is open to the root directory.
pub fn sub_dir<P: AsPath>(
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<Self>
[src]
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<Self>
Open a subdirectory of this directory.
path
or one of its components can refer to a symlink (unless LookupFlags::NO_SYMLINKS
is passed), but the specified subdirectory must be contained within this directory.
pub fn create_dir<P: AsPath>(
&self,
path: P,
mode: mode_t,
lookup_flags: LookupFlags
) -> Result<()>
[src]
&self,
path: P,
mode: mode_t,
lookup_flags: LookupFlags
) -> Result<()>
Create a directory within this directory.
pub fn remove_dir<P: AsPath>(
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<()>
[src]
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<()>
Remove a subdirectory of this directory.
pub fn remove_file<P: AsPath>(
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<()>
[src]
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<()>
Remove a file within this directory.
pub fn symlink<P: AsPath, T: AsPath>(
&self,
path: P,
target: T,
lookup_flags: LookupFlags
) -> Result<()>
[src]
&self,
path: P,
target: T,
lookup_flags: LookupFlags
) -> Result<()>
Create a symlink within this directory.
path
specifies the path where the symlink is created, and target
specifies the file
that the symlink will point to. Note that the order is swapped compared to the C symlink()
function (and Rust's std::os::unix::fs::symlink()
).
pub fn read_link<P: AsPath>(
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<PathBuf>
[src]
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<PathBuf>
Read the contents of the specified symlink.
pub fn local_rename<P: AsPath, R: AsPath>(
&self,
old: P,
new: R,
lookup_flags: LookupFlags
) -> Result<()>
[src]
&self,
old: P,
new: R,
lookup_flags: LookupFlags
) -> Result<()>
Rename a file in this directory.
This is exactly equivalent to rename(self, old, self, new, lookup_flags)
.
pub fn list_self(&self) -> Result<ReadDirIter>
[src]
List the contents of this directory.
pub fn list_dir<P: AsPath>(
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<ReadDirIter>
[src]
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<ReadDirIter>
List the contents of the specified subdirectory.
This is equivalent to self.sub_dir(path, lookup_flags)?.list_self()
, but more efficient.
pub fn try_clone(&self) -> Result<Self>
[src]
Try to "clone" this Dir
.
This is equivalent to self.sub_dir(".")
, but more efficient.
pub fn self_metadata(&self) -> Result<Metadata>
[src]
Retrieve metadata of this directory.
This is equivalent to self.metadata(".", LookupFlags::empty())
, but it's significantly
more efficient.
pub fn metadata<P: AsPath>(
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<Metadata>
[src]
&self,
path: P,
lookup_flags: LookupFlags
) -> Result<Metadata>
Retrieve information on the file with the given path.
The specified file must be located within this directory. Symlinks in the final component of the path are not followed.
pub fn recover_path(&self) -> Result<PathBuf>
[src]
Recover the path to the directory that this Dir
is currently open to.
WARNINGS (make sure to read):
- Do NOT use this path to open any files within the directory (i.e.
File::open(path.join("file.txt"))
! That would defeat the entire purpose of this crate by opening vectors for symlink attacks. - If a potentially malicious user controls a parent directory of the directory that this
Dir
is currently open to, the path returned by this function is NOT safe to use.
OS-specific optimizations:
- On Linux, this will try
readlink("/proc/self/fd/$fd")
. - On macOS, this will try
fcntl(fd, F_GETPATH)
.
If either of these techniques fails (or on other platforms), it will fall back on a more reliable (but slower) strategy.
Some notes:
- If the directory has been deleted, this function will fail with
ENOENT
(this is guaranteed, though beware of race conditions). - This function may or may not fail with
EACCES
if the currrent process does not have access to one or more of this directory's parent directories.
pub fn change_cwd_to(&self) -> Result<()>
[src]
Set this process's current working directory to this directory.
This is roughly equivalent to std::env::set_current_dir(self.recover_path()?)
, but 1) it
is much more efficient, and 2) it is more secure (notably, it avoids race conditions).
pub fn open_file(&self) -> OpenOptions<'_>
[src]
Return an OpenOptions
struct that can be use to open files within this directory.
See the documentation of OpenOptions
for more details.
Trait Implementations
impl AsRawFd for Dir
[src]
impl Debug for Dir
[src]
impl Drop for Dir
[src]
impl FromRawFd for Dir
[src]
unsafe fn from_raw_fd(fd: RawFd) -> Self
[src]
impl IntoRawFd for Dir
[src]
fn into_raw_fd(self) -> RawFd
[src]
Auto Trait Implementations
impl RefUnwindSafe for Dir
[src]
impl Send for Dir
[src]
impl Sync for Dir
[src]
impl Unpin for Dir
[src]
impl UnwindSafe for Dir
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,