gencache: don't fail gencache_stabilize if there were records to delete.
[Samba.git] / source3 / lib / gencache.c
blobd9cc08195d487065b9cd5a3c1c3d8896afd68a4d
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 TIMEOUT_LEN 12
35 #define CACHE_DATA_FMT "%12u/"
36 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
37 #define BLOB_TYPE "DATA_BLOB"
38 #define BLOB_TYPE_LEN 9
40 static struct tdb_wrap *cache;
41 static struct tdb_wrap *cache_notrans;
42 static int cache_notrans_seqnum;
44 /**
45 * @file gencache.c
46 * @brief Generic, persistent and shared between processes cache mechanism
47 * for use by various parts of the Samba code
49 **/
52 /**
53 * Cache initialisation function. Opens cache tdb file or creates
54 * it if does not exist.
56 * @return true on successful initialisation of the cache or
57 * false on failure
58 **/
60 static bool gencache_init(void)
62 char* cache_fname = NULL;
63 int open_flags = O_RDWR|O_CREAT;
65 /* skip file open if it's already opened */
66 if (cache) return True;
68 cache_fname = cache_path("gencache.tdb");
69 if (cache_fname == NULL) {
70 return false;
73 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
75 cache = tdb_wrap_open(NULL, cache_fname, 0,
76 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
77 open_flags, 0644);
78 if (cache) {
79 int ret;
80 ret = tdb_check(cache->tdb, NULL, NULL);
81 if (ret != 0) {
82 TALLOC_FREE(cache);
85 * Retry with CLEAR_IF_FIRST.
87 * Warning: Converting this to dbwrap won't work
88 * directly. gencache.c does transactions on this tdb,
89 * and dbwrap forbids this for CLEAR_IF_FIRST
90 * databases. tdb does allow transactions on
91 * CLEAR_IF_FIRST databases, so lets use it here to
92 * clean up a broken database.
94 cache = tdb_wrap_open(NULL, cache_fname, 0,
95 TDB_DEFAULT|
96 TDB_INCOMPATIBLE_HASH|
97 TDB_CLEAR_IF_FIRST,
98 open_flags, 0644);
102 if (!cache && (errno == EACCES)) {
103 open_flags = O_RDONLY;
104 cache = tdb_wrap_open(NULL, cache_fname, 0,
105 TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
106 open_flags, 0644);
107 if (cache) {
108 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
111 TALLOC_FREE(cache_fname);
113 if (!cache) {
114 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
115 return False;
118 cache_fname = lock_path("gencache_notrans.tdb");
119 if (cache_fname == NULL) {
120 TALLOC_FREE(cache);
121 return false;
124 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
126 cache_notrans = tdb_wrap_open(NULL, cache_fname, 0,
127 TDB_CLEAR_IF_FIRST|
128 TDB_INCOMPATIBLE_HASH|
129 TDB_SEQNUM|
130 TDB_NOSYNC|
131 TDB_MUTEX_LOCKING,
132 open_flags, 0644);
133 if (cache_notrans == NULL) {
134 DEBUG(5, ("Opening %s failed: %s\n", cache_fname,
135 strerror(errno)));
136 TALLOC_FREE(cache_fname);
137 TALLOC_FREE(cache);
138 return false;
140 TALLOC_FREE(cache_fname);
142 return True;
145 static TDB_DATA last_stabilize_key(void)
147 TDB_DATA result;
148 result.dptr = discard_const_p(uint8_t, "@LAST_STABILIZED");
149 result.dsize = 17;
150 return result;
153 struct gencache_have_val_state {
154 time_t new_timeout;
155 const DATA_BLOB *data;
156 bool gotit;
159 static void gencache_have_val_parser(time_t old_timeout, DATA_BLOB data,
160 void *private_data)
162 struct gencache_have_val_state *state =
163 (struct gencache_have_val_state *)private_data;
164 time_t now = time(NULL);
165 int cache_time_left, new_time_left, additional_time;
168 * Excuse the many variables, but these time calculations are
169 * confusing to me. We do not want to write to gencache with a
170 * possibly expensive transaction if we are about to write the same
171 * value, just extending the remaining timeout by less than 10%.
174 cache_time_left = old_timeout - now;
175 if (cache_time_left <= 0) {
177 * timed out, write new value
179 return;
182 new_time_left = state->new_timeout - now;
183 if (new_time_left <= 0) {
185 * Huh -- no new timeout?? Write it.
187 return;
190 if (new_time_left < cache_time_left) {
192 * Someone wants to shorten the timeout. Let it happen.
194 return;
198 * By how much does the new timeout extend the remaining cache time?
200 additional_time = new_time_left - cache_time_left;
202 if (additional_time * 10 < 0) {
204 * Integer overflow. We extend by so much that we have to write it.
206 return;
210 * The comparison below is essentially equivalent to
212 * new_time_left > cache_time_left * 1.10
214 * but without floating point calculations.
217 if (additional_time * 10 > cache_time_left) {
219 * We extend the cache timeout by more than 10%. Do it.
221 return;
225 * Now the more expensive data compare.
227 if (data_blob_cmp(state->data, &data) != 0) {
229 * Write a new value. Certainly do it.
231 return;
235 * Extending the timeout by less than 10% for the same cache value is
236 * not worth the trouble writing a value into gencache under a
237 * possibly expensive transaction.
239 state->gotit = true;
242 static bool gencache_have_val(const char *keystr, const DATA_BLOB *data,
243 time_t timeout)
245 struct gencache_have_val_state state;
247 state.new_timeout = timeout;
248 state.data = data;
249 state.gotit = false;
251 if (!gencache_parse(keystr, gencache_have_val_parser, &state)) {
252 return false;
254 return state.gotit;
257 static int last_stabilize_parser(TDB_DATA key, TDB_DATA data,
258 void *private_data)
260 time_t *last_stabilize = private_data;
262 if ((data.dsize != 0) && (data.dptr[data.dsize-1] == '\0')) {
263 *last_stabilize = atoi((char *)data.dptr);
265 return 0;
269 * Set an entry in the cache file. If there's no such
270 * one, then add it.
272 * @param keystr string that represents a key of this entry
273 * @param blob DATA_BLOB value being cached
274 * @param timeout time when the value is expired
276 * @retval true when entry is successfuly stored
277 * @retval false on failure
280 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob,
281 time_t timeout)
283 int ret;
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()) return False;
300 if (gencache_have_val(keystr, blob, timeout)) {
301 DEBUG(10, ("Did not store value for %s, we already got it\n",
302 keystr));
303 return true;
306 val = talloc_asprintf(talloc_tos(), CACHE_DATA_FMT, (int)timeout);
307 if (val == NULL) {
308 return False;
310 val = talloc_realloc(NULL, val, char, talloc_array_length(val)-1);
311 if (val == NULL) {
312 return false;
314 val = (char *)talloc_append_blob(NULL, val, *blob);
315 if (val == NULL) {
316 return false;
319 DEBUG(10, ("Adding cache entry with key=[%s] and timeout="
320 "[%s] (%d seconds %s)\n", keystr,
321 timestring(talloc_tos(), timeout),
322 (int)(timeout - time(NULL)),
323 timeout > time(NULL) ? "ahead" : "in the past"));
325 ret = tdb_store_bystring(
326 cache_notrans->tdb, keystr,
327 make_tdb_data((uint8_t *)val, talloc_array_length(val)),
329 TALLOC_FREE(val);
331 if (ret != 0) {
332 return false;
336 * Every 100 writes within a single process, stabilize the cache with
337 * a transaction. This is done to prevent a single transaction to
338 * become huge and chew lots of memory.
340 writecount += 1;
341 if (writecount > lp_parm_int(-1, "gencache", "stabilize_count", 100)) {
342 gencache_stabilize();
343 writecount = 0;
344 goto done;
348 * Every 5 minutes, call gencache_stabilize() to not let grow
349 * gencache_notrans.tdb too large.
352 last_stabilize = 0;
354 tdb_parse_record(cache_notrans->tdb, last_stabilize_key(),
355 last_stabilize_parser, &last_stabilize);
357 if ((last_stabilize
358 + lp_parm_int(-1, "gencache", "stabilize_interval", 300))
359 < time(NULL)) {
360 gencache_stabilize();
363 done:
364 return ret == 0;
368 * Delete one entry from the cache file.
370 * @param keystr string that represents a key of this entry
372 * @retval true upon successful deletion
373 * @retval false in case of failure
376 bool gencache_del(const char *keystr)
378 bool exists, was_expired;
379 bool ret = false;
380 DATA_BLOB value;
382 if (keystr == NULL) {
383 return false;
386 if (!gencache_init()) return False;
388 DEBUG(10, ("Deleting cache entry (key=[%s])\n", keystr));
391 * We delete an element by setting its timeout to 0. This way we don't
392 * have to do a transaction on gencache.tdb every time we delete an
393 * element.
396 exists = gencache_get_data_blob(keystr, NULL, &value, NULL,
397 &was_expired);
399 if (!exists && was_expired) {
401 * gencache_get_data_blob has implicitly deleted this
402 * entry, so we have to return success here.
404 return true;
407 if (exists) {
408 data_blob_free(&value);
409 ret = gencache_set(keystr, "", 0);
411 return ret;
414 static bool gencache_pull_timeout(char *val, time_t *pres, char **pendptr)
416 time_t res;
417 char *endptr;
419 if (val == NULL) {
420 return false;
423 res = strtol(val, &endptr, 10);
425 if ((endptr == NULL) || (*endptr != '/')) {
426 DEBUG(2, ("Invalid gencache data format: %s\n", val));
427 return false;
429 if (pres != NULL) {
430 *pres = res;
432 if (pendptr != NULL) {
433 *pendptr = endptr;
435 return true;
438 struct gencache_parse_state {
439 void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
440 void *private_data;
441 bool is_memcache;
444 static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
446 struct gencache_parse_state *state;
447 DATA_BLOB blob;
448 time_t t;
449 char *endptr;
450 bool ret;
452 if (data.dptr == NULL) {
453 return -1;
455 ret = gencache_pull_timeout((char *)data.dptr, &t, &endptr);
456 if (!ret) {
457 return -1;
459 state = (struct gencache_parse_state *)private_data;
460 blob = data_blob_const(
461 endptr+1, data.dsize - PTR_DIFF(endptr+1, data.dptr));
462 state->parser(t, blob, state->private_data);
464 if (!state->is_memcache) {
465 memcache_add(NULL, GENCACHE_RAM,
466 data_blob_const(key.dptr, key.dsize),
467 data_blob_const(data.dptr, data.dsize));
470 return 0;
473 bool gencache_parse(const char *keystr,
474 void (*parser)(time_t timeout, DATA_BLOB blob,
475 void *private_data),
476 void *private_data)
478 struct gencache_parse_state state;
479 TDB_DATA key = string_term_tdb_data(keystr);
480 DATA_BLOB memcache_val;
481 int ret;
483 if (keystr == NULL) {
484 return false;
486 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
487 return false;
489 if (!gencache_init()) {
490 return false;
493 state.parser = parser;
494 state.private_data = private_data;
496 if (memcache_lookup(NULL, GENCACHE_RAM,
497 data_blob_const(key.dptr, key.dsize),
498 &memcache_val)) {
500 * Make sure that nobody has changed the gencache behind our
501 * back.
503 int current_seqnum = tdb_get_seqnum(cache_notrans->tdb);
504 if (current_seqnum == cache_notrans_seqnum) {
506 * Ok, our memcache is still current, use it without
507 * going to the tdb files.
509 state.is_memcache = true;
510 gencache_parse_fn(key, make_tdb_data(memcache_val.data,
511 memcache_val.length),
512 &state);
513 return true;
515 memcache_flush(NULL, GENCACHE_RAM);
516 cache_notrans_seqnum = current_seqnum;
519 state.is_memcache = false;
521 ret = tdb_parse_record(cache_notrans->tdb, key,
522 gencache_parse_fn, &state);
523 if (ret == 0) {
524 return true;
526 ret = tdb_parse_record(cache->tdb, key, gencache_parse_fn, &state);
527 return (ret == 0);
530 struct gencache_get_data_blob_state {
531 TALLOC_CTX *mem_ctx;
532 DATA_BLOB *blob;
533 time_t timeout;
534 bool result;
537 static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
538 void *private_data)
540 struct gencache_get_data_blob_state *state =
541 (struct gencache_get_data_blob_state *)private_data;
543 if (timeout == 0) {
544 state->result = false;
545 return;
547 state->timeout = timeout;
549 if (state->blob == NULL) {
550 state->result = true;
551 return;
554 *state->blob = data_blob_talloc(state->mem_ctx, blob.data,
555 blob.length);
556 if (state->blob->data == NULL) {
557 state->result = false;
558 return;
560 state->result = true;
564 * Get existing entry from the cache file.
566 * @param keystr string that represents a key of this entry
567 * @param blob DATA_BLOB that is filled with entry's blob
568 * @param timeout pointer to a time_t that is filled with entry's
569 * timeout
571 * @retval true when entry is successfuly fetched
572 * @retval False for failure
575 bool gencache_get_data_blob(const char *keystr, TALLOC_CTX *mem_ctx,
576 DATA_BLOB *blob,
577 time_t *timeout, bool *was_expired)
579 struct gencache_get_data_blob_state state;
580 bool expired = false;
582 state.result = false;
583 state.mem_ctx = mem_ctx;
584 state.blob = blob;
586 if (!gencache_parse(keystr, gencache_get_data_blob_parser, &state)) {
587 goto fail;
589 if (!state.result) {
590 goto fail;
592 if (state.timeout <= time(NULL)) {
594 * We're expired, delete the entry. We can't use gencache_del
595 * here, because that uses gencache_get_data_blob for checking
596 * the existence of a record. We know the thing exists and
597 * directly store an empty value with 0 timeout.
599 gencache_set(keystr, "", 0);
600 expired = true;
601 goto fail;
603 if (timeout) {
604 *timeout = state.timeout;
607 return True;
609 fail:
610 if (was_expired != NULL) {
611 *was_expired = expired;
613 if (state.result && state.blob) {
614 data_blob_free(state.blob);
616 return false;
619 struct stabilize_state {
620 bool written;
622 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
623 void *priv);
625 static int wipe_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
626 void *priv);
629 * Stabilize gencache
631 * Migrate the clear-if-first gencache data to the stable,
632 * transaction-based gencache.tdb
635 bool gencache_stabilize(void)
637 struct stabilize_state state;
638 int res;
639 char *now;
641 if (!gencache_init()) {
642 return false;
645 res = tdb_transaction_start_nonblock(cache->tdb);
646 if (res != 0) {
647 if (tdb_error(cache->tdb) == TDB_ERR_NOLOCK)
650 * Someone else already does the stabilize,
651 * this does not have to be done twice
653 return true;
656 DEBUG(10, ("Could not start transaction on gencache.tdb: "
657 "%s\n", tdb_errorstr_compat(cache->tdb)));
658 return false;
661 res = tdb_lockall(cache_notrans->tdb);
662 if (res != 0) {
663 tdb_transaction_cancel(cache->tdb);
664 DEBUG(10, ("Could not get allrecord lock on "
665 "gencache_notrans.tdb: %s\n",
666 tdb_errorstr_compat(cache_notrans->tdb)));
667 return false;
670 state.written = false;
672 res = tdb_traverse(cache_notrans->tdb, stabilize_fn, &state);
673 if (res < 0) {
674 tdb_unlockall(cache_notrans->tdb);
675 tdb_transaction_cancel(cache->tdb);
676 return false;
679 if (!state.written) {
680 tdb_unlockall(cache_notrans->tdb);
681 tdb_transaction_cancel(cache->tdb);
682 return true;
685 res = tdb_transaction_commit(cache->tdb);
686 if (res != 0) {
687 DEBUG(10, ("tdb_transaction_commit on gencache.tdb failed: "
688 "%s\n", tdb_errorstr_compat(cache->tdb)));
689 tdb_unlockall(cache_notrans->tdb);
690 return false;
693 res = tdb_traverse(cache_notrans->tdb, wipe_fn, NULL);
694 if (res < 0) {
695 DEBUG(10, ("tdb_traverse with wipe_fn on gencache_notrans.tdb "
696 "failed: %s\n",
697 tdb_errorstr_compat(cache_notrans->tdb)));
698 tdb_unlockall(cache_notrans->tdb);
699 return false;
702 res = tdb_unlockall(cache_notrans->tdb);
703 if (res != 0) {
704 DEBUG(10, ("tdb_unlockall on gencache.tdb failed: "
705 "%s\n", tdb_errorstr_compat(cache->tdb)));
706 return false;
709 now = talloc_asprintf(talloc_tos(), "%d", (int)time(NULL));
710 if (now != NULL) {
711 tdb_store(cache_notrans->tdb, last_stabilize_key(),
712 string_term_tdb_data(now), 0);
713 TALLOC_FREE(now);
716 return true;
719 static int stabilize_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
720 void *priv)
722 struct stabilize_state *state = (struct stabilize_state *)priv;
723 int res;
724 time_t timeout;
726 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
727 return 0;
730 if (!gencache_pull_timeout((char *)val.dptr, &timeout, NULL)) {
731 DEBUG(10, ("Ignoring invalid entry\n"));
732 return 0;
734 if ((timeout < time(NULL)) || (val.dsize == 0)) {
735 res = tdb_delete(cache->tdb, key);
736 if (res == 0) {
737 state->written = true;
738 } else if (tdb_error(cache->tdb) == TDB_ERR_NOEXIST) {
739 res = 0;
741 } else {
742 res = tdb_store(cache->tdb, key, val, 0);
743 if (res == 0) {
744 state->written = true;
748 if (res != 0) {
749 DEBUG(10, ("Transfer to gencache.tdb failed: %s\n",
750 tdb_errorstr_compat(cache->tdb)));
751 return -1;
754 return 0;
757 static int wipe_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA val,
758 void *priv)
760 int res;
761 bool ok;
762 time_t timeout;
764 res = tdb_data_cmp(key, last_stabilize_key());
765 if (res == 0) {
766 return 0;
769 ok = gencache_pull_timeout((char *)val.dptr, &timeout, NULL);
770 if (!ok) {
771 DEBUG(10, ("Ignoring invalid entry\n"));
772 return 0;
775 res = tdb_delete(tdb, key);
776 if (res != 0) {
777 DEBUG(10, ("tdb_delete from gencache_notrans.tdb failed: "
778 "%s\n", tdb_errorstr_compat(cache_notrans->tdb)));
779 return -1;
782 return 0;
787 * Get existing entry from the cache file.
789 * @param keystr string that represents a key of this entry
790 * @param valstr buffer that is allocated and filled with the entry value
791 * buffer's disposing must be done outside
792 * @param timeout pointer to a time_t that is filled with entry's
793 * timeout
795 * @retval true when entry is successfuly fetched
796 * @retval False for failure
799 bool gencache_get(const char *keystr, TALLOC_CTX *mem_ctx, char **value,
800 time_t *ptimeout)
802 DATA_BLOB blob;
803 bool ret = False;
805 ret = gencache_get_data_blob(keystr, mem_ctx, &blob, ptimeout, NULL);
806 if (!ret) {
807 return false;
809 if ((blob.data == NULL) || (blob.length == 0)) {
810 data_blob_free(&blob);
811 return false;
813 if (blob.data[blob.length-1] != '\0') {
814 /* Not NULL terminated, can't be a string */
815 data_blob_free(&blob);
816 return false;
818 if (value) {
820 * talloc_move generates a type-punned warning here. As we
821 * leave the function immediately, do a simple talloc_steal.
823 *value = (char *)talloc_steal(mem_ctx, blob.data);
824 return true;
826 data_blob_free(&blob);
827 return true;
831 * Set an entry in the cache file. If there's no such
832 * one, then add it.
834 * @param keystr string that represents a key of this entry
835 * @param value text representation value being cached
836 * @param timeout time when the value is expired
838 * @retval true when entry is successfuly stored
839 * @retval false on failure
842 bool gencache_set(const char *keystr, const char *value, time_t timeout)
844 DATA_BLOB blob = data_blob_const(value, strlen(value)+1);
845 return gencache_set_data_blob(keystr, &blob, timeout);
848 struct gencache_iterate_blobs_state {
849 void (*fn)(const char *key, DATA_BLOB value,
850 time_t timeout, void *private_data);
851 const char *pattern;
852 void *private_data;
853 bool in_persistent;
856 static int gencache_iterate_blobs_fn(struct tdb_context *tdb, TDB_DATA key,
857 TDB_DATA data, void *priv)
859 struct gencache_iterate_blobs_state *state =
860 (struct gencache_iterate_blobs_state *)priv;
861 char *keystr;
862 char *free_key = NULL;
863 time_t timeout;
864 char *endptr;
866 if (tdb_data_cmp(key, last_stabilize_key()) == 0) {
867 return 0;
869 if (state->in_persistent && tdb_exists(cache_notrans->tdb, key)) {
870 return 0;
873 if (key.dptr[key.dsize-1] == '\0') {
874 keystr = (char *)key.dptr;
875 } else {
876 /* ensure 0-termination */
877 keystr = talloc_strndup(talloc_tos(), (char *)key.dptr, key.dsize);
878 free_key = keystr;
879 if (keystr == NULL) {
880 goto done;
884 if (!gencache_pull_timeout((char *)data.dptr, &timeout, &endptr)) {
885 goto done;
887 endptr += 1;
889 if (fnmatch(state->pattern, keystr, 0) != 0) {
890 goto done;
893 DEBUG(10, ("Calling function with arguments "
894 "(key=[%s], timeout=[%s])\n",
895 keystr, timestring(talloc_tos(), timeout)));
897 state->fn(keystr,
898 data_blob_const(endptr,
899 data.dsize - PTR_DIFF(endptr, data.dptr)),
900 timeout, state->private_data);
902 done:
903 TALLOC_FREE(free_key);
904 return 0;
907 void gencache_iterate_blobs(void (*fn)(const char *key, DATA_BLOB value,
908 time_t timeout, void *private_data),
909 void *private_data, const char *pattern)
911 struct gencache_iterate_blobs_state state;
913 if ((fn == NULL) || (pattern == NULL) || !gencache_init()) {
914 return;
917 DEBUG(5, ("Searching cache keys with pattern %s\n", pattern));
919 state.fn = fn;
920 state.pattern = pattern;
921 state.private_data = private_data;
923 state.in_persistent = false;
924 tdb_traverse(cache_notrans->tdb, gencache_iterate_blobs_fn, &state);
926 state.in_persistent = true;
927 tdb_traverse(cache->tdb, gencache_iterate_blobs_fn, &state);
931 * Iterate through all entries which key matches to specified pattern
933 * @param fn pointer to the function that will be supplied with each single
934 * matching cache entry (key, value and timeout) as an arguments
935 * @param data void pointer to an arbitrary data that is passed directly to the fn
936 * function on each call
937 * @param keystr_pattern pattern the existing entries' keys are matched to
941 struct gencache_iterate_state {
942 void (*fn)(const char *key, const char *value, time_t timeout,
943 void *priv);
944 void *private_data;
947 static void gencache_iterate_fn(const char *key, DATA_BLOB value,
948 time_t timeout, void *private_data)
950 struct gencache_iterate_state *state =
951 (struct gencache_iterate_state *)private_data;
952 char *valstr;
953 char *free_val = NULL;
955 if (value.data[value.length-1] == '\0') {
956 valstr = (char *)value.data;
957 } else {
958 /* ensure 0-termination */
959 valstr = talloc_strndup(talloc_tos(), (char *)value.data, value.length);
960 free_val = valstr;
961 if (valstr == NULL) {
962 goto done;
966 DEBUG(10, ("Calling function with arguments "
967 "(key=[%s], value=[%s], timeout=[%s])\n",
968 key, valstr, timestring(talloc_tos(), timeout)));
970 state->fn(key, valstr, timeout, state->private_data);
972 done:
974 TALLOC_FREE(free_val);
977 void gencache_iterate(void (*fn)(const char *key, const char *value,
978 time_t timeout, void *dptr),
979 void *private_data, const char *pattern)
981 struct gencache_iterate_state state;
983 if (fn == NULL) {
984 return;
986 state.fn = fn;
987 state.private_data = private_data;
988 gencache_iterate_blobs(gencache_iterate_fn, &state, pattern);