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_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') {
614 apr_size_t len
= strlen(val
.dptr
+ offset
);
615 dp
->owner
= apr_pstrmemdup(p
, val
.dptr
+ offset
, len
);
619 if (*(val
.dptr
+ offset
) == '\0') {
623 apr_size_t len
= strlen(val
.dptr
+ offset
);
624 dp
->auth_user
= apr_pstrmemdup(p
, val
.dptr
+ offset
, len
);
628 if (!dav_generic_lock_expired(dp
->f
.timeout
)) {
633 need_save
= DAV_TRUE
;
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
;
656 need_save
= DAV_TRUE
;
662 apr_dbm_freedatum(lockdb
->info
->db
, val
);
664 /* ### should use a computed_desc and insert corrupt token data */
666 return dav_new_error(p
,
667 HTTP_INTERNAL_SERVER_ERROR
,
668 DAV_ERR_LOCK_CORRUPT_DB
,
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
);
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
)
699 dav_lock_discovery
*dir
;
700 dav_lock_indirect
*ind
;
702 if ((err
= dav_generic_load_lock_record(lockdb
, indirect
->key
,
704 &dir
, &ind
)) != NULL
) {
705 /* ### insert a higher-level description? */
708 if (ref_dp
!= NULL
) {
713 for (; dir
!= NULL
; dir
= dir
->next
) {
714 if (!dav_compare_locktoken(indirect
->locktoken
, dir
->locktoken
)) {
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 "
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
;
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. */
770 static dav_error
* dav_generic_create_lock(dav_lockdb
*lockdb
,
771 const dav_resource
*resource
,
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
;
785 static dav_error
* dav_generic_get_locks(dav_lockdb
*lockdb
,
786 const dav_resource
*resource
,
790 apr_pool_t
*p
= lockdb
->info
->pool
;
793 dav_lock
*lock
= NULL
;
795 dav_lock_discovery
*dp
;
796 dav_lock_indirect
*ip
;
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");
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? */
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
;
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
);
838 /* ### push a higher-level desc? */
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
;
850 /* DAV_GETLOCKS_PARTIAL */
851 newlock
->rectype
= DAV_LOCKREC_INDIRECT_PARTIAL
;
854 /* hook into the result list */
855 newlock
->next
= lock
;
863 static dav_error
* dav_generic_find_lock(dav_lockdb
*lockdb
,
864 const dav_resource
*resource
,
865 const dav_locktoken
*locktoken
,
871 dav_lock_discovery
*dp
;
872 dav_lock_indirect
*ip
;
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? */
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
;
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! */
904 (*lock
)->rectype
= DAV_LOCKREC_INDIRECT_PARTIAL
;
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? */
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
;
927 static dav_error
* dav_generic_has_locks(dav_lockdb
*lockdb
,
928 const dav_resource
*resource
,
936 if ((err
= dav_generic_really_open_lockdb(lockdb
)) != NULL
) {
937 /* ### insert a higher-level error description */
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
)
948 key
= dav_generic_build_key(lockdb
->info
->pool
, resource
);
950 *locks_present
= apr_dbm_exists(lockdb
->info
->db
, key
);
955 static dav_error
* dav_generic_append_locks(dav_lockdb
*lockdb
,
956 const dav_resource
*resource
,
958 const dav_lock
*lock
)
960 apr_pool_t
*p
= lockdb
->info
->pool
;
962 dav_lock_indirect
*ip
;
963 dav_lock_discovery
*dp
;
966 key
= dav_generic_build_key(lockdb
->info
->pool
, resource
);
968 err
= dav_generic_load_lock_record(lockdb
, key
, 0, &dp
, &ip
);
970 /* ### maybe add in a higher-level description */
975 * ### when we store the lock more directly, we need to update
976 * ### lock->rectype and lock->is_locknull
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
;
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
;
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
;
1025 if ((err
= dav_generic_save_lock_record(lockdb
, key
, dp
, ip
)) != NULL
) {
1026 /* ### maybe add a higher-level description */
1033 static dav_error
* dav_generic_remove_lock(dav_lockdb
*lockdb
,
1034 const dav_resource
*resource
,
1035 const dav_locktoken
*locktoken
)
1038 dav_lock_discovery
*dh
= NULL
;
1039 dav_lock_indirect
*ih
= NULL
;
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 */
1056 for (dp
= dh
; dp
!= NULL
; dp
= dp
->next
) {
1057 if (dav_compare_locktoken(locktoken
, dp
->locktoken
) == 0) {
1059 dprev
->next
= dp
->next
;
1066 for (ip
= ih
; ip
!= NULL
; ip
= ip
->next
) {
1067 if (dav_compare_locktoken(locktoken
, ip
->locktoken
) == 0) {
1069 iprev
->next
= ip
->next
;
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 */
1087 static int dav_generic_do_refresh(dav_lock_discovery
*dp
,
1088 const dav_locktoken_list
*ltl
,
1093 for (; ltl
!= NULL
; ltl
= ltl
->next
) {
1094 if (dav_compare_locktoken(dp
->locktoken
, ltl
->locktoken
) == 0)
1096 dp
->f
.timeout
= new_time
;
1104 static dav_error
* dav_generic_refresh_locks(dav_lockdb
*lockdb
,
1105 const dav_resource
*resource
,
1106 const dav_locktoken_list
*ltl
,
1112 dav_lock_discovery
*dp
;
1113 dav_lock_discovery
*dp_scan
;
1114 dav_lock_indirect
*ip
;
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 */
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
;
1149 /* if we refreshed any locks, then save them back. */
1151 && (err
= dav_generic_save_lock_record(lockdb
, key
, dp
, ip
)) != NULL
) {
1152 /* ### maybe add in a higher-level description */
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? */
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
;
1180 /* save the (resolved) direct lock back */
1181 if ((err
= dav_generic_save_lock_record(lockdb
, ip
->key
, ref_dp
,
1183 /* ### push a higher-level desc? */
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 */