Sun, 20 Mar 2016 11:27:31 +0100
improved error handling in libidav
dav/scfg.c | file | annotate | diff | comparison | revisions | |
dav/sync.c | file | annotate | diff | comparison | revisions | |
libidav/methods.c | file | annotate | diff | comparison | revisions | |
libidav/resource.c | file | annotate | diff | comparison | revisions | |
libidav/session.c | file | annotate | diff | comparison | revisions | |
libidav/session.h | file | annotate | diff | comparison | revisions | |
libidav/utils.c | file | annotate | diff | comparison | revisions | |
libidav/webdav.h | file | annotate | diff | comparison | revisions |
--- a/dav/scfg.c Sat Mar 19 18:49:36 2016 +0100 +++ b/dav/scfg.c Sun Mar 20 11:27:31 2016 +0100 @@ -331,7 +331,6 @@ void free_sync_config() { - printf("free_sync_config\n"); UcxMapIterator i = ucx_map_iterator(directories); SyncDirectory *dir; UCX_MAP_FOREACH(elm, dir, i) {
--- a/dav/sync.c Sat Mar 19 18:49:36 2016 +0100 +++ b/dav/sync.c Sun Mar 20 11:27:31 2016 +0100 @@ -112,6 +112,8 @@ ret = cmd_add_directory(args); } else if(!strcmp(cmd, "list-directories")) { ret = list_syncdirs(); + } else { + print_usage(argv[0]); } // cleanup
--- a/libidav/methods.c Sat Mar 19 18:49:36 2016 +0100 +++ b/libidav/methods.c Sun Mar 20 11:27:31 2016 +0100 @@ -458,7 +458,7 @@ char *url = NULL; curl_easy_getinfo(sn->handle, CURLINFO_EFFECTIVE_URL, &url); if(!root) { - printf("method.c: TODO: remove\n"); + printf("methods.c: TODO: remove\n"); root = dav_resource_new_href(sn, util_url_path(url)); // TODO: remove } @@ -491,14 +491,14 @@ DavKey *key = NULL; if(DAV_DECRYPT_NAME(sn) && response->crypto_name && (key = dav_context_get_key(sn->context, response->crypto_key))) { if(!response->crypto_key) { - // TODO: error - fprintf(stderr, "encrypted resource without key\n"); + sn->error = DAV_ERROR; + dav_session_set_errstr(sn, "Missing crypto-key property"); return NULL; } name = util_decrypt_str_k(sn, response->crypto_name, key); if(!name) { - // TODO: error - fprintf(stderr, "decrypted name is null\n"); + sn->error = DAV_ERROR; + dav_session_set_errstr(sn, "Cannot decrypt resource name"); return NULL; } } else { @@ -640,13 +640,15 @@ char *name = NULL; if(DAV_DECRYPT_NAME(sn) && crypto_name) { if(!crypto_key) { - // TODO: error - fprintf(stderr, "encrypted resource without key\n"); + sn->error = DAV_ERROR; + dav_session_set_errstr(sn, "Missing crypto-key property"); + return -1; } name = util_decrypt_str(sn, crypto_name, crypto_key); if(!name) { - // TODO: error - fprintf(stderr, "decrypted name is null\n"); + sn->error = DAV_ERROR; + dav_session_set_errstr(sn, "Cannot decrypt resource name"); + return -1; } } else { sstr_t resname = sstr(util_resource_name(href));
--- a/libidav/resource.c Sat Mar 19 18:49:36 2016 +0100 +++ b/libidav/resource.c Sun Mar 20 11:27:31 2016 +0100 @@ -746,7 +746,7 @@ DavResource* dav_create_child(DavResource *parent, char *name) { DavResource *res = dav_resource_new_child(parent->session, parent, name); if(dav_create(res)) { - // TODO: free resource + dav_resource_free(res); return NULL; } else { return res; @@ -808,7 +808,8 @@ name[len - 1] = '\0'; } if(resource_add_crypto_info(sn, h, name, NULL)) { - // TODO: error + sn->error = DAV_ERROR; + dav_session_set_errstr(sn, "Cannot set crypto properties for ancestor"); } break; } else if(status == 405) { @@ -852,13 +853,15 @@ if(code == CURLE_OK && (s >= 200 && s < 300)) { sn->error = DAV_OK; // if the session has encrypted file names, add crypto infos - resource_add_crypto_info(sn, res->href, res->name, NULL); // TODO: check return type - - // do a minimal propfind request - UcxBuffer *rqbuf = create_propfind_request(sn, NULL); - int ret = dav_propfind(sn, res, rqbuf); - ucx_buffer_free(rqbuf); - return ret; + if(!resource_add_crypto_info(sn, res->href, res->name, NULL)) { + // do a minimal propfind request + UcxBuffer *rqbuf = create_propfind_request(sn, NULL); + int ret = dav_propfind(sn, res, rqbuf); + ucx_buffer_free(rqbuf); + return ret; + } else { + return 1; + } } else { dav_session_set_error(sn, code, s); return 1;
--- a/libidav/session.c Sat Mar 19 18:49:36 2016 +0100 +++ b/libidav/session.c Sun Mar 20 11:27:31 2016 +0100 @@ -178,12 +178,23 @@ } } if(c != CURLE_OK) { - sn->errorstr = curl_easy_strerror(c); + dav_session_set_errstr(sn, curl_easy_strerror(c)); } else { - sn->errorstr = NULL; + dav_session_set_errstr(sn, NULL); } } +void dav_session_set_errstr(DavSession *sn, const char *str) { + if(sn->errorstr) { + dav_session_free(sn, sn->errorstr); + } + char *errstr = NULL; + if(str) { + errstr = dav_session_strdup(sn, str); + } + sn->errorstr = errstr; +} + void dav_session_destroy(DavSession *sn) { // remove session from context UcxList *sessions = sn->context->sessions; @@ -217,8 +228,8 @@ ucx_mempool_free(sn->mp, ptr); } -char* dav_session_strdup(DavSession *sn, char *str) { - return sstrdup_a(sn->mp->allocator, sstr(str)).ptr; +char* dav_session_strdup(DavSession *sn, const char *str) { + return sstrdup_a(sn->mp->allocator, sstr((char*)str)).ptr; }
--- a/libidav/session.h Sat Mar 19 18:49:36 2016 +0100 +++ b/libidav/session.h Sun Mar 20 11:27:31 2016 +0100 @@ -79,6 +79,7 @@ } DavLockManager; void dav_session_set_error(DavSession *sn, CURLcode c, int status); +void dav_session_set_errstr(DavSession *sn, const char *str); char* dav_session_create_plain_href(DavSession *sn, char *path);
--- a/libidav/utils.c Sat Mar 19 18:49:36 2016 +0100 +++ b/libidav/utils.c Sun Mar 20 11:27:31 2016 +0100 @@ -52,9 +52,10 @@ #include <openssl/buffer.h> #include <openssl/rand.h> +#include "webdav.h" #include "utils.h" #include "crypto.h" -#include "webdav.h" +#include "session.h" static size_t extractval(sstr_t str, char *result, char delim) { size_t n = 0; @@ -505,7 +506,10 @@ char* util_encrypt_str(DavSession *sn, char *str, char *key) { DavKey *k = dav_context_get_key(sn->context, key); if(!k) { - // TODO: session error + sn->error = DAV_ERROR; + sstr_t err = ucx_sprintf("Key %s not found", key); + dav_session_set_errstr(sn, err.ptr); + free(err.ptr); return NULL; } @@ -522,7 +526,10 @@ char* util_decrypt_str(DavSession *sn, char *str, char *key) { DavKey *k = dav_context_get_key(sn->context, key); if(!k) { - // TODO: session error + sn->error = DAV_ERROR; + sstr_t err = ucx_sprintf("Key %s not found", key); + dav_session_set_errstr(sn, err.ptr); + free(err.ptr); return NULL; }
--- a/libidav/webdav.h Sat Mar 19 18:49:36 2016 +0100 +++ b/libidav/webdav.h Sun Mar 20 11:27:31 2016 +0100 @@ -126,7 +126,7 @@ void *locks; uint32_t flags; DavError error; - const char *errorstr; + char *errorstr; }; struct DavContext { @@ -196,7 +196,7 @@ void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size); void* dav_session_realloc(DavSession *sn, void *ptr, size_t size); void dav_session_free(DavSession *sn, void *ptr); -char* dav_session_strdup(DavSession *sn, char *str); +char* dav_session_strdup(DavSession *sn, const char *str); DavResource* dav_get(DavSession *sn, char *path, char *properties);