1 | /* locks-table.c : operations on the `locks' table |
---|
2 | * |
---|
3 | * ==================================================================== |
---|
4 | * Copyright (c) 2000-2004 CollabNet. All rights reserved. |
---|
5 | * |
---|
6 | * This software is licensed as described in the file COPYING, which |
---|
7 | * you should have received as part of this distribution. The terms |
---|
8 | * are also available at http://subversion.tigris.org/license-1.html. |
---|
9 | * If newer versions of this license are posted there, you may use a |
---|
10 | * newer version instead, at your option. |
---|
11 | * |
---|
12 | * This software consists of voluntary contributions made by many |
---|
13 | * individuals. For exact contribution history, see the revision |
---|
14 | * history and logs, available at http://subversion.tigris.org/. |
---|
15 | * ==================================================================== |
---|
16 | */ |
---|
17 | |
---|
18 | #include <string.h> |
---|
19 | #include <assert.h> |
---|
20 | |
---|
21 | #include "bdb_compat.h" |
---|
22 | |
---|
23 | #include "svn_pools.h" |
---|
24 | #include "private/svn_skel.h" |
---|
25 | |
---|
26 | #include "dbt.h" |
---|
27 | #include "../err.h" |
---|
28 | #include "../fs.h" |
---|
29 | #include "../util/fs_skels.h" |
---|
30 | #include "../trail.h" |
---|
31 | #include "../../libsvn_fs/fs-loader.h" |
---|
32 | #include "bdb-err.h" |
---|
33 | #include "locks-table.h" |
---|
34 | #include "lock-tokens-table.h" |
---|
35 | |
---|
36 | #include "private/svn_fs_util.h" |
---|
37 | |
---|
38 | |
---|
39 | int |
---|
40 | svn_fs_bdb__open_locks_table(DB **locks_p, |
---|
41 | DB_ENV *env, |
---|
42 | svn_boolean_t create) |
---|
43 | { |
---|
44 | const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0); |
---|
45 | DB *locks; |
---|
46 | int error; |
---|
47 | |
---|
48 | BDB_ERR(svn_fs_bdb__check_version()); |
---|
49 | BDB_ERR(db_create(&locks, env, 0)); |
---|
50 | error = (locks->open)(SVN_BDB_OPEN_PARAMS(locks, NULL), |
---|
51 | "locks", 0, DB_BTREE, |
---|
52 | open_flags, 0666); |
---|
53 | |
---|
54 | /* Create the table if it doesn't yet exist. This is a form of |
---|
55 | automagical repository upgrading. */ |
---|
56 | if (error == ENOENT && (! create)) |
---|
57 | { |
---|
58 | BDB_ERR(locks->close(locks, 0)); |
---|
59 | return svn_fs_bdb__open_locks_table(locks_p, env, TRUE); |
---|
60 | } |
---|
61 | BDB_ERR(error); |
---|
62 | |
---|
63 | *locks_p = locks; |
---|
64 | return 0; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | svn_error_t * |
---|
70 | svn_fs_bdb__lock_add(svn_fs_t *fs, |
---|
71 | const char *lock_token, |
---|
72 | svn_lock_t *lock, |
---|
73 | trail_t *trail, |
---|
74 | apr_pool_t *pool) |
---|
75 | { |
---|
76 | base_fs_data_t *bfd = fs->fsap_data; |
---|
77 | svn_skel_t *lock_skel; |
---|
78 | DBT key, value; |
---|
79 | |
---|
80 | /* Convert native type to skel. */ |
---|
81 | SVN_ERR(svn_fs_base__unparse_lock_skel(&lock_skel, lock, pool)); |
---|
82 | |
---|
83 | svn_fs_base__str_to_dbt(&key, lock_token); |
---|
84 | svn_fs_base__skel_to_dbt(&value, lock_skel, pool); |
---|
85 | svn_fs_base__trail_debug(trail, "lock", "add"); |
---|
86 | return BDB_WRAP(fs, "storing lock record", |
---|
87 | bfd->locks->put(bfd->locks, trail->db_txn, |
---|
88 | &key, &value, 0)); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | svn_error_t * |
---|
94 | svn_fs_bdb__lock_delete(svn_fs_t *fs, |
---|
95 | const char *lock_token, |
---|
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, lock_token); |
---|
104 | svn_fs_base__trail_debug(trail, "locks", "del"); |
---|
105 | db_err = bfd->locks->del(bfd->locks, trail->db_txn, &key, 0); |
---|
106 | |
---|
107 | if (db_err == DB_NOTFOUND) |
---|
108 | return svn_fs_base__err_bad_lock_token(fs, lock_token); |
---|
109 | return BDB_WRAP(fs, "deleting lock from 'locks' table", db_err); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | svn_error_t * |
---|
115 | svn_fs_bdb__lock_get(svn_lock_t **lock_p, |
---|
116 | svn_fs_t *fs, |
---|
117 | const char *lock_token, |
---|
118 | trail_t *trail, |
---|
119 | apr_pool_t *pool) |
---|
120 | { |
---|
121 | base_fs_data_t *bfd = fs->fsap_data; |
---|
122 | DBT key, value; |
---|
123 | int db_err; |
---|
124 | svn_skel_t *skel; |
---|
125 | svn_lock_t *lock; |
---|
126 | |
---|
127 | svn_fs_base__trail_debug(trail, "lock", "get"); |
---|
128 | db_err = bfd->locks->get(bfd->locks, trail->db_txn, |
---|
129 | svn_fs_base__str_to_dbt(&key, lock_token), |
---|
130 | svn_fs_base__result_dbt(&value), |
---|
131 | 0); |
---|
132 | svn_fs_base__track_dbt(&value, pool); |
---|
133 | |
---|
134 | if (db_err == DB_NOTFOUND) |
---|
135 | return svn_fs_base__err_bad_lock_token(fs, lock_token); |
---|
136 | SVN_ERR(BDB_WRAP(fs, "reading lock", db_err)); |
---|
137 | |
---|
138 | /* Parse TRANSACTION skel */ |
---|
139 | skel = svn_skel__parse(value.data, value.size, pool); |
---|
140 | if (! skel) |
---|
141 | return svn_fs_base__err_corrupt_lock(fs, lock_token); |
---|
142 | |
---|
143 | /* Convert skel to native type. */ |
---|
144 | SVN_ERR(svn_fs_base__parse_lock_skel(&lock, skel, pool)); |
---|
145 | |
---|
146 | /* Possibly auto-expire the lock. */ |
---|
147 | if (lock->expiration_date && (apr_time_now() > lock->expiration_date)) |
---|
148 | { |
---|
149 | SVN_ERR(svn_fs_bdb__lock_delete(fs, lock_token, trail, pool)); |
---|
150 | return SVN_FS__ERR_LOCK_EXPIRED(fs, lock_token); |
---|
151 | } |
---|
152 | |
---|
153 | *lock_p = lock; |
---|
154 | return SVN_NO_ERROR; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | static svn_error_t * |
---|
159 | get_lock(svn_lock_t **lock_p, |
---|
160 | svn_fs_t *fs, |
---|
161 | const char *path, |
---|
162 | const char *lock_token, |
---|
163 | trail_t *trail, |
---|
164 | apr_pool_t *pool) |
---|
165 | { |
---|
166 | svn_error_t *err = SVN_NO_ERROR; |
---|
167 | *lock_p = NULL; |
---|
168 | |
---|
169 | /* Make sure the token points to an existing, non-expired lock, by |
---|
170 | doing a lookup in the `locks' table. Use 'pool'. */ |
---|
171 | err = svn_fs_bdb__lock_get(lock_p, fs, lock_token, trail, pool); |
---|
172 | if (err && ((err->apr_err == SVN_ERR_FS_LOCK_EXPIRED) |
---|
173 | || (err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN))) |
---|
174 | { |
---|
175 | svn_error_clear(err); |
---|
176 | |
---|
177 | /* If `locks' doesn't have the lock, then we should lose it |
---|
178 | from `lock-tokens' table as well, then skip to the next |
---|
179 | matching path-key. */ |
---|
180 | err = svn_fs_bdb__lock_token_delete(fs, path, trail, pool); |
---|
181 | } |
---|
182 | return err; |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | svn_error_t * |
---|
187 | svn_fs_bdb__locks_get(svn_fs_t *fs, |
---|
188 | const char *path, |
---|
189 | svn_fs_get_locks_callback_t get_locks_func, |
---|
190 | void *get_locks_baton, |
---|
191 | trail_t *trail, |
---|
192 | apr_pool_t *pool) |
---|
193 | { |
---|
194 | base_fs_data_t *bfd = fs->fsap_data; |
---|
195 | DBC *cursor; |
---|
196 | DBT key, value; |
---|
197 | int db_err, db_c_err; |
---|
198 | apr_pool_t *subpool = svn_pool_create(pool); |
---|
199 | const char *lock_token; |
---|
200 | svn_lock_t *lock; |
---|
201 | svn_error_t *err; |
---|
202 | const char *lookup_path = path; |
---|
203 | |
---|
204 | /* First, try to lookup PATH itself. */ |
---|
205 | err = svn_fs_bdb__lock_token_get(&lock_token, fs, path, trail, pool); |
---|
206 | if (err && ((err->apr_err == SVN_ERR_FS_LOCK_EXPIRED) |
---|
207 | || (err->apr_err == SVN_ERR_FS_BAD_LOCK_TOKEN) |
---|
208 | || (err->apr_err == SVN_ERR_FS_NO_SUCH_LOCK))) |
---|
209 | { |
---|
210 | svn_error_clear(err); |
---|
211 | } |
---|
212 | else if (err) |
---|
213 | { |
---|
214 | return err; |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | SVN_ERR(get_lock(&lock, fs, path, lock_token, trail, pool)); |
---|
219 | if (lock && get_locks_func) |
---|
220 | SVN_ERR(get_locks_func(get_locks_baton, lock, pool)); |
---|
221 | } |
---|
222 | |
---|
223 | /* Now go hunt for possible children of PATH. */ |
---|
224 | if (strcmp(path, "/") != 0) |
---|
225 | lookup_path = apr_pstrcat(pool, path, "/", NULL); |
---|
226 | |
---|
227 | svn_fs_base__trail_debug(trail, "lock-tokens", "cursor"); |
---|
228 | db_err = bfd->lock_tokens->cursor(bfd->lock_tokens, trail->db_txn, |
---|
229 | &cursor, 0); |
---|
230 | SVN_ERR(BDB_WRAP(fs, "creating cursor for reading lock tokens", db_err)); |
---|
231 | |
---|
232 | /* Since the key is going to be returned as well as the value make |
---|
233 | sure BDB malloc's the returned key. */ |
---|
234 | svn_fs_base__str_to_dbt(&key, lookup_path); |
---|
235 | key.flags |= DB_DBT_MALLOC; |
---|
236 | |
---|
237 | /* Get the first matching key that is either equal or greater than |
---|
238 | the one passed in, by passing in the DB_RANGE_SET flag. */ |
---|
239 | db_err = svn_bdb_dbc_get(cursor, &key, svn_fs_base__result_dbt(&value), |
---|
240 | DB_SET_RANGE); |
---|
241 | |
---|
242 | /* As long as the prefix of the returned KEY matches LOOKUP_PATH we |
---|
243 | know it is either LOOKUP_PATH or a decendant thereof. */ |
---|
244 | while ((! db_err) |
---|
245 | && strncmp(lookup_path, key.data, strlen(lookup_path)) == 0) |
---|
246 | { |
---|
247 | const char *child_path; |
---|
248 | |
---|
249 | svn_pool_clear(subpool); |
---|
250 | |
---|
251 | svn_fs_base__track_dbt(&key, subpool); |
---|
252 | svn_fs_base__track_dbt(&value, subpool); |
---|
253 | |
---|
254 | /* Create a usable path and token in temporary memory. */ |
---|
255 | child_path = apr_pstrmemdup(subpool, key.data, key.size); |
---|
256 | lock_token = apr_pstrmemdup(subpool, value.data, value.size); |
---|
257 | |
---|
258 | /* Get the lock for CHILD_PATH. */ |
---|
259 | err = get_lock(&lock, fs, child_path, lock_token, trail, subpool); |
---|
260 | if (err) |
---|
261 | { |
---|
262 | svn_bdb_dbc_close(cursor); |
---|
263 | return err; |
---|
264 | } |
---|
265 | |
---|
266 | /* Lock is verified, hand it off to our callback. */ |
---|
267 | if (lock && get_locks_func) |
---|
268 | { |
---|
269 | err = get_locks_func(get_locks_baton, lock, subpool); |
---|
270 | if (err) |
---|
271 | { |
---|
272 | svn_bdb_dbc_close(cursor); |
---|
273 | return err; |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | svn_fs_base__result_dbt(&key); |
---|
278 | svn_fs_base__result_dbt(&value); |
---|
279 | db_err = svn_bdb_dbc_get(cursor, &key, &value, DB_NEXT); |
---|
280 | } |
---|
281 | |
---|
282 | svn_pool_destroy(subpool); |
---|
283 | db_c_err = svn_bdb_dbc_close(cursor); |
---|
284 | |
---|
285 | if (db_err && (db_err != DB_NOTFOUND)) |
---|
286 | SVN_ERR(BDB_WRAP(fs, "fetching lock tokens", db_err)); |
---|
287 | if (db_c_err) |
---|
288 | SVN_ERR(BDB_WRAP(fs, "fetching lock tokens (closing cursor)", db_c_err)); |
---|
289 | |
---|
290 | return SVN_NO_ERROR; |
---|
291 | } |
---|
292 | |
---|