cargo::util::flock

Struct Filesystem

Source
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

Source

pub fn new(path: PathBuf) -> Filesystem

Creates a new filesystem to be rooted at the given path.

Source

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.

Source

pub fn push<T: AsRef<Path>>(&mut self, other: T)

Like Path::push, pushes a new path component onto this filesystem.

Source

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!.

Source

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!.

Source

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.

Source

pub fn display(&self) -> Display<'_>

Returns an adaptor that can be used to print the path of this filesystem.

Source

pub fn open_rw_exclusive_create<P>( &self, path: P, gctx: &GlobalContext, msg: &str, ) -> CargoResult<FileLock>
where P: AsRef<Path>,

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.

Source

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.

Source

pub fn open_ro_shared<P>( &self, path: P, gctx: &GlobalContext, msg: &str, ) -> CargoResult<FileLock>
where P: AsRef<Path>,

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.

Source

pub fn open_ro_shared_create<P: AsRef<Path>>( &self, path: P, gctx: &GlobalContext, msg: &str, ) -> CargoResult<FileLock>

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.

Source

pub fn try_open_ro_shared_create<P: AsRef<Path>>( &self, path: P, ) -> CargoResult<Option<FileLock>>

A non-blocking version of Filesystem::open_ro_shared_create.

Returns None if the operation would block due to another process holding the lock.

Source

fn open( &self, path: &Path, opts: &OpenOptions, create: bool, ) -> CargoResult<(PathBuf, File)>

Trait Implementations§

Source§

impl Clone for Filesystem

Source§

fn clone(&self) -> Filesystem

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Filesystem

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq<Filesystem> for Path

Source§

fn eq(&self, other: &Filesystem) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Path> for Filesystem

Source§

fn eq(&self, other: &Path) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more

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