1/*-
2 * Copyright (c) 2007 Diomidis Spinellis
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <sys/types.h>
32#include <sys/acct.h>
33
34#include <ctype.h>
35#include <err.h>
36#include <errno.h>
37#include <db.h>
38#include <fcntl.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <limits.h>
43
44#include "extern.h"
45
46/* Key used to store the version of the database data elements. */
47#define VERSION_KEY "\0VERSION"
48
49/*
50 * Create the in-memory database, *mdb.
51 * If iflag is not set, fill-in mdb with the records of the disk-based
52 * database dbname.
53 * Upgrade old-version records by calling v1_to_v2.
54 * Return 0 if OK, -1 on error.
55 */
56int
57db_copy_in(DB **mdb, const char *dbname, const char *uname, BTREEINFO *bti,
58    int (*v1_to_v2)(DBT *key, DBT *data))
59{
60	DBT key, data;
61	DB *ddb;
62	int error, rv, version;
63
64	if ((*mdb = dbopen(NULL, O_RDWR, 0, DB_BTREE, bti)) == NULL)
65		return (-1);
66
67	if (iflag)
68		return (0);
69
70	if ((ddb = dbopen(dbname, O_RDONLY, 0, DB_BTREE, bti)) == NULL) {
71		if (errno == ENOENT)
72			return (0);
73		warn("retrieving %s summary", uname);
74		db_destroy(*mdb, uname);
75		return (-1);
76	}
77
78	error = 0;
79
80	/* Obtain/set version. */
81	version = 1;
82	key.data = (void*)&VERSION_KEY;
83	key.size = sizeof(VERSION_KEY);
84
85	rv = DB_GET(ddb, &key, &data, 0);
86	if (rv < 0) {
87		warn("get version key from %s stats", uname);
88		error = -1;
89		goto closeout;
90	} else if (rv == 0) {	/* It's there; verify version. */
91		if (data.size != sizeof(version)) {
92			warnx("invalid version size %zd in %s",
93			    data.size, uname);
94			error = -1;
95			goto closeout;
96		}
97		memcpy(&version, data.data, data.size);
98		if (version != 2) {
99			warnx("unsupported version %d in %s",
100			    version, uname);
101			error = -1;
102			goto closeout;
103		}
104	}
105
106	for (rv = DB_SEQ(ddb, &key, &data, R_FIRST); rv == 0;
107	    rv = DB_SEQ(ddb, &key, &data, R_NEXT)) {
108
109		/* See if this is a version record. */
110		if (key.size == sizeof(VERSION_KEY) &&
111		    memcmp(key.data, VERSION_KEY, sizeof(VERSION_KEY)) == 0)
112			continue;
113
114		/* Convert record from v1, if needed. */
115		if (version == 1 && v1_to_v2(&key, &data) < 0) {
116			warn("converting %s stats", uname);
117			error = -1;
118			goto closeout;
119		}
120
121		/* Copy record to the in-memory database. */
122		if ((rv = DB_PUT(*mdb, &key, &data, 0)) < 0) {
123			warn("initializing %s stats", uname);
124			error = -1;
125			goto closeout;
126		}
127	}
128	if (rv < 0) {
129		warn("retrieving %s summary", uname);
130		error = -1;
131	}
132
133closeout:
134	if (DB_CLOSE(ddb) < 0) {
135		warn("closing %s summary", uname);
136		error = -1;
137	}
138
139	if (error)
140		db_destroy(*mdb, uname);
141	return (error);
142}
143
144/*
145 * Save the in-memory database mdb to the disk database dbname.
146 * Return 0 if OK, -1 on error.
147 */
148int
149db_copy_out(DB *mdb, const char *dbname, const char *uname, BTREEINFO *bti)
150{
151	DB *ddb;
152	DBT key, data;
153	int error, rv, version;
154
155	if ((ddb = dbopen(dbname, O_RDWR|O_CREAT|O_TRUNC, 0644,
156	    DB_BTREE, bti)) == NULL) {
157		warn("creating %s summary", uname);
158		return (-1);
159	}
160
161	error = 0;
162
163	for (rv = DB_SEQ(mdb, &key, &data, R_FIRST);
164	    rv == 0; rv = DB_SEQ(mdb, &key, &data, R_NEXT)) {
165		if ((rv = DB_PUT(ddb, &key, &data, 0)) < 0) {
166			warn("saving %s summary", uname);
167			error = -1;
168			goto out;
169		}
170	}
171	if (rv < 0) {
172		warn("retrieving %s stats", uname);
173		error = -1;
174	}
175
176out:
177	/* Add a version record. */
178	key.data = (void*)&VERSION_KEY;
179	key.size = sizeof(VERSION_KEY);
180	version = 2;
181	data.data = &version;
182	data.size = sizeof(version);
183	if ((rv = DB_PUT(ddb, &key, &data, 0)) < 0) {
184		warn("add version record to %s stats", uname);
185		error = -1;
186	} else if (rv == 1) {
187		warnx("duplicate version record in %s stats", uname);
188		error = -1;
189	}
190
191	if (DB_SYNC(ddb, 0) < 0) {
192		warn("syncing %s summary", uname);
193		error = -1;
194	}
195	if (DB_CLOSE(ddb) < 0) {
196		warn("closing %s summary", uname);
197		error = -1;
198	}
199	return error;
200}
201
202void
203db_destroy(DB *db, const char *uname)
204{
205	if (DB_CLOSE(db) < 0)
206		warn("destroying %s stats", uname);
207}
208