pub struct Filesystem {
root: PathBuf,
}
Expand description
A “filesystem” is intended to be a globally shared, hence locked, resource in Cargo.
The Path
of a filesystem cannot be learned unless it’s done in a locked
fashion, and otherwise functions on this structure are prepared to handle
concurrent invocations across multiple instances of Cargo.
The methods on Filesystem
that open files return a FileLock
which
holds the lock, and that type provides methods for accessing the
underlying file.
If the blocking methods (like Filesystem::open_ro_shared
) detect that
they will block, then they will display a message to the user letting them
know it is blocked. There are non-blocking variants starting with the
try_
prefix like Filesystem::try_open_ro_shared_create
.
The behavior of locks acquired by the Filesystem
depend on the operating
system. On unix-like system, they are advisory using flock
, and thus
not enforced against processes which do not try to acquire the lock. On
Windows, they are mandatory using LockFileEx
, enforced against all
processes.
This does not guarantee that a lock is acquired. In some cases, for
example on filesystems that don’t support locking, it will return a
FileLock
even though the filesystem lock was not acquired. This is
intended to provide a graceful fallback instead of refusing to work.
Usually there aren’t multiple processes accessing the same resource. In
that case, it is the user’s responsibility to not run concurrent
processes.
Fields§
§root: PathBuf
Implementations§
Source§impl Filesystem
impl Filesystem
Sourcepub fn new(path: PathBuf) -> Filesystem
pub fn new(path: PathBuf) -> Filesystem
Creates a new filesystem to be rooted at the given path.
Sourcepub fn join<T: AsRef<Path>>(&self, other: T) -> Filesystem
pub fn join<T: AsRef<Path>>(&self, other: T) -> Filesystem
Like Path::join
, creates a new filesystem rooted at this filesystem
joined with the given path.
Sourcepub fn push<T: AsRef<Path>>(&mut self, other: T)
pub fn push<T: AsRef<Path>>(&mut self, other: T)
Like Path::push
, pushes a new path component onto this filesystem.
Sourcepub fn into_path_unlocked(self) -> PathBuf
pub fn into_path_unlocked(self) -> PathBuf
Consumes this filesystem and returns the underlying PathBuf
.
Note that this is a relatively dangerous operation and should be used with great caution!.
Sourcepub fn as_path_unlocked(&self) -> &Path
pub fn as_path_unlocked(&self) -> &Path
Returns the underlying Path
.
Note that this is a relatively dangerous operation and should be used with great caution!.
Sourcepub fn create_dir(&self) -> CargoResult<()>
pub fn create_dir(&self) -> CargoResult<()>
Creates the directory pointed to by this filesystem.
Handles errors where other Cargo processes are also attempting to concurrently create this directory.
Sourcepub fn display(&self) -> Display<'_>
pub fn display(&self) -> Display<'_>
Returns an adaptor that can be used to print the path of this filesystem.
Sourcepub fn open_rw_exclusive_create<P>(
&self,
path: P,
gctx: &GlobalContext,
msg: &str,
) -> CargoResult<FileLock>
pub fn open_rw_exclusive_create<P>( &self, path: P, gctx: &GlobalContext, msg: &str, ) -> CargoResult<FileLock>
Opens read-write exclusive access to a file, returning the locked version of a file.
This function will create a file at path
if it doesn’t already exist
(including intermediate directories), and then it will acquire an
exclusive lock on path
. If the process must block waiting for the
lock, the msg
is printed to GlobalContext
.
The returned file can be accessed to look at the path and also has read/write access to the underlying file.
Sourcepub fn try_open_rw_exclusive_create<P: AsRef<Path>>(
&self,
path: P,
) -> CargoResult<Option<FileLock>>
pub fn try_open_rw_exclusive_create<P: AsRef<Path>>( &self, path: P, ) -> CargoResult<Option<FileLock>>
A non-blocking version of Filesystem::open_rw_exclusive_create
.
Returns None
if the operation would block due to another process
holding the lock.
Opens read-only shared access to a file, returning the locked version of a file.
This function will fail if path
doesn’t already exist, but if it does
then it will acquire a shared lock on path
. If the process must block
waiting for the lock, the msg
is printed to GlobalContext
.
The returned file can be accessed to look at the path and also has read access to the underlying file. Any writes to the file will return an error.
Opens read-only shared access to a file, returning the locked version of a file.
Compared to Filesystem::open_ro_shared
, this will create the file
(and any directories in the parent) if the file does not already
exist.
A non-blocking version of Filesystem::open_ro_shared_create
.
Returns None
if the operation would block due to another process
holding the lock.
fn open( &self, path: &Path, opts: &OpenOptions, create: bool, ) -> CargoResult<(PathBuf, File)>
Trait Implementations§
Source§impl Clone for Filesystem
impl Clone for Filesystem
Source§fn clone(&self) -> Filesystem
fn clone(&self) -> Filesystem
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Filesystem
impl Debug for Filesystem
Source§impl PartialEq<Filesystem> for Path
impl PartialEq<Filesystem> for Path
Source§impl PartialEq<Path> for Filesystem
impl PartialEq<Path> for Filesystem
Auto Trait Implementations§
impl Freeze for Filesystem
impl RefUnwindSafe for Filesystem
impl Send for Filesystem
impl Sync for Filesystem
impl Unpin for Filesystem
impl UnwindSafe for Filesystem
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
S: Into<Dispatch>,
§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 24 bytes