1    /* rev-table.c : working with the `revisions' 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 "bdb_compat.h"
24
25#include "svn_fs.h"
26#include "private/svn_skel.h"
27
28#include "../fs.h"
29#include "../err.h"
30#include "../util/fs_skels.h"
31#include "../../libsvn_fs/fs-loader.h"
32#include "bdb-err.h"
33#include "dbt.h"
34#include "rev-table.h"
35
36#include "svn_private_config.h"
37#include "private/svn_fs_util.h"
38
39
40/* Opening/creating the `revisions' table.  */
41
42int svn_fs_bdb__open_revisions_table(DB **revisions_p,
43                                     DB_ENV *env,
44                                     svn_boolean_t create)
45{
46  const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
47  DB *revisions;
48
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,
53                            open_flags, 0666));
54
55  *revisions_p = revisions;
56  return 0;
57}
58
59
60
61/* Storing and retrieving filesystem revisions.  */
62
63
64svn_error_t *
65svn_fs_bdb__get_rev(revision_t **revision_p,
66                    svn_fs_t *fs,
67                    svn_revnum_t rev,
68                    trail_t *trail,
69                    apr_pool_t *pool)
70{
71  base_fs_data_t *bfd = fs->fsap_data;
72  int db_err;
73  DBT key, value;
74  svn_skel_t *skel;
75  revision_t *revision;
76
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;
81
82  if (!SVN_IS_VALID_REVNUM(rev))
83    return svn_fs_base__err_dangling_rev(fs, rev);
84
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,
88                                                    sizeof(recno)),
89                               svn_fs_base__result_dbt(&value),
90                               0);
91  svn_fs_base__track_dbt(&value, pool);
92
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);
96
97  /* Handle any other error conditions.  */
98  SVN_ERR(BDB_WRAP(fs, N_("reading filesystem revision"), db_err));
99
100  /* Parse REVISION skel.  */
101  skel = svn_skel__parse(value.data, value.size, pool);
102  if (! skel)
103    return svn_fs_base__err_corrupt_fs_revision(fs, rev);
104
105  /* Convert skel to native type. */
106  SVN_ERR(svn_fs_base__parse_revision_skel(&revision, skel, pool));
107
108  *revision_p = revision;
109  return SVN_NO_ERROR;
110}
111
112
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
116   in *REV.  */
117svn_error_t *
118svn_fs_bdb__put_rev(svn_revnum_t *rev,
119                    svn_fs_t *fs,
120                    const revision_t *revision,
121                    trail_t *trail,
122                    apr_pool_t *pool)
123{
124  base_fs_data_t *bfd = fs->fsap_data;
125  int db_err;
126  db_recno_t recno = 0;
127  svn_skel_t *skel;
128  DBT key, value;
129
130  /* Convert native type to skel. */
131  SVN_ERR(svn_fs_base__unparse_revision_skel(&skel, revision, pool));
132
133  if (SVN_IS_VALID_REVNUM(*rev))
134    {
135      DBT query, result;
136
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);
145    }
146
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),
151                               DB_APPEND);
152  SVN_ERR(BDB_WRAP(fs, N_("storing filesystem revision"), db_err));
153
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.  */
157  *rev = recno - 1;
158  return SVN_NO_ERROR;
159}
160
161
162
163/* Getting the youngest revision.  */
164
165
166svn_error_t *
167svn_fs_bdb__youngest_rev(svn_revnum_t *youngest_p,
168                         svn_fs_t *fs,
169                         trail_t *trail,
170                         apr_pool_t *pool)
171{
172  base_fs_data_t *bfd = fs->fsap_data;
173  int db_err;
174  DBC *cursor = 0;
175  DBT key, value;
176  db_recno_t recno;
177
178  SVN_ERR(svn_fs__check_fs(fs, TRUE));
179
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,
184                                          &cursor, 0)));
185
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),
190                           DB_LAST);
191
192  if (db_err)
193    {
194      /* Free the cursor.  Ignore any error value --- the error above
195         is more interesting.  */
196      svn_bdb_dbc_close(cursor);
197
198      if (db_err == DB_NOTFOUND)
199        /* The revision 0 should always be present, at least.  */
200        return
201          svn_error_createf
202          (SVN_ERR_FS_CORRUPT, 0,
203           "Corrupt DB: revision 0 missing from 'revisions' table, in "
204           "filesystem '%s'", fs->path);
205
206      SVN_ERR(BDB_WRAP(fs, N_("getting youngest revision (finding last entry)"),
207                       db_err));
208    }
209
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)));
218
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;
223  return SVN_NO_ERROR;
224}
225