lock.c revision 08cb74ca432a8c24e39f17dedce527e6a47b8001
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
** DAV filesystem lock implementation
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_file_io.h"
#include "apr_uuid.h"
#define APR_WANT_MEMFUNC
#include "apr_want.h"
#include "httpd.h"
#include "http_log.h"
#include "mod_dav.h"
#include "repos.h"
/* ---------------------------------------------------------------
**
** Lock database primitives
**
*/
/*
** LOCK DATABASES
**
** Lockdiscovery information is stored in the single lock database specified
** by the DAVLockDB directive. Information about this db is stored in the
** global server configuration.
**
** KEY
**
** The database is keyed by a key_type unsigned char (DAV_TYPE_INODE or
** DAV_TYPE_FNAME) followed by inode and device number if possible,
** otherwise full path (in the case of Win32 or lock-null resources).
**
** VALUE
**
** The value consists of a list of elements.
** DIRECT LOCK: [char (DAV_LOCK_DIRECT),
** char (dav_lock_scope),
** char (dav_lock_type),
** int depth,
** time_t expires,
** apr_uuid_t locktoken,
** char[] owner,
** char[] auth_user]
**
** INDIRECT LOCK: [char (DAV_LOCK_INDIRECT),
** apr_uuid_t locktoken,
** time_t expires,
** apr_size_t key_size,
** char[] key]
** The key is to the collection lock that resulted in this indirect lock
*/
#define DAV_TRUE 1
#define DAV_FALSE 0
#define DAV_CREATE_LIST 23
#define DAV_APPEND_LIST 24
/* Stored lock_discovery prefix */
#define DAV_LOCK_DIRECT 1
#define DAV_LOCK_INDIRECT 2
#define DAV_TYPE_INODE 10
#define DAV_TYPE_FNAME 11
/* ack. forward declare. */
const char *filename,
dav_buffer *pbuf);
/*
** Use the opaquelock scheme for locktokens
*/
struct dav_locktoken {
};
/* #################################################################
** ### keep these structures (internal) or move fully to dav_lock?
*/
/*
** We need to reliably size the fixed-length portion of
** dav_lock_discovery; best to separate it into another
** struct for a convenient sizeof, unless we pack lock_discovery.
*/
typedef struct dav_lock_discovery_fixed
{
char scope;
char type;
int depth;
typedef struct dav_lock_discovery
{
struct dav_lock_discovery_fixed f;
const char *owner; /* owner field from activelock */
const char *auth_user; /* authenticated user who created the lock */
struct dav_lock_discovery *next;
/* Indirect locks represent locks inherited from containing collections.
* They reference the lock token for the collection the lock is
* inherited from. A lock provider may also define a key to the
* inherited lock, for fast datbase lookup. The key is opaque outside
* the lock provider.
*/
typedef struct dav_lock_indirect
{
struct dav_lock_indirect *next;
/* ################################################################# */
/*
** Stored direct lock info - full lock_discovery length:
** prefix + Fixed length + lock token + 2 strings + 2 nulls (one for each string)
*/
+ sizeof(apr_uuid_t) \
+ 2)
/* Stored indirect lock info - lock token and apr_datum_t */
+ sizeof(time_t) \
/*
** The lockdb structure.
**
** The <db> field may be NULL, meaning one of two things:
** 1) That we have not actually opened the underlying database (yet). The
** <opened> field should be false.
** 2) We opened it readonly and it wasn't present.
**
** The delayed opening (determined by <opened>) makes creating a lockdb
** quick, while deferring the underlying I/O until it is actually required.
**
** We export the notion of a lockdb, but hide the details of it. Most
** implementations will use a database of some kind, but it is certainly
** possible that alternatives could be used.
*/
struct dav_lockdb_private
{
request_rec *r; /* for accessing the uuid state */
const char *lockdb_path; /* where is the lock database? */
int opened; /* we opened the database */
};
typedef struct
{
/*
** The private part of the lock structure.
*/
struct dav_lock_private
{
};
typedef struct
{
/*
** This must be forward-declared so the open_lockdb function can use it.
*/
extern const dav_hooks_locks dav_hooks_locks_fs;
/* internal function for creating locks */
const dav_locktoken *locktoken)
{
}
else {
}
}
/*
** dav_fs_parse_locktoken
**
** Parse an opaquelocktoken URI into a locktoken.
*/
static dav_error * dav_fs_parse_locktoken(
apr_pool_t *p,
const char *char_token,
{
return dav_new_error(p,
"The lock token uses an unknown State-token "
"format and could not be parsed.");
}
char_token += 16;
"The opaquelocktoken has an incorrect format "
"and could not be parsed.");
}
*locktoken_p = locktoken;
return NULL;
}
/*
** dav_fs_format_locktoken
**
** Generate the URI for a locktoken
*/
static const char *dav_fs_format_locktoken(
apr_pool_t *p,
const dav_locktoken *locktoken)
{
}
/*
** dav_fs_compare_locktoken
**
** Determine whether two locktokens are the same
*/
static int dav_fs_compare_locktoken(
const dav_locktoken *lt1,
const dav_locktoken *lt2)
{
}
/*
** dav_fs_really_open_lockdb:
**
** If the database hasn't been opened yet, then open the thing.
*/
{
return NULL;
"Could not open the lock database.",
err);
}
/* all right. it is opened now. */
return NULL;
}
/*
** dav_fs_open_lockdb:
**
** "open" the lock database, as specified in the global server configuration.
** If force is TRUE, then the database is opened now, rather than lazily.
**
*/
dav_lockdb **lockdb)
{
"A lock database was not specified with the "
"DAVLockDB directive. One must be specified "
"to use the locking functionality.");
}
/* done initializing. return it. */
if (force) {
/* ### add a higher-level comment? */
return dav_fs_really_open_lockdb(*lockdb);
}
return NULL;
}
/*
** dav_fs_close_lockdb:
**
** Close it. Duh.
*/
{
}
/*
** dav_fs_build_fname_key
**
** Given a pathname, build a DAV_TYPE_FNAME lock database key.
*/
{
/* ### does this allocation have a proper lifetime? need to check */
/* ### can we use a buffer for this? */
/* size is TYPE + pathname + null */
return key;
}
/*
** dav_fs_build_key: Given a resource, return a apr_datum_t key
** to look up lock information for this file.
**
** apr_datum_t->dvalue = full path
**
** apr_datum_t->dvalue = inode, dev
*/
const dav_resource *resource)
{
/* ### use lstat() ?? */
/*
*/
{
/* ### can we use a buffer for this? */
return key;
}
return dav_fs_build_fname_key(p, file);
}
/*
** dav_fs_lock_expired: return 1 (true) if the given timeout is in the past
** or present (the lock has expired), or 0 (false) if in the future
** (the lock has not yet expired).
*/
{
}
/*
** dav_fs_save_lock_record: Saves the lock information specified in the
** direct and indirect lock lists about path into the lock database.
** If direct and indirect == NULL, the key is removed.
*/
{
apr_datum_t val = { 0 };
char *ptr;
#if DAV_DEBUG
"INTERNAL DESIGN ERROR: the lockdb was opened "
"readonly, but an attempt to save locks was "
"performed.");
}
#endif
/* ### add a higher-level error? */
return err;
}
/* If nothing to save, delete key */
/* don't fail if the key is not present */
/* ### but what about other errors? */
return NULL;
}
while(dp) {
}
while(ip) {
}
/* ### can this be apr_palloc() ? */
/* ### hmmm.... investigate the use of a buffer here */
while(dp) {
*ptr++ = '\0';
}
else {
}
*ptr++ = '\0';
}
else {
}
}
while(ip) {
}
/* ### more details? add an error_id? */
"Could not save lock information.",
err);
}
return NULL;
}
/*
** dav_load_lock_record: Reads lock information about key from lock db;
** creates linked lists of the direct and indirect locks.
**
** If add_method = DAV_APPEND_LIST, the result will be appended to the
** head of the direct and indirect lists supplied.
**
** Passive lock removal: If lock has timed out, it will not be returned.
** ### How much "logging" does RFC 2518 require?
*/
int add_method,
{
apr_size_t offset = 0;
apr_datum_t val = { 0 };
dav_buffer buf = { 0 };
if (add_method != DAV_APPEND_LIST) {
}
/* ### add a higher-level error? */
return err;
}
/*
** If we opened readonly and the db wasn't there, then there are no
** locks for this resource. Just exit.
*/
return NULL;
return err;
return NULL;
case DAV_LOCK_DIRECT:
/* Create and fill a dav_lock_discovery structure */
++offset;
}
else {
}
++offset;
}
else {
}
}
else {
/* Remove timed-out locknull fm .locknull list */
/* if we don't see the file, then it's a locknull */
/* ### push a higher-level description? */
return err;
}
}
}
}
break;
case DAV_LOCK_INDIRECT:
/* Create and fill a dav_lock_indirect structure */
}
else {
/* A locknull resource will never be locked indirectly */
}
break;
default:
/* ### should use a computed_desc and insert corrupt token data */
--offset;
return dav_new_error(p,
apr_psprintf(p,
"The lock database was found to "
"be corrupt. offset %"
APR_SIZE_T_FMT ", c=%02x",
}
}
/* Clean up this record if we found expired locks */
/*
** ### shouldn't do this if we've been opened READONLY. elide the
** ### timed-out locks from the response, but don't save that info back
*/
}
return NULL;
}
/* resolve <indirect>, returning <*direct> */
{
/* ### insert a higher-level description? */
return err;
}
}
return NULL;
}
}
/* No match found (but we should have found one!) */
"The lock database was found to be corrupt. "
"An indirect lock's direct lock could not "
"be found.");
}
/* ---------------------------------------------------------------
**
** Property-related lock functions
**
*/
/*
** dav_fs_get_supportedlock: Returns a static string for all supportedlock
** properties. I think we save more returning a static string than
** constructing it every time, though it might look cleaner.
*/
{
"<D:lockentry>" DEBUG_CR
"<D:lockscope><D:exclusive/></D:lockscope>" DEBUG_CR
"<D:locktype><D:write/></D:locktype>" DEBUG_CR
"</D:lockentry>" DEBUG_CR
"<D:lockentry>" DEBUG_CR
"<D:lockscope><D:shared/></D:lockscope>" DEBUG_CR
"<D:locktype><D:write/></D:locktype>" DEBUG_CR
"</D:lockentry>" DEBUG_CR;
return supported;
}
/* ---------------------------------------------------------------
**
** General lock functions
**
*/
/* ---------------------------------------------------------------
**
** Functions dealing with lock-null resources
**
*/
/*
** dav_fs_load_locknull_list: Returns a dav_buffer dump of the locknull file
** for the given directory.
*/
{
/* reset this in case we leave w/o reading into the buffer */
p) != APR_SUCCESS) {
return NULL;
}
if (rv != APR_SUCCESS) {
apr_psprintf(p,
"Opened but could not stat file %s",
goto loaderror;
}
apr_psprintf(p,
"Opened but rejected huge file %s",
goto loaderror;
}
apr_psprintf(p,
"Failure reading locknull file "
"for %s", dirpath));
/* just in case the caller disregards the returned error */
goto loaderror;
}
return err;
}
/*
** dav_fs_save_locknull_list: Saves contents of pbuf into the
** locknull file for dirpath.
*/
{
const char *pathname;
return NULL;
pathname = apr_pstrcat(p,
NULL);
/* delete the file if cur_len == 0 */
if (apr_file_remove(pathname, p) != 0) {
return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
apr_psprintf(p,
"Error removing %s", pathname));
}
return NULL;
}
APR_OS_DEFAULT, p) != APR_SUCCESS) {
return dav_new_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
apr_psprintf(p,
"Error opening %s for writing",
pathname));
}
apr_psprintf(p,
"Error writing %" APR_SIZE_T_FMT
" bytes to %s",
}
return err;
}
/*
** dav_fs_remove_locknull_member: Removes filename from the locknull list
** for directory path.
*/
const char *filename,
{
char *scan;
const char *scanend;
int dirty = 0;
*fname++ = '\0';
else
/* ### add a higher level description? */
return err;
}
dirty = 1;
break;
}
}
if (dirty) {
/* ### add a higher level description? */
return err;
}
}
return NULL;
}
/* Note: used by dav_fs_repos.c */
const dav_resource *resource,
{
const char *dirpath;
/* ### should test this result value... */
}
/* ### fold into append_lock? */
/* ### take an optional buf parameter? */
static dav_error * dav_fs_add_locknull_state(
const dav_resource *resource)
{
dav_buffer buf = { 0 };
const char *dirpath;
const char *fname;
/* ### should test this result value... */
return dav_push_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
"Could not load .locknull file.", err);
}
return dav_push_error(p, HTTP_INTERNAL_SERVER_ERROR, 0,
"Could not save .locknull file.", err);
}
return NULL;
}
/*
** dav_fs_remove_locknull_state: Given a request, check to see if r->filename
**
** ### this function is broken... it doesn't check!
**
** In this implementation, this involves two things:
** (b) on *nix, convert the key from a filename to an inode.
*/
static dav_error * dav_fs_remove_locknull_state(
const dav_resource *resource)
{
dav_buffer buf = { 0 };
/* ### add a higher-level description? */
return err;
}
{
/*
** Fetch the lock(s) that made the resource lock-null. Remove
** them under the filename key. Obtain the new inode key, and
** save the same lock information under it.
*/
/* ### insert a higher-level error description */
return err;
}
/* ### insert a higher-level error description */
return err;
}
/* ### insert a higher-level error description */
return err;
}
}
return NULL;
}
const dav_resource *resource,
{
key,
NULL);
return NULL;
}
const dav_resource *resource,
int calltype,
{
#if DAV_DEBUG
if (calltype == DAV_GETLOCKS_COMPLETE) {
"INTERNAL DESIGN ERROR: DAV_GETLOCKS_COMPLETE "
"is not yet supported");
}
#endif
/* ### push a higher-level desc? */
return err;
}
/* copy all direct locks to the result list */
/* hook into the result list */
}
/* copy all the indirect locks to the result list. resolve as needed. */
if (calltype == DAV_GETLOCKS_RESOLVED) {
/* ### push a higher-level desc? */
return err;
}
}
else {
/* DAV_GETLOCKS_PARTIAL */
}
/* hook into the result list */
}
return NULL;
}
const dav_resource *resource,
const dav_locktoken *locktoken,
int partial_ok,
{
/* ### push a higher-level desc? */
return err;
}
return NULL;
}
}
/* ### nobody uses the resolving right now! */
if (partial_ok) {
}
else {
/* ### push a higher-level desc? */
return err;
}
}
return NULL;
}
}
return NULL;
}
const dav_resource *resource,
int *locks_present)
{
*locks_present = 0;
/* ### insert a higher-level error description */
return err;
}
/*
** If we opened readonly and the db wasn't there, then there are no
** locks for this resource. Just exit.
*/
return NULL;
return NULL;
}
const dav_resource *resource,
int make_indirect,
{
/* ### maybe add in a higher-level description */
return err;
}
/*
** ### when we store the lock more directly, we need to update
** ### lock->rectype and lock->is_locknull
*/
if (make_indirect) {
/* ### this works for any <lock> rectype */
/* ### shut off the const warning for now */
}
}
else {
/* create and link in the right kind of lock */
/* ### shut off the const warning for now */
}
else {
/* DAV_LOCKREC_INDIRECT(_PARTIAL) */
/* ### shut off the const warning for now */
}
}
}
/* ### maybe add a higher-level description */
return err;
}
/* we have a special list for recording locknull resources */
/* ### ack! this can add two copies to the locknull list */
/* ### maybe add a higher-level description */
return err;
}
return NULL;
}
const dav_resource *resource,
const dav_locktoken *locktoken)
{
dav_buffer buf = { 0 };
/* ### maybe add a higher-level description */
return err;
}
if (dprev)
else
}
}
if (iprev)
else
}
}
}
/* save the modified locks, or remove all locks (dh=ih=NULL). */
/* ### maybe add a higher-level description */
return err;
}
/*
** If this resource is a locknull resource AND no more locks exist,
** then remove the locknull member.
**
** Note: remove_locknull_state() attempts to convert a locknull member
** to a real member. In this case, all locks are gone, so the
** locknull resource returns to the null state (ie. doesn't exist),
** so there is no need to update the lockdb (and it won't find
** any because a precondition is that none exist).
*/
/* ### maybe add a higher-level description */
return err;
}
return NULL;
}
const dav_locktoken_list *ltl,
{
int dirty = 0;
{
dirty = 1;
}
}
return dirty;
}
const dav_resource *resource,
const dav_locktoken_list *ltl,
{
int dirty = 0;
/* ### maybe add in a higher-level description */
return err;
}
/* ### we should be refreshing direct AND (resolved) indirect locks! */
/* refresh all of the direct locks on this resource */
/* the lock was refreshed. return the lock. */
dirty = 1;
}
}
/* if we refreshed any locks, then save them back. */
if (dirty
/* ### maybe add in a higher-level description */
return err;
}
/* for each indirect lock, find its direct lock and refresh it. */
/* ### push a higher-level desc? */
return err;
}
/* the lock was refreshed. return the lock. */
/* save the (resolved) direct lock back */
/* ### push a higher-level desc? */
return err;
}
}
}
return NULL;
}
const dav_hooks_locks dav_hooks_locks_fs =
{
NULL, /* lookup_resource */
NULL /* ctx */
};