]> git.ipfire.org Git - thirdparty/systemd.git/blob - tdb/tdbback.c
[PATCH] tweak tdb to build within udev better.
[thirdparty/systemd.git] / tdb / tdbback.c
1 /*
2 Unix SMB/CIFS implementation.
3 low level tdb backup and restore utility
4 Copyright (C) Andrew Tridgell 2002
5
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.
10
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.
15
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.
19 */
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <time.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <ctype.h>
33 #include <signal.h>
34 #include "tdb.h"
35
36 static int failed;
37
38 char *add_suffix(const char *name, const char *suffix)
39 {
40 char *ret;
41 int len = strlen(name) + strlen(suffix) + 1;
42 ret = malloc(len);
43 if (!ret) {
44 fprintf(stderr,"Out of memory!\n");
45 exit(1);
46 }
47 strncpy(ret, name, len);
48 strncat(ret, suffix, len);
49 return ret;
50 }
51
52 static int copy_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
53 {
54 TDB_CONTEXT *tdb_new = (TDB_CONTEXT *)state;
55
56 if (tdb_store(tdb_new, key, dbuf, TDB_INSERT) != 0) {
57 fprintf(stderr,"Failed to insert into %s\n", tdb_new->name);
58 failed = 1;
59 return 1;
60 }
61 return 0;
62 }
63
64
65 static int test_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
66 {
67 return 0;
68 }
69
70 /*
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
74 */
75 int backup_tdb(const char *old_name, const char *new_name)
76 {
77 TDB_CONTEXT *tdb;
78 TDB_CONTEXT *tdb_new;
79 char *tmp_name;
80 struct stat st;
81 int count1, count2;
82
83 tmp_name = add_suffix(new_name, ".tmp");
84
85 /* stat the old tdb to find its permissions */
86 if (stat(old_name, &st) != 0) {
87 perror(old_name);
88 return 1;
89 }
90
91 /* open the old tdb */
92 tdb = tdb_open(old_name, 0, 0, O_RDWR, 0);
93 if (!tdb) {
94 printf("Failed to open %s\n", old_name);
95 return 1;
96 }
97
98 /* create the new tdb */
99 unlink(tmp_name);
100 tdb_new = tdb_open(tmp_name, tdb->header.hash_size,
101 TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL,
102 st.st_mode & 0777);
103 if (!tdb_new) {
104 perror(tmp_name);
105 free(tmp_name);
106 return 1;
107 }
108
109 /* lock the old tdb */
110 if (tdb_lockall(tdb) != 0) {
111 fprintf(stderr,"Failed to lock %s\n", old_name);
112 tdb_close(tdb);
113 tdb_close(tdb_new);
114 unlink(tmp_name);
115 free(tmp_name);
116 return 1;
117 }
118
119 failed = 0;
120
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);
125 tdb_close(tdb);
126 tdb_close(tdb_new);
127 unlink(tmp_name);
128 free(tmp_name);
129 return 1;
130 }
131
132 /* close the old tdb */
133 tdb_close(tdb);
134
135 /* close the new tdb and re-open read-only */
136 tdb_close(tdb_new);
137 tdb_new = tdb_open(tmp_name, 0, TDB_DEFAULT, O_RDONLY, 0);
138 if (!tdb_new) {
139 fprintf(stderr,"failed to reopen %s\n", tmp_name);
140 unlink(tmp_name);
141 perror(tmp_name);
142 free(tmp_name);
143 return 1;
144 }
145
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);
150 tdb_close(tdb_new);
151 unlink(tmp_name);
152 free(tmp_name);
153 return 1;
154 }
155
156 /* make sure the new tdb has reached stable storage */
157 fsync(tdb_new->fd);
158
159 /* close the new tdb and rename it to .bak */
160 tdb_close(tdb_new);
161 unlink(new_name);
162 if (rename(tmp_name, new_name) != 0) {
163 perror(new_name);
164 free(tmp_name);
165 return 1;
166 }
167
168 free(tmp_name);
169
170 return 0;
171 }
172
173
174
175 /*
176 verify a tdb and if it is corrupt then restore from *.bak
177 */
178 int verify_tdb(const char *fname, const char *bak_name)
179 {
180 TDB_CONTEXT *tdb;
181 int count = -1;
182
183 /* open the tdb */
184 tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
185
186 /* traverse the tdb, then close it */
187 if (tdb) {
188 count = tdb_traverse(tdb, test_fn, NULL);
189 tdb_close(tdb);
190 }
191
192 /* count is < 0 means an error */
193 if (count < 0) {
194 printf("restoring %s\n", fname);
195 return backup_tdb(bak_name, fname);
196 }
197
198 printf("%s : %d records\n", fname, count);
199
200 return 0;
201 }