Struct obnth::Dir[][src]

pub struct Dir { /* fields omitted */ }

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]

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]

Create a directory within this directory.

pub fn remove_dir<P: AsPath>(
    &self,
    path: P,
    lookup_flags: LookupFlags
) -> Result<()>
[src]

Remove a subdirectory of this directory.

pub fn remove_file<P: AsPath>(
    &self,
    path: P,
    lookup_flags: LookupFlags
) -> Result<()>
[src]

Remove a file within this directory.

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()).

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]

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]

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]

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]

impl IntoRawFd for Dir[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.