1 /* node-origins-table.c : operations on the `node-origins' 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"
30 #include "../../libsvn_fs/fs-loader.h"
31 #include "node-origins-table.h"
33 #include "svn_private_config.h"
36 int svn_fs_bdb__open_node_origins_table(DB **node_origins_p,
40 const u_int32_t open_flags = (create ? (DB_CREATE | DB_EXCL) : 0);
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,
50 /* Create the node-origins table if it doesn't exist. */
51 if (error == ENOENT && (! create))
53 BDB_ERR(node_origins->close(node_origins, 0));
54 return svn_fs_bdb__open_node_origins_table(node_origins_p, env, TRUE);
59 *node_origins_p = node_origins;
63 svn_error_t *svn_fs_bdb__get_node_origin(const svn_fs_id_t **origin_id,
69 base_fs_data_t *bfd = fs->fsap_data;
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);
79 if (db_err == DB_NOTFOUND)
80 return svn_fs_base__err_no_such_node_origin(fs, node_id);
82 *origin_id = svn_fs_base__id_parse(value.data, value.size, pool);
86 svn_error_t *svn_fs_bdb__set_node_origin(svn_fs_t *fs,
88 const svn_fs_id_t *origin_id,
92 base_fs_data_t *bfd = fs->fsap_data;
96 /* Create a key from our NODE_ID. */
97 svn_fs_base__str_to_dbt(&key, node_id);
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)
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);
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);
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,
132 svn_error_t *svn_fs_bdb__delete_node_origin(svn_fs_t *fs,
137 base_fs_data_t *bfd = fs->fsap_data;
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));