added some error messages

Tue, 13 Aug 2013 13:51:00 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 13 Aug 2013 13:51:00 +0200
changeset 13
8a0cc4d90de7
parent 12
493128ef1b12
child 14
d1a43035d3a2

added some error messages

dav/main.c file | annotate | diff | comparison | revisions
dav/methods.c 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	Tue Aug 13 12:44:23 2013 +0200
+++ b/dav/main.c	Tue Aug 13 13:51:00 2013 +0200
@@ -132,6 +132,29 @@
     *path = strdup("/");
 }
 
+void print_resource_error(DavSession *sn, DavResource *res) {
+    char *res_url = util_concat_path(sn->base_url, res->path);
+    switch(sn->error) {
+        default: {
+            fprintf(stderr, "Cannot get resource %s.\n", res_url);
+            break;
+        }
+        case DAV_NOT_FOUND: {
+            fprintf(stderr, "Resource %s not found.\n", res_url);
+            break;
+        }
+        case DAV_UNAUTHORIZED: {
+            fprintf(stderr, "Authentication required.\n");
+            break;
+        }
+        case DAV_FORBIDDEN: {
+            fprintf(stderr, "Access forbidden.\n");
+            break;
+        }
+    }
+    free(res_url);
+}
+
 
 int cmd_list(CmdArgs *a) {
     if(a->argc == 0) {
@@ -155,7 +178,7 @@
     
     DavResource *ls = dav_get(sn, path, NULL);
     if(!ls) {
-        fprintf(stderr, "error\n");
+        print_resource_error(sn, ls);
         return -1;
     }
     DavResource *child = ls->children;
@@ -187,7 +210,7 @@
     
     DavResource *res = dav_get(sn, path, "U:crypto-key");
     if(!res) {
-        fprintf(stderr, "error\n");
+        print_resource_error(sn, res);
         return -1;
     }
     
@@ -316,7 +339,8 @@
     }
     
     if(dav_store(res)) {
-        fprintf(stderr, "cannot upload file\n");
+        print_resource_error(sn, res);
+        fprintf(stderr, "Cannot upload file.\n");
         fclose(in);
         return -1;
     }
--- a/dav/methods.c	Tue Aug 13 12:44:23 2013 +0200
+++ b/dav/methods.c	Tue Aug 13 13:51:00 2013 +0200
@@ -158,6 +158,7 @@
     xmlDoc *doc = xmlReadMemory(response->space, response->size, url, NULL, 0);
     if(!doc) {
         // TODO: free stuff
+        sn->error = DAV_ERROR;
         return NULL;
     }
     
@@ -185,6 +186,7 @@
                 xmlNode *href_content = node->children;
                 if(href_content->type != XML_TEXT_NODE) {
                     // error
+                    resource->session->error = DAV_ERROR;
                     return 1;
                 }
                 if(xstreq(resource->href, href_content->content)) {
@@ -205,10 +207,12 @@
                         } else if(xstreq(n->name, "status")) {
                             xmlNode *status_node = n->children;
                             if(status_node->type != XML_TEXT_NODE) {
+                                resource->session->error = DAV_ERROR;
                                 return 1;
                             }
                             sstr_t status_str = sstr((char*)status_node->content);
                             if(status_str.length < 13) {
+                                resource->session->error = DAV_ERROR;
                                 return 1;
                             }
                             status_str = sstrsubsl(status_str, 9, 3);
--- a/dav/webdav.c	Tue Aug 13 12:44:23 2013 +0200
+++ b/dav/webdav.c	Tue Aug 13 13:51:00 2013 +0200
@@ -133,6 +133,19 @@
     }
 }
 
+void session_set_error(DavSession *sn, CURLcode c, int status) {
+    if(status > 0) {
+        switch(status) {
+            default: sn->error = DAV_ERROR; break;
+            case 401: sn->error = DAV_UNAUTHORIZED; break;
+            case 403: sn->error = DAV_FORBIDDEN; break;
+            case 404: sn->error = DAV_NOT_FOUND; break;
+        }
+    } else {
+        sn->error = DAV_ERROR;
+    }
+}
+
 DavResource* dav_get(DavSession *sn, char *path, char *properties) {
     char *url = util_concat_path(sn->base_url, path);
     
@@ -157,6 +170,9 @@
     if(ret == CURLE_OK && status == 207) {
         //printf("response\n%s\n", rpbuf->space); 
         resource = parse_propfind_response(sn, rpbuf);
+        sn->error = DAV_OK;
+    } else  {
+        session_set_error(sn, ret, status);
     }
     return resource;
 }
@@ -315,6 +331,8 @@
     //printf("\n");
     
     CURLcode ret = do_propfind_request(handle, rqbuf, rpbuf);
+    int status = 0;
+    curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
     if(ret == CURLE_OK) {
         //printf("response\n%s\n", rpbuf->space);
         // TODO: use parse_propfind_response()
@@ -335,6 +353,8 @@
         }
         
         set_davprops(res);
+    } else {
+        session_set_error(sn, ret, status);
     }
     return 0;
 }
@@ -364,7 +384,7 @@
             data->read = NULL;
             data->length = 0;
         } else {
-            res->session->error = 1;
+            session_set_error(sn, ret, status);
             return 1;
         }
     }
@@ -384,11 +404,11 @@
             data->set = NULL;
             data->remove = NULL;
         } else {
-            res->session->error = 1;
+            session_set_error(sn, ret, status);
             return 1;
         }
     }
-    
+    sn->error = DAV_OK;
     return 0;
 }
 
@@ -465,8 +485,10 @@
     int status = 0;
     curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
     if(ret == CURLE_OK && (status >= 200 && status < 300)) {
+        res->session->error = DAV_OK;
         return 0;
     } else {
+        session_set_error(res->session, ret, status);
         return 1;
     }
 }
--- a/dav/webdav.h	Tue Aug 13 12:44:23 2013 +0200
+++ b/dav/webdav.h	Tue Aug 13 13:51:00 2013 +0200
@@ -52,6 +52,16 @@
 typedef size_t(*dav_read_func)(void*, size_t, size_t, void*);
 typedef size_t(*dav_write_func)(const void*, size_t, size_t, void*);
 
+enum DavError {
+    DAV_OK = 0,
+    DAV_ERROR,
+    DAV_NOT_FOUND,
+    DAV_UNAUTHORIZED,
+    DAV_FORBIDDEN
+};
+
+typedef enum DavError DavError;
+
 struct DavNamespace {
     char *prefix;
     char *name;
@@ -80,7 +90,7 @@
     char          *base_url;
     UcxMempool    *mp;
     UcxAllocator  *allocator;
-    int           error;
+    DavError      error;
 };
 
 struct DavContext {
@@ -130,6 +140,8 @@
 DavSession* dav_session_new_auth(DavContext *context, char *base_url, char *user, char *password);
 void dav_session_set_auth(DavSession *sn, char *user, char *password);
 
+void session_set_error(DavSession *sn, CURLcode c, int status);
+
 DavResource* dav_get(DavSession *sn, char *path, char *properties);
 
 UcxList* parse_properties_string(DavContext *context, sstr_t str);

mercurial