Bläddra i källkod

Check whether new synchronized directories exist and are empty.

Mathias Gottschlag 4 år sedan
förälder
incheckning
28517cd188
2 ändrade filer med 16 tillägg och 4 borttagningar
  1. 2
    0
      src/bin/cli.rs
  2. 14
    4
      src/bin/client.rs

+ 2
- 0
src/bin/cli.rs Visa fil

@@ -28,6 +28,8 @@ pub enum Command {
28 28
         // TODO: Login information.
29 29
         local_directory: PathBuf,
30 30
         remote_url: String,
31
+        #[structopt(short, long)]
32
+        force: bool,
31 33
     },
32 34
     RemoveDirectory {
33 35
         local_directory: PathBuf,

+ 14
- 4
src/bin/client.rs Visa fil

@@ -52,9 +52,7 @@ impl SynchronizedDirectory {
52 52
         errors: mpsc::Sender<SynchronizationError>,
53 53
     ) -> Result<SynchronizedDirectory, Error> {
54 54
         // TODO: First, connect to the server and test whether we can access the URL?
55
-        // At this point, the directory is still paused.
56
-        // TODO: Return an error if the directory is not empty (unless the user specified an
57
-        // override option).
55
+        // Probably not possible, at this point the directory is still paused.
58 56
         let file_tree = Arc::new(Mutex::new(FileTree::create(
59 57
             database, directory, remote_url,
60 58
         )?));
@@ -157,7 +155,7 @@ async fn main() {
157 155
                                 stream.write_all(&(-1i32).to_be_bytes()).await.ok();
158 156
                                 stream.write_all(&output).await.ok();
159 157
                                 stream
160
-                                    .write_all(format!("Error: {:?}", e).as_bytes())
158
+                                    .write_all(format!("Error: {}\n", e).as_bytes())
161 159
                                     .await
162 160
                                     .ok();
163 161
                             }
@@ -239,7 +237,19 @@ async fn handle_cli_client(
239 237
         cli::Command::AddDirectory {
240 238
             local_directory,
241 239
             remote_url,
240
+            force,
242 241
         } => {
242
+            if !local_directory.exists() || !local_directory.is_dir() {
243
+                return Err(Error::InvalidParam(
244
+                    "the specified local path does not exist or is no directory".to_owned(),
245
+                ));
246
+            }
247
+            if local_directory.read_dir()?.next().is_some() && !force {
248
+                return Err(Error::InvalidParam(
249
+                    "the specified local directory is not empty (use --force to add the directory)"
250
+                        .to_owned(),
251
+                ));
252
+            }
243 253
             let local_directory = fs::canonicalize(local_directory)?;
244 254
             // We need to check that we are not synchronizing directories that are within
245 255
             // other synchronized directories.

Loading…
Avbryt
Spara