lib: Simplify gencache_del
[Samba.git] / source3 / lib / gencache.c
blobb0242f6c35a004f1ed3e18a86380d69cfc7d6ca5
1 /*
2 Unix SMB/CIFS implementation.
4 Generic, persistent and shared between processes cache mechanism for use
5 by various parts of the Samba code
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Volker Lendecke 2009
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "system/glob.h"
27 #include "util_tdb.h"
28 #include "tdb_wrap/tdb_wrap.h"
29 #include "../lib/util/memcache.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_TDB
34 #define CACHE_DATA_FMT "%12u/"
36 static struct tdb_wrap *cache;
37 static struct tdb_wrap *cache_notrans;
38 static int cache_notrans_seqnum;
40 /**
41 * @file gencache.c
42 * @brief Generic, persistent and shared between processes cache mechanism
43 * for use by various parts of the Samba code
45 **/
48 /**
49 * Cache initialisation function. Opens cache tdb file or creates
50 * it if does not exist.
52 * @return true on successful initialisation of the cache or
53 * false on failure
54 **/
56 static bool gencache_init(void)
58 char* cache_fname = NULL;
59 int open_flags = O_RDWR|O_CREAT;
61 /* skip file open if it's already opened */
62 if (cache) {
63 return true;
66 cache_fname = cache_path("gencache.tdb");
67 if (cache_fname == NULL) {
68 return false;
71 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
73 cache = tdb_wrap_open(NULL, cache_fname, 0,
74 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
75 open_flags, 0644);
76 if (cache) {
77 int ret;
78 ret = tdb_check(cache->tdb, NULL, NULL);
79 if (ret != 0) {
80 TALLOC_FREE(cache);
83 * Retry with CLEAR_IF_FIRST.
85 * Warning: Converting this to dbwrap won't work
86 * directly. gencache.c does transactions on this tdb,
87 * and dbwrap forbids this for CLEAR_IF_FIRST
88 * databases. tdb does allow transactions on
89 * CLEAR_IF_FIRST databases, so lets use it here to
90 * clean up a broken database.
92 cache = tdb_wrap_open(NULL, cache_fname, 0,
93 TDB_DEFAULT|
94 TDB_INCOMPATIBLE_HASH|
95 TDB_CLEAR_IF_FIRST,
96 open_flags, 0644);
100 if (!cache && (errno == EACCES)) {
101 open_flags = O_RDONLY;
102 cache = tdb_wrap_open(NULL, cache_fname, 0,
103 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
104 open_flags, 0644);
105 if (cache) {
106 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
109 TALLOC_FREE(cache_fname);
111 if (!cache) {
112 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
113 return false;
116 cache_fname = lock_path("gencache_notrans.tdb");
117 if (cache_fname == NULL) {
118 TALLOC_FREE(cache);
119 return false;
122 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
124 cache_notrans = tdb_wrap_open(NULL, cache_fname, 0,
125 TDB_CLEAR_IF_FIRST|
126 TDB_INCOMPATIBLE_HASH|
127 TDB_SEQNUM|
128 TDB_NOSYNC|
129 TDB_MUTEX_LOCKING,
130 open_flags, 0644);
131 if (cache_notrans == NULL) {
132 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
133 strerror(errno)));
134 TALLOC_FREE(cache_fname);
135 TALLOC_FREE(cache);
136 return false;
138 TALLOC_FREE(cache_fname);
140 return true;
143 static TDB_DATA last_stabilize_key(void)
145 TDB_DATA result;
146 result.dptr = discard_const_p(uint8_t, "@LAST_STABILIZED");
147 result.dsize = 17;
148 return result;
151 struct gencache_have_val_state {
152 time_t new_timeout;
153 const DATA_BLOB *data;
154 bool gotit;
157 static void gencache_have_val_parser(time_t old_timeout, DATA_BLOB data,
158 void *private_data)
160 struct gencache_have_val_state *state =
161 (struct gencache_have_val_state *)private_data;
162 time_t now = time(NULL);
163 int cache_time_left, new_time_left, additional_time;
166 * Excuse the many variables, but these time calculations are
167 * confusing to me. We do not want to write to gencache with a
168 * possibly expensive transaction if we are about to write the same
169 * value, just extending the remaining timeout by less than 10%.
172 cache_time_left = old_timeout - now;
173 if (cache_time_left <= 0) {
175 * timed out, write new value
177 return;
180 new_time_left = state->new_timeout - now;
181 if (new_time_left <= 0) {
183 * Huh -- no new timeout?? Write it.
185 return;
188 if (new_time_left < cache_time_left) {
190 * Someone wants to shorten the timeout. Let it happen.
192 return;
196 * By how much does the new timeout extend the remaining cache time?
198 additional_time = new_time_left - cache_time_left;
200 if (additional_time * 10 < 0) {
202 * Integer overflow. We extend by so much that we have to write it.
204 return;
208 * The comparison below is essentially equivalent to
210 * new_time_left > cache_time_left * 1.10
212 * but without floating point calculations.
215 if (additional_time * 10 > cache_time_left) {
217 * We extend the cache timeout by more than 10%. Do it.
219 return;
223 * Now the more expensive data compare.
225 if (data_blob_cmp(state->data, &data) != 0) {
227 * Write a new value. Certainly do it.
229 return;
233 * Extending the timeout by less than 10% for the same cache value is
234 * not worth the trouble writing a value into gencache under a
235 * possibly expensive transaction.
237 state->gotit = true;
240 static bool gencache_have_val(const char *keystr, const DATA_BLOB *data,
241 time_t timeout)
243 struct gencache_have_val_state state;
245 state.new_timeout = timeout;
246 state.data = data;
247 state.gotit = false;
249 if (!gencache_parse(keystr, gencache_have_val_parser, &state)) {
250 return false;
252 return state.gotit;
255 static int last_stabilize_parser(TDB_DATA key, TDB_DATA data,
256 void *private_data)
258 time_t *last_stabilize = private_data;
260 if ((data.dsize != 0) && (data.dptr[data.dsize-1] == '\0')) {
261 *last_stabilize = atoi((char *)data.dptr);
263 return 0;
267 * Set an entry in the cache file. If there's no such
268 * one, then add it.
270 * @param keystr string that represents a key of this entry
271 * @param blob DATA_BLOB value being cached
272 * @param timeout time when the value is expired
274 * @retval true when entry is successfully stored
275 * @retval false on failure
278 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
279 time_t timeout)
281 int ret;
282 fstring hdr;
283 int hdr_len;
284 char* val;
285 time_t last_stabilize;
286 static int writecount;
288 if (tdb_data_cmp(string_term_tdb_data(keystr),
289 last_stabilize_key()) == 0) {
290 DEBUG(10, ("Can't store %s as a key\n", keystr));
291 return false;
294 if ((keystr == NULL) || (blob == NULL)) {
295 return false;
298 if (!gencache_init()) {
299 return false;
302 if (gencache_have_val(keystr, blob, timeout)) {
303 DEBUG(10, ("Did not store value for %s, we already got it\n",
304 keystr));
305 return true;
308 hdr_len = fstr_sprintf(hdr, CACHE_DATA_FMT, (int)timeout);
310 if (hdr_len == -1) {
311 return false;
313 if ((blob->length + (size_t)hdr_len) < blob->length) {
314 return false;
317 val = talloc_array(talloc_tos(), char, hdr_len + blob->length);
318 if (val == NULL) {
319 return false;
322 memcpy(val, hdr, hdr_len);
323 memcpy(val+hdr_len, blob->data, blob->length);
325 DEBUG(10, ("Adding cache entry with key=[%s] and timeout="
326 "[%s] (%d seconds %s)\n", keystr,
327 timestring(talloc_tos(), timeout),
328 (int)(timeout - time(NULL)),
329 timeout > time(NULL) ? "ahead" : "in the past"));
331 ret = tdb_store_bystring(
332 cache_notrans->tdb, keystr,
333 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
335 TALLOC_FREE(val);
337 if (ret != 0) {
338 return false;
342 * Every 100 writes within a single process, stabilize the cache with
343 * a transaction. This is done to prevent a single transaction to
344 * become huge and chew lots of memory.
346 writecount += 1;
347 if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
348 gencache_stabilize();
349 writecount = 0;
350 goto done;
354 * Every 5 minutes, call gencache_stabilize() to not let grow
355 * gencache_notrans.tdb too large.
358 last_stabilize = 0;
360 tdb_parse_record(cache_notrans->tdb, last_stabilize_key(),
361 last_stabilize_parser, &last_stabilize);
363 if ((last_stabilize
364 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
365 < time(NULL)) {
366 gencache_stabilize();
369 done:
370 return ret == 0;
373 static void gencache_del_parser(time_t timeout, DATA_BLOB blob,
374 void *private_data)
376 if (timeout != 0) {
377 bool *exists = private_data;
378 *exists = true;
383 * Delete one entry from the cache file.
385 * @param keystr string that represents a key of this entry
387 * @retval true upon successful deletion
388 * @retval false in case of failure
391 bool gencache_del(const char *keystr)
393 TDB_DATA key = string_term_tdb_data(keystr);
394 bool exists = false;
395 bool result = false;
396 int ret;
398 if (keystr == NULL) {
399 return false;
402 if (!gencache_init()) {
403 return false;
406 DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr));
408 ret = tdb_chainlock(cache_notrans->tdb, key);
409 if (ret == -1) {
410 return false;
413 gencache_parse(keystr, gencache_del_parser, &exists);
415 if (exists) {
417 * We delete an element by setting its timeout to
418 * 0. This way we don't have to do a transaction on
419 * gencache.tdb every time we delete an element.
421 result = gencache_set(keystr, "", 0);
424 tdb_chainunlock(cache_notrans->tdb, key);
426 return result;
429 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
431 time_t res;
432 char *endptr;
434 if (val == NULL) {
435 return false;
438 res = strtol(val, &endptr, 10);
440 if ((endptr == NULL) || (*endptr != '/')) {
441 DEBUG(2, ("Invalid gencache data format: %s\n", val));
442 return false;
444 if (pres != NULL) {
445 *pres = res;
447 if (pendptr != NULL) {
448 *pendptr = endptr;
450 return true;
453 struct gencache_parse_state {
454 void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
455 void *private_data;
456 bool is_memcache;
459 static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
461 struct gencache_parse_state *state;
462 DATA_BLOB blob;
463 time_t t;
464 char *endptr;
465 bool ret;
467 if (data.dptr == NULL) {
468 return -1;
470 ret = gencache_pull_timeout((char *)data.dptr, &t, &endptr);
471 if (!ret) {
472 return -1;
474 state = (struct gencache_parse_state *)private_data;
475 blob = data_blob_const(
476 endptr+1, data.dsize - PTR_DIFF(endptr+1, data.dptr));
477 state->parser(t, blob, state->private_data);
479 if (!state->is_memcache) {
480 memcache_add(NULL, GENCACHE_RAM,
481 data_blob_const(key.dptr, key.dsize),
482 data_blob_const(data.dptr, data.dsize));
485 return 0;
488 bool gencache_parse(const char *keystr,
489 void (*parser)(time_t timeout, DATA_BLOB blob,
490 void *private_data),
491 void *private_data)
493 struct gencache_parse_state state;
494 TDB_DATA key = string_term_tdb_data(keystr);
495 DATA_BLOB memcache_val;
496 int ret;
498 if (keystr == NULL) {
499 return false;
501 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
502 return false;
504 if (!gencache_init()) {
505 return false;
508 state.parser = parser;
509 state.private_data = private_data;
511 if (memcache_lookup(NULL, GENCACHE_RAM,
512 data_blob_const(key.dptr, key.dsize),
513 &memcache_val)) {
515 * Make sure that nobody has changed the gencache behind our
516 * back.
518 int current_seqnum = tdb_get_seqnum(cache_notrans->tdb);
519 if (current_seqnum == cache_notrans_seqnum) {
521 * Ok, our memcache is still current, use it without
522 * going to the tdb files.
524 state.is_memcache = true;
525 gencache_parse_fn(key, make_tdb_data(memcache_val.data,
526 memcache_val.length),
527 &state);
528 return true;
530 memcache_flush(NULL, GENCACHE_RAM);
531 cache_notrans_seqnum = current_seqnum;
534 state.is_memcache = false;
536 ret = tdb_parse_record(cache_notrans->tdb, key,
537 gencache_parse_fn, &state);
538 if (ret == 0) {
539 return true;
541 ret = tdb_parse_record(cache->tdb, key, gencache_parse_fn, &state);
542 return (ret == 0);
545 struct gencache_get_data_blob_state {
546 TALLOC_CTX *mem_ctx;
547 DATA_BLOB *blob;
548 time_t timeout;
549 bool result;
552 static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
553 void *private_data)
555 struct gencache_get_data_blob_state *state =
556 (struct gencache_get_data_blob_state *)private_data;
558 if (timeout == 0) {
559 state->result = false;
560 return;
562 state->timeout = timeout;
564 if (state->blob == NULL) {
565 state->result = true;
566 return;
569 *state->blob = data_blob_talloc(state->mem_ctx, blob.data,
570 blob.length);
571 if (state->blob->data == NULL) {
572 state->result = false;
573 return;
575 state->result = true;
579 * Get existing entry from the cache file.
581 * @param keystr string that represents a key of this entry
582 * @param blob DATA_BLOB that is filled with entry's blob
583 * @param timeout pointer to a time_t that is filled with entry's
584 * timeout
586 * @retval true when entry is successfuly fetched
587 * @retval false for failure
590 bool gencache_get_data_blob(const char *keystr, TALLOC_CTX *mem_ctx,
591 DATA_BLOB *blob,
592 time_t *timeout, bool *was_expired)
594 struct gencache_get_data_blob_state state;
595 bool expired = false;
597 state.result = false;
598 state.mem_ctx = mem_ctx;
599 state.blob = blob;
601 if (!gencache_parse(keystr, gencache_get_data_blob_parser, &state)) {
602 goto fail;
604 if (!state.result) {
605 goto fail;
607 if (state.timeout <= time(NULL)) {
609 * We're expired, delete the entry. We can't use gencache_del
610 * here, because that uses gencache_get_data_blob for checking
611 * the existence of a record. We know the thing exists and
612 * directly store an empty value with 0 timeout.
614 gencache_set(keystr, "", 0);
615 expired = true;
616 goto fail;
618 if (timeout) {
619 *timeout = state.timeout;
622 return true;
624 fail:
625 if (was_expired != NULL) {
626 *was_expired = expired;
628 if (state.result && state.blob) {
629 data_blob_free(state.blob);
631 return false;
634 struct stabilize_state {
635 bool written;
637 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
638 void *priv);
640 static int wipe_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
641 void *priv);
644 * Stabilize gencache
646 * Migrate the clear-if-first gencache data to the stable,
647 * transaction-based gencache.tdb
650 bool gencache_stabilize(void)
652 struct stabilize_state state;
653 int res;
654 char *now;
656 if (!gencache_init()) {
657 return false;
660 res = tdb_transaction_start_nonblock(cache->tdb);
661 if (res != 0) {
662 if (tdb_error(cache->tdb) == TDB_ERR_NOLOCK)
665 * Someone else already does the stabilize,
666 * this does not have to be done twice
668 return true;
671 DEBUG(10, ("Could not start transaction on gencache.tdb: "
672 "%s\n", tdb_errorstr(cache->tdb)));
673 return false;
676 res = tdb_lockall(cache_notrans->tdb);
677 if (res != 0) {
678 tdb_transaction_cancel(cache->tdb);
679 DEBUG(10, ("Could not get allrecord lock on "
680 "gencache_notrans.tdb: %s\n",
681 tdb_errorstr(cache_notrans->tdb)));
682 return false;
685 state.written = false;
687 res = tdb_traverse(cache_notrans->tdb, stabilize_fn, &state);
688 if (res < 0) {
689 tdb_unlockall(cache_notrans->tdb);
690 tdb_transaction_cancel(cache->tdb);
691 return false;
694 if (!state.written) {
695 tdb_unlockall(cache_notrans->tdb);
696 tdb_transaction_cancel(cache->tdb);
697 return true;
700 res = tdb_transaction_commit(cache->tdb);
701 if (res != 0) {
702 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
703 "%s\n", tdb_errorstr(cache->tdb)));
704 tdb_unlockall(cache_notrans->tdb);
705 return false;
708 res = tdb_traverse(cache_notrans->tdb, wipe_fn, NULL);
709 if (res < 0) {
710 DEBUG(10, ("tdb_traverse with wipe_fn on gencache_notrans.tdb "
711 "failed: %s\n",
712 tdb_errorstr(cache_notrans->tdb)));
713 tdb_unlockall(cache_notrans->tdb);
714 return false;
717 res = tdb_unlockall(cache_notrans->tdb);
718 if (res != 0) {
719 DEBUG(10, ("tdb_unlockall on gencache.tdb failed: "
720 "%s\n", tdb_errorstr(cache->tdb)));
721 return false;
724 now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
725 if (now != NULL) {
726 tdb_store(cache_notrans->tdb, last_stabilize_key(),
727 string_term_tdb_data(now), 0);
728 TALLOC_FREE(now);
731 return true;
734 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
735 void *priv)
737 struct stabilize_state *state = (struct stabilize_state *)priv;
738 int res;
739 time_t timeout;
741 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
742 return 0;
745 if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
746 DEBUG(10, ("Ignoring invalid entry\n"));
747 return 0;
749 if ((timeout < time(NULL)) || (val.dsize == 0)) {
750 res = tdb_delete(cache->tdb, key);
751 if (res == 0) {
752 state->written = true;
753 } else if (tdb_error(cache->tdb) == TDB_ERR_NOEXIST) {
754 res = 0;
756 } else {
757 res = tdb_store(cache->tdb, key, val, 0);
758 if (res == 0) {
759 state->written = true;
763 if (res != 0) {
764 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
765 tdb_errorstr(cache->tdb)));
766 return -1;
769 return 0;
772 static int wipe_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
773 void *priv)
775 int res;
776 bool ok;
777 time_t timeout;
779 res = tdb_data_cmp(key, last_stabilize_key());
780 if (res == 0) {
781 return 0;
784 ok = gencache_pull_timeout((char *)val.dptr, &timeout, NULL);
785 if (!ok) {
786 DEBUG(10, ("Ignoring invalid entry\n"));
787 return 0;
790 res = tdb_delete(tdb, key);
791 if (res != 0) {
792 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
793 "%s\n", tdb_errorstr(cache_notrans->tdb)));
794 return -1;
797 return 0;
802 * Get existing entry from the cache file.
804 * @param keystr string that represents a key of this entry
805 * @param valstr buffer that is allocated and filled with the entry value
806 * buffer's disposing must be done outside
807 * @param timeout pointer to a time_t that is filled with entry's
808 * timeout
810 * @retval true when entry is successfuly fetched
811 * @retval false for failure
814 bool gencache_get(const char *keystr, TALLOC_CTX *mem_ctx, char **value,
815 time_t *ptimeout)
817 DATA_BLOB blob;
818 bool ret = false;
820 ret = gencache_get_data_blob(keystr, mem_ctx, &blob, ptimeout, NULL);
821 if (!ret) {
822 return false;
824 if ((blob.data == NULL) || (blob.length == 0)) {
825 data_blob_free(&blob);
826 return false;
828 if (blob.data[blob.length-1] != '\0') {
829 /* Not NULL terminated, can't be a string */
830 data_blob_free(&blob);
831 return false;
833 if (value) {
835 * talloc_move generates a type-punned warning here. As we
836 * leave the function immediately, do a simple talloc_steal.
838 *value = (char *)talloc_steal(mem_ctx, blob.data);
839 return true;
841 data_blob_free(&blob);
842 return true;
846 * Set an entry in the cache file. If there's no such
847 * one, then add it.
849 * @param keystr string that represents a key of this entry
850 * @param value text representation value being cached
851 * @param timeout time when the value is expired
853 * @retval true when entry is successfuly stored
854 * @retval false on failure
857 bool gencache_set(const char *keystr, const char *value, time_t timeout)
859 DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
860 return gencache_set_data_blob(keystr, &blob, timeout);
863 struct gencache_iterate_blobs_state {
864 void (*fn)(const char *key, DATA_BLOB value,
865 time_t timeout, void *private_data);
866 const char *pattern;
867 void *private_data;
868 bool in_persistent;
871 static int gencache_iterate_blobs_fn(struct tdb_context *tdb, TDB_DATA key,
872 TDB_DATA data, void *priv)
874 struct gencache_iterate_blobs_state *state =
875 (struct gencache_iterate_blobs_state *)priv;
876 char *keystr;
877 char *free_key = NULL;
878 time_t timeout;
879 char *endptr;
881 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
882 return 0;
884 if (state->in_persistent && tdb_exists(cache_notrans->tdb, key)) {
885 return 0;
888 if (key.dptr[key.dsize-1] == '\0') {
889 keystr = (char *)key.dptr;
890 } else {
891 /* ensure 0-termination */
892 keystr = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
893 free_key = keystr;
894 if (keystr == NULL) {
895 goto done;
899 if (!gencache_pull_timeout((char *)data.dptr, &timeout, &endptr)) {
900 goto done;
902 endptr += 1;
904 if (timeout == 0) {
905 /* delete marker */
906 goto done;
909 if (fnmatch(state->pattern, keystr, 0) != 0) {
910 goto done;
913 DEBUG(10, ("Calling function with arguments "
914 "(key=[%s], timeout=[%s])\n",
915 keystr, timestring(talloc_tos(), timeout)));
917 state->fn(keystr,
918 data_blob_const(endptr,
919 data.dsize - PTR_DIFF(endptr, data.dptr)),
920 timeout, state->private_data);
922 done:
923 TALLOC_FREE(free_key);
924 return 0;
927 void gencache_iterate_blobs(void (*fn)(const char *key, DATA_BLOB value,
928 time_t timeout, void *private_data),
929 void *private_data, const char *pattern)
931 struct gencache_iterate_blobs_state state;
933 if ((fn == NULL) || (pattern == NULL) || !gencache_init()) {
934 return;
937 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern));
939 state.fn = fn;
940 state.pattern = pattern;
941 state.private_data = private_data;
943 state.in_persistent = false;
944 tdb_traverse(cache_notrans->tdb, gencache_iterate_blobs_fn, &state);
946 state.in_persistent = true;
947 tdb_traverse(cache->tdb, gencache_iterate_blobs_fn, &state);
951 * Iterate through all entries which key matches to specified pattern
953 * @param fn pointer to the function that will be supplied with each single
954 * matching cache entry (key, value and timeout) as an arguments
955 * @param data void pointer to an arbitrary data that is passed directly to the fn
956 * function on each call
957 * @param keystr_pattern pattern the existing entries' keys are matched to
961 struct gencache_iterate_state {
962 void (*fn)(const char *key, const char *value, time_t timeout,
963 void *priv);
964 void *private_data;
967 static void gencache_iterate_fn(const char *key, DATA_BLOB value,
968 time_t timeout, void *private_data)
970 struct gencache_iterate_state *state =
971 (struct gencache_iterate_state *)private_data;
972 char *valstr;
973 char *free_val = NULL;
975 if (value.data[value.length-1] == '\0') {
976 valstr = (char *)value.data;
977 } else {
978 /* ensure 0-termination */
979 valstr = talloc_strndup(talloc_tos(), (char *)value.data, value.length);
980 free_val = valstr;
981 if (valstr == NULL) {
982 goto done;
986 DEBUG(10, ("Calling function with arguments "
987 "(key=[%s], value=[%s], timeout=[%s])\n",
988 key, valstr, timestring(talloc_tos(), timeout)));
990 state->fn(key, valstr, timeout, state->private_data);
992 done:
994 TALLOC_FREE(free_val);
997 void gencache_iterate(void (*fn)(const char *key, const char *value,
998 time_t timeout, void *dptr),
999 void *private_data, const char *pattern)
1001 struct gencache_iterate_state state;
1003 if (fn == NULL) {
1004 return;
1006 state.fn = fn;
1007 state.private_data = private_data;
1008 gencache_iterate_blobs(gencache_iterate_fn, &state, pattern);