1 /* miscellaneous-table.c : operations on the `miscellaneous' 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 <string.h>
24 #include <assert.h>
25 #include "bdb_compat.h"
26 
27 #include "svn_pools.h"
28 #include "dbt.h"
29 #include "../err.h"
30 #include "../fs.h"
31 #include "../trail.h"
32 #include "../../libsvn_fs/fs-loader.h"
33 #include "bdb-err.h"
34 #include "miscellaneous-table.h"
35 
36 #include "private/svn_fs_util.h"
37 
38 
39 int
svn_fs_bdb__open_miscellaneous_table(DB ** miscellaneous_p,DB_ENV * env,svn_boolean_t create)40 svn_fs_bdb__open_miscellaneous_table(DB **miscellaneous_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 *miscellaneous;
46   int error;
47 
48   BDB_ERR(svn_fs_bdb__check_version());
49   BDB_ERR(db_create(&miscellaneous, env, 0));
50   error = (miscellaneous->open)(SVN_BDB_OPEN_PARAMS(miscellaneous, NULL),
51                                 "miscellaneous", 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(miscellaneous->close(miscellaneous, 0));
59       return svn_fs_bdb__open_miscellaneous_table(miscellaneous_p, env, TRUE);
60     }
61   BDB_ERR(error);
62 
63   /* If we're creating the table from scratch (not upgrading), record the
64      upgrade rev as 0. */
65   if (create)
66     {
67       DBT key, value;
68 
69       BDB_ERR(miscellaneous->put
70               (miscellaneous, 0,
71                svn_fs_base__str_to_dbt
72                (&key, SVN_FS_BASE__MISC_FORWARD_DELTA_UPGRADE),
73                svn_fs_base__str_to_dbt(&value, "0"), 0));
74     }
75 
76   *miscellaneous_p = miscellaneous;
77   return 0;
78 }
79 
80 
81 svn_error_t *
svn_fs_bdb__miscellaneous_set(svn_fs_t * fs,const char * key_str,const char * val,trail_t * trail,apr_pool_t * pool)82 svn_fs_bdb__miscellaneous_set(svn_fs_t *fs,
83                               const char *key_str,
84                               const char *val,
85                               trail_t *trail,
86                               apr_pool_t *pool)
87 {
88   base_fs_data_t *bfd = fs->fsap_data;
89   DBT key, value;
90 
91   svn_fs_base__str_to_dbt(&key, key_str);
92   if (val == NULL)
93     {
94       svn_fs_base__trail_debug(trail, "miscellaneous", "del");
95       return BDB_WRAP(fs, N_("deleting record from 'miscellaneous' table"),
96                       bfd->miscellaneous->del(bfd->miscellaneous,
97                                               trail->db_txn, &key, 0));
98     }
99   else
100     {
101       svn_fs_base__str_to_dbt(&value, val);
102       svn_fs_base__trail_debug(trail, "miscellaneous", "add");
103       return BDB_WRAP(fs, N_("storing miscellaneous record"),
104                       bfd->miscellaneous->put(bfd->miscellaneous,
105                                               trail->db_txn,
106                                               &key, &value, 0));
107     }
108 }
109 
110 
111 svn_error_t *
svn_fs_bdb__miscellaneous_get(const char ** val,svn_fs_t * fs,const char * key_str,trail_t * trail,apr_pool_t * pool)112 svn_fs_bdb__miscellaneous_get(const char **val,
113                               svn_fs_t *fs,
114                               const char *key_str,
115                               trail_t *trail,
116                               apr_pool_t *pool)
117 {
118   base_fs_data_t *bfd = fs->fsap_data;
119   DBT key, value;
120   int db_err;
121 
122   *val = NULL;
123   svn_fs_base__trail_debug(trail, "miscellaneous", "get");
124   db_err = bfd->miscellaneous->get(bfd->miscellaneous, trail->db_txn,
125                                    svn_fs_base__str_to_dbt(&key, key_str),
126                                    svn_fs_base__result_dbt(&value), 0);
127   svn_fs_base__track_dbt(&value, pool);
128 
129   if (db_err != DB_NOTFOUND)
130     {
131       SVN_ERR(BDB_WRAP(fs, N_("fetching miscellaneous record"), db_err));
132       *val = apr_pstrmemdup(pool, value.data, value.size);
133     }
134   return SVN_NO_ERROR;
135 }
136