Преглед изворни кода

client: Add a lock file to prevent parallel execution.

Mathias Gottschlag пре 5 година
родитељ
комит
c4009de3f8
4 измењених фајлова са 24 додато и 2 уклоњено
  1. 11
    0
      Cargo.lock
  2. 1
    0
      Cargo.toml
  3. 8
    2
      src/bin/client.rs
  4. 4
    0
      src/paths.rs

+ 11
- 0
Cargo.lock Прегледај датотеку

@@ -319,6 +319,15 @@ name = "fnv"
319 319
 version = "1.0.7"
320 320
 source = "registry+https://github.com/rust-lang/crates.io-index"
321 321
 
322
+[[package]]
323
+name = "fslock"
324
+version = "0.1.6"
325
+source = "registry+https://github.com/rust-lang/crates.io-index"
326
+dependencies = [
327
+ "libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
328
+ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
329
+]
330
+
322 331
 [[package]]
323 332
 name = "fswatcher"
324 333
 version = "0.1.0"
@@ -1181,6 +1190,7 @@ dependencies = [
1181 1190
  "criterion 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
1182 1191
  "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
1183 1192
  "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
1193
+ "fslock 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
1184 1194
  "fswatcher 0.1.0 (git+https://github.com/mgottschlag/fswatcher-rs.git)",
1185 1195
  "futures 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
1186 1196
  "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1412,6 +1422,7 @@ dependencies = [
1412 1422
 "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
1413 1423
 "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
1414 1424
 "checksum fnv 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
1425
+"checksum fslock 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b14c83e47c73f7d62d907ae24a1a98e9132df3c33eb6c54fcf4bce0dbc41d5af"
1415 1426
 "checksum fswatcher 0.1.0 (git+https://github.com/mgottschlag/fswatcher-rs.git)" = "<none>"
1416 1427
 "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
1417 1428
 "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"

+ 1
- 0
Cargo.toml Прегледај датотеку

@@ -19,6 +19,7 @@ rmp-serde = "0.14"
19 19
 fswatcher = { git = "https://github.com/mgottschlag/fswatcher-rs.git" }
20 20
 url = "*"
21 21
 range-set = "*"
22
+fslock = "0.1"
22 23
 
23 24
 [dev-dependencies]
24 25
 criterion = "0.3"

+ 8
- 2
src/bin/client.rs Прегледај датотеку

@@ -1,4 +1,4 @@
1
-use std::fs::{create_dir_all, remove_file};
1
+use std::fs::{self, create_dir_all, remove_file};
2 2
 use std::sync::Arc;
3 3
 use std::ffi::{OsStr, OsString};
4 4
 
@@ -7,6 +7,7 @@ use tokio::net::{UnixListener, UnixStream};
7 7
 use tokio::stream::StreamExt;
8 8
 use tokio::sync::{mpsc, Mutex};
9 9
 use tokio::signal;
10
+use fslock::LockFile;
10 11
 
11 12
 use twfss::{Database, FileTree, ClientSideSync, FileSystemWatcher, Error, SynchronizationError, paths};
12 13
 
@@ -68,7 +69,12 @@ async fn main() {
68 69
     create_dir_all(data_dir()).unwrap();
69 70
 
70 71
     // Check whether the client is already running, and exit if it is.
71
-    // TODO
72
+    fs::write(&paths::client_lock(), b"").ok();
73
+    let mut lock = LockFile::open(&paths::client_lock()).expect("cannot open lockfile");
74
+    if !lock.try_lock().expect("error while locking the lock file") {
75
+        eprintln!("Client already running, exiting...");
76
+        return;
77
+    }
72 78
 
73 79
     let mut ctrl_c = Box::pin(signal::ctrl_c());
74 80
     let (errors_send, mut errors) = mpsc::channel(32);

+ 4
- 0
src/paths.rs Прегледај датотеку

@@ -19,3 +19,7 @@ pub fn client_db() -> String {
19 19
 pub fn client_socket() -> String {
20 20
     format!("{}/client.sock", client_data())
21 21
 }
22
+
23
+pub fn client_lock() -> String {
24
+    format!("{}/client.lock", client_data())
25
+}

Loading…
Откажи
Сачувај