node-origins-table.c revision 251886
1/* node-origins-table.c : operations on the `node-origins' 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#include "../fs.h"
25#include "../err.h"
26#include "../id.h"
27#include "dbt.h"
28#include "../trail.h"
29#include "bdb-err.h"
30#include "../../libsvn_fs/fs-loader.h"
31#include "node-origins-table.h"
32
33#include "svn_private_config.h"
34
35
36int svn_fs_bdb__open_node_origins_table(DB **node_origins_p,
37                                        DB_ENV *env,
38                                        svn_boolean_t create)
39{
40  const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
41  DB *node_origins;
42  int error;
43
44  BDB_ERR(svn_fs_bdb__check_version());
45  BDB_ERR(db_create(&node_origins, env, 0));
46  error = (node_origins->open)(SVN_BDB_OPEN_PARAMS(node_origins, NULL),
47                               "node-origins", 0, DB_BTREE,
48                               open_flags, 0666);
49
50  /* Create the node-origins table if it doesn't exist. */
51  if (error == ENOENT && (! create))
52    {
53      BDB_ERR(node_origins->close(node_origins, 0));
54      return svn_fs_bdb__open_node_origins_table(node_origins_p, env, TRUE);
55    }
56
57  BDB_ERR(error);
58
59  *node_origins_p = node_origins;
60  return 0;
61}
62
63svn_error_t *svn_fs_bdb__get_node_origin(const svn_fs_id_t **origin_id,
64                                         svn_fs_t *fs,
65                                         const char *node_id,
66                                         trail_t *trail,
67                                         apr_pool_t *pool)
68{
69  base_fs_data_t *bfd = fs->fsap_data;
70  DBT key, value;
71  int db_err;
72
73  svn_fs_base__trail_debug(trail, "node-origins", "get");
74  db_err = bfd->node_origins->get(bfd->node_origins, trail->db_txn,
75                                  svn_fs_base__str_to_dbt(&key, node_id),
76                                  svn_fs_base__result_dbt(&value), 0);
77  svn_fs_base__track_dbt(&value, pool);
78
79  if (db_err == DB_NOTFOUND)
80    return svn_fs_base__err_no_such_node_origin(fs, node_id);
81
82  *origin_id = svn_fs_base__id_parse(value.data, value.size, pool);
83  return SVN_NO_ERROR;
84}
85
86svn_error_t *svn_fs_bdb__set_node_origin(svn_fs_t *fs,
87                                         const char *node_id,
88                                         const svn_fs_id_t *origin_id,
89                                         trail_t *trail,
90                                         apr_pool_t *pool)
91{
92  base_fs_data_t *bfd = fs->fsap_data;
93  DBT key, value;
94  int db_err;
95
96  /* Create a key from our NODE_ID. */
97  svn_fs_base__str_to_dbt(&key, node_id);
98
99  /* Check to see if we already have a mapping for NODE_ID.  If so,
100     and the value is the same one we were about to write.  That's
101     cool -- just do nothing.  If, however, the value is *different*,
102     that's a red flag!  */
103  svn_fs_base__trail_debug(trail, "node-origins", "get");
104  db_err = bfd->node_origins->get(bfd->node_origins, trail->db_txn,
105                                  &key, svn_fs_base__result_dbt(&value), 0);
106  svn_fs_base__track_dbt(&value, pool);
107  if (db_err != DB_NOTFOUND)
108    {
109      const svn_string_t *origin_id_str =
110        svn_fs_base__id_unparse(origin_id, pool);
111      const svn_string_t *old_origin_id_str =
112        svn_string_ncreate(value.data, value.size, pool);
113
114      if (! svn_string_compare(origin_id_str, old_origin_id_str))
115        return svn_error_createf
116          (SVN_ERR_FS_CORRUPT, NULL,
117           _("Node origin for '%s' exists in filesystem '%s' with a different "
118             "value (%s) than what we were about to store (%s)"),
119           node_id, fs->path, old_origin_id_str->data, origin_id_str->data);
120      else
121        return SVN_NO_ERROR;
122    }
123
124  /* Create a value from our ORIGIN_ID, and add this record to the table. */
125  svn_fs_base__id_to_dbt(&value, origin_id, pool);
126  svn_fs_base__trail_debug(trail, "node-origins", "put");
127  return BDB_WRAP(fs, N_("storing node-origins record"),
128                  bfd->node_origins->put(bfd->node_origins, trail->db_txn,
129                                         &key, &value, 0));
130}
131
132svn_error_t *svn_fs_bdb__delete_node_origin(svn_fs_t *fs,
133                                            const char *node_id,
134                                            trail_t *trail,
135                                            apr_pool_t *pool)
136{
137  base_fs_data_t *bfd = fs->fsap_data;
138  DBT key;
139
140  svn_fs_base__str_to_dbt(&key, node_id);
141  svn_fs_base__trail_debug(trail, "node-origins", "del");
142  return BDB_WRAP(fs, N_("deleting entry from 'node-origins' table"),
143                  bfd->node_origins->del(bfd->node_origins,
144                                         trail->db_txn, &key, 0));
145}
146