added put method

Sat, 01 Dec 2012 20:34:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 01 Dec 2012 20:34:55 +0100
changeset 4
ae5a98f0545c
parent 3
323689ada09d
child 5
88625853ae74

added put method

dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
dav/utils.c file | annotate | diff | comparison | revisions
dav/utils.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Sat Dec 01 16:31:36 2012 +0100
+++ b/dav/main.c	Sat Dec 01 20:34:55 2012 +0100
@@ -29,7 +29,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <libxml/xmlerror.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "propfind.h"
 #include "utils.h"
@@ -52,6 +55,8 @@
         return cmd_list(argc - 2, argv + 2);
     } else if(!strcmp(argv[1], "get")) {
         return cmd_get(argc - 2, argv + 2);
+    } else if(!strcmp(argv[1], "put")) {
+        return cmd_put(argc - 2, argv + 2);
     }
     
     print_usage();
@@ -62,6 +67,76 @@
     
 }
 
+void get_file(CURL *curl, char *url, char *path) {
+    curl_easy_setopt(curl, CURLOPT_URL, url);
+    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
+    
+    FILE *out = fopen(path, "w");
+    
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
+    curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);
+    
+    CURLcode res = curl_easy_perform(curl);
+    
+    fclose(out);
+    // handle some errors (http://curl.haxx.se/libcurl/c/libcurl-errors.html)
+    switch(res) {
+        case CURLE_OK: {
+            return;
+        }
+        case CURLE_REMOTE_ACCESS_DENIED: {
+            
+            break;
+        }
+        case CURLE_SSL_CONNECT_ERROR: {
+            
+            break;
+        }
+        case CURLE_LOGIN_DENIED: {
+            
+            break;
+        }
+        case CURLE_REMOTE_FILE_NOT_FOUND: {
+            
+            break;
+        }
+        default: {
+            
+            break;
+        }
+    }
+    
+    unlink(path);
+}
+
+void put_file(CURL *curl, char *url, char *path) {
+    curl_easy_setopt(curl, CURLOPT_URL, url);
+    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
+    
+    curl_easy_setopt(curl, CURLOPT_PUT, 1L);
+    
+    struct stat s;
+    if(stat(path, &s) != 0) {
+        perror("stat");
+        fprintf(stderr, "file: %s\n", path);
+        return;
+    }
+    
+    FILE *in = fopen(path, "r");
+    
+    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+    curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread);
+    curl_easy_setopt(curl, CURLOPT_READDATA, in);
+    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)s.st_size);
+    
+    CURLcode res = curl_easy_perform(curl);
+    fclose(in);
+    
+    if(res != CURLE_OK) {
+        fprintf(stderr, "put_file: %s\n", curl_easy_strerror(res));
+    }
+}
+
 int cmd_list(int argc, char **argv) {
     if(argc == 0) {
         return -1;
@@ -113,44 +188,27 @@
     return 0;
 }
 
-void get_file(CURL *curl, char *url, char *path) {
-    curl_easy_setopt(curl, CURLOPT_URL, url);
-    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
-    
-    FILE *out = fopen(path, "w");
-    
-    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
-    curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);
-    
-    CURLcode res = curl_easy_perform(curl);
-    
-    fclose(out);
-    // handle some errors (http://curl.haxx.se/libcurl/c/libcurl-errors.html)
-    switch(res) {
-        case CURLE_OK: {
-            return;
-        }
-        case CURLE_REMOTE_ACCESS_DENIED: {
-            
-            break;
-        }
-        case CURLE_SSL_CONNECT_ERROR: {
-            
-            break;
-        }
-        case CURLE_LOGIN_DENIED: {
-            
-            break;
-        }
-        case CURLE_REMOTE_FILE_NOT_FOUND: {
-            
-            break;
-        }
-        default: {
-            
-            break;
-        }
+int cmd_put(int argc, char **argv) {
+    if(argc == 0) {
+        return -1;
     }
     
-    unlink(path);
+    char *url = argv[0]; // TODO: use arg as url or alias
+    char *path;
+    if(argc > 0) {
+        path = argv[1];
+    } else {
+        fprintf(stderr, "put: missing file argument\n");
+        return -1;
+    }
+    
+    char *uploadurl = util_upload_url(url, path);
+    
+    CURL *curl = curl_easy_init();
+    
+    // TODO: check possible name conflicts
+    
+    put_file(curl, uploadurl, path);
+    
+    curl_easy_cleanup(curl);
 }
--- a/dav/main.h	Sat Dec 01 16:31:36 2012 +0100
+++ b/dav/main.h	Sat Dec 01 20:34:55 2012 +0100
@@ -37,10 +37,12 @@
 #endif
 
 void print_usage();
+void get_file(CURL *curl, char *url, char *path);
+void put_file(CURL *curl, char *url, char *path);
+
 int cmd_list(int argc, char **argv);
 int cmd_get(int argc, char **argv);
-
-void get_file(CURL *curl, char *url, char *path);
+int cmd_put(int argc, char **argv);
 
 #ifdef	__cplusplus
 }
--- a/dav/utils.c	Sat Dec 01 16:31:36 2012 +0100
+++ b/dav/utils.c	Sat Dec 01 20:34:55 2012 +0100
@@ -111,3 +111,31 @@
     
     return url;
 }
+
+char* util_upload_url(char *url, char *filepath) {
+    int urllen = strlen(url);
+    int filepathlen = strlen(filepath);
+    
+    for(int i=filepathlen-1;i>=0;i--) {
+        if(filepath[i] == '/') {
+            filepath = filepath + i + 1;
+            break;
+        }
+    }
+    
+    int pathlen = urllen + filepathlen;
+    char *path = malloc(pathlen + 2);
+    
+    int j = urllen;
+    memcpy(path, url, urllen);
+    if(url[urllen - 1] != '/') {
+        path[j] = '/';
+        j++;
+    }
+    memcpy(path + j, filepath, filepathlen);
+    j += filepathlen;
+    path[j] = 0;
+    
+    return path;
+}
+
--- a/dav/utils.h	Sat Dec 01 16:31:36 2012 +0100
+++ b/dav/utils.h	Sat Dec 01 20:34:55 2012 +0100
@@ -44,6 +44,7 @@
 
 char* util_child_url(char *url, char *href);
 
+char* util_upload_url(char *url, char *filepath);
 
 #ifdef	__cplusplus
 }

mercurial