]>
git.ipfire.org Git - thirdparty/systemd.git/blob - tdb/tdbback.c
2 Unix SMB/CIFS implementation.
3 low level tdb backup and restore utility
4 Copyright (C) Andrew Tridgell 2002
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.
38 char *add_suffix(const char *name
, const char *suffix
)
41 int len
= strlen(name
) + strlen(suffix
) + 1;
44 fprintf(stderr
,"Out of memory!\n");
47 strncpy(ret
, name
, len
);
48 strncat(ret
, suffix
, len
);
52 static int copy_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
54 TDB_CONTEXT
*tdb_new
= (TDB_CONTEXT
*)state
;
56 if (tdb_store(tdb_new
, key
, dbuf
, TDB_INSERT
) != 0) {
57 fprintf(stderr
,"Failed to insert into %s\n", tdb_new
->name
);
65 static int test_fn(TDB_CONTEXT
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
71 carefully backup a tdb, validating the contents and
72 only doing the backup if its OK
73 this function is also used for restore
75 int backup_tdb(const char *old_name
, const char *new_name
)
83 tmp_name
= add_suffix(new_name
, ".tmp");
85 /* stat the old tdb to find its permissions */
86 if (stat(old_name
, &st
) != 0) {
91 /* open the old tdb */
92 tdb
= tdb_open(old_name
, 0, 0, O_RDWR
, 0);
94 printf("Failed to open %s\n", old_name
);
98 /* create the new tdb */
100 tdb_new
= tdb_open(tmp_name
, tdb
->header
.hash_size
,
101 TDB_DEFAULT
, O_RDWR
|O_CREAT
|O_EXCL
,
109 /* lock the old tdb */
110 if (tdb_lockall(tdb
) != 0) {
111 fprintf(stderr
,"Failed to lock %s\n", old_name
);
121 /* traverse and copy */
122 count1
= tdb_traverse(tdb
, copy_fn
, (void *)tdb_new
);
123 if (count1
< 0 || failed
) {
124 fprintf(stderr
,"failed to copy %s\n", old_name
);
132 /* close the old tdb */
135 /* close the new tdb and re-open read-only */
137 tdb_new
= tdb_open(tmp_name
, 0, TDB_DEFAULT
, O_RDONLY
, 0);
139 fprintf(stderr
,"failed to reopen %s\n", tmp_name
);
146 /* traverse the new tdb to confirm */
147 count2
= tdb_traverse(tdb_new
, test_fn
, 0);
148 if (count2
!= count1
) {
149 fprintf(stderr
,"failed to copy %s\n", old_name
);
156 /* make sure the new tdb has reached stable storage */
159 /* close the new tdb and rename it to .bak */
162 if (rename(tmp_name
, new_name
) != 0) {
176 verify a tdb and if it is corrupt then restore from *.bak
178 int verify_tdb(const char *fname
, const char *bak_name
)
184 tdb
= tdb_open(fname
, 0, 0, O_RDONLY
, 0);
186 /* traverse the tdb, then close it */
188 count
= tdb_traverse(tdb
, test_fn
, NULL
);
192 /* count is < 0 means an error */
194 printf("restoring %s\n", fname
);
195 return backup_tdb(bak_name
, fname
);
198 printf("%s : %d records\n", fname
, count
);