1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
56 ** DAV filesystem lock implementation
61 #include "apr_file_io.h"
62 #include "apr_strings.h"
65 #include "dav_opaquelock.h"
69 /* ---------------------------------------------------------------
71 ** Lock database primitives
78 ** Lockdiscovery information is stored in the single lock database specified
79 ** by the DAVLockDB directive. Information about this db is stored in the
80 ** global server configuration.
84 ** The database is keyed by a key_type unsigned char (DAV_TYPE_INODE or
85 ** DAV_TYPE_FNAME) followed by inode and device number if possible,
86 ** otherwise full path (in the case of Win32 or lock-null resources).
90 ** The value consists of a list of elements.
91 ** DIRECT LOCK: [char (DAV_LOCK_DIRECT),
92 ** char (dav_lock_scope),
93 ** char (dav_lock_type),
100 ** INDIRECT LOCK: [char (DAV_LOCK_INDIRECT),
105 ** The key is to the collection lock that resulted in this indirect lock
111 #define DAV_CREATE_LIST 23
112 #define DAV_APPEND_LIST 24
114 /* Stored lock_discovery prefix */
115 #define DAV_LOCK_DIRECT 1
116 #define DAV_LOCK_INDIRECT 2
118 #define DAV_TYPE_INODE 10
119 #define DAV_TYPE_FNAME 11
122 /* ack. forward declare. */
123 static dav_error * dav_fs_remove_locknull_member(apr_pool_t *p,
124 const char *filename,
128 ** Use the opaquelock scheme for locktokens
130 struct dav_locktoken {
135 /* #################################################################
136 ** ### keep these structures (internal) or move fully to dav_lock?
140 ** We need to reliably size the fixed-length portion of
141 ** dav_lock_discovery; best to separate it into another
142 ** struct for a convenient sizeof, unless we pack lock_discovery.
144 typedef struct dav_lock_discovery_fixed
150 } dav_lock_discovery_fixed;
152 typedef struct dav_lock_discovery
154 struct dav_lock_discovery_fixed f;
156 dav_locktoken *locktoken;
157 const char *owner; /* owner field from activelock */
158 const char *auth_user; /* authenticated user who created the lock */
159 struct dav_lock_discovery *next;
160 } dav_lock_discovery;
162 /* Indirect locks represent locks inherited from containing collections.
163 * They reference the lock token for the collection the lock is
164 * inherited from. A lock provider may also define a key to the
165 * inherited lock, for fast datbase lookup. The key is opaque outside
168 typedef struct dav_lock_indirect
170 dav_locktoken *locktoken;
172 struct dav_lock_indirect *next;
176 /* ################################################################# */
180 ** Stored direct lock info - full lock_discovery length:
181 ** prefix + Fixed length + lock token + 2 strings + 2 nulls (one for each string)
183 #define dav_size_direct(a) (1 + sizeof(dav_lock_discovery_fixed) \
185 + ((a)->owner ? strlen((a)->owner) : 0) \
186 + ((a)->auth_user ? strlen((a)->auth_user) : 0) \
189 /* Stored indirect lock info - lock token and dav_datum */
190 #define dav_size_indirect(a) (1 + sizeof(uuid_t) \
192 + sizeof(int) + (a)->key.dsize)
195 ** The lockdb structure.
197 ** The <db> field may be NULL, meaning one of two things:
198 ** 1) That we have not actually opened the underlying database (yet). The
199 ** <opened> field should be false.
200 ** 2) We opened it readonly and it wasn't present.
202 ** The delayed opening (determined by <opened>) makes creating a lockdb
203 ** quick, while deferring the underlying I/O until it is actually required.
205 ** We export the notion of a lockdb, but hide the details of it. Most
206 ** implementations will use a database of some kind, but it is certainly
207 ** possible that alternatives could be used.
209 struct dav_lockdb_private
211 request_rec *r; /* for accessing the uuid state */
212 apr_pool_t *pool; /* a pool to use */
213 const char *lockdb_path; /* where is the lock database? */
215 int opened; /* we opened the database */
216 dav_db *db; /* if non-NULL, the lock database */
221 dav_lockdb_private priv;
222 } dav_lockdb_combined;
225 ** The private part of the lock structure.
227 struct dav_lock_private
229 dav_datum key; /* key into the lock database */
234 dav_lock_private priv;
239 ** This must be forward-declared so the open_lockdb function can use it.
241 extern const dav_hooks_locks dav_hooks_locks_fs;
244 /* internal function for creating locks */
245 static dav_lock *dav_fs_alloc_lock(dav_lockdb *lockdb, dav_datum key,
246 const dav_locktoken *locktoken)
248 dav_lock_combined *comb;
250 comb = apr_pcalloc(lockdb->info->pool, sizeof(*comb));
251 comb->pub.rectype = DAV_LOCKREC_DIRECT;
252 comb->pub.info = &comb->priv;
253 comb->priv.key = key;
255 if (locktoken == NULL) {
256 comb->pub.locktoken = &comb->token;
257 dav_create_opaquelocktoken(dav_get_uuid_state(lockdb->info->r),
261 comb->pub.locktoken = locktoken;
268 ** dav_fs_parse_locktoken
270 ** Parse an opaquelocktoken URI into a locktoken.
272 static dav_error * dav_fs_parse_locktoken(
274 const char *char_token,
275 dav_locktoken **locktoken_p)
277 dav_locktoken *locktoken;
279 if (ap_strstr_c(char_token, "opaquelocktoken:") != char_token) {
280 return dav_new_error(p,
281 HTTP_BAD_REQUEST, DAV_ERR_LOCK_UNK_STATE_TOKEN,
282 "The lock token uses an unknown State-token "
283 "format and could not be parsed.");
287 locktoken = apr_pcalloc(p, sizeof(*locktoken));
288 if (dav_parse_opaquelocktoken(char_token, &locktoken->uuid)) {
289 return dav_new_error(p, HTTP_BAD_REQUEST, DAV_ERR_LOCK_PARSE_TOKEN,
290 "The opaquelocktoken has an incorrect format "
291 "and could not be parsed.");
294 *locktoken_p = locktoken;
299 ** dav_fs_format_locktoken
301 ** Generate the URI for a locktoken
303 static const char *dav_fs_format_locktoken(
305 const dav_locktoken *locktoken)
307 const char *uuid_token = dav_format_opaquelocktoken(p, &locktoken->uuid);
308 return apr_pstrcat(p, "opaquelocktoken:", uuid_token, NULL);
312 ** dav_fs_compare_locktoken
314 ** Determine whether two locktokens are the same
316 static int dav_fs_compare_locktoken(
317 const dav_locktoken *lt1,
318 const dav_locktoken *lt2)
320 return dav_compare_opaquelocktoken(lt1->uuid, lt2->uuid);
324 ** dav_fs_really_open_lockdb:
326 ** If the database hasn't been opened yet, then open the thing.
328 static dav_error * dav_fs_really_open_lockdb(dav_lockdb *lockdb)
332 if (lockdb->info->opened)
335 err = dav_dbm_open_direct(lockdb->info->pool,
336 lockdb->info->lockdb_path,
340 return dav_push_error(lockdb->info->pool,
341 HTTP_INTERNAL_SERVER_ERROR,
343 "Could not open the lock database.",
347 /* all right. it is opened now. */
348 lockdb->info->opened = 1;
354 ** dav_fs_open_lockdb:
356 ** "open" the lock database, as specified in the global server configuration.
357 ** If force is TRUE, then the database is opened now, rather than lazily.
359 ** Note that only one can be open read/write.
361 static dav_error * dav_fs_open_lockdb(request_rec *r, int ro, int force,
364 dav_lockdb_combined *comb;
366 comb = apr_pcalloc(r->pool, sizeof(*comb));
367 comb->pub.hooks = &dav_hooks_locks_fs;
369 comb->pub.info = &comb->priv;
371 comb->priv.pool = r->pool;
373 comb->priv.lockdb_path = dav_get_lockdb_path(r);
374 if (comb->priv.lockdb_path == NULL) {
375 return dav_new_error(r->pool, HTTP_INTERNAL_SERVER_ERROR,
377 "A lock database was not specified with the "
378 "DAVLockDB directive. One must be specified "
379 "to use the locking functionality.");
382 /* done initializing. return it. */
383 *lockdb = &comb->pub;
386 /* ### add a higher-level comment? */
387 return dav_fs_really_open_lockdb(*lockdb);
394 ** dav_fs_close_lockdb:
398 static void dav_fs_close_lockdb(dav_lockdb *lockdb)
400 if (lockdb->info->db != NULL)
401 (*dav_hooks_db_dbm.close)(lockdb->info->db);
405 ** dav_fs_build_fname_key
407 ** Given a pathname, build a DAV_TYPE_FNAME lock database key.
409 static dav_datum dav_fs_build_fname_key(apr_pool_t *p, const char *pathname)
413 /* ### does this allocation have a proper lifetime? need to check */
414 /* ### can we use a buffer for this? */
416 /* size is TYPE + pathname + null */
417 key.dsize = strlen(pathname) + 2;
418 key.dptr = apr_palloc(p, key.dsize);
419 *key.dptr = DAV_TYPE_FNAME;
420 memcpy(key.dptr + 1, pathname, key.dsize - 1);
421 if (key.dptr[key.dsize - 2] == '/')
422 key.dptr[--key.dsize - 1] = '\0';
427 ** dav_fs_build_key: Given a resource, return a dav_datum key
428 ** to look up lock information for this file.
430 ** (Win32 or file is lock-null):
431 ** dav_datum->dvalue = full path
433 ** (non-Win32 and file exists ):
434 ** dav_datum->dvalue = inode, dev_major, dev_minor
436 static dav_datum dav_fs_build_key(apr_pool_t *p, const dav_resource *resource)
438 const char *file = dav_fs_pathname(resource);
443 /* ### use lstat() ?? */
444 if (apr_stat(&finfo, file, p) == 0) {
446 /* ### can we use a buffer for this? */
447 key.dsize = 1 + sizeof(finfo.inode) + sizeof(finfo.device);
448 key.dptr = apr_palloc(p, key.dsize);
449 *key.dptr = DAV_TYPE_INODE;
450 memcpy(key.dptr + 1, &finfo.inode, sizeof(finfo.inode));
451 memcpy(key.dptr + 1 + sizeof(finfo.inode), &finfo.device,
452 sizeof(finfo.device));
458 return dav_fs_build_fname_key(p, file);
462 ** dav_fs_lock_expired: return 1 (true) if the given timeout is in the past
463 ** or present (the lock has expired), or 0 (false) if in the future
464 ** (the lock has not yet expired).
466 static int dav_fs_lock_expired(time_t expires)
468 return expires != DAV_TIMEOUT_INFINITE && time(NULL) >= expires;
472 ** dav_fs_save_lock_record: Saves the lock information specified in the
473 ** direct and indirect lock lists about path into the lock database.
474 ** If direct and indirect == NULL, the key is removed.
476 static dav_error * dav_fs_save_lock_record(dav_lockdb *lockdb, dav_datum key,
477 dav_lock_discovery *direct,
478 dav_lock_indirect *indirect)
481 dav_datum val = { 0 };
483 dav_lock_discovery *dp = direct;
484 dav_lock_indirect *ip = indirect;
488 return dav_new_error(lockdb->info->pool,
489 HTTP_INTERNAL_SERVER_ERROR, 0,
490 "INTERNAL DESIGN ERROR: the lockdb was opened "
491 "readonly, but an attempt to save locks was "
496 if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
497 /* ### add a higher-level error? */
501 /* If nothing to save, delete key */
502 if (dp == NULL && ip == NULL) {
503 /* don't fail if the key is not present */
504 /* ### but what about other errors? */
505 (void) (*dav_hooks_db_dbm.remove)(lockdb->info->db, key);
510 val.dsize += dav_size_direct(dp);
514 val.dsize += dav_size_indirect(ip);
518 /* ### can this be apr_palloc() ? */
519 /* ### hmmm.... investigate the use of a buffer here */
520 ptr = val.dptr = apr_pcalloc(lockdb->info->pool, val.dsize);
525 *ptr++ = DAV_LOCK_DIRECT; /* Direct lock - lock_discovery struct follows */
526 memcpy(ptr, dp, sizeof(dp->f)); /* Fixed portion of struct */
527 ptr += sizeof(dp->f);
528 memcpy(ptr, dp->locktoken, sizeof(*dp->locktoken));
529 ptr += sizeof(*dp->locktoken);
530 if (dp->owner == NULL) {
534 memcpy(ptr, dp->owner, strlen(dp->owner) + 1);
535 ptr += strlen(dp->owner) + 1;
537 if (dp->auth_user == NULL) {
541 memcpy(ptr, dp->auth_user, strlen(dp->auth_user) + 1);
542 ptr += strlen(dp->auth_user) + 1;
549 *ptr++ = DAV_LOCK_INDIRECT; /* Indirect lock prefix */
550 memcpy(ptr, ip->locktoken, sizeof(*ip->locktoken)); /* Locktoken */
551 ptr += sizeof(*ip->locktoken);
552 memcpy(ptr, &ip->timeout, sizeof(ip->timeout)); /* Expire time */
553 ptr += sizeof(ip->timeout);
554 memcpy(ptr, &ip->key.dsize, sizeof(ip->key.dsize)); /* Size of key */
555 ptr += sizeof(ip->key.dsize);
556 memcpy(ptr, ip->key.dptr, ip->key.dsize); /* Key data */
557 ptr += ip->key.dsize;
561 if ((err = (*dav_hooks_db_dbm.store)(lockdb->info->db,
562 key, val)) != NULL) {
563 /* ### more details? add an error_id? */
564 return dav_push_error(lockdb->info->pool,
565 HTTP_INTERNAL_SERVER_ERROR,
566 DAV_ERR_LOCK_SAVE_LOCK,
567 "Could not save lock information.",
575 ** dav_load_lock_record: Reads lock information about key from lock db;
576 ** creates linked lists of the direct and indirect locks.
578 ** If add_method = DAV_APPEND_LIST, the result will be appended to the
579 ** head of the direct and indirect lists supplied.
581 ** Passive lock removal: If lock has timed out, it will not be returned.
582 ** ### How much "logging" does RFC 2518 require?
584 static dav_error * dav_fs_load_lock_record(dav_lockdb *lockdb, dav_datum key,
586 dav_lock_discovery **direct,
587 dav_lock_indirect **indirect)
589 apr_pool_t *p = lockdb->info->pool;
592 int need_save = DAV_FALSE;
593 dav_datum val = { 0 };
594 dav_lock_discovery *dp;
595 dav_lock_indirect *ip;
596 dav_buffer buf = { 0 };
598 if (add_method != DAV_APPEND_LIST) {
603 if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
604 /* ### add a higher-level error? */
609 ** If we opened readonly and the db wasn't there, then there are no
610 ** locks for this resource. Just exit.
612 if (lockdb->info->db == NULL)
615 if ((err = (*dav_hooks_db_dbm.fetch)(lockdb->info->db, key, &val)) != NULL)
621 while (offset < val.dsize) {
622 switch (*(val.dptr + offset++)) {
623 case DAV_LOCK_DIRECT:
624 /* Create and fill a dav_lock_discovery structure */
626 dp = apr_pcalloc(p, sizeof(*dp));
627 memcpy(dp, val.dptr + offset, sizeof(dp->f));
628 offset += sizeof(dp->f);
629 dp->locktoken = apr_palloc(p, sizeof(*dp->locktoken));
630 memcpy(dp->locktoken, val.dptr + offset, sizeof(*dp->locktoken));
631 offset += sizeof(*dp->locktoken);
632 if (*(val.dptr + offset) == '\0') {
636 dp->owner = apr_pstrdup(p, val.dptr + offset);
637 offset += strlen(dp->owner) + 1;
640 if (*(val.dptr + offset) == '\0') {
644 dp->auth_user = apr_pstrdup(p, val.dptr + offset);
645 offset += strlen(dp->auth_user) + 1;
648 if (!dav_fs_lock_expired(dp->f.timeout)) {
653 need_save = DAV_TRUE;
655 /* Remove timed-out locknull fm .locknull list */
656 if (*key.dptr == DAV_TYPE_FNAME) {
657 const char *fname = key.dptr + 1;
660 /* if we don't see the file, then it's a locknull */
661 if (apr_lstat(&finfo, fname, p) != 0) {
662 if ((err = dav_fs_remove_locknull_member(p, fname, &buf)) != NULL) {
663 /* ### push a higher-level description? */
671 case DAV_LOCK_INDIRECT:
672 /* Create and fill a dav_lock_indirect structure */
674 ip = apr_pcalloc(p, sizeof(*ip));
675 ip->locktoken = apr_palloc(p, sizeof(*ip->locktoken));
676 memcpy(ip->locktoken, val.dptr + offset, sizeof(*ip->locktoken));
677 offset += sizeof(*ip->locktoken);
678 memcpy(&ip->timeout, val.dptr + offset, sizeof(ip->timeout));
679 offset += sizeof(ip->timeout);
680 ip->key.dsize = *((int *) (val.dptr + offset)); /* length of datum */
681 offset += sizeof(ip->key.dsize);
682 ip->key.dptr = apr_palloc(p, ip->key.dsize);
683 memcpy(ip->key.dptr, val.dptr + offset, ip->key.dsize);
684 offset += ip->key.dsize;
686 if (!dav_fs_lock_expired(ip->timeout)) {
687 ip->next = *indirect;
691 need_save = DAV_TRUE;
692 /* A locknull resource will never be locked indirectly */
698 (*dav_hooks_db_dbm.freedatum)(lockdb->info->db, val);
700 /* ### should use a computed_desc and insert corrupt token data */
702 return dav_new_error(p,
703 HTTP_INTERNAL_SERVER_ERROR,
704 DAV_ERR_LOCK_CORRUPT_DB,
706 "The lock database was found to "
707 "be corrupt. offset %i, c=%02x",
708 offset, val.dptr[offset]));
712 (*dav_hooks_db_dbm.freedatum)(lockdb->info->db, val);
714 /* Clean up this record if we found expired locks */
716 ** ### shouldn't do this if we've been opened READONLY. elide the
717 ** ### timed-out locks from the response, but don't save that info back
719 if (need_save == DAV_TRUE) {
720 return dav_fs_save_lock_record(lockdb, key, *direct, *indirect);
726 /* resolve <indirect>, returning <*direct> */
727 static dav_error * dav_fs_resolve(dav_lockdb *lockdb,
728 dav_lock_indirect *indirect,
729 dav_lock_discovery **direct,
730 dav_lock_discovery **ref_dp,
731 dav_lock_indirect **ref_ip)
734 dav_lock_discovery *dir;
735 dav_lock_indirect *ind;
737 if ((err = dav_fs_load_lock_record(lockdb, indirect->key,
739 &dir, &ind)) != NULL) {
740 /* ### insert a higher-level description? */
743 if (ref_dp != NULL) {
748 for (; dir != NULL; dir = dir->next) {
749 if (!dav_compare_opaquelocktoken(indirect->locktoken->uuid,
750 dir->locktoken->uuid)) {
756 /* No match found (but we should have found one!) */
758 /* ### use a different description and/or error ID? */
759 return dav_new_error(lockdb->info->pool,
760 HTTP_INTERNAL_SERVER_ERROR,
761 DAV_ERR_LOCK_CORRUPT_DB,
762 "The lock database was found to be corrupt. "
763 "An indirect lock's direct lock could not "
767 /* ---------------------------------------------------------------
769 ** Property-related lock functions
774 ** dav_fs_get_supportedlock: Returns a static string for all supportedlock
775 ** properties. I think we save more returning a static string than
776 ** constructing it every time, though it might look cleaner.
778 static const char *dav_fs_get_supportedlock(void)
780 static const char supported[] = DEBUG_CR
781 "<D:lockentry>" DEBUG_CR
782 "<D:lockscope><D:exclusive/></D:lockscope>" DEBUG_CR
783 "<D:locktype><D:write/></D:locktype>" DEBUG_CR
784 "</D:lockentry>" DEBUG_CR
785 "<D:lockentry>" DEBUG_CR
786 "<D:lockscope><D:shared/></D:lockscope>" DEBUG_CR
787 "<D:locktype><D:write/></D:locktype>" DEBUG_CR
788 "</D:lockentry>" DEBUG_CR;
793 /* ---------------------------------------------------------------
795 ** General lock functions
799 /* ---------------------------------------------------------------
801 ** Functions dealing with lock-null resources
806 ** dav_fs_load_locknull_list: Returns a dav_buffer dump of the locknull file
807 ** for the given directory.
809 static dav_error * dav_fs_load_locknull_list(apr_pool_t *p, const char *dirpath,
813 apr_file_t *file = NULL;
814 dav_error *err = NULL;
817 dav_buffer_init(p, pbuf, dirpath);
819 if (pbuf->buf[pbuf->cur_len - 1] == '/')
820 pbuf->buf[--pbuf->cur_len] = '\0';
822 dav_buffer_place(p, pbuf, "/" DAV_FS_STATE_DIR "/" DAV_FS_LOCK_NULL_FILE);
824 /* reset this in case we leave w/o reading into the buffer */
827 if (apr_open(&file, pbuf->buf, APR_READ | APR_BINARY, APR_OS_DEFAULT,
832 if (apr_getfileinfo(&finfo, file) != APR_SUCCESS) {
833 err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
835 "Opened but could not stat file %s",
840 dav_set_bufsize(p, pbuf, finfo.size);
842 if (apr_read(file, pbuf->buf, &amt) != APR_SUCCESS
843 || amt != finfo.size) {
844 err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
846 "Failure reading locknull file "
849 /* just in case the caller disregards the returned error */
860 ** dav_fs_save_locknull_list: Saves contents of pbuf into the
861 ** locknull file for dirpath.
863 static dav_error * dav_fs_save_locknull_list(apr_pool_t *p, const char *dirpath,
866 const char *pathname;
867 apr_file_t *file = NULL;
868 dav_error *err = NULL;
871 if (pbuf->buf == NULL)
874 dav_fs_ensure_state_dir(p, dirpath);
875 pathname = apr_pstrcat(p,
877 dirpath[strlen(dirpath) - 1] == '/' ? "" : "/",
878 DAV_FS_STATE_DIR "/" DAV_FS_LOCK_NULL_FILE,
881 if (pbuf->cur_len == 0) {
882 /* delete the file if cur_len == 0 */
883 if (remove(pathname) != 0) {
884 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
886 "Error removing %s", pathname));
891 if (apr_open(&file, pathname,
892 APR_WRITE | APR_CREATE | APR_TRUNCATE | APR_BINARY,
893 APR_OS_DEFAULT, p) != APR_SUCCESS) {
894 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
896 "Error opening %s for writing",
901 if (apr_write(file, pbuf->buf, &amt) != APR_SUCCESS
902 || amt != pbuf->cur_len) {
903 err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
905 "Error writing %i bytes to %s",
906 pbuf->cur_len, pathname));
914 ** dav_fs_remove_locknull_member: Removes filename from the locknull list
915 ** for directory path.
917 static dav_error * dav_fs_remove_locknull_member(apr_pool_t *p,
918 const char *filename,
926 char *dirpath = apr_pstrdup(p, filename);
927 char *fname = strrchr(dirpath, '/');
934 len = strlen(fname) + 1;
936 if ((err = dav_fs_load_locknull_list(p, dirpath, pbuf)) != NULL) {
937 /* ### add a higher level description? */
941 for (scan = pbuf->buf, scanend = scan + pbuf->cur_len;
944 scanlen = strlen(scan) + 1;
945 if (len == scanlen && memcmp(fname, scan, scanlen) == 0) {
946 pbuf->cur_len -= scanlen;
947 memmove(scan, scan + scanlen, scanend - (scan + scanlen));
954 if ((err = dav_fs_save_locknull_list(p, dirpath, pbuf)) != NULL) {
955 /* ### add a higher level description? */
963 /* Note: used by dav_fs_repos.c */
964 dav_error * dav_fs_get_locknull_members(
965 const dav_resource *resource,
970 dav_fs_dir_file_name(resource, &dirpath, NULL);
971 return dav_fs_load_locknull_list(dav_fs_pool(resource), dirpath, pbuf);
974 /* ### fold into append_lock? */
975 /* ### take an optional buf parameter? */
976 static dav_error * dav_fs_add_locknull_state(
978 const dav_resource *resource)
980 dav_buffer buf = { 0 };
981 apr_pool_t *p = lockdb->info->pool;
986 dav_fs_dir_file_name(resource, &dirpath, &fname);
988 if ((err = dav_fs_load_locknull_list(p, dirpath, &buf)) != NULL) {
989 return dav_push_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
990 "Could not load .locknull file.", err);
993 dav_buffer_append(p, &buf, fname);
994 buf.cur_len++; /* we want the null-term here */
996 if ((err = dav_fs_save_locknull_list(p, dirpath, &buf)) != NULL) {
997 return dav_push_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
998 "Could not save .locknull file.", err);
1005 ** dav_fs_remove_locknull_state: Given a request, check to see if r->filename
1006 ** is/was a lock-null resource. If so, return it to an existant state.
1008 ** ### this function is broken... it doesn't check!
1010 ** In this implementation, this involves two things:
1011 ** (a) remove it from the list in the appropriate .DAV/locknull file
1012 ** (b) on *nix, convert the key from a filename to an inode.
1014 static dav_error * dav_fs_remove_locknull_state(
1016 const dav_resource *resource)
1018 dav_buffer buf = { 0 };
1020 apr_pool_t *p = lockdb->info->pool;
1021 const char *pathname = dav_fs_pathname(resource);
1023 if ((err = dav_fs_remove_locknull_member(p, pathname, &buf)) != NULL) {
1024 /* ### add a higher-level description? */
1030 dav_lock_discovery *ld;
1031 dav_lock_indirect *id;
1035 ** Fetch the lock(s) that made the resource lock-null. Remove
1036 ** them under the filename key. Obtain the new inode key, and
1037 ** save the same lock information under it.
1039 key = dav_fs_build_fname_key(p, pathname);
1040 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1041 &ld, &id)) != NULL) {
1042 /* ### insert a higher-level error description */
1046 if ((err = dav_fs_save_lock_record(lockdb, key, NULL, NULL)) != NULL) {
1047 /* ### insert a higher-level error description */
1051 key = dav_fs_build_key(p, resource);
1052 if ((err = dav_fs_save_lock_record(lockdb, key, ld, id)) != NULL) {
1053 /* ### insert a higher-level error description */
1062 static dav_error * dav_fs_create_lock(dav_lockdb *lockdb,
1063 const dav_resource *resource,
1068 key = dav_fs_build_key(lockdb->info->pool, resource);
1070 *lock = dav_fs_alloc_lock(lockdb,
1074 (*lock)->is_locknull = !resource->exists;
1079 static dav_error * dav_fs_get_locks(dav_lockdb *lockdb,
1080 const dav_resource *resource,
1084 apr_pool_t *p = lockdb->info->pool;
1087 dav_lock *lock = NULL;
1089 dav_lock_discovery *dp;
1090 dav_lock_indirect *ip;
1093 if (calltype == DAV_GETLOCKS_COMPLETE) {
1094 return dav_new_error(lockdb->info->pool,
1095 HTTP_INTERNAL_SERVER_ERROR, 0,
1096 "INTERNAL DESIGN ERROR: DAV_GETLOCKS_COMPLETE "
1097 "is not yet supported");
1101 key = dav_fs_build_key(p, resource);
1102 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1103 &dp, &ip)) != NULL) {
1104 /* ### push a higher-level desc? */
1108 /* copy all direct locks to the result list */
1109 for (; dp != NULL; dp = dp->next) {
1110 newlock = dav_fs_alloc_lock(lockdb, key, dp->locktoken);
1111 newlock->is_locknull = !resource->exists;
1112 newlock->scope = dp->f.scope;
1113 newlock->type = dp->f.type;
1114 newlock->depth = dp->f.depth;
1115 newlock->timeout = dp->f.timeout;
1116 newlock->owner = dp->owner;
1117 newlock->auth_user = dp->auth_user;
1119 /* hook into the result list */
1120 newlock->next = lock;
1124 /* copy all the indirect locks to the result list. resolve as needed. */
1125 for (; ip != NULL; ip = ip->next) {
1126 newlock = dav_fs_alloc_lock(lockdb, ip->key, ip->locktoken);
1127 newlock->is_locknull = !resource->exists;
1129 if (calltype == DAV_GETLOCKS_RESOLVED) {
1130 if ((err = dav_fs_resolve(lockdb, ip, &dp, NULL, NULL)) != NULL) {
1131 /* ### push a higher-level desc? */
1135 newlock->scope = dp->f.scope;
1136 newlock->type = dp->f.type;
1137 newlock->depth = dp->f.depth;
1138 newlock->timeout = dp->f.timeout;
1139 newlock->owner = dp->owner;
1140 newlock->auth_user = dp->auth_user;
1143 /* DAV_GETLOCKS_PARTIAL */
1144 newlock->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
1147 /* hook into the result list */
1148 newlock->next = lock;
1156 static dav_error * dav_fs_find_lock(dav_lockdb *lockdb,
1157 const dav_resource *resource,
1158 const dav_locktoken *locktoken,
1164 dav_lock_discovery *dp;
1165 dav_lock_indirect *ip;
1169 key = dav_fs_build_key(lockdb->info->pool, resource);
1170 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1171 &dp, &ip)) != NULL) {
1172 /* ### push a higher-level desc? */
1176 for (; dp != NULL; dp = dp->next) {
1177 if (!dav_compare_opaquelocktoken(locktoken->uuid,
1178 dp->locktoken->uuid)) {
1179 *lock = dav_fs_alloc_lock(lockdb, key, locktoken);
1180 (*lock)->is_locknull = !resource->exists;
1181 (*lock)->scope = dp->f.scope;
1182 (*lock)->type = dp->f.type;
1183 (*lock)->depth = dp->f.depth;
1184 (*lock)->timeout = dp->f.timeout;
1185 (*lock)->owner = dp->owner;
1186 (*lock)->auth_user = dp->auth_user;
1191 for (; ip != NULL; ip = ip->next) {
1192 if (!dav_compare_opaquelocktoken(locktoken->uuid,
1193 ip->locktoken->uuid)) {
1194 *lock = dav_fs_alloc_lock(lockdb, ip->key, locktoken);
1195 (*lock)->is_locknull = !resource->exists;
1197 /* ### nobody uses the resolving right now! */
1199 (*lock)->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
1202 (*lock)->rectype = DAV_LOCKREC_INDIRECT;
1203 if ((err = dav_fs_resolve(lockdb, ip, &dp,
1204 NULL, NULL)) != NULL) {
1205 /* ### push a higher-level desc? */
1208 (*lock)->scope = dp->f.scope;
1209 (*lock)->type = dp->f.type;
1210 (*lock)->depth = dp->f.depth;
1211 (*lock)->timeout = dp->f.timeout;
1212 (*lock)->owner = dp->owner;
1213 (*lock)->auth_user = dp->auth_user;
1222 static dav_error * dav_fs_has_locks(dav_lockdb *lockdb,
1223 const dav_resource *resource,
1231 if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
1232 /* ### insert a higher-level error description */
1237 ** If we opened readonly and the db wasn't there, then there are no
1238 ** locks for this resource. Just exit.
1240 if (lockdb->info->db == NULL)
1243 key = dav_fs_build_key(lockdb->info->pool, resource);
1245 *locks_present = (*dav_hooks_db_dbm.exists)(lockdb->info->db, key);
1250 static dav_error * dav_fs_append_locks(dav_lockdb *lockdb,
1251 const dav_resource *resource,
1253 const dav_lock *lock)
1255 apr_pool_t *p = lockdb->info->pool;
1257 dav_lock_indirect *ip;
1258 dav_lock_discovery *dp;
1261 key = dav_fs_build_key(lockdb->info->pool, resource);
1262 if ((err = dav_fs_load_lock_record(lockdb, key, 0, &dp, &ip)) != NULL) {
1263 /* ### maybe add in a higher-level description */
1268 ** ### when we store the lock more directly, we need to update
1269 ** ### lock->rectype and lock->is_locknull
1272 if (make_indirect) {
1273 for (; lock != NULL; lock = lock->next) {
1275 /* ### this works for any <lock> rectype */
1276 dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
1278 /* ### shut off the const warning for now */
1279 newi->locktoken = (dav_locktoken *)lock->locktoken;
1280 newi->timeout = lock->timeout;
1281 newi->key = lock->info->key;
1287 for (; lock != NULL; lock = lock->next) {
1288 /* create and link in the right kind of lock */
1290 if (lock->rectype == DAV_LOCKREC_DIRECT) {
1291 dav_lock_discovery *newd = apr_pcalloc(p, sizeof(*newd));
1293 newd->f.scope = lock->scope;
1294 newd->f.type = lock->type;
1295 newd->f.depth = lock->depth;
1296 newd->f.timeout = lock->timeout;
1297 /* ### shut off the const warning for now */
1298 newd->locktoken = (dav_locktoken *)lock->locktoken;
1299 newd->owner = lock->owner;
1300 newd->auth_user = lock->auth_user;
1305 /* DAV_LOCKREC_INDIRECT(_PARTIAL) */
1307 dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
1309 /* ### shut off the const warning for now */
1310 newi->locktoken = (dav_locktoken *)lock->locktoken;
1311 newi->key = lock->info->key;
1318 if ((err = dav_fs_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1319 /* ### maybe add a higher-level description */
1323 /* we have a special list for recording locknull resources */
1324 /* ### ack! this can add two copies to the locknull list */
1325 if (!resource->exists
1326 && (err = dav_fs_add_locknull_state(lockdb, resource)) != NULL) {
1327 /* ### maybe add a higher-level description */
1334 static dav_error * dav_fs_remove_lock(dav_lockdb *lockdb,
1335 const dav_resource *resource,
1336 const dav_locktoken *locktoken)
1339 dav_buffer buf = { 0 };
1340 dav_lock_discovery *dh = NULL;
1341 dav_lock_indirect *ih = NULL;
1344 key = dav_fs_build_key(lockdb->info->pool, resource);
1346 if (locktoken != NULL) {
1347 dav_lock_discovery *dp;
1348 dav_lock_discovery *dprev = NULL;
1349 dav_lock_indirect *ip;
1350 dav_lock_indirect *iprev = NULL;
1352 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1353 &dh, &ih)) != NULL) {
1354 /* ### maybe add a higher-level description */
1358 for (dp = dh; dp != NULL; dp = dp->next) {
1359 if (dav_compare_opaquelocktoken(locktoken->uuid,
1360 dp->locktoken->uuid) == 0) {
1362 dprev->next = dp->next;
1369 for (ip = ih; ip != NULL; ip = ip->next) {
1370 if (dav_compare_opaquelocktoken(locktoken->uuid,
1371 ip->locktoken->uuid) == 0) {
1373 iprev->next = ip->next;
1382 /* save the modified locks, or remove all locks (dh=ih=NULL). */
1383 if ((err = dav_fs_save_lock_record(lockdb, key, dh, ih)) != NULL) {
1384 /* ### maybe add a higher-level description */
1389 ** If this resource is a locknull resource AND no more locks exist,
1390 ** then remove the locknull member.
1392 ** Note: remove_locknull_state() attempts to convert a locknull member
1393 ** to a real member. In this case, all locks are gone, so the
1394 ** locknull resource returns to the null state (ie. doesn't exist),
1395 ** so there is no need to update the lockdb (and it won't find
1396 ** any because a precondition is that none exist).
1398 if (!resource->exists && dh == NULL && ih == NULL
1399 && (err = dav_fs_remove_locknull_member(lockdb->info->pool,
1400 dav_fs_pathname(resource),
1402 /* ### maybe add a higher-level description */
1409 static int dav_fs_do_refresh(dav_lock_discovery *dp,
1410 const dav_locktoken_list *ltl,
1415 for (; ltl != NULL; ltl = ltl->next) {
1416 if (dav_compare_opaquelocktoken(dp->locktoken->uuid,
1417 ltl->locktoken->uuid) == 0)
1419 dp->f.timeout = new_time;
1427 static dav_error * dav_fs_refresh_locks(dav_lockdb *lockdb,
1428 const dav_resource *resource,
1429 const dav_locktoken_list *ltl,
1435 dav_lock_discovery *dp;
1436 dav_lock_discovery *dp_scan;
1437 dav_lock_indirect *ip;
1443 key = dav_fs_build_key(lockdb->info->pool, resource);
1444 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1445 &dp, &ip)) != NULL) {
1446 /* ### maybe add in a higher-level description */
1450 /* ### we should be refreshing direct AND (resolved) indirect locks! */
1452 /* refresh all of the direct locks on this resource */
1453 for (dp_scan = dp; dp_scan != NULL; dp_scan = dp_scan->next) {
1454 if (dav_fs_do_refresh(dp_scan, ltl, new_time)) {
1455 /* the lock was refreshed. return the lock. */
1456 newlock = dav_fs_alloc_lock(lockdb, key, dp_scan->locktoken);
1457 newlock->is_locknull = !resource->exists;
1458 newlock->scope = dp_scan->f.scope;
1459 newlock->type = dp_scan->f.type;
1460 newlock->depth = dp_scan->f.depth;
1461 newlock->timeout = dp_scan->f.timeout;
1462 newlock->owner = dp_scan->owner;
1463 newlock->auth_user = dp_scan->auth_user;
1465 newlock->next = *locks;
1472 /* if we refreshed any locks, then save them back. */
1474 && (err = dav_fs_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1475 /* ### maybe add in a higher-level description */
1479 /* for each indirect lock, find its direct lock and refresh it. */
1480 for (; ip != NULL; ip = ip->next) {
1481 dav_lock_discovery *ref_dp;
1482 dav_lock_indirect *ref_ip;
1484 if ((err = dav_fs_resolve(lockdb, ip, &dp_scan,
1485 &ref_dp, &ref_ip)) != NULL) {
1486 /* ### push a higher-level desc? */
1489 if (dav_fs_do_refresh(dp_scan, ltl, new_time)) {
1490 /* the lock was refreshed. return the lock. */
1491 newlock = dav_fs_alloc_lock(lockdb, ip->key, dp->locktoken);
1492 newlock->is_locknull = !resource->exists;
1493 newlock->scope = dp->f.scope;
1494 newlock->type = dp->f.type;
1495 newlock->depth = dp->f.depth;
1496 newlock->timeout = dp->f.timeout;
1497 newlock->owner = dp->owner;
1498 newlock->auth_user = dp_scan->auth_user;
1500 newlock->next = *locks;
1503 /* save the (resolved) direct lock back */
1504 if ((err = dav_fs_save_lock_record(lockdb, ip->key, ref_dp,
1506 /* ### push a higher-level desc? */
1516 const dav_hooks_locks dav_hooks_locks_fs =
1518 dav_fs_get_supportedlock,
1519 dav_fs_parse_locktoken,
1520 dav_fs_format_locktoken,
1521 dav_fs_compare_locktoken,
1523 dav_fs_close_lockdb,
1524 dav_fs_remove_locknull_state,
1529 dav_fs_append_locks,
1531 dav_fs_refresh_locks,
1532 NULL, /* get_resource */