Mathias Gottschlag 6 роки тому
джерело
коміт
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,12 +7,11 @@ use byteorder::{BigEndian, ReadBytesExt};
7 7
 use serde::{Deserialize, Serialize};
8 8
 use structopt::StructOpt;
9 9
 
10
-const SOCKET_PATH: &str = "$HOME/.twfss/sock";
11
-
12 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 17
 #[derive(StructOpt, Serialize, Deserialize)]
@@ -30,12 +29,14 @@ pub enum Command {
30 29
         status: bool,
31 30
     },
32 31
     AddDirectory {
32
+        // TODO: Login information.
33 33
         local_directory: PathBuf,
34 34
         remote_url: String,
35 35
     },
36 36
     RemoveDirectory {
37 37
         local_directory: PathBuf,
38 38
     },
39
+    // TODO: Command to update login information in case of login problems.
39 40
 }
40 41
 
41 42
 // The file is included in the sync client, where main() is unused.

+ 36
- 1
src/bin/client.rs Переглянути файл

@@ -1,15 +1,50 @@
1
+use std::fs::create_dir_all;
1 2
 use std::io::{self, Read, Write};
2 3
 use std::os::unix::net::{UnixListener, UnixStream};
4
+use std::sync::{Arc, Mutex};
3 5
 use std::thread;
4 6
 
7
+use twfss::{Database, SynchronizedDirectory};
8
+
5 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 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 35
     // TODO
10 36
 
11 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 48
     // Listen for CLI commands.
14 49
     // TODO: Graceful shutdown on errors.
15 50
     let listener = UnixListener::bind(cli::socket_path()).unwrap();

+ 65
- 0
src/lib.rs Переглянути файл

@@ -0,0 +1,65 @@
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
+}

Завантаження…
Відмінити
Зберегти