dav/main.c

changeset 795
05647e862a17
parent 789
378b5ab86f77
child 796
81e0f67386a6
--- a/dav/main.c	Sun Sep 17 13:51:01 2023 +0200
+++ b/dav/main.c	Sat Sep 30 16:33:47 2023 +0200
@@ -424,7 +424,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -603,7 +603,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -850,7 +850,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -1038,7 +1038,7 @@
 #endif
 }
 
-int get_resource(Repository *repo, GetResource *getres, CmdArgs *a, void *unused) {
+int get_resource(DavCfgRepository *repo, GetResource *getres, CmdArgs *a, void *unused) {
     DavResource *res = getres->res;
     char *out = getres->path;
     
@@ -1096,7 +1096,7 @@
 #define DEFAULT_DIR_MODE T_IRUSR | T_IWUSR | T_IXUSR | T_IRGRP | T_IXGRP | T_IROTH | T_IXOTH
 #define DEFAULT_FILE_MODE T_IRUSR | T_IWUSR | T_IRGRP | T_IROTH
 
-int resource2tar(Repository *repo, GetResource *res, CmdArgs *a, TarOutputStream *tar) { 
+int resource2tar(DavCfgRepository *repo, GetResource *res, CmdArgs *a, TarOutputStream *tar) { 
     DavResource *d = res->res;
     
     if(d->iscollection) {
@@ -1148,7 +1148,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -1221,7 +1221,7 @@
 
 
 int put_entry(
-        Repository *repo,
+        DavCfgRepository *repo,
         CmdArgs *a,
         DavSession *sn,
         char *path,
@@ -1319,7 +1319,7 @@
     return ret;
 }
 
-int put_tar(Repository *repo, CmdArgs *a, DavSession *sn, char *tarfile, char *path) {
+int put_tar(DavCfgRepository *repo, CmdArgs *a, DavSession *sn, char *tarfile, char *path) {
     int isstdin = !strcmp(tarfile, "-");
     FILE *in = isstdin ? stdin : fopen(tarfile, "rb");
     if(!in) {
@@ -1396,7 +1396,7 @@
 }
 
 int put_file(
-        Repository *repo,
+        DavCfgRepository *repo,
         CmdArgs *a,
         DavSession *sn,
         const char *path,
@@ -1496,7 +1496,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     int exit_code = -1;
@@ -1564,7 +1564,7 @@
     
     char *srcurl = a->argv[0];
     char *srcpath = NULL;
-    Repository *srcrepo = url2repo(srcurl, &srcpath);
+    DavCfgRepository *srcrepo = dav_config_url2repo(get_config(), srcurl, &srcpath);
     
     DavSession *srcsn = connect_to_repo(ctx, srcrepo, srcpath, request_auth, a);
     if(set_session_config(srcsn, a)) {
@@ -1576,7 +1576,7 @@
     
     char *desturl = a->argv[1];
     char *destpath = NULL;
-    Repository *destrepo = url2repo(desturl, &destpath);
+    DavCfgRepository *destrepo = dav_config_url2repo(get_config(), desturl, &destpath);
     
     if(srcrepo == destrepo) {
         DavResource *res = dav_resource_new(srcsn, srcpath);
@@ -1588,8 +1588,8 @@
             return -1;
         }
     } else {
-        char *srchost = util_url_base(srcrepo->url);
-        char *desthost = util_url_base(destrepo->url);     
+        char *srchost = util_url_base(srcrepo->url.value.ptr);
+        char *desthost = util_url_base(destrepo->url.value.ptr);     
         if(!strcmp(srchost, desthost)) {
             DavSession *destsn = connect_to_repo(ctx, destrepo, destpath, request_auth, a);
             if(set_session_config(destsn, a)) {
@@ -1642,7 +1642,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -1734,7 +1734,7 @@
     } else if (a->argc == 1) {
         char *url = a->argv[0];
         char *path = NULL;
-        Repository *repo = url2repo(url, &path);
+        DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
         DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
 
         DavResource *res = dav_resource_new(sn, path);
@@ -1765,7 +1765,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -1839,7 +1839,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -1902,7 +1902,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -1943,7 +1943,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     cxMempoolRegister(sn->mp, path, free);
     
@@ -2016,7 +2016,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     cxMempoolRegister(sn->mp, path, free);
     if(set_session_config(sn, a)) {
@@ -2076,7 +2076,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -2172,7 +2172,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -2199,7 +2199,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -2226,7 +2226,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -2252,7 +2252,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -2279,7 +2279,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    Repository *repo = url2repo(url, &path);
+    DavCfgRepository *repo = dav_config_url2repo(get_config(), url, &path);
     DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, a);
     
     if(set_session_config(sn, a)) {
@@ -2442,15 +2442,18 @@
     }
     printf("\n");
     
-    Repository repo;
-    memset(&repo, 0, sizeof(Repository));
-    repo.name = name;
-    repo.url = url;
-    repo.user = user;
-    repo.password = password;
+    DavConfig *config = get_config();
+    const CxAllocator *a = config->mp->allocator;
+    DavCfgRepository *repo = dav_repository_new(config);
+    
+    repo->name.value = cx_strdup_a(a, cx_str(name));
+    repo->url.value = cx_strdup_a(a, cx_str(url));
+    dav_repository_set_auth(config, repo, cx_str(user), cx_str(password));
+    
+    dav_config_add_repository(config, repo);
     
     int ret = 0;
-    if(add_repository(&repo)) {
+    if(store_config()) {
         fprintf(stderr, "Cannot write config.xml\n");
         ret = -1;
     } else {
@@ -2476,14 +2479,15 @@
         return -1;
     }
     
+    DavConfig *config = get_config();
+    
+    DavBool store = FALSE;
     for(int i = 0 ; i < args->argc ; i++) {
         cxstring reponame = cx_str(args->argv[i]);
-        Repository* repo = get_repository(reponame);
+        DavCfgRepository* repo = dav_config_get_repository(config, reponame);
         if(repo) {
-            if(remove_repository(repo)) {
-                fprintf(stderr, "Cannot write config.xml\n");
-                return -1;
-            }
+            dav_repository_remove_and_free(config, repo);
+            store = TRUE;
         } else {
             fprintf(stderr, "Repository %s does not exist - skipped.\n",
                     reponame.ptr);
@@ -2491,7 +2495,11 @@
         }
     }
     
-    return -1;
+    if(store) {
+        return store_config();
+    } else {
+        return -1;
+    }
 }
 
 int cmd_repository_url(CmdArgs *args) {
@@ -2502,10 +2510,10 @@
     }
     
     cxstring reponame = cx_str(args->argv[0]);
-    Repository* repo = get_repository(reponame);
+    DavCfgRepository* repo = dav_config_get_repository(get_config(), reponame);
     if(repo) {
-        cxstring url = cx_str(repo->url);
-        if(repo->user && !cmd_getoption(args, "plain")) {
+        cxstring url = cx_strcast(repo->url.value);
+        if(repo->user.value.ptr && !cmd_getoption(args, "plain")) {
             int hostindex = 0;
             if(cx_strprefix(url, CX_STR("https://"))) {
                 printf("https://");
@@ -2514,13 +2522,13 @@
                 printf("http://");
                 hostindex = 7;
             }
-            printf("%s", repo->user);
-            if(repo->password) {
+            printf("%.*s", (int)repo->user.value.length, repo->user.value.ptr);
+            if(repo->password.value.ptr) {
                 CURL *curl = curl_easy_init();
                 char *pw = curl_easy_escape(
                         curl,
-                        repo->password,
-                        strlen(repo->password));
+                        repo->password.value.ptr,
+                        repo->password.value.length);
                 printf(":%s", pw);
                 curl_free(pw);
                 curl_easy_cleanup(curl);
@@ -3125,7 +3133,7 @@
         cxMapPut(args->options, cx_hash_key_str("noinput"), "");
         
         char *path = NULL;
-        Repository *repo = url2repo_s(url, &path);
+        DavCfgRepository *repo = dav_config_url2repo(get_config(), url.ptr, &path);
         DavSession *sn = connect_to_repo(ctx, repo, path, request_auth, args);
         if(!sn) {
             return 0;
@@ -3160,7 +3168,7 @@
                 }
                 
                 CxBuffer *out = cxBufferCreate(NULL, 512, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
-                cxBufferPutString(out, repo->name);
+                cxBufferWrite(repo->name.value.ptr, repo->name.value.length, 1, out);
                 if(space) {
                     size_t l = strlen(elm->path);
                     for(int i=0;i<l;i++) {

mercurial