Sat, 23 Sep 2017 11:45:03 +0200
adds lock timeout
dav/main.c | file | annotate | diff | comparison | revisions | |
dav/optparser.c | file | annotate | diff | comparison | revisions | |
libidav/methods.c | file | annotate | diff | comparison | revisions | |
libidav/methods.h | file | annotate | diff | comparison | revisions | |
libidav/resource.c | file | annotate | diff | comparison | revisions | |
libidav/webdav.h | file | annotate | diff | comparison | revisions |
--- a/dav/main.c Fri Sep 22 20:42:33 2017 +0200 +++ b/dav/main.c Sat Sep 23 11:45:03 2017 +0200 @@ -176,7 +176,7 @@ fprintf( stderr, " remove-property [-pc] [-n <uri>] <url> <property>\n"); - fprintf(stderr, " lock [-pc] <url>\n"); + fprintf(stderr, " lock [-pc] [-T timeout] <url>\n"); fprintf(stderr, " unlock [-pc] [-L <lock>] <url>\n"); fprintf(stderr, " info [-pc] <url>\n"); fprintf(stderr, " date [url]\n"); @@ -201,6 +201,7 @@ fprintf(stderr, " -e show extended flags\n"); fprintf(stderr, " -O override resources\n"); fprintf(stderr, " -L <lock> specificy lock token\n"); + fprintf(stderr, " -T <sec> timeout in seconds\n"); fprintf(stderr, " -n <uri> specify namespace uri\n"); fprintf(stderr, " -N disable authentication prompt (all commands)\n"); fprintf(stderr, " -i disable cert verification (all commands)\n"); @@ -1322,9 +1323,19 @@ return -1; } + time_t timeout = 0; + char *timeoutstr = cmd_getoption(a, "timeout"); + if(timeoutstr) { + if(!sstrcasecmp(sstr(timeoutstr), S("infinity"))) { + timeout = -1; + } else { + timeout = (time_t)atoi(timeoutstr); // TODO: use strtol + } + } + DavResource *res = dav_resource_new(sn, path); for(int i=0;i<2;i++) { - if(!dav_lock(res)) { + if(!dav_lock_t(res, timeout)) { break; } if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
--- a/dav/optparser.c Fri Sep 22 20:42:33 2017 +0200 +++ b/dav/optparser.c Sat Sep 23 11:45:03 2017 +0200 @@ -184,6 +184,20 @@ } break; } + case 'T': { + if(!option) { + option = "timeout"; + optchar = 'T'; + } else { + fprintf( + stderr, + "Missing argument for option -%c\n", + optchar); + cmd_args_free(a); + return NULL; + } + break; + } } } } else if(option) {
--- a/libidav/methods.c Fri Sep 22 20:42:33 2017 +0200 +++ b/libidav/methods.c Sat Sep 23 11:45:03 2017 +0200 @@ -111,7 +111,7 @@ return ret; } -UcxBuffer* create_allprop_propfind_request() { +UcxBuffer* create_allprop_propfind_request(void) { UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOFREE); sstr_t s; @@ -247,7 +247,7 @@ return buf; } -UcxBuffer* create_basic_propfind_request() { +UcxBuffer* create_basic_propfind_request(void) { UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND); sstr_t s; @@ -1077,7 +1077,7 @@ } -UcxBuffer* create_lock_request() { +UcxBuffer* create_lock_request(void) { UcxBuffer *buf = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND); sstr_t s; @@ -1156,13 +1156,24 @@ return ret; } -CURLcode do_lock_request(CURL *handle, UcxBuffer *request, UcxBuffer *response) { +CURLcode do_lock_request(CURL *handle, UcxBuffer *request, UcxBuffer *response, time_t timeout) { curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "LOCK"); curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L); request->pos = 0; - // clear headers + // clear headers struct curl_slist *headers = NULL; + + if(timeout != 0) { + sstr_t thdr; + if(timeout < 0) { + thdr = ucx_sprintf("%s", "Timeout: Infinite"); + } else { + thdr = ucx_sprintf("Timeout: Second-%u", (unsigned int)timeout); + } + headers = curl_slist_append(headers, thdr.ptr); + free(thdr.ptr); + } curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(handle, CURLOPT_UPLOAD, 1); @@ -1175,6 +1186,10 @@ CURLcode ret = curl_easy_perform(handle); + if(headers) { + curl_slist_free_all(headers); + } + return ret; }
--- a/libidav/methods.h Fri Sep 22 20:42:33 2017 +0200 +++ b/libidav/methods.h Sat Sep 23 11:45:03 2017 +0200 @@ -79,9 +79,9 @@ dav_read_func read_func, size_t length); -UcxBuffer* create_allprop_propfind_request(); +UcxBuffer* create_allprop_propfind_request(void); UcxBuffer* create_propfind_request(DavSession *sn, UcxList *properties); -UcxBuffer* create_basic_propfind_request(); +UcxBuffer* create_basic_propfind_request(void); PropfindParser* create_propfind_parser(UcxBuffer *response, char *url); void destroy_propfind_parser(PropfindParser *parser); @@ -113,9 +113,9 @@ CURLcode do_copy_move_request(CURL *handle, char *dest, char *lock, DavBool copy, DavBool override); -UcxBuffer* create_lock_request(); +UcxBuffer* create_lock_request(void); int parse_lock_response(DavSession *sn, UcxBuffer *response, LockDiscovery *lock); -CURLcode do_lock_request(CURL *handle, UcxBuffer *request, UcxBuffer *response); +CURLcode do_lock_request(CURL *handle, UcxBuffer *request, UcxBuffer *response, time_t timeout); CURLcode do_unlock_request(CURL *handle, char *locktoken); #ifdef __cplusplus
--- a/libidav/resource.c Fri Sep 22 20:42:33 2017 +0200 +++ b/libidav/resource.c Sat Sep 23 11:45:03 2017 +0200 @@ -998,13 +998,17 @@ } int dav_lock(DavResource *res) { + return dav_lock_t(res, 0); +} + +int dav_lock_t(DavResource *res, time_t timeout) { DavSession *sn = res->session; CURL *handle = sn->handle; util_set_url(sn, dav_resource_get_href(res)); UcxBuffer *request = create_lock_request(); UcxBuffer *response = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND); - CURLcode ret = do_lock_request(handle, request, response); + CURLcode ret = do_lock_request(handle, request, response, timeout); //printf("\nlock\n"); //printf("%.*s\n\n", request->size, request->space);
--- a/libidav/webdav.h Fri Sep 22 20:42:33 2017 +0200 +++ b/libidav/webdav.h Sat Sep 23 11:45:03 2017 +0200 @@ -242,6 +242,7 @@ int dav_moveto(DavResource *res, char *url, DavBool override); int dav_lock(DavResource *res); +int dav_lock_t(DavResource *res, time_t timeout); int dav_unlock(DavResource *res); char* dav_get_property(DavResource *res, char *name);