1/* lock-tokens-table.c : operations on the `lock-tokens' table
2 *
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
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
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
19 *    under the License.
20 * ====================================================================
21 */
22
23#include <string.h>
24#include <assert.h>
25
26#include "bdb_compat.h"
27
28#include "svn_pools.h"
29#include "private/svn_skel.h"
30
31#include "dbt.h"
32#include "../err.h"
33#include "../fs.h"
34#include "../util/fs_skels.h"
35#include "../trail.h"
36#include "../../libsvn_fs/fs-loader.h"
37#include "bdb-err.h"
38#include "lock-tokens-table.h"
39#include "locks-table.h"
40
41#include "private/svn_fs_util.h"
42
43
44int
45svn_fs_bdb__open_lock_tokens_table(DB **lock_tokens_p,
46                                   DB_ENV *env,
47                                   svn_boolean_t create)
48{
49  const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
50  DB *lock_tokens;
51  int error;
52
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,
57                              open_flags, 0666);
58
59  /* Create the table if it doesn't yet exist.  This is a form of
60     automagical repository upgrading. */
61  if (error == ENOENT && (! create))
62    {
63      BDB_ERR(lock_tokens->close(lock_tokens, 0));
64      return svn_fs_bdb__open_lock_tokens_table(lock_tokens_p, env, TRUE);
65    }
66  BDB_ERR(error);
67
68  *lock_tokens_p = lock_tokens;
69  return 0;
70}
71
72
73svn_error_t *
74svn_fs_bdb__lock_token_add(svn_fs_t *fs,
75                           const char *path,
76                           const char *lock_token,
77                           trail_t *trail,
78                           apr_pool_t *pool)
79{
80
81  base_fs_data_t *bfd = fs->fsap_data;
82  DBT key, value;
83
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,
89                                        &key, &value, 0));
90}
91
92
93svn_error_t *
94svn_fs_bdb__lock_token_delete(svn_fs_t *fs,
95                              const char *path,
96                              trail_t *trail,
97                              apr_pool_t *pool)
98{
99  base_fs_data_t *bfd = fs->fsap_data;
100  DBT key;
101  int db_err;
102
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);
109}
110
111
112svn_error_t *
113svn_fs_bdb__lock_token_get(const char **lock_token_p,
114                           svn_fs_t *fs,
115                           const char *path,
116                           trail_t *trail,
117                           apr_pool_t *pool)
118{
119  base_fs_data_t *bfd = fs->fsap_data;
120  DBT key, value;
121  svn_error_t *err;
122  svn_lock_t *lock;
123  const char *lock_token;
124  int db_err;
125
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),
130                                 0);
131  svn_fs_base__track_dbt(&value, pool);
132
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));
136
137  lock_token = apr_pstrmemdup(pool, value.data, value.size);
138
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)))
144    {
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);
148      if (delete_err)
149        svn_error_compose(err, delete_err);
150      return svn_error_trace(err);
151    }
152  else if (err)
153    return svn_error_trace(err);
154
155  *lock_token_p = lock_token;
156  return SVN_NO_ERROR;
157}
158