src/server/webdav/operation.c

branch
webdav
changeset 217
8ed14d76db42
child 218
2ba512b284b9
equal deleted inserted replaced
216:ce2866ec97f6 217:8ed14d76db42
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2019 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
32 #include <ucx/list.h>
33
34 #include "../daemon/session.h"
35
36 #include "operation.h"
37
38 #define WEBDAV_PATH_MAX 8192
39
40 WebdavOperation* webdav_operation_create(
41 pool_handle_t *pool,
42 WebdavBackend *dav,
43 UcxList *requests,
44 WebdavResponse *response)
45 {
46 WebdavOperation *op = pool_malloc(pool, sizeof(WebdavOperation));
47 ZERO(op, sizeof(WebdavOperation));
48 op->dav = dav;
49 op->requests = requests;
50 op->response = response;
51
52 response->op = op;
53
54 return op;
55 }
56
57 int webdav_op_propfind_begin(
58 WebdavOperation *op,
59 const char *path,
60 VFS_DIR parent,
61 struct stat *s)
62 {
63 // create WebdavResource object for requested resource
64 WebdavResource *resource = op->response->addresource(op->response, path);
65 if(!resource) {
66 return REQ_ABORTED;
67 }
68
69 // store data that we need when the resource will be closed
70 op->stat = s;
71 op->parent = parent;
72
73 // get first propfind object
74 WebdavPropfindRequest *propfind = op->requests->data;
75
76 // execute propfind_do of the first backend for the first resource
77 if(op->dav->propfind_do(propfind, op->response, NULL, resource, s)) {
78 return REQ_ABORTED;
79 }
80
81 return REQ_PROCEED;
82 }
83
84 int webdav_op_propfind_children(
85 WebdavOperation *op,
86 VFSContext *vfs,
87 char *path)
88 {
89 WebdavResponse *response = op->response;
90 WebdavPropfindRequest *request = op->requests->data;
91
92 UcxAllocator *a = session_get_allocator(request->sn);
93 pool_handle_t *pool = request->sn->pool;
94 UcxList *stack = ucx_list_prepend_a(a, NULL, path);
95 UcxList *stack_end = stack;
96 if(!stack) {
97 return 1;
98 }
99
100 // reusable buffer for full child path
101 char *newpath = NULL;
102 size_t newpathlen = 0;
103
104 int err = 0;
105 while(stack && !err) {
106 char *cur_path = stack->data;
107 size_t parent_len = strlen(cur_path);
108 if(parent_len > WEBDAV_PATH_MAX) {
109 log_ereport(LOG_FAILURE, "webdav: maximal path length exceeded");
110 err = 1;
111 break;
112 }
113 if(cur_path[parent_len-1] == '/') {
114 parent_len--;
115 }
116 size_t max_child_len = WEBDAV_PATH_MAX - parent_len;
117
118 // when newpath is initialized with the parent path
119 // set path_buf_init to TRUE
120 WSBool path_buf_init = FALSE;
121
122 VFS_DIR dir = vfs_opendir(vfs, path);
123 if(!dir) {
124 log_ereport(
125 LOG_FAILURE,
126 "webdav: propfind: cannot open directory %d",
127 vfs->vfs_errno);
128 err = 1;
129 break;
130 }
131
132 VFS_ENTRY f;
133 while(vfs_readdir_stat(dir, &f)) {
134 if(f.stat_errno != 0) {
135 continue;
136 }
137
138 size_t child_len = strlen(f.name);
139 if(child_len > max_child_len) {
140 log_ereport(LOG_FAILURE, "webdav: maximal path length exceeded");
141 err = 1;
142 break;
143 }
144 size_t childpathlen = parent_len + child_len + 1; // +1 '/'
145 if(childpathlen > newpathlen) {
146 // we're gonna need a bigger boa^H^H^Hbuffer
147 if(newpath) {
148 pool_free(pool, newpath);
149 }
150 newpath = pool_malloc(pool, childpathlen + 1);
151 if(!newpath) {
152 err = 1;
153 break;
154 }
155 newpathlen = childpathlen;
156 path_buf_init = FALSE;
157 }
158 // create full path string for this child
159 if(!path_buf_init) {
160 memcpy(newpath, cur_path, parent_len);
161 newpath[parent_len] = '/';
162 }
163 memcpy(newpath+parent_len+1, f.name, child_len);
164 newpath[childpathlen] = 0;
165
166 // propfind for this child
167 if(webdav_op_propfind_begin(op, newpath, dir, &f.stat)) {
168 err = 1;
169 break;
170 }
171
172 // depth of -1 means infinity
173 if(request->depth == -1 && S_ISDIR(f.stat.st_mode)) {
174 char *pathcp = pool_malloc(pool, childpathlen + 1);
175 memcpy(pathcp, newpath, childpathlen + 1);
176
177 // add the newpath copy to the stack
178 // stack_end is always not NULL here, because we remove
179 // the first stack element at the end of the loop
180 UcxList *newlistelm = ucx_list_append_a(a, stack_end, pathcp);
181 if(!newlistelm) {
182 err = 1;
183 break;
184 }
185 stack_end = newlistelm;
186 }
187 }
188
189 vfs_closedir(dir);
190
191 if(cur_path != path) {
192 pool_free(pool, cur_path);
193 }
194 stack = ucx_list_remove_a(a, stack, stack);
195 }
196
197 // in case of an error, we have to free all remaining stack elements
198 UCX_FOREACH(elm, stack) {
199 char *data = elm->data;
200 if(data != path) {
201 pool_free(pool, data);
202 }
203 }
204
205 return err;
206 }
207
208 int webdav_op_propfiond_close_resource(
209 WebdavOperation *op,
210 WebdavResource *resource)
211 {
212 // start with second backend and request, because
213 // the first one was already called by webdav_op_propfind_begin
214 WebdavBackend *dav = op->dav->next;
215 UcxList *request = op->requests->next;
216
217 // call propfind_do of all remaining backends
218 int ret = REQ_PROCEED;
219 while(dav && request) {
220 if(dav->propfind_do(
221 request->data,
222 op->response,
223 op->parent,
224 resource,
225 op->stat))
226 {
227 ret = REQ_ABORTED;
228 }
229
230 dav = dav->next;
231 request = request->next;
232 }
233 return ret;
234 }
235
236 /*
237 * Executes propfind_finish for each Backend
238 */
239 int webdav_op_propfind_finish(WebdavOperation *op) {
240 WebdavBackend *dav = op->dav;
241 UcxList *requests = op->requests;
242
243 int ret = REQ_PROCEED;
244 while(dav && requests) {
245 if(dav->propfind_finish(requests->data)) {
246 ret = REQ_ABORTED;
247 }
248
249 dav = dav->next;
250 requests = requests->next;
251 }
252 return ret;
253 }

mercurial