浏览代码

Client WIP.

父节点
当前提交
7d4a478d15
共有 3 个文件被更改,包括 107 次插入6 次删除
  1. 6
    5
      src/bin/cli.rs
  2. 36
    1
      src/bin/client.rs
  3. 65
    0
      src/lib.rs

+ 6
- 5
src/bin/cli.rs 查看文件

7
 use serde::{Deserialize, Serialize};
7
 use serde::{Deserialize, Serialize};
8
 use structopt::StructOpt;
8
 use structopt::StructOpt;
9
 
9
 
10
-const SOCKET_PATH: &str = "$HOME/.twfss/sock";
11
-
12
 pub fn socket_path() -> String {
10
 pub fn socket_path() -> String {
13
-    SOCKET_PATH
14
-        .to_owned()
15
-        .replace("$HOME", dirs::home_dir().unwrap().to_str().unwrap())
11
+    format!(
12
+        "{}/.twfss/sock",
13
+        dirs::home_dir().unwrap().to_str().unwrap().to_owned()
14
+    )
16
 }
15
 }
17
 
16
 
18
 #[derive(StructOpt, Serialize, Deserialize)]
17
 #[derive(StructOpt, Serialize, Deserialize)]
30
         status: bool,
29
         status: bool,
31
     },
30
     },
32
     AddDirectory {
31
     AddDirectory {
32
+        // TODO: Login information.
33
         local_directory: PathBuf,
33
         local_directory: PathBuf,
34
         remote_url: String,
34
         remote_url: String,
35
     },
35
     },
36
     RemoveDirectory {
36
     RemoveDirectory {
37
         local_directory: PathBuf,
37
         local_directory: PathBuf,
38
     },
38
     },
39
+    // TODO: Command to update login information in case of login problems.
39
 }
40
 }
40
 
41
 
41
 // The file is included in the sync client, where main() is unused.
42
 // The file is included in the sync client, where main() is unused.

+ 36
- 1
src/bin/client.rs 查看文件

1
+use std::fs::create_dir_all;
1
 use std::io::{self, Read, Write};
2
 use std::io::{self, Read, Write};
2
 use std::os::unix::net::{UnixListener, UnixStream};
3
 use std::os::unix::net::{UnixListener, UnixStream};
4
+use std::sync::{Arc, Mutex};
3
 use std::thread;
5
 use std::thread;
4
 
6
 
7
+use twfss::{Database, SynchronizedDirectory};
8
+
5
 mod cli;
9
 mod cli;
6
 
10
 
11
+fn config_dir() -> String {
12
+    format!(
13
+        "{}/.config/twfss",
14
+        dirs::home_dir().unwrap().to_str().unwrap().to_owned()
15
+    )
16
+}
17
+
18
+fn data_dir() -> String {
19
+    format!(
20
+        "{}/.local/share/twfss-client",
21
+        dirs::home_dir().unwrap().to_str().unwrap().to_owned()
22
+    )
23
+}
24
+
25
+fn db_path() -> String {
26
+    format!("{}/client.db", data_dir())
27
+}
28
+
7
 fn main() {
29
 fn main() {
8
-    // Initialize the existing synchronized directories.
30
+    // Create the data directories if necessary.
31
+    create_dir_all(config_dir()).unwrap();
32
+    create_dir_all(data_dir()).unwrap();
33
+
34
+    // Check whether the client is already running, and exit if it is.
9
     // TODO
35
     // TODO
10
 
36
 
11
     // TODO: Register signal handler for graceful shutdown.
37
     // TODO: Register signal handler for graceful shutdown.
12
 
38
 
39
+    // Initialize the existing synchronized directories.
40
+    let mut directories = Vec::new();
41
+    let db = Arc::new(Mutex::new(Database::create_or_open(&db_path()).unwrap()));
42
+    for directory in db.lock().unwrap().synchronized_directories() {
43
+        // TODO: Error handling. If the directory was removed, remove it from the
44
+        // list of synchronized directories.
45
+        directories.push(SynchronizedDirectory::open(db.clone(), &directory, false).unwrap());
46
+    }
47
+
13
     // Listen for CLI commands.
48
     // Listen for CLI commands.
14
     // TODO: Graceful shutdown on errors.
49
     // TODO: Graceful shutdown on errors.
15
     let listener = UnixListener::bind(cli::socket_path()).unwrap();
50
     let listener = UnixListener::bind(cli::socket_path()).unwrap();

+ 65
- 0
src/lib.rs 查看文件

1
+use std::io;
2
+use std::sync::{Arc, Mutex};
3
+
4
+pub struct SynchronizedDirectory {
5
+    db: Arc<Mutex<Database>>,
6
+    master: bool,
7
+    // TODO
8
+}
9
+
10
+impl SynchronizedDirectory {
11
+    pub fn open(
12
+        db: Arc<Mutex<Database>>,
13
+        _path: &str,
14
+        master: bool,
15
+    ) -> Result<SynchronizedDirectory, Error> {
16
+        // TODO
17
+        Ok(SynchronizedDirectory { db, master })
18
+    }
19
+
20
+    // TODO: Login information.
21
+    pub fn new(
22
+        db: Arc<Mutex<Database>>,
23
+        local_path: &str,
24
+        remote_path: &str,
25
+        master: bool,
26
+    ) -> Result<SynchronizedDirectory, Error> {
27
+        // TODO
28
+        Ok(SynchronizedDirectory { db, master })
29
+    }
30
+}
31
+
32
+pub struct SyncEvent {
33
+    // TODO
34
+}
35
+
36
+impl SyncEvent {
37
+    // TODO
38
+}
39
+
40
+pub struct Database {
41
+    // TODO
42
+}
43
+
44
+impl Database {
45
+    pub fn create_or_open(_path: &str) -> Result<Database, Error> {
46
+        // TODO
47
+        Ok(Database {})
48
+    }
49
+
50
+    pub fn synchronized_directories(&self) -> Vec<String> {
51
+        // TODO
52
+        Vec::new()
53
+    }
54
+}
55
+
56
+#[derive(Debug)]
57
+pub enum Error {
58
+    Io(std::io::Error),
59
+}
60
+
61
+impl From<io::Error> for Error {
62
+    fn from(e: io::Error) -> Error {
63
+        Error::Io(e)
64
+    }
65
+}

正在加载...
取消
保存