added date command

Thu, 22 Aug 2013 12:45:12 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 22 Aug 2013 12:45:12 +0200
changeset 29
938957a4eea7
parent 28
4e46c65711ef
child 30
9a5a7a513a96

added date 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	Thu Aug 22 11:25:16 2013 +0200
+++ b/dav/main.c	Thu Aug 22 12:45:12 2013 +0200
@@ -88,6 +88,8 @@
         ret = cmd_remove(args);
     } else if(!strcasecmp(cmd, "mkdir") || !strcasecmp(cmd, "mkcol")) {
         ret = cmd_mkdir(args);
+    } else if(!strcasecmp(cmd, "date")) {
+        ret = cmd_date(args);
     } else {
         print_usage(argv[0]);
     }
@@ -104,6 +106,7 @@
     fprintf(stderr, "        put [-p] [-k <key>] <url> <file>\n");
     fprintf(stderr, "        mkdir <url>\n");
     fprintf(stderr, "        remove <url>\n");
+    fprintf(stderr, "        date [url]\n");
     fprintf(stderr, "\n");
     fprintf(stderr, "Options:\n");
     fprintf(stderr,
@@ -785,3 +788,52 @@
     
     return 0;
 }
+
+
+static size_t get_date_header_cb(void *header, int s, int n, void *data) {
+    char **date_str = (char**)data;
+    
+    //printf("header: %.*s\n", s*n, header);
+    sstr_t h = sstrn(header, s*n);
+    if(sstrprefix(h, S("Date:"))) {
+        sstr_t v = sstrsubs(h, 5);
+        v = sstrdup(sstrtrim(v));
+        *date_str = v.ptr;
+    }
+    return s*n;
+}
+
+int cmd_date(CmdArgs *a) {
+    if(a->argc < 1) {
+        time_t now = time(NULL);
+        struct tm *date = gmtime(&now);
+        char str[32];
+        putenv("LC_TIME=C");
+        size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT\n", date);
+        fwrite(str, 1, len, stdout);
+    } else {
+        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);
+        char *date = NULL;
+        curl_easy_setopt(sn->handle, CURLOPT_HEADERFUNCTION, get_date_header_cb);
+        curl_easy_setopt(sn->handle, CURLOPT_WRITEHEADER, &date);
+        if(dav_exists(res) && date) {
+            printf("%s\n", date);
+        } else {
+            return -1;
+        }
+    }
+    return 0;
+}
--- a/dav/main.h	Thu Aug 22 11:25:16 2013 +0200
+++ b/dav/main.h	Thu Aug 22 12:45:12 2013 +0200
@@ -53,6 +53,8 @@
 int cmd_remove(CmdArgs *args);
 int cmd_mkdir(CmdArgs *args);
 
+int cmd_date(CmdArgs *args);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/dav/methods.c	Thu Aug 22 11:25:16 2013 +0200
+++ b/dav/methods.c	Thu Aug 22 12:45:12 2013 +0200
@@ -500,3 +500,19 @@
     CURLcode ret = curl_easy_perform(handle);
     return ret;
 }
+
+
+CURLcode do_head_request(CURL *handle) {
+    curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "HEAD");
+    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_NOBODY, 1L);
+    
+    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, dummy_write);
+    curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
+    
+    CURLcode ret = curl_easy_perform(handle);
+    curl_easy_setopt(handle, CURLOPT_NOBODY, 0L);
+    return ret;
+}
--- a/dav/methods.h	Thu Aug 22 11:25:16 2013 +0200
+++ b/dav/methods.h	Thu Aug 22 12:45:12 2013 +0200
@@ -63,6 +63,8 @@
 
 CURLcode do_mkcol_request(CURL *handle);
 
+CURLcode do_head_request(CURL *handle);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/dav/webdav.c	Thu Aug 22 11:25:16 2013 +0200
+++ b/dav/webdav.c	Thu Aug 22 12:45:12 2013 +0200
@@ -811,3 +811,21 @@
         return 1;
     }
 }
+
+int dav_exists(DavResource *res) {
+    DavSession *sn = res->session;
+    char *url = util_concat_path(sn->base_url, res->path);  
+    CURL *handle = sn->handle;
+    curl_easy_setopt(handle, CURLOPT_URL, url);
+    free(url);
+    
+    CURLcode ret = do_head_request(handle);
+    int status = 0;
+    curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
+    if(ret == CURLE_OK && (status >= 200 && status < 300)) {
+        return 1;
+    } else {
+        session_set_error(sn, ret, status);
+        return 0;
+    }
+}
--- a/dav/webdav.h	Thu Aug 22 11:25:16 2013 +0200
+++ b/dav/webdav.h	Thu Aug 22 12:45:12 2013 +0200
@@ -196,6 +196,8 @@
 int dav_delete(DavResource *res);
 int dav_create(DavResource *res);
 
+int dav_exists(DavResource *res);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial