]> granicus.if.org Git - apache/blob - modules/dav/fs/lock.c
Updated patch to fix compile warnings from not including apr_strings.h
[apache] / modules / dav / fs / lock.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  */
54
55 /*
56 ** DAV filesystem lock implementation
57 */
58
59 #include "httpd.h"
60 #include "http_log.h"
61 #include "apr_file_io.h"
62 #include "apr_strings.h"
63
64 #include "mod_dav.h"
65 #include "dav_opaquelock.h"
66 #include "repos.h"
67
68
69 /* ---------------------------------------------------------------
70 **
71 ** Lock database primitives
72 **
73 */
74
75 /*
76 ** LOCK DATABASES
77 ** 
78 ** Lockdiscovery information is stored in the single lock database specified
79 ** by the DAVLockDB directive.  Information about this db is stored in the
80 ** global server configuration.
81 **
82 ** KEY
83 **
84 ** The database is keyed by a key_type unsigned char (DAV_TYPE_INODE or
85 ** DAV_TYPE_FNAME) followed by inode and device number if possible,
86 ** otherwise full path (in the case of Win32 or lock-null resources).
87 **
88 ** VALUE
89 **
90 ** The value consists of a list of elements.
91 **    DIRECT LOCK:     [char  (DAV_LOCK_DIRECT),
92 **                      char  (dav_lock_scope),
93 **                      char  (dav_lock_type),
94 **                      int    depth,
95 **                      time_t expires,
96 **                      uuid_t locktoken,
97 **                      char[] owner,
98 **                      char[] auth_user]
99 **
100 **    INDIRECT LOCK:   [char  (DAV_LOCK_INDIRECT),
101 **                      uuid_t locktoken,
102 **                      time_t expires,
103 **                      int    key_size,
104 **                      char[] key]
105 **       The key is to the collection lock that resulted in this indirect lock
106 */
107
108 #define DAV_TRUE                1
109 #define DAV_FALSE               0
110
111 #define DAV_CREATE_LIST         23
112 #define DAV_APPEND_LIST         24
113
114 /* Stored lock_discovery prefix */
115 #define DAV_LOCK_DIRECT         1
116 #define DAV_LOCK_INDIRECT       2
117
118 #define DAV_TYPE_INODE          10
119 #define DAV_TYPE_FNAME          11
120
121
122 /* ack. forward declare. */
123 static dav_error * dav_fs_remove_locknull_member(apr_pool_t *p,
124                                                  const char *filename,
125                                                  dav_buffer *pbuf);
126
127 /*
128 ** Use the opaquelock scheme for locktokens
129 */
130 struct dav_locktoken {
131     uuid_t uuid;
132 };
133
134
135 /* #################################################################
136 ** ### keep these structures (internal) or move fully to dav_lock?
137 */
138
139 /*
140 ** We need to reliably size the fixed-length portion of
141 ** dav_lock_discovery; best to separate it into another 
142 ** struct for a convenient sizeof, unless we pack lock_discovery.
143 */
144 typedef struct dav_lock_discovery_fixed
145 {
146     char scope;
147     char type;
148     int depth;
149     time_t timeout;
150 } dav_lock_discovery_fixed;
151
152 typedef struct dav_lock_discovery
153 {
154     struct dav_lock_discovery_fixed f;
155
156     dav_locktoken *locktoken;
157     const char *owner;          /* owner field from activelock */
158     const char *auth_user;      /* authenticated user who created the lock */
159     struct dav_lock_discovery *next;
160 } dav_lock_discovery;
161
162 /* Indirect locks represent locks inherited from containing collections.
163  * They reference the lock token for the collection the lock is
164  * inherited from. A lock provider may also define a key to the
165  * inherited lock, for fast datbase lookup. The key is opaque outside
166  * the lock provider.
167  */
168 typedef struct dav_lock_indirect
169 {
170     dav_locktoken *locktoken;
171     dav_datum key;
172     struct dav_lock_indirect *next;
173     time_t timeout;
174 } dav_lock_indirect;
175
176 /* ################################################################# */
177
178
179 /*
180 ** Stored direct lock info - full lock_discovery length:  
181 ** prefix + Fixed length + lock token + 2 strings + 2 nulls (one for each string)
182 */
183 #define dav_size_direct(a)      (1 + sizeof(dav_lock_discovery_fixed) \
184                                  + sizeof(uuid_t) \
185                                  + ((a)->owner ? strlen((a)->owner) : 0) \
186                                  + ((a)->auth_user ? strlen((a)->auth_user) : 0) \
187                                  + 2)
188
189 /* Stored indirect lock info - lock token and dav_datum */
190 #define dav_size_indirect(a)    (1 + sizeof(uuid_t) \
191                                  + sizeof(time_t) \
192                                  + sizeof(int) + (a)->key.dsize)
193
194 /*
195 ** The lockdb structure.
196 **
197 ** The <db> field may be NULL, meaning one of two things:
198 ** 1) That we have not actually opened the underlying database (yet). The
199 **    <opened> field should be false.
200 ** 2) We opened it readonly and it wasn't present.
201 **
202 ** The delayed opening (determined by <opened>) makes creating a lockdb
203 ** quick, while deferring the underlying I/O until it is actually required.
204 **
205 ** We export the notion of a lockdb, but hide the details of it. Most
206 ** implementations will use a database of some kind, but it is certainly
207 ** possible that alternatives could be used.
208 */
209 struct dav_lockdb_private
210 {
211     request_rec *r;                     /* for accessing the uuid state */
212     apr_pool_t *pool;                   /* a pool to use */
213     const char *lockdb_path;            /* where is the lock database? */
214
215     int opened;                         /* we opened the database */
216     dav_db *db;                         /* if non-NULL, the lock database */
217 };
218 typedef struct
219 {
220     dav_lockdb pub;
221     dav_lockdb_private priv;
222 } dav_lockdb_combined;
223
224 /*
225 ** The private part of the lock structure.
226 */
227 struct dav_lock_private
228 {
229     dav_datum key;      /* key into the lock database */
230 };
231 typedef struct
232 {
233     dav_lock pub;
234     dav_lock_private priv;
235     dav_locktoken token;
236 } dav_lock_combined;
237
238 /*
239 ** This must be forward-declared so the open_lockdb function can use it.
240 */
241 extern const dav_hooks_locks dav_hooks_locks_fs;
242
243
244 /* internal function for creating locks */
245 static dav_lock *dav_fs_alloc_lock(dav_lockdb *lockdb, dav_datum key,
246                                    const dav_locktoken *locktoken)
247 {
248     dav_lock_combined *comb;
249
250     comb = apr_pcalloc(lockdb->info->pool, sizeof(*comb));
251     comb->pub.rectype = DAV_LOCKREC_DIRECT;
252     comb->pub.info = &comb->priv;
253     comb->priv.key = key;
254
255     if (locktoken == NULL) {
256         comb->pub.locktoken = &comb->token;
257         dav_create_opaquelocktoken(dav_get_uuid_state(lockdb->info->r),
258                                    &comb->token.uuid);
259     }
260     else {
261         comb->pub.locktoken = locktoken;
262     }
263
264     return &comb->pub;
265 }
266
267 /*
268 ** dav_fs_parse_locktoken
269 **
270 ** Parse an opaquelocktoken URI into a locktoken.
271 */
272 static dav_error * dav_fs_parse_locktoken(
273     apr_pool_t *p,
274     const char *char_token,
275     dav_locktoken **locktoken_p)
276 {
277     dav_locktoken *locktoken;
278
279     if (ap_strstr_c(char_token, "opaquelocktoken:") != char_token) {
280         return dav_new_error(p,
281                              HTTP_BAD_REQUEST, DAV_ERR_LOCK_UNK_STATE_TOKEN,
282                              "The lock token uses an unknown State-token "
283                              "format and could not be parsed.");
284     }
285     char_token += 16;
286
287     locktoken = apr_pcalloc(p, sizeof(*locktoken));
288     if (dav_parse_opaquelocktoken(char_token, &locktoken->uuid)) {
289         return dav_new_error(p, HTTP_BAD_REQUEST, DAV_ERR_LOCK_PARSE_TOKEN,
290                              "The opaquelocktoken has an incorrect format "
291                              "and could not be parsed.");
292     }
293     
294     *locktoken_p = locktoken;
295     return NULL;
296 }
297
298 /*
299 ** dav_fs_format_locktoken
300 **
301 ** Generate the URI for a locktoken
302 */
303 static const char *dav_fs_format_locktoken(
304     apr_pool_t *p,
305     const dav_locktoken *locktoken)
306 {
307     const char *uuid_token = dav_format_opaquelocktoken(p, &locktoken->uuid);
308     return apr_pstrcat(p, "opaquelocktoken:", uuid_token, NULL);
309 }
310
311 /*
312 ** dav_fs_compare_locktoken
313 **
314 ** Determine whether two locktokens are the same
315 */
316 static int dav_fs_compare_locktoken(
317     const dav_locktoken *lt1,
318     const dav_locktoken *lt2)
319 {
320     return dav_compare_opaquelocktoken(lt1->uuid, lt2->uuid);
321 }
322
323 /*
324 ** dav_fs_really_open_lockdb:
325 **
326 ** If the database hasn't been opened yet, then open the thing.
327 */
328 static dav_error * dav_fs_really_open_lockdb(dav_lockdb *lockdb)
329 {
330     dav_error *err;
331
332     if (lockdb->info->opened)
333         return NULL;
334
335     err = dav_dbm_open_direct(lockdb->info->pool,
336                               lockdb->info->lockdb_path,
337                               lockdb->ro,
338                               &lockdb->info->db);
339     if (err != NULL) {
340         return dav_push_error(lockdb->info->pool,
341                               HTTP_INTERNAL_SERVER_ERROR,
342                               DAV_ERR_LOCK_OPENDB,
343                               "Could not open the lock database.",
344                               err);
345     }
346
347     /* all right. it is opened now. */
348     lockdb->info->opened = 1;
349
350     return NULL;
351 }
352
353 /*
354 ** dav_fs_open_lockdb:
355 **
356 ** "open" the lock database, as specified in the global server configuration.
357 ** If force is TRUE, then the database is opened now, rather than lazily.
358 **
359 ** Note that only one can be open read/write.
360 */
361 static dav_error * dav_fs_open_lockdb(request_rec *r, int ro, int force,
362                                       dav_lockdb **lockdb)
363 {
364     dav_lockdb_combined *comb;
365
366     comb = apr_pcalloc(r->pool, sizeof(*comb));
367     comb->pub.hooks = &dav_hooks_locks_fs;
368     comb->pub.ro = ro;
369     comb->pub.info = &comb->priv;
370     comb->priv.r = r;
371     comb->priv.pool = r->pool;
372
373     comb->priv.lockdb_path = dav_get_lockdb_path(r);
374     if (comb->priv.lockdb_path == NULL) {
375         return dav_new_error(r->pool, HTTP_INTERNAL_SERVER_ERROR,
376                              DAV_ERR_LOCK_NO_DB,
377                              "A lock database was not specified with the "
378                              "DAVLockDB directive. One must be specified "
379                              "to use the locking functionality.");
380     }
381
382     /* done initializing. return it. */
383     *lockdb = &comb->pub;
384
385     if (force) {
386         /* ### add a higher-level comment? */
387         return dav_fs_really_open_lockdb(*lockdb);
388     }
389
390     return NULL;
391 }
392
393 /*
394 ** dav_fs_close_lockdb:
395 **
396 ** Close it. Duh.
397 */
398 static void dav_fs_close_lockdb(dav_lockdb *lockdb)
399 {
400     if (lockdb->info->db != NULL)
401         (*dav_hooks_db_dbm.close)(lockdb->info->db);
402 }
403
404 /*
405 ** dav_fs_build_fname_key
406 **
407 ** Given a pathname, build a DAV_TYPE_FNAME lock database key.
408 */
409 static dav_datum dav_fs_build_fname_key(apr_pool_t *p, const char *pathname)
410 {
411     dav_datum key;
412
413     /* ### does this allocation have a proper lifetime? need to check */
414     /* ### can we use a buffer for this? */
415
416     /* size is TYPE + pathname + null */
417     key.dsize = strlen(pathname) + 2;
418     key.dptr = apr_palloc(p, key.dsize);
419     *key.dptr = DAV_TYPE_FNAME;
420     memcpy(key.dptr + 1, pathname, key.dsize - 1);
421     if (key.dptr[key.dsize - 2] == '/')
422         key.dptr[--key.dsize - 1] = '\0';
423     return key;
424 }
425
426 /*
427 ** dav_fs_build_key:  Given a resource, return a dav_datum key
428 **    to look up lock information for this file.
429 **
430 **    (Win32 or file is lock-null):
431 **       dav_datum->dvalue = full path
432 **
433 **    (non-Win32 and file exists ):
434 **       dav_datum->dvalue = inode, dev_major, dev_minor
435 */
436 static dav_datum dav_fs_build_key(apr_pool_t *p, const dav_resource *resource)
437 {
438     const char *file = dav_fs_pathname(resource);
439 #ifndef WIN32
440     dav_datum key;
441     apr_finfo_t finfo;
442
443     /* ### use lstat() ?? */
444     if (apr_stat(&finfo, file, p) == 0) {
445
446         /* ### can we use a buffer for this? */
447         key.dsize = 1 + sizeof(finfo.inode) + sizeof(finfo.device);
448         key.dptr = apr_palloc(p, key.dsize);
449         *key.dptr = DAV_TYPE_INODE;
450         memcpy(key.dptr + 1, &finfo.inode, sizeof(finfo.inode));
451         memcpy(key.dptr + 1 + sizeof(finfo.inode), &finfo.device,
452                sizeof(finfo.device));
453
454         return key;
455     }
456 #endif
457
458     return dav_fs_build_fname_key(p, file);
459 }
460
461 /*
462 ** dav_fs_lock_expired:  return 1 (true) if the given timeout is in the past
463 **    or present (the lock has expired), or 0 (false) if in the future
464 **    (the lock has not yet expired).
465 */
466 static int dav_fs_lock_expired(time_t expires)
467 {
468     return expires != DAV_TIMEOUT_INFINITE && time(NULL) >= expires;
469 }
470
471 /*
472 ** dav_fs_save_lock_record:  Saves the lock information specified in the
473 **    direct and indirect lock lists about path into the lock database.
474 **    If direct and indirect == NULL, the key is removed.
475 */
476 static dav_error * dav_fs_save_lock_record(dav_lockdb *lockdb, dav_datum key,
477                                            dav_lock_discovery *direct,
478                                            dav_lock_indirect *indirect)
479 {
480     dav_error *err;
481     dav_datum val = { 0 };
482     char *ptr;
483     dav_lock_discovery *dp = direct;
484     dav_lock_indirect *ip = indirect;
485
486 #if DAV_DEBUG
487     if (lockdb->ro) {
488         return dav_new_error(lockdb->info->pool,
489                              HTTP_INTERNAL_SERVER_ERROR, 0,
490                              "INTERNAL DESIGN ERROR: the lockdb was opened "
491                              "readonly, but an attempt to save locks was "
492                              "performed.");
493     }
494 #endif
495
496     if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
497         /* ### add a higher-level error? */
498         return err;
499     }
500
501     /* If nothing to save, delete key */
502     if (dp == NULL && ip == NULL) {
503         /* don't fail if the key is not present */
504         /* ### but what about other errors? */
505         (void) (*dav_hooks_db_dbm.remove)(lockdb->info->db, key);
506         return NULL;
507     }
508                 
509     while(dp) {
510         val.dsize += dav_size_direct(dp);
511         dp = dp->next;
512     }
513     while(ip) {
514         val.dsize += dav_size_indirect(ip);
515         ip = ip->next;
516     }
517
518     /* ### can this be apr_palloc() ? */
519     /* ### hmmm.... investigate the use of a buffer here */
520     ptr = val.dptr = apr_pcalloc(lockdb->info->pool, val.dsize);
521     dp  = direct;
522     ip  = indirect;
523
524     while(dp) {
525         *ptr++ = DAV_LOCK_DIRECT;       /* Direct lock - lock_discovery struct follows */
526         memcpy(ptr, dp, sizeof(dp->f)); /* Fixed portion of struct */
527         ptr += sizeof(dp->f);
528         memcpy(ptr, dp->locktoken, sizeof(*dp->locktoken));
529         ptr += sizeof(*dp->locktoken);
530         if (dp->owner == NULL) {
531             *ptr++ = '\0';
532         }
533         else {
534             memcpy(ptr, dp->owner, strlen(dp->owner) + 1);      
535             ptr += strlen(dp->owner) + 1;
536         }
537         if (dp->auth_user == NULL) {
538             *ptr++ = '\0';
539         }
540         else {
541             memcpy(ptr, dp->auth_user, strlen(dp->auth_user) + 1);
542             ptr += strlen(dp->auth_user) + 1;
543         }
544
545         dp = dp->next;
546     }
547
548     while(ip) {
549         *ptr++ = DAV_LOCK_INDIRECT;     /* Indirect lock prefix */
550         memcpy(ptr, ip->locktoken, sizeof(*ip->locktoken));     /* Locktoken */
551         ptr += sizeof(*ip->locktoken);
552         memcpy(ptr, &ip->timeout, sizeof(ip->timeout));         /* Expire time */
553         ptr += sizeof(ip->timeout);
554         memcpy(ptr, &ip->key.dsize, sizeof(ip->key.dsize));     /* Size of key */
555         ptr += sizeof(ip->key.dsize);
556         memcpy(ptr, ip->key.dptr, ip->key.dsize);       /* Key data */
557         ptr += ip->key.dsize;
558         ip = ip->next;
559     }
560
561     if ((err = (*dav_hooks_db_dbm.store)(lockdb->info->db,
562                                                 key, val)) != NULL) {
563         /* ### more details? add an error_id? */
564         return dav_push_error(lockdb->info->pool,
565                               HTTP_INTERNAL_SERVER_ERROR,
566                               DAV_ERR_LOCK_SAVE_LOCK,
567                               "Could not save lock information.",
568                               err);
569     }
570
571     return NULL;
572 }
573
574 /*
575 ** dav_load_lock_record:  Reads lock information about key from lock db;
576 **    creates linked lists of the direct and indirect locks.
577 **
578 **    If add_method = DAV_APPEND_LIST, the result will be appended to the
579 **    head of the direct and indirect lists supplied.
580 **
581 **    Passive lock removal:  If lock has timed out, it will not be returned.
582 **    ### How much "logging" does RFC 2518 require?
583 */
584 static dav_error * dav_fs_load_lock_record(dav_lockdb *lockdb, dav_datum key,
585                                            int add_method,
586                                            dav_lock_discovery **direct,
587                                            dav_lock_indirect **indirect)
588 {
589     apr_pool_t *p = lockdb->info->pool;
590     dav_error *err;
591     size_t offset = 0;
592     int need_save = DAV_FALSE;
593     dav_datum val = { 0 };
594     dav_lock_discovery *dp;
595     dav_lock_indirect *ip;
596     dav_buffer buf = { 0 };
597
598     if (add_method != DAV_APPEND_LIST) {
599         *direct = NULL;
600         *indirect = NULL;
601     }
602
603     if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
604         /* ### add a higher-level error? */
605         return err;
606     }
607
608     /*
609     ** If we opened readonly and the db wasn't there, then there are no
610     ** locks for this resource. Just exit.
611     */
612     if (lockdb->info->db == NULL)
613         return NULL;
614
615     if ((err = (*dav_hooks_db_dbm.fetch)(lockdb->info->db, key, &val)) != NULL)
616         return err;
617         
618     if (!val.dsize)
619         return NULL;
620
621     while (offset < val.dsize) {
622         switch (*(val.dptr + offset++)) {
623         case DAV_LOCK_DIRECT:
624             /* Create and fill a dav_lock_discovery structure */
625
626             dp = apr_pcalloc(p, sizeof(*dp));
627             memcpy(dp, val.dptr + offset, sizeof(dp->f));
628             offset += sizeof(dp->f);
629             dp->locktoken = apr_palloc(p, sizeof(*dp->locktoken));
630             memcpy(dp->locktoken, val.dptr + offset, sizeof(*dp->locktoken));
631             offset += sizeof(*dp->locktoken);
632             if (*(val.dptr + offset) == '\0') {
633                 ++offset;
634             }
635             else {
636                 dp->owner = apr_pstrdup(p, val.dptr + offset);
637                 offset += strlen(dp->owner) + 1;
638             }
639
640             if (*(val.dptr + offset) == '\0') {
641                 ++offset;
642             } 
643             else {
644                 dp->auth_user = apr_pstrdup(p, val.dptr + offset);
645                 offset += strlen(dp->auth_user) + 1;
646             }
647
648             if (!dav_fs_lock_expired(dp->f.timeout)) {
649                 dp->next = *direct;
650                 *direct = dp;
651             }
652             else {
653                 need_save = DAV_TRUE;
654
655                 /* Remove timed-out locknull fm .locknull list */
656                 if (*key.dptr == DAV_TYPE_FNAME) {
657                     const char *fname = key.dptr + 1;
658                     apr_finfo_t finfo;
659
660                     /* if we don't see the file, then it's a locknull */
661                     if (apr_lstat(&finfo, fname, p) != 0) {
662                         if ((err = dav_fs_remove_locknull_member(p, fname, &buf)) != NULL) {
663                             /* ### push a higher-level description? */
664                             return err;
665                         }
666                     }
667                 }
668             }
669             break;
670
671         case DAV_LOCK_INDIRECT:
672             /* Create and fill a dav_lock_indirect structure */
673
674             ip = apr_pcalloc(p, sizeof(*ip));
675             ip->locktoken = apr_palloc(p, sizeof(*ip->locktoken));
676             memcpy(ip->locktoken, val.dptr + offset, sizeof(*ip->locktoken));
677             offset += sizeof(*ip->locktoken);
678             memcpy(&ip->timeout, val.dptr + offset, sizeof(ip->timeout));
679             offset += sizeof(ip->timeout);
680             ip->key.dsize = *((int *) (val.dptr + offset));     /* length of datum */
681             offset += sizeof(ip->key.dsize);
682             ip->key.dptr = apr_palloc(p, ip->key.dsize); 
683             memcpy(ip->key.dptr, val.dptr + offset, ip->key.dsize);
684             offset += ip->key.dsize;
685
686             if (!dav_fs_lock_expired(ip->timeout)) {
687                 ip->next = *indirect;
688                 *indirect = ip;
689             }
690             else {
691                 need_save = DAV_TRUE;
692                 /* A locknull resource will never be locked indirectly */
693             }
694
695             break;
696
697         default:
698             (*dav_hooks_db_dbm.freedatum)(lockdb->info->db, val);
699
700             /* ### should use a computed_desc and insert corrupt token data */
701             --offset;
702             return dav_new_error(p,
703                                  HTTP_INTERNAL_SERVER_ERROR,
704                                  DAV_ERR_LOCK_CORRUPT_DB,
705                                  apr_psprintf(p,
706                                              "The lock database was found to "
707                                              "be corrupt. offset %i, c=%02x",
708                                              offset, val.dptr[offset]));
709         }
710     }
711
712     (*dav_hooks_db_dbm.freedatum)(lockdb->info->db, val);
713
714     /* Clean up this record if we found expired locks */
715     /*
716     ** ### shouldn't do this if we've been opened READONLY. elide the
717     ** ### timed-out locks from the response, but don't save that info back
718     */
719     if (need_save == DAV_TRUE) {
720         return dav_fs_save_lock_record(lockdb, key, *direct, *indirect);
721     }
722
723     return NULL;
724 }
725
726 /* resolve <indirect>, returning <*direct> */
727 static dav_error * dav_fs_resolve(dav_lockdb *lockdb,
728                                   dav_lock_indirect *indirect,
729                                   dav_lock_discovery **direct,
730                                   dav_lock_discovery **ref_dp,
731                                   dav_lock_indirect **ref_ip)
732 {
733     dav_error *err;
734     dav_lock_discovery *dir;
735     dav_lock_indirect *ind;
736         
737     if ((err = dav_fs_load_lock_record(lockdb, indirect->key,
738                                        DAV_CREATE_LIST,
739                                        &dir, &ind)) != NULL) {
740         /* ### insert a higher-level description? */
741         return err;
742     }
743     if (ref_dp != NULL) {
744         *ref_dp = dir;
745         *ref_ip = ind;
746     }
747                 
748     for (; dir != NULL; dir = dir->next) {
749         if (!dav_compare_opaquelocktoken(indirect->locktoken->uuid, 
750                                          dir->locktoken->uuid)) {
751             *direct = dir;
752             return NULL;
753         }
754     }
755
756     /* No match found (but we should have found one!) */
757
758     /* ### use a different description and/or error ID? */
759     return dav_new_error(lockdb->info->pool,
760                          HTTP_INTERNAL_SERVER_ERROR,
761                          DAV_ERR_LOCK_CORRUPT_DB,
762                          "The lock database was found to be corrupt. "
763                          "An indirect lock's direct lock could not "
764                          "be found.");
765 }
766
767 /* ---------------------------------------------------------------
768 **
769 ** Property-related lock functions
770 **
771 */
772
773 /*
774 ** dav_fs_get_supportedlock:  Returns a static string for all supportedlock
775 **    properties. I think we save more returning a static string than
776 **    constructing it every time, though it might look cleaner.
777 */
778 static const char *dav_fs_get_supportedlock(void)
779 {
780     static const char supported[] = DEBUG_CR
781         "<D:lockentry>" DEBUG_CR
782         "<D:lockscope><D:exclusive/></D:lockscope>" DEBUG_CR
783         "<D:locktype><D:write/></D:locktype>" DEBUG_CR
784         "</D:lockentry>" DEBUG_CR
785         "<D:lockentry>" DEBUG_CR
786         "<D:lockscope><D:shared/></D:lockscope>" DEBUG_CR
787         "<D:locktype><D:write/></D:locktype>" DEBUG_CR
788         "</D:lockentry>" DEBUG_CR;
789
790     return supported;
791 }
792
793 /* ---------------------------------------------------------------
794 **
795 ** General lock functions
796 **
797 */
798
799 /* ---------------------------------------------------------------
800 **
801 ** Functions dealing with lock-null resources
802 **
803 */
804
805 /*
806 ** dav_fs_load_locknull_list:  Returns a dav_buffer dump of the locknull file
807 **    for the given directory.
808 */
809 static dav_error * dav_fs_load_locknull_list(apr_pool_t *p, const char *dirpath,
810                                              dav_buffer *pbuf) 
811 {
812     apr_finfo_t finfo;
813     apr_file_t *file = NULL;
814     dav_error *err = NULL;
815     apr_ssize_t amt;
816
817     dav_buffer_init(p, pbuf, dirpath);
818
819     if (pbuf->buf[pbuf->cur_len - 1] == '/')
820         pbuf->buf[--pbuf->cur_len] = '\0';
821
822     dav_buffer_place(p, pbuf, "/" DAV_FS_STATE_DIR "/" DAV_FS_LOCK_NULL_FILE);
823
824     /* reset this in case we leave w/o reading into the buffer */
825     pbuf->cur_len = 0;
826
827     if (apr_open(&file, pbuf->buf, APR_READ | APR_BINARY, APR_OS_DEFAULT,
828                 p) != APR_SUCCESS) {
829         return NULL;
830     }
831
832     if (apr_getfileinfo(&finfo, file) != APR_SUCCESS) {
833         err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
834                             apr_psprintf(p,
835                                         "Opened but could not stat file %s",
836                                         pbuf->buf));
837         goto loaderror;
838     }
839
840     dav_set_bufsize(p, pbuf, finfo.size);
841     amt = finfo.size;
842     if (apr_read(file, pbuf->buf, &amt) != APR_SUCCESS
843         || amt != finfo.size) {
844         err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
845                             apr_psprintf(p,
846                                         "Failure reading locknull file "
847                                         "for %s", dirpath));
848
849         /* just in case the caller disregards the returned error */
850         pbuf->cur_len = 0;
851         goto loaderror;
852     }
853
854   loaderror:
855     apr_close(file);
856     return err;
857 }
858
859 /*
860 ** dav_fs_save_locknull_list:  Saves contents of pbuf into the
861 **    locknull file for dirpath.
862 */
863 static dav_error * dav_fs_save_locknull_list(apr_pool_t *p, const char *dirpath,
864                                              dav_buffer *pbuf)
865 {
866     const char *pathname;
867     apr_file_t *file = NULL;
868     dav_error *err = NULL;
869     apr_ssize_t amt;
870
871     if (pbuf->buf == NULL)
872         return NULL;
873
874     dav_fs_ensure_state_dir(p, dirpath);
875     pathname = apr_pstrcat(p,
876                           dirpath,
877                           dirpath[strlen(dirpath) - 1] == '/' ? "" : "/",
878                           DAV_FS_STATE_DIR "/" DAV_FS_LOCK_NULL_FILE,
879                           NULL);
880
881     if (pbuf->cur_len == 0) {
882         /* delete the file if cur_len == 0 */
883         if (remove(pathname) != 0) {
884             return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
885                                  apr_psprintf(p,
886                                              "Error removing %s", pathname));
887         }
888         return NULL;
889     }
890
891     if (apr_open(&file, pathname,
892                 APR_WRITE | APR_CREATE | APR_TRUNCATE | APR_BINARY,
893                 APR_OS_DEFAULT, p) != APR_SUCCESS) {
894         return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
895                              apr_psprintf(p,
896                                          "Error opening %s for writing",
897                                          pathname));
898     }
899
900     amt = pbuf->cur_len;
901     if (apr_write(file, pbuf->buf, &amt) != APR_SUCCESS
902         || amt != pbuf->cur_len) {
903         err = dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
904                             apr_psprintf(p,
905                                         "Error writing %i bytes to %s",
906                                         pbuf->cur_len, pathname));
907     }
908
909     apr_close(file);
910     return err;
911 }
912
913 /*
914 ** dav_fs_remove_locknull_member:  Removes filename from the locknull list
915 **    for directory path.
916 */
917 static dav_error * dav_fs_remove_locknull_member(apr_pool_t *p,
918                                                  const char *filename,
919                                                  dav_buffer *pbuf)
920 {
921     dav_error *err;
922     size_t len;
923     size_t scanlen;
924     char *scan;
925     const char *scanend;
926     char *dirpath = apr_pstrdup(p, filename);
927     char *fname = strrchr(dirpath, '/');
928     int dirty = 0;
929
930     if (fname != NULL)
931         *fname++ = '\0';
932     else
933         fname = dirpath;
934     len = strlen(fname) + 1;
935
936     if ((err = dav_fs_load_locknull_list(p, dirpath, pbuf)) != NULL) {
937         /* ### add a higher level description? */
938         return err;
939     }
940
941     for (scan = pbuf->buf, scanend = scan + pbuf->cur_len;
942          scan < scanend;
943          scan += scanlen) {
944         scanlen = strlen(scan) + 1;
945         if (len == scanlen && memcmp(fname, scan, scanlen) == 0) {
946             pbuf->cur_len -= scanlen;
947             memmove(scan, scan + scanlen, scanend - (scan + scanlen));
948             dirty = 1;
949             break;
950         }
951     }
952
953     if (dirty) {
954         if ((err = dav_fs_save_locknull_list(p, dirpath, pbuf)) != NULL) {
955             /* ### add a higher level description? */
956             return err;
957         }
958     }
959
960     return NULL;
961 }
962
963 /* Note: used by dav_fs_repos.c */
964 dav_error * dav_fs_get_locknull_members(
965     const dav_resource *resource,
966     dav_buffer *pbuf)
967 {
968     const char *dirpath;
969
970     dav_fs_dir_file_name(resource, &dirpath, NULL);
971     return dav_fs_load_locknull_list(dav_fs_pool(resource), dirpath, pbuf);
972 }
973
974 /* ### fold into append_lock? */
975 /* ### take an optional buf parameter? */
976 static dav_error * dav_fs_add_locknull_state(
977     dav_lockdb *lockdb,
978     const dav_resource *resource)
979 {
980     dav_buffer buf = { 0 };
981     apr_pool_t *p = lockdb->info->pool;
982     const char *dirpath;
983     const char *fname;
984     dav_error *err;
985
986     dav_fs_dir_file_name(resource, &dirpath, &fname);
987
988     if ((err = dav_fs_load_locknull_list(p, dirpath, &buf)) != NULL) {
989         return dav_push_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
990                               "Could not load .locknull file.", err);
991     }
992
993     dav_buffer_append(p, &buf, fname);
994     buf.cur_len++;      /* we want the null-term here */
995
996     if ((err = dav_fs_save_locknull_list(p, dirpath, &buf)) != NULL) {
997         return dav_push_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
998                               "Could not save .locknull file.", err);
999     }
1000
1001     return NULL;
1002 }
1003
1004 /*
1005 ** dav_fs_remove_locknull_state:  Given a request, check to see if r->filename
1006 **    is/was a lock-null resource.  If so, return it to an existant state.
1007 **
1008 **    ### this function is broken... it doesn't check!
1009 **
1010 **    In this implementation, this involves two things:
1011 **    (a) remove it from the list in the appropriate .DAV/locknull file
1012 **    (b) on *nix, convert the key from a filename to an inode.
1013 */
1014 static dav_error * dav_fs_remove_locknull_state(
1015     dav_lockdb *lockdb,
1016     const dav_resource *resource)
1017 {
1018     dav_buffer buf = { 0 };
1019     dav_error *err;
1020     apr_pool_t *p = lockdb->info->pool;
1021     const char *pathname = dav_fs_pathname(resource);
1022
1023     if ((err = dav_fs_remove_locknull_member(p, pathname, &buf)) != NULL) {
1024         /* ### add a higher-level description? */
1025         return err;
1026     }
1027
1028 #ifndef WIN32
1029     {
1030         dav_lock_discovery *ld;
1031         dav_lock_indirect  *id;
1032         dav_datum key;
1033
1034         /*
1035         ** Fetch the lock(s) that made the resource lock-null. Remove
1036         ** them under the filename key. Obtain the new inode key, and
1037         ** save the same lock information under it.
1038         */
1039         key = dav_fs_build_fname_key(p, pathname);
1040         if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1041                                            &ld, &id)) != NULL) {
1042             /* ### insert a higher-level error description */
1043             return err;
1044         }
1045
1046         if ((err = dav_fs_save_lock_record(lockdb, key, NULL, NULL)) != NULL) {
1047             /* ### insert a higher-level error description */
1048             return err;
1049         }
1050
1051         key = dav_fs_build_key(p, resource);
1052         if ((err = dav_fs_save_lock_record(lockdb, key, ld, id)) != NULL) {
1053             /* ### insert a higher-level error description */
1054             return err;
1055         }
1056     }
1057 #endif
1058
1059     return NULL;
1060 }
1061
1062 static dav_error * dav_fs_create_lock(dav_lockdb *lockdb,
1063                                       const dav_resource *resource,
1064                                       dav_lock **lock)
1065 {
1066     dav_datum key;
1067
1068     key = dav_fs_build_key(lockdb->info->pool, resource);
1069
1070     *lock = dav_fs_alloc_lock(lockdb,
1071                               key,
1072                               NULL);
1073
1074     (*lock)->is_locknull = !resource->exists;
1075
1076     return NULL;
1077 }
1078
1079 static dav_error * dav_fs_get_locks(dav_lockdb *lockdb,
1080                                     const dav_resource *resource,
1081                                     int calltype,
1082                                     dav_lock **locks)
1083 {
1084     apr_pool_t *p = lockdb->info->pool;
1085     dav_datum key;
1086     dav_error *err;
1087     dav_lock *lock = NULL;
1088     dav_lock *newlock;
1089     dav_lock_discovery *dp;
1090     dav_lock_indirect *ip;
1091
1092 #if DAV_DEBUG
1093     if (calltype == DAV_GETLOCKS_COMPLETE) {
1094         return dav_new_error(lockdb->info->pool,
1095                              HTTP_INTERNAL_SERVER_ERROR, 0,
1096                              "INTERNAL DESIGN ERROR: DAV_GETLOCKS_COMPLETE "
1097                              "is not yet supported");
1098     }
1099 #endif
1100
1101     key = dav_fs_build_key(p, resource);
1102     if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1103                                        &dp, &ip)) != NULL) {
1104         /* ### push a higher-level desc? */
1105         return err;
1106     }
1107
1108     /* copy all direct locks to the result list */
1109     for (; dp != NULL; dp = dp->next) {
1110         newlock = dav_fs_alloc_lock(lockdb, key, dp->locktoken);
1111         newlock->is_locknull = !resource->exists;
1112         newlock->scope = dp->f.scope;
1113         newlock->type = dp->f.type;
1114         newlock->depth = dp->f.depth;
1115         newlock->timeout = dp->f.timeout;
1116         newlock->owner = dp->owner;
1117         newlock->auth_user = dp->auth_user;
1118
1119         /* hook into the result list */
1120         newlock->next = lock;
1121         lock = newlock;
1122     }
1123
1124     /* copy all the indirect locks to the result list. resolve as needed. */
1125     for (; ip != NULL; ip = ip->next) {
1126         newlock = dav_fs_alloc_lock(lockdb, ip->key, ip->locktoken);
1127         newlock->is_locknull = !resource->exists;
1128
1129         if (calltype == DAV_GETLOCKS_RESOLVED) {
1130             if ((err = dav_fs_resolve(lockdb, ip, &dp, NULL, NULL)) != NULL) {
1131                 /* ### push a higher-level desc? */
1132                 return err;
1133             }
1134
1135             newlock->scope = dp->f.scope;
1136             newlock->type = dp->f.type;
1137             newlock->depth = dp->f.depth;
1138             newlock->timeout = dp->f.timeout;
1139             newlock->owner = dp->owner;
1140             newlock->auth_user = dp->auth_user;
1141         }
1142         else {
1143             /* DAV_GETLOCKS_PARTIAL */
1144             newlock->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
1145         }
1146
1147         /* hook into the result list */
1148         newlock->next = lock;
1149         lock = newlock;
1150     }
1151
1152     *locks = lock;
1153     return NULL;
1154 }
1155
1156 static dav_error * dav_fs_find_lock(dav_lockdb *lockdb,
1157                                     const dav_resource *resource,
1158                                     const dav_locktoken *locktoken,
1159                                     int partial_ok,
1160                                     dav_lock **lock)
1161 {
1162     dav_error *err;
1163     dav_datum key;
1164     dav_lock_discovery *dp;
1165     dav_lock_indirect *ip;
1166
1167     *lock = NULL;
1168
1169     key = dav_fs_build_key(lockdb->info->pool, resource);
1170     if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1171                                        &dp, &ip)) != NULL) {
1172         /* ### push a higher-level desc? */
1173         return err;
1174     }
1175
1176     for (; dp != NULL; dp = dp->next) {
1177         if (!dav_compare_opaquelocktoken(locktoken->uuid,
1178                                          dp->locktoken->uuid)) {
1179             *lock = dav_fs_alloc_lock(lockdb, key, locktoken);
1180             (*lock)->is_locknull = !resource->exists;
1181             (*lock)->scope = dp->f.scope;
1182             (*lock)->type = dp->f.type;
1183             (*lock)->depth = dp->f.depth;
1184             (*lock)->timeout = dp->f.timeout;
1185             (*lock)->owner = dp->owner;
1186             (*lock)->auth_user = dp->auth_user;
1187             return NULL;
1188         }
1189     }
1190
1191     for (; ip != NULL; ip = ip->next) {
1192         if (!dav_compare_opaquelocktoken(locktoken->uuid,
1193                                          ip->locktoken->uuid)) {
1194             *lock = dav_fs_alloc_lock(lockdb, ip->key, locktoken);
1195             (*lock)->is_locknull = !resource->exists;
1196
1197             /* ### nobody uses the resolving right now! */
1198             if (partial_ok) {
1199                 (*lock)->rectype = DAV_LOCKREC_INDIRECT_PARTIAL;
1200             }
1201             else {
1202                 (*lock)->rectype = DAV_LOCKREC_INDIRECT;
1203                 if ((err = dav_fs_resolve(lockdb, ip, &dp,
1204                                           NULL, NULL)) != NULL) {
1205                     /* ### push a higher-level desc? */
1206                     return err;
1207                 }
1208                 (*lock)->scope = dp->f.scope;
1209                 (*lock)->type = dp->f.type;
1210                 (*lock)->depth = dp->f.depth;
1211                 (*lock)->timeout = dp->f.timeout;
1212                 (*lock)->owner = dp->owner;
1213                 (*lock)->auth_user = dp->auth_user;
1214             }
1215             return NULL;
1216         }
1217     }
1218
1219     return NULL;
1220 }
1221
1222 static dav_error * dav_fs_has_locks(dav_lockdb *lockdb,
1223                                     const dav_resource *resource,
1224                                     int *locks_present)
1225 {
1226     dav_error *err;
1227     dav_datum key;
1228
1229     *locks_present = 0;
1230
1231     if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {
1232         /* ### insert a higher-level error description */
1233         return err;
1234     }
1235
1236     /*
1237     ** If we opened readonly and the db wasn't there, then there are no
1238     ** locks for this resource. Just exit.
1239     */
1240     if (lockdb->info->db == NULL)
1241         return NULL;
1242
1243     key = dav_fs_build_key(lockdb->info->pool, resource);
1244
1245     *locks_present = (*dav_hooks_db_dbm.exists)(lockdb->info->db, key);
1246
1247     return NULL;
1248 }
1249
1250 static dav_error * dav_fs_append_locks(dav_lockdb *lockdb,
1251                                        const dav_resource *resource,
1252                                        int make_indirect,
1253                                        const dav_lock *lock)
1254 {
1255     apr_pool_t *p = lockdb->info->pool;
1256     dav_error *err;
1257     dav_lock_indirect *ip;
1258     dav_lock_discovery *dp;
1259     dav_datum key;
1260
1261     key = dav_fs_build_key(lockdb->info->pool, resource);
1262     if ((err = dav_fs_load_lock_record(lockdb, key, 0, &dp, &ip)) != NULL) {
1263         /* ### maybe add in a higher-level description */
1264         return err;
1265     }
1266
1267     /*
1268     ** ### when we store the lock more directly, we need to update
1269     ** ### lock->rectype and lock->is_locknull
1270     */
1271
1272     if (make_indirect) {
1273         for (; lock != NULL; lock = lock->next) {
1274
1275             /* ### this works for any <lock> rectype */
1276             dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
1277
1278             /* ### shut off the const warning for now */
1279             newi->locktoken = (dav_locktoken *)lock->locktoken;
1280             newi->timeout   = lock->timeout;
1281             newi->key       = lock->info->key;
1282             newi->next      = ip;
1283             ip              = newi;
1284         }
1285     }
1286     else {
1287         for (; lock != NULL; lock = lock->next) {
1288             /* create and link in the right kind of lock */
1289
1290             if (lock->rectype == DAV_LOCKREC_DIRECT) {
1291                 dav_lock_discovery *newd = apr_pcalloc(p, sizeof(*newd));
1292
1293                 newd->f.scope = lock->scope;
1294                 newd->f.type = lock->type;
1295                 newd->f.depth = lock->depth;
1296                 newd->f.timeout = lock->timeout;
1297                 /* ### shut off the const warning for now */
1298                 newd->locktoken = (dav_locktoken *)lock->locktoken;
1299                 newd->owner = lock->owner;
1300                 newd->auth_user = lock->auth_user;
1301                 newd->next = dp;
1302                 dp = newd;
1303             }
1304             else {
1305                 /* DAV_LOCKREC_INDIRECT(_PARTIAL) */
1306
1307                 dav_lock_indirect *newi = apr_pcalloc(p, sizeof(*newi));
1308
1309                 /* ### shut off the const warning for now */
1310                 newi->locktoken = (dav_locktoken *)lock->locktoken;
1311                 newi->key       = lock->info->key;
1312                 newi->next      = ip;
1313                 ip              = newi;
1314             }
1315         }
1316     }
1317
1318     if ((err = dav_fs_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1319         /* ### maybe add a higher-level description */
1320         return err;
1321     }
1322
1323     /* we have a special list for recording locknull resources */
1324     /* ### ack! this can add two copies to the locknull list */
1325     if (!resource->exists
1326         && (err = dav_fs_add_locknull_state(lockdb, resource)) != NULL) {
1327         /* ### maybe add a higher-level description */
1328         return err;
1329     }
1330
1331     return NULL;
1332 }
1333
1334 static dav_error * dav_fs_remove_lock(dav_lockdb *lockdb,
1335                                       const dav_resource *resource,
1336                                       const dav_locktoken *locktoken)
1337 {
1338     dav_error *err;
1339     dav_buffer buf = { 0 };
1340     dav_lock_discovery *dh = NULL;
1341     dav_lock_indirect *ih = NULL;
1342     dav_datum key;
1343
1344     key = dav_fs_build_key(lockdb->info->pool, resource);
1345
1346     if (locktoken != NULL) {
1347         dav_lock_discovery *dp;
1348         dav_lock_discovery *dprev = NULL;
1349         dav_lock_indirect *ip;
1350         dav_lock_indirect *iprev = NULL;
1351
1352         if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1353                                            &dh, &ih)) != NULL) {
1354             /* ### maybe add a higher-level description */
1355             return err;
1356         }
1357
1358         for (dp = dh; dp != NULL; dp = dp->next) {
1359             if (dav_compare_opaquelocktoken(locktoken->uuid,
1360                                             dp->locktoken->uuid) == 0) {
1361                 if (dprev)
1362                     dprev->next = dp->next;
1363                 else
1364                     dh = dh->next;
1365             }
1366             dprev = dp;
1367         }
1368
1369         for (ip = ih; ip != NULL; ip = ip->next) {
1370             if (dav_compare_opaquelocktoken(locktoken->uuid,
1371                                             ip->locktoken->uuid) == 0) {
1372                 if (iprev)
1373                     iprev->next = ip->next;
1374                 else
1375                     ih = ih->next;
1376             }
1377             iprev = ip;
1378         }
1379
1380     }
1381
1382     /* save the modified locks, or remove all locks (dh=ih=NULL). */
1383     if ((err = dav_fs_save_lock_record(lockdb, key, dh, ih)) != NULL) {
1384         /* ### maybe add a higher-level description */
1385         return err;
1386     }
1387
1388     /*
1389     ** If this resource is a locknull resource AND no more locks exist,
1390     ** then remove the locknull member.
1391     **
1392     ** Note: remove_locknull_state() attempts to convert a locknull member
1393     **       to a real member. In this case, all locks are gone, so the
1394     **       locknull resource returns to the null state (ie. doesn't exist),
1395     **       so there is no need to update the lockdb (and it won't find
1396     **       any because a precondition is that none exist).
1397     */
1398     if (!resource->exists && dh == NULL && ih == NULL
1399         && (err = dav_fs_remove_locknull_member(lockdb->info->pool,
1400                                                 dav_fs_pathname(resource),
1401                                                 &buf)) != NULL) {
1402         /* ### maybe add a higher-level description */
1403         return err;
1404     }
1405
1406     return NULL;
1407 }
1408
1409 static int dav_fs_do_refresh(dav_lock_discovery *dp,
1410                              const dav_locktoken_list *ltl,
1411                              time_t new_time)
1412 {
1413     int dirty = 0;
1414
1415     for (; ltl != NULL; ltl = ltl->next) {
1416         if (dav_compare_opaquelocktoken(dp->locktoken->uuid,
1417                                         ltl->locktoken->uuid) == 0)
1418         {
1419             dp->f.timeout = new_time;
1420             dirty = 1;
1421         }
1422     }
1423
1424     return dirty;
1425 }
1426
1427 static dav_error * dav_fs_refresh_locks(dav_lockdb *lockdb,
1428                                         const dav_resource *resource,
1429                                         const dav_locktoken_list *ltl,
1430                                         time_t new_time,
1431                                         dav_lock **locks)
1432 {
1433     dav_error *err;
1434     dav_datum key;
1435     dav_lock_discovery *dp;
1436     dav_lock_discovery *dp_scan;
1437     dav_lock_indirect *ip;
1438     int dirty = 0;
1439     dav_lock *newlock;
1440
1441     *locks = NULL;
1442
1443     key = dav_fs_build_key(lockdb->info->pool, resource);
1444     if ((err = dav_fs_load_lock_record(lockdb, key, DAV_CREATE_LIST,
1445                                        &dp, &ip)) != NULL) {
1446         /* ### maybe add in a higher-level description */
1447         return err;
1448     }
1449
1450     /* ### we should be refreshing direct AND (resolved) indirect locks! */
1451
1452     /* refresh all of the direct locks on this resource */
1453     for (dp_scan = dp; dp_scan != NULL; dp_scan = dp_scan->next) {
1454         if (dav_fs_do_refresh(dp_scan, ltl, new_time)) {
1455             /* the lock was refreshed. return the lock. */
1456             newlock = dav_fs_alloc_lock(lockdb, key, dp_scan->locktoken);
1457             newlock->is_locknull = !resource->exists;
1458             newlock->scope = dp_scan->f.scope;
1459             newlock->type = dp_scan->f.type;
1460             newlock->depth = dp_scan->f.depth;
1461             newlock->timeout = dp_scan->f.timeout;
1462             newlock->owner = dp_scan->owner;
1463             newlock->auth_user = dp_scan->auth_user;
1464
1465             newlock->next = *locks;
1466             *locks = newlock;
1467
1468             dirty = 1;
1469         }
1470     }
1471
1472     /* if we refreshed any locks, then save them back. */
1473     if (dirty
1474         && (err = dav_fs_save_lock_record(lockdb, key, dp, ip)) != NULL) {
1475         /* ### maybe add in a higher-level description */
1476         return err;
1477     }
1478
1479     /* for each indirect lock, find its direct lock and refresh it. */
1480     for (; ip != NULL; ip = ip->next) {
1481         dav_lock_discovery *ref_dp;
1482         dav_lock_indirect *ref_ip;
1483
1484         if ((err = dav_fs_resolve(lockdb, ip, &dp_scan,
1485                                   &ref_dp, &ref_ip)) != NULL) {
1486             /* ### push a higher-level desc? */
1487             return err;
1488         }
1489         if (dav_fs_do_refresh(dp_scan, ltl, new_time)) {
1490             /* the lock was refreshed. return the lock. */
1491             newlock = dav_fs_alloc_lock(lockdb, ip->key, dp->locktoken);
1492             newlock->is_locknull = !resource->exists;
1493             newlock->scope = dp->f.scope;
1494             newlock->type = dp->f.type;
1495             newlock->depth = dp->f.depth;
1496             newlock->timeout = dp->f.timeout;
1497             newlock->owner = dp->owner;
1498             newlock->auth_user = dp_scan->auth_user;
1499
1500             newlock->next = *locks;
1501             *locks = newlock;
1502
1503             /* save the (resolved) direct lock back */
1504             if ((err = dav_fs_save_lock_record(lockdb, ip->key, ref_dp,
1505                                                ref_ip)) != NULL) {
1506                 /* ### push a higher-level desc? */
1507                 return err;
1508             }
1509         }
1510     }
1511
1512     return NULL;
1513 }
1514
1515
1516 const dav_hooks_locks dav_hooks_locks_fs =
1517 {
1518     dav_fs_get_supportedlock,
1519     dav_fs_parse_locktoken,
1520     dav_fs_format_locktoken,
1521     dav_fs_compare_locktoken,
1522     dav_fs_open_lockdb,
1523     dav_fs_close_lockdb,
1524     dav_fs_remove_locknull_state,
1525     dav_fs_create_lock,
1526     dav_fs_get_locks,
1527     dav_fs_find_lock,
1528     dav_fs_has_locks,
1529     dav_fs_append_locks,
1530     dav_fs_remove_lock,
1531     dav_fs_refresh_locks,
1532     NULL, /* get_resource */
1533 };