summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Breitmoser <look@my.amazin.horse>2019-01-09 21:15:45 +0100
committerVincent Breitmoser <look@my.amazin.horse>2019-01-09 21:17:15 +0100
commitb6dd85a0ef39b728efe96b5e055e147f37a3d250 (patch)
treea742eab8135c1bc307eb2c7ce64bb285d1b2091b
parentc1793d4ecad573f2bc918ff3c28468a3db846726 (diff)
index: use file lock while running
-rw-r--r--Cargo.lock12
-rw-r--r--Cargo.toml2
-rw-r--r--src/defaults.rs4
-rw-r--r--src/index.rs4
-rw-r--r--src/lib.rs5
5 files changed, 25 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 079e3c0..59951ea 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -181,6 +181,15 @@ version = "1.0.6"
source = "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.46 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -281,6 +290,7 @@ dependencies = [
"assert_fs 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"indoc 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -290,6 +300,7 @@ dependencies = [
"serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)",
"stderrlog 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -940,6 +951,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum env_logger 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "afb070faf94c85d17d50ca44f6ad076bce18ae92f0037d350947240a36e9d42e"
"checksum float-cmp 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "134a8fa843d80a51a5b77d36d42bc2def9edcb0262c914861d08129fd1926600"
"checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3"
+"checksum fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213"
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
diff --git a/Cargo.toml b/Cargo.toml
index a9a9f71..0ee7977 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -18,7 +18,9 @@ toml = "0.4.2"
serde = "1.0.82"
serde_derive = "1.0.82"
uuid = { version = "0.7", features = ["v4"] }
+fs2 = "0.4.3"
[dev-dependencies]
assert_fs = "0.10.1"
+tempfile = "3.0.5"
predicates = "1.0"
diff --git a/src/defaults.rs b/src/defaults.rs
index 9f96a56..9230a42 100644
--- a/src/defaults.rs
+++ b/src/defaults.rs
@@ -25,6 +25,10 @@ pub fn get_indexfile(key: &str) -> PathBuf {
[DATADIR, INDEXDIR, key].iter().collect()
}
+pub fn get_indexlockfile() -> PathBuf {
+ [DATADIR, "index-lock"].iter().collect()
+}
+
pub fn get_caldir() -> PathBuf {
[DATADIR, CALDIR].iter().collect()
}
diff --git a/src/index.rs b/src/index.rs
index b46516f..1f3e45c 100644
--- a/src/index.rs
+++ b/src/index.rs
@@ -2,6 +2,7 @@ use icalwrap::*;
use std::collections::HashMap;
use std::fs;
use std::path::{Path,PathBuf};
+use lock;
use defaults::*;
use utils;
@@ -21,6 +22,8 @@ fn add_buckets_for_calendar(buckets: &mut HashMap<String, Vec<String>>, cal: &Ic
pub fn index_dir(dir: &Path) {
use std::time::Instant;
+ let _lock = lock::lock_file_exclusive(&get_indexlockfile());
+
info!("Recursively indexing '.ics' files in directory: {}", dir.to_string_lossy());
if !dir.exists() {
error!("Directory doesn't exist: {}", dir.to_string_lossy());
@@ -39,7 +42,6 @@ pub fn index_dir(dir: &Path) {
},
Err(error) => error!("{}", error),
}
-
}
fn get_ics_files(dir: &Path) -> impl Iterator<Item = PathBuf> {
diff --git a/src/lib.rs b/src/lib.rs
index 722278d..551303c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -10,6 +10,7 @@ pub mod edit;
pub mod icalwrap;
pub mod index;
pub mod list;
+pub mod lock;
pub mod new;
pub mod prettyprint;
pub mod select;
@@ -22,8 +23,11 @@ pub mod utils;
#[cfg(test)]
pub mod testdata;
+#[cfg(test)]
+extern crate tempfile;
extern crate chrono;
+extern crate fs2;
extern crate itertools;
extern crate libc;
extern crate libical_sys as ical;
@@ -41,4 +45,3 @@ extern crate log;
#[macro_use]
extern crate indoc;
-