⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lock.c

📁 最新apache的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* 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 * *     http://www.apache.org/licenses/LICENSE-2.0 * * 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. */static dav_error * dav_fs_remove_locknull_member(apr_pool_t *p,						 const char *filename,						 dav_buffer *pbuf);/*** Use the opaquelock scheme for locktokens*/struct dav_locktoken {    apr_uuid_t uuid;};#define dav_compare_locktoken(plt1, plt2) \		memcmp(&(plt1)->uuid, &(plt2)->uuid, sizeof((plt1)->uuid))/* #################################################################** ### 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;    time_t timeout;} dav_lock_discovery_fixed;typedef struct dav_lock_discovery{    struct dav_lock_discovery_fixed f;    dav_locktoken *locktoken;    const char *owner;		/* owner field from activelock */    const char *auth_user;	/* authenticated user who created the lock */    struct dav_lock_discovery *next;} dav_lock_discovery;/* 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{    dav_locktoken *locktoken;    apr_datum_t key;    struct dav_lock_indirect *next;    time_t timeout;} dav_lock_indirect;/* ################################################################# *//*** Stored direct lock info - full lock_discovery length:  ** prefix + Fixed length + lock token + 2 strings + 2 nulls (one for each string)*/#define dav_size_direct(a)	(1 + sizeof(dav_lock_discovery_fixed) \				 + sizeof(apr_uuid_t) \				 + ((a)->owner ? strlen((a)->owner) : 0) \				 + ((a)->auth_user ? strlen((a)->auth_user) : 0) \				 + 2)/* Stored indirect lock info - lock token and apr_datum_t */#define dav_size_indirect(a)	(1 + sizeof(apr_uuid_t) \				 + sizeof(time_t) \				 + sizeof((a)->key.dsize) + (a)->key.dsize)/*** 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 */    apr_pool_t *pool;			/* a pool to use */    const char *lockdb_path;		/* where is the lock database? */    int opened;				/* we opened the database */    dav_db *db;				/* if non-NULL, the lock database */};typedef struct{    dav_lockdb pub;    dav_lockdb_private priv;} dav_lockdb_combined;/*** The private part of the lock structure.*/struct dav_lock_private{    apr_datum_t key;	/* key into the lock database */};typedef struct{    dav_lock pub;    dav_lock_private priv;    dav_locktoken token;} dav_lock_combined;/*** 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 */static dav_lock *dav_fs_alloc_lock(dav_lockdb *lockdb, apr_datum_t key,				   const dav_locktoken *locktoken){    dav_lock_combined *comb;    comb = apr_pcalloc(lockdb->info->pool, sizeof(*comb));    comb->pub.rectype = DAV_LOCKREC_DIRECT;    comb->pub.info = &comb->priv;    comb->priv.key = key;    if (locktoken == NULL) {	comb->pub.locktoken = &comb->token;        apr_uuid_get(&comb->token.uuid);    }    else {	comb->pub.locktoken = locktoken;    }    return &comb->pub;}/*** 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,    dav_locktoken **locktoken_p){    dav_locktoken *locktoken;    if (ap_strstr_c(char_token, "opaquelocktoken:") != char_token) {	return dav_new_error(p,			     HTTP_BAD_REQUEST, DAV_ERR_LOCK_UNK_STATE_TOKEN,			     "The lock token uses an unknown State-token "			     "format and could not be parsed.");    }    char_token += 16;    locktoken = apr_pcalloc(p, sizeof(*locktoken));    if (apr_uuid_parse(&locktoken->uuid, char_token)) {	return dav_new_error(p, HTTP_BAD_REQUEST, DAV_ERR_LOCK_PARSE_TOKEN,			     "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){    char buf[APR_UUID_FORMATTED_LENGTH + 1];    apr_uuid_format(buf, &locktoken->uuid);    return apr_pstrcat(p, "opaquelocktoken:", buf, NULL);}/*** 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){    return dav_compare_locktoken(lt1, lt2);}/*** dav_fs_really_open_lockdb:**** If the database hasn't been opened yet, then open the thing.*/static dav_error * dav_fs_really_open_lockdb(dav_lockdb *lockdb){    dav_error *err;    if (lockdb->info->opened)	return NULL;    err = dav_dbm_open_direct(lockdb->info->pool,			      lockdb->info->lockdb_path,			      lockdb->ro,			      &lockdb->info->db);    if (err != NULL) {	return dav_push_error(lockdb->info->pool,			      HTTP_INTERNAL_SERVER_ERROR,			      DAV_ERR_LOCK_OPENDB,			      "Could not open the lock database.",			      err);    }    /* all right. it is opened now. */    lockdb->info->opened = 1;    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.**** Note that only one can be open read/write.*/static dav_error * dav_fs_open_lockdb(request_rec *r, int ro, int force,				      dav_lockdb **lockdb){    dav_lockdb_combined *comb;    comb = apr_pcalloc(r->pool, sizeof(*comb));    comb->pub.hooks = &dav_hooks_locks_fs;    comb->pub.ro = ro;    comb->pub.info = &comb->priv;    comb->priv.r = r;    comb->priv.pool = r->pool;    comb->priv.lockdb_path = dav_get_lockdb_path(r);    if (comb->priv.lockdb_path == NULL) {	return dav_new_error(r->pool, HTTP_INTERNAL_SERVER_ERROR,			     DAV_ERR_LOCK_NO_DB,			     "A lock database was not specified with the "			     "DAVLockDB directive. One must be specified "			     "to use the locking functionality.");    }    /* done initializing. return it. */    *lockdb = &comb->pub;    if (force) {	/* ### add a higher-level comment? */	return dav_fs_really_open_lockdb(*lockdb);    }    return NULL;}/*** dav_fs_close_lockdb:**** Close it. Duh.*/static void dav_fs_close_lockdb(dav_lockdb *lockdb){    if (lockdb->info->db != NULL)	dav_dbm_close(lockdb->info->db);}/*** dav_fs_build_fname_key**** Given a pathname, build a DAV_TYPE_FNAME lock database key.*/static apr_datum_t dav_fs_build_fname_key(apr_pool_t *p, const char *pathname){    apr_datum_t key;    /* ### does this allocation have a proper lifetime? need to check */    /* ### can we use a buffer for this? */    /* size is TYPE + pathname + null */    key.dsize = strlen(pathname) + 2;    key.dptr = apr_palloc(p, key.dsize);    *key.dptr = DAV_TYPE_FNAME;    memcpy(key.dptr + 1, pathname, key.dsize - 1);    if (key.dptr[key.dsize - 2] == '/')	key.dptr[--key.dsize - 1] = '\0';    return key;}/*** dav_fs_build_key:  Given a resource, return a apr_datum_t key**    to look up lock information for this file.****    (inode/dev not supported or file is lock-null):**       apr_datum_t->dvalue = full path****    (inode/dev supported and file exists ):**       apr_datum_t->dvalue = inode, dev*/static apr_datum_t dav_fs_build_key(apr_pool_t *p,                                    const dav_resource *resource){    const char *file = dav_fs_pathname(resource);    apr_datum_t key;    apr_finfo_t finfo;    apr_status_t rv;    /* ### use lstat() ?? */    /*     * XXX: What for platforms with no IDENT (dev/inode)?     */    rv = apr_stat(&finfo, file, APR_FINFO_IDENT, p);    if ((rv == APR_SUCCESS || rv == APR_INCOMPLETE)        && ((finfo.valid & APR_FINFO_IDENT) == APR_FINFO_IDENT))    {	/* ### can we use a buffer for this? */	key.dsize = 1 + sizeof(finfo.inode) + sizeof(finfo.device);	key.dptr = apr_palloc(p, key.dsize);	*key.dptr = DAV_TYPE_INODE;	memcpy(key.dptr + 1, &finfo.inode, sizeof(finfo.inode));	memcpy(key.dptr + 1 + sizeof(finfo.inode), &finfo.device,	       sizeof(finfo.device));	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).*/static int dav_fs_lock_expired(time_t expires){    return expires != DAV_TIMEOUT_INFINITE && time(NULL) >= expires;}/*** 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.*/static dav_error * dav_fs_save_lock_record(dav_lockdb *lockdb, apr_datum_t key,					   dav_lock_discovery *direct,					   dav_lock_indirect *indirect){    dav_error *err;    apr_datum_t val = { 0 };    char *ptr;    dav_lock_discovery *dp = direct;    dav_lock_indirect *ip = indirect;#if DAV_DEBUG    if (lockdb->ro) {	return dav_new_error(lockdb->info->pool,			     HTTP_INTERNAL_SERVER_ERROR, 0,			     "INTERNAL DESIGN ERROR: the lockdb was opened "			     "readonly, but an attempt to save locks was "			     "performed.");    }#endif    if ((err = dav_fs_really_open_lockdb(lockdb)) != NULL) {	/* ### add a higher-level error? */	return err;    }    /* If nothing to save, delete key */    if (dp == NULL && ip == NULL) {        /* don't fail if the key is not present */        /* ### but what about other errors? */	(void) dav_dbm_delete(lockdb->info->db, key);        return NULL;    }		    while(dp) {	val.dsize += dav_size_direct(dp);	dp = dp->next;    }    while(ip) {	val.dsize += dav_size_indirect(ip);	ip = ip->next;    }    /* ### can this be apr_palloc() ? */    /* ### hmmm.... investigate the use of a buffer here */    ptr = val.dptr = apr_pcalloc(lockdb->info->pool, val.dsize);    dp  = direct;    ip  = indirect;    while(dp) {	*ptr++ = DAV_LOCK_DIRECT;	/* Direct lock - lock_discovery struct follows */	memcpy(ptr, dp, sizeof(dp->f));	/* Fixed portion of struct */	ptr += sizeof(dp->f);        memcpy(ptr, dp->locktoken, sizeof(*dp->locktoken));        ptr += sizeof(*dp->locktoken);	if (dp->owner == NULL) {	    *ptr++ = '\0';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -