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.
22
#include "apr_strings.h"
23
#include "apr_file_io.h"
26
#define APR_WANT_MEMFUNC
37
/* ---------------------------------------------------------------
39
* Lock database primitives
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.
52
* The database is keyed by a key_type unsigned char (DAV_TYPE_FNAME)
53
* followed by full path.
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,
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_FNAME 11
87
/* Use the opaquelock scheme for locktokens */
88
struct dav_locktoken {
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
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
128
typedef struct dav_lock_indirect
130
dav_locktoken *locktoken;
132
struct dav_lock_indirect *next;
136
/* ################################################################# */
139
* Stored direct lock info - full lock_discovery length:
140
* prefix + Fixed length + lock token + 2 strings + 2 nulls (one for each
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) \
149
/* Stored indirect lock info - lock token and apr_datum_t */
150
#define dav_size_indirect(a) (1 + sizeof(apr_uuid_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 */
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 */
195
dav_lock_private priv;
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,
207
int save_errno = errno;
213
if (status == APR_SUCCESS) {
217
/* There might not be a <db> if we had problems creating it. */
220
errstr = "Could not open property database.";
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;
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);
248
comb->pub.locktoken = locktoken;
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.");
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;
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)
319
if (lockdb->info->opened) {
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);
328
err = dav_generic_dbm_new_error(lockdb->info->db, lockdb->info->pool,
330
return dav_push_error(lockdb->info->pool,
331
HTTP_INTERNAL_SERVER_ERROR,
333
"Could not open the lock database.",
337
/* all right. it is opened now. */
338
lockdb->info->opened = 1;
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,
354
dav_lockdb_combined *comb;
356
comb = apr_pcalloc(r->pool, sizeof(*comb));
357
comb->pub.hooks = &dav_hooks_locks_generic;
359
comb->pub.info = &comb->priv;
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,
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;
376
/* ### add a higher-level comment? */
377
return dav_generic_really_open_lockdb(*lockdb);
384
* dav_generic_close_lockdb:
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)
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';
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,
437
dav_lock_discovery *direct,
438
dav_lock_indirect *indirect)
442
apr_datum_t val = { 0 };
444
dav_lock_discovery *dp = direct;
445
dav_lock_indirect *ip = indirect;
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 "
457
if ((err = dav_generic_really_open_lockdb(lockdb)) != NULL) {
458
/* ### add a higher-level error? */
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);
471
val.dsize += dav_size_direct(dp);
475
val.dsize += dav_size_indirect(ip);
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);
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) {
496
memcpy(ptr, dp->owner, strlen(dp->owner) + 1);
497
ptr += strlen(dp->owner) + 1;
499
if (dp->auth_user == NULL) {
503
memcpy(ptr, dp->auth_user, strlen(dp->auth_user) + 1);
504
ptr += strlen(dp->auth_user) + 1;
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;
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,
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.",
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,
556
dav_lock_discovery **direct,
557
dav_lock_indirect **indirect)
559
apr_pool_t *p = lockdb->info->pool;
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) {
573
if ((err = dav_generic_really_open_lockdb(lockdb)) != NULL) {
574
/* ### add a higher-level error? */
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) {
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);
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_palloc(p, sizeof(*dp->locktoken));
607
memcpy(dp->locktoken, val.dptr + offset, sizeof(*dp->locktoken));
608
offset += sizeof(*dp->locktoken);
610
/* Do we have an owner field? */
611
if (*(val.dptr + offset) == '\0') {
615
apr_size_t len = strlen(val.dptr + offset);
616
dp->owner = apr_pstrmemdup(p, val.dptr + offset, len);
620
if (*(val.dptr + offset) == '\0') {
624
apr_size_t len = strlen(val.dptr + offset);
625
dp->auth_user = apr_pstrmemdup(p, val.dptr + offset, len);
629
if (!dav_generic_lock_expired(dp->f.timeout)) {
634
need_save = DAV_TRUE;
638
case DAV_LOCK_INDIRECT:
639
/* Create and fill a dav_lock_indirect structure */
641
ip = apr_pcalloc(p, sizeof(*ip));
642
ip->locktoken = apr_palloc(p, sizeof(*ip->locktoken));
643
memcpy(ip->locktoken, val.dptr + offset, sizeof(*ip->locktoken));
644
offset += sizeof(*ip->locktoken);
645
memcpy(&ip->timeout, val.dptr + offset, sizeof(ip->timeout));
646
offset += sizeof(ip->timeout);
647
/* length of datum */
648
ip->key.dsize = *((int *) (val.dptr + offset));
649
offset += sizeof(ip->key.dsize);
650
ip->key.dptr = apr_palloc(p, ip->key.dsize);
651
memcpy(ip->key.dptr, val.dptr + offset, ip->key.dsize);
652
offset += ip->key.dsize;
654
if (!dav_generic_lock_expired(ip->timeout)) {
655
ip->next = *indirect;
659
need_save = DAV_TRUE;
665
apr_dbm_freedatum(lockdb->info->db, val);
667
/* ### should use a computed_desc and insert corrupt token data */
669
return dav_new_error(p,
670
HTTP_INTERNAL_SERVER_ERROR,
671
DAV_ERR_LOCK_CORRUPT_DB,
673
"The lock database was found to "
674
"be corrupt. offset %"
675
APR_SIZE_T_FMT ", c=%02x",
676
offset, val.dptr[offset]));
680
apr_dbm_freedatum(lockdb->info->db, val);
682
/* Clean up this record if we found expired locks */
684
* ### shouldn't do this if we've been opened READONLY. elide the
685
* ### timed-out locks from the response, but don't save that info back
687
if (need_save == DAV_TRUE) {
688
return dav_generic_save_lock_record(lockdb, key, *direct, *indirect);
694
/* resolve <indirect>, returning <*direct> */
695
static dav_error * dav_generic_resolve(dav_lockdb *lockdb,
696
dav_lock_indirect *indirect,
697
dav_lock_discovery **direct,
698
dav_lock_discovery **ref_dp,
699
dav_lock_indirect **ref_ip)
702
dav_lock_discovery *dir;
703
dav_lock_indirect *ind;
705
if ((err = dav_generic_load_lock_record(lockdb, indirect->key,
707
&dir, &ind)) != NULL) {
708
/* ### insert a higher-level description? */
711
if (ref_dp != NULL) {
716
for (; dir != NULL; dir = dir->next) {
717
if (!dav_compare_locktoken(indirect->locktoken, dir->locktoken)) {
723
/* No match found (but we should have found one!) */
725
/* ### use a different description and/or error ID? */
726
return dav_new_error(lockdb->info->pool,
727
HTTP_INTERNAL_SERVER_ERROR,
728
DAV_ERR_LOCK_CORRUPT_DB,
729
"The lock database was found to be corrupt. "
730
"An indirect lock's direct lock could not "
734
/* ---------------------------------------------------------------
736
* Property-related lock functions
741
* dav_generic_get_supportedlock: Returns a static string for all
742
* supportedlock properties. I think we save more returning a static string
743
* than constructing it every time, though it might look cleaner.
745
static const char *dav_generic_get_supportedlock(const dav_resource *resource)
747
static const char supported[] = DEBUG_CR
748
"<D:lockentry>" DEBUG_CR
749
"<D:lockscope><D:exclusive/></D:lockscope>" DEBUG_CR
750
"<D:locktype><D:write/></D:locktype>" DEBUG_CR
751
"</D:lockentry>" DEBUG_CR
752
"<D:lockentry>" DEBUG_CR
753
"<D:lockscope><D:shared/></D:lockscope>" DEBUG_CR
754
"<D:locktype><D:write/></D:locktype>" DEBUG_CR
755
"</D:lockentry>" DEBUG_CR;
760
/* ---------------------------------------------------------------
762
* General lock functions
766
static dav_error * dav_generic_remove_locknull_state(dav_lockdb *lockdb,
767
const dav_resource *resource)
769
/* We don't need to do anything. */
773
static dav_error * dav_generic_create_lock(dav_lockdb *lockdb,
774
const dav_resource *resource,
779
key = dav_generic_build_key(lockdb->info->pool, resource);
781
*lock = dav_generic_alloc_lock(lockdb, key, NULL);
783
(*lock)->is_locknull = !resource->exists;
788
static dav_error * dav_generic_get_locks(dav_lockdb *lockdb,
789
const dav_resource *resource,
793
apr_pool_t *p = lockdb->info->pool;
796
dav_lock *lock = NULL;
798
dav_lock_discovery *dp;
799
dav_lock_indirect *ip;
802
if (calltype == DAV_GETLOCKS_COMPLETE) {
803
return dav_new_error(lockdb->info->pool,
804
HTTP_INTERNAL_SERVER_ERROR, 0,
805
"INTERNAL DESIGN ERROR: DAV_GETLOCKS_COMPLETE "
806
"is not yet supported");
810
key = dav_generic_build_key(p, resource);
811
if ((err = dav_generic_load_lock_record(lockdb, key, DAV_CREATE_LIST,
812
&dp, &ip)) != NULL) {
813
/* ### push a higher-level desc? */
817
/* copy all direct locks to the result list */
818
for (; dp != NULL; dp = dp->next) {
819
newlock = dav_generic_alloc_lock(lockdb, key, dp->locktoken);
820
newlock->is_locknull = !resource->exists;
821
newlock->scope = dp->f.scope;
822
newlock->type = dp->f.type;
823
newlock->depth = dp->f.depth;
824
newlock->timeout = dp->f.timeout;
825
newlock->owner = dp->owner;
826
newlock->auth_user = dp->auth_user;
828
/* hook into the result list */
829
newlock->next = lock;
833
/* copy all the indirect locks to the result list. resolve as needed. */
834
for (; ip != NULL; ip = ip->next) {
835
newlock = dav_generic_alloc_lock(lockdb, ip->key, ip->locktoken);
836
newlock->is_locknull = !resource->exists;
838
if (calltype == DAV_GETLOCKS_RESOLVED) {
839
err = dav_generic_resolve(lockdb, ip, &dp, NULL, NULL);
841
/* ### push a higher-level desc? */
845
newlock->scope = dp->f.scope;
846
newlock->type = dp->f.type;
847
newlock->depth = dp->f.depth;
848
newlock->timeout = dp->f.timeout;
849
newlock->owner = dp->owner;
850
newlock->auth_user = dp->auth_user;
853
/* DAV_GETLOCKS_PARTIAL */
854
newlock->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
857
/* hook into the result list */
858
newlock->next = lock;
866
static dav_error * dav_generic_find_lock(dav_lockdb *lockdb,
867
const dav_resource *resource,
868
const dav_locktoken *locktoken,
874
dav_lock_discovery *dp;
875
dav_lock_indirect *ip;
879
key = dav_generic_build_key(lockdb->info->pool, resource);
880
if ((err = dav_generic_load_lock_record(lockdb, key, DAV_CREATE_LIST,
881
&dp, &ip)) != NULL) {
882
/* ### push a higher-level desc? */
886
for (; dp != NULL; dp = dp->next) {
887
if (!dav_compare_locktoken(locktoken, dp->locktoken)) {
888
*lock = dav_generic_alloc_lock(lockdb, key, locktoken);
889
(*lock)->is_locknull = !resource->exists;
890
(*lock)->scope = dp->f.scope;
891
(*lock)->type = dp->f.type;
892
(*lock)->depth = dp->f.depth;
893
(*lock)->timeout = dp->f.timeout;
894
(*lock)->owner = dp->owner;
895
(*lock)->auth_user = dp->auth_user;
900
for (; ip != NULL; ip = ip->next) {
901
if (!dav_compare_locktoken(locktoken, ip->locktoken)) {
902
*lock = dav_generic_alloc_lock(lockdb, ip->key, locktoken);
903
(*lock)->is_locknull = !resource->exists;
905
/* ### nobody uses the resolving right now! */
907
(*lock)->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
910
(*lock)->rectype = DAV_LOCKREC_INDIRECT;
911
if ((err = dav_generic_resolve(lockdb, ip, &dp,
912
NULL, NULL)) != NULL) {
913
/* ### push a higher-level desc? */
916
(*lock)->scope = dp->f.scope;
917
(*lock)->type = dp->f.type;
918
(*lock)->depth = dp->f.depth;
919
(*lock)->timeout = dp->f.timeout;
920
(*lock)->owner = dp->owner;
921
(*lock)->auth_user = dp->auth_user;
930
static dav_error * dav_generic_has_locks(dav_lockdb *lockdb,
931
const dav_resource *resource,
939
if ((err = dav_generic_really_open_lockdb(lockdb)) != NULL) {
940
/* ### insert a higher-level error description */
945
* If we opened readonly and the db wasn't there, then there are no
946
* locks for this resource. Just exit.
948
if (lockdb->info->db == NULL)
951
key = dav_generic_build_key(lockdb->info->pool, resource);
953
*locks_present = apr_dbm_exists(lockdb->info->db, key);
958
static dav_error * dav_generic_append_locks(dav_lockdb *lockdb,
959
const dav_resource *resource,
961
const dav_lock *lock)
963
apr_pool_t *p = lockdb->info->pool;
965
dav_lock_indirect *ip;
966
dav_lock_discovery *dp;
969
key = dav_generic_build_key(lockdb->info->pool, resource);
971
err = dav_generic_load_lock_record(lockdb, key, 0, &dp, &ip);
973
/* ### maybe add in a higher-level description */
978
* ### when we store the lock more directly, we need to update
979
* ### lock->rectype and lock->is_locknull
983
for (; lock != NULL; lock = lock->next) {
985
/* ### this works for any <lock> rectype */
986
dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
988
/* ### shut off the const warning for now */
989
newi->locktoken = (dav_locktoken *)lock->locktoken;
990
newi->timeout = lock->timeout;
991
newi->key = lock->info->key;
997
for (; lock != NULL; lock = lock->next) {
998
/* create and link in the right kind of lock */
1000
if (lock->rectype == DAV_LOCKREC_DIRECT) {
1001
dav_lock_discovery *newd = apr_pcalloc(p, sizeof(*newd));
1003
newd->f.scope = lock->scope;
1004
newd->f.type = lock->type;
1005
newd->f.depth = lock->depth;
1006
newd->f.timeout = lock->timeout;
1007
/* ### shut off the const warning for now */
1008
newd->locktoken = (dav_locktoken *)lock->locktoken;
1009
newd->owner = lock->owner;
1010
newd->auth_user = lock->auth_user;
1015
/* DAV_LOCKREC_INDIRECT(_PARTIAL) */
1017
dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
1019
/* ### shut off the const warning for now */
1020
newi->locktoken = (dav_locktoken *)lock->locktoken;
1021
newi->key = lock->info->key;
1028
if ((err = dav_generic_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1029
/* ### maybe add a higher-level description */
1036
static dav_error * dav_generic_remove_lock(dav_lockdb *lockdb,
1037
const dav_resource *resource,
1038
const dav_locktoken *locktoken)
1041
dav_lock_discovery *dh = NULL;
1042
dav_lock_indirect *ih = NULL;
1045
key = dav_generic_build_key(lockdb->info->pool, resource);
1047
if (locktoken != NULL) {
1048
dav_lock_discovery *dp;
1049
dav_lock_discovery *dprev = NULL;
1050
dav_lock_indirect *ip;
1051
dav_lock_indirect *iprev = NULL;
1053
if ((err = dav_generic_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1054
&dh, &ih)) != NULL) {
1055
/* ### maybe add a higher-level description */
1059
for (dp = dh; dp != NULL; dp = dp->next) {
1060
if (dav_compare_locktoken(locktoken, dp->locktoken) == 0) {
1062
dprev->next = dp->next;
1069
for (ip = ih; ip != NULL; ip = ip->next) {
1070
if (dav_compare_locktoken(locktoken, ip->locktoken) == 0) {
1072
iprev->next = ip->next;
1081
/* save the modified locks, or remove all locks (dh=ih=NULL). */
1082
if ((err = dav_generic_save_lock_record(lockdb, key, dh, ih)) != NULL) {
1083
/* ### maybe add a higher-level description */
1090
static int dav_generic_do_refresh(dav_lock_discovery *dp,
1091
const dav_locktoken_list *ltl,
1096
for (; ltl != NULL; ltl = ltl->next) {
1097
if (dav_compare_locktoken(dp->locktoken, ltl->locktoken) == 0)
1099
dp->f.timeout = new_time;
1107
static dav_error * dav_generic_refresh_locks(dav_lockdb *lockdb,
1108
const dav_resource *resource,
1109
const dav_locktoken_list *ltl,
1115
dav_lock_discovery *dp;
1116
dav_lock_discovery *dp_scan;
1117
dav_lock_indirect *ip;
1123
key = dav_generic_build_key(lockdb->info->pool, resource);
1124
if ((err = dav_generic_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1125
&dp, &ip)) != NULL) {
1126
/* ### maybe add in a higher-level description */
1130
/* ### we should be refreshing direct AND (resolved) indirect locks! */
1132
/* refresh all of the direct locks on this resource */
1133
for (dp_scan = dp; dp_scan != NULL; dp_scan = dp_scan->next) {
1134
if (dav_generic_do_refresh(dp_scan, ltl, new_time)) {
1135
/* the lock was refreshed. return the lock. */
1136
newlock = dav_generic_alloc_lock(lockdb, key, dp_scan->locktoken);
1137
newlock->is_locknull = !resource->exists;
1138
newlock->scope = dp_scan->f.scope;
1139
newlock->type = dp_scan->f.type;
1140
newlock->depth = dp_scan->f.depth;
1141
newlock->timeout = dp_scan->f.timeout;
1142
newlock->owner = dp_scan->owner;
1143
newlock->auth_user = dp_scan->auth_user;
1145
newlock->next = *locks;
1152
/* if we refreshed any locks, then save them back. */
1154
&& (err = dav_generic_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1155
/* ### maybe add in a higher-level description */
1159
/* for each indirect lock, find its direct lock and refresh it. */
1160
for (; ip != NULL; ip = ip->next) {
1161
dav_lock_discovery *ref_dp;
1162
dav_lock_indirect *ref_ip;
1164
if ((err = dav_generic_resolve(lockdb, ip, &dp_scan,
1165
&ref_dp, &ref_ip)) != NULL) {
1166
/* ### push a higher-level desc? */
1169
if (dav_generic_do_refresh(dp_scan, ltl, new_time)) {
1170
/* the lock was refreshed. return the lock. */
1171
newlock = dav_generic_alloc_lock(lockdb, ip->key, dp->locktoken);
1172
newlock->is_locknull = !resource->exists;
1173
newlock->scope = dp->f.scope;
1174
newlock->type = dp->f.type;
1175
newlock->depth = dp->f.depth;
1176
newlock->timeout = dp->f.timeout;
1177
newlock->owner = dp->owner;
1178
newlock->auth_user = dp_scan->auth_user;
1180
newlock->next = *locks;
1183
/* save the (resolved) direct lock back */
1184
if ((err = dav_generic_save_lock_record(lockdb, ip->key, ref_dp,
1186
/* ### push a higher-level desc? */
1196
const dav_hooks_locks dav_hooks_locks_generic =
1198
dav_generic_get_supportedlock,
1199
dav_generic_parse_locktoken,
1200
dav_generic_format_locktoken,
1201
dav_generic_compare_locktoken,
1202
dav_generic_open_lockdb,
1203
dav_generic_close_lockdb,
1204
dav_generic_remove_locknull_state,
1205
dav_generic_create_lock,
1206
dav_generic_get_locks,
1207
dav_generic_find_lock,
1208
dav_generic_has_locks,
1209
dav_generic_append_locks,
1210
dav_generic_remove_lock,
1211
dav_generic_refresh_locks,
1212
NULL, /* lookup_resource */