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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/locale.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "system/wait.h"
28 static int read_linehead(FILE *f
)
43 for (i
=0; i
<sizeof(prefix
); i
++) {
53 if (i
== sizeof(prefix
)) {
58 if (sscanf(prefix
, "%d) = ", &num_bytes
) != 1) {
64 static int read_hex(void) {
68 fprintf(stderr
, "Unexpected EOF in data\n");
70 } else if (c
== '"') {
71 fprintf(stderr
, "Unexpected \\\" sequence\n");
73 } else if ('0' <= c
&& c
<= '9') {
75 } else if ('A' <= c
&& c
<= 'F') {
77 } else if ('a' <= c
&& c
<= 'f') {
80 fprintf(stderr
, "Invalid hex: %c\n", c
);
85 static int read_data(FILE *f
, TDB_DATA
*d
, size_t size
) {
89 d
->dptr
= (unsigned char *)malloc(size
);
90 if (d
->dptr
== NULL
) {
95 for (i
=0; i
<size
; i
++) {
98 fprintf(stderr
, "Unexpected EOF in data\n");
100 } else if (c
== '"') {
102 } else if (c
== '\\') {
108 assert(high
== (high
& 0xf0));
113 assert(low
== (low
& 0x0f));
114 d
->dptr
[i
] = (low
|high
);
122 static int swallow(FILE *f
, const char *s
, int *eof
)
126 if (fgets(line
, sizeof(line
), f
) == NULL
) {
132 if (strcmp(line
, s
) != 0) {
138 static int read_rec(FILE *f
, TDB_CONTEXT
*tdb
, int *eof
)
147 if (swallow(f
, "{\n", eof
) == -1) {
150 length
= read_linehead(f
);
154 if (read_data(f
, &key
, length
) == -1) {
157 if (swallow(f
, "\"\n", NULL
) == -1) {
160 length
= read_linehead(f
);
164 if (read_data(f
, &data
, length
) == -1) {
167 if ((swallow(f
, "\"\n", NULL
) == -1)
168 || (swallow(f
, "}\n", NULL
) == -1)) {
171 if (tdb_store(tdb
, key
, data
, TDB_INSERT
) != 0) {
172 fprintf(stderr
, "TDB error: %s\n", tdb_errorstr(tdb
));
183 static int restore_tdb(const char *fname
)
187 tdb
= tdb_open(fname
, 0, 0, O_RDWR
|O_CREAT
|O_EXCL
, 0666);
190 fprintf(stderr
, "Failed to open %s\n", fname
);
196 if (read_rec(stdin
, tdb
, &eof
) == -1) {
203 if (tdb_close(tdb
)) {
204 fprintf(stderr
, "Error closing tdb\n");
210 int main(int argc
, char *argv
[])
215 printf("Usage: %s dbname < tdbdump_output\n", argv
[0]);
221 return restore_tdb(fname
);