1 /* rev-table.c : working with the `revisions' 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 * ====================================================================
23 #include "bdb_compat.h"
26 #include "private/svn_skel.h"
30 #include "../util/fs_skels.h"
31 #include "../../libsvn_fs/fs-loader.h"
34 #include "rev-table.h"
36 #include "svn_private_config.h"
37 #include "private/svn_fs_util.h"
40 /* Opening/creating the `revisions' table. */
42 int svn_fs_bdb__open_revisions_table(DB **revisions_p,
46 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
49 BDB_ERR(svn_fs_bdb__check_version());
50 BDB_ERR(db_create(&revisions, env, 0));
51 BDB_ERR((revisions->open)(SVN_BDB_OPEN_PARAMS(revisions, NULL),
52 "revisions", 0, DB_RECNO,
55 *revisions_p = revisions;
61 /* Storing and retrieving filesystem revisions. */
65 svn_fs_bdb__get_rev(revision_t **revision_p,
71 base_fs_data_t *bfd = fs->fsap_data;
77 /* Turn the revision number into a Berkeley DB record number.
78 Revisions are numbered starting with zero; Berkeley DB record
79 numbers begin with one. */
80 db_recno_t recno = (db_recno_t) rev + 1;
82 if (!SVN_IS_VALID_REVNUM(rev))
83 return svn_fs_base__err_dangling_rev(fs, rev);
85 svn_fs_base__trail_debug(trail, "revisions", "get");
86 db_err = bfd->revisions->get(bfd->revisions, trail->db_txn,
87 svn_fs_base__set_dbt(&key, &recno,
89 svn_fs_base__result_dbt(&value),
91 svn_fs_base__track_dbt(&value, pool);
93 /* If there's no such revision, return an appropriately specific error. */
94 if (db_err == DB_NOTFOUND)
95 return svn_fs_base__err_dangling_rev(fs, rev);
97 /* Handle any other error conditions. */
98 SVN_ERR(BDB_WRAP(fs, N_("reading filesystem revision"), db_err));
100 /* Parse REVISION skel. */
101 skel = svn_skel__parse(value.data, value.size, pool);
103 return svn_fs_base__err_corrupt_fs_revision(fs, rev);
105 /* Convert skel to native type. */
106 SVN_ERR(svn_fs_base__parse_revision_skel(&revision, skel, pool));
108 *revision_p = revision;
113 /* Write REVISION to FS as part of TRAIL. If *REV is a valid revision
114 number, write this revision as one that corresponds to *REV, else
115 write a new revision and return its newly created revision number
118 svn_fs_bdb__put_rev(svn_revnum_t *rev,
120 const revision_t *revision,
124 base_fs_data_t *bfd = fs->fsap_data;
126 db_recno_t recno = 0;
130 /* Convert native type to skel. */
131 SVN_ERR(svn_fs_base__unparse_revision_skel(&skel, revision, pool));
133 if (SVN_IS_VALID_REVNUM(*rev))
137 /* Update the filesystem revision with the new skel. */
138 recno = (db_recno_t) *rev + 1;
139 svn_fs_base__trail_debug(trail, "revisions", "put");
140 db_err = bfd->revisions->put
141 (bfd->revisions, trail->db_txn,
142 svn_fs_base__set_dbt(&query, &recno, sizeof(recno)),
143 svn_fs_base__skel_to_dbt(&result, skel, pool), 0);
144 return BDB_WRAP(fs, N_("updating filesystem revision"), db_err);
147 svn_fs_base__trail_debug(trail, "revisions", "put");
148 db_err = bfd->revisions->put(bfd->revisions, trail->db_txn,
149 svn_fs_base__recno_dbt(&key, &recno),
150 svn_fs_base__skel_to_dbt(&value, skel, pool),
152 SVN_ERR(BDB_WRAP(fs, N_("storing filesystem revision"), db_err));
154 /* Turn the record number into a Subversion revision number.
155 Revisions are numbered starting with zero; Berkeley DB record
156 numbers begin with one. */
163 /* Getting the youngest revision. */
167 svn_fs_bdb__youngest_rev(svn_revnum_t *youngest_p,
172 base_fs_data_t *bfd = fs->fsap_data;
178 SVN_ERR(svn_fs__check_fs(fs, TRUE));
180 /* Create a database cursor. */
181 svn_fs_base__trail_debug(trail, "revisions", "cursor");
182 SVN_ERR(BDB_WRAP(fs, N_("getting youngest revision (creating cursor)"),
183 bfd->revisions->cursor(bfd->revisions, trail->db_txn,
186 /* Find the last entry in the `revisions' table. */
187 db_err = svn_bdb_dbc_get(cursor,
188 svn_fs_base__recno_dbt(&key, &recno),
189 svn_fs_base__nodata_dbt(&value),
194 /* Free the cursor. Ignore any error value --- the error above
195 is more interesting. */
196 svn_bdb_dbc_close(cursor);
198 if (db_err == DB_NOTFOUND)
199 /* The revision 0 should always be present, at least. */
202 (SVN_ERR_FS_CORRUPT, 0,
203 "Corrupt DB: revision 0 missing from 'revisions' table, in "
204 "filesystem '%s'", fs->path);
206 SVN_ERR(BDB_WRAP(fs, N_("getting youngest revision (finding last entry)"),
210 /* You can't commit a transaction with open cursors, because:
211 1) key/value pairs don't get deleted until the cursors referring
212 to them are closed, so closing a cursor can fail for various
213 reasons, and txn_commit shouldn't fail that way, and
214 2) using a cursor after committing its transaction can cause
215 undetectable database corruption. */
216 SVN_ERR(BDB_WRAP(fs, N_("getting youngest revision (closing cursor)"),
217 svn_bdb_dbc_close(cursor)));
219 /* Turn the record number into a Subversion revision number.
220 Revisions are numbered starting with zero; Berkeley DB record
221 numbers begin with one. */
222 *youngest_p = recno - 1;