1 /* locks-table.c : operations on the `locks' 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"
30 #include "private/svn_skel.h"
35 #include "../util/fs_skels.h"
37 #include "../../libsvn_fs/fs-loader.h"
39 #include "locks-table.h"
40 #include "lock-tokens-table.h"
42 #include "private/svn_fs_util.h"
43 #include "private/svn_fspath.h"
47 svn_fs_bdb__open_locks_table(DB **locks_p,
51 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
55 BDB_ERR(svn_fs_bdb__check_version());
56 BDB_ERR(db_create(&locks, env, 0));
57 error = (locks->open)(SVN_BDB_OPEN_PARAMS(locks, NULL),
61 /* Create the table if it doesn't yet exist. This is a form of
62 automagical repository upgrading. */
63 if (error == ENOENT && (! create))
65 BDB_ERR(locks->close(locks, 0));
66 return svn_fs_bdb__open_locks_table(locks_p, env, TRUE);
77 svn_fs_bdb__lock_add(svn_fs_t *fs,
78 const char *lock_token,
83 base_fs_data_t *bfd = fs->fsap_data;
84 svn_skel_t *lock_skel;
87 /* Convert native type to skel. */
88 SVN_ERR(svn_fs_base__unparse_lock_skel(&lock_skel, lock, pool));
90 svn_fs_base__str_to_dbt(&key, lock_token);
91 svn_fs_base__skel_to_dbt(&value, lock_skel, pool);
92 svn_fs_base__trail_debug(trail, "lock", "add");
93 return BDB_WRAP(fs, N_("storing lock record"),
94 bfd->locks->put(bfd->locks, trail->db_txn,
101 svn_fs_bdb__lock_delete(svn_fs_t *fs,
102 const char *lock_token,
106 base_fs_data_t *bfd = fs->fsap_data;
110 svn_fs_base__str_to_dbt(&key, lock_token);
111 svn_fs_base__trail_debug(trail, "locks", "del");
112 db_err = bfd->locks->del(bfd->locks, trail->db_txn, &key, 0);
114 if (db_err == DB_NOTFOUND)
115 return svn_fs_base__err_bad_lock_token(fs, lock_token);
116 return BDB_WRAP(fs, N_("deleting lock from 'locks' table"), db_err);
122 svn_fs_bdb__lock_get(svn_lock_t **lock_p,
124 const char *lock_token,
128 base_fs_data_t *bfd = fs->fsap_data;
134 svn_fs_base__trail_debug(trail, "lock", "get");
135 db_err = bfd->locks->get(bfd->locks, trail->db_txn,
136 svn_fs_base__str_to_dbt(&key, lock_token),
137 svn_fs_base__result_dbt(&value),
139 svn_fs_base__track_dbt(&value, pool);
141 if (db_err == DB_NOTFOUND)
142 return svn_fs_base__err_bad_lock_token(fs, lock_token);
143 SVN_ERR(BDB_WRAP(fs, N_("reading lock"), db_err));
145 /* Parse TRANSACTION skel */
146 skel = svn_skel__parse(value.data, value.size, pool);
148 return svn_fs_base__err_corrupt_lock(fs, lock_token);
150 /* Convert skel to native type. */
151 SVN_ERR(svn_fs_base__parse_lock_skel(&lock, skel, pool));
153 /* Possibly auto-expire the lock. */
154 if (lock->expiration_date && (apr_time_now() > lock->expiration_date))
156 SVN_ERR(svn_fs_bdb__lock_delete(fs, lock_token, trail, pool));
157 return SVN_FS__ERR_LOCK_EXPIRED(fs, lock_token);
166 get_lock(svn_lock_t **lock_p,
169 const char *lock_token,
173 svn_error_t *err = SVN_NO_ERROR;
176 /* Make sure the token points to an existing, non-expired lock, by
177 doing a lookup in the `locks' table. Use 'pool'. */
178 err = svn_fs_bdb__lock_get(lock_p, fs, lock_token, trail, pool);
179 if (err && ((err->apr_err == SVN_ERR_FS_LOCK_EXPIRED)
180 || (err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN)))
182 svn_error_clear(err);
184 /* If `locks' doesn't have the lock, then we should lose it
185 from `lock-tokens' table as well, then skip to the next
186 matching path-key. */
187 err = svn_fs_bdb__lock_token_delete(fs, path, trail, pool);
189 return svn_error_trace(err);
194 svn_fs_bdb__locks_get(svn_fs_t *fs,
197 svn_fs_get_locks_callback_t get_locks_func,
198 void *get_locks_baton,
202 base_fs_data_t *bfd = fs->fsap_data;
205 int db_err, db_c_err;
206 apr_pool_t *subpool = svn_pool_create(pool);
207 const char *lock_token;
210 const char *lookup_path = path;
211 apr_size_t lookup_len;
213 /* First, try to lookup PATH itself. */
214 err = svn_fs_bdb__lock_token_get(&lock_token, fs, path, trail, pool);
215 if (err && ((err->apr_err == SVN_ERR_FS_LOCK_EXPIRED)
216 || (err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN)
217 || (err->apr_err == SVN_ERR_FS_NO_SUCH_LOCK)))
219 svn_error_clear(err);
223 return svn_error_trace(err);
227 SVN_ERR(get_lock(&lock, fs, path, lock_token, trail, pool));
228 if (lock && get_locks_func)
230 SVN_ERR(get_locks_func(get_locks_baton, lock, pool));
232 /* Found a lock so PATH is a file and we can ignore depth */
237 /* If we're only looking at PATH itself (depth = empty), stop here. */
238 if (depth == svn_depth_empty)
241 /* Now go hunt for possible children of PATH. */
243 svn_fs_base__trail_debug(trail, "lock-tokens", "cursor");
244 db_err = bfd->lock_tokens->cursor(bfd->lock_tokens, trail->db_txn,
246 SVN_ERR(BDB_WRAP(fs, N_("creating cursor for reading lock tokens"),
249 /* Since the key is going to be returned as well as the value make
250 sure BDB malloc's the returned key. */
251 svn_fs_base__str_to_dbt(&key, lookup_path);
252 key.flags |= DB_DBT_MALLOC;
254 /* Get the first matching key that is either equal or greater than
255 the one passed in, by passing in the DB_RANGE_SET flag. */
256 db_err = svn_bdb_dbc_get(cursor, &key, svn_fs_base__result_dbt(&value),
259 if (!svn_fspath__is_root(path, strlen(path)))
260 lookup_path = apr_pstrcat(pool, path, "/", (char *)NULL);
261 lookup_len = strlen(lookup_path);
263 /* As long as the prefix of the returned KEY matches LOOKUP_PATH we
264 know it is either LOOKUP_PATH or a decendant thereof. */
266 && lookup_len < key.size
267 && strncmp(lookup_path, key.data, lookup_len) == 0)
269 const char *child_path;
271 svn_pool_clear(subpool);
273 svn_fs_base__track_dbt(&key, subpool);
274 svn_fs_base__track_dbt(&value, subpool);
276 /* Create a usable path and token in temporary memory. */
277 child_path = apr_pstrmemdup(subpool, key.data, key.size);
278 lock_token = apr_pstrmemdup(subpool, value.data, value.size);
280 if ((depth == svn_depth_files) || (depth == svn_depth_immediates))
282 /* On the assumption that we only store locks for files,
283 depth=files and depth=immediates should boil down to the
284 same set of results. So just see if CHILD_PATH is an
285 immediate child of PATH. If not, we don't care about
287 const char *rel_path = svn_fspath__skip_ancestor(path, child_path);
288 if (!rel_path || (svn_path_component_count(rel_path) != 1))
292 /* Get the lock for CHILD_PATH. */
293 err = get_lock(&lock, fs, child_path, lock_token, trail, subpool);
296 svn_bdb_dbc_close(cursor);
297 return svn_error_trace(err);
300 /* Lock is verified, hand it off to our callback. */
301 if (lock && get_locks_func)
303 err = get_locks_func(get_locks_baton, lock, subpool);
306 svn_bdb_dbc_close(cursor);
307 return svn_error_trace(err);
312 svn_fs_base__result_dbt(&key);
313 svn_fs_base__result_dbt(&value);
314 db_err = svn_bdb_dbc_get(cursor, &key, &value, DB_NEXT);
317 svn_pool_destroy(subpool);
318 db_c_err = svn_bdb_dbc_close(cursor);
320 if (db_err && (db_err != DB_NOTFOUND))
321 SVN_ERR(BDB_WRAP(fs, N_("fetching lock tokens"), db_err));
323 SVN_ERR(BDB_WRAP(fs, N_("fetching lock tokens (closing cursor)"),