1a207fa66916a33d5305da93f114d7ebf106be35
[metze/samba/wip.git] / source3 / lib / tdb_validate.h
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * A general tdb content validation mechanism
5  *
6  * Copyright (C) Michael Adam      2007
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #ifndef __TDB_VALIDATE_H__
23 #define __TDB_VALIDATE_H__
24
25 #include "includes.h"
26
27 /**
28  * Flag field for keeping track of the status of a validation.
29  */
30 struct tdb_validation_status {
31         bool tdb_error;
32         bool bad_freelist;
33         bool bad_entry;
34         bool unknown_key;
35         bool success;
36 };
37
38 /**
39  * Callback function type for the validation mechanism.
40  */
41 typedef int (*tdb_validate_data_func)(TDB_CONTEXT *the_tdb, TDB_DATA kbuf,
42                                       TDB_DATA dbuf, void *state);
43
44 /**
45  * tdb validation function.
46  * returns 0 if tdb is ok, != 0 if it isn't.
47  * this function expects an opened tdb.
48  */
49 int tdb_validate(struct tdb_context *tdb,
50                  tdb_validate_data_func validate_fn);
51
52 /**
53  * tdb validation function.
54  * returns 0 if tdb is ok, != 0 if it isn't.
55  * This is a wrapper around the actual validation function that
56  * opens and closes the tdb.
57  */
58 int tdb_validate_open(const char *tdb_path,
59                       tdb_validate_data_func validate_fn);
60
61 /**
62  * validation function with backup handling:
63  *
64  *  - calls tdb_validate
65  *  - if the tdb is ok, create a backup "name.bak", possibly moving
66  *    existing backup to name.bak.old,
67  *    return 0 (success) even if the backup fails
68  *  - if the tdb is corrupt:
69  *    - move the tdb to "name.corrupt"
70  *    - check if there is valid backup.
71  *      if so, restore the backup.
72  *      if restore is successful, return 0 (success),
73  *    - otherwise return -1 (failure)
74  */
75 int tdb_validate_and_backup(const char *tdb_path,
76                             tdb_validate_data_func validate_fn);
77
78 #endif /* __TDB_VALIDATE_H__ */