added trash-info and empty-trash commands

Thu, 03 Mar 2016 15:52:18 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 03 Mar 2016 15:52:18 +0100
changeset 204
4f0207044837
parent 203
9a93ebd71984
child 205
bf756f6c3581

added trash-info and empty-trash commands

dav/main.c file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Wed Mar 02 10:38:03 2016 +0100
+++ b/dav/main.c	Thu Mar 03 15:52:18 2016 +0100
@@ -260,6 +260,7 @@
         repo = calloc(1, sizeof(Repository));
         repo->name = strdup("");
         repo->decrypt_content = true;
+        repo->verification = true;
         repo->authmethods = CURLAUTH_BASIC;
         if(url[ulen-1] == '/') {
             repo->url = strdup(url);
--- a/dav/sync.c	Wed Mar 02 10:38:03 2016 +0100
+++ b/dav/sync.c	Thu Mar 03 15:52:18 2016 +0100
@@ -90,6 +90,10 @@
         ret = cmd_pull(args);
     } else if(!strcmp(cmd, "push")) {
         ret = cmd_push(args);
+    } else if(!strcmp(cmd, "trash-info")) {
+        ret = cmd_trash_info(args);
+    } else if(!strcmp(cmd, "empty-trash")) {
+        ret = cmd_empty_trash(args);
     } else if(!strcmp(cmd, "add-directory")) {
         ret = cmd_add_directory(args);
     } else if(!strcmp(cmd, "list-directories")) {
@@ -113,11 +117,14 @@
     fprintf(stderr, "Commands:\n");
     fprintf(stderr, "        pull [-c] <directory>\n");
     //fprintf(stderr, "        push [-r] <directory>\n\n");
-    fprintf(stderr, "        push <directory>\n\n");
+    fprintf(stderr, "        push <directory>\n");
+    fprintf(stderr, "        trash-info <directory>\n");
+    fprintf(stderr, "        empty-trash <directory>\n\n");
     
     fprintf(stderr, "Options:\n");
-    fprintf(stderr, "        -c         Disable conflict detection\n\n");
+    fprintf(stderr, "        -c         Disable conflict detection\n");
     //fprintf(stderr, "        -r         Read changes from stdin\n\n");
+    fprintf(stderr, "        -v         verbose output (all commands)\n\n");
     
     fprintf(stderr, "Config commands:\n");
     fprintf(stderr, "        add-directory\n");
@@ -1033,6 +1040,183 @@
     return ret;
 }
 
+// TODO: remove code dup (main.c ls_size_str)
+static char* size_str(uint64_t size) {
+    char *str = malloc(16);
+    
+    if(size < 0x400) {
+        snprintf(str, 16, "%" PRIu64 " bytes", size);
+    } else if(size < 0x100000) {
+        float s = (float)size/0x400;
+        int diff = (s*100 - (int)s*100);
+        if(diff > 90) {
+            diff = 0;
+            s += 0.10f;
+        }
+        if(size < 0x2800 && diff != 0) {
+            // size < 10 KiB
+            snprintf(str, 16, "%.1f KiB", s);
+        } else {
+            snprintf(str, 16, "%.0f KiB", s);
+        }
+    } else if(size < 0x40000000) {
+        float s = (float)size/0x100000;
+        int diff = (s*100 - (int)s*100);
+        if(diff > 90) {
+            diff = 0;
+            s += 0.10f;
+        }
+        if(size < 0xa00000 && diff != 0) {
+            // size < 10 MiB
+            snprintf(str, 16, "%.1f MiB", s);
+        } else {
+            size /= 0x100000;
+            snprintf(str, 16, "%.0f MiB", s);
+        }
+    } else if(size < 0x1000000000ULL) {
+        float s = (float)size/0x40000000;
+        int diff = (s*100 - (int)s*100);
+        if(diff > 90) {
+            diff = 0;
+            s += 0.10f;
+        }
+        if(size < 0x280000000 && diff != 0) {
+            // size < 10 GiB
+            snprintf(str, 16, "%.1f GiB", s);
+        } else {
+            size /= 0x40000000;
+            snprintf(str, 16, "%.0f GiB", s);
+        }
+    } else {
+        size /= 1024;
+        float s = (float)size/0x40000000;
+        int diff = (s*100 - (int)s*100);
+        if(diff > 90) {
+            diff = 0;
+            s += 0.10f;
+        }
+        if(size < 0x280000000 && diff != 0) {
+            // size < 10 TiB
+            snprintf(str, 16, "%.1f TiB", s);
+        } else {
+            size /= 0x40000000;
+            snprintf(str, 16, "%.0f TiB", s);
+        }
+    }
+    return str;
+}
+
+int cmd_trash_info(CmdArgs *a) {
+    if(a->argc != 1) {
+        fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few" : "many");
+        return -1;
+    }
+    
+    SyncDirectory *syncdir = scfg_get_dir(a->argv[0]);
+    if(!syncdir) {
+        fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
+        return -1;
+    }
+    
+    if(!syncdir->trash) {
+        printf("trash not configured\n", syncdir->name);
+        return 0;
+    }
+    
+    DIR *dir = opendir(syncdir->trash);
+    if(!dir) {
+        fprintf(stderr, "cannot open trash directory: %s\n", syncdir->trash);
+        perror("opendir");
+        return -1;
+    }
+    
+    uint64_t trashsize = 0;
+    int count = 0;
+    struct dirent *ent;
+    while((ent = readdir(dir)) != NULL) {
+        if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
+            continue;
+        }
+        
+        char *path = util_concat_path(syncdir->trash, ent->d_name);
+        
+        struct stat s;
+        if(stat(path, &s)) {
+            perror("stat");
+        } else {
+            trashsize += s.st_size;
+        }
+        count++;
+        
+        free(path);
+    }
+    closedir(dir);
+    
+    printf("path: %s\n", syncdir->trash);
+    printf("%d %s\n", count, count == 1 ? "file" : "files");
+    char *sizestr = size_str(trashsize);
+    printf("%s\n", sizestr);
+    free(sizestr);
+    
+    return 0;
+}
+
+
+int cmd_empty_trash(CmdArgs *a) {
+    if(a->argc != 1) {
+        fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few" : "many");
+        return -1;
+    }
+    
+    SyncDirectory *syncdir = scfg_get_dir(a->argv[0]);
+    if(!syncdir) {
+        fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
+        return -1;
+    }
+    
+    if(!syncdir->trash) {
+        fprintf(stderr, "trash not configured for %s\n", syncdir->name);
+        return -1;
+    }
+    
+    DIR *dir = opendir(syncdir->trash);
+    if(!dir) {
+        fprintf(stderr, "cannot open trash directory: %s\n", syncdir->trash);
+        perror("opendir");
+        return -1;
+    }
+    
+    struct dirent *ent;
+    while((ent = readdir(dir)) != NULL) {
+        if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) {
+            continue;
+        }
+        
+        char *path = util_concat_path(syncdir->trash, ent->d_name);
+        printf("delete: %s\n", path);
+        
+        struct stat s;
+        if(stat(path, &s)) {
+            perror("stat");
+            free(path);
+            continue;
+        }
+        if(S_ISDIR(s.st_mode)) {
+            if(rmdir(path)) {
+                perror("unlink");
+            }
+        } else {
+            if(unlink(path)) {
+                perror("unlink");
+            }
+        }
+        
+        free(path);
+    }
+    closedir(dir);
+    
+    return 0;
+}
 
 int cmd_add_directory(CmdArgs *args) {
     printf("Each sync directory must have an unique name.\n");
--- a/dav/sync.h	Wed Mar 02 10:38:03 2016 +0100
+++ b/dav/sync.h	Thu Mar 03 15:52:18 2016 +0100
@@ -75,6 +75,8 @@
 int sync_delete_remote_resource(DavSession *sn, LocalResource *res, int *counter);
 
 
+int cmd_trash_info(CmdArgs *args);
+int cmd_empty_trash(CmdArgs *args);
 int cmd_add_directory(CmdArgs *args);
 
 #ifdef	__cplusplus

mercurial