added remove command

Thu, 15 Aug 2013 11:23:25 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 Aug 2013 11:23:25 +0200
changeset 15
182af08b4813
parent 14
d1a43035d3a2
child 16
5dbef9e07376

added remove command

dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
dav/methods.c file | annotate | diff | comparison | revisions
dav/methods.h file | annotate | diff | comparison | revisions
dav/webdav.c file | annotate | diff | comparison | revisions
dav/webdav.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Wed Aug 14 18:01:37 2013 +0200
+++ b/dav/main.c	Thu Aug 15 11:23:25 2013 +0200
@@ -74,6 +74,8 @@
         return cmd_get(args);
     } else if(!strcmp(cmd, "put")) {
         return cmd_put(args);
+    } else if(!strcmp(cmd, "remove") || !strcmp(cmd, "rm")) {
+        return cmd_remove(args);
     }
     
     print_usage(argv[0]);
@@ -135,11 +137,11 @@
     *path = strdup("/");
 }
 
-void print_resource_error(DavSession *sn, DavResource *res) {
-    char *res_url = util_concat_path(sn->base_url, res->path);
+void print_resource_error(DavSession *sn, char *path) {
+    char *res_url = util_concat_path(sn->base_url, path);
     switch(sn->error) {
         default: {
-            fprintf(stderr, "Cannot get resource %s.\n", res_url);
+            fprintf(stderr, "Operation failed for resource %s.\n", res_url);
             break;
         }
         case DAV_NOT_FOUND: {
@@ -161,6 +163,7 @@
 
 int cmd_list(CmdArgs *a) {
     if(a->argc == 0) {
+        fprintf(stderr, "Too few arguments\n");
         return -1;
     }
     
@@ -181,7 +184,7 @@
     
     DavResource *ls = dav_get(sn, path, "U:crypto-key");
     if(!ls) {
-        print_resource_error(sn, ls);
+        print_resource_error(sn, path);
         return -1;
     }
     
@@ -222,23 +225,67 @@
 static char* ls_size_str(DavResource *res) {
     char *str = malloc(16);
     uint64_t size = res->contentlength;
-
+    
     if(res->iscollection) {
-        snprintf(str, 16, "%" PRIu64, size);
+        snprintf(str, 16, "");
     } else if(size < 0x400) {
-        snprintf(str, 16, "%" PRIu64 " Bytes", size);
+        snprintf(str, 16, "%" PRIu64 " bytes", size);
     } else if(size < 0x100000) {
-        size /= 0x400;
-        snprintf(str, 16, "%" PRIu64 " KiB", size);
+        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) {
-        size /= 0x100000;
-        snprintf(str, 16, "%" PRIu64 "MiB", size);
+        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) {
-        size /= 0x40000000;
-        snprintf(str, 16, "%" PRIu64 "GiB", size);
+        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 /= 0x1000000000ULL;
-        snprintf(str, 16, "%" PRIu64, "TiB", size);
+        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;
 }
@@ -289,6 +336,7 @@
 
 int cmd_get(CmdArgs *a) {
     if(a->argc == 0) {
+        fprintf(stderr, "Too few arguments\n");
         return -1;
     }
     
@@ -307,7 +355,7 @@
     
     DavResource *res = dav_get(sn, path, "U:crypto-key");
     if(!res) {
-        print_resource_error(sn, res);
+        print_resource_error(sn, path);
         return -1;
     }
     
@@ -380,6 +428,7 @@
 
 int cmd_put(CmdArgs *a) {
     if(a->argc < 2) {
+        fprintf(stderr, "Too few arguments\n");
         return -1;
     }
     
@@ -436,7 +485,7 @@
     }
     
     if(dav_store(res)) {
-        print_resource_error(sn, res);
+        print_resource_error(sn, res->path);
         fprintf(stderr, "Cannot upload file.\n");
         fclose(in);
         return -1;
@@ -447,3 +496,37 @@
     fclose(in);
     return 0;
 }
+
+int cmd_remove(CmdArgs *a) {
+    if(a->argc < 1) {
+        fprintf(stderr, "Too few arguments\n");
+        return -1;
+    }
+    
+    DavSession *sn = NULL;
+    char *url = a->argv[0];
+    char *root = NULL;
+    char *path = NULL;
+    url_get_parts(url, &root, &path);
+    
+    Repository *repo = get_repository(root);
+    if(repo) {
+        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
+    } else {
+        sn = dav_session_new(ctx, root);
+    }
+    
+    DavResource *res = dav_resource_new(sn, path);
+    if(!res) {
+        fprintf(stderr, "error\n");
+        return -1;
+    }
+    
+    if(dav_delete(res)) {
+        print_resource_error(sn, res->path);
+        fprintf(stderr, "Cannot delete resource.\n");
+        return -1;
+    }
+    
+    return 0;
+}
--- a/dav/main.h	Wed Aug 14 18:01:37 2013 +0200
+++ b/dav/main.h	Thu Aug 15 11:23:25 2013 +0200
@@ -45,6 +45,7 @@
 
 int cmd_get(CmdArgs *args);
 int cmd_put(CmdArgs *args);
+int cmd_remove(CmdArgs *args);
 
 #ifdef	__cplusplus
 }
--- a/dav/methods.c	Wed Aug 14 18:01:37 2013 +0200
+++ b/dav/methods.c	Thu Aug 15 11:23:25 2013 +0200
@@ -460,3 +460,15 @@
     return res;
 }
 
+CURLcode do_delete_request(CURL *handle, UcxBuffer *response) {
+    curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "DELETE");
+    curl_easy_setopt(handle, CURLOPT_PUT, 0L);  
+    curl_easy_setopt(handle, CURLOPT_UPLOAD, 0L);
+    curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0);
+    
+    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ucx_buffer_write);
+    curl_easy_setopt(handle, CURLOPT_WRITEDATA, response);
+    
+    CURLcode res = curl_easy_perform(handle);
+    return res;
+}
--- a/dav/methods.h	Wed Aug 14 18:01:37 2013 +0200
+++ b/dav/methods.h	Thu Aug 15 11:23:25 2013 +0200
@@ -59,6 +59,8 @@
 
 UcxBuffer* create_proppatch_request(DavNodeData *data);
 
+CURLcode do_delete_request(CURL *handle, UcxBuffer *response);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/dav/webdav.c	Wed Aug 14 18:01:37 2013 +0200
+++ b/dav/webdav.c	Thu Aug 15 11:23:25 2013 +0200
@@ -481,7 +481,6 @@
     curl_easy_setopt(handle, CURLOPT_WRITEDATA, stream);
     
     CURLcode ret = curl_easy_perform(handle);
-    
     int status = 0;
     curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
     if(ret == CURLE_OK && (status >= 200 && status < 300)) {
@@ -493,3 +492,25 @@
     }
 }
 
+int dav_delete(DavResource *res) {
+    char *url = util_concat_path(res->session->base_url, res->path);
+    CURL *handle = res->session->handle;
+    curl_easy_setopt(handle, CURLOPT_URL, url);
+    free(url);
+    
+    UcxBuffer *response = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
+    CURLcode ret = do_delete_request(handle, response);
+    int status = 0;
+    curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
+    if(ret == CURLE_OK && (status >= 200 && status < 300)) {
+        res->session->error = DAV_OK;
+        
+        // TODO: parse response
+        // TODO: free res
+        
+        return 0;
+    } else {
+        session_set_error(res->session, ret, status);
+        return 1;
+    }
+}
--- a/dav/webdav.h	Wed Aug 14 18:01:37 2013 +0200
+++ b/dav/webdav.h	Thu Aug 15 11:23:25 2013 +0200
@@ -170,6 +170,8 @@
 
 int dav_get_content(DavResource *res, void *stream, dav_write_func write_func);
 
+int dav_delete(DavResource *res);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial