add existing code (build system, libs, initial mizucp code)
[mizunara.git] / libidav / versioning.c
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2018 Olaf Wintermann. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "versioning.h"
34
35 #include "methods.h"
36 #include "utils.h"
37 #include "session.h"
38
39 static int basic_deltav_op(DavResource *res, char *method) {
40     DavSession *sn = res->session;
41     CURL *handle = sn->handle;
42     util_set_url(sn, dav_resource_get_href(res));
43     
44     DavLock *lock = dav_get_lock(res->session, res->path);
45     char *locktoken = lock ? lock->token : NULL;
46     
47     CURLcode ret = do_simple_request(sn, method, locktoken);
48     long status = 0;
49     curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
50     if(!(ret == CURLE_OK && (status >= 200 && status < 300))) {
51         dav_session_set_error(sn, ret, status);
52         return 1;
53     }
54     return 0;
55 }
56
57 int dav_versioncontrol(DavResource *res) {
58     return basic_deltav_op(res, "VERSION-CONTROL");
59 }
60
61 int dav_checkout(DavResource *res) {
62     return basic_deltav_op(res, "CHECKOUT");
63 }
64
65 int dav_checkin(DavResource *res) {
66     return basic_deltav_op(res, "CHECKIN");
67 }
68
69 int dav_uncheckout(DavResource *res) {
70     return basic_deltav_op(res, "UNCHECKOUT");
71 }
72
73 DavResource* dav_versiontree(DavResource *res, char *properties) {
74     DavSession *sn = res->session;
75     util_set_url(sn, dav_resource_get_href(res));
76     
77     UcxList *proplist = NULL;
78     if(properties) {
79         proplist = parse_properties_string(sn->context, sstr(properties));
80     }
81     
82     // check if the list already contains a D:version-name property
83     int add_vname = 1;
84     UCX_FOREACH(elm, proplist) {
85         DavProperty *p = elm->data;
86         if(!strcmp(p->ns->name, "DAV:") && !strcmp(p->name, "version-name")) {
87             add_vname = 0;
88             break;
89         }
90     }
91     if(add_vname) {
92         // we need at least the D:version-name prop
93         DavProperty *p = malloc(sizeof(DavProperty));
94         p->ns = dav_get_namespace(sn->context, "D");
95         p->name = strdup("version-name");
96         p->value = NULL;
97         proplist = ucx_list_prepend(proplist, p);
98     }
99     
100     // create a version-tree request, which is almost the same as propfind
101     UcxBuffer *rqbuf = create_propfind_request(sn, proplist, "version-tree", 1);
102     UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
103     
104     // do the request
105     CURLcode ret = do_report_request(sn, rqbuf, rpbuf);
106     long status = 0;
107     curl_easy_getinfo (sn->handle, CURLINFO_RESPONSE_CODE, &status);
108     int error = 0;
109     DavResource *versions = NULL;
110     if(ret == CURLE_OK && status == 207) {
111         sn->error = DAV_OK;
112         
113         // parse multistatus response
114         PropfindParser *parser = create_propfind_parser(rpbuf, NULL);
115         if(parser) {
116             DavResource *list_end = NULL;
117             
118             ResponseTag response;
119             int r;
120             
121             // we don't want name decryption for version resources
122             int snflags = sn->flags;
123             sn->flags = 0;
124             while((r = get_propfind_response(parser, &response)) != 0) {
125                 if(r == -1) {
126                     res->session->error = DAV_ERROR;
127                     error = 1;
128                     break;
129                 }
130                 DavResource *v = response2resource(sn, &response, NULL);
131                 // add version to list
132                 if(!versions) {
133                     versions = v;
134                 } else {
135                     list_end->next = v;
136                 }
137                 list_end = v;
138                 
139                 cleanup_response(&response);
140             }
141             sn->flags = snflags;
142             
143             destroy_propfind_parser(parser);
144         } else {
145             sn->error = DAV_ERROR;
146             error = 1;
147         }
148     } else {
149         dav_session_set_error(sn, ret, status);
150         error = 1;
151     }
152     
153     // cleanup
154     while(proplist) {
155         DavProperty *p = proplist->data;
156         free(p->name);
157         free(p);
158         UcxList *next = proplist->next;
159         free(proplist);
160         proplist = next;
161     }
162     if(error && versions) {
163         DavResource *cur = versions;
164         while(cur) {
165             DavResource *next = cur->next;
166             dav_resource_free(cur);
167             cur = next;
168         }
169         versions = NULL;
170     }
171     
172     return versions;
173 }