pub struct Dir(_);
Expand description
A safe wrapper around directory file descriptor
Construct it either with Dir::cwd()
or Dir::open(path)
Implementations
sourceimpl Dir
impl Dir
sourcepub fn cwd() -> Dir
👎 Deprecated since 0.1.15: Use Dir::open(".")
instead. Dir::cwd() doesn’t open actual file descriptor and uses magic value instead which resolves to current dir on any syscall invocation. This is usually counter-intuitive and yields a broken file descriptor when using Dir::as_raw_fd
. Will be removed in version v0.2 of the library.
pub fn cwd() -> Dir
Use Dir::open(".")
instead. Dir::cwd() doesn’t open actual file descriptor and uses magic value instead which resolves to current dir on any syscall invocation. This is usually counter-intuitive and yields a broken file descriptor when using Dir::as_raw_fd
. Will be removed in version v0.2 of the library.
Creates a directory descriptor that resolves paths relative to current working directory (AT_FDCWD)
sourcepub fn list_dir<P: AsPath>(&self, path: P) -> Result<DirIter>
pub fn list_dir<P: AsPath>(&self, path: P) -> Result<DirIter>
List subdirectory of this dir
You can list directory itself with list_self
.
sourcepub fn sub_dir<P: AsPath>(&self, path: P) -> Result<Dir>
pub fn sub_dir<P: AsPath>(&self, path: P) -> Result<Dir>
Open subdirectory
Note that this method does not resolve symlinks by default, so you may have to call
read_link
to resolve the real path first.
sourcepub fn open_file<P: AsPath>(&self, path: P) -> Result<File>
pub fn open_file<P: AsPath>(&self, path: P) -> Result<File>
Open file for reading in this directory
Note that this method does not resolve symlinks by default, so you may have to call
read_link
to resolve the real path first.
sourcepub fn write_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
pub fn write_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
Open file for writing, create if necessary, truncate on open
If there exists a symlink at the destination path, this method will fail. In that case, you
will need to remove the symlink before calling this method. If you are on Linux, you can
alternatively create an unnamed file with new_unnamed_file
and then rename it,
clobbering the symlink at the destination.
sourcepub fn append_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
pub fn append_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
Open file for append, create if necessary
If there exists a symlink at the destination path, this method will fail. In that case, you
will need to call read_link
to resolve the real path first.
sourcepub fn create_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
👎 Deprecated since 0.1.7: please use write_file
instead
pub fn create_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
please use write_file
instead
Create file for writing (and truncate) in this directory
Deprecated alias for write_file
If there exists a symlink at the destination path, this method will fail. In that case, you
will need to remove the symlink before calling this method. If you are on Linux, you can
alternatively create an unnamed file with new_unnamed_file
and then rename it,
clobbering the symlink at the destination.
sourcepub fn new_unnamed_file(&self, mode: mode_t) -> Result<File>
pub fn new_unnamed_file(&self, mode: mode_t) -> Result<File>
Create a tmpfile in this directory which isn’t linked to any filename
This works by passing O_TMPFILE
into the openat call. The flag is
supported only on linux. So this function always returns error on
such systems.
WARNING! On glibc < 2.22 file permissions of the newly created file may be arbitrary. Consider chowning after creating a file.
Note: It may be unclear why creating unnamed file requires a dir. There are two reasons:
- It’s created (and occupies space) on a real filesystem, so the directory is a way to find out which filesystem to attach file to
- This method is mostly needed to initialize the file then link it
using
link_file_at
to the real directory entry. When linking it must be linked into the same filesystem. But because for most programs finding out filesystem layout is an overkill the rule of thumb is to create a file in the the target directory.
Currently, we recommend to fallback on any error if this operation can’t be accomplished rather than relying on specific error codes, because semantics of errors are very ugly.
sourcepub fn link_file_at<F: AsRawFd, P: AsPath>(
&self,
file: &F,
path: P
) -> Result<()>
pub fn link_file_at<F: AsRawFd, P: AsPath>(
&self,
file: &F,
path: P
) -> Result<()>
Link open file to a specified path
This is used with new_unnamed_file()
to create and initialize the
file before linking it into a filesystem. This requires /proc
to be
mounted and works only on linux.
On systems other than linux this always returns error. It’s expected
that in most cases this methos is not called if new_unnamed_file
fails. But in obscure scenarios where /proc
is not mounted this
method may fail even on linux. So your code should be able to fallback
to a named file if this method fails too.
sourcepub fn new_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
pub fn new_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
Create file if not exists, fail if exists
This function checks existence and creates file atomically with respect to other threads and processes.
Technically it means passing O_EXCL
flag to open.
sourcepub fn update_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
pub fn update_file<P: AsPath>(&self, path: P, mode: mode_t) -> Result<File>
Open file for reading and writing without truncation, create if needed
If there exists a symlink at the destination path, this method will fail. In that case, you
will need to call read_link
to resolve the real path first.
sourcepub fn symlink<P: AsPath, R: AsPath>(&self, path: P, value: R) -> Result<()>
pub fn symlink<P: AsPath, R: AsPath>(&self, path: P, value: R) -> Result<()>
Make a symlink in this directory
Note: the order of arguments differ from symlinkat
sourcepub fn create_dir<P: AsPath>(&self, path: P, mode: mode_t) -> Result<()>
pub fn create_dir<P: AsPath>(&self, path: P, mode: mode_t) -> Result<()>
Create a subdirectory in this directory
sourcepub fn local_rename<P: AsPath, R: AsPath>(&self, old: P, new: R) -> Result<()>
pub fn local_rename<P: AsPath, R: AsPath>(&self, old: P, new: R) -> Result<()>
Rename a file in this directory to another name (keeping same dir)
sourcepub fn local_exchange<P: AsPath, R: AsPath>(&self, old: P, new: R) -> Result<()>
pub fn local_exchange<P: AsPath, R: AsPath>(&self, old: P, new: R) -> Result<()>
Similar to local_rename
but atomically swaps both paths
Only supported on Linux.
sourcepub fn remove_dir<P: AsPath>(&self, path: P) -> Result<()>
pub fn remove_dir<P: AsPath>(&self, path: P) -> Result<()>
Remove a subdirectory in this directory
Note only empty directory may be removed
sourcepub fn remove_file<P: AsPath>(&self, path: P) -> Result<()>
pub fn remove_file<P: AsPath>(&self, path: P) -> Result<()>
Remove a file in this directory
sourcepub fn recover_path(&self) -> Result<PathBuf>
pub fn recover_path(&self) -> Result<PathBuf>
Get the path of this directory (if possible)
This uses symlinks in /proc/self
, they sometimes may not be
available so use with care.
sourcepub fn metadata<P: AsPath>(&self, path: P) -> Result<Metadata>
pub fn metadata<P: AsPath>(&self, path: P) -> Result<Metadata>
Returns metadata of an entry in this directory
If the destination path is a symlink, this will return the metadata of the symlink itself.
If you would like to follow the symlink and return the metadata of the target, you will
have to call read_link
to resolve the real path first.
sourcepub fn self_metadata(&self) -> Result<Metadata>
pub fn self_metadata(&self) -> Result<Metadata>
Returns the metadata of the directory itself.
sourcepub unsafe fn from_raw_fd_checked(fd: RawFd) -> Result<Self>
pub unsafe fn from_raw_fd_checked(fd: RawFd) -> Result<Self>
Constructs a new Dir
from a given raw file descriptor,
ensuring it is a directory file descriptor first.
This function consumes ownership of the specified file
descriptor. The returned Dir
will take responsibility for
closing it when it goes out of scope.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Dir
impl Send for Dir
impl Sync for Dir
impl Unpin for Dir
impl UnwindSafe for Dir
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more