diff options
Diffstat (limited to 'src/utils/lock.rs')
-rw-r--r-- | src/utils/lock.rs | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/src/utils/lock.rs b/src/utils/lock.rs index 21af628..1517b28 100644 --- a/src/utils/lock.rs +++ b/src/utils/lock.rs @@ -1,51 +1,51 @@ +use fs2::FileExt; use std::fs; use std::io; -use std::path::{Path,PathBuf}; -use fs2::FileExt; +use std::path::{Path, PathBuf}; pub struct FileLock { - path: PathBuf, - lockfile: fs::File + path: PathBuf, + lockfile: fs::File, } impl Drop for FileLock { - fn drop(&mut self) { - debug!("Dropping lock on file {}", self.path.to_string_lossy()); + fn drop(&mut self) { + debug!("Dropping lock on file {}", self.path.to_string_lossy()); - self.lockfile.unlock().unwrap(); - } + self.lockfile.unlock().unwrap(); + } } pub fn lock_file_exclusive(path: &Path) -> io::Result<FileLock> { - debug!("Locking on file ({})", path.to_string_lossy()); + debug!("Locking on file ({})", path.to_string_lossy()); - let lockfile = fs::File::create(path)?; - lockfile.try_lock_exclusive()?; + let lockfile = fs::File::create(path)?; + lockfile.try_lock_exclusive()?; - Ok(FileLock { - path: PathBuf::from(path), - lockfile, - }) + Ok(FileLock { + path: PathBuf::from(path), + lockfile, + }) } #[cfg(test)] mod tests { - use super::*; - use tempfile::NamedTempFile; - - #[test] - fn test_lock() { - let lockfile = NamedTempFile::new().unwrap(); - let lock = lock_file_exclusive(lockfile.path()); - assert!(lock.is_ok()); - } - - #[test] - fn test_lock_fail() { - let lockfile = NamedTempFile::new().unwrap(); - let lock = lock_file_exclusive(lockfile.path()); - let lock_err = lock_file_exclusive(lockfile.path()); - assert!(lock.is_ok()); - assert!(lock_err.is_err()); - } + use super::*; + use tempfile::NamedTempFile; + + #[test] + fn test_lock() { + let lockfile = NamedTempFile::new().unwrap(); + let lock = lock_file_exclusive(lockfile.path()); + assert!(lock.is_ok()); + } + + #[test] + fn test_lock_fail() { + let lockfile = NamedTempFile::new().unwrap(); + let lock = lock_file_exclusive(lockfile.path()); + let lock_err = lock_file_exclusive(lockfile.path()); + assert!(lock.is_ok()); + assert!(lock_err.is_err()); + } } |