1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 CxList *proplist =
NULL;
78 if(properties) {
79 proplist = parse_properties_string(sn->context, cx_str(properties));
80
81
82 int add_vname =
1;
83 CxIterator i = cxListIterator(proplist);
84 cx_foreach(DavProperty *, p, i) {
85 if(!strcmp(p->ns->name,
"DAV:") && !strcmp(p->name,
"version-name")) {
86 add_vname =
0;
87 break;
88 }
89 }
90 if(add_vname) {
91
92 DavProperty p;
93 p.ns = dav_get_namespace(sn->context,
"D");
94 p.name = strdup(
"version-name");
95 p.value =
NULL;
96 cxListInsert(proplist,
0, &p);
97 }
98 }
99
100
101
102
103 CxBuffer *rqbuf = create_propfind_request(sn, proplist,
"version-tree",
1);
104 CxBuffer *rpbuf = cxBufferCreate(
NULL,
4096, cxDefaultAllocator,
CX_BUFFER_FREE_CONTENTS|
CX_BUFFER_AUTO_EXTEND);
105
106
107 CURLcode ret = do_report_request(sn, rqbuf, rpbuf);
108 long status =
0;
109 curl_easy_getinfo (sn->handle,
CURLINFO_RESPONSE_CODE, &status);
110 int error =
0;
111 DavResource *versions =
NULL;
112 if(ret ==
CURLE_OK && status ==
207) {
113 sn->error =
DAV_OK;
114
115
116 PropfindParser *parser = create_propfind_parser(rpbuf,
NULL);
117 if(parser) {
118 DavResource *list_end =
NULL;
119
120 ResponseTag response;
121 int r;
122
123
124 int snflags = sn->flags;
125 sn->flags =
0;
126 while((r = get_propfind_response(parser, &response)) !=
0) {
127 if(r == -
1) {
128 res->session->error =
DAV_ERROR;
129 error =
1;
130 break;
131 }
132 DavResource *v = response2resource(sn, &response,
NULL);
133
134 if(!versions) {
135 versions = v;
136 }
else {
137 list_end->next = v;
138 }
139 list_end = v;
140
141 cleanup_response(&response);
142 }
143 sn->flags = snflags;
144
145 destroy_propfind_parser(parser);
146 }
else {
147 sn->error =
DAV_ERROR;
148 error =
1;
149 }
150 }
else {
151 dav_session_set_error(sn, ret, status);
152 error =
1;
153 }
154
155
156 if(proplist) {
157 CxIterator i = cxListIterator(proplist);
158 cx_foreach(DavProperty*, p, i) {
159 free(p->name);
160 }
161 cxListDestroy(proplist);
162 }
163
164 if(error && versions) {
165 DavResource *cur = versions;
166 while(cur) {
167 DavResource *next = cur->next;
168 dav_resource_free(cur);
169 cur = next;
170 }
171 versions =
NULL;
172 }
173
174 return versions;
175 }
176