tdb/common/tdbback.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    low level tdb backup and restore utility
00004    Copyright (C) Andrew Tridgell              2002
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #ifdef STANDALONE
00022 #if HAVE_CONFIG_H
00023 #include <config.h>
00024 #endif
00025 
00026 #include <errno.h>
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <fcntl.h>
00030 #include <unistd.h>
00031 #include <string.h>
00032 #include <fcntl.h>
00033 #include <time.h>
00034 #include <sys/mman.h>
00035 
00036 #include <sys/stat.h>
00037 #include <sys/time.h>
00038 #include <ctype.h>
00039 #include <signal.h>
00040 
00041 #else
00042 #include "includes.h"
00043 
00044 #ifdef malloc
00045 #undef malloc
00046 #endif
00047                                                                                                                  
00048 #ifdef realloc
00049 #undef realloc
00050 #endif
00051                                                                                                                  
00052 #ifdef calloc
00053 #undef calloc
00054 #endif
00055 
00056 #endif
00057 
00058 #include "tdb.h"
00059 
00060 static int failed;
00061 
00062 char *add_suffix(const char *name, const char *suffix)
00063 {
00064         char *ret;
00065         int len = strlen(name) + strlen(suffix) + 1;
00066         ret = (char *)malloc(len);
00067         if (!ret) {
00068                 fprintf(stderr,"Out of memory!\n");
00069                 exit(1);
00070         }
00071         snprintf(ret, len, "%s%s", name, suffix);
00072         return ret;
00073 }
00074 
00075 static int copy_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
00076 {
00077         TDB_CONTEXT *tdb_new = (TDB_CONTEXT *)state;
00078 
00079         if (tdb_store(tdb_new, key, dbuf, TDB_INSERT) != 0) {
00080                 fprintf(stderr,"Failed to insert into %s\n", tdb_name(tdb_new));
00081                 failed = 1;
00082                 return 1;
00083         }
00084         return 0;
00085 }
00086 
00087 
00088 static int test_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
00089 {
00090         return 0;
00091 }
00092 
00093 /*
00094   carefully backup a tdb, validating the contents and
00095   only doing the backup if its OK
00096   this function is also used for restore
00097 */
00098 int backup_tdb(const char *old_name, const char *new_name, int hash_size)
00099 {
00100         TDB_CONTEXT *tdb;
00101         TDB_CONTEXT *tdb_new;
00102         char *tmp_name;
00103         struct stat st;
00104         int count1, count2;
00105 
00106         tmp_name = add_suffix(new_name, ".tmp");
00107 
00108         /* stat the old tdb to find its permissions */
00109         if (stat(old_name, &st) != 0) {
00110                 perror(old_name);
00111                 free(tmp_name);
00112                 return 1;
00113         }
00114 
00115         /* open the old tdb */
00116         tdb = tdb_open(old_name, 0, 0, O_RDWR, 0);
00117         if (!tdb) {
00118                 printf("Failed to open %s\n", old_name);
00119                 free(tmp_name);
00120                 return 1;
00121         }
00122 
00123         /* create the new tdb */
00124         unlink(tmp_name);
00125         tdb_new = tdb_open(tmp_name,
00126                            hash_size ? hash_size : tdb_hash_size(tdb),
00127                            TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL, 
00128                            st.st_mode & 0777);
00129         if (!tdb_new) {
00130                 perror(tmp_name);
00131                 free(tmp_name);
00132                 return 1;
00133         }
00134 
00135         /* lock the old tdb */
00136         if (tdb_lockall(tdb) != 0) {
00137                 fprintf(stderr,"Failed to lock %s\n", old_name);
00138                 tdb_close(tdb);
00139                 tdb_close(tdb_new);
00140                 unlink(tmp_name);
00141                 free(tmp_name);
00142                 return 1;
00143         }
00144 
00145         failed = 0;
00146 
00147         /* traverse and copy */
00148         count1 = tdb_traverse(tdb, copy_fn, (void *)tdb_new);
00149         if (count1 < 0 || failed) {
00150                 fprintf(stderr,"failed to copy %s\n", old_name);
00151                 tdb_close(tdb);
00152                 tdb_close(tdb_new);
00153                 unlink(tmp_name);
00154                 free(tmp_name);
00155                 return 1;
00156         }
00157 
00158         /* close the old tdb */
00159         tdb_close(tdb);
00160 
00161         /* close the new tdb and re-open read-only */
00162         tdb_close(tdb_new);
00163         tdb_new = tdb_open(tmp_name, 0, TDB_DEFAULT, O_RDONLY, 0);
00164         if (!tdb_new) {
00165                 fprintf(stderr,"failed to reopen %s\n", tmp_name);
00166                 unlink(tmp_name);
00167                 perror(tmp_name);
00168                 free(tmp_name);
00169                 return 1;
00170         }
00171         
00172         /* traverse the new tdb to confirm */
00173         count2 = tdb_traverse(tdb_new, test_fn, 0);
00174         if (count2 != count1) {
00175                 fprintf(stderr,"failed to copy %s\n", old_name);
00176                 tdb_close(tdb_new);
00177                 unlink(tmp_name);
00178                 free(tmp_name);
00179                 return 1;
00180         }
00181 
00182         /* make sure the new tdb has reached stable storage */
00183         fsync(tdb_fd(tdb_new));
00184 
00185         /* close the new tdb and rename it to .bak */
00186         tdb_close(tdb_new);
00187         unlink(new_name);
00188         if (rename(tmp_name, new_name) != 0) {
00189                 perror(new_name);
00190                 free(tmp_name);
00191                 return 1;
00192         }
00193 
00194         free(tmp_name);
00195 
00196         return 0;
00197 }
00198 
00199 
00200 
00201 /*
00202   verify a tdb and if it is corrupt then restore from *.bak
00203 */
00204 int verify_tdb(const char *fname, const char *bak_name)
00205 {
00206         TDB_CONTEXT *tdb;
00207         int count = -1;
00208 
00209         /* open the tdb */
00210         tdb = tdb_open(fname, 0, 0, O_RDONLY, 0);
00211 
00212         /* traverse the tdb, then close it */
00213         if (tdb) {
00214                 count = tdb_traverse(tdb, test_fn, NULL);
00215                 tdb_close(tdb);
00216         }
00217 
00218         /* count is < 0 means an error */
00219         if (count < 0) {
00220                 printf("restoring %s\n", fname);
00221                 return backup_tdb(bak_name, fname, 0);
00222         }
00223 
00224         printf("%s : %d records\n", fname, count);
00225 
00226         return 0;
00227 }

Sambaに対してSat Aug 29 21:23:26 2009に生成されました。  doxygen 1.4.7