pub struct FileLock { /* private fields */ }Expand description
Exclusive file lock to prevent concurrent aria2 instances from writing to the same file simultaneously.
Uses platform-appropriate locking mechanism:
- On Unix:
flock(LOCK_EX | LOCK_NB)via libc (non-blocking). - On Windows: Creates a
.lockmarker file with exclusive semantics. - On other platforms: No-op placeholder.
The lock is automatically released when this struct is dropped.
Implementations§
Source§impl FileLock
impl FileLock
Sourcepub fn acquire(path: &Path) -> Result<Self, String>
pub fn acquire(path: &Path) -> Result<Self, String>
Acquire an exclusive lock on the given file path.
§Unix Behavior
Opens/creates the file and calls flock(fd, LOCK_EX | LOCK_NB).
If another process already holds the lock, returns an error immediately
(non-blocking). The lock is advisory – cooperative processes must
all use flock for it to be effective. The lock is released when the
returned FileLock is dropped (or when [release] is called).
§Windows Behavior
Creates a sibling .lock file in the same directory. This is a simple
marker-based approach; a production implementation would use
LockFileEx or a named mutex for true exclusivity.
§Errors
Returns Err if:
- The file cannot be created/opened (permission denied, disk full, etc.)
- Another process already holds the lock (Unix only)
Sourcepub fn release(self)
pub fn release(self)
Explicitly release the lock.
After calling this method, the lock is released and the FileLock
should no longer be used. Dropping the FileLock also releases the
lock, so explicit release is optional.
Sourcepub fn is_held(&self) -> bool
pub fn is_held(&self) -> bool
Check whether we currently hold the lock.
Always returns true if [acquire] succeeded (the lock is held until
dropped or explicitly released). Returns false only if the lock was
never acquired.
Sourcepub fn lock_file_path(&self) -> PathBuf
pub fn lock_file_path(&self) -> PathBuf
Return the actual platform-specific lock file path used for locking.
- Unix: Same as
path()(the file itself is locked viaflock). - Windows: The
.lockmarker file (e.g.,foo.lockfor targetfoo).