1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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.
17
#include "apr_file_info.h"
18
#include "apr_file_io.h"
21
#include "sdbm_private.h"
22
#include "sdbm_tune.h"
24
/* NOTE: this function blocks until it acquires the lock */
25
APU_DECLARE(apr_status_t) apr_sdbm_lock(apr_sdbm_t *db, int type)
29
if (!(type == APR_FLOCK_SHARED || type == APR_FLOCK_EXCLUSIVE))
32
if (db->flags & SDBM_EXCLUSIVE_LOCK) {
36
else if (db->flags & SDBM_SHARED_LOCK) {
38
* Cannot promote a shared lock to an exlusive lock
39
* in a cross-platform compatibile manner.
41
if (type == APR_FLOCK_EXCLUSIVE)
47
* zero size: either a fresh database, or one with a single,
48
* unsplit data page: dirpage is all zeros.
50
if ((status = apr_file_lock(db->dirf, type)) == APR_SUCCESS)
53
if ((status = apr_file_info_get(&finfo, APR_FINFO_SIZE, db->dirf))
55
(void) apr_file_unlock(db->dirf);
59
SDBM_INVALIDATE_CACHE(db, finfo);
62
if (type == APR_FLOCK_SHARED)
63
db->flags |= SDBM_SHARED_LOCK;
64
else if (type == APR_FLOCK_EXCLUSIVE)
65
db->flags |= SDBM_EXCLUSIVE_LOCK;
70
APU_DECLARE(apr_status_t) apr_sdbm_unlock(apr_sdbm_t *db)
72
if (!(db->flags & (SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK)))
76
db->flags &= ~(SDBM_SHARED_LOCK | SDBM_EXCLUSIVE_LOCK);
77
return apr_file_unlock(db->dirf);