2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Diomidis Spinellis
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
48 /* Key used to store the version of the database data elements. */
49 static char VERSION_KEY[] = "\0VERSION";
52 * Create the in-memory database, *mdb.
53 * If iflag is not set, fill-in mdb with the records of the disk-based
55 * Upgrade old-version records by calling v1_to_v2.
56 * Return 0 if OK, -1 on error.
59 db_copy_in(DB **mdb, const char *dbname, const char *uname, BTREEINFO *bti,
60 int (*v1_to_v2)(DBT *key, DBT *data))
64 int error, rv, version;
66 if ((*mdb = dbopen(NULL, O_RDWR, 0, DB_BTREE, bti)) == NULL)
72 if ((ddb = dbopen(dbname, O_RDONLY, 0, DB_BTREE, bti)) == NULL) {
75 warn("retrieving %s summary", uname);
76 db_destroy(*mdb, uname);
82 /* Obtain/set version. */
84 key.data = (void*)&VERSION_KEY;
85 key.size = sizeof(VERSION_KEY);
87 rv = DB_GET(ddb, &key, &data, 0);
89 warn("get version key from %s stats", uname);
92 } else if (rv == 0) { /* It's there; verify version. */
93 if (data.size != sizeof(version)) {
94 warnx("invalid version size %zd in %s",
99 memcpy(&version, data.data, data.size);
101 warnx("unsupported version %d in %s",
108 for (rv = DB_SEQ(ddb, &key, &data, R_FIRST); rv == 0;
109 rv = DB_SEQ(ddb, &key, &data, R_NEXT)) {
111 /* See if this is a version record. */
112 if (key.size == sizeof(VERSION_KEY) &&
113 memcmp(key.data, VERSION_KEY, sizeof(VERSION_KEY)) == 0)
116 /* Convert record from v1, if needed. */
117 if (version == 1 && v1_to_v2(&key, &data) < 0) {
118 warn("converting %s stats", uname);
123 /* Copy record to the in-memory database. */
124 if ((rv = DB_PUT(*mdb, &key, &data, 0)) < 0) {
125 warn("initializing %s stats", uname);
131 warn("retrieving %s summary", uname);
136 if (DB_CLOSE(ddb) < 0) {
137 warn("closing %s summary", uname);
142 db_destroy(*mdb, uname);
147 * Save the in-memory database mdb to the disk database dbname.
148 * Return 0 if OK, -1 on error.
151 db_copy_out(DB *mdb, const char *dbname, const char *uname, BTREEINFO *bti)
155 int error, rv, version;
157 if ((ddb = dbopen(dbname, O_RDWR|O_CREAT|O_TRUNC, 0644,
158 DB_BTREE, bti)) == NULL) {
159 warn("creating %s summary", uname);
165 for (rv = DB_SEQ(mdb, &key, &data, R_FIRST);
166 rv == 0; rv = DB_SEQ(mdb, &key, &data, R_NEXT)) {
167 if ((rv = DB_PUT(ddb, &key, &data, 0)) < 0) {
168 warn("saving %s summary", uname);
174 warn("retrieving %s stats", uname);
179 /* Add a version record. */
180 key.data = (void*)&VERSION_KEY;
181 key.size = sizeof(VERSION_KEY);
183 data.data = &version;
184 data.size = sizeof(version);
185 if ((rv = DB_PUT(ddb, &key, &data, 0)) < 0) {
186 warn("add version record to %s stats", uname);
188 } else if (rv == 1) {
189 warnx("duplicate version record in %s stats", uname);
193 if (DB_SYNC(ddb, 0) < 0) {
194 warn("syncing %s summary", uname);
197 if (DB_CLOSE(ddb) < 0) {
198 warn("closing %s summary", uname);
205 db_destroy(DB *db, const char *uname)
207 if (DB_CLOSE(db) < 0)
208 warn("destroying %s stats", uname);