lib: uid_wrapper: Fix setgroups and syscall detection on a system without native...
[Samba.git] / lib / dbwrap / dbwrap_cache.c
blobc5f7cce61ad463dc0da06774de33432475049e6d
1 /*
2 Unix SMB/CIFS implementation.
3 Cache db contents for parse_record based on seqnum
4 Copyright (C) Volker Lendecke 2012
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "lib/dbwrap/dbwrap.h"
22 #include "lib/dbwrap/dbwrap_private.h"
23 #include "lib/dbwrap/dbwrap_rbt.h"
24 #include "lib/dbwrap/dbwrap_cache.h"
26 struct db_cache_ctx {
27 int seqnum;
28 struct db_context *backing;
29 struct db_context *positive;
30 struct db_context *negative;
33 static bool dbwrap_cache_validate(struct db_cache_ctx *ctx)
35 int backing_seqnum;
37 backing_seqnum = dbwrap_get_seqnum(ctx->backing);
38 if (backing_seqnum == ctx->seqnum) {
39 return true;
42 TALLOC_FREE(ctx->positive);
43 ctx->positive = db_open_rbt(ctx);
44 if (ctx->positive == NULL) {
45 return false;
48 TALLOC_FREE(ctx->negative);
49 ctx->negative = db_open_rbt(ctx);
50 if (ctx->negative == NULL) {
51 return false;
54 ctx->seqnum = backing_seqnum;
55 return true;
58 static NTSTATUS dbwrap_cache_parse_record(
59 struct db_context *db, TDB_DATA key,
60 void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data),
61 void *private_data)
63 struct db_cache_ctx *ctx = talloc_get_type_abort(
64 db->private_data, struct db_cache_ctx);
65 TDB_DATA value;
66 NTSTATUS status;
68 if (!dbwrap_cache_validate(ctx)) {
69 return NT_STATUS_NO_MEMORY;
72 if (dbwrap_exists(ctx->negative, key)) {
73 return NT_STATUS_NOT_FOUND;
75 status = dbwrap_parse_record(ctx->positive, key, parser, private_data);
76 if (NT_STATUS_IS_OK(status)) {
77 return status;
80 status = dbwrap_fetch(ctx->backing, talloc_tos(), key, &value);
82 if (NT_STATUS_IS_OK(status)) {
83 dbwrap_store(ctx->positive, key, value, 0);
84 parser(key, value, private_data);
85 TALLOC_FREE(value.dptr);
86 return NT_STATUS_OK;
89 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
90 char c = '\0';
91 value.dptr = (uint8_t *)&c;
92 value.dsize = sizeof(c);
93 dbwrap_store(ctx->negative, key, value, 0);
94 return NT_STATUS_NOT_FOUND;
96 return status;
99 static struct db_record *dbwrap_cache_fetch_locked(
100 struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
102 struct db_cache_ctx *ctx = talloc_get_type_abort(
103 db->private_data, struct db_cache_ctx);
104 return dbwrap_fetch_locked(ctx->backing, mem_ctx, key);
107 static int dbwrap_cache_traverse(struct db_context *db,
108 int (*f)(struct db_record *rec,
109 void *private_data),
110 void *private_data)
112 struct db_cache_ctx *ctx = talloc_get_type_abort(
113 db->private_data, struct db_cache_ctx);
114 NTSTATUS status;
115 int ret;
116 status = dbwrap_traverse(ctx->backing, f, private_data, &ret);
117 if (!NT_STATUS_IS_OK(status)) {
118 return -1;
120 return ret;
123 static int dbwrap_cache_traverse_read(struct db_context *db,
124 int (*f)(struct db_record *rec,
125 void *private_data),
126 void *private_data)
128 struct db_cache_ctx *ctx = talloc_get_type_abort(
129 db->private_data, struct db_cache_ctx);
130 NTSTATUS status;
131 int ret;
132 status = dbwrap_traverse_read(ctx->backing, f, private_data, &ret);
133 if (!NT_STATUS_IS_OK(status)) {
134 return -1;
136 return ret;
139 static int dbwrap_cache_get_seqnum(struct db_context *db)
141 struct db_cache_ctx *ctx = talloc_get_type_abort(
142 db->private_data, struct db_cache_ctx);
143 return dbwrap_get_seqnum(ctx->backing);
146 static int dbwrap_cache_transaction_start(struct db_context *db)
148 struct db_cache_ctx *ctx = talloc_get_type_abort(
149 db->private_data, struct db_cache_ctx);
150 return dbwrap_transaction_start(ctx->backing);
153 static int dbwrap_cache_transaction_commit(struct db_context *db)
155 struct db_cache_ctx *ctx = talloc_get_type_abort(
156 db->private_data, struct db_cache_ctx);
157 return dbwrap_transaction_commit(ctx->backing);
160 static int dbwrap_cache_transaction_cancel(struct db_context *db)
162 struct db_cache_ctx *ctx = talloc_get_type_abort(
163 db->private_data, struct db_cache_ctx);
164 return dbwrap_transaction_cancel(ctx->backing);
167 static int dbwrap_cache_exists(struct db_context *db, TDB_DATA key)
169 struct db_cache_ctx *ctx = talloc_get_type_abort(
170 db->private_data, struct db_cache_ctx);
172 if (ctx->positive && dbwrap_exists(ctx->positive, key)) {
173 return true;
175 if (ctx->negative && dbwrap_exists(ctx->negative, key)) {
176 return false;
178 return dbwrap_exists(ctx->backing, key);
181 static void dbwrap_cache_id(struct db_context *db, const uint8_t **id,
182 size_t *idlen)
184 struct db_cache_ctx *ctx = talloc_get_type_abort(
185 db->private_data, struct db_cache_ctx);
186 dbwrap_db_id(ctx->backing, id, idlen);
189 struct db_context *db_open_cache(TALLOC_CTX *mem_ctx,
190 struct db_context *backing)
192 struct db_context *db;
193 struct db_cache_ctx *ctx;
195 db = talloc_zero(mem_ctx, struct db_context);
196 if (db == NULL) {
197 return NULL;
199 ctx = talloc_zero(db, struct db_cache_ctx);
200 if (ctx == NULL) {
201 TALLOC_FREE(db);
202 return NULL;
205 ctx->seqnum = -1;
206 ctx->backing = talloc_move(ctx, &backing);
207 db->private_data = ctx;
208 if (!dbwrap_cache_validate(ctx)) {
209 TALLOC_FREE(db);
210 return NULL;
213 db->fetch_locked = dbwrap_cache_fetch_locked;
214 db->traverse = dbwrap_cache_traverse;
215 db->traverse_read = dbwrap_cache_traverse_read;
216 db->get_seqnum = dbwrap_cache_get_seqnum;
217 db->transaction_start = dbwrap_cache_transaction_start;
218 db->transaction_commit = dbwrap_cache_transaction_commit;
219 db->transaction_cancel = dbwrap_cache_transaction_cancel;
220 db->parse_record = dbwrap_cache_parse_record;
221 db->exists = dbwrap_cache_exists;
222 db->id = dbwrap_cache_id;
223 db->name = dbwrap_name(ctx->backing);
224 db->hash_size = dbwrap_hash_size(ctx->backing);
225 return db;