1 /* Copyright 2000-2004 The Apache Software Foundation
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ** DAV filesystem-based repository provider
21 #include "apr_file_io.h"
22 #include "apr_strings.h"
23 #include "apr_buckets.h"
26 #include <stdio.h> /* for sprintf() */
31 #include "http_protocol.h" /* for ap_set_* (in dav_fs_set_headers) */
32 #include "http_request.h" /* for ap_update_mtime() */
38 /* to assist in debugging mod_dav's GET handling */
39 #define DEBUG_GET_HANDLER 0
41 #define DAV_FS_COPY_BLOCKSIZE 16384 /* copy 16k at a time */
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 */
50 /* private context for doing a filesystem walk */
52 /* the input walk parameters */
53 const dav_walk_params *params;
55 /* reused as we walk */
56 dav_walk_resource wres;
59 dav_resource_private info1;
63 /* MOVE/COPY need a secondary path */
65 dav_resource_private info2;
68 dav_buffer locknull_buf;
70 } dav_fs_walker_context;
73 int is_move; /* is this a MOVE? */
74 dav_buffer work_buf; /* handy buffer for copymove_file() */
76 /* CALLBACK: this is a secondary resource managed specially for us */
77 const dav_resource *res_dst;
79 /* copied from dav_walk_params (they are invariant across the walk) */
80 const dav_resource *root;
83 } dav_fs_copymove_walk_ctx;
85 /* an internal WALKTYPE to walk hidden files (the .DAV directory) */
86 #define DAV_WALKTYPE_HIDDEN 0x4000
88 /* an internal WALKTYPE to call collections (again) after their contents */
89 #define DAV_WALKTYPE_POSTFIX 0x8000
91 #define DAV_CALLTYPE_POSTFIX 1000 /* a private call type */
94 /* pull this in from the other source file */
95 extern const dav_hooks_locks dav_hooks_locks_fs;
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;
102 ** The namespace URIs that we use. This list and the enumeration must
105 static const char * const dav_fs_namespace_uris[] =
108 "http://apache.org/dav/props/",
113 DAV_FS_URI_DAV, /* the DAV: namespace URI */
114 DAV_FS_URI_MYPROPS /* the namespace URI for our custom props */
118 ** Does this platform support an executable flag?
120 ** ### need a way to portably abstract this query
123 #define DAV_FS_HAS_EXECUTABLE
127 ** The single property that we define (in the DAV_FS_URI_MYPROPS namespace)
129 #define DAV_PROPID_FS_executable 1
131 static const dav_liveprop_spec dav_fs_props[] =
133 /* standard DAV properties */
137 DAV_PROPID_creationdate,
143 DAV_PROPID_getcontentlength,
155 DAV_PROPID_getlastmodified,
159 /* our custom properties */
163 DAV_PROPID_FS_executable,
164 0 /* handled special in dav_fs_is_writable */
170 static const dav_liveprop_group dav_fs_liveprop_group =
173 dav_fs_namespace_uris,
174 &dav_hooks_liveprop_fs
178 /* define the dav_stream structure for our use */
182 const char *pathname; /* we may need to remove it at close time */
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)
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);
198 /* --------------------------------------------------------------------
200 ** PRIVATE REPOSITORY FUNCTIONS
202 apr_pool_t *dav_fs_pool(const dav_resource *resource)
204 return resource->info->pool;
207 const char *dav_fs_pathname(const dav_resource *resource)
209 return resource->info->pathname;
212 dav_error * dav_fs_dir_file_name(
213 const dav_resource *resource,
214 const char **dirpath_p,
215 const char **fname_p)
217 dav_resource_private *ctx = resource->info;
219 if (resource->collection) {
220 *dirpath_p = ctx->pathname;
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;
232 rv = apr_filepath_root(&rootpath, &testpath, 0, ctx->pool);
235 /* remove trailing slash from dirpath, unless it's a root path
237 if ((rv == APR_SUCCESS && testpath && *testpath)
238 || rv == APR_ERELATIVE) {
239 if (dirpath[dirlen - 1] == '/') {
240 dirpath[dirlen - 1] = '\0';
244 /* ###: Looks like a response could be appropriate
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
254 if (rv == APR_SUCCESS || rv == APR_ERELATIVE) {
255 *dirpath_p = dirpath;
257 *fname_p = ctx->pathname + dirlen;
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.");
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)
275 /* ### what to do if fails? */
276 (void) apr_time_exp_gmt(&tms, sec);
278 if (style == DAV_STYLE_ISO8601) {
279 /* ### should we use "-00:00" instead of "Z" ?? */
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);
288 /* RFC 822 date format; as strftime '%a, %d %b %Y %T GMT' */
290 /* 29 chars plus null term */
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],
296 tms.tm_hour, tms.tm_min, tms.tm_sec);
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
302 static dav_error * dav_fs_copymove_file(
307 const apr_finfo_t *src_finfo,
308 const apr_finfo_t *dst_finfo,
311 dav_buffer work_buf = { 0 };
312 apr_file_t *inf = NULL;
313 apr_file_t *outf = NULL;
315 apr_fileperms_t perms;
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");
331 perms = src_finfo->protection;
335 perms = APR_OS_DEFAULT;
338 dav_set_bufsize(p, pbuf, DAV_FS_COPY_BLOCKSIZE);
340 if ((apr_file_open(&inf, src, APR_READ | APR_BINARY, APR_OS_DEFAULT, p))
342 /* ### use something besides 500? */
343 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
344 "Could not open file for reading");
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) {
353 return dav_new_error(p, MAP_IO2HTTP(status), 0,
354 "Could not open file for writing");
358 apr_size_t len = DAV_FS_COPY_BLOCKSIZE;
360 status = apr_file_read(inf, pbuf->buf, &len);
361 if (status != APR_SUCCESS && status != APR_EOF) {
363 apr_file_close(outf);
365 if (apr_file_remove(dst, p) != APR_SUCCESS) {
366 /* ### ACK! Inconsistent state... */
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.");
375 /* ### use something besides 500? */
376 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
377 "Could not read input file");
380 if (status == APR_EOF)
383 /* write any bytes that were read */
384 status = apr_file_write_full(outf, pbuf->buf, len, NULL);
385 if (status != APR_SUCCESS) {
387 apr_file_close(outf);
389 if (apr_file_remove(dst, p) != APR_SUCCESS) {
390 /* ### ACK! Inconsistent state... */
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.");
399 return dav_new_error(p, MAP_IO2HTTP(status), 0,
400 "Could not write output file");
405 apr_file_close(outf);
407 if (is_move && apr_file_remove(src, p) != APR_SUCCESS) {
409 int save_errno = errno; /* save the errno that got us here */
411 if (apr_file_remove(dst, p) != APR_SUCCESS) {
412 /* ### ACK. this creates an inconsistency. do more!? */
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 "
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;
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(
438 const char *src_dir, const char *src_file,
439 const char *dst_dir, const char *dst_file,
442 apr_finfo_t src_finfo; /* finfo for source file */
443 apr_finfo_t dst_state_finfo; /* finfo for STATE directory */
448 /* build the propset pathname for the source file */
449 src = apr_pstrcat(p, src_dir, "/" DAV_FS_STATE_DIR "/", src_file, NULL);
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) {
457 /* build the pathname for the destination state dir */
458 dst = apr_pstrcat(p, dst_dir, "/" DAV_FS_STATE_DIR, NULL);
460 /* ### do we need to deal with the umask? */
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");
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");
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");
489 /* append the target file to the state directory pathname */
490 dst = apr_pstrcat(p, dst, "/", dst_file, NULL);
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.");
503 /* gotta copy (and delete) */
504 return dav_fs_copymove_file(is_move, p, src, dst, NULL, NULL, pbuf);
510 static dav_error *dav_fs_copymoveset(int is_move, apr_pool_t *p,
511 const dav_resource *src,
512 const dav_resource *dst,
516 const char *src_file;
517 const char *src_state1;
518 const char *src_state2;
520 const char *dst_file;
521 const char *dst_state1;
522 const char *dst_state2;
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);
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);
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.");
542 err = dav_fs_copymove_state(is_move, p,
547 if (err == NULL && src_state2 != NULL) {
548 err = dav_fs_copymove_state(is_move, p,
554 /* ### CRAP. inconsistency. */
555 /* ### should perform some cleanup at the target if we still
556 ### have the original files */
558 /* Change the error to reflect the bad server state. */
559 err->status = HTTP_INTERNAL_SERVER_ERROR;
561 "Could not fully copy/move the properties. "
562 "The server is now in an inconsistent state.";
569 static dav_error *dav_fs_deleteset(apr_pool_t *p, const dav_resource *resource)
575 const char *pathname;
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);
583 /* build the propset pathname for the file */
584 pathname = apr_pstrcat(p,
586 "/" DAV_FS_STATE_DIR "/",
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.");
597 if (state2 != NULL) {
598 /* build the propset pathname for the file */
599 pathname = apr_pstrcat(p,
601 "/" DAV_FS_STATE_DIR "/",
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 "
618 /* --------------------------------------------------------------------
620 ** REPOSITORY HOOK FUNCTIONS
623 static dav_error * dav_fs_get_resource(
625 const char *root_dir,
628 dav_resource **result_resource)
630 dav_resource_private *ctx;
631 dav_resource *resource;
636 /* ### optimize this into a single allocation! */
638 /* Create private resource context descriptor */
639 ctx = apr_pcalloc(r->pool, sizeof(*ctx));
640 ctx->finfo = r->finfo;
642 /* ### this should go away */
645 /* Preserve case on OSes which fold canonical filenames */
647 /* ### not available in Apache 2.0 yet */
648 filename = r->case_preserved_filename;
650 filename = r->filename;
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
659 s = apr_pstrcat(r->pool, filename, r->path_info, NULL);
661 /* make sure the pathname does not have a trailing "/" */
663 if (len > 1 && s[len - 1] == '/') {
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;
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);
683 resource->uri = r->uri;
686 if (r->finfo.filetype != 0) {
687 resource->exists = 1;
688 resource->collection = r->finfo.filetype == APR_DIR;
690 /* unused info in the URL will indicate a null resource */
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') {
698 ** This URL/filename represents a locknull resource or
699 ** possibly a destination of a MOVE/COPY
701 resource->exists = 0;
702 resource->collection = 0;
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.
712 return dav_new_error(r->pool, HTTP_BAD_REQUEST, 0,
713 "The URL contains extraneous path "
714 "components. The resource could not "
718 /* retain proper integrity across the structures */
719 if (!resource->exists) {
720 ctx->finfo.filetype = 0;
725 *result_resource = resource;
729 static dav_error * dav_fs_get_parent_resource(const dav_resource *resource,
730 dav_resource **result_parent)
732 dav_resource_private *ctx = resource->info;
733 dav_resource_private *parent_ctx;
734 dav_resource *parent_resource;
737 const char *testroot;
738 const char *testpath;
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;
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.
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;
759 /* ### optimize this into a single allocation! */
761 /* Create private resource context descriptor */
762 parent_ctx = apr_pcalloc(ctx->pool, sizeof(*parent_ctx));
764 /* ### this should go away */
765 parent_ctx->pool = ctx->pool;
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;
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;
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;
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;
791 *result_parent = parent_resource;
795 static int dav_fs_is_same_resource(
796 const dav_resource *res1,
797 const dav_resource *res2)
799 dav_resource_private *ctx1 = res1->info;
800 dav_resource_private *ctx2 = res2->info;
802 if (res1->hooks != res2->hooks)
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;
810 return strcmp(ctx1->pathname, ctx2->pathname) == 0;
814 static int dav_fs_is_parent_resource(
815 const dav_resource *res1,
816 const dav_resource *res2)
818 dav_resource_private *ctx1 = res1->info;
819 dav_resource_private *ctx2 = res2->info;
820 apr_size_t len1 = strlen(ctx1->pathname);
823 if (res1->hooks != res2->hooks)
826 /* it is safe to use ctx2 now */
827 len2 = strlen(ctx2->pathname);
830 && memcmp(ctx1->pathname, ctx2->pathname, len1) == 0
831 && ctx2->pathname[len1] == '/');
834 static dav_error * dav_fs_open_stream(const dav_resource *resource,
835 dav_stream_mode mode,
838 apr_pool_t *p = resource->info->pool;
839 dav_stream *ds = apr_pcalloc(p, sizeof(*ds));
845 flags = APR_READ | APR_BINARY;
848 case DAV_MODE_WRITE_TRUNC:
849 flags = APR_WRITE | APR_CREATE | APR_TRUNCATE | APR_BINARY;
851 case DAV_MODE_WRITE_SEEKABLE:
852 flags = APR_WRITE | APR_CREATE | APR_BINARY;
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.");
864 /* (APR registers cleanups for the fd with the pool) */
870 static dav_error * dav_fs_close_stream(dav_stream *stream, int commit)
872 apr_file_close(stream->f);
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.");
887 static dav_error * dav_fs_write_stream(dav_stream *stream,
888 const void *buf, apr_size_t bufsize)
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 "
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 "
907 static dav_error * dav_fs_seek_stream(dav_stream *stream, apr_off_t abs_pos)
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 "
921 #if DEBUG_GET_HANDLER
923 /* only define set_headers() and deliver() for debug purposes */
926 static dav_error * dav_fs_set_headers(request_rec *r,
927 const dav_resource *resource)
929 /* ### this function isn't really used since we have a get_pathname */
930 if (!resource->exists)
933 /* make sure the proper mtime is in the request record */
934 ap_update_mtime(r, resource->info->finfo.mtime);
936 /* ### note that these use r->filename rather than <resource> */
937 ap_set_last_modified(r);
940 /* we accept byte-ranges */
941 apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
943 /* set up the Content-Length header */
944 ap_set_content_length(r, resource->info->finfo.size);
946 /* ### how to set the content type? */
947 /* ### until this is resolved, the Content-Type header is busted */
952 static dav_error * dav_fs_deliver(const dav_resource *resource,
955 apr_pool_t *pool = resource->pool;
956 apr_bucket_brigade *bb;
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.");
968 if (resource->collection) {
969 return dav_new_error(pool, HTTP_CONFLICT, 0,
970 "There is no default response to GET for a "
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.");
981 bb = apr_brigade_create(pool, output->c->bucket_alloc);
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);
989 bkt = apr_bucket_eos_create(output->c->bucket_alloc);
990 APR_BRIGADE_INSERT_TAIL(bb, bkt);
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.");
1000 #endif /* DEBUG_GET_HANDLER */
1003 static dav_error * dav_fs_create_collection(dav_resource *resource)
1005 dav_resource_private *ctx = resource->info;
1006 apr_status_t status;
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.");
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.");
1020 /* update resource state to show it exists as a collection */
1021 resource->exists = 1;
1022 resource->collection = 1;
1027 static dav_error * dav_fs_copymove_walker(dav_walk_resource *wres,
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;
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.
1040 /* ### we are ignoring any error here; what should we do? */
1041 (void) apr_dir_remove(srcinfo->pathname, ctx->pool);
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);
1054 err = dav_fs_copymove_file(ctx->is_move, ctx->pool,
1055 srcinfo->pathname, dstinfo->pathname,
1057 ctx->res_dst->exists ? &dstinfo->finfo : NULL,
1059 /* ### push a higher-level description? */
1063 ** If we have a "not so bad" error, then it might need to go into a
1064 ** multistatus response.
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
1071 ** For a COPY, we are traversing in a prefix fashion. If the root fails,
1072 ** then we can just bail out now.
1075 && !ap_is_HTTP_SERVER_ERROR(err->status)
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);
1081 /* the error is in the multistatus now. do not stop the traversal. */
1088 static dav_error *dav_fs_copymove_resource(
1090 const dav_resource *src,
1091 const dav_resource *dst,
1093 dav_response **response)
1095 dav_error *err = NULL;
1096 dav_buffer work_buf = { 0 };
1100 /* if a collection, recursively copy/move it and its children,
1101 * including the state dirs
1103 if (src->collection) {
1104 dav_walk_params params = { 0 };
1105 dav_response *multi_status;
1107 params.walk_type = DAV_WALKTYPE_NORMAL | DAV_WALKTYPE_HIDDEN;
1108 params.func = dav_fs_copymove_walker;
1109 params.pool = src->info->pool;
1112 /* params.walk_ctx is managed by dav_fs_internal_walk() */
1114 /* postfix is needed for MOVE to delete source dirs */
1116 params.walk_type |= DAV_WALKTYPE_POSTFIX;
1118 /* note that we return the error OR the multistatus. never both */
1120 if ((err = dav_fs_internal_walk(¶ms, depth, is_move, dst,
1121 &multi_status)) != NULL) {
1122 /* on a "real" error, then just punt. nothing else to do. */
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.");
1136 /* not a collection */
1137 if ((err = dav_fs_copymove_file(is_move, src->info->pool,
1138 src->info->pathname, dst->info->pathname,
1140 dst->exists ? &dst->info->finfo : NULL,
1141 &work_buf)) != NULL) {
1142 /* ### push a higher-level description? */
1146 /* copy/move properties as well */
1147 return dav_fs_copymoveset(is_move, src->info->pool, src, dst, &work_buf);
1150 static dav_error * dav_fs_copy_resource(
1151 const dav_resource *src,
1154 dav_response **response)
1159 if (src->hooks != dst->hooks) {
1161 ** ### strictly speaking, this is a design error; we should not
1162 ** ### have reached this point.
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.");
1170 if ((err = dav_fs_copymove_resource(0, src, dst, depth,
1171 response)) == NULL) {
1173 /* update state of destination resource to show it exists */
1175 dst->collection = src->collection;
1181 static dav_error * dav_fs_move_resource(
1184 dav_response **response)
1186 dav_resource_private *srcinfo = src->info;
1187 dav_resource_private *dstinfo = dst->info;
1192 if (src->hooks != dst->hooks) {
1194 ** ### strictly speaking, this is a design error; we should not
1195 ** ### have reached this point.
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.");
1203 /* determine whether a simple rename will work.
1204 * Assume source exists, else we wouldn't get called.
1206 if (dstinfo->finfo.filetype != 0) {
1207 if (dstinfo->finfo.device == srcinfo->finfo.device) {
1208 /* target exists and is on the same device. */
1213 const char *dirpath;
1217 /* destination does not exist, but the parent directory should,
1220 dirpath = ap_make_dirstr_parent(dstinfo->pool, dstinfo->pathname);
1222 * XXX: If missing dev ... then what test?
1223 * Really need a try and failover for those platforms.
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)) {
1234 /* if we can't simply rename, then do it the hard way... */
1236 if ((err = dav_fs_copymove_resource(1, src, dst, DAV_INFINITY,
1237 response)) == NULL) {
1238 /* update resource states */
1240 dst->collection = src->collection;
1242 src->collection = 0;
1248 /* a rename should work. do it, and move properties as well */
1250 /* no multistatus response */
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.");
1261 /* update resource states */
1263 dst->collection = src->collection;
1265 src->collection = 0;
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. */
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.",
1287 /* update resource states again */
1289 src->collection = dst->collection;
1291 dst->collection = 0;
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.",
1305 static dav_error * dav_fs_delete_walker(dav_walk_resource *wres, int calltype)
1307 dav_resource_private *info = wres->resource->info;
1309 /* do not attempt to remove a null resource,
1310 * or a collection with children
1312 if (wres->resource->exists &&
1313 (!wres->resource->collection || calltype == DAV_CALLTYPE_POSTFIX)) {
1314 /* try to remove the resource */
1315 apr_status_t result;
1317 result = wres->resource->collection
1318 ? apr_dir_remove(info->pathname, wres->pool)
1319 : apr_file_remove(info->pathname, wres->pool);
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.
1326 ** (also: remember we are deleting via a postfix traversal)
1328 if (result != APR_SUCCESS) {
1329 /* ### assume there is a permissions problem */
1331 /* ### use errno to generate DAV:responsedescription? */
1332 dav_add_response(wres, HTTP_FORBIDDEN, NULL);
1339 static dav_error * dav_fs_remove_resource(dav_resource *resource,
1340 dav_response **response)
1342 dav_resource_private *info = resource->info;
1346 /* if a collection, recursively remove it and its children,
1347 * including the state dirs
1349 if (resource->collection) {
1350 dav_walk_params params = { 0 };
1351 dav_error *err = NULL;
1352 dav_response *multi_status;
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;
1361 if ((err = dav_fs_walk(¶ms, DAV_INFINITY,
1362 &multi_status)) != NULL) {
1363 /* on a "real" error, then just punt. nothing else to do. */
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.");
1374 /* no errors... update resource state */
1375 resource->exists = 0;
1376 resource->collection = 0;
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);
1387 /* update resource state */
1388 resource->exists = 0;
1389 resource->collection = 0;
1391 /* remove properties and return its result */
1392 return dav_fs_deleteset(info->pool, resource);
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)
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;
1407 /* ensure the context is prepared properly, then call the func */
1408 err = (*params->func)(&fsctx->wres,
1410 ? DAV_CALLTYPE_COLLECTION
1411 : DAV_CALLTYPE_MEMBER);
1416 if (depth == 0 || !isdir) {
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 */
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 */
1433 /* Note: the URI should ALREADY have a trailing "/" */
1435 /* for this first pass of files, all resources exist */
1436 fsctx->res1.exists = 1;
1438 /* a file is the default; we'll adjust if we hit a directory */
1439 fsctx->res1.collection = 0;
1440 fsctx->res2.collection = 0;
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);
1447 while ((apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp)) == APR_SUCCESS) {
1449 apr_status_t status;
1451 len = strlen(dirent.name);
1453 /* avoid recursing into our current, parent, or state directories */
1454 if (dirent.name[0] == '.'
1455 && (len == 1 || (dirent.name[1] == '.' && len == 2))) {
1459 if (params->walk_type & DAV_WALKTYPE_AUTH) {
1460 /* ### need to authorize each file */
1461 /* ### example: .htaccess is normally configured to fail auth */
1463 /* stuff in the state directory is never authorized! */
1464 if (!strcmp(dirent.name, DAV_FS_STATE_DIR)) {
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)) {
1474 /* append this file onto the path buffer (copy null term) */
1475 dav_buffer_place_mem(pool, &fsctx->path1, dirent.name, len + 1, 0);
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);
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);
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);
1497 /* set up the (internal) pathnames for the two resources */
1498 fsctx->info1.pathname = fsctx->path1.buf;
1499 fsctx->info2.pathname = fsctx->path2.buf;
1501 /* set up the URI for the current resource */
1502 fsctx->res1.uri = fsctx->uri_buf.buf;
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? */
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;
1518 /* adjust length to incorporate the subdir name */
1519 fsctx->path1.cur_len += len;
1520 fsctx->path2.cur_len += len;
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';
1527 /* switch over to a collection */
1528 fsctx->res1.collection = 1;
1529 fsctx->res2.collection = 1;
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? */
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;
1543 fsctx->res1.collection = 0;
1544 fsctx->res2.collection = 0;
1546 /* assert: res1.exists == 1 */
1550 /* ### check the return value of this? */
1551 apr_dir_close(dirp);
1556 if (params->walk_type & DAV_WALKTYPE_LOCKNULL) {
1557 apr_size_t offset = 0;
1559 /* null terminate the directory name */
1560 fsctx->path1.buf[fsctx->path1.cur_len - 1] = '\0';
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? */
1570 /* put a slash back on the end of the directory */
1571 fsctx->path1.buf[fsctx->path1.cur_len - 1] = '/';
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));
1578 while (offset < fsctx->locknull_buf.cur_len) {
1579 apr_size_t len = strlen(fsctx->locknull_buf.buf + offset);
1580 dav_lock *locks = NULL;
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.
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,
1597 /* set up the (internal) pathnames for the two resources */
1598 fsctx->info1.pathname = fsctx->path1.buf;
1599 fsctx->info2.pathname = fsctx->path2.buf;
1601 /* set up the URI for the current resource */
1602 fsctx->res1.uri = fsctx->uri_buf.buf;
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
1612 ** NOTE: we assume the caller has opened the lock database
1613 ** if they have provided DAV_WALKTYPE_LOCKNULL.
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.
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.
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).
1634 if ((err = dav_lock_query(params->lockdb, &fsctx->res1,
1636 /* ### maybe add a higher-level description? */
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? */
1651 /* reset the exists flag */
1652 fsctx->res1.exists = 1;
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';
1663 /* this is a collection which exists */
1664 fsctx->res1.collection = 1;
1666 return (*params->func)(&fsctx->wres, DAV_CALLTYPE_POSTFIX);
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)
1677 dav_fs_walker_context fsctx = { 0 };
1679 dav_fs_copymove_walk_ctx cm_ctx = { 0 };
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.");
1690 fsctx.params = params;
1691 fsctx.wres.walk_ctx = params->walk_ctx;
1692 fsctx.wres.pool = params->pool;
1694 /* ### zero out versioned, working, baselined? */
1696 fsctx.res1 = *params->root;
1697 fsctx.res1.pool = params->pool;
1699 fsctx.res1.info = &fsctx.info1;
1700 fsctx.info1 = *params->root->info;
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;
1706 if (root_dst != NULL) {
1707 /* internal call from the COPY/MOVE code. set it up. */
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;
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;
1721 fsctx.res2.info = &fsctx.info2;
1722 fsctx.info2 = *root_dst->info;
1724 /* res2 does not exist -- clear its finfo structure */
1725 memset(&fsctx.info2.finfo, 0, sizeof(fsctx.info2.finfo));
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;
1732 /* prep the URI buffer */
1733 dav_buffer_init(params->pool, &fsctx.uri_buf, params->root->uri);
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] != '/') {
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';
1744 /* the current resource's URI is stored in the uri_buf buffer */
1745 fsctx.res1.uri = fsctx.uri_buf.buf;
1747 /* point the callback's resource at our structure */
1748 fsctx.wres.resource = &fsctx.res1;
1750 /* always return the error, and any/all multistatus responses */
1751 err = dav_fs_walker(&fsctx, depth);
1752 *response = fsctx.wres.response;
1756 static dav_error * dav_fs_walk(const dav_walk_params *params, int depth,
1757 dav_response **response)
1759 /* always return the error, and any/all multistatus responses */
1760 return dav_fs_internal_walk(params, depth, 0, NULL, response);
1763 /* dav_fs_etag: Stolen from ap_make_etag. Creates a strong etag
1765 * ### do we need to return weak tags sometimes?
1767 static const char *dav_fs_getetag(const dav_resource *resource)
1769 dav_resource_private *ctx = resource->info;
1771 if (!resource->exists)
1772 return apr_pstrdup(ctx->pool, "");
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);
1781 return apr_psprintf(ctx->pool, "\"%lx\"", (unsigned long) ctx->finfo.mtime);
1784 static const dav_hooks_repository dav_hooks_repository_fs =
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,
1792 dav_fs_close_stream,
1793 dav_fs_write_stream,
1795 #if DEBUG_GET_HANDLER
1802 dav_fs_create_collection,
1803 dav_fs_copy_resource,
1804 dav_fs_move_resource,
1805 dav_fs_remove_resource,
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)
1816 apr_pool_t *p = resource->info->pool;
1817 const dav_liveprop_spec *info;
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];
1825 ** None of FS provider properties are defined if the resource does not
1826 ** exist. Just bail for this case.
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
1832 if (!resource->exists)
1833 return DAV_PROP_INSERT_NOTDEF;
1836 case DAV_PROPID_creationdate:
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.
1842 dav_format_time(DAV_STYLE_ISO8601,
1843 resource->info->finfo.ctime,
1848 case DAV_PROPID_getcontentlength:
1849 /* our property, but not defined on collection resources */
1850 if (resource->collection)
1851 return DAV_PROP_INSERT_NOTDEF;
1853 (void) sprintf(buf, "%" APR_OFF_T_FMT, resource->info->finfo.size);
1857 case DAV_PROPID_getetag:
1858 value = dav_fs_getetag(resource);
1861 case DAV_PROPID_getlastmodified:
1862 dav_format_time(DAV_STYLE_RFC822,
1863 resource->info->finfo.mtime,
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;
1873 /* our property, but not defined on this platform */
1874 if (!(resource->info->finfo.valid & APR_FINFO_UPROT))
1875 return DAV_PROP_INSERT_NOTDEF;
1877 /* the files are "ours" so we only need to check owner exec privs */
1878 if (resource->info->finfo.protection & APR_UEXECUTE)
1885 /* ### what the heck was this property? */
1886 return DAV_PROP_INSERT_NOTDEF;
1889 /* assert: value != NULL */
1891 /* get the information and global NS index for the property */
1892 global_ns = dav_get_liveprop_info(propid, &dav_fs_liveprop_group, &info);
1894 /* assert: info != NULL && info->name != NULL */
1896 /* DBG3("FS: inserting lp%d:%s (local %d)", ns, scan->name, scan->ns); */
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);
1902 else if (what == DAV_PROP_INSERT_NAME) {
1903 s = apr_psprintf(p, "<lp%d:%s/>" DEBUG_CR, global_ns, info->name);
1906 /* assert: what == DAV_PROP_INSERT_SUPPORTED */
1908 "<D:supported-live-property D:name=\"%s\" "
1909 "D:namespace=\"%s\"/>" DEBUG_CR,
1910 info->name, dav_fs_namespace_uris[info->ns]);
1912 apr_text_append(p, phdr, s);
1914 /* we inserted what was asked for */
1918 static int dav_fs_is_writable(const dav_resource *resource, int propid)
1920 const dav_liveprop_spec *info;
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)
1929 (void) dav_get_liveprop_info(propid, &dav_fs_liveprop_group, &info);
1930 return info->is_writable;
1933 static dav_error *dav_fs_patch_validate(const dav_resource *resource,
1934 const apr_xml_elem *elem,
1939 const apr_text *cdata;
1940 const apr_text *f_cdata;
1942 dav_elem_private *priv = elem->priv;
1944 if (priv->propid != DAV_PROPID_FS_executable) {
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.");
1954 cdata = elem->first_cdata.first;
1956 /* ### hmm. this isn't actually looking at all the possible text items */
1957 f_cdata = elem->first_child == NULL
1959 : elem->first_child->following_cdata.first;
1961 /* DBG3("name=%s cdata=%s f_cdata=%s",elem->name,cdata ? cdata->text : "[null]",f_cdata ? f_cdata->text : "[null]"); */
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.");
1972 else if (f_cdata != NULL)
1975 if (cdata->next != NULL || strlen(cdata->text) != 1)
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.");
1986 *context = (void *)(value == 'T');
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.");
1998 static dav_error *dav_fs_patch_exec(const dav_resource *resource,
1999 const apr_xml_elem *elem,
2002 dav_liveprop_rollback **rollback_ctx)
2004 int value = context != NULL;
2005 apr_fileperms_t perms = resource->info->finfo.protection;
2006 int old_value = (perms & APR_UEXECUTE) != 0;
2008 /* assert: prop == executable. operation == SET. */
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)
2015 perms &= ~APR_UEXECUTE;
2017 perms |= APR_UEXECUTE;
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.");
2026 /* update the resource and set up the rollback context */
2027 resource->info->finfo.protection = perms;
2028 *rollback_ctx = (dav_liveprop_rollback *)old_value;
2033 static void dav_fs_patch_commit(const dav_resource *resource,
2036 dav_liveprop_rollback *rollback_ctx)
2041 static dav_error *dav_fs_patch_rollback(const dav_resource *resource,
2044 dav_liveprop_rollback *rollback_ctx)
2046 apr_fileperms_t perms = resource->info->finfo.protection & ~APR_UEXECUTE;
2047 int value = rollback_ctx != NULL;
2049 /* assert: prop == executable. operation == SET. */
2051 /* restore the executable bit */
2053 perms |= APR_UEXECUTE;
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.");
2062 /* restore the resource's state */
2063 resource->info->finfo.protection = perms;
2069 static const dav_hooks_liveprop dav_hooks_liveprop_fs =
2073 dav_fs_namespace_uris,
2074 dav_fs_patch_validate,
2076 dav_fs_patch_commit,
2077 dav_fs_patch_rollback
2080 static const dav_provider dav_fs_provider =
2082 &dav_hooks_repository_fs,
2084 &dav_hooks_locks_fs,
2092 void dav_fs_gather_propsets(apr_array_header_t *uris)
2094 #ifdef DAV_FS_HAS_EXECUTABLE
2095 *(const char **)apr_array_push(uris) =
2096 "<http://apache.org/dav/propset/fs/1>";
2100 int dav_fs_find_liveprop(const dav_resource *resource,
2101 const char *ns_uri, const char *name,
2102 const dav_hooks_liveprop **hooks)
2104 /* don't try to find any liveprops if this isn't "our" resource */
2105 if (resource->hooks != &dav_hooks_repository_fs)
2107 return dav_do_find_liveprop(ns_uri, name, &dav_fs_liveprop_group, hooks);
2110 void dav_fs_insert_all_liveprops(request_rec *r, const dav_resource *resource,
2111 dav_prop_insert what, apr_text_header *phdr)
2113 /* don't insert any liveprops if this isn't "our" resource */
2114 if (resource->hooks != &dav_hooks_repository_fs)
2117 if (!resource->exists) {
2118 /* a lock-null resource */
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".
2127 (void) dav_fs_insert_prop(resource, DAV_PROPID_creationdate,
2129 (void) dav_fs_insert_prop(resource, DAV_PROPID_getcontentlength,
2131 (void) dav_fs_insert_prop(resource, DAV_PROPID_getlastmodified,
2133 (void) dav_fs_insert_prop(resource, DAV_PROPID_getetag,
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,
2142 /* ### we know the others aren't defined as liveprops */
2145 void dav_fs_register(apr_pool_t *p)
2147 /* register the namespace URIs */
2148 dav_register_liveprop_group(p, &dav_fs_liveprop_group);
2150 /* register the repository provider */
2151 dav_register_provider(p, "filesystem", &dav_fs_provider);