equal
deleted
inserted
replaced
1 //! Filesystem-based locks for local repositories |
1 //! Filesystem-based locks for local repositories |
2 |
2 |
3 use crate::errors::HgError; |
3 use crate::errors::HgError; |
4 use crate::errors::HgResultExt; |
4 use crate::errors::HgResultExt; |
|
5 use crate::vfs::Vfs; |
5 use crate::vfs::VfsImpl; |
6 use crate::vfs::VfsImpl; |
6 use std::io; |
7 use std::io; |
7 use std::io::ErrorKind; |
8 use std::io::ErrorKind; |
|
9 use std::path::Path; |
8 |
10 |
9 #[derive(derive_more::From)] |
11 #[derive(derive_more::From)] |
10 pub enum LockError { |
12 pub enum LockError { |
11 AlreadyHeld, |
13 AlreadyHeld, |
12 #[from] |
14 #[from] |
63 // acquired the lock in the meantime |
65 // acquired the lock in the meantime |
64 let lock_data = read_lock(hg_vfs, lock_filename)?; |
66 let lock_data = read_lock(hg_vfs, lock_filename)?; |
65 if !lock_should_be_broken(&lock_data) { |
67 if !lock_should_be_broken(&lock_data) { |
66 return Err(LockError::AlreadyHeld); |
68 return Err(LockError::AlreadyHeld); |
67 } |
69 } |
68 Ok(hg_vfs.remove_file(lock_filename)?) |
70 Ok(hg_vfs.unlink(Path::new(lock_filename))?) |
69 })? |
71 })? |
70 } |
72 } |
71 |
73 |
72 #[cfg(unix)] |
74 #[cfg(unix)] |
73 fn make_lock( |
75 fn make_lock( |
97 Ok(None) |
99 Ok(None) |
98 } |
100 } |
99 } |
101 } |
100 |
102 |
101 fn unlock(hg_vfs: &VfsImpl, lock_filename: &str) -> Result<(), HgError> { |
103 fn unlock(hg_vfs: &VfsImpl, lock_filename: &str) -> Result<(), HgError> { |
102 hg_vfs.remove_file(lock_filename) |
104 hg_vfs.unlink(Path::new(lock_filename)) |
103 } |
105 } |
104 |
106 |
105 /// Return whether the process that is/was holding the lock is known not to be |
107 /// Return whether the process that is/was holding the lock is known not to be |
106 /// running anymore. |
108 /// running anymore. |
107 fn lock_should_be_broken(data: &Option<String>) -> bool { |
109 fn lock_should_be_broken(data: &Option<String>) -> bool { |