2 tdbrestore -- construct a tdb from tdbdump output.
3 Copyright (C) Volker Lendecke 2010
4 Copyright (C) Simon McVittie 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "system/locale.h"
24 #include "system/time.h"
25 #include "system/filesys.h"
26 #include "system/wait.h"
29 #define debug_fprintf(file, fmt, ...) do {/*nothing*/} while (0)
31 static int read_linehead(FILE *f)
46 for (i=0; i<sizeof(prefix); i++) {
56 if (i == sizeof(prefix)) {
61 if (sscanf(prefix, "%d) = ", &num_bytes) != 1) {
67 static int read_hex(void) {
71 fprintf(stderr, "Unexpected EOF in data\n");
73 } else if (c == '"') {
74 fprintf(stderr, "Unexpected \\\" sequence\n");
76 } else if ('0' <= c && c <= '9') {
78 } else if ('A' <= c && c <= 'F') {
80 } else if ('a' <= c && c <= 'f') {
83 fprintf(stderr, "Invalid hex: %c\n", c);
88 static int read_data(FILE *f, TDB_DATA *d, size_t size) {
92 d->dptr = (unsigned char *)malloc(size);
93 if (d->dptr == NULL) {
98 for (i=0; i<size; i++) {
101 fprintf(stderr, "Unexpected EOF in data\n");
103 } else if (c == '"') {
105 } else if (c == '\\') {
111 assert(high == (high & 0xf0));
116 assert(low == (low & 0x0f));
117 d->dptr[i] = (low|high);
125 static int swallow(FILE *f, const char *s, int *eof)
129 if (fgets(line, sizeof(line), f) == NULL) {
135 if (strcmp(line, s) != 0) {
141 static int read_rec(FILE *f, TDB_CONTEXT *tdb, int *eof)
150 if (swallow(f, "{\n", eof) == -1) {
153 length = read_linehead(f);
157 if (read_data(f, &key, length) == -1) {
160 if (swallow(f, "\"\n", NULL) == -1) {
163 length = read_linehead(f);
167 if (read_data(f, &data, length) == -1) {
170 if ((swallow(f, "\"\n", NULL) == -1)
171 || (swallow(f, "}\n", NULL) == -1)) {
174 if (tdb_store(tdb, key, data, TDB_INSERT) == -1) {
175 fprintf(stderr, "TDB error: %s\n", tdb_errorstr(tdb));
186 static int restore_tdb(const char *fname)
190 tdb = tdb_open(fname, 0, 0, O_RDWR|O_CREAT|O_EXCL, 0666);
193 fprintf(stderr, "Failed to open %s\n", fname);
199 if (read_rec(stdin, tdb, &eof) == -1) {
206 if (tdb_close(tdb)) {
207 fprintf(stderr, "Error closing tdb\n");
210 fprintf(stderr, "EOF\n");
214 int main(int argc, char *argv[])
219 printf("Usage: %s dbname < tdbdump_output\n", argv[0]);
225 return restore_tdb(fname);