Struct SharedFile

Source
pub struct SharedFile { /* private fields */ }
Expand description

This struct implements an “advisory lock” of a file using an auxiliary lock file to control the shared read access to the file as well as an exclusive read and write access to it.

Internally, it uses the crate fd-lock to control the access to the lock file while protecting the access tot the actual file.

The protected file is always opened with shared read and write and create options.

§Locking the same file in multiple threads

It is very important to notice that this struct is not thread safe and must be protected by a Mutex whenever necessary. It happens because the file offset pointer cannot be safely shared among multiple threads even for read operations. Unfortunately, the use of the mutex or other sync mechanisms will lead to the serialization of both read and write locks inside the same application.

Because of that, it is recommended to create multiple instances of this struct pointing to the same file. The access control will be guaranteed by the use of the lock file instead of the traditional thread sync mechanisms.

Implementations§

Source§

impl SharedFile

Source

pub fn new(file: &Path) -> Result<Self>

Creates a new SharedFile. The name of the lock file will be determine automatically based on the name of the original file.

The shared file is opened with the Self::default_options().

Arguments:

  • file: The file to be protected;

Returns the new instance of an IO error to indicate what went wrong.

Source

pub fn with_options(file: &Path, options: &OpenOptions) -> Result<Self>

Creates a new SharedFile. The name of the lock file will be determine automatically based on the name of the original file.

Arguments:

  • file: The file to be protected;
  • options: OpenOptions used to open the file;

Returns the new instance of an IO error to indicate what went wrong.

Source

pub fn with_option_builder( file: &Path, options: &OpenOptions, lock_file_builder: &dyn SharedFileLockNameBuilder, ) -> Result<Self>

Creates a new SharedFile. The name of the lock file will be determine by the specified SharedFileLockNameBuilder.

Arguments:

  • file: The file to be protected;
  • options: OpenOptions used to open the file;
  • lock_file_builder: The lock file builder to use;

Returns the new instance of an IO error to indicate what went wrong.

Source

pub fn with_option_lock_file( file: &Path, options: &OpenOptions, lock_file: &Path, ) -> Result<Self>

Creates a new SharedFile.

Arguments:

  • file: The file to be protected;
  • options: OpenOptions used to open the file;
  • lock_file: The lock file to use;

Returns the new instance of an IO error to indicate what went wrong.

Source

pub fn default_options() -> OpenOptions

Returns the default open options used to open the target file. It sets read write and create to true.

Source

pub fn read(&mut self) -> Result<SharedFileReadLockGuard<'_>>

Locks the file for shared read.

Returns read lock that grants access to the file.

Source

pub fn write(&mut self) -> Result<SharedFileWriteLockGuard<'_>>

Locks the file for exclusive write and read

Returns read/write lock that grants access to the file.

Source

pub fn try_read(&mut self) -> Result<SharedFileReadLockGuard<'_>>

Attempts to locks the file for shared read. It fails without waiting if the lock cannot be acquired.

Returns read lock that grants access to the file.

Source

pub fn try_write(&mut self) -> Result<SharedFileWriteLockGuard<'_>>

Attempts to acquire the file lock for exclusive write and read. It fails without waiting if the lock cannot be acquired.

Returns read/write lock that grants access to the file.

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.

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.
Source§

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

Source§

fn vzip(self) -> V