mirror of
https://github.com/Llewellynvdm/zoxide.git
synced 2024-11-10 23:30:57 +00:00
Remove flock
This commit is contained in:
parent
03e8cd47a2
commit
b3470de799
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -134,15 +134,6 @@ dependencies = [
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fs2"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.14"
|
||||
@ -376,7 +367,6 @@ dependencies = [
|
||||
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"envy 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"structopt 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
@ -400,7 +390,6 @@ dependencies = [
|
||||
"checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3"
|
||||
"checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
|
||||
"checksum envy 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f938a4abd5b75fe3737902dbc2e79ca142cc1526827a9e40b829a086758531a9"
|
||||
"checksum fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
|
||||
"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
|
||||
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
|
||||
"checksum hermit-abi 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1010591b26bbfe835e9faeabeb11866061cc7dcebffd56ad7d0942d0e61aefd8"
|
||||
|
@ -18,7 +18,6 @@ bincode = "1.2.1"
|
||||
clap = "2.33.0"
|
||||
dirs = "2.0.2"
|
||||
envy = "0.4.1"
|
||||
fs2 = "0.4.3"
|
||||
serde = { version = "1.0.104", features = ["derive"] }
|
||||
structopt = "0.3.11"
|
||||
|
||||
|
40
src/db.rs
40
src/db.rs
@ -1,17 +1,12 @@
|
||||
use crate::dir::Dir;
|
||||
use crate::types::{Epoch, Rank};
|
||||
use anyhow::{anyhow, bail, Context, Result};
|
||||
use fs2::FileExt;
|
||||
use std::fs::{self, File, OpenOptions};
|
||||
use std::fs::{self, File};
|
||||
use std::io::{self, BufRead, BufReader, BufWriter};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub struct DB {
|
||||
path: PathBuf,
|
||||
path_tmp: PathBuf,
|
||||
|
||||
file_tmp: File,
|
||||
|
||||
dirs: Vec<Dir>,
|
||||
modified: bool,
|
||||
}
|
||||
@ -20,19 +15,6 @@ impl DB {
|
||||
pub fn open<P: AsRef<Path>>(path: P) -> Result<DB> {
|
||||
let path = path.as_ref().to_path_buf();
|
||||
|
||||
let mut path_tmp = path.clone();
|
||||
path_tmp.set_file_name(".zo.tmp");
|
||||
|
||||
let file_tmp = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(&path_tmp)
|
||||
.with_context(|| anyhow!("could not open temporary database file"))?;
|
||||
|
||||
file_tmp
|
||||
.lock_exclusive()
|
||||
.with_context(|| anyhow!("could not lock temporary database file"))?;
|
||||
|
||||
let dirs = match File::open(&path) {
|
||||
Ok(file) => {
|
||||
let reader = BufReader::new(&file);
|
||||
@ -47,8 +29,6 @@ impl DB {
|
||||
|
||||
Ok(DB {
|
||||
path,
|
||||
path_tmp,
|
||||
file_tmp,
|
||||
dirs,
|
||||
modified: false,
|
||||
})
|
||||
@ -56,14 +36,16 @@ impl DB {
|
||||
|
||||
pub fn save(&mut self) -> Result<()> {
|
||||
if self.modified {
|
||||
self.file_tmp
|
||||
.set_len(0)
|
||||
.with_context(|| "could not truncate temporary database file")?;
|
||||
let path_tmp = self.get_path_tmp();
|
||||
|
||||
let writer = BufWriter::new(&self.file_tmp);
|
||||
let file_tmp = File::create(&path_tmp)
|
||||
.with_context(|| anyhow!("could not open temporary database file"))?;
|
||||
|
||||
let writer = BufWriter::new(&file_tmp);
|
||||
bincode::serialize_into(writer, &self.dirs)
|
||||
.with_context(|| anyhow!("could not serialize database"))?;
|
||||
fs::rename(&self.path_tmp, &self.path)
|
||||
|
||||
fs::rename(&path_tmp, &self.path)
|
||||
.with_context(|| anyhow!("could not move temporary database file"))?;
|
||||
}
|
||||
|
||||
@ -249,6 +231,12 @@ If you wish to merge the two, specify the `--merge` flag."
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_path_tmp(&self) -> PathBuf {
|
||||
let mut path_tmp = self.path.clone();
|
||||
path_tmp.set_file_name(".zo.tmp");
|
||||
path_tmp
|
||||
}
|
||||
|
||||
fn remove_invalid(&mut self) {
|
||||
let orig_len = self.dirs.len();
|
||||
self.dirs.retain(Dir::is_dir);
|
||||
|
Loading…
Reference in New Issue
Block a user