|
|
@@ -1,12 +1,13 @@
|
|
1
|
1
|
use std::fs::create_dir_all;
|
|
2
|
|
-use std::io;
|
|
3
|
|
-use std::sync::{Arc, Mutex};
|
|
|
2
|
+use std::sync::Arc;
|
|
|
3
|
+use std::ffi::OsStr;
|
|
4
|
4
|
|
|
5
|
5
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
6
|
6
|
use tokio::net::{UnixListener, UnixStream};
|
|
7
|
7
|
use tokio::stream::StreamExt;
|
|
|
8
|
+use tokio::sync::{mpsc, Mutex};
|
|
8
|
9
|
|
|
9
|
|
-use twfss::Database;
|
|
|
10
|
+use twfss::{Database, FileTree, ClientSideSync, FileSystemWatcher, Error, SynchronizationError};
|
|
10
|
11
|
|
|
11
|
12
|
mod cli;
|
|
12
|
13
|
|
|
|
@@ -28,6 +29,33 @@ fn db_path() -> String {
|
|
28
|
29
|
format!("{}/client.db", data_dir())
|
|
29
|
30
|
}
|
|
30
|
31
|
|
|
|
32
|
+struct SynchronizedDirectory {
|
|
|
33
|
+ client_side_sync: ClientSideSync,
|
|
|
34
|
+ file_system_watcher: FileSystemWatcher,
|
|
|
35
|
+}
|
|
|
36
|
+
|
|
|
37
|
+impl SynchronizedDirectory {
|
|
|
38
|
+ async fn open(database: Arc<Mutex<Database>>, directory: &OsStr, errors: mpsc::Sender<SynchronizationError>) -> Result<SynchronizedDirectory, Error> {
|
|
|
39
|
+ let file_tree = Arc::new(Mutex::new(FileTree::open(database, directory)?));
|
|
|
40
|
+ let client_side_sync = ClientSideSync::new(file_tree.clone(), errors.clone()).await?;
|
|
|
41
|
+ let file_system_watcher = FileSystemWatcher::new(file_tree, errors).await?;
|
|
|
42
|
+ Ok(SynchronizedDirectory{
|
|
|
43
|
+ client_side_sync,
|
|
|
44
|
+ file_system_watcher,
|
|
|
45
|
+ })
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ async fn unpause(&mut self) {
|
|
|
49
|
+ self.client_side_sync.unpause().await;
|
|
|
50
|
+ self.file_system_watcher.unpause().await;
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ async fn pause(&mut self) {
|
|
|
54
|
+ self.client_side_sync.pause().await;
|
|
|
55
|
+ self.file_system_watcher.pause().await;
|
|
|
56
|
+ }
|
|
|
57
|
+}
|
|
|
58
|
+
|
|
31
|
59
|
#[tokio::main]
|
|
32
|
60
|
async fn main() {
|
|
33
|
61
|
// Create the data directories if necessary.
|
|
|
@@ -39,14 +67,20 @@ async fn main() {
|
|
39
|
67
|
|
|
40
|
68
|
// TODO: Register signal handler for graceful shutdown.
|
|
41
|
69
|
|
|
|
70
|
+ // TODO: Error handling. If a directory was removed, remove it from the
|
|
|
71
|
+ // list of synchronized directories (or mark it for the user).
|
|
|
72
|
+ let (errors_send, errors) = mpsc::channel(32);
|
|
|
73
|
+
|
|
42
|
74
|
// Initialize the existing synchronized directories.
|
|
43
|
|
- //let mut directories = Vec::new();
|
|
44
|
|
- let _db = Arc::new(Mutex::new(Database::create_or_open(&db_path()).unwrap()));
|
|
45
|
|
- /*for directory in db.lock().unwrap().synchronized_directories() {
|
|
46
|
|
- // TODO: Error handling. If the directory was removed, remove it from the
|
|
47
|
|
- // list of synchronized directories.
|
|
48
|
|
- directories.push(SynchronizedDirectory::open(db.clone(), &directory, false).unwrap());
|
|
49
|
|
- }*/
|
|
|
75
|
+ let mut directories = Vec::new();
|
|
|
76
|
+ let db = Arc::new(Mutex::new(Database::create_or_open(&db_path()).unwrap()));
|
|
|
77
|
+ for directory in db.lock().await.synchronized_directories() {
|
|
|
78
|
+ // TODO: Error handling - remove the synchronized directory instead (or mark it for the
|
|
|
79
|
+ // user).
|
|
|
80
|
+ let mut sync_dir = SynchronizedDirectory::open(db.clone(), &directory, errors_send.clone()).await.unwrap();
|
|
|
81
|
+ sync_dir.unpause().await;
|
|
|
82
|
+ directories.push(sync_dir);
|
|
|
83
|
+ }
|
|
50
|
84
|
|
|
51
|
85
|
// Listen for CLI commands.
|
|
52
|
86
|
// TODO: Graceful shutdown on errors.
|
|
|
@@ -98,20 +132,3 @@ async fn handle_cli_client(stream: &mut UnixStream) -> Result<(), Error> {
|
|
98
|
132
|
Ok(())
|
|
99
|
133
|
}
|
|
100
|
134
|
|
|
101
|
|
-#[derive(Debug)]
|
|
102
|
|
-enum Error {
|
|
103
|
|
- Json(serde_json::error::Error),
|
|
104
|
|
- Io(std::io::Error),
|
|
105
|
|
-}
|
|
106
|
|
-
|
|
107
|
|
-impl From<serde_json::error::Error> for Error {
|
|
108
|
|
- fn from(e: serde_json::error::Error) -> Error {
|
|
109
|
|
- Error::Json(e)
|
|
110
|
|
- }
|
|
111
|
|
-}
|
|
112
|
|
-
|
|
113
|
|
-impl From<io::Error> for Error {
|
|
114
|
|
- fn from(e: io::Error) -> Error {
|
|
115
|
|
- Error::Io(e)
|
|
116
|
|
- }
|
|
117
|
|
-}
|