]> granicus.if.org Git - apache/blob - modules/dav/fs/repos.c
cf7876534c495ff9ffeacdbbc3b2c12e40210c3f
[apache] / modules / dav / fs / repos.c
1 /* Copyright 2000-2004 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /*
17 ** DAV filesystem-based repository provider
18 */
19
20 #include "apr.h"
21 #include "apr_file_io.h"
22 #include "apr_strings.h"
23 #include "apr_buckets.h"
24
25 #if APR_HAVE_STDIO_H
26 #include <stdio.h>              /* for sprintf() */
27 #endif
28
29 #include "httpd.h"
30 #include "http_log.h"
31 #include "http_protocol.h"      /* for ap_set_* (in dav_fs_set_headers) */
32 #include "http_request.h"       /* for ap_update_mtime() */
33
34 #include "mod_dav.h"
35 #include "repos.h"
36
37
38 /* to assist in debugging mod_dav's GET handling */
39 #define DEBUG_GET_HANDLER       0
40
41 #define DAV_FS_COPY_BLOCKSIZE   16384   /* copy 16k at a time */
42
43 /* context needed to identify a resource */
44 struct dav_resource_private {
45     apr_pool_t *pool;        /* memory storage pool associated with request */
46     const char *pathname;   /* full pathname to resource */
47     apr_finfo_t finfo;       /* filesystem info */
48 };
49
50 /* private context for doing a filesystem walk */
51 typedef struct {
52     /* the input walk parameters */
53     const dav_walk_params *params;
54
55     /* reused as we walk */
56     dav_walk_resource wres;
57
58     dav_resource res1;
59     dav_resource_private info1;
60     dav_buffer path1;
61     dav_buffer uri_buf;
62
63     /* MOVE/COPY need a secondary path */
64     dav_resource res2;
65     dav_resource_private info2;
66     dav_buffer path2;
67
68     dav_buffer locknull_buf;
69
70 } dav_fs_walker_context;
71
72 typedef struct {
73     int is_move;                /* is this a MOVE? */
74     dav_buffer work_buf;        /* handy buffer for copymove_file() */
75
76     /* CALLBACK: this is a secondary resource managed specially for us */
77     const dav_resource *res_dst;
78
79     /* copied from dav_walk_params (they are invariant across the walk) */
80     const dav_resource *root;
81     apr_pool_t *pool;
82
83 } dav_fs_copymove_walk_ctx;
84
85 /* an internal WALKTYPE to walk hidden files (the .DAV directory) */
86 #define DAV_WALKTYPE_HIDDEN     0x4000
87
88 /* an internal WALKTYPE to call collections (again) after their contents */
89 #define DAV_WALKTYPE_POSTFIX    0x8000
90
91 #define DAV_CALLTYPE_POSTFIX    1000    /* a private call type */
92
93
94 /* pull this in from the other source file */
95 extern const dav_hooks_locks dav_hooks_locks_fs;
96
97 /* forward-declare the hook structures */
98 static const dav_hooks_repository dav_hooks_repository_fs;
99 static const dav_hooks_liveprop dav_hooks_liveprop_fs;
100
101 /*
102 ** The namespace URIs that we use. This list and the enumeration must
103 ** stay in sync.
104 */
105 static const char * const dav_fs_namespace_uris[] =
106 {
107     "DAV:",
108     "http://apache.org/dav/props/",
109
110     NULL        /* sentinel */
111 };
112 enum {
113     DAV_FS_URI_DAV,            /* the DAV: namespace URI */
114     DAV_FS_URI_MYPROPS         /* the namespace URI for our custom props */
115 };
116
117 /*
118 ** Does this platform support an executable flag?
119 **
120 ** ### need a way to portably abstract this query
121 */
122 #ifndef WIN32
123 #define DAV_FS_HAS_EXECUTABLE
124 #endif
125
126 /*
127 ** The single property that we define (in the DAV_FS_URI_MYPROPS namespace)
128 */
129 #define DAV_PROPID_FS_executable        1
130
131 static const dav_liveprop_spec dav_fs_props[] =
132 {
133     /* standard DAV properties */
134     {
135         DAV_FS_URI_DAV,
136         "creationdate",
137         DAV_PROPID_creationdate,
138         0
139     },
140     {
141         DAV_FS_URI_DAV,
142         "getcontentlength",
143         DAV_PROPID_getcontentlength,
144         0
145     },
146     {
147         DAV_FS_URI_DAV,
148         "getetag",
149         DAV_PROPID_getetag,
150         0
151     },
152     {
153         DAV_FS_URI_DAV,
154         "getlastmodified",
155         DAV_PROPID_getlastmodified,
156         0
157     },
158
159     /* our custom properties */
160     {
161         DAV_FS_URI_MYPROPS,
162         "executable",
163         DAV_PROPID_FS_executable,
164         0       /* handled special in dav_fs_is_writable */
165     },
166
167     { 0 }        /* sentinel */
168 };
169
170 static const dav_liveprop_group dav_fs_liveprop_group =
171 {
172     dav_fs_props,
173     dav_fs_namespace_uris,
174     &dav_hooks_liveprop_fs
175 };
176
177
178 /* define the dav_stream structure for our use */
179 struct dav_stream {
180     apr_pool_t *p;
181     apr_file_t *f;
182     const char *pathname;       /* we may need to remove it at close time */
183 };
184
185 /* returns an appropriate HTTP status code given an APR status code for a 
186  * failed I/O operation.  ### use something besides 500? */
187 #define MAP_IO2HTTP(e) (APR_STATUS_IS_ENOSPC(e) ? HTTP_INSUFFICIENT_STORAGE : \
188                         HTTP_INTERNAL_SERVER_ERROR)
189
190 /* forward declaration for internal treewalkers */
191 static dav_error * dav_fs_walk(const dav_walk_params *params, int depth,
192                                dav_response **response);
193 static dav_error * dav_fs_internal_walk(const dav_walk_params *params,
194                                         int depth, int is_move,
195                                         const dav_resource *root_dst,
196                                         dav_response **response);
197
198 /* --------------------------------------------------------------------
199 **
200 ** PRIVATE REPOSITORY FUNCTIONS
201 */
202 apr_pool_t *dav_fs_pool(const dav_resource *resource)
203 {
204     return resource->info->pool;
205 }
206
207 const char *dav_fs_pathname(const dav_resource *resource)
208 {
209     return resource->info->pathname;
210 }
211
212 dav_error * dav_fs_dir_file_name(
213     const dav_resource *resource,
214     const char **dirpath_p,
215     const char **fname_p)
216 {
217     dav_resource_private *ctx = resource->info;
218
219     if (resource->collection) {
220         *dirpath_p = ctx->pathname;
221         if (fname_p != NULL)
222             *fname_p = NULL;
223     }
224     else {
225         const char *testpath, *rootpath;
226         char *dirpath = ap_make_dirstr_parent(ctx->pool, ctx->pathname);
227         apr_size_t dirlen = strlen(dirpath);
228         apr_status_t rv = APR_SUCCESS;
229
230         testpath = dirpath;
231         if (dirlen > 0) {
232             rv = apr_filepath_root(&rootpath, &testpath, 0, ctx->pool);
233         }
234         
235         /* remove trailing slash from dirpath, unless it's a root path
236          */
237         if ((rv == APR_SUCCESS && testpath && *testpath)
238             || rv == APR_ERELATIVE) {
239             if (dirpath[dirlen - 1] == '/') {
240                 dirpath[dirlen - 1] = '\0';
241             }
242         }
243         
244         /* ###: Looks like a response could be appropriate
245          *
246          * APR_SUCCESS     here tells us the dir is a root
247          * APR_ERELATIVE   told us we had no root (ok)
248          * APR_EINCOMPLETE an incomplete testpath told us
249          *                 there was no -file- name here!
250          * APR_EBADPATH    or other errors tell us this file
251          *                 path is undecipherable
252          */
253
254         if (rv == APR_SUCCESS || rv == APR_ERELATIVE) {
255             *dirpath_p = dirpath;
256             if (fname_p != NULL)
257                 *fname_p = ctx->pathname + dirlen;
258         }
259         else {
260             return dav_new_error(ctx->pool, HTTP_INTERNAL_SERVER_ERROR, 0,
261                                  "An incomplete/bad path was found in "
262                                  "dav_fs_dir_file_name.");
263         }
264     }
265
266     return NULL;
267 }
268
269 /* Note: picked up from ap_gm_timestr_822() */
270 /* NOTE: buf must be at least DAV_TIMEBUF_SIZE chars in size */
271 static void dav_format_time(int style, apr_time_t sec, char *buf)
272 {
273     apr_time_exp_t tms;
274     
275     /* ### what to do if fails? */
276     (void) apr_time_exp_gmt(&tms, sec);
277
278     if (style == DAV_STYLE_ISO8601) {
279         /* ### should we use "-00:00" instead of "Z" ?? */
280
281         /* 20 chars plus null term */
282         sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2dZ",
283                tms.tm_year + 1900, tms.tm_mon + 1, tms.tm_mday,
284                tms.tm_hour, tms.tm_min, tms.tm_sec);
285         return;
286     }
287
288     /* RFC 822 date format; as strftime '%a, %d %b %Y %T GMT' */
289
290     /* 29 chars plus null term */
291     sprintf(buf,
292             "%s, %.2d %s %d %.2d:%.2d:%.2d GMT",
293            apr_day_snames[tms.tm_wday],
294            tms.tm_mday, apr_month_snames[tms.tm_mon],
295            tms.tm_year + 1900,
296            tms.tm_hour, tms.tm_min, tms.tm_sec);
297 }
298
299 /* Copy or move src to dst; src_finfo is used to propagate permissions
300  * bits across if non-NULL; dst_finfo must be non-NULL iff dst already
301  * exists. */
302 static dav_error * dav_fs_copymove_file(
303     int is_move,
304     apr_pool_t * p,
305     const char *src,
306     const char *dst,
307     const apr_finfo_t *src_finfo,
308     const apr_finfo_t *dst_finfo,
309     dav_buffer *pbuf)
310 {
311     dav_buffer work_buf = { 0 };
312     apr_file_t *inf = NULL;
313     apr_file_t *outf = NULL;
314     apr_status_t status;
315     apr_fileperms_t perms;
316
317     if (pbuf == NULL)
318         pbuf = &work_buf;
319
320     /* Determine permissions to use for destination */
321     if (src_finfo && src_finfo->valid & APR_FINFO_PROT
322         && src_finfo->protection & APR_UEXECUTE) {
323         if (dst_finfo != NULL) {
324             /* chmod it if it already exist */
325             if (apr_file_perms_set(dst, src_finfo->protection)) {
326                 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
327                                      "Could not set permissions on destination");
328             }
329         } 
330         else {
331             perms = src_finfo->protection;
332         }
333     } 
334     else {
335         perms = APR_OS_DEFAULT;
336     }
337
338     dav_set_bufsize(p, pbuf, DAV_FS_COPY_BLOCKSIZE);
339
340     if ((apr_file_open(&inf, src, APR_READ | APR_BINARY, APR_OS_DEFAULT, p)) 
341             != APR_SUCCESS) {
342         /* ### use something besides 500? */
343         return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
344                              "Could not open file for reading");
345     }
346
347     /* ### do we need to deal with the umask? */
348     status = apr_file_open(&outf, dst, APR_WRITE | APR_CREATE | APR_TRUNCATE 
349                            | APR_BINARY, perms, p);
350     if (status != APR_SUCCESS) {
351         apr_file_close(inf);
352
353         return dav_new_error(p, MAP_IO2HTTP(status), 0,
354                              "Could not open file for writing");
355     }
356
357     while (1) {
358         apr_size_t len = DAV_FS_COPY_BLOCKSIZE;
359
360         status = apr_file_read(inf, pbuf->buf, &len);
361         if (status != APR_SUCCESS && status != APR_EOF) {
362             apr_file_close(inf);
363             apr_file_close(outf);
364             
365             if (apr_file_remove(dst, p) != APR_SUCCESS) {
366                 /* ### ACK! Inconsistent state... */
367
368                 /* ### use something besides 500? */
369                 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
370                                      "Could not delete output after read "
371                                      "failure. Server is now in an "
372                                      "inconsistent state.");
373             }
374
375             /* ### use something besides 500? */
376             return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
377                                  "Could not read input file");
378         }
379
380         if (status == APR_EOF)
381             break;
382
383         /* write any bytes that were read */
384         status = apr_file_write_full(outf, pbuf->buf, len, NULL);
385         if (status != APR_SUCCESS) {
386             apr_file_close(inf);
387             apr_file_close(outf);
388
389             if (apr_file_remove(dst, p) != APR_SUCCESS) {
390                 /* ### ACK! Inconsistent state... */
391
392                 /* ### use something besides 500? */
393                 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
394                                      "Could not delete output after write "
395                                      "failure. Server is now in an "
396                                      "inconsistent state.");
397             }
398
399             return dav_new_error(p, MAP_IO2HTTP(status), 0,
400                                  "Could not write output file");
401         }
402     }
403
404     apr_file_close(inf);
405     apr_file_close(outf);
406
407     if (is_move && apr_file_remove(src, p) != APR_SUCCESS) {
408         dav_error *err;
409         int save_errno = errno;   /* save the errno that got us here */
410
411         if (apr_file_remove(dst, p) != APR_SUCCESS) {
412             /* ### ACK. this creates an inconsistency. do more!? */
413
414             /* ### use something besides 500? */
415             /* Note that we use the latest errno */
416             return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
417                                  "Could not remove source or destination "
418                                  "file. Server is now in an inconsistent "
419                                  "state.");
420         }
421
422         /* ### use something besides 500? */
423         err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
424                             "Could not remove source file after move. "
425                             "Destination was removed to ensure consistency.");
426         err->save_errno = save_errno;
427         return err;
428     }
429
430     return NULL;
431 }
432
433 /* copy/move a file from within a state dir to another state dir */
434 /* ### need more buffers to replace the pool argument */
435 static dav_error * dav_fs_copymove_state(
436     int is_move,
437     apr_pool_t * p,
438     const char *src_dir, const char *src_file,
439     const char *dst_dir, const char *dst_file,
440     dav_buffer *pbuf)
441 {
442     apr_finfo_t src_finfo;        /* finfo for source file */
443     apr_finfo_t dst_state_finfo;        /* finfo for STATE directory */
444     apr_status_t rv;
445     const char *src;
446     const char *dst;
447
448     /* build the propset pathname for the source file */
449     src = apr_pstrcat(p, src_dir, "/" DAV_FS_STATE_DIR "/", src_file, NULL);
450
451     /* the source file doesn't exist */
452     rv = apr_stat(&src_finfo, src, APR_FINFO_NORM, p);
453     if (rv != APR_SUCCESS && rv != APR_INCOMPLETE) {
454         return NULL;
455     }
456
457     /* build the pathname for the destination state dir */
458     dst = apr_pstrcat(p, dst_dir, "/" DAV_FS_STATE_DIR, NULL);
459
460     /* ### do we need to deal with the umask? */
461
462     /* ensure that it exists */
463     rv = apr_dir_make(dst, APR_OS_DEFAULT, p);
464     if (rv != APR_SUCCESS) {
465         if (!APR_STATUS_IS_EEXIST(rv)) {
466             /* ### use something besides 500? */
467             return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
468                                  "Could not create internal state directory");
469         }
470     }
471
472     /* get info about the state directory */
473     rv = apr_stat(&dst_state_finfo, dst, APR_FINFO_NORM, p);
474     if (rv != APR_SUCCESS && rv != APR_INCOMPLETE) {
475         /* Ack! Where'd it go? */
476         /* ### use something besides 500? */
477         return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
478                              "State directory disappeared");
479     }
480
481     /* The mkdir() may have failed because a *file* exists there already */
482     if (dst_state_finfo.filetype != APR_DIR) {
483         /* ### try to recover by deleting this file? (and mkdir again) */
484         /* ### use something besides 500? */
485         return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
486                              "State directory is actually a file");
487     }
488
489     /* append the target file to the state directory pathname */
490     dst = apr_pstrcat(p, dst, "/", dst_file, NULL);
491
492     /* copy/move the file now */
493     if (is_move && src_finfo.device == dst_state_finfo.device) {
494         /* simple rename is possible since it is on the same device */
495         if (apr_file_rename(src, dst, p) != APR_SUCCESS) {
496             /* ### use something besides 500? */
497             return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
498                                  "Could not move state file.");
499         }
500     }
501     else
502     {
503         /* gotta copy (and delete) */
504         return dav_fs_copymove_file(is_move, p, src, dst, NULL, NULL, pbuf);
505     }
506
507     return NULL;
508 }
509
510 static dav_error *dav_fs_copymoveset(int is_move, apr_pool_t *p,
511                                      const dav_resource *src,
512                                      const dav_resource *dst,
513                                      dav_buffer *pbuf)
514 {
515     const char *src_dir;
516     const char *src_file;
517     const char *src_state1;
518     const char *src_state2;
519     const char *dst_dir;
520     const char *dst_file;
521     const char *dst_state1;
522     const char *dst_state2;
523     dav_error *err;
524
525     /* Get directory and filename for resources */
526     /* ### should test these result values... */
527     (void) dav_fs_dir_file_name(src, &src_dir, &src_file);
528     (void) dav_fs_dir_file_name(dst, &dst_dir, &dst_file);
529
530     /* Get the corresponding state files for each resource */
531     dav_dbm_get_statefiles(p, src_file, &src_state1, &src_state2);
532     dav_dbm_get_statefiles(p, dst_file, &dst_state1, &dst_state2);
533 #if DAV_DEBUG
534     if ((src_state2 != NULL && dst_state2 == NULL) ||
535         (src_state2 == NULL && dst_state2 != NULL)) {
536         return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
537                              "DESIGN ERROR: dav_dbm_get_statefiles() "
538                              "returned inconsistent results.");
539     }
540 #endif
541
542     err = dav_fs_copymove_state(is_move, p,
543                                 src_dir, src_state1,
544                                 dst_dir, dst_state1,
545                                 pbuf);
546
547     if (err == NULL && src_state2 != NULL) {
548         err = dav_fs_copymove_state(is_move, p,
549                                     src_dir, src_state2,
550                                     dst_dir, dst_state2,
551                                     pbuf);
552
553         if (err != NULL) {
554             /* ### CRAP. inconsistency. */
555             /* ### should perform some cleanup at the target if we still
556                ### have the original files */
557
558             /* Change the error to reflect the bad server state. */
559             err->status = HTTP_INTERNAL_SERVER_ERROR;
560             err->desc =
561                 "Could not fully copy/move the properties. "
562                 "The server is now in an inconsistent state.";
563         }
564     }
565
566     return err;
567 }
568
569 static dav_error *dav_fs_deleteset(apr_pool_t *p, const dav_resource *resource)
570 {
571     const char *dirpath;
572     const char *fname;
573     const char *state1;
574     const char *state2;
575     const char *pathname;
576     apr_status_t status;
577
578     /* Get directory, filename, and state-file names for the resource */
579     /* ### should test this result value... */
580     (void) dav_fs_dir_file_name(resource, &dirpath, &fname);
581     dav_dbm_get_statefiles(p, fname, &state1, &state2);
582
583     /* build the propset pathname for the file */
584     pathname = apr_pstrcat(p,
585                           dirpath,
586                           "/" DAV_FS_STATE_DIR "/",
587                           state1,
588                           NULL);
589
590     /* note: we may get ENOENT if the state dir is not present */
591     if ((status = apr_file_remove(pathname, p)) != APR_SUCCESS
592         && !APR_STATUS_IS_ENOENT(status)) {
593         return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
594                              "Could not remove properties.");
595     }
596
597     if (state2 != NULL) {
598         /* build the propset pathname for the file */
599         pathname = apr_pstrcat(p,
600                               dirpath,
601                               "/" DAV_FS_STATE_DIR "/",
602                               state2,
603                               NULL);
604
605         if ((status = apr_file_remove(pathname, p)) != APR_SUCCESS
606             && !APR_STATUS_IS_ENOENT(status)) {
607             /* ### CRAP. only removed half. */
608             return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
609                                  "Could not fully remove properties. "
610                                  "The server is now in an inconsistent "
611                                  "state.");
612         }
613     }
614
615     return NULL;
616 }
617
618 /* --------------------------------------------------------------------
619 **
620 ** REPOSITORY HOOK FUNCTIONS
621 */
622
623 static dav_error * dav_fs_get_resource(
624     request_rec *r,
625     const char *root_dir,
626     const char *label,
627     int use_checked_in,
628     dav_resource **result_resource)
629 {
630     dav_resource_private *ctx;
631     dav_resource *resource;
632     char *s;
633     char *filename;
634     apr_size_t len;
635
636     /* ### optimize this into a single allocation! */
637
638     /* Create private resource context descriptor */
639     ctx = apr_pcalloc(r->pool, sizeof(*ctx));
640     ctx->finfo = r->finfo;
641
642     /* ### this should go away */
643     ctx->pool = r->pool;
644
645     /* Preserve case on OSes which fold canonical filenames */
646 #if 0
647     /* ### not available in Apache 2.0 yet */
648     filename = r->case_preserved_filename;
649 #else
650     filename = r->filename;
651 #endif
652
653     /*
654     ** If there is anything in the path_info, then this indicates that the
655     ** entire path was not used to specify the file/dir. We want to append
656     ** it onto the filename so that we get a "valid" pathname for null
657     ** resources.
658     */
659     s = apr_pstrcat(r->pool, filename, r->path_info, NULL);
660
661     /* make sure the pathname does not have a trailing "/" */
662     len = strlen(s);
663     if (len > 1 && s[len - 1] == '/') {
664         s[len - 1] = '\0';
665     }
666     ctx->pathname = s;
667
668     /* Create resource descriptor */
669     resource = apr_pcalloc(r->pool, sizeof(*resource));
670     resource->type = DAV_RESOURCE_TYPE_REGULAR;
671     resource->info = ctx;
672     resource->hooks = &dav_hooks_repository_fs;
673     resource->pool = r->pool;
674
675     /* make sure the URI does not have a trailing "/" */
676     len = strlen(r->uri);
677     if (len > 1 && r->uri[len - 1] == '/') {
678         s = apr_pstrdup(r->pool, r->uri);
679         s[len - 1] = '\0';
680         resource->uri = s;
681     }
682     else {
683         resource->uri = r->uri;
684     }
685
686     if (r->finfo.filetype != 0) {
687         resource->exists = 1;
688         resource->collection = r->finfo.filetype == APR_DIR;
689
690         /* unused info in the URL will indicate a null resource */
691
692         if (r->path_info != NULL && *r->path_info != '\0') {
693             if (resource->collection) {
694                 /* only a trailing "/" is allowed */
695                 if (*r->path_info != '/' || r->path_info[1] != '\0') {
696
697                     /*
698                     ** This URL/filename represents a locknull resource or
699                     ** possibly a destination of a MOVE/COPY
700                     */
701                     resource->exists = 0;
702                     resource->collection = 0;
703                 }
704             }
705             else
706             {
707                 /*
708                 ** The base of the path refers to a file -- nothing should
709                 ** be in path_info. The resource is simply an error: it
710                 ** can't be a null or a locknull resource.
711                 */
712                 return dav_new_error(r->pool, HTTP_BAD_REQUEST, 0,
713                                      "The URL contains extraneous path "
714                                      "components. The resource could not "
715                                      "be identified.");
716             }
717
718             /* retain proper integrity across the structures */
719             if (!resource->exists) {
720                 ctx->finfo.filetype = 0;
721             }
722         }
723     }
724
725     *result_resource = resource;
726     return NULL;
727 }
728
729 static dav_error * dav_fs_get_parent_resource(const dav_resource *resource,
730                                               dav_resource **result_parent)
731 {
732     dav_resource_private *ctx = resource->info;
733     dav_resource_private *parent_ctx;
734     dav_resource *parent_resource;
735     apr_status_t rv;
736     char *dirpath;
737     const char *testroot;
738     const char *testpath;
739
740     /* If we're at the root of the URL space, then there is no parent. */
741     if (strcmp(resource->uri, "/") == 0) {
742         *result_parent = NULL;
743         return NULL;
744     }
745
746     /* If given resource is root, then there is no parent.
747      * Unless we can retrieve the filepath root, this is
748      * intendend to fail.  If we split the root and
749      * no path info remains, then we also fail.
750      */
751     testpath = ctx->pathname;
752     rv = apr_filepath_root(&testroot, &testpath, 0, ctx->pool);
753     if ((rv != APR_SUCCESS && rv != APR_ERELATIVE) 
754         || !testpath || !*testpath) {
755         *result_parent = NULL;
756         return NULL;
757     }
758
759     /* ### optimize this into a single allocation! */
760
761     /* Create private resource context descriptor */
762     parent_ctx = apr_pcalloc(ctx->pool, sizeof(*parent_ctx));
763
764     /* ### this should go away */
765     parent_ctx->pool = ctx->pool;
766
767     dirpath = ap_make_dirstr_parent(ctx->pool, ctx->pathname);
768     if (strlen(dirpath) > 1 && dirpath[strlen(dirpath) - 1] == '/') 
769         dirpath[strlen(dirpath) - 1] = '\0';
770     parent_ctx->pathname = dirpath;
771
772     parent_resource = apr_pcalloc(ctx->pool, sizeof(*parent_resource));
773     parent_resource->info = parent_ctx;
774     parent_resource->collection = 1;
775     parent_resource->hooks = &dav_hooks_repository_fs;
776     parent_resource->pool = resource->pool;
777
778     if (resource->uri != NULL) {
779         char *uri = ap_make_dirstr_parent(ctx->pool, resource->uri);
780         if (strlen(uri) > 1 && uri[strlen(uri) - 1] == '/')
781             uri[strlen(uri) - 1] = '\0';
782         parent_resource->uri = uri;
783     }
784
785     rv = apr_stat(&parent_ctx->finfo, parent_ctx->pathname, 
786                   APR_FINFO_NORM, ctx->pool);
787     if (rv == APR_SUCCESS || rv == APR_INCOMPLETE) {
788         parent_resource->exists = 1;
789     }
790
791     *result_parent = parent_resource;
792     return NULL;
793 }
794
795 static int dav_fs_is_same_resource(
796     const dav_resource *res1,
797     const dav_resource *res2)
798 {
799     dav_resource_private *ctx1 = res1->info;
800     dav_resource_private *ctx2 = res2->info;
801
802     if (res1->hooks != res2->hooks)
803         return 0;
804
805     if ((ctx1->finfo.filetype != 0) && (ctx2->finfo.filetype != 0)
806         && (ctx1->finfo.valid & ctx2->finfo.valid & APR_FINFO_INODE)) {
807         return ctx1->finfo.inode == ctx2->finfo.inode;
808     }
809     else {
810         return strcmp(ctx1->pathname, ctx2->pathname) == 0;
811     }
812 }
813
814 static int dav_fs_is_parent_resource(
815     const dav_resource *res1,
816     const dav_resource *res2)
817 {
818     dav_resource_private *ctx1 = res1->info;
819     dav_resource_private *ctx2 = res2->info;
820     apr_size_t len1 = strlen(ctx1->pathname);
821     apr_size_t len2;
822
823     if (res1->hooks != res2->hooks)
824         return 0;
825
826     /* it is safe to use ctx2 now */
827     len2 = strlen(ctx2->pathname);
828
829     return (len2 > len1
830             && memcmp(ctx1->pathname, ctx2->pathname, len1) == 0
831             && ctx2->pathname[len1] == '/');
832 }
833
834 static dav_error * dav_fs_open_stream(const dav_resource *resource,
835                                       dav_stream_mode mode,
836                                       dav_stream **stream)
837 {
838     apr_pool_t *p = resource->info->pool;
839     dav_stream *ds = apr_pcalloc(p, sizeof(*ds));
840     apr_int32_t flags;
841     apr_status_t rv;
842
843     switch (mode) {
844     default:
845         flags = APR_READ | APR_BINARY;
846         break;
847
848     case DAV_MODE_WRITE_TRUNC:
849         flags = APR_WRITE | APR_CREATE | APR_TRUNCATE | APR_BINARY;
850         break;
851     case DAV_MODE_WRITE_SEEKABLE:
852         flags = APR_WRITE | APR_CREATE | APR_BINARY;
853         break;
854     }
855
856     ds->p = p;
857     ds->pathname = resource->info->pathname;
858     rv = apr_file_open(&ds->f, ds->pathname, flags, APR_OS_DEFAULT, ds->p);
859     if (rv != APR_SUCCESS) {
860         return dav_new_error(p, MAP_IO2HTTP(rv), 0,
861                              "An error occurred while opening a resource.");
862     }
863
864     /* (APR registers cleanups for the fd with the pool) */
865
866     *stream = ds;
867     return NULL;
868 }
869
870 static dav_error * dav_fs_close_stream(dav_stream *stream, int commit)
871 {
872     apr_file_close(stream->f);
873
874     if (!commit) {
875         if (apr_file_remove(stream->pathname, stream->p) != APR_SUCCESS) {
876             /* ### use a better description? */
877             return dav_new_error(stream->p, HTTP_INTERNAL_SERVER_ERROR, 0,
878                                  "There was a problem removing (rolling "
879                                  "back) the resource "
880                                  "when it was being closed.");
881         }
882     }
883
884     return NULL;
885 }
886
887 static dav_error * dav_fs_write_stream(dav_stream *stream,
888                                        const void *buf, apr_size_t bufsize)
889 {
890     apr_status_t status;
891
892     status = apr_file_write_full(stream->f, buf, bufsize, NULL);
893     if (APR_STATUS_IS_ENOSPC(status)) {
894         return dav_new_error(stream->p, HTTP_INSUFFICIENT_STORAGE, 0,
895                              "There is not enough storage to write to "
896                              "this resource.");
897     }
898     else if (status != APR_SUCCESS) {
899         /* ### use something besides 500? */
900         return dav_new_error(stream->p, HTTP_INTERNAL_SERVER_ERROR, 0,
901                              "An error occurred while writing to a "
902                              "resource.");
903     }
904     return NULL;
905 }
906
907 static dav_error * dav_fs_seek_stream(dav_stream *stream, apr_off_t abs_pos)
908 {
909     if (apr_file_seek(stream->f, APR_SET, &abs_pos) != APR_SUCCESS) {
910         /* ### should check whether apr_file_seek set abs_pos was set to the
911          * correct position? */
912         /* ### use something besides 500? */
913         return dav_new_error(stream->p, HTTP_INTERNAL_SERVER_ERROR, 0,
914                              "Could not seek to specified position in the "
915                              "resource.");
916     }
917     return NULL;
918 }
919
920
921 #if DEBUG_GET_HANDLER
922
923 /* only define set_headers() and deliver() for debug purposes */
924
925
926 static dav_error * dav_fs_set_headers(request_rec *r,
927                                       const dav_resource *resource)
928 {
929     /* ### this function isn't really used since we have a get_pathname */
930     if (!resource->exists)
931         return NULL;
932
933     /* make sure the proper mtime is in the request record */
934     ap_update_mtime(r, resource->info->finfo.mtime);
935
936     /* ### note that these use r->filename rather than <resource> */
937     ap_set_last_modified(r);
938     ap_set_etag(r);
939
940     /* we accept byte-ranges */
941     apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
942
943     /* set up the Content-Length header */
944     ap_set_content_length(r, resource->info->finfo.size);
945
946     /* ### how to set the content type? */
947     /* ### until this is resolved, the Content-Type header is busted */
948
949     return NULL;
950 }
951
952 static dav_error * dav_fs_deliver(const dav_resource *resource,
953                                   ap_filter_t *output)
954 {
955     apr_pool_t *pool = resource->pool;
956     apr_bucket_brigade *bb;
957     apr_file_t *fd;
958     apr_status_t status;
959     apr_bucket *bkt;
960
961     /* Check resource type */
962     if (resource->type != DAV_RESOURCE_TYPE_REGULAR
963         && resource->type != DAV_RESOURCE_TYPE_VERSION
964         && resource->type != DAV_RESOURCE_TYPE_WORKING) {
965         return dav_new_error(pool, HTTP_CONFLICT, 0,
966                              "Cannot GET this type of resource.");
967     }
968     if (resource->collection) {
969         return dav_new_error(pool, HTTP_CONFLICT, 0,
970                              "There is no default response to GET for a "
971                              "collection.");
972     }
973
974     if ((status = apr_file_open(&fd, resource->info->pathname,
975                                 APR_READ | APR_BINARY, 0,
976                                 pool)) != APR_SUCCESS) {
977         return dav_new_error(pool, HTTP_FORBIDDEN, 0,
978                              "File permissions deny server access.");
979     }
980
981     bb = apr_brigade_create(pool, output->c->bucket_alloc);
982
983     /* ### this does not handle large files. but this is test code anyway */
984     bkt = apr_bucket_file_create(fd, 0,
985                                  (apr_size_t)resource->info->finfo.size,
986                                  pool, output->c->bucket_alloc);
987     APR_BRIGADE_INSERT_TAIL(bb, bkt);
988
989     bkt = apr_bucket_eos_create(output->c->bucket_alloc);
990     APR_BRIGADE_INSERT_TAIL(bb, bkt);
991
992     if ((status = ap_pass_brigade(output, bb)) != APR_SUCCESS) {
993         return dav_new_error(pool, HTTP_FORBIDDEN, 0,
994                              "Could not write contents to filter.");
995     }
996
997     return NULL;
998 }
999
1000 #endif /* DEBUG_GET_HANDLER */
1001
1002
1003 static dav_error * dav_fs_create_collection(dav_resource *resource)
1004 {
1005     dav_resource_private *ctx = resource->info;
1006     apr_status_t status;
1007
1008     status = apr_dir_make(ctx->pathname, APR_OS_DEFAULT, ctx->pool);
1009     if (APR_STATUS_IS_ENOSPC(status)) {
1010         return dav_new_error(ctx->pool, HTTP_INSUFFICIENT_STORAGE, 0,
1011                              "There is not enough storage to create "
1012                              "this collection.");
1013     }
1014     else if (status != APR_SUCCESS) {
1015         /* ### refine this error message? */
1016         return dav_new_error(ctx->pool, HTTP_FORBIDDEN, 0,
1017                              "Unable to create collection.");
1018     }
1019
1020     /* update resource state to show it exists as a collection */
1021     resource->exists = 1;
1022     resource->collection = 1;
1023
1024     return NULL;
1025 }
1026
1027 static dav_error * dav_fs_copymove_walker(dav_walk_resource *wres,
1028                                           int calltype)
1029 {
1030     dav_fs_copymove_walk_ctx *ctx = wres->walk_ctx;
1031     dav_resource_private *srcinfo = wres->resource->info;
1032     dav_resource_private *dstinfo = ctx->res_dst->info;
1033     dav_error *err = NULL;
1034
1035     if (wres->resource->collection) {
1036         if (calltype == DAV_CALLTYPE_POSTFIX) {
1037             /* Postfix call for MOVE. delete the source dir.
1038              * Note: when copying, we do not enable the postfix-traversal.
1039              */
1040             /* ### we are ignoring any error here; what should we do? */
1041             (void) apr_dir_remove(srcinfo->pathname, ctx->pool);
1042         }
1043         else {
1044             /* copy/move of a collection. Create the new, target collection */
1045             if (apr_dir_make(dstinfo->pathname, APR_OS_DEFAULT,
1046                              ctx->pool) != APR_SUCCESS) {
1047                 /* ### assume it was a permissions problem */
1048                 /* ### need a description here */
1049                 err = dav_new_error(ctx->pool, HTTP_FORBIDDEN, 0, NULL);
1050             }
1051         }
1052     }
1053     else {
1054         err = dav_fs_copymove_file(ctx->is_move, ctx->pool, 
1055                                    srcinfo->pathname, dstinfo->pathname, 
1056                                    &srcinfo->finfo, 
1057                                    ctx->res_dst->exists ? &dstinfo->finfo : NULL,
1058                                    &ctx->work_buf);
1059         /* ### push a higher-level description? */
1060     }
1061
1062     /*
1063     ** If we have a "not so bad" error, then it might need to go into a
1064     ** multistatus response.
1065     **
1066     ** For a MOVE, it will always go into the multistatus. It could be
1067     ** that everything has been moved *except* for the root. Using a
1068     ** multistatus (with no errors for the other resources) will signify
1069     ** this condition.
1070     **
1071     ** For a COPY, we are traversing in a prefix fashion. If the root fails,
1072     ** then we can just bail out now.
1073     */
1074     if (err != NULL
1075         && !ap_is_HTTP_SERVER_ERROR(err->status)
1076         && (ctx->is_move
1077             || !dav_fs_is_same_resource(wres->resource, ctx->root))) {
1078         /* ### use errno to generate DAV:responsedescription? */
1079         dav_add_response(wres, err->status, NULL);
1080
1081         /* the error is in the multistatus now. do not stop the traversal. */
1082         return NULL;
1083     }
1084
1085     return err;
1086 }
1087
1088 static dav_error *dav_fs_copymove_resource(
1089     int is_move,
1090     const dav_resource *src,
1091     const dav_resource *dst,
1092     int depth,
1093     dav_response **response)
1094 {
1095     dav_error *err = NULL;
1096     dav_buffer work_buf = { 0 };
1097
1098     *response = NULL;
1099
1100     /* if a collection, recursively copy/move it and its children,
1101      * including the state dirs
1102      */
1103     if (src->collection) {
1104         dav_walk_params params = { 0 };
1105         dav_response *multi_status;
1106
1107         params.walk_type = DAV_WALKTYPE_NORMAL | DAV_WALKTYPE_HIDDEN;
1108         params.func = dav_fs_copymove_walker;
1109         params.pool = src->info->pool;
1110         params.root = src;
1111
1112         /* params.walk_ctx is managed by dav_fs_internal_walk() */
1113
1114         /* postfix is needed for MOVE to delete source dirs */
1115         if (is_move)
1116             params.walk_type |= DAV_WALKTYPE_POSTFIX;
1117
1118         /* note that we return the error OR the multistatus. never both */
1119
1120         if ((err = dav_fs_internal_walk(&params, depth, is_move, dst,
1121                                         &multi_status)) != NULL) {
1122             /* on a "real" error, then just punt. nothing else to do. */
1123             return err;
1124         }
1125
1126         if ((*response = multi_status) != NULL) {
1127             /* some multistatus responses exist. wrap them in a 207 */
1128             return dav_new_error(src->info->pool, HTTP_MULTI_STATUS, 0,
1129                                  "Error(s) occurred on some resources during "
1130                                  "the COPY/MOVE process.");
1131         }
1132
1133         return NULL;
1134     }
1135
1136     /* not a collection */
1137     if ((err = dav_fs_copymove_file(is_move, src->info->pool,
1138                                     src->info->pathname, dst->info->pathname,
1139                                     &src->info->finfo, 
1140                                     dst->exists ? &dst->info->finfo : NULL,
1141                                     &work_buf)) != NULL) {
1142         /* ### push a higher-level description? */
1143         return err;
1144     }
1145         
1146     /* copy/move properties as well */
1147     return dav_fs_copymoveset(is_move, src->info->pool, src, dst, &work_buf);
1148 }
1149
1150 static dav_error * dav_fs_copy_resource(
1151     const dav_resource *src,
1152     dav_resource *dst,
1153     int depth,
1154     dav_response **response)
1155 {
1156     dav_error *err;
1157
1158 #if DAV_DEBUG
1159     if (src->hooks != dst->hooks) {
1160         /*
1161         ** ### strictly speaking, this is a design error; we should not
1162         ** ### have reached this point.
1163         */
1164         return dav_new_error(src->info->pool, HTTP_INTERNAL_SERVER_ERROR, 0,
1165                              "DESIGN ERROR: a mix of repositories "
1166                              "was passed to copy_resource.");
1167     }
1168 #endif
1169
1170     if ((err = dav_fs_copymove_resource(0, src, dst, depth,
1171                                         response)) == NULL) {
1172
1173         /* update state of destination resource to show it exists */
1174         dst->exists = 1;
1175         dst->collection = src->collection;
1176     }
1177
1178     return err;
1179 }
1180
1181 static dav_error * dav_fs_move_resource(
1182     dav_resource *src,
1183     dav_resource *dst,
1184     dav_response **response)
1185 {
1186     dav_resource_private *srcinfo = src->info;
1187     dav_resource_private *dstinfo = dst->info;
1188     dav_error *err;
1189     int can_rename = 0;
1190
1191 #if DAV_DEBUG
1192     if (src->hooks != dst->hooks) {
1193         /*
1194         ** ### strictly speaking, this is a design error; we should not
1195         ** ### have reached this point.
1196         */
1197         return dav_new_error(src->info->pool, HTTP_INTERNAL_SERVER_ERROR, 0,
1198                              "DESIGN ERROR: a mix of repositories "
1199                              "was passed to move_resource.");
1200     }
1201 #endif
1202
1203     /* determine whether a simple rename will work.
1204      * Assume source exists, else we wouldn't get called.
1205      */
1206     if (dstinfo->finfo.filetype != 0) {
1207         if (dstinfo->finfo.device == srcinfo->finfo.device) {
1208             /* target exists and is on the same device. */
1209             can_rename = 1;
1210         }
1211     }
1212     else {
1213         const char *dirpath;
1214         apr_finfo_t finfo;
1215         apr_status_t rv;
1216
1217         /* destination does not exist, but the parent directory should,
1218          * so try it
1219          */
1220         dirpath = ap_make_dirstr_parent(dstinfo->pool, dstinfo->pathname);
1221         /* 
1222          * XXX: If missing dev ... then what test?
1223          * Really need a try and failover for those platforms.
1224          * 
1225          */
1226         rv = apr_stat(&finfo, dirpath, APR_FINFO_DEV, dstinfo->pool);
1227         if ((rv == APR_SUCCESS || rv == APR_INCOMPLETE)
1228             && (finfo.valid & srcinfo->finfo.valid & APR_FINFO_DEV)
1229             && (finfo.device == srcinfo->finfo.device)) {
1230             can_rename = 1;
1231         }
1232     }
1233
1234     /* if we can't simply rename, then do it the hard way... */
1235     if (!can_rename) {
1236         if ((err = dav_fs_copymove_resource(1, src, dst, DAV_INFINITY,
1237                                             response)) == NULL) {
1238             /* update resource states */
1239             dst->exists = 1;
1240             dst->collection = src->collection;
1241             src->exists = 0;
1242             src->collection = 0;
1243         }
1244
1245         return err;
1246     }
1247
1248     /* a rename should work. do it, and move properties as well */
1249
1250     /* no multistatus response */
1251     *response = NULL;
1252
1253     /* ### APR has no rename? */
1254     if (apr_file_rename(srcinfo->pathname, dstinfo->pathname,
1255                        srcinfo->pool) != APR_SUCCESS) {
1256         /* ### should have a better error than this. */
1257         return dav_new_error(srcinfo->pool, HTTP_INTERNAL_SERVER_ERROR, 0,
1258                              "Could not rename resource.");
1259     }
1260
1261     /* update resource states */
1262     dst->exists = 1;
1263     dst->collection = src->collection;
1264     src->exists = 0;
1265     src->collection = 0;
1266
1267     if ((err = dav_fs_copymoveset(1, src->info->pool,
1268                                   src, dst, NULL)) == NULL) {
1269         /* no error. we're done. go ahead and return now. */
1270         return NULL;
1271     }
1272
1273     /* error occurred during properties move; try to put resource back */
1274     if (apr_file_rename(dstinfo->pathname, srcinfo->pathname,
1275                        srcinfo->pool) != APR_SUCCESS) {
1276         /* couldn't put it back! */
1277         return dav_push_error(srcinfo->pool,
1278                               HTTP_INTERNAL_SERVER_ERROR, 0,
1279                               "The resource was moved, but a failure "
1280                               "occurred during the move of its "
1281                               "properties. The resource could not be "
1282                               "restored to its original location. The "
1283                               "server is now in an inconsistent state.",
1284                               err);
1285     }
1286
1287     /* update resource states again */
1288     src->exists = 1;
1289     src->collection = dst->collection;
1290     dst->exists = 0;
1291     dst->collection = 0;
1292
1293     /* resource moved back, but properties may be inconsistent */
1294     return dav_push_error(srcinfo->pool,
1295                           HTTP_INTERNAL_SERVER_ERROR, 0,
1296                           "The resource was moved, but a failure "
1297                           "occurred during the move of its properties. "
1298                           "The resource was moved back to its original "
1299                           "location, but its properties may have been "
1300                           "partially moved. The server may be in an "
1301                           "inconsistent state.",
1302                           err);
1303 }
1304
1305 static dav_error * dav_fs_delete_walker(dav_walk_resource *wres, int calltype)
1306 {
1307     dav_resource_private *info = wres->resource->info;
1308
1309     /* do not attempt to remove a null resource,
1310      * or a collection with children
1311      */
1312     if (wres->resource->exists &&
1313         (!wres->resource->collection || calltype == DAV_CALLTYPE_POSTFIX)) {
1314         /* try to remove the resource */
1315         apr_status_t result;
1316
1317         result = wres->resource->collection
1318             ? apr_dir_remove(info->pathname, wres->pool)
1319             : apr_file_remove(info->pathname, wres->pool);
1320
1321         /*
1322         ** If an error occurred, then add it to multistatus response.
1323         ** Note that we add it for the root resource, too. It is quite
1324         ** possible to delete the whole darn tree, yet fail on the root.
1325         **
1326         ** (also: remember we are deleting via a postfix traversal)
1327         */
1328         if (result != APR_SUCCESS) {
1329             /* ### assume there is a permissions problem */
1330
1331             /* ### use errno to generate DAV:responsedescription? */
1332             dav_add_response(wres, HTTP_FORBIDDEN, NULL);
1333         }
1334     }
1335
1336     return NULL;
1337 }
1338
1339 static dav_error * dav_fs_remove_resource(dav_resource *resource,
1340                                           dav_response **response)
1341 {
1342     dav_resource_private *info = resource->info;
1343
1344     *response = NULL;
1345
1346     /* if a collection, recursively remove it and its children,
1347      * including the state dirs
1348      */
1349     if (resource->collection) {
1350         dav_walk_params params = { 0 };
1351         dav_error *err = NULL;
1352         dav_response *multi_status;
1353
1354         params.walk_type = (DAV_WALKTYPE_NORMAL
1355                             | DAV_WALKTYPE_HIDDEN
1356                             | DAV_WALKTYPE_POSTFIX);
1357         params.func = dav_fs_delete_walker;
1358         params.pool = info->pool;
1359         params.root = resource;
1360
1361         if ((err = dav_fs_walk(&params, DAV_INFINITY,
1362                                &multi_status)) != NULL) {
1363             /* on a "real" error, then just punt. nothing else to do. */
1364             return err;
1365         }
1366
1367         if ((*response = multi_status) != NULL) {
1368             /* some multistatus responses exist. wrap them in a 207 */
1369             return dav_new_error(info->pool, HTTP_MULTI_STATUS, 0,
1370                                  "Error(s) occurred on some resources during "
1371                                  "the deletion process.");
1372         }
1373
1374         /* no errors... update resource state */
1375         resource->exists = 0;
1376         resource->collection = 0;
1377
1378         return NULL;
1379     }
1380
1381     /* not a collection; remove the file and its properties */
1382     if (apr_file_remove(info->pathname, info->pool) != APR_SUCCESS) {
1383         /* ### put a description in here */
1384         return dav_new_error(info->pool, HTTP_FORBIDDEN, 0, NULL);
1385     }
1386
1387     /* update resource state */
1388     resource->exists = 0;
1389     resource->collection = 0;
1390
1391     /* remove properties and return its result */
1392     return dav_fs_deleteset(info->pool, resource);
1393 }
1394
1395 /* ### move this to dav_util? */
1396 /* Walk recursively down through directories, *
1397  * including lock-null resources as we go.    */
1398 static dav_error * dav_fs_walker(dav_fs_walker_context *fsctx, int depth)
1399 {
1400     const dav_walk_params *params = fsctx->params;
1401     apr_pool_t *pool = params->pool;
1402     dav_error *err = NULL;
1403     int isdir = fsctx->res1.collection;
1404     apr_finfo_t dirent;
1405     apr_dir_t *dirp;
1406
1407     /* ensure the context is prepared properly, then call the func */
1408     err = (*params->func)(&fsctx->wres,
1409                           isdir
1410                           ? DAV_CALLTYPE_COLLECTION
1411                           : DAV_CALLTYPE_MEMBER);
1412     if (err != NULL) {
1413         return err;
1414     }
1415
1416     if (depth == 0 || !isdir) {
1417         return NULL;
1418     }
1419
1420     /* put a trailing slash onto the directory, in preparation for appending
1421      * files to it as we discovery them within the directory */
1422     dav_check_bufsize(pool, &fsctx->path1, DAV_BUFFER_PAD);
1423     fsctx->path1.buf[fsctx->path1.cur_len++] = '/';
1424     fsctx->path1.buf[fsctx->path1.cur_len] = '\0';        /* in pad area */
1425
1426     /* if a secondary path is present, then do that, too */
1427     if (fsctx->path2.buf != NULL) {
1428         dav_check_bufsize(pool, &fsctx->path2, DAV_BUFFER_PAD);
1429         fsctx->path2.buf[fsctx->path2.cur_len++] = '/';
1430         fsctx->path2.buf[fsctx->path2.cur_len] = '\0';        /* in pad area */
1431     }
1432
1433     /* Note: the URI should ALREADY have a trailing "/" */
1434
1435     /* for this first pass of files, all resources exist */
1436     fsctx->res1.exists = 1;
1437
1438     /* a file is the default; we'll adjust if we hit a directory */
1439     fsctx->res1.collection = 0;
1440     fsctx->res2.collection = 0;
1441
1442     /* open and scan the directory */
1443     if ((apr_dir_open(&dirp, fsctx->path1.buf, pool)) != APR_SUCCESS) {
1444         /* ### need a better error */
1445         return dav_new_error(pool, HTTP_NOT_FOUND, 0, NULL);
1446     }
1447     while ((apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp)) == APR_SUCCESS) {
1448         apr_size_t len;
1449         apr_status_t status;
1450
1451         len = strlen(dirent.name);
1452
1453         /* avoid recursing into our current, parent, or state directories */
1454         if (dirent.name[0] == '.' 
1455               && (len == 1 || (dirent.name[1] == '.' && len == 2))) {
1456             continue;
1457         }
1458
1459         if (params->walk_type & DAV_WALKTYPE_AUTH) {
1460             /* ### need to authorize each file */
1461             /* ### example: .htaccess is normally configured to fail auth */
1462
1463             /* stuff in the state directory is never authorized! */
1464             if (!strcmp(dirent.name, DAV_FS_STATE_DIR)) {
1465                 continue;
1466             }
1467         }
1468         /* skip the state dir unless a HIDDEN is performed */
1469         if (!(params->walk_type & DAV_WALKTYPE_HIDDEN)
1470             && !strcmp(dirent.name, DAV_FS_STATE_DIR)) {
1471             continue;
1472         }
1473
1474         /* append this file onto the path buffer (copy null term) */
1475         dav_buffer_place_mem(pool, &fsctx->path1, dirent.name, len + 1, 0);
1476
1477
1478         /* ### Optimize me, dirent can give us what we need! */
1479         status = apr_stat(&fsctx->info1.finfo, fsctx->path1.buf, 
1480                           APR_FINFO_NORM | APR_FINFO_LINK, pool);
1481         if (status != APR_SUCCESS && status != APR_INCOMPLETE) {
1482             /* woah! where'd it go? */
1483             /* ### should have a better error here */
1484             err = dav_new_error(pool, HTTP_NOT_FOUND, 0, NULL);
1485             break;
1486         }
1487
1488         /* copy the file to the URI, too. NOTE: we will pad an extra byte
1489            for the trailing slash later. */
1490         dav_buffer_place_mem(pool, &fsctx->uri_buf, dirent.name, len + 1, 1);
1491
1492         /* if there is a secondary path, then do that, too */
1493         if (fsctx->path2.buf != NULL) {
1494             dav_buffer_place_mem(pool, &fsctx->path2, dirent.name, len + 1, 0);
1495         }
1496
1497         /* set up the (internal) pathnames for the two resources */
1498         fsctx->info1.pathname = fsctx->path1.buf;
1499         fsctx->info2.pathname = fsctx->path2.buf;
1500
1501         /* set up the URI for the current resource */
1502         fsctx->res1.uri = fsctx->uri_buf.buf;
1503
1504         /* ### for now, only process regular files (e.g. skip symlinks) */
1505         if (fsctx->info1.finfo.filetype == APR_REG) {
1506             /* call the function for the specified dir + file */
1507             if ((err = (*params->func)(&fsctx->wres,
1508                                        DAV_CALLTYPE_MEMBER)) != NULL) {
1509                 /* ### maybe add a higher-level description? */
1510                 break;
1511             }
1512         }
1513         else if (fsctx->info1.finfo.filetype == APR_DIR) {
1514             apr_size_t save_path_len = fsctx->path1.cur_len;
1515             apr_size_t save_uri_len = fsctx->uri_buf.cur_len;
1516             apr_size_t save_path2_len = fsctx->path2.cur_len;
1517
1518             /* adjust length to incorporate the subdir name */
1519             fsctx->path1.cur_len += len;
1520             fsctx->path2.cur_len += len;
1521
1522             /* adjust URI length to incorporate subdir and a slash */
1523             fsctx->uri_buf.cur_len += len + 1;
1524             fsctx->uri_buf.buf[fsctx->uri_buf.cur_len - 1] = '/';
1525             fsctx->uri_buf.buf[fsctx->uri_buf.cur_len] = '\0';
1526
1527             /* switch over to a collection */
1528             fsctx->res1.collection = 1;
1529             fsctx->res2.collection = 1;
1530
1531             /* recurse on the subdir */
1532             /* ### don't always want to quit on error from single child */
1533             if ((err = dav_fs_walker(fsctx, depth - 1)) != NULL) {
1534                 /* ### maybe add a higher-level description? */
1535                 break;
1536             }
1537
1538             /* put the various information back */
1539             fsctx->path1.cur_len = save_path_len;
1540             fsctx->path2.cur_len = save_path2_len;
1541             fsctx->uri_buf.cur_len = save_uri_len;
1542
1543             fsctx->res1.collection = 0;
1544             fsctx->res2.collection = 0;
1545
1546             /* assert: res1.exists == 1 */
1547         }
1548     }
1549
1550     /* ### check the return value of this? */
1551     apr_dir_close(dirp);
1552
1553     if (err != NULL)
1554         return err;
1555
1556     if (params->walk_type & DAV_WALKTYPE_LOCKNULL) {
1557         apr_size_t offset = 0;
1558
1559         /* null terminate the directory name */
1560         fsctx->path1.buf[fsctx->path1.cur_len - 1] = '\0';
1561
1562         /* Include any lock null resources found in this collection */
1563         fsctx->res1.collection = 1;
1564         if ((err = dav_fs_get_locknull_members(&fsctx->res1,
1565                                                &fsctx->locknull_buf)) != NULL) {
1566             /* ### maybe add a higher-level description? */
1567             return err;
1568         }
1569
1570         /* put a slash back on the end of the directory */
1571         fsctx->path1.buf[fsctx->path1.cur_len - 1] = '/';
1572
1573         /* these are all non-existant (files) */
1574         fsctx->res1.exists = 0;
1575         fsctx->res1.collection = 0;
1576         memset(&fsctx->info1.finfo, 0, sizeof(fsctx->info1.finfo));
1577
1578         while (offset < fsctx->locknull_buf.cur_len) {
1579             apr_size_t len = strlen(fsctx->locknull_buf.buf + offset);
1580             dav_lock *locks = NULL;
1581
1582             /*
1583             ** Append the locknull file to the paths and the URI. Note that
1584             ** we don't have to pad the URI for a slash since a locknull
1585             ** resource is not a collection.
1586             */
1587             dav_buffer_place_mem(pool, &fsctx->path1,
1588                                  fsctx->locknull_buf.buf + offset, len + 1, 0);
1589             dav_buffer_place_mem(pool, &fsctx->uri_buf,
1590                                  fsctx->locknull_buf.buf + offset, len + 1, 0);
1591             if (fsctx->path2.buf != NULL) {
1592                 dav_buffer_place_mem(pool, &fsctx->path2,
1593                                      fsctx->locknull_buf.buf + offset,
1594                                      len + 1, 0);
1595             }
1596
1597             /* set up the (internal) pathnames for the two resources */
1598             fsctx->info1.pathname = fsctx->path1.buf;
1599             fsctx->info2.pathname = fsctx->path2.buf;
1600
1601             /* set up the URI for the current resource */
1602             fsctx->res1.uri = fsctx->uri_buf.buf;
1603
1604             /*
1605             ** To prevent a PROPFIND showing an expired locknull
1606             ** resource, query the lock database to force removal
1607             ** of both the lock entry and .locknull, if necessary..
1608             ** Sure, the query in PROPFIND would do this.. after
1609             ** the locknull resource was already included in the 
1610             ** return.
1611             **
1612             ** NOTE: we assume the caller has opened the lock database
1613             **       if they have provided DAV_WALKTYPE_LOCKNULL.
1614             */
1615             /* ### we should also look into opening it read-only and
1616                ### eliding timed-out items from the walk, yet leaving
1617                ### them in the locknull database until somebody opens
1618                ### the thing writable.
1619                */
1620             /* ### probably ought to use has_locks. note the problem
1621                ### mentioned above, though... we would traverse this as
1622                ### a locknull, but then a PROPFIND would load the lock
1623                ### info, causing a timeout and the locks would not be
1624                ### reported. Therefore, a null resource would be returned
1625                ### in the PROPFIND.
1626                ###
1627                ### alternative: just load unresolved locks. any direct
1628                ### locks will be timed out (correct). any indirect will
1629                ### not (correct; consider if a parent timed out -- the
1630                ### timeout routines do not walk and remove indirects;
1631                ### even the resolve func would probably fail when it
1632                ### tried to find a timed-out direct lock).
1633             */
1634             if ((err = dav_lock_query(params->lockdb, &fsctx->res1,
1635                                       &locks)) != NULL) {
1636                 /* ### maybe add a higher-level description? */
1637                 return err;
1638             }
1639
1640             /* call the function for the specified dir + file */
1641             if (locks != NULL &&
1642                 (err = (*params->func)(&fsctx->wres,
1643                                        DAV_CALLTYPE_LOCKNULL)) != NULL) {
1644                 /* ### maybe add a higher-level description? */
1645                 return err;
1646             }
1647
1648             offset += len + 1;
1649         }
1650
1651         /* reset the exists flag */
1652         fsctx->res1.exists = 1;
1653     }
1654
1655     if (params->walk_type & DAV_WALKTYPE_POSTFIX) {
1656         /* replace the dirs' trailing slashes with null terms */
1657         fsctx->path1.buf[--fsctx->path1.cur_len] = '\0';
1658         fsctx->uri_buf.buf[--fsctx->uri_buf.cur_len] = '\0';
1659         if (fsctx->path2.buf != NULL) {
1660             fsctx->path2.buf[--fsctx->path2.cur_len] = '\0';
1661         }
1662
1663         /* this is a collection which exists */
1664         fsctx->res1.collection = 1;
1665
1666         return (*params->func)(&fsctx->wres, DAV_CALLTYPE_POSTFIX);
1667     }
1668
1669     return NULL;
1670 }
1671
1672 static dav_error * dav_fs_internal_walk(const dav_walk_params *params,
1673                                         int depth, int is_move,
1674                                         const dav_resource *root_dst,
1675                                         dav_response **response)
1676 {
1677     dav_fs_walker_context fsctx = { 0 };
1678     dav_error *err;
1679     dav_fs_copymove_walk_ctx cm_ctx = { 0 };
1680
1681 #if DAV_DEBUG
1682     if ((params->walk_type & DAV_WALKTYPE_LOCKNULL) != 0
1683         && params->lockdb == NULL) {
1684         return dav_new_error(params->pool, HTTP_INTERNAL_SERVER_ERROR, 0,
1685                              "DESIGN ERROR: walker called to walk locknull "
1686                              "resources, but a lockdb was not provided.");
1687     }
1688 #endif
1689
1690     fsctx.params = params;
1691     fsctx.wres.walk_ctx = params->walk_ctx;
1692     fsctx.wres.pool = params->pool;
1693
1694     /* ### zero out versioned, working, baselined? */
1695
1696     fsctx.res1 = *params->root;
1697     fsctx.res1.pool = params->pool;
1698
1699     fsctx.res1.info = &fsctx.info1;
1700     fsctx.info1 = *params->root->info;
1701
1702     /* the pathname is stored in the path1 buffer */
1703     dav_buffer_init(params->pool, &fsctx.path1, fsctx.info1.pathname);
1704     fsctx.info1.pathname = fsctx.path1.buf;
1705
1706     if (root_dst != NULL) {
1707         /* internal call from the COPY/MOVE code. set it up. */
1708
1709         fsctx.wres.walk_ctx = &cm_ctx;
1710         cm_ctx.is_move = is_move;
1711         cm_ctx.res_dst = &fsctx.res2;
1712         cm_ctx.root = params->root;
1713         cm_ctx.pool = params->pool;
1714
1715         fsctx.res2 = *root_dst;
1716         fsctx.res2.exists = 0;
1717         fsctx.res2.collection = 0;
1718         fsctx.res2.uri = NULL;          /* we don't track this */
1719         fsctx.res2.pool = params->pool;
1720
1721         fsctx.res2.info = &fsctx.info2;
1722         fsctx.info2 = *root_dst->info;
1723
1724         /* res2 does not exist -- clear its finfo structure */
1725         memset(&fsctx.info2.finfo, 0, sizeof(fsctx.info2.finfo));
1726
1727         /* the pathname is stored in the path2 buffer */
1728         dav_buffer_init(params->pool, &fsctx.path2, fsctx.info2.pathname);
1729         fsctx.info2.pathname = fsctx.path2.buf;
1730     }
1731
1732     /* prep the URI buffer */
1733     dav_buffer_init(params->pool, &fsctx.uri_buf, params->root->uri);
1734
1735     /* if we have a directory, then ensure the URI has a trailing "/" */
1736     if (fsctx.res1.collection
1737         && fsctx.uri_buf.buf[fsctx.uri_buf.cur_len - 1] != '/') {
1738
1739         /* this will fall into the pad area */
1740         fsctx.uri_buf.buf[fsctx.uri_buf.cur_len++] = '/';
1741         fsctx.uri_buf.buf[fsctx.uri_buf.cur_len] = '\0';
1742     }
1743
1744     /* the current resource's URI is stored in the uri_buf buffer */
1745     fsctx.res1.uri = fsctx.uri_buf.buf;
1746
1747     /* point the callback's resource at our structure */
1748     fsctx.wres.resource = &fsctx.res1;
1749
1750     /* always return the error, and any/all multistatus responses */
1751     err = dav_fs_walker(&fsctx, depth);
1752     *response = fsctx.wres.response;
1753     return err;
1754 }
1755
1756 static dav_error * dav_fs_walk(const dav_walk_params *params, int depth,
1757                                dav_response **response)
1758 {
1759     /* always return the error, and any/all multistatus responses */
1760     return dav_fs_internal_walk(params, depth, 0, NULL, response);
1761 }
1762
1763 /* dav_fs_etag:  Stolen from ap_make_etag.  Creates a strong etag
1764  *    for file path.
1765  * ### do we need to return weak tags sometimes?
1766  */
1767 static const char *dav_fs_getetag(const dav_resource *resource)
1768 {
1769     dav_resource_private *ctx = resource->info;
1770
1771     if (!resource->exists) 
1772         return apr_pstrdup(ctx->pool, "");
1773
1774     if (ctx->finfo.filetype != 0) {
1775         return apr_psprintf(ctx->pool, "\"%lx-%lx-%lx\"",
1776                            (unsigned long) ctx->finfo.inode,
1777                            (unsigned long) ctx->finfo.size,
1778                            (unsigned long) ctx->finfo.mtime);
1779     }
1780
1781     return apr_psprintf(ctx->pool, "\"%lx\"", (unsigned long) ctx->finfo.mtime);
1782 }
1783
1784 static const dav_hooks_repository dav_hooks_repository_fs =
1785 {
1786     DEBUG_GET_HANDLER,   /* normally: special GET handling not required */
1787     dav_fs_get_resource,
1788     dav_fs_get_parent_resource,
1789     dav_fs_is_same_resource,
1790     dav_fs_is_parent_resource,
1791     dav_fs_open_stream,
1792     dav_fs_close_stream,
1793     dav_fs_write_stream,
1794     dav_fs_seek_stream,
1795 #if DEBUG_GET_HANDLER
1796     dav_fs_set_headers,
1797     dav_fs_deliver,
1798 #else
1799     NULL,
1800     NULL,
1801 #endif
1802     dav_fs_create_collection,
1803     dav_fs_copy_resource,
1804     dav_fs_move_resource,
1805     dav_fs_remove_resource,
1806     dav_fs_walk,
1807     dav_fs_getetag,
1808 };
1809
1810 static dav_prop_insert dav_fs_insert_prop(const dav_resource *resource,
1811                                           int propid, dav_prop_insert what,
1812                                           apr_text_header *phdr)
1813 {
1814     const char *value;
1815     const char *s;
1816     apr_pool_t *p = resource->info->pool;
1817     const dav_liveprop_spec *info;
1818     int global_ns;
1819
1820     /* an HTTP-date can be 29 chars plus a null term */
1821     /* a 64-bit size can be 20 chars plus a null term */
1822     char buf[DAV_TIMEBUF_SIZE];
1823
1824     /*
1825     ** None of FS provider properties are defined if the resource does not
1826     ** exist. Just bail for this case.
1827     **
1828     ** Even though we state that the FS properties are not defined, the
1829     ** client cannot store dead values -- we deny that thru the is_writable
1830     ** hook function.
1831     */
1832     if (!resource->exists)
1833         return DAV_PROP_INSERT_NOTDEF;
1834
1835     switch (propid) {
1836     case DAV_PROPID_creationdate:
1837         /*
1838         ** Closest thing to a creation date. since we don't actually
1839         ** perform the operations that would modify ctime (after we
1840         ** create the file), then we should be pretty safe here.
1841         */
1842         dav_format_time(DAV_STYLE_ISO8601,
1843                         resource->info->finfo.ctime,
1844                         buf);
1845         value = buf;
1846         break;
1847
1848     case DAV_PROPID_getcontentlength:
1849         /* our property, but not defined on collection resources */
1850         if (resource->collection)
1851             return DAV_PROP_INSERT_NOTDEF;
1852
1853         (void) sprintf(buf, "%" APR_OFF_T_FMT, resource->info->finfo.size);
1854         value = buf;
1855         break;
1856
1857     case DAV_PROPID_getetag:
1858         value = dav_fs_getetag(resource);
1859         break;
1860
1861     case DAV_PROPID_getlastmodified:
1862         dav_format_time(DAV_STYLE_RFC822,
1863                         resource->info->finfo.mtime,
1864                         buf);
1865         value = buf;
1866         break;
1867
1868     case DAV_PROPID_FS_executable:
1869         /* our property, but not defined on collection resources */
1870         if (resource->collection)
1871             return DAV_PROP_INSERT_NOTDEF;
1872
1873         /* our property, but not defined on this platform */
1874         if (!(resource->info->finfo.valid & APR_FINFO_UPROT))
1875             return DAV_PROP_INSERT_NOTDEF;
1876
1877         /* the files are "ours" so we only need to check owner exec privs */
1878         if (resource->info->finfo.protection & APR_UEXECUTE)
1879             value = "T";
1880         else
1881             value = "F";
1882         break;
1883
1884     default:
1885         /* ### what the heck was this property? */
1886         return DAV_PROP_INSERT_NOTDEF;
1887     }
1888
1889     /* assert: value != NULL */
1890
1891     /* get the information and global NS index for the property */
1892     global_ns = dav_get_liveprop_info(propid, &dav_fs_liveprop_group, &info);
1893
1894     /* assert: info != NULL && info->name != NULL */
1895
1896     /* DBG3("FS: inserting lp%d:%s  (local %d)", ns, scan->name, scan->ns); */
1897
1898     if (what == DAV_PROP_INSERT_VALUE) {
1899         s = apr_psprintf(p, "<lp%d:%s>%s</lp%d:%s>" DEBUG_CR,
1900                          global_ns, info->name, value, global_ns, info->name);
1901     }
1902     else if (what == DAV_PROP_INSERT_NAME) {
1903         s = apr_psprintf(p, "<lp%d:%s/>" DEBUG_CR, global_ns, info->name);
1904     }
1905     else {
1906         /* assert: what == DAV_PROP_INSERT_SUPPORTED */
1907         s = apr_psprintf(p,
1908                          "<D:supported-live-property D:name=\"%s\" "
1909                          "D:namespace=\"%s\"/>" DEBUG_CR,
1910                          info->name, dav_fs_namespace_uris[info->ns]);
1911     }
1912     apr_text_append(p, phdr, s);
1913
1914     /* we inserted what was asked for */
1915     return what;
1916 }
1917
1918 static int dav_fs_is_writable(const dav_resource *resource, int propid)
1919 {
1920     const dav_liveprop_spec *info;
1921
1922 #ifdef DAV_FS_HAS_EXECUTABLE
1923     /* if we have the executable property, and this isn't a collection,
1924        then the property is writable. */
1925     if (propid == DAV_PROPID_FS_executable && !resource->collection)
1926         return 1;
1927 #endif
1928
1929     (void) dav_get_liveprop_info(propid, &dav_fs_liveprop_group, &info);
1930     return info->is_writable;
1931 }
1932
1933 static dav_error *dav_fs_patch_validate(const dav_resource *resource,
1934                                         const apr_xml_elem *elem,
1935                                         int operation,
1936                                         void **context,
1937                                         int *defer_to_dead)
1938 {
1939     const apr_text *cdata;
1940     const apr_text *f_cdata;
1941     char value;
1942     dav_elem_private *priv = elem->priv;
1943
1944     if (priv->propid != DAV_PROPID_FS_executable) {
1945         *defer_to_dead = 1;
1946         return NULL;
1947     }
1948
1949     if (operation == DAV_PROP_OP_DELETE) {
1950         return dav_new_error(resource->info->pool, HTTP_CONFLICT, 0,
1951                              "The 'executable' property cannot be removed.");
1952     }
1953
1954     cdata = elem->first_cdata.first;
1955
1956     /* ### hmm. this isn't actually looking at all the possible text items */
1957     f_cdata = elem->first_child == NULL
1958         ? NULL
1959         : elem->first_child->following_cdata.first;
1960
1961     /* DBG3("name=%s  cdata=%s  f_cdata=%s",elem->name,cdata ? cdata->text : "[null]",f_cdata ? f_cdata->text : "[null]"); */
1962
1963     if (cdata == NULL) {
1964         if (f_cdata == NULL) {
1965             return dav_new_error(resource->info->pool, HTTP_CONFLICT, 0,
1966                                  "The 'executable' property expects a single "
1967                                  "character, valued 'T' or 'F'. There was no "
1968                                  "value submitted.");
1969         }
1970         cdata = f_cdata;
1971     }
1972     else if (f_cdata != NULL)
1973         goto too_long;
1974
1975     if (cdata->next != NULL || strlen(cdata->text) != 1)
1976         goto too_long;
1977
1978     value = cdata->text[0];
1979     if (value != 'T' && value != 'F') {
1980         return dav_new_error(resource->info->pool, HTTP_CONFLICT, 0,
1981                              "The 'executable' property expects a single "
1982                              "character, valued 'T' or 'F'. The value "
1983                              "submitted is invalid.");
1984     }
1985
1986     *context = (void *)(value == 'T');
1987
1988     return NULL;
1989
1990   too_long:
1991     return dav_new_error(resource->info->pool, HTTP_CONFLICT, 0,
1992                          "The 'executable' property expects a single "
1993                          "character, valued 'T' or 'F'. The value submitted "
1994                          "has too many characters.");
1995
1996 }
1997
1998 static dav_error *dav_fs_patch_exec(const dav_resource *resource,
1999                                     const apr_xml_elem *elem,
2000                                     int operation,
2001                                     void *context,
2002                                     dav_liveprop_rollback **rollback_ctx)
2003 {
2004     int value = context != NULL;
2005     apr_fileperms_t perms = resource->info->finfo.protection;
2006     int old_value = (perms & APR_UEXECUTE) != 0;
2007
2008     /* assert: prop == executable. operation == SET. */
2009
2010     /* don't do anything if there is no change. no rollback info either. */
2011     /* DBG2("new value=%d  (old=%d)", value, old_value); */
2012     if (value == old_value)
2013         return NULL;
2014
2015     perms &= ~APR_UEXECUTE;
2016     if (value)
2017         perms |= APR_UEXECUTE;
2018
2019     if (apr_file_perms_set(resource->info->pathname, perms) != APR_SUCCESS) {
2020         return dav_new_error(resource->info->pool,
2021                              HTTP_INTERNAL_SERVER_ERROR, 0,
2022                              "Could not set the executable flag of the "
2023                              "target resource.");
2024     }
2025
2026     /* update the resource and set up the rollback context */
2027     resource->info->finfo.protection = perms;
2028     *rollback_ctx = (dav_liveprop_rollback *)old_value;
2029
2030     return NULL;
2031 }
2032
2033 static void dav_fs_patch_commit(const dav_resource *resource,
2034                                 int operation,
2035                                 void *context,
2036                                 dav_liveprop_rollback *rollback_ctx)
2037 {
2038     /* nothing to do */
2039 }
2040
2041 static dav_error *dav_fs_patch_rollback(const dav_resource *resource,
2042                                         int operation,
2043                                         void *context,
2044                                         dav_liveprop_rollback *rollback_ctx)
2045 {
2046     apr_fileperms_t perms = resource->info->finfo.protection & ~APR_UEXECUTE;
2047     int value = rollback_ctx != NULL;
2048
2049     /* assert: prop == executable. operation == SET. */
2050
2051     /* restore the executable bit */
2052     if (value)
2053         perms |= APR_UEXECUTE;
2054
2055     if (apr_file_perms_set(resource->info->pathname, perms) != APR_SUCCESS) {
2056         return dav_new_error(resource->info->pool,
2057                              HTTP_INTERNAL_SERVER_ERROR, 0,
2058                              "After a failure occurred, the resource's "
2059                              "executable flag could not be restored.");
2060     }
2061
2062     /* restore the resource's state */
2063     resource->info->finfo.protection = perms;
2064
2065     return NULL;
2066 }
2067
2068
2069 static const dav_hooks_liveprop dav_hooks_liveprop_fs =
2070 {
2071     dav_fs_insert_prop,
2072     dav_fs_is_writable,
2073     dav_fs_namespace_uris,
2074     dav_fs_patch_validate,
2075     dav_fs_patch_exec,
2076     dav_fs_patch_commit,
2077     dav_fs_patch_rollback
2078 };
2079
2080 static const dav_provider dav_fs_provider =
2081 {
2082     &dav_hooks_repository_fs,
2083     &dav_hooks_db_dbm,
2084     &dav_hooks_locks_fs,
2085     NULL,               /* vsn */
2086     NULL,               /* binding */
2087     NULL,               /* search */
2088
2089     NULL                /* ctx */
2090 };
2091
2092 void dav_fs_gather_propsets(apr_array_header_t *uris)
2093 {
2094 #ifdef DAV_FS_HAS_EXECUTABLE
2095     *(const char **)apr_array_push(uris) =
2096         "<http://apache.org/dav/propset/fs/1>";
2097 #endif
2098 }
2099
2100 int dav_fs_find_liveprop(const dav_resource *resource,
2101                          const char *ns_uri, const char *name,
2102                          const dav_hooks_liveprop **hooks)
2103 {
2104     /* don't try to find any liveprops if this isn't "our" resource */
2105     if (resource->hooks != &dav_hooks_repository_fs)
2106         return 0;
2107     return dav_do_find_liveprop(ns_uri, name, &dav_fs_liveprop_group, hooks);
2108 }
2109
2110 void dav_fs_insert_all_liveprops(request_rec *r, const dav_resource *resource,
2111                                  dav_prop_insert what, apr_text_header *phdr)
2112 {
2113     /* don't insert any liveprops if this isn't "our" resource */
2114     if (resource->hooks != &dav_hooks_repository_fs)
2115         return;
2116
2117     if (!resource->exists) {
2118         /* a lock-null resource */
2119         /*
2120         ** ### technically, we should insert empty properties. dunno offhand
2121         ** ### what part of the spec said this, but it was essentially thus:
2122         ** ### "the properties should be defined, but may have no value".
2123         */
2124         return;
2125     }
2126
2127     (void) dav_fs_insert_prop(resource, DAV_PROPID_creationdate,
2128                               what, phdr);
2129     (void) dav_fs_insert_prop(resource, DAV_PROPID_getcontentlength,
2130                               what, phdr);
2131     (void) dav_fs_insert_prop(resource, DAV_PROPID_getlastmodified,
2132                               what, phdr);
2133     (void) dav_fs_insert_prop(resource, DAV_PROPID_getetag,
2134                               what, phdr);
2135
2136 #ifdef DAV_FS_HAS_EXECUTABLE
2137     /* Only insert this property if it is defined for this platform. */
2138     (void) dav_fs_insert_prop(resource, DAV_PROPID_FS_executable,
2139                               what, phdr);
2140 #endif
2141
2142     /* ### we know the others aren't defined as liveprops */
2143 }
2144
2145 void dav_fs_register(apr_pool_t *p)
2146 {
2147     /* register the namespace URIs */
2148     dav_register_liveprop_group(p, &dav_fs_liveprop_group);
2149
2150     /* register the repository provider */
2151     dav_register_provider(p, "filesystem", &dav_fs_provider);
2152 }