1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 ** DAV filesystem lock implementation
22 #include "apr_strings.h"
23 #include "apr_file_io.h"
26 #define APR_WANT_MEMFUNC
36 /* ---------------------------------------------------------------
38 ** Lock database primitives
45 ** Lockdiscovery information is stored in the single lock database specified
46 ** by the DAVLockDB directive. Information about this db is stored in the
47 ** global server configuration.
51 ** The database is keyed by a key_type unsigned char (DAV_TYPE_INODE or
52 ** DAV_TYPE_FNAME) followed by inode and device number if possible,
53 ** otherwise full path (in the case of Win32 or lock-null resources).
57 ** The value consists of a list of elements.
58 ** DIRECT LOCK: [char (DAV_LOCK_DIRECT),
59 ** char (dav_lock_scope),
60 ** char (dav_lock_type),
63 ** apr_uuid_t locktoken,
67 ** INDIRECT LOCK: [char (DAV_LOCK_INDIRECT),
68 ** apr_uuid_t locktoken,
70 ** apr_size_t key_size,
72 ** The key is to the collection lock that resulted in this indirect lock
78 #define DAV_CREATE_LIST 23
79 #define DAV_APPEND_LIST 24
81 /* Stored lock_discovery prefix */
82 #define DAV_LOCK_DIRECT 1
83 #define DAV_LOCK_INDIRECT 2
85 #define DAV_TYPE_INODE 10
86 #define DAV_TYPE_FNAME 11
89 /* ack. forward declare. */
90 static dav_error * dav_fs_remove_locknull_member(apr_pool_t *p,
95 ** Use the opaquelock scheme for locktokens
97 struct dav_locktoken {
100 #define dav_compare_locktoken(plt1, plt2) \
101 memcmp(&(plt1)->uuid, &(plt2)->uuid, sizeof((plt1)->uuid))
104 /* #################################################################
105 ** ### keep these structures (internal) or move fully to dav_lock?
109 ** We need to reliably size the fixed-length portion of
110 ** dav_lock_discovery; best to separate it into another
111 ** struct for a convenient sizeof, unless we pack lock_discovery.
113 typedef struct dav_lock_discovery_fixed
119 } dav_lock_discovery_fixed;
121 typedef struct dav_lock_discovery
123 struct dav_lock_discovery_fixed f;
125 dav_locktoken *locktoken;
126 const char *owner; /* owner field from activelock */
127 const char *auth_user; /* authenticated user who created the lock */
128 struct dav_lock_discovery *next;
129 } dav_lock_discovery;
131 /* Indirect locks represent locks inherited from containing collections.
132 * They reference the lock token for the collection the lock is
133 * inherited from. A lock provider may also define a key to the
134 * inherited lock, for fast datbase lookup. The key is opaque outside
137 typedef struct dav_lock_indirect
139 dav_locktoken *locktoken;
141 struct dav_lock_indirect *next;
145 /* ################################################################# */
149 ** Stored direct lock info - full lock_discovery length:
150 ** prefix + Fixed length + lock token + 2 strings + 2 nulls (one for each string)
152 #define dav_size_direct(a) (1 + sizeof(dav_lock_discovery_fixed) \
153 + sizeof(apr_uuid_t) \
154 + ((a)->owner ? strlen((a)->owner) : 0) \
155 + ((a)->auth_user ? strlen((a)->auth_user) : 0) \
158 /* Stored indirect lock info - lock token and apr_datum_t */
159 #define dav_size_indirect(a) (1 + sizeof(apr_uuid_t) \
161 + sizeof((a)->key.dsize) + (a)->key.dsize)
164 ** The lockdb structure.
166 ** The <db> field may be NULL, meaning one of two things:
167 ** 1) That we have not actually opened the underlying database (yet). The
168 ** <opened> field should be false.
169 ** 2) We opened it readonly and it wasn't present.
171 ** The delayed opening (determined by <opened>) makes creating a lockdb
172 ** quick, while deferring the underlying I/O until it is actually required.
174 ** We export the notion of a lockdb, but hide the details of it. Most
175 ** implementations will use a database of some kind, but it is certainly
176 ** possible that alternatives could be used.
178 struct dav_lockdb_private
180 request_rec *r; /* for accessing the uuid state */
181 apr_pool_t *pool; /* a pool to use */
182 const char *lockdb_path; /* where is the lock database? */
184 int opened; /* we opened the database */
185 dav_db *db; /* if non-NULL, the lock database */
190 dav_lockdb_private priv;
191 } dav_lockdb_combined;
194 ** The private part of the lock structure.
196 struct dav_lock_private
198 apr_datum_t key; /* key into the lock database */
203 dav_lock_private priv;
208 ** This must be forward-declared so the open_lockdb function can use it.
210 extern const dav_hooks_locks dav_hooks_locks_fs;
213 /* internal function for creating locks */
214 static dav_lock *dav_fs_alloc_lock(dav_lockdb *lockdb, apr_datum_t key,
215 const dav_locktoken *locktoken)
217 dav_lock_combined *comb;
219 comb = apr_pcalloc(lockdb->info->pool, sizeof(*comb));
220 comb->pub.rectype = DAV_LOCKREC_DIRECT;
221 comb->pub.info = &comb->priv;
222 comb->priv.key = key;
224 if (locktoken == NULL) {
225 comb->pub.locktoken = &comb->token;
226 apr_uuid_get(&comb->token.uuid);
229 comb->pub.locktoken = locktoken;
236 ** dav_fs_parse_locktoken
238 ** Parse an opaquelocktoken URI into a locktoken.
240 static dav_error * dav_fs_parse_locktoken(
242 const char *char_token,
243 dav_locktoken **locktoken_p)
245 dav_locktoken *locktoken;
247 if (ap_strstr_c(char_token, "opaquelocktoken:") != char_token) {
248 return dav_new_error(p,
249 HTTP_BAD_REQUEST, DAV_ERR_LOCK_UNK_STATE_TOKEN,
250 "The lock token uses an unknown State-token "
251 "format and could not be parsed.");
255 locktoken = apr_pcalloc(p, sizeof(*locktoken));
256 if (apr_uuid_parse(&locktoken->uuid, char_token)) {
257 return dav_new_error(p, HTTP_BAD_REQUEST, DAV_ERR_LOCK_PARSE_TOKEN,
258 "The opaquelocktoken has an incorrect format "
259 "and could not be parsed.");
262 *locktoken_p = locktoken;
267 ** dav_fs_format_locktoken
269 ** Generate the URI for a locktoken
271 static const char *dav_fs_format_locktoken(
273 const dav_locktoken *locktoken)
275 char buf[APR_UUID_FORMATTED_LENGTH + 1];
277 apr_uuid_format(buf, &locktoken->uuid);
278 return apr_pstrcat(p, "opaquelocktoken:", buf, NULL);
282 ** dav_fs_compare_locktoken
284 ** Determine whether two locktokens are the same
286 static int dav_fs_compare_locktoken(
287 const dav_locktoken *lt1,
288 const dav_locktoken *lt2)
290 return dav_compare_locktoken(lt1, lt2);
294 ** dav_fs_really_open_lockdb:
296 ** If the database hasn't been opened yet, then open the thing.
298 static dav_error * dav_fs_really_open_lockdb(dav_lockdb *lockdb)
302 if (lockdb->info->opened)
305 err = dav_dbm_open_direct(lockdb->info->pool,
306 lockdb->info->lockdb_path,
310 return dav_push_error(lockdb->info->pool,
311 HTTP_INTERNAL_SERVER_ERROR,
313 "Could not open the lock database.",
317 /* all right. it is opened now. */
318 lockdb->info->opened = 1;
324 ** dav_fs_open_lockdb:
326 ** "open" the lock database, as specified in the global server configuration.
327 ** If force is TRUE, then the database is opened now, rather than lazily.
329 ** Note that only one can be open read/write.
331 static dav_error * dav_fs_open_lockdb(request_rec *r, int ro, int force,
334 dav_lockdb_combined *comb;
336 comb = apr_pcalloc(r->pool, sizeof(*comb));
337 comb->pub.hooks = &dav_hooks_locks_fs;
339 comb->pub.info = &comb->priv;
341 comb->priv.pool = r->pool;
343 comb->priv.lockdb_path = dav_get_lockdb_path(r);
344 if (comb->priv.lockdb_path == NULL) {
345 return dav_new_error(r->pool, HTTP_INTERNAL_SERVER_ERROR,
347 "A lock database was not specified with the "
348 "DAVLockDB directive. One must be specified "
349 "to use the locking functionality.");
352 /* done initializing. return it. */
353 *lockdb = &comb->pub;
356 /* ### add a higher-level comment? */
357 return dav_fs_really_open_lockdb(*lockdb);
364 ** dav_fs_close_lockdb:
368 static void dav_fs_close_lockdb(dav_lockdb *lockdb)
370 if (lockdb->info->db != NULL)
371 dav_dbm_close(lockdb->info->db);
375 ** dav_fs_build_fname_key
377 ** Given a pathname, build a DAV_TYPE_FNAME lock database key.
379 static apr_datum_t dav_fs_build_fname_key(apr_pool_t *p, const char *pathname)
383 /* ### does this allocation have a proper lifetime? need to check */
384 /* ### can we use a buffer for this? */
386 /* size is TYPE + pathname + null */
387 key.dsize = strlen(pathname) + 2;
388 key.dptr = apr_palloc(p, key.dsize);
389 *key.dptr = DAV_TYPE_FNAME;
390 memcpy(key.dptr + 1, pathname, key.dsize - 1);
391 if (key.dptr[key.dsize - 2] == '/')
392 key.dptr[--key.dsize - 1] = '\0';
397 ** dav_fs_build_key: Given a resource, return a apr_datum_t key
398 ** to look up lock information for this file.
400 ** (inode/dev not supported or file is lock-null):
401 ** apr_datum_t->dvalue = full path
403 ** (inode/dev supported and file exists ):
404 ** apr_datum_t->dvalue = inode, dev
406 static apr_datum_t dav_fs_build_key(apr_pool_t *p,
407 const dav_resource *resource)
409 const char *file = dav_fs_pathname(resource);
414 /* ### use lstat() ?? */
416 * XXX: What for platforms with no IDENT (dev/inode)?
418 rv = apr_stat(&finfo, file, APR_FINFO_IDENT, p);
419 if ((rv == APR_SUCCESS || rv == APR_INCOMPLETE)
420 && ((finfo.valid & APR_FINFO_IDENT) == APR_FINFO_IDENT))
422 /* ### can we use a buffer for this? */
423 key.dsize = 1 + sizeof(finfo.inode) + sizeof(finfo.device);
424 key.dptr = apr_palloc(p, key.dsize);
425 *key.dptr = DAV_TYPE_INODE;
426 memcpy(key.dptr + 1, &finfo.inode, sizeof(finfo.inode));
427 memcpy(key.dptr + 1 + sizeof(finfo.inode), &finfo.device,
428 sizeof(finfo.device));
433 return dav_fs_build_fname_key(p, file);
437 ** dav_fs_lock_expired: return 1 (true) if the given timeout is in the past
438 ** or present (the lock has expired), or 0 (false) if in the future
439 ** (the lock has not yet expired).
441 static int dav_fs_lock_expired(time_t expires)
443 return expires != DAV_TIMEOUT_INFINITE && time(NULL) >= expires;
447 ** dav_fs_save_lock_record: Saves the lock information specified in the
448 ** direct and indirect lock lists about path into the lock database.
449 ** If direct and indirect == NULL, the key is removed.
451 static dav_error * dav_fs_save_lock_record(dav_lockdb *lockdb, apr_datum_t key,
452 dav_lock_discovery *direct,
453 dav_lock_indirect *indirect)
456 apr_datum_t val = { 0 };
458 dav_lock_discovery *dp = direct;
459 dav_lock_indirect *ip = indirect;
463 return dav_new_error(lockdb->info->pool,
464 HTTP_INTERNAL_SERVER_ERROR, 0,
465 "INTERNAL DESIGN ERROR: the lockdb was opened "
466 "readonly, but an attempt to save locks was "
471 if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
472 /* ### add a higher-level error? */
476 /* If nothing to save, delete key */
477 if (dp == NULL && ip == NULL) {
478 /* don't fail if the key is not present */
479 /* ### but what about other errors? */
480 (void) dav_dbm_delete(lockdb->info->db, key);
485 val.dsize += dav_size_direct(dp);
489 val.dsize += dav_size_indirect(ip);
493 /* ### can this be apr_palloc() ? */
494 /* ### hmmm.... investigate the use of a buffer here */
495 ptr = val.dptr = apr_pcalloc(lockdb->info->pool, val.dsize);
500 *ptr++ = DAV_LOCK_DIRECT; /* Direct lock - lock_discovery struct follows */
501 memcpy(ptr, dp, sizeof(dp->f)); /* Fixed portion of struct */
502 ptr += sizeof(dp->f);
503 memcpy(ptr, dp->locktoken, sizeof(*dp->locktoken));
504 ptr += sizeof(*dp->locktoken);
505 if (dp->owner == NULL) {
509 memcpy(ptr, dp->owner, strlen(dp->owner) + 1);
510 ptr += strlen(dp->owner) + 1;
512 if (dp->auth_user == NULL) {
516 memcpy(ptr, dp->auth_user, strlen(dp->auth_user) + 1);
517 ptr += strlen(dp->auth_user) + 1;
524 *ptr++ = DAV_LOCK_INDIRECT; /* Indirect lock prefix */
525 memcpy(ptr, ip->locktoken, sizeof(*ip->locktoken)); /* Locktoken */
526 ptr += sizeof(*ip->locktoken);
527 memcpy(ptr, &ip->timeout, sizeof(ip->timeout)); /* Expire time */
528 ptr += sizeof(ip->timeout);
529 memcpy(ptr, &ip->key.dsize, sizeof(ip->key.dsize)); /* Size of key */
530 ptr += sizeof(ip->key.dsize);
531 memcpy(ptr, ip->key.dptr, ip->key.dsize); /* Key data */
532 ptr += ip->key.dsize;
536 if ((err = dav_dbm_store(lockdb->info->db, key, val)) != NULL) {
537 /* ### more details? add an error_id? */
538 return dav_push_error(lockdb->info->pool,
539 HTTP_INTERNAL_SERVER_ERROR,
540 DAV_ERR_LOCK_SAVE_LOCK,
541 "Could not save lock information.",
549 ** dav_load_lock_record: Reads lock information about key from lock db;
550 ** creates linked lists of the direct and indirect locks.
552 ** If add_method = DAV_APPEND_LIST, the result will be appended to the
553 ** head of the direct and indirect lists supplied.
555 ** Passive lock removal: If lock has timed out, it will not be returned.
556 ** ### How much "logging" does RFC 2518 require?
558 static dav_error * dav_fs_load_lock_record(dav_lockdb *lockdb, apr_datum_t key,
560 dav_lock_discovery **direct,
561 dav_lock_indirect **indirect)
563 apr_pool_t *p = lockdb->info->pool;
565 apr_size_t offset = 0;
566 int need_save = DAV_FALSE;
567 apr_datum_t val = { 0 };
568 dav_lock_discovery *dp;
569 dav_lock_indirect *ip;
570 dav_buffer buf = { 0 };
572 if (add_method != DAV_APPEND_LIST) {
577 if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
578 /* ### add a higher-level error? */
583 ** If we opened readonly and the db wasn't there, then there are no
584 ** locks for this resource. Just exit.
586 if (lockdb->info->db == NULL)
589 if ((err = dav_dbm_fetch(lockdb->info->db, key, &val)) != NULL)
595 while (offset < val.dsize) {
596 switch (*(val.dptr + offset++)) {
597 case DAV_LOCK_DIRECT:
598 /* Create and fill a dav_lock_discovery structure */
600 dp = apr_pcalloc(p, sizeof(*dp));
601 memcpy(dp, val.dptr + offset, sizeof(dp->f));
602 offset += sizeof(dp->f);
603 dp->locktoken = apr_palloc(p, sizeof(*dp->locktoken));
604 memcpy(dp->locktoken, val.dptr + offset, sizeof(*dp->locktoken));
605 offset += sizeof(*dp->locktoken);
606 if (*(val.dptr + offset) == '\0') {
610 dp->owner = apr_pstrdup(p, val.dptr + offset);
611 offset += strlen(dp->owner) + 1;
614 if (*(val.dptr + offset) == '\0') {
618 dp->auth_user = apr_pstrdup(p, val.dptr + offset);
619 offset += strlen(dp->auth_user) + 1;
622 if (!dav_fs_lock_expired(dp->f.timeout)) {
627 need_save = DAV_TRUE;
629 /* Remove timed-out locknull fm .locknull list */
630 if (*key.dptr == DAV_TYPE_FNAME) {
631 const char *fname = key.dptr + 1;
635 /* if we don't see the file, then it's a locknull */
636 rv = apr_lstat(&finfo, fname, APR_FINFO_MIN, p);
637 if (rv != APR_SUCCESS && rv != APR_INCOMPLETE) {
638 if ((err = dav_fs_remove_locknull_member(p, fname, &buf)) != NULL) {
639 /* ### push a higher-level description? */
647 case DAV_LOCK_INDIRECT:
648 /* Create and fill a dav_lock_indirect structure */
650 ip = apr_pcalloc(p, sizeof(*ip));
651 ip->locktoken = apr_palloc(p, sizeof(*ip->locktoken));
652 memcpy(ip->locktoken, val.dptr + offset, sizeof(*ip->locktoken));
653 offset += sizeof(*ip->locktoken);
654 memcpy(&ip->timeout, val.dptr + offset, sizeof(ip->timeout));
655 offset += sizeof(ip->timeout);
656 memcpy(&ip->key.dsize, val.dptr + offset, sizeof(ip->key.dsize)); /* length of datum */
657 offset += sizeof(ip->key.dsize);
658 ip->key.dptr = apr_palloc(p, ip->key.dsize);
659 memcpy(ip->key.dptr, val.dptr + offset, ip->key.dsize);
660 offset += ip->key.dsize;
662 if (!dav_fs_lock_expired(ip->timeout)) {
663 ip->next = *indirect;
667 need_save = DAV_TRUE;
668 /* A locknull resource will never be locked indirectly */
674 dav_dbm_freedatum(lockdb->info->db, val);
676 /* ### should use a computed_desc and insert corrupt token data */
678 return dav_new_error(p,
679 HTTP_INTERNAL_SERVER_ERROR,
680 DAV_ERR_LOCK_CORRUPT_DB,
682 "The lock database was found to "
683 "be corrupt. offset %"
684 APR_SIZE_T_FMT ", c=%02x",
685 offset, val.dptr[offset]));
689 dav_dbm_freedatum(lockdb->info->db, val);
691 /* Clean up this record if we found expired locks */
693 ** ### shouldn't do this if we've been opened READONLY. elide the
694 ** ### timed-out locks from the response, but don't save that info back
696 if (need_save == DAV_TRUE) {
697 return dav_fs_save_lock_record(lockdb, key, *direct, *indirect);
703 /* resolve <indirect>, returning <*direct> */
704 static dav_error * dav_fs_resolve(dav_lockdb *lockdb,
705 dav_lock_indirect *indirect,
706 dav_lock_discovery **direct,
707 dav_lock_discovery **ref_dp,
708 dav_lock_indirect **ref_ip)
711 dav_lock_discovery *dir;
712 dav_lock_indirect *ind;
714 if ((err = dav_fs_load_lock_record(lockdb, indirect->key,
716 &dir, &ind)) != NULL) {
717 /* ### insert a higher-level description? */
720 if (ref_dp != NULL) {
725 for (; dir != NULL; dir = dir->next) {
726 if (!dav_compare_locktoken(indirect->locktoken, dir->locktoken)) {
732 /* No match found (but we should have found one!) */
734 /* ### use a different description and/or error ID? */
735 return dav_new_error(lockdb->info->pool,
736 HTTP_INTERNAL_SERVER_ERROR,
737 DAV_ERR_LOCK_CORRUPT_DB,
738 "The lock database was found to be corrupt. "
739 "An indirect lock's direct lock could not "
743 /* ---------------------------------------------------------------
745 ** Property-related lock functions
750 ** dav_fs_get_supportedlock: Returns a static string for all supportedlock
751 ** properties. I think we save more returning a static string than
752 ** constructing it every time, though it might look cleaner.
754 static const char *dav_fs_get_supportedlock(const dav_resource *resource)
756 static const char supported[] = DEBUG_CR
757 "<D:lockentry>" DEBUG_CR
758 "<D:lockscope><D:exclusive/></D:lockscope>" DEBUG_CR
759 "<D:locktype><D:write/></D:locktype>" DEBUG_CR
760 "</D:lockentry>" DEBUG_CR
761 "<D:lockentry>" DEBUG_CR
762 "<D:lockscope><D:shared/></D:lockscope>" DEBUG_CR
763 "<D:locktype><D:write/></D:locktype>" DEBUG_CR
764 "</D:lockentry>" DEBUG_CR;
769 /* ---------------------------------------------------------------
771 ** General lock functions
775 /* ---------------------------------------------------------------
777 ** Functions dealing with lock-null resources
782 ** dav_fs_load_locknull_list: Returns a dav_buffer dump of the locknull file
783 ** for the given directory.
785 static dav_error * dav_fs_load_locknull_list(apr_pool_t *p, const char *dirpath,
789 apr_file_t *file = NULL;
790 dav_error *err = NULL;
794 dav_buffer_init(p, pbuf, dirpath);
796 if (pbuf->buf[pbuf->cur_len - 1] == '/')
797 pbuf->buf[--pbuf->cur_len] = '\0';
799 dav_buffer_place(p, pbuf, "/" DAV_FS_STATE_DIR "/" DAV_FS_LOCK_NULL_FILE);
801 /* reset this in case we leave w/o reading into the buffer */
804 if (apr_file_open(&file, pbuf->buf, APR_READ | APR_BINARY, APR_OS_DEFAULT,
809 rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, file);
810 if (rv != APR_SUCCESS) {
811 err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
813 "Opened but could not stat file %s",
818 if (finfo.size != (apr_size_t)finfo.size) {
819 err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
821 "Opened but rejected huge file %s",
826 amt = (apr_size_t)finfo.size;
827 dav_set_bufsize(p, pbuf, amt);
828 if (apr_file_read(file, pbuf->buf, &amt) != APR_SUCCESS
829 || amt != finfo.size) {
830 err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
832 "Failure reading locknull file "
835 /* just in case the caller disregards the returned error */
841 apr_file_close(file);
846 ** dav_fs_save_locknull_list: Saves contents of pbuf into the
847 ** locknull file for dirpath.
849 static dav_error * dav_fs_save_locknull_list(apr_pool_t *p, const char *dirpath,
852 const char *pathname;
853 apr_file_t *file = NULL;
854 dav_error *err = NULL;
857 if (pbuf->buf == NULL)
860 dav_fs_ensure_state_dir(p, dirpath);
861 pathname = apr_pstrcat(p,
863 dirpath[strlen(dirpath) - 1] == '/' ? "" : "/",
864 DAV_FS_STATE_DIR "/" DAV_FS_LOCK_NULL_FILE,
867 if (pbuf->cur_len == 0) {
868 /* delete the file if cur_len == 0 */
869 if (apr_file_remove(pathname, p) != 0) {
870 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
872 "Error removing %s", pathname));
877 if (apr_file_open(&file, pathname,
878 APR_WRITE | APR_CREATE | APR_TRUNCATE | APR_BINARY,
879 APR_OS_DEFAULT, p) != APR_SUCCESS) {
880 return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
882 "Error opening %s for writing",
887 if (apr_file_write(file, pbuf->buf, &amt) != APR_SUCCESS
888 || amt != pbuf->cur_len) {
889 err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
891 "Error writing %" APR_SIZE_T_FMT
893 pbuf->cur_len, pathname));
896 apr_file_close(file);
901 ** dav_fs_remove_locknull_member: Removes filename from the locknull list
902 ** for directory path.
904 static dav_error * dav_fs_remove_locknull_member(apr_pool_t *p,
905 const char *filename,
913 char *dirpath = apr_pstrdup(p, filename);
914 char *fname = strrchr(dirpath, '/');
921 len = strlen(fname) + 1;
923 if ((err = dav_fs_load_locknull_list(p, dirpath, pbuf)) != NULL) {
924 /* ### add a higher level description? */
928 for (scan = pbuf->buf, scanend = scan + pbuf->cur_len;
931 scanlen = strlen(scan) + 1;
932 if (len == scanlen && memcmp(fname, scan, scanlen) == 0) {
933 pbuf->cur_len -= scanlen;
934 memmove(scan, scan + scanlen, scanend - (scan + scanlen));
941 if ((err = dav_fs_save_locknull_list(p, dirpath, pbuf)) != NULL) {
942 /* ### add a higher level description? */
950 /* Note: used by dav_fs_repos.c */
951 dav_error * dav_fs_get_locknull_members(
952 const dav_resource *resource,
957 /* ### should test this result value... */
958 (void) dav_fs_dir_file_name(resource, &dirpath, NULL);
959 return dav_fs_load_locknull_list(dav_fs_pool(resource), dirpath, pbuf);
962 /* ### fold into append_lock? */
963 /* ### take an optional buf parameter? */
964 static dav_error * dav_fs_add_locknull_state(
966 const dav_resource *resource)
968 dav_buffer buf = { 0 };
969 apr_pool_t *p = lockdb->info->pool;
974 /* ### should test this result value... */
975 (void) dav_fs_dir_file_name(resource, &dirpath, &fname);
977 if ((err = dav_fs_load_locknull_list(p, dirpath, &buf)) != NULL) {
978 return dav_push_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
979 "Could not load .locknull file.", err);
982 dav_buffer_append(p, &buf, fname);
983 buf.cur_len++; /* we want the null-term here */
985 if ((err = dav_fs_save_locknull_list(p, dirpath, &buf)) != NULL) {
986 return dav_push_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
987 "Could not save .locknull file.", err);
994 ** dav_fs_remove_locknull_state: Given a request, check to see if r->filename
995 ** is/was a lock-null resource. If so, return it to an existant state.
997 ** ### this function is broken... it doesn't check!
999 ** In this implementation, this involves two things:
1000 ** (a) remove it from the list in the appropriate .DAV/locknull file
1001 ** (b) on *nix, convert the key from a filename to an inode.
1003 static dav_error * dav_fs_remove_locknull_state(
1005 const dav_resource *resource)
1007 dav_buffer buf = { 0 };
1009 apr_pool_t *p = lockdb->info->pool;
1010 const char *pathname = dav_fs_pathname(resource);
1012 if ((err = dav_fs_remove_locknull_member(p, pathname, &buf)) != NULL) {
1013 /* ### add a higher-level description? */
1018 dav_lock_discovery *ld;
1019 dav_lock_indirect *id;
1023 ** Fetch the lock(s) that made the resource lock-null. Remove
1024 ** them under the filename key. Obtain the new inode key, and
1025 ** save the same lock information under it.
1027 key = dav_fs_build_fname_key(p, pathname);
1028 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1029 &ld, &id)) != NULL) {
1030 /* ### insert a higher-level error description */
1034 if ((err = dav_fs_save_lock_record(lockdb, key, NULL, NULL)) != NULL) {
1035 /* ### insert a higher-level error description */
1039 key = dav_fs_build_key(p, resource);
1040 if ((err = dav_fs_save_lock_record(lockdb, key, ld, id)) != NULL) {
1041 /* ### insert a higher-level error description */
1049 static dav_error * dav_fs_create_lock(dav_lockdb *lockdb,
1050 const dav_resource *resource,
1055 key = dav_fs_build_key(lockdb->info->pool, resource);
1057 *lock = dav_fs_alloc_lock(lockdb,
1061 (*lock)->is_locknull = !resource->exists;
1066 static dav_error * dav_fs_get_locks(dav_lockdb *lockdb,
1067 const dav_resource *resource,
1071 apr_pool_t *p = lockdb->info->pool;
1074 dav_lock *lock = NULL;
1076 dav_lock_discovery *dp;
1077 dav_lock_indirect *ip;
1080 if (calltype == DAV_GETLOCKS_COMPLETE) {
1081 return dav_new_error(lockdb->info->pool,
1082 HTTP_INTERNAL_SERVER_ERROR, 0,
1083 "INTERNAL DESIGN ERROR: DAV_GETLOCKS_COMPLETE "
1084 "is not yet supported");
1088 key = dav_fs_build_key(p, resource);
1089 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1090 &dp, &ip)) != NULL) {
1091 /* ### push a higher-level desc? */
1095 /* copy all direct locks to the result list */
1096 for (; dp != NULL; dp = dp->next) {
1097 newlock = dav_fs_alloc_lock(lockdb, key, dp->locktoken);
1098 newlock->is_locknull = !resource->exists;
1099 newlock->scope = dp->f.scope;
1100 newlock->type = dp->f.type;
1101 newlock->depth = dp->f.depth;
1102 newlock->timeout = dp->f.timeout;
1103 newlock->owner = dp->owner;
1104 newlock->auth_user = dp->auth_user;
1106 /* hook into the result list */
1107 newlock->next = lock;
1111 /* copy all the indirect locks to the result list. resolve as needed. */
1112 for (; ip != NULL; ip = ip->next) {
1113 newlock = dav_fs_alloc_lock(lockdb, ip->key, ip->locktoken);
1114 newlock->is_locknull = !resource->exists;
1116 if (calltype == DAV_GETLOCKS_RESOLVED) {
1117 if ((err = dav_fs_resolve(lockdb, ip, &dp, NULL, NULL)) != NULL) {
1118 /* ### push a higher-level desc? */
1122 newlock->scope = dp->f.scope;
1123 newlock->type = dp->f.type;
1124 newlock->depth = dp->f.depth;
1125 newlock->timeout = dp->f.timeout;
1126 newlock->owner = dp->owner;
1127 newlock->auth_user = dp->auth_user;
1130 /* DAV_GETLOCKS_PARTIAL */
1131 newlock->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
1134 /* hook into the result list */
1135 newlock->next = lock;
1143 static dav_error * dav_fs_find_lock(dav_lockdb *lockdb,
1144 const dav_resource *resource,
1145 const dav_locktoken *locktoken,
1151 dav_lock_discovery *dp;
1152 dav_lock_indirect *ip;
1156 key = dav_fs_build_key(lockdb->info->pool, resource);
1157 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1158 &dp, &ip)) != NULL) {
1159 /* ### push a higher-level desc? */
1163 for (; dp != NULL; dp = dp->next) {
1164 if (!dav_compare_locktoken(locktoken, dp->locktoken)) {
1165 *lock = dav_fs_alloc_lock(lockdb, key, locktoken);
1166 (*lock)->is_locknull = !resource->exists;
1167 (*lock)->scope = dp->f.scope;
1168 (*lock)->type = dp->f.type;
1169 (*lock)->depth = dp->f.depth;
1170 (*lock)->timeout = dp->f.timeout;
1171 (*lock)->owner = dp->owner;
1172 (*lock)->auth_user = dp->auth_user;
1177 for (; ip != NULL; ip = ip->next) {
1178 if (!dav_compare_locktoken(locktoken, ip->locktoken)) {
1179 *lock = dav_fs_alloc_lock(lockdb, ip->key, locktoken);
1180 (*lock)->is_locknull = !resource->exists;
1182 /* ### nobody uses the resolving right now! */
1184 (*lock)->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
1187 (*lock)->rectype = DAV_LOCKREC_INDIRECT;
1188 if ((err = dav_fs_resolve(lockdb, ip, &dp,
1189 NULL, NULL)) != NULL) {
1190 /* ### push a higher-level desc? */
1193 (*lock)->scope = dp->f.scope;
1194 (*lock)->type = dp->f.type;
1195 (*lock)->depth = dp->f.depth;
1196 (*lock)->timeout = dp->f.timeout;
1197 (*lock)->owner = dp->owner;
1198 (*lock)->auth_user = dp->auth_user;
1207 static dav_error * dav_fs_has_locks(dav_lockdb *lockdb,
1208 const dav_resource *resource,
1216 if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
1217 /* ### insert a higher-level error description */
1222 ** If we opened readonly and the db wasn't there, then there are no
1223 ** locks for this resource. Just exit.
1225 if (lockdb->info->db == NULL)
1228 key = dav_fs_build_key(lockdb->info->pool, resource);
1230 *locks_present = dav_dbm_exists(lockdb->info->db, key);
1235 static dav_error * dav_fs_append_locks(dav_lockdb *lockdb,
1236 const dav_resource *resource,
1238 const dav_lock *lock)
1240 apr_pool_t *p = lockdb->info->pool;
1242 dav_lock_indirect *ip;
1243 dav_lock_discovery *dp;
1246 key = dav_fs_build_key(lockdb->info->pool, resource);
1247 if ((err = dav_fs_load_lock_record(lockdb, key, 0, &dp, &ip)) != NULL) {
1248 /* ### maybe add in a higher-level description */
1253 ** ### when we store the lock more directly, we need to update
1254 ** ### lock->rectype and lock->is_locknull
1257 if (make_indirect) {
1258 for (; lock != NULL; lock = lock->next) {
1260 /* ### this works for any <lock> rectype */
1261 dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
1263 /* ### shut off the const warning for now */
1264 newi->locktoken = (dav_locktoken *)lock->locktoken;
1265 newi->timeout = lock->timeout;
1266 newi->key = lock->info->key;
1272 for (; lock != NULL; lock = lock->next) {
1273 /* create and link in the right kind of lock */
1275 if (lock->rectype == DAV_LOCKREC_DIRECT) {
1276 dav_lock_discovery *newd = apr_pcalloc(p, sizeof(*newd));
1278 newd->f.scope = lock->scope;
1279 newd->f.type = lock->type;
1280 newd->f.depth = lock->depth;
1281 newd->f.timeout = lock->timeout;
1282 /* ### shut off the const warning for now */
1283 newd->locktoken = (dav_locktoken *)lock->locktoken;
1284 newd->owner = lock->owner;
1285 newd->auth_user = lock->auth_user;
1290 /* DAV_LOCKREC_INDIRECT(_PARTIAL) */
1292 dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
1294 /* ### shut off the const warning for now */
1295 newi->locktoken = (dav_locktoken *)lock->locktoken;
1296 newi->key = lock->info->key;
1303 if ((err = dav_fs_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1304 /* ### maybe add a higher-level description */
1308 /* we have a special list for recording locknull resources */
1309 /* ### ack! this can add two copies to the locknull list */
1310 if (!resource->exists
1311 && (err = dav_fs_add_locknull_state(lockdb, resource)) != NULL) {
1312 /* ### maybe add a higher-level description */
1319 static dav_error * dav_fs_remove_lock(dav_lockdb *lockdb,
1320 const dav_resource *resource,
1321 const dav_locktoken *locktoken)
1324 dav_buffer buf = { 0 };
1325 dav_lock_discovery *dh = NULL;
1326 dav_lock_indirect *ih = NULL;
1329 key = dav_fs_build_key(lockdb->info->pool, resource);
1331 if (locktoken != NULL) {
1332 dav_lock_discovery *dp;
1333 dav_lock_discovery *dprev = NULL;
1334 dav_lock_indirect *ip;
1335 dav_lock_indirect *iprev = NULL;
1337 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1338 &dh, &ih)) != NULL) {
1339 /* ### maybe add a higher-level description */
1343 for (dp = dh; dp != NULL; dp = dp->next) {
1344 if (dav_compare_locktoken(locktoken, dp->locktoken) == 0) {
1346 dprev->next = dp->next;
1353 for (ip = ih; ip != NULL; ip = ip->next) {
1354 if (dav_compare_locktoken(locktoken, ip->locktoken) == 0) {
1356 iprev->next = ip->next;
1365 /* save the modified locks, or remove all locks (dh=ih=NULL). */
1366 if ((err = dav_fs_save_lock_record(lockdb, key, dh, ih)) != NULL) {
1367 /* ### maybe add a higher-level description */
1372 ** If this resource is a locknull resource AND no more locks exist,
1373 ** then remove the locknull member.
1375 ** Note: remove_locknull_state() attempts to convert a locknull member
1376 ** to a real member. In this case, all locks are gone, so the
1377 ** locknull resource returns to the null state (ie. doesn't exist),
1378 ** so there is no need to update the lockdb (and it won't find
1379 ** any because a precondition is that none exist).
1381 if (!resource->exists && dh == NULL && ih == NULL
1382 && (err = dav_fs_remove_locknull_member(lockdb->info->pool,
1383 dav_fs_pathname(resource),
1385 /* ### maybe add a higher-level description */
1392 static int dav_fs_do_refresh(dav_lock_discovery *dp,
1393 const dav_locktoken_list *ltl,
1398 for (; ltl != NULL; ltl = ltl->next) {
1399 if (dav_compare_locktoken(dp->locktoken, ltl->locktoken) == 0)
1401 dp->f.timeout = new_time;
1409 static dav_error * dav_fs_refresh_locks(dav_lockdb *lockdb,
1410 const dav_resource *resource,
1411 const dav_locktoken_list *ltl,
1417 dav_lock_discovery *dp;
1418 dav_lock_discovery *dp_scan;
1419 dav_lock_indirect *ip;
1425 key = dav_fs_build_key(lockdb->info->pool, resource);
1426 if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1427 &dp, &ip)) != NULL) {
1428 /* ### maybe add in a higher-level description */
1432 /* ### we should be refreshing direct AND (resolved) indirect locks! */
1434 /* refresh all of the direct locks on this resource */
1435 for (dp_scan = dp; dp_scan != NULL; dp_scan = dp_scan->next) {
1436 if (dav_fs_do_refresh(dp_scan, ltl, new_time)) {
1437 /* the lock was refreshed. return the lock. */
1438 newlock = dav_fs_alloc_lock(lockdb, key, dp_scan->locktoken);
1439 newlock->is_locknull = !resource->exists;
1440 newlock->scope = dp_scan->f.scope;
1441 newlock->type = dp_scan->f.type;
1442 newlock->depth = dp_scan->f.depth;
1443 newlock->timeout = dp_scan->f.timeout;
1444 newlock->owner = dp_scan->owner;
1445 newlock->auth_user = dp_scan->auth_user;
1447 newlock->next = *locks;
1454 /* if we refreshed any locks, then save them back. */
1456 && (err = dav_fs_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1457 /* ### maybe add in a higher-level description */
1461 /* for each indirect lock, find its direct lock and refresh it. */
1462 for (; ip != NULL; ip = ip->next) {
1463 dav_lock_discovery *ref_dp;
1464 dav_lock_indirect *ref_ip;
1466 if ((err = dav_fs_resolve(lockdb, ip, &dp_scan,
1467 &ref_dp, &ref_ip)) != NULL) {
1468 /* ### push a higher-level desc? */
1471 if (dav_fs_do_refresh(dp_scan, ltl, new_time)) {
1472 /* the lock was refreshed. return the lock. */
1473 newlock = dav_fs_alloc_lock(lockdb, ip->key, dp_scan->locktoken);
1474 newlock->is_locknull = !resource->exists;
1475 newlock->scope = dp_scan->f.scope;
1476 newlock->type = dp_scan->f.type;
1477 newlock->depth = dp_scan->f.depth;
1478 newlock->timeout = dp_scan->f.timeout;
1479 newlock->owner = dp_scan->owner;
1480 newlock->auth_user = dp_scan->auth_user;
1482 newlock->next = *locks;
1485 /* save the (resolved) direct lock back */
1486 if ((err = dav_fs_save_lock_record(lockdb, ip->key, ref_dp,
1488 /* ### push a higher-level desc? */
1498 const dav_hooks_locks dav_hooks_locks_fs =
1500 dav_fs_get_supportedlock,
1501 dav_fs_parse_locktoken,
1502 dav_fs_format_locktoken,
1503 dav_fs_compare_locktoken,
1505 dav_fs_close_lockdb,
1506 dav_fs_remove_locknull_state,
1511 dav_fs_append_locks,
1513 dav_fs_refresh_locks,
1514 NULL, /* lookup_resource */