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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define DBGC_CLASS DBGC_TDB
28 #define TIMEOUT_LEN 12
29 #define CACHE_DATA_FMT "%12u/%s"
30 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
31 #define BLOB_TYPE "DATA_BLOB"
32 #define BLOB_TYPE_LEN 9
34 static struct tdb_context *cache;
38 * @brief Generic, persistent and shared between processes cache mechanism
39 * for use by various parts of the Samba code
45 * Cache initialisation function. Opens cache tdb file or creates
46 * it if does not exist.
48 * @return true on successful initialisation of the cache or
52 bool gencache_init(void)
54 char* cache_fname = NULL;
56 /* skip file open if it's already opened */
57 if (cache) return True;
59 cache_fname = lock_path("gencache.tdb");
61 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
63 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
64 O_RDWR|O_CREAT, 0644);
66 if (!cache && (errno == EACCES)) {
67 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDONLY, 0644);
69 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
74 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
82 * Cache shutdown function. Closes opened cache tdb file.
84 * @return true on successful closing the cache or
85 * false on failure during cache shutdown
88 bool gencache_shutdown(void)
91 /* tdb_close routine returns -1 on error */
92 if (!cache) return False;
93 DEBUG(5, ("Closing cache file\n"));
94 ret = tdb_close(cache);
101 * Set an entry in the cache file. If there's no such
104 * @param keystr string that represents a key of this entry
105 * @param value text representation value being cached
106 * @param timeout time when the value is expired
108 * @retval true when entry is successfuly stored
109 * @retval false on failure
112 bool gencache_set(const char *keystr, const char *value, time_t timeout)
118 if ((keystr == NULL) || (value == NULL)) {
122 if (!gencache_init()) return False;
124 if (asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value) == -1) {
128 databuf = string_term_tdb_data(valstr);
129 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
130 " %s (%d seconds %s)\n", keystr, value,ctime(&timeout),
131 (int)(timeout - time(NULL)),
132 timeout > time(NULL) ? "ahead" : "in the past"));
134 ret = tdb_store_bystring(cache, keystr, databuf, 0);
141 * Delete one entry from the cache file.
143 * @param keystr string that represents a key of this entry
145 * @retval true upon successful deletion
146 * @retval false in case of failure
149 bool gencache_del(const char *keystr)
153 if (keystr == NULL) {
157 if (!gencache_init()) return False;
159 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
160 ret = tdb_delete_bystring(cache, keystr);
167 * Get existing entry from the cache file.
169 * @param keystr string that represents a key of this entry
170 * @param valstr buffer that is allocated and filled with the entry value
171 * buffer's disposing must be done outside
172 * @param timeout pointer to a time_t that is filled with entry's
175 * @retval true when entry is successfuly fetched
176 * @retval False for failure
179 bool gencache_get(const char *keystr, char **valstr, time_t *timeout)
185 if (keystr == NULL) {
189 if (!gencache_init()) {
193 databuf = tdb_fetch_bystring(cache, keystr);
195 if (databuf.dptr == NULL) {
196 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
201 t = strtol((const char *)databuf.dptr, &endptr, 10);
203 if ((endptr == NULL) || (*endptr != '/')) {
204 DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
205 SAFE_FREE(databuf.dptr);
209 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
210 "timeout = %s", t > time(NULL) ? "valid" :
211 "expired", keystr, endptr+1, ctime(&t)));
213 if (t <= time(NULL)) {
215 /* We're expired, delete the entry */
216 tdb_delete_bystring(cache, keystr);
218 SAFE_FREE(databuf.dptr);
223 *valstr = SMB_STRDUP(endptr+1);
224 if (*valstr == NULL) {
225 SAFE_FREE(databuf.dptr);
226 DEBUG(0, ("strdup failed\n"));
231 SAFE_FREE(databuf.dptr);
241 * Get existing entry from the cache file.
243 * @param keystr string that represents a key of this entry
244 * @param blob DATA_BLOB that is filled with entry's blob
245 * @param expired pointer to a bool that indicates whether the entry is expired
247 * @retval true when entry is successfuly fetched
248 * @retval False for failure
251 bool gencache_get_data_blob(const char *keystr, DATA_BLOB *blob, bool *expired)
256 unsigned char *buf = NULL;
259 int buflen = 0, len = 0, blob_len = 0;
260 unsigned char *blob_buf = NULL;
262 if (keystr == NULL) {
266 if (!gencache_init()) {
270 databuf = tdb_fetch_bystring(cache, keystr);
272 DEBUG(10,("Cache entry with key = %s couldn't be found\n",
277 buf = (unsigned char *)databuf.dptr;
278 buflen = databuf.dsize;
280 len += tdb_unpack(buf+len, buflen-len, "fB",
282 &blob_len, &blob_buf);
287 t = strtol(valstr, &blob_type, 10);
289 if (strcmp(blob_type+1, BLOB_TYPE) != 0) {
293 DEBUG(10,("Returning %s cache entry: key = %s, "
294 "timeout = %s", t > time(NULL) ? "valid" :
295 "expired", keystr, ctime(&t)));
297 if (t <= time(NULL)) {
305 *blob = data_blob(blob_buf, blob_len);
314 SAFE_FREE(databuf.dptr);
320 * Set an entry in the cache file. If there's no such
323 * @param keystr string that represents a key of this entry
324 * @param blob DATA_BLOB value being cached
325 * @param timeout time when the value is expired
327 * @retval true when entry is successfuly stored
328 * @retval false on failure
331 bool gencache_set_data_blob(const char *keystr, const DATA_BLOB *blob, time_t timeout)
337 unsigned char *buf = NULL;
338 int len = 0, buflen = 0;
340 if ((keystr == NULL) || (blob == NULL)) {
344 if (!gencache_init()) {
348 if (asprintf(&valstr, "%12u/%s", (int)timeout, BLOB_TYPE) == -1) {
355 len += tdb_pack(buf+len, buflen-len, "fB",
357 blob->length, blob->data);
365 buf = SMB_MALLOC_ARRAY(unsigned char, len);
373 databuf = make_tdb_data(buf, len);
375 DEBUG(10,("Adding cache entry with key = %s; "
376 "blob size = %d and timeout = %s"
377 "(%d seconds %s)\n", keystr, (int)databuf.dsize,
378 ctime(&timeout), (int)(timeout - time(NULL)),
379 timeout > time(NULL) ? "ahead" : "in the past"));
381 tdb_ret = tdb_store_bystring(cache, keystr, databuf, 0);
394 * Iterate through all entries which key matches to specified pattern
396 * @param fn pointer to the function that will be supplied with each single
397 * matching cache entry (key, value and timeout) as an arguments
398 * @param data void pointer to an arbitrary data that is passed directly to the fn
399 * function on each call
400 * @param keystr_pattern pattern the existing entries' keys are matched to
404 struct gencache_iterate_state {
405 void (*fn)(const char *key, const char *value, time_t timeout,
411 static int gencache_iterate_fn(struct tdb_context *tdb, TDB_DATA key,
412 TDB_DATA value, void *priv)
414 struct gencache_iterate_state *state =
415 (struct gencache_iterate_state *)priv;
417 char *free_key = NULL;
419 char *free_val = NULL;
424 if (key.dptr[key.dsize-1] == '\0') {
425 keystr = (char *)key.dptr;
427 /* ensure 0-termination */
428 keystr = SMB_STRNDUP((char *)key.dptr, key.dsize);
432 if ((value.dptr == NULL) || (value.dsize <= TIMEOUT_LEN)) {
436 if (fnmatch(state->pattern, keystr, 0) != 0) {
440 if (value.dptr[value.dsize-1] == '\0') {
441 valstr = (char *)value.dptr;
443 /* ensure 0-termination */
444 valstr = SMB_STRNDUP((char *)value.dptr, value.dsize);
448 u = strtoul(valstr, &timeout_endp, 10);
450 if ((*timeout_endp != '/') || ((timeout_endp-valstr) != TIMEOUT_LEN)) {
457 DEBUG(10, ("Calling function with arguments "
458 "(key = %s, value = %s, timeout = %s)\n",
459 keystr, timeout_endp, ctime(&timeout)));
460 state->fn(keystr, timeout_endp, timeout, state->priv);
468 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
469 void* data, const char* keystr_pattern)
471 struct gencache_iterate_state state;
473 if ((fn == NULL) || (keystr_pattern == NULL)) {
477 if (!gencache_init()) return;
479 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
482 state.pattern = keystr_pattern;
484 tdb_traverse(cache, gencache_iterate_fn, &state);