1 /* lock-tokens-table.c : operations on the `lock-tokens' table
3 * ====================================================================
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
20 * ====================================================================
26 #include "bdb_compat.h"
28 #include "svn_pools.h"
29 #include "private/svn_skel.h"
34 #include "../util/fs_skels.h"
36 #include "../../libsvn_fs/fs-loader.h"
38 #include "lock-tokens-table.h"
39 #include "locks-table.h"
41 #include "private/svn_fs_util.h"
45 svn_fs_bdb__open_lock_tokens_table(DB **lock_tokens_p,
49 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
53 BDB_ERR(svn_fs_bdb__check_version());
54 BDB_ERR(db_create(&lock_tokens, env, 0));
55 error = (lock_tokens->open)(SVN_BDB_OPEN_PARAMS(lock_tokens, NULL),
56 "lock-tokens", 0, DB_BTREE,
59 /* Create the table if it doesn't yet exist. This is a form of
60 automagical repository upgrading. */
61 if (error == ENOENT && (! create))
63 BDB_ERR(lock_tokens->close(lock_tokens, 0));
64 return svn_fs_bdb__open_lock_tokens_table(lock_tokens_p, env, TRUE);
68 *lock_tokens_p = lock_tokens;
74 svn_fs_bdb__lock_token_add(svn_fs_t *fs,
76 const char *lock_token,
81 base_fs_data_t *bfd = fs->fsap_data;
84 svn_fs_base__str_to_dbt(&key, path);
85 svn_fs_base__str_to_dbt(&value, lock_token);
86 svn_fs_base__trail_debug(trail, "lock-tokens", "add");
87 return BDB_WRAP(fs, N_("storing lock token record"),
88 bfd->lock_tokens->put(bfd->lock_tokens, trail->db_txn,
94 svn_fs_bdb__lock_token_delete(svn_fs_t *fs,
99 base_fs_data_t *bfd = fs->fsap_data;
103 svn_fs_base__str_to_dbt(&key, path);
104 svn_fs_base__trail_debug(trail, "lock-tokens", "del");
105 db_err = bfd->lock_tokens->del(bfd->lock_tokens, trail->db_txn, &key, 0);
106 if (db_err == DB_NOTFOUND)
107 return SVN_FS__ERR_NO_SUCH_LOCK(fs, path);
108 return BDB_WRAP(fs, N_("deleting entry from 'lock-tokens' table"), db_err);
113 svn_fs_bdb__lock_token_get(const char **lock_token_p,
119 base_fs_data_t *bfd = fs->fsap_data;
123 const char *lock_token;
126 svn_fs_base__trail_debug(trail, "lock-tokens", "get");
127 db_err = bfd->lock_tokens->get(bfd->lock_tokens, trail->db_txn,
128 svn_fs_base__str_to_dbt(&key, path),
129 svn_fs_base__result_dbt(&value),
131 svn_fs_base__track_dbt(&value, pool);
133 if (db_err == DB_NOTFOUND)
134 return SVN_FS__ERR_NO_SUCH_LOCK(fs, path);
135 SVN_ERR(BDB_WRAP(fs, N_("reading lock token"), db_err));
137 lock_token = apr_pstrmemdup(pool, value.data, value.size);
139 /* Make sure the token still points to an existing, non-expired
140 lock, by doing a lookup in the `locks' table. */
141 err = svn_fs_bdb__lock_get(&lock, fs, lock_token, trail, pool);
142 if (err && ((err->apr_err == SVN_ERR_FS_LOCK_EXPIRED)
143 || (err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN)))
145 /* If `locks' doesn't have the lock, then we should lose it too. */
146 svn_error_t *delete_err;
147 delete_err = svn_fs_bdb__lock_token_delete(fs, path, trail, pool);
149 svn_error_compose(err, delete_err);
150 return svn_error_trace(err);
153 return svn_error_trace(err);
155 *lock_token_p = lock_token;