tdb/common/freelistcheck.c

説明を見る。
00001 /*
00002    Unix SMB/CIFS implementation.
00003 
00004    trivial database library
00005 
00006    Copyright (C) Jeremy Allison                    2006
00007 
00008      ** NOTE! The following LGPL license applies to the tdb
00009      ** library. This does NOT imply that all of Samba is released
00010      ** under the LGPL
00011 
00012    This library is free software; you can redistribute it and/or
00013    modify it under the terms of the GNU Lesser General Public
00014    License as published by the Free Software Foundation; either
00015    version 2 of the License, or (at your option) any later version.
00016 
00017    This library is distributed in the hope that it will be useful,
00018    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020    Lesser General Public License for more details.
00021 
00022    You should have received a copy of the GNU Lesser General Public
00023    License along with this library; if not, write to the Free Software
00024    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 */
00026 
00027 #include "tdb_private.h"
00028 
00029 /* Check the freelist is good and contains no loops.
00030    Very memory intensive - only do this as a consistency
00031    checker. Heh heh - uses an in memory tdb as the storage
00032    for the "seen" record list. For some reason this strikes
00033    me as extremely clever as I don't have to write another tree
00034    data structure implementation :-).
00035  */
00036 
00037 static int seen_insert(struct tdb_context *mem_tdb, tdb_off_t rec_ptr)
00038 {
00039         TDB_DATA key, data;
00040 
00041         memset(&data, '\0', sizeof(data));
00042         key.dptr = (char *)&rec_ptr;
00043         key.dsize = sizeof(rec_ptr);
00044         return tdb_store(mem_tdb, key, data, TDB_INSERT);
00045 }
00046 
00047 int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries)
00048 {
00049         struct tdb_context *mem_tdb = NULL;
00050         struct list_struct rec;
00051         tdb_off_t rec_ptr, last_ptr;
00052         int ret = -1;
00053 
00054         *pnum_entries = 0;
00055 
00056         mem_tdb = tdb_open("flval", tdb->header.hash_size,
00057                                 TDB_INTERNAL, O_RDWR, 0600);
00058         if (!mem_tdb) {
00059                 return -1;
00060         }
00061 
00062         if (tdb_lock(tdb, -1, F_WRLCK) == -1) {
00063                 tdb_close(mem_tdb);
00064                 return 0;
00065         }
00066 
00067         last_ptr = FREELIST_TOP;
00068 
00069         /* Store the FREELIST_TOP record. */
00070         if (seen_insert(mem_tdb, last_ptr) == -1) {
00071                 ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
00072                 goto fail;
00073         }
00074 
00075         /* read in the freelist top */
00076         if (tdb_ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1) {
00077                 goto fail;
00078         }
00079 
00080         while (rec_ptr) {
00081 
00082                 /* If we can't store this record (we've seen it
00083                    before) then the free list has a loop and must
00084                    be corrupt. */
00085 
00086                 if (seen_insert(mem_tdb, rec_ptr)) {
00087                         ret = TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
00088                         goto fail;
00089                 }
00090 
00091                 if (rec_free_read(tdb, rec_ptr, &rec) == -1) {
00092                         goto fail;
00093                 }
00094 
00095                 /* move to the next record */
00096                 last_ptr = rec_ptr;
00097                 rec_ptr = rec.next;
00098                 *pnum_entries += 1;
00099         }
00100 
00101         ret = 0;
00102 
00103   fail:
00104 
00105         tdb_close(mem_tdb);
00106         tdb_unlock(tdb, -1, F_WRLCK);
00107         return ret;
00108 }

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