s3:dbwrap: move dbwrap_traverse() to dbwrap.c, the core of the dbrwap subsystem
[Samba/gebeck_regimport.git] / source3 / lib / dbwrap / dbwrap.c
blobf0d61a9a2f292dd2879b0d7eb46e751e7e5b5522
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
6 Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
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.
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.
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/>.
22 #include "includes.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_private.h"
25 #include "util_tdb.h"
28 * Fall back using fetch_locked if no genuine fetch operation is provided
31 int dbwrap_fallback_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
32 TDB_DATA key, TDB_DATA *data)
34 struct db_record *rec;
36 if (!(rec = db->fetch_locked(db, mem_ctx, key))) {
37 return -1;
40 data->dsize = rec->value.dsize;
41 data->dptr = talloc_move(mem_ctx, &rec->value.dptr);
42 TALLOC_FREE(rec);
43 return 0;
47 * Fall back using fetch if no genuine parse operation is provided
50 int dbwrap_fallback_parse_record(struct db_context *db, TDB_DATA key,
51 int (*parser)(TDB_DATA key,
52 TDB_DATA data,
53 void *private_data),
54 void *private_data)
56 TDB_DATA data;
57 int res;
59 res = db->fetch(db, talloc_tos(), key, &data);
60 if (res != 0) {
61 return res;
64 res = parser(key, data, private_data);
65 TALLOC_FREE(data.dptr);
66 return res;
70 TDB_DATA dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
71 TDB_DATA key)
73 TDB_DATA result;
75 if (db->fetch(db, mem_ctx, key, &result) != 0) {
76 return make_tdb_data(NULL, 0);
79 return result;
82 NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
83 TDB_DATA data, int flags)
85 struct db_record *rec;
86 NTSTATUS status;
88 rec = db->fetch_locked(db, talloc_tos(), key);
89 if (rec == NULL) {
90 return NT_STATUS_NO_MEMORY;
93 status = rec->store(rec, data, flags);
94 TALLOC_FREE(rec);
95 return status;
98 NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
100 struct db_record *rec;
101 NTSTATUS status;
103 rec = db->fetch_locked(db, talloc_tos(), key);
104 if (rec == NULL) {
105 return NT_STATUS_NO_MEMORY;
107 status = rec->delete_rec(rec);
108 TALLOC_FREE(rec);
109 return status;
112 NTSTATUS dbwrap_traverse(struct db_context *db,
113 int (*f)(struct db_record*, void*),
114 void *private_data)
116 int ret = db->traverse(db, f, private_data);
117 return (ret < 0) ? NT_STATUS_INTERNAL_DB_CORRUPTION : NT_STATUS_OK;