~vcs-imports/samba/main

« back to all changes in this revision

Viewing changes to source/tdb/tdbback.c

  • Committer: jerry
  • Date: 2006-07-14 21:48:39 UTC
  • Revision ID: vcs-imports@canonical.com-20060714214839-586d8c489a8fcead
gutting trunk to move to svn:externals

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#ifdef STANDALONE
22
 
#if HAVE_CONFIG_H
23
 
#include <config.h>
24
 
#endif
25
 
 
26
 
#include <errno.h>
27
 
#include <stdlib.h>
28
 
#include <stdio.h>
29
 
#include <fcntl.h>
30
 
#include <unistd.h>
31
 
#include <string.h>
32
 
#include <fcntl.h>
33
 
#include <time.h>
34
 
#include <sys/mman.h>
35
 
 
36
 
#include <sys/stat.h>
37
 
#include <sys/time.h>
38
 
#include <ctype.h>
39
 
#include <signal.h>
40
 
 
41
 
#else
42
 
#include "includes.h"
43
 
 
44
 
#ifdef malloc
45
 
#undef malloc
46
 
#endif
47
 
                                                                                                                 
48
 
#ifdef realloc
49
 
#undef realloc
50
 
#endif
51
 
                                                                                                                 
52
 
#ifdef calloc
53
 
#undef calloc
54
 
#endif
55
 
 
56
 
#endif
57
 
 
58
 
#include "tdb.h"
59
 
 
60
 
static int failed;
61
 
 
62
 
char *add_suffix(const char *name, const char *suffix)
63
 
{
64
 
        char *ret;
65
 
        int len = strlen(name) + strlen(suffix) + 1;
66
 
        ret = malloc(len);
67
 
        if (!ret) {
68
 
                fprintf(stderr,"Out of memory!\n");
69
 
                exit(1);
70
 
        }
71
 
        snprintf(ret, len, "%s%s", name, suffix);
72
 
        return ret;
73
 
}
74
 
 
75
 
static int copy_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
76
 
{
77
 
        TDB_CONTEXT *tdb_new = (TDB_CONTEXT *)state;
78
 
 
79
 
        if (tdb_store(tdb_new, key, dbuf, TDB_INSERT) != 0) {
80
 
                fprintf(stderr,"Failed to insert into %s\n", tdb_name(tdb));
81
 
                failed = 1;
82
 
                return 1;
83
 
        }
84
 
        return 0;
85
 
}
86
 
 
87
 
 
88
 
static int test_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
89
 
{
90
 
        return 0;
91
 
}
92
 
 
93
 
/*
94
 
  carefully backup a tdb, validating the contents and
95
 
  only doing the backup if its OK
96
 
  this function is also used for restore
97
 
*/
98
 
int backup_tdb(const char *old_name, const char *new_name)
99
 
{
100
 
        TDB_CONTEXT *tdb;
101
 
        TDB_CONTEXT *tdb_new;
102
 
        char *tmp_name;
103
 
        struct stat st;
104
 
        int count1, count2;
105
 
 
106
 
        tmp_name = add_suffix(new_name, ".tmp");
107
 
 
108
 
        /* stat the old tdb to find its permissions */
109
 
        if (stat(old_name, &st) != 0) {
110
 
                perror(old_name);
111
 
                free(tmp_name);
112
 
                return 1;
113
 
        }
114
 
 
115
 
        /* open the old tdb */
116
 
        tdb = tdb_open(old_name, 0, 0, O_RDWR, 0);
117
 
        if (!tdb) {
118
 
                printf("Failed to open %s\n", old_name);
119
 
                free(tmp_name);
120
 
                return 1;
121
 
        }
122
 
 
123
 
        /* create the new tdb */
124
 
        unlink(tmp_name);
125
 
        tdb_new = tdb_open(tmp_name, tdb_hash_size(tdb),
126
 
                           TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL, 
127
 
                           st.st_mode & 0777);
128
 
        if (!tdb_new) {
129
 
                perror(tmp_name);
130
 
                free(tmp_name);
131
 
                return 1;
132
 
        }
133
 
 
134
 
        /* lock the old tdb */
135
 
        if (tdb_lockall(tdb) != 0) {
136
 
                fprintf(stderr,"Failed to lock %s\n", old_name);
137
 
                tdb_close(tdb);
138
 
                tdb_close(tdb_new);
139
 
                unlink(tmp_name);
140
 
                free(tmp_name);
141
 
                return 1;
142
 
        }
143
 
 
144
 
        failed = 0;
145
 
 
146
 
        /* traverse and copy */
147
 
        count1 = tdb_traverse(tdb, copy_fn, (void *)tdb_new);
148
 
        if (count1 < 0 || failed) {
149
 
                fprintf(stderr,"failed to copy %s\n", old_name);
150
 
                tdb_close(tdb);
151
 
                tdb_close(tdb_new);
152
 
                unlink(tmp_name);
153
 
                free(tmp_name);
154
 
                return 1;
155
 
        }
156
 
 
157
 
        /* close the old tdb */
158
 
        tdb_close(tdb);
159
 
 
160
 
        /* close the new tdb and re-open read-only */
161
 
        tdb_close(tdb_new);
162
 
        tdb_new = tdb_open(tmp_name, 0, TDB_DEFAULT, O_RDONLY, 0);
163
 
        if (!tdb_new) {
164
 
                fprintf(stderr,"failed to reopen %s\n", tmp_name);
165
 
                unlink(tmp_name);
166
 
                perror(tmp_name);
167
 
                free(tmp_name);
168
 
                return 1;
169
 
        }
170
 
        
171
 
        /* traverse the new tdb to confirm */
172
 
        count2 = tdb_traverse(tdb_new, test_fn, 0);
173
 
        if (count2 != count1) {
174
 
                fprintf(stderr,"failed to copy %s\n", old_name);
175
 
                tdb_close(tdb_new);
176
 
                unlink(tmp_name);
177
 
                free(tmp_name);
178
 
                return 1;
179
 
        }
180
 
 
181
 
        /* make sure the new tdb has reached stable storage */
182
 
        fsync(tdb_fd(tdb_new));
183
 
 
184
 
        /* close the new tdb and rename it to .bak */
185
 
        tdb_close(tdb_new);
186
 
        unlink(new_name);
187
 
        if (rename(tmp_name, new_name) != 0) {
188
 
                perror(new_name);
189
 
                free(tmp_name);
190
 
                return 1;
191
 
        }
192
 
 
193
 
        free(tmp_name);
194
 
 
195
 
        return 0;
196
 
}
197
 
 
198
 
 
199
 
 
200
 
/*
201
 
  verify a tdb and if it is corrupt then restore from *.bak
202
 
*/
203
 
int verify_tdb(const char *fname, const char *bak_name)
204
 
{
205
 
        TDB_CONTEXT *tdb;
206
 
        int count = -1;
207
 
 
208
 
        /* open the tdb */
209
 
        tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
210
 
 
211
 
        /* traverse the tdb, then close it */
212
 
        if (tdb) {
213
 
                count = tdb_traverse(tdb, test_fn, NULL);
214
 
                tdb_close(tdb);
215
 
        }
216
 
 
217
 
        /* count is < 0 means an error */
218
 
        if (count < 0) {
219
 
                printf("restoring %s\n", fname);
220
 
                return backup_tdb(bak_name, fname);
221
 
        }
222
 
 
223
 
        printf("%s : %d records\n", fname, count);
224
 
 
225
 
        return 0;
226
 
}