mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-03 15:33:13 +03:00
remove unused lock
module and dependency to fslock
This commit is contained in:
parent
bab3ab376d
commit
7ffb13550a
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -1739,16 +1739,6 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fslock"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04412b8935272e3a9bae6f48c7bfff74c2911f60525404edfdd28e49884c3bfb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "funty"
|
||||
version = "2.0.0"
|
||||
@ -2179,7 +2169,6 @@ dependencies = [
|
||||
"diffy",
|
||||
"dirs 5.0.1",
|
||||
"filetime",
|
||||
"fslock",
|
||||
"futures",
|
||||
"git2",
|
||||
"git2-hooks",
|
||||
|
@ -26,7 +26,6 @@ bstr = "1.9.1"
|
||||
diffy = "0.3.0"
|
||||
dirs = "5.0.1"
|
||||
filetime = "0.2.23"
|
||||
fslock = "0.2.1"
|
||||
futures = "0.3"
|
||||
git2.workspace = true
|
||||
git2-hooks = "0.3"
|
||||
|
@ -18,7 +18,6 @@ pub mod error;
|
||||
pub mod fs;
|
||||
pub mod git;
|
||||
pub mod id;
|
||||
pub mod lock;
|
||||
pub mod path;
|
||||
pub mod secret;
|
||||
pub mod serde;
|
||||
|
@ -1,51 +0,0 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Dir {
|
||||
inner: Arc<Inner>,
|
||||
}
|
||||
|
||||
impl Dir {
|
||||
pub fn new<P: AsRef<std::path::Path>>(path: P) -> Result<Self, std::io::Error> {
|
||||
Inner::new(path).map(Arc::new).map(|inner| Self { inner })
|
||||
}
|
||||
|
||||
pub fn batch<R>(
|
||||
&self,
|
||||
action: impl FnOnce(&std::path::Path) -> R,
|
||||
) -> Result<R, std::io::Error> {
|
||||
self.inner.batch(action)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Inner {
|
||||
path: std::path::PathBuf,
|
||||
flock: Mutex<fslock::LockFile>,
|
||||
}
|
||||
|
||||
impl Inner {
|
||||
fn new<P: AsRef<std::path::Path>>(path: P) -> Result<Self, std::io::Error> {
|
||||
let path = path.as_ref().to_path_buf();
|
||||
if !path.exists() {
|
||||
std::fs::create_dir_all(&path)?;
|
||||
} else if !path.is_dir() {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::NotFound,
|
||||
format!("{} is not a directory", path.display()),
|
||||
));
|
||||
}
|
||||
let flock = fslock::LockFile::open(&path.with_extension("lock")).map(Mutex::new)?;
|
||||
Ok(Self { path, flock })
|
||||
}
|
||||
|
||||
fn batch<R>(&self, action: impl FnOnce(&std::path::Path) -> R) -> Result<R, std::io::Error> {
|
||||
let mut flock = self.flock.lock().unwrap();
|
||||
|
||||
flock.lock()?;
|
||||
let result = action(&self.path);
|
||||
flock.unlock()?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
@ -1,2 +1 @@
|
||||
mod lock;
|
||||
mod types;
|
||||
|
@ -1,91 +0,0 @@
|
||||
use gitbutler_core::lock::Dir;
|
||||
|
||||
use gitbutler_testsupport::temp_dir;
|
||||
|
||||
#[tokio::test]
|
||||
async fn lock_same_instance() {
|
||||
let dir_path = temp_dir();
|
||||
std::fs::write(dir_path.path().join("file.txt"), "").unwrap();
|
||||
let dir = Dir::new(dir_path.path()).unwrap();
|
||||
|
||||
let (tx, rx) = std::sync::mpsc::sync_channel(1);
|
||||
|
||||
// spawn a task that will signal right after aquireing the lock
|
||||
let _ = tokio::spawn({
|
||||
let dir = dir.clone();
|
||||
async move {
|
||||
dir.batch(|root| {
|
||||
tx.send(()).unwrap();
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join("file.txt")).unwrap(),
|
||||
String::new()
|
||||
);
|
||||
std::fs::write(root.join("file.txt"), "1")
|
||||
})
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// then we wait until the lock is aquired
|
||||
rx.recv().unwrap();
|
||||
|
||||
// and immidiately try to lock again
|
||||
dir.batch(|root| {
|
||||
assert_eq!(std::fs::read_to_string(root.join("file.txt")).unwrap(), "1");
|
||||
std::fs::write(root.join("file.txt"), "2")
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir_path.path().join("file.txt")).unwrap(),
|
||||
"2"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn lock_different_instances() {
|
||||
let dir_path = temp_dir();
|
||||
std::fs::write(dir_path.path().join("file.txt"), "").unwrap();
|
||||
|
||||
let (tx, rx) = std::sync::mpsc::sync_channel(1);
|
||||
|
||||
// spawn a task that will signal right after aquireing the lock
|
||||
let _ = tokio::spawn({
|
||||
let dir_path = dir_path.path().to_owned();
|
||||
async move {
|
||||
// one dir instance is created on a separate thread
|
||||
let dir = Dir::new(&dir_path).unwrap();
|
||||
dir.batch(|root| {
|
||||
tx.send(()).unwrap();
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(root.join("file.txt")).unwrap(),
|
||||
String::new()
|
||||
);
|
||||
std::fs::write(root.join("file.txt"), "1")
|
||||
})
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// another dir instance is created on the main thread
|
||||
let dir = Dir::new(&dir_path).unwrap();
|
||||
|
||||
// then we wait until the lock is aquired
|
||||
rx.recv().unwrap();
|
||||
|
||||
// and immidiately try to lock again
|
||||
dir.batch(|root| {
|
||||
assert_eq!(std::fs::read_to_string(root.join("file.txt")).unwrap(), "1");
|
||||
std::fs::write(root.join("file.txt"), "2")
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(dir_path.path().join("file.txt")).unwrap(),
|
||||
"2"
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue
Block a user