tdb/include/tdbback.h

ソースコードを見る。

関数

char * add_suffix (const char *name, const char *suffix)
int backup_tdb (const char *old_name, const char *new_name, int hash_size)
int verify_tdb (const char *fname, const char *bak_name)


関数

char* add_suffix ( const char *  name,
const char *  suffix 
)

tdbback.c62 行で定義されています。

参照先 fprintf()lensnprintf().

参照元 backup_tdb()main().

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 }

int backup_tdb ( const char *  old_name,
const char *  new_name,
int  hash_size 
)

tdbback.c98 行で定義されています。

参照先 add_suffix()copy_fn()failedfprintf()printf()tdbtdb_close()tdb_fd()tdb_hash_size()tdb_lockall()tdb_open()tdb_traverse()test_fn().

参照元 idmap_tdb_upgrade()main()verify_tdb().

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 }

int verify_tdb ( const char *  fname,
const char *  bak_name 
)

tdbback.c204 行で定義されています。

参照先 backup_tdb()printf()tdbtdb_close()tdb_open()tdb_traverse()test_fn().

参照元 main().

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:24:31 2009に生成されました。  doxygen 1.4.7