switch to a 60 bit hash
[httpd-crcsyncproxy.git] / modules / dav / lock / locks.c
bloba4e0e214c51a1e09104b51531401b2458059dd39
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 * Generic DAV lock implementation that a DAV provider can use.
21 #include "apr.h"
22 #include "apr_strings.h"
23 #include "apr_file_io.h"
24 #include "apr_uuid.h"
26 #define APR_WANT_MEMFUNC
27 #include "apr_want.h"
29 #include "httpd.h"
30 #include "http_log.h"
32 #include "mod_dav.h"
34 #include "locks.h"
37 /* ---------------------------------------------------------------
39 * Lock database primitives
44 * LOCK DATABASES
46 * Lockdiscovery information is stored in the single lock database specified
47 * by the DAVGenericLockDB directive. Information about this db is stored in
48 * the per-dir configuration.
50 * KEY
52 * The database is keyed by a key_type unsigned char (DAV_TYPE_FNAME)
53 * followed by full path.
55 * VALUE
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),
61 * int depth,
62 * time_t expires,
63 * apr_uuid_t locktoken,
64 * char[] owner,
65 * char[] auth_user]
67 * INDIRECT LOCK: [char (DAV_LOCK_INDIRECT),
68 * apr_uuid_t locktoken,
69 * time_t expires,
70 * int key_size,
71 * char[] key]
72 * The key is to the collection lock that resulted in this indirect lock
75 #define DAV_TRUE 1
76 #define DAV_FALSE 0
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_FNAME 11
87 /* Use the opaquelock scheme for locktokens */
88 struct dav_locktoken {
89 apr_uuid_t uuid;
91 #define dav_compare_locktoken(plt1, plt2) \
92 memcmp(&(plt1)->uuid, &(plt2)->uuid, sizeof((plt1)->uuid))
95 /* #################################################################
96 * ### keep these structures (internal) or move fully to dav_lock?
100 * We need to reliably size the fixed-length portion of
101 * dav_lock_discovery; best to separate it into another
102 * struct for a convenient sizeof, unless we pack lock_discovery.
104 typedef struct dav_lock_discovery_fixed
106 char scope;
107 char type;
108 int depth;
109 time_t timeout;
110 } dav_lock_discovery_fixed;
112 typedef struct dav_lock_discovery
114 struct dav_lock_discovery_fixed f;
116 dav_locktoken *locktoken;
117 const char *owner; /* owner field from activelock */
118 const char *auth_user; /* authenticated user who created the lock */
119 struct dav_lock_discovery *next;
120 } dav_lock_discovery;
122 /* Indirect locks represent locks inherited from containing collections.
123 * They reference the lock token for the collection the lock is
124 * inherited from. A lock provider may also define a key to the
125 * inherited lock, for fast datbase lookup. The key is opaque outside
126 * the lock provider.
128 typedef struct dav_lock_indirect
130 dav_locktoken *locktoken;
131 apr_datum_t key;
132 struct dav_lock_indirect *next;
133 time_t timeout;
134 } dav_lock_indirect;
136 /* ################################################################# */
139 * Stored direct lock info - full lock_discovery length:
140 * prefix + Fixed length + lock token + 2 strings + 2 nulls (one for each
141 * string)
143 #define dav_size_direct(a) (1 + sizeof(dav_lock_discovery_fixed) \
144 + sizeof(apr_uuid_t) \
145 + ((a)->owner ? strlen((a)->owner) : 0) \
146 + ((a)->auth_user ? strlen((a)->auth_user) : 0) \
147 + 2)
149 /* Stored indirect lock info - lock token and apr_datum_t */
150 #define dav_size_indirect(a) (1 + sizeof(apr_uuid_t) \
151 + sizeof(time_t) \
152 + sizeof(int) + (a)->key.dsize)
155 * The lockdb structure.
157 * The <db> field may be NULL, meaning one of two things:
158 * 1) That we have not actually opened the underlying database (yet). The
159 * <opened> field should be false.
160 * 2) We opened it readonly and it wasn't present.
162 * The delayed opening (determined by <opened>) makes creating a lockdb
163 * quick, while deferring the underlying I/O until it is actually required.
165 * We export the notion of a lockdb, but hide the details of it. Most
166 * implementations will use a database of some kind, but it is certainly
167 * possible that alternatives could be used.
169 struct dav_lockdb_private
171 request_rec *r; /* for accessing the uuid state */
172 apr_pool_t *pool; /* a pool to use */
173 const char *lockdb_path; /* where is the lock database? */
175 int opened; /* we opened the database */
176 apr_dbm_t *db; /* if non-NULL, the lock database */
179 typedef struct
181 dav_lockdb pub;
182 dav_lockdb_private priv;
183 } dav_lockdb_combined;
186 * The private part of the lock structure.
188 struct dav_lock_private
190 apr_datum_t key; /* key into the lock database */
192 typedef struct
194 dav_lock pub;
195 dav_lock_private priv;
196 dav_locktoken token;
197 } dav_lock_combined;
200 * This must be forward-declared so the open_lockdb function can use it.
202 extern const dav_hooks_locks dav_hooks_locks_generic;
204 static dav_error * dav_generic_dbm_new_error(apr_dbm_t *db, apr_pool_t *p,
205 apr_status_t status)
207 int save_errno = errno;
208 int errcode;
209 const char *errstr;
210 dav_error *err;
211 char errbuf[200];
213 if (status == APR_SUCCESS) {
214 return NULL;
217 /* There might not be a <db> if we had problems creating it. */
218 if (db == NULL) {
219 errcode = 1;
220 errstr = "Could not open property database.";
222 else {
223 (void) apr_dbm_geterror(db, &errcode, errbuf, sizeof(errbuf));
224 errstr = apr_pstrdup(p, errbuf);
227 err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, errcode, errstr);
228 err->save_errno = save_errno;
229 return err;
232 /* internal function for creating locks */
233 static dav_lock *dav_generic_alloc_lock(dav_lockdb *lockdb, apr_datum_t key,
234 const dav_locktoken *locktoken)
236 dav_lock_combined *comb;
238 comb = apr_pcalloc(lockdb->info->pool, sizeof(*comb));
239 comb->pub.rectype = DAV_LOCKREC_DIRECT;
240 comb->pub.info = &comb->priv;
241 comb->priv.key = key;
243 if (locktoken == NULL) {
244 comb->pub.locktoken = &comb->token;
245 apr_uuid_get(&comb->token.uuid);
247 else {
248 comb->pub.locktoken = locktoken;
251 return &comb->pub;
255 * dav_generic_parse_locktoken
257 * Parse an opaquelocktoken URI into a locktoken.
259 static dav_error * dav_generic_parse_locktoken(apr_pool_t *p,
260 const char *char_token,
261 dav_locktoken **locktoken_p)
263 dav_locktoken *locktoken;
265 if (ap_strstr_c(char_token, "opaquelocktoken:") != char_token) {
266 return dav_new_error(p,
267 HTTP_BAD_REQUEST, DAV_ERR_LOCK_UNK_STATE_TOKEN,
268 "The lock token uses an unknown State-token "
269 "format and could not be parsed.");
271 char_token += 16;
273 locktoken = apr_pcalloc(p, sizeof(*locktoken));
274 if (apr_uuid_parse(&locktoken->uuid, char_token)) {
275 return dav_new_error(p, HTTP_BAD_REQUEST, DAV_ERR_LOCK_PARSE_TOKEN,
276 "The opaquelocktoken has an incorrect format "
277 "and could not be parsed.");
280 *locktoken_p = locktoken;
281 return NULL;
285 * dav_generic_format_locktoken
287 * Generate the URI for a locktoken
289 static const char *dav_generic_format_locktoken(apr_pool_t *p,
290 const dav_locktoken *locktoken)
292 char buf[APR_UUID_FORMATTED_LENGTH + 1];
294 apr_uuid_format(buf, &locktoken->uuid);
295 return apr_pstrcat(p, "opaquelocktoken:", buf, NULL);
299 * dav_generic_compare_locktoken
301 * Determine whether two locktokens are the same
303 static int dav_generic_compare_locktoken(const dav_locktoken *lt1,
304 const dav_locktoken *lt2)
306 return dav_compare_locktoken(lt1, lt2);
310 * dav_generic_really_open_lockdb:
312 * If the database hasn't been opened yet, then open the thing.
314 static dav_error * dav_generic_really_open_lockdb(dav_lockdb *lockdb)
316 dav_error *err;
317 apr_status_t status;
319 if (lockdb->info->opened) {
320 return NULL;
323 status = apr_dbm_open(&lockdb->info->db, lockdb->info->lockdb_path,
324 lockdb->ro ? APR_DBM_READONLY : APR_DBM_RWCREATE,
325 APR_OS_DEFAULT, lockdb->info->pool);
327 if (status) {
328 err = dav_generic_dbm_new_error(lockdb->info->db, lockdb->info->pool,
329 status);
330 return dav_push_error(lockdb->info->pool,
331 HTTP_INTERNAL_SERVER_ERROR,
332 DAV_ERR_LOCK_OPENDB,
333 "Could not open the lock database.",
334 err);
337 /* all right. it is opened now. */
338 lockdb->info->opened = 1;
340 return NULL;
344 * dav_generic_open_lockdb:
346 * "open" the lock database, as specified in the global server configuration.
347 * If force is TRUE, then the database is opened now, rather than lazily.
349 * Note that only one can be open read/write.
351 static dav_error * dav_generic_open_lockdb(request_rec *r, int ro, int force,
352 dav_lockdb **lockdb)
354 dav_lockdb_combined *comb;
356 comb = apr_pcalloc(r->pool, sizeof(*comb));
357 comb->pub.hooks = &dav_hooks_locks_generic;
358 comb->pub.ro = ro;
359 comb->pub.info = &comb->priv;
360 comb->priv.r = r;
361 comb->priv.pool = r->pool;
363 comb->priv.lockdb_path = dav_generic_get_lockdb_path(r);
364 if (comb->priv.lockdb_path == NULL) {
365 return dav_new_error(r->pool, HTTP_INTERNAL_SERVER_ERROR,
366 DAV_ERR_LOCK_NO_DB,
367 "A lock database was not specified with the "
368 "DAVGenericLockDB directive. One must be "
369 "specified to use the locking functionality.");
372 /* done initializing. return it. */
373 *lockdb = &comb->pub;
375 if (force) {
376 /* ### add a higher-level comment? */
377 return dav_generic_really_open_lockdb(*lockdb);
380 return NULL;
384 * dav_generic_close_lockdb:
386 * Close it. Duh.
388 static void dav_generic_close_lockdb(dav_lockdb *lockdb)
390 if (lockdb->info->db != NULL) {
391 apr_dbm_close(lockdb->info->db);
393 lockdb->info->opened = 0;
397 * dav_generic_build_key
399 * Given a pathname, build a DAV_TYPE_FNAME lock database key.
401 static apr_datum_t dav_generic_build_key(apr_pool_t *p,
402 const dav_resource *resource)
404 apr_datum_t key;
405 const char *pathname = resource->uri;
407 /* ### does this allocation have a proper lifetime? need to check */
408 /* ### can we use a buffer for this? */
410 /* size is TYPE + pathname + null */
411 key.dsize = strlen(pathname) + 2;
412 key.dptr = apr_palloc(p, key.dsize);
413 *key.dptr = DAV_TYPE_FNAME;
414 memcpy(key.dptr + 1, pathname, key.dsize - 1);
415 if (key.dptr[key.dsize - 2] == '/')
416 key.dptr[--key.dsize - 1] = '\0';
417 return key;
421 * dav_generic_lock_expired: return 1 (true) if the given timeout is in the
422 * past or present (the lock has expired), or 0 (false) if in the future
423 * (the lock has not yet expired).
425 static int dav_generic_lock_expired(time_t expires)
427 return expires != DAV_TIMEOUT_INFINITE && time(NULL) >= expires;
431 * dav_generic_save_lock_record: Saves the lock information specified in the
432 * direct and indirect lock lists about path into the lock database.
433 * If direct and indirect == NULL, the key is removed.
435 static dav_error * dav_generic_save_lock_record(dav_lockdb *lockdb,
436 apr_datum_t key,
437 dav_lock_discovery *direct,
438 dav_lock_indirect *indirect)
440 dav_error *err;
441 apr_status_t status;
442 apr_datum_t val = { 0 };
443 char *ptr;
444 dav_lock_discovery *dp = direct;
445 dav_lock_indirect *ip = indirect;
447 #if DAV_DEBUG
448 if (lockdb->ro) {
449 return dav_new_error(lockdb->info->pool,
450 HTTP_INTERNAL_SERVER_ERROR, 0,
451 "INTERNAL DESIGN ERROR: the lockdb was opened "
452 "readonly, but an attempt to save locks was "
453 "performed.");
455 #endif
457 if ((err = dav_generic_really_open_lockdb(lockdb)) != NULL) {
458 /* ### add a higher-level error? */
459 return err;
462 /* If nothing to save, delete key */
463 if (dp == NULL && ip == NULL) {
464 /* don't fail if the key is not present */
465 /* ### but what about other errors? */
466 apr_dbm_delete(lockdb->info->db, key);
467 return NULL;
470 while(dp) {
471 val.dsize += dav_size_direct(dp);
472 dp = dp->next;
474 while(ip) {
475 val.dsize += dav_size_indirect(ip);
476 ip = ip->next;
479 /* ### can this be apr_palloc() ? */
480 /* ### hmmm.... investigate the use of a buffer here */
481 ptr = val.dptr = apr_pcalloc(lockdb->info->pool, val.dsize);
482 dp = direct;
483 ip = indirect;
485 while(dp) {
486 /* Direct lock - lock_discovery struct follows */
487 *ptr++ = DAV_LOCK_DIRECT;
488 memcpy(ptr, dp, sizeof(dp->f)); /* Fixed portion of struct */
489 ptr += sizeof(dp->f);
490 memcpy(ptr, dp->locktoken, sizeof(*dp->locktoken));
491 ptr += sizeof(*dp->locktoken);
492 if (dp->owner == NULL) {
493 *ptr++ = '\0';
495 else {
496 memcpy(ptr, dp->owner, strlen(dp->owner) + 1);
497 ptr += strlen(dp->owner) + 1;
499 if (dp->auth_user == NULL) {
500 *ptr++ = '\0';
502 else {
503 memcpy(ptr, dp->auth_user, strlen(dp->auth_user) + 1);
504 ptr += strlen(dp->auth_user) + 1;
507 dp = dp->next;
510 while(ip) {
511 /* Indirect lock prefix */
512 *ptr++ = DAV_LOCK_INDIRECT;
514 memcpy(ptr, ip->locktoken, sizeof(*ip->locktoken));
515 ptr += sizeof(*ip->locktoken);
517 memcpy(ptr, &ip->timeout, sizeof(ip->timeout));
518 ptr += sizeof(ip->timeout);
520 memcpy(ptr, &ip->key.dsize, sizeof(ip->key.dsize));
521 ptr += sizeof(ip->key.dsize);
523 memcpy(ptr, ip->key.dptr, ip->key.dsize);
524 ptr += ip->key.dsize;
526 ip = ip->next;
529 if ((status = apr_dbm_store(lockdb->info->db, key, val)) != APR_SUCCESS) {
530 /* ### more details? add an error_id? */
531 err = dav_generic_dbm_new_error(lockdb->info->db, lockdb->info->pool,
532 status);
533 return dav_push_error(lockdb->info->pool,
534 HTTP_INTERNAL_SERVER_ERROR,
535 DAV_ERR_LOCK_SAVE_LOCK,
536 "Could not save lock information.",
537 err);
540 return NULL;
544 * dav_load_lock_record: Reads lock information about key from lock db;
545 * creates linked lists of the direct and indirect locks.
547 * If add_method = DAV_APPEND_LIST, the result will be appended to the
548 * head of the direct and indirect lists supplied.
550 * Passive lock removal: If lock has timed out, it will not be returned.
551 * ### How much "logging" does RFC 2518 require?
553 static dav_error * dav_generic_load_lock_record(dav_lockdb *lockdb,
554 apr_datum_t key,
555 int add_method,
556 dav_lock_discovery **direct,
557 dav_lock_indirect **indirect)
559 apr_pool_t *p = lockdb->info->pool;
560 dav_error *err;
561 apr_status_t status;
562 apr_size_t offset = 0;
563 int need_save = DAV_FALSE;
564 apr_datum_t val = { 0 };
565 dav_lock_discovery *dp;
566 dav_lock_indirect *ip;
568 if (add_method != DAV_APPEND_LIST) {
569 *direct = NULL;
570 *indirect = NULL;
573 if ((err = dav_generic_really_open_lockdb(lockdb)) != NULL) {
574 /* ### add a higher-level error? */
575 return err;
579 * If we opened readonly and the db wasn't there, then there are no
580 * locks for this resource. Just exit.
582 if (lockdb->info->db == NULL) {
583 return NULL;
586 if ((status = apr_dbm_fetch(lockdb->info->db, key, &val)) != APR_SUCCESS) {
587 return dav_generic_dbm_new_error(lockdb->info->db, p, status);
590 if (!val.dsize) {
591 return NULL;
594 while (offset < val.dsize) {
595 switch (*(val.dptr + offset++)) {
596 case DAV_LOCK_DIRECT:
597 /* Create and fill a dav_lock_discovery structure */
599 dp = apr_pcalloc(p, sizeof(*dp));
601 /* Copy the dav_lock_discovery_fixed portion */
602 memcpy(dp, val.dptr + offset, sizeof(dp->f));
603 offset += sizeof(dp->f);
605 /* Copy the lock token. */
606 dp->locktoken = apr_pmemdup(p, val.dptr + offset, sizeof(*dp->locktoken));
607 offset += sizeof(*dp->locktoken);
609 /* Do we have an owner field? */
610 if (*(val.dptr + offset) == '\0') {
611 ++offset;
613 else {
614 apr_size_t len = strlen(val.dptr + offset);
615 dp->owner = apr_pstrmemdup(p, val.dptr + offset, len);
616 offset += len + 1;
619 if (*(val.dptr + offset) == '\0') {
620 ++offset;
622 else {
623 apr_size_t len = strlen(val.dptr + offset);
624 dp->auth_user = apr_pstrmemdup(p, val.dptr + offset, len);
625 offset += len + 1;
628 if (!dav_generic_lock_expired(dp->f.timeout)) {
629 dp->next = *direct;
630 *direct = dp;
632 else {
633 need_save = DAV_TRUE;
635 break;
637 case DAV_LOCK_INDIRECT:
638 /* Create and fill a dav_lock_indirect structure */
640 ip = apr_pcalloc(p, sizeof(*ip));
641 ip->locktoken = apr_pmemdup(p, val.dptr + offset, sizeof(*ip->locktoken));
642 offset += sizeof(*ip->locktoken);
643 memcpy(&ip->timeout, val.dptr + offset, sizeof(ip->timeout));
644 offset += sizeof(ip->timeout);
645 /* length of datum */
646 ip->key.dsize = *((int *) (val.dptr + offset));
647 offset += sizeof(ip->key.dsize);
648 ip->key.dptr = apr_pmemdup(p, val.dptr + offset, ip->key.dsize);
649 offset += ip->key.dsize;
651 if (!dav_generic_lock_expired(ip->timeout)) {
652 ip->next = *indirect;
653 *indirect = ip;
655 else {
656 need_save = DAV_TRUE;
659 break;
661 default:
662 apr_dbm_freedatum(lockdb->info->db, val);
664 /* ### should use a computed_desc and insert corrupt token data */
665 --offset;
666 return dav_new_error(p,
667 HTTP_INTERNAL_SERVER_ERROR,
668 DAV_ERR_LOCK_CORRUPT_DB,
669 apr_psprintf(p,
670 "The lock database was found to "
671 "be corrupt. offset %"
672 APR_SIZE_T_FMT ", c=%02x",
673 offset, val.dptr[offset]));
677 apr_dbm_freedatum(lockdb->info->db, val);
679 /* Clean up this record if we found expired locks */
681 * ### shouldn't do this if we've been opened READONLY. elide the
682 * ### timed-out locks from the response, but don't save that info back
684 if (need_save == DAV_TRUE) {
685 return dav_generic_save_lock_record(lockdb, key, *direct, *indirect);
688 return NULL;
691 /* resolve <indirect>, returning <*direct> */
692 static dav_error * dav_generic_resolve(dav_lockdb *lockdb,
693 dav_lock_indirect *indirect,
694 dav_lock_discovery **direct,
695 dav_lock_discovery **ref_dp,
696 dav_lock_indirect **ref_ip)
698 dav_error *err;
699 dav_lock_discovery *dir;
700 dav_lock_indirect *ind;
702 if ((err = dav_generic_load_lock_record(lockdb, indirect->key,
703 DAV_CREATE_LIST,
704 &dir, &ind)) != NULL) {
705 /* ### insert a higher-level description? */
706 return err;
708 if (ref_dp != NULL) {
709 *ref_dp = dir;
710 *ref_ip = ind;
713 for (; dir != NULL; dir = dir->next) {
714 if (!dav_compare_locktoken(indirect->locktoken, dir->locktoken)) {
715 *direct = dir;
716 return NULL;
720 /* No match found (but we should have found one!) */
722 /* ### use a different description and/or error ID? */
723 return dav_new_error(lockdb->info->pool,
724 HTTP_INTERNAL_SERVER_ERROR,
725 DAV_ERR_LOCK_CORRUPT_DB,
726 "The lock database was found to be corrupt. "
727 "An indirect lock's direct lock could not "
728 "be found.");
731 /* ---------------------------------------------------------------
733 * Property-related lock functions
738 * dav_generic_get_supportedlock: Returns a static string for all
739 * supportedlock properties. I think we save more returning a static string
740 * than constructing it every time, though it might look cleaner.
742 static const char *dav_generic_get_supportedlock(const dav_resource *resource)
744 static const char supported[] = DEBUG_CR
745 "<D:lockentry>" DEBUG_CR
746 "<D:lockscope><D:exclusive/></D:lockscope>" DEBUG_CR
747 "<D:locktype><D:write/></D:locktype>" DEBUG_CR
748 "</D:lockentry>" DEBUG_CR
749 "<D:lockentry>" DEBUG_CR
750 "<D:lockscope><D:shared/></D:lockscope>" DEBUG_CR
751 "<D:locktype><D:write/></D:locktype>" DEBUG_CR
752 "</D:lockentry>" DEBUG_CR;
754 return supported;
757 /* ---------------------------------------------------------------
759 * General lock functions
763 static dav_error * dav_generic_remove_locknull_state(dav_lockdb *lockdb,
764 const dav_resource *resource)
766 /* We don't need to do anything. */
767 return NULL;
770 static dav_error * dav_generic_create_lock(dav_lockdb *lockdb,
771 const dav_resource *resource,
772 dav_lock **lock)
774 apr_datum_t key;
776 key = dav_generic_build_key(lockdb->info->pool, resource);
778 *lock = dav_generic_alloc_lock(lockdb, key, NULL);
780 (*lock)->is_locknull = !resource->exists;
782 return NULL;
785 static dav_error * dav_generic_get_locks(dav_lockdb *lockdb,
786 const dav_resource *resource,
787 int calltype,
788 dav_lock **locks)
790 apr_pool_t *p = lockdb->info->pool;
791 apr_datum_t key;
792 dav_error *err;
793 dav_lock *lock = NULL;
794 dav_lock *newlock;
795 dav_lock_discovery *dp;
796 dav_lock_indirect *ip;
798 #if DAV_DEBUG
799 if (calltype == DAV_GETLOCKS_COMPLETE) {
800 return dav_new_error(lockdb->info->pool,
801 HTTP_INTERNAL_SERVER_ERROR, 0,
802 "INTERNAL DESIGN ERROR: DAV_GETLOCKS_COMPLETE "
803 "is not yet supported");
805 #endif
807 key = dav_generic_build_key(p, resource);
808 if ((err = dav_generic_load_lock_record(lockdb, key, DAV_CREATE_LIST,
809 &dp, &ip)) != NULL) {
810 /* ### push a higher-level desc? */
811 return err;
814 /* copy all direct locks to the result list */
815 for (; dp != NULL; dp = dp->next) {
816 newlock = dav_generic_alloc_lock(lockdb, key, dp->locktoken);
817 newlock->is_locknull = !resource->exists;
818 newlock->scope = dp->f.scope;
819 newlock->type = dp->f.type;
820 newlock->depth = dp->f.depth;
821 newlock->timeout = dp->f.timeout;
822 newlock->owner = dp->owner;
823 newlock->auth_user = dp->auth_user;
825 /* hook into the result list */
826 newlock->next = lock;
827 lock = newlock;
830 /* copy all the indirect locks to the result list. resolve as needed. */
831 for (; ip != NULL; ip = ip->next) {
832 newlock = dav_generic_alloc_lock(lockdb, ip->key, ip->locktoken);
833 newlock->is_locknull = !resource->exists;
835 if (calltype == DAV_GETLOCKS_RESOLVED) {
836 err = dav_generic_resolve(lockdb, ip, &dp, NULL, NULL);
837 if (err != NULL) {
838 /* ### push a higher-level desc? */
839 return err;
842 newlock->scope = dp->f.scope;
843 newlock->type = dp->f.type;
844 newlock->depth = dp->f.depth;
845 newlock->timeout = dp->f.timeout;
846 newlock->owner = dp->owner;
847 newlock->auth_user = dp->auth_user;
849 else {
850 /* DAV_GETLOCKS_PARTIAL */
851 newlock->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
854 /* hook into the result list */
855 newlock->next = lock;
856 lock = newlock;
859 *locks = lock;
860 return NULL;
863 static dav_error * dav_generic_find_lock(dav_lockdb *lockdb,
864 const dav_resource *resource,
865 const dav_locktoken *locktoken,
866 int partial_ok,
867 dav_lock **lock)
869 dav_error *err;
870 apr_datum_t key;
871 dav_lock_discovery *dp;
872 dav_lock_indirect *ip;
874 *lock = NULL;
876 key = dav_generic_build_key(lockdb->info->pool, resource);
877 if ((err = dav_generic_load_lock_record(lockdb, key, DAV_CREATE_LIST,
878 &dp, &ip)) != NULL) {
879 /* ### push a higher-level desc? */
880 return err;
883 for (; dp != NULL; dp = dp->next) {
884 if (!dav_compare_locktoken(locktoken, dp->locktoken)) {
885 *lock = dav_generic_alloc_lock(lockdb, key, locktoken);
886 (*lock)->is_locknull = !resource->exists;
887 (*lock)->scope = dp->f.scope;
888 (*lock)->type = dp->f.type;
889 (*lock)->depth = dp->f.depth;
890 (*lock)->timeout = dp->f.timeout;
891 (*lock)->owner = dp->owner;
892 (*lock)->auth_user = dp->auth_user;
893 return NULL;
897 for (; ip != NULL; ip = ip->next) {
898 if (!dav_compare_locktoken(locktoken, ip->locktoken)) {
899 *lock = dav_generic_alloc_lock(lockdb, ip->key, locktoken);
900 (*lock)->is_locknull = !resource->exists;
902 /* ### nobody uses the resolving right now! */
903 if (partial_ok) {
904 (*lock)->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
906 else {
907 (*lock)->rectype = DAV_LOCKREC_INDIRECT;
908 if ((err = dav_generic_resolve(lockdb, ip, &dp,
909 NULL, NULL)) != NULL) {
910 /* ### push a higher-level desc? */
911 return err;
913 (*lock)->scope = dp->f.scope;
914 (*lock)->type = dp->f.type;
915 (*lock)->depth = dp->f.depth;
916 (*lock)->timeout = dp->f.timeout;
917 (*lock)->owner = dp->owner;
918 (*lock)->auth_user = dp->auth_user;
920 return NULL;
924 return NULL;
927 static dav_error * dav_generic_has_locks(dav_lockdb *lockdb,
928 const dav_resource *resource,
929 int *locks_present)
931 dav_error *err;
932 apr_datum_t key;
934 *locks_present = 0;
936 if ((err = dav_generic_really_open_lockdb(lockdb)) != NULL) {
937 /* ### insert a higher-level error description */
938 return err;
942 * If we opened readonly and the db wasn't there, then there are no
943 * locks for this resource. Just exit.
945 if (lockdb->info->db == NULL)
946 return NULL;
948 key = dav_generic_build_key(lockdb->info->pool, resource);
950 *locks_present = apr_dbm_exists(lockdb->info->db, key);
952 return NULL;
955 static dav_error * dav_generic_append_locks(dav_lockdb *lockdb,
956 const dav_resource *resource,
957 int make_indirect,
958 const dav_lock *lock)
960 apr_pool_t *p = lockdb->info->pool;
961 dav_error *err;
962 dav_lock_indirect *ip;
963 dav_lock_discovery *dp;
964 apr_datum_t key;
966 key = dav_generic_build_key(lockdb->info->pool, resource);
968 err = dav_generic_load_lock_record(lockdb, key, 0, &dp, &ip);
969 if (err != NULL) {
970 /* ### maybe add in a higher-level description */
971 return err;
975 * ### when we store the lock more directly, we need to update
976 * ### lock->rectype and lock->is_locknull
979 if (make_indirect) {
980 for (; lock != NULL; lock = lock->next) {
982 /* ### this works for any <lock> rectype */
983 dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
985 /* ### shut off the const warning for now */
986 newi->locktoken = (dav_locktoken *)lock->locktoken;
987 newi->timeout = lock->timeout;
988 newi->key = lock->info->key;
989 newi->next = ip;
990 ip = newi;
993 else {
994 for (; lock != NULL; lock = lock->next) {
995 /* create and link in the right kind of lock */
997 if (lock->rectype == DAV_LOCKREC_DIRECT) {
998 dav_lock_discovery *newd = apr_pcalloc(p, sizeof(*newd));
1000 newd->f.scope = lock->scope;
1001 newd->f.type = lock->type;
1002 newd->f.depth = lock->depth;
1003 newd->f.timeout = lock->timeout;
1004 /* ### shut off the const warning for now */
1005 newd->locktoken = (dav_locktoken *)lock->locktoken;
1006 newd->owner = lock->owner;
1007 newd->auth_user = lock->auth_user;
1008 newd->next = dp;
1009 dp = newd;
1011 else {
1012 /* DAV_LOCKREC_INDIRECT(_PARTIAL) */
1014 dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
1016 /* ### shut off the const warning for now */
1017 newi->locktoken = (dav_locktoken *)lock->locktoken;
1018 newi->key = lock->info->key;
1019 newi->next = ip;
1020 ip = newi;
1025 if ((err = dav_generic_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1026 /* ### maybe add a higher-level description */
1027 return err;
1030 return NULL;
1033 static dav_error * dav_generic_remove_lock(dav_lockdb *lockdb,
1034 const dav_resource *resource,
1035 const dav_locktoken *locktoken)
1037 dav_error *err;
1038 dav_lock_discovery *dh = NULL;
1039 dav_lock_indirect *ih = NULL;
1040 apr_datum_t key;
1042 key = dav_generic_build_key(lockdb->info->pool, resource);
1044 if (locktoken != NULL) {
1045 dav_lock_discovery *dp;
1046 dav_lock_discovery *dprev = NULL;
1047 dav_lock_indirect *ip;
1048 dav_lock_indirect *iprev = NULL;
1050 if ((err = dav_generic_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1051 &dh, &ih)) != NULL) {
1052 /* ### maybe add a higher-level description */
1053 return err;
1056 for (dp = dh; dp != NULL; dp = dp->next) {
1057 if (dav_compare_locktoken(locktoken, dp->locktoken) == 0) {
1058 if (dprev)
1059 dprev->next = dp->next;
1060 else
1061 dh = dh->next;
1063 dprev = dp;
1066 for (ip = ih; ip != NULL; ip = ip->next) {
1067 if (dav_compare_locktoken(locktoken, ip->locktoken) == 0) {
1068 if (iprev)
1069 iprev->next = ip->next;
1070 else
1071 ih = ih->next;
1073 iprev = ip;
1078 /* save the modified locks, or remove all locks (dh=ih=NULL). */
1079 if ((err = dav_generic_save_lock_record(lockdb, key, dh, ih)) != NULL) {
1080 /* ### maybe add a higher-level description */
1081 return err;
1084 return NULL;
1087 static int dav_generic_do_refresh(dav_lock_discovery *dp,
1088 const dav_locktoken_list *ltl,
1089 time_t new_time)
1091 int dirty = 0;
1093 for (; ltl != NULL; ltl = ltl->next) {
1094 if (dav_compare_locktoken(dp->locktoken, ltl->locktoken) == 0)
1096 dp->f.timeout = new_time;
1097 dirty = 1;
1101 return dirty;
1104 static dav_error * dav_generic_refresh_locks(dav_lockdb *lockdb,
1105 const dav_resource *resource,
1106 const dav_locktoken_list *ltl,
1107 time_t new_time,
1108 dav_lock **locks)
1110 dav_error *err;
1111 apr_datum_t key;
1112 dav_lock_discovery *dp;
1113 dav_lock_discovery *dp_scan;
1114 dav_lock_indirect *ip;
1115 int dirty = 0;
1116 dav_lock *newlock;
1118 *locks = NULL;
1120 key = dav_generic_build_key(lockdb->info->pool, resource);
1121 if ((err = dav_generic_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1122 &dp, &ip)) != NULL) {
1123 /* ### maybe add in a higher-level description */
1124 return err;
1127 /* ### we should be refreshing direct AND (resolved) indirect locks! */
1129 /* refresh all of the direct locks on this resource */
1130 for (dp_scan = dp; dp_scan != NULL; dp_scan = dp_scan->next) {
1131 if (dav_generic_do_refresh(dp_scan, ltl, new_time)) {
1132 /* the lock was refreshed. return the lock. */
1133 newlock = dav_generic_alloc_lock(lockdb, key, dp_scan->locktoken);
1134 newlock->is_locknull = !resource->exists;
1135 newlock->scope = dp_scan->f.scope;
1136 newlock->type = dp_scan->f.type;
1137 newlock->depth = dp_scan->f.depth;
1138 newlock->timeout = dp_scan->f.timeout;
1139 newlock->owner = dp_scan->owner;
1140 newlock->auth_user = dp_scan->auth_user;
1142 newlock->next = *locks;
1143 *locks = newlock;
1145 dirty = 1;
1149 /* if we refreshed any locks, then save them back. */
1150 if (dirty
1151 && (err = dav_generic_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1152 /* ### maybe add in a higher-level description */
1153 return err;
1156 /* for each indirect lock, find its direct lock and refresh it. */
1157 for (; ip != NULL; ip = ip->next) {
1158 dav_lock_discovery *ref_dp;
1159 dav_lock_indirect *ref_ip;
1161 if ((err = dav_generic_resolve(lockdb, ip, &dp_scan,
1162 &ref_dp, &ref_ip)) != NULL) {
1163 /* ### push a higher-level desc? */
1164 return err;
1166 if (dav_generic_do_refresh(dp_scan, ltl, new_time)) {
1167 /* the lock was refreshed. return the lock. */
1168 newlock = dav_generic_alloc_lock(lockdb, ip->key, dp->locktoken);
1169 newlock->is_locknull = !resource->exists;
1170 newlock->scope = dp->f.scope;
1171 newlock->type = dp->f.type;
1172 newlock->depth = dp->f.depth;
1173 newlock->timeout = dp->f.timeout;
1174 newlock->owner = dp->owner;
1175 newlock->auth_user = dp_scan->auth_user;
1177 newlock->next = *locks;
1178 *locks = newlock;
1180 /* save the (resolved) direct lock back */
1181 if ((err = dav_generic_save_lock_record(lockdb, ip->key, ref_dp,
1182 ref_ip)) != NULL) {
1183 /* ### push a higher-level desc? */
1184 return err;
1189 return NULL;
1193 const dav_hooks_locks dav_hooks_locks_generic =
1195 dav_generic_get_supportedlock,
1196 dav_generic_parse_locktoken,
1197 dav_generic_format_locktoken,
1198 dav_generic_compare_locktoken,
1199 dav_generic_open_lockdb,
1200 dav_generic_close_lockdb,
1201 dav_generic_remove_locknull_state,
1202 dav_generic_create_lock,
1203 dav_generic_get_locks,
1204 dav_generic_find_lock,
1205 dav_generic_has_locks,
1206 dav_generic_append_locks,
1207 dav_generic_remove_lock,
1208 dav_generic_refresh_locks,
1209 NULL, /* lookup_resource */
1211 NULL /* ctx */