diff -r 49f3c58cc06c -r 571b62700df6 dav/main.c --- a/dav/main.c Fri Dec 02 13:24:01 2016 +0100 +++ b/dav/main.c Fri Dec 09 10:55:18 2016 +0100 @@ -231,8 +231,14 @@ fflush(stderr); user = fgets(ubuf, 256, stdin); } + if(!user) { + return 0; + } char *password = util_password_input("Password: "); + if(!password || strlen(password) == 0) { + return 0; + } size_t ulen = strlen(user); if(user[ulen-1] == '\n') { @@ -1109,7 +1115,8 @@ char *url = a->argv[0]; char *path = NULL; - DavSession *sn = connect_to_repo(url2repo(url, &path), a); + Repository *repo = url2repo(url, &path); + DavSession *sn = connect_to_repo(repo, a); if(set_session_config(sn, a)) { return -1; @@ -1119,9 +1126,14 @@ char *property = a->argv[1]; DavResource *res = dav_resource_new(sn, path); - if(dav_load(res)) { - print_resource_error(sn, res->path); - return -1; + for(int i=0;i<2;i++) { + if(dav_load(res)) { + if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { + continue; + } + print_resource_error(sn, res->path); + return -1; + } } char *value = namespace ? @@ -1153,7 +1165,8 @@ char *url = a->argv[0]; char *path = NULL; - DavSession *sn = connect_to_repo(url2repo(url, &path), a); + Repository *repo = url2repo(url, &path); + DavSession *sn = connect_to_repo(repo, a); if(set_session_config(sn, a)) { return -1; @@ -1161,9 +1174,14 @@ set_session_lock(sn, a); DavResource *res = dav_resource_new(sn, path); - if(!dav_exists(res)) { - print_resource_error(sn, res->path); - return -1; + for(int i=0;i<2;i++) { + if(!dav_exists(res)) { + if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { + continue; + } + print_resource_error(sn, res->path); + return -1; + } } char *namespace = cmd_getoption(a, "namespace"); @@ -1176,14 +1194,21 @@ dav_set_property(res, property, value); } - if(dav_store(res)) { - print_resource_error(sn, res->path); - fprintf(stderr, "Cannot set property.\n"); - return -1; + int ret = 0; + for(int i=0;i<2;i++) { + if(dav_store(res)) { + if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { + continue; + } + print_resource_error(sn, res->path); + fprintf(stderr, "Cannot set property.\n"); + ret = -1; + break; + } } free(path); - return 0; + return ret; } int cmd_lock(CmdArgs *a) { @@ -1194,7 +1219,8 @@ char *url = a->argv[0]; char *path = NULL; - DavSession *sn = connect_to_repo(url2repo(url, &path), a); + Repository *repo = url2repo(url, &path); + DavSession *sn = connect_to_repo(repo, a); ucx_mempool_reg_destr(sn->mp, path, free); if(set_session_config(sn, a)) { @@ -1202,9 +1228,14 @@ } DavResource *res = dav_resource_new(sn, path); - if(dav_lock(res)) { - print_resource_error(sn, res->path); - return -1; + for(int i=0;i<2;i++) { + if(dav_lock(res)) { + if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { + continue; + } + print_resource_error(sn, res->path); + return -1; + } } DavLock *lock = dav_get_lock(sn, res->path); @@ -1248,6 +1279,7 @@ char *url = a->argv[0]; char *path = NULL; + Repository *repo = url2repo(url, &path); DavSession *sn = connect_to_repo(url2repo(url, &path), a); ucx_mempool_reg_destr(sn->mp, path, free); if(set_session_config(sn, a)) { @@ -1269,14 +1301,21 @@ free(locktoken); } + int ret = 0; DavResource *res = dav_resource_new(sn, path); - if(dav_unlock(res)) { - print_resource_error(sn, res->path); - return -1; + for(int i=0;i<2;i++) { + if(dav_unlock(res)) { + if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { + continue; + } + print_resource_error(sn, res->path); + ret = -1; + break; + } } dav_session_destroy(sn); - return 0; + return ret; } static int count_children(DavResource *res) { @@ -1297,55 +1336,62 @@ char *url = a->argv[0]; char *path = NULL; - DavSession *sn = connect_to_repo(url2repo(url, &path), a); + Repository *repo = url2repo(url, &path); + DavSession *sn = connect_to_repo(repo, a); if(set_session_config(sn, a)) { return -1; } DavResource *res = dav_resource_new(sn, path); - if(!dav_load(res)) { - printf("name: %s\n", res->name); - printf("path: %s\n", res->path); - - char *server = util_url_base(sn->base_url); - char *url = util_concat_path(server, res->href); - printf("url: %s\n", url); - free(url); - free(server); - - if(res->iscollection) { - printf("type: collection\n"); - printf("size: %d\n", count_children(res)); + for(int i=0;i<2;i++) { + if(!dav_load(res)) { + printf("name: %s\n", res->name); + printf("path: %s\n", res->path); + + char *server = util_url_base(sn->base_url); + char *url = util_concat_path(server, res->href); + printf("url: %s\n", url); + free(url); + free(server); + + if(res->iscollection) { + printf("type: collection\n"); + printf("size: %d\n", count_children(res)); + } else { + printf("type: resource\n"); + char *len = ls_size_str(res); + printf("size: %s\n", len); + free(len); + } + + size_t count = 0; + DavPropName *properties = dav_get_property_names(res, &count); + + char *last_ns = NULL; + for(int i=0;ierror == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) { + continue; + } + print_resource_error(sn, res->path); + break; } - - size_t count = 0; - DavPropName *properties = dav_get_property_names(res, &count); - - char *last_ns = NULL; - for(int i=0;ipath); - return -1; } + return -1; }