Trait otter_api_tests::FileExt [−][src]
pub trait FileExt {
pub fn duplicate(&self) -> Result<File, Error>;
pub fn allocated_size(&self) -> Result<u64, Error>;
pub fn allocate(&self, len: u64) -> Result<(), Error>;
pub fn lock_shared(&self) -> Result<(), Error>;
pub fn lock_exclusive(&self) -> Result<(), Error>;
pub fn try_lock_shared(&self) -> Result<(), Error>;
pub fn try_lock_exclusive(&self) -> Result<(), Error>;
pub fn unlock(&self) -> Result<(), Error>;
}Extension trait for std::fs::File which provides allocation, duplication and locking methods.
Notes on File Locks
This library provides whole-file locks in both shared (read) and exclusive (read-write) varieties.
File locks are a cross-platform hazard since the file lock APIs exposed by operating system kernels vary in subtle and not-so-subtle ways.
The API exposed by this library can be safely used across platforms as long as the following rules are followed:
- Multiple locks should not be created on an individual
Fileinstance concurrently. - Duplicated files should not be locked without great care.
- Files to be locked should be opened with at least read or write permissions.
- File locks may only be relied upon to be advisory.
See the tests in lib.rs for cross-platform lock behavior that may be
relied upon; see the tests in unix.rs and windows.rs for examples of
platform-specific behavior. File locks are implemented with
flock(2) on Unix and
LockFile
on Windows.
Required methods
pub fn duplicate(&self) -> Result<File, Error>[src]
Returns a duplicate instance of the file.
The returned file will share the same file position as the original file.
If using rustc version 1.9 or later, prefer using File::try_clone to this.
Notes
This is implemented with
dup(2) on Unix and
DuplicateHandle
on Windows.
pub fn allocated_size(&self) -> Result<u64, Error>[src]
Returns the amount of physical space allocated for a file.
pub fn allocate(&self, len: u64) -> Result<(), Error>[src]
Ensures that at least len bytes of disk space are allocated for the
file, and the file size is at least len bytes. After a successful call
to allocate, subsequent writes to the file within the specified length
are guaranteed not to fail because of lack of disk space.
pub fn lock_shared(&self) -> Result<(), Error>[src]
Locks the file for shared usage, blocking if the file is currently locked exclusively.
pub fn lock_exclusive(&self) -> Result<(), Error>[src]
Locks the file for exclusive usage, blocking if the file is currently locked.
pub fn try_lock_shared(&self) -> Result<(), Error>[src]
Locks the file for shared usage, or returns a an error if the file is
currently locked (see lock_contended_error).
pub fn try_lock_exclusive(&self) -> Result<(), Error>[src]
Locks the file for shared usage, or returns a an error if the file is
currently locked (see lock_contended_error).
pub fn unlock(&self) -> Result<(), Error>[src]
Unlocks the file.