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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/tdb/include/tdbutil.h"
26 #include "system/time.h"
27 #include "system/filesys.h"
30 #define TIMEOUT_LEN 12
31 #define CACHE_DATA_FMT "%12u/%s"
33 static struct tdb_wrap *cache;
37 * @brief Generic, persistent and shared between processes cache mechanism
38 * for use by various parts of the Samba code
44 * Cache initialisation function. Opens cache tdb file or creates
45 * it if does not exist.
47 * @return true on successful initialisation of the cache or
51 BOOL gencache_init(void)
53 char* cache_fname = NULL;
55 /* skip file open if it's already opened */
56 if (cache) return True;
58 asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb");
60 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
62 DEBUG(0, ("Filename allocation failed.\n"));
66 cache = tdb_wrap_open(NULL, cache_fname, 0, TDB_DEFAULT,
67 O_RDWR|O_CREAT, 0644);
69 SAFE_FREE(cache_fname);
71 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
79 * Cache shutdown function. Closes opened cache tdb file.
81 * @return true on successful closing the cache or
82 * false on failure during cache shutdown
85 BOOL gencache_shutdown(void)
87 if (!cache) return False;
88 DEBUG(5, ("Closing cache file\n"));
95 * Set an entry in the cache file. If there's no such
98 * @param keystr string that represents a key of this entry
99 * @param value text representation value being cached
100 * @param timeout time when the value is expired
102 * @retval true when entry is successfuly stored
103 * @retval false on failure
106 BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
109 TDB_DATA keybuf, databuf;
112 /* fail completely if get null pointers passed */
113 SMB_ASSERT(keystr && value);
115 if (!gencache_init()) return False;
117 asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
121 keybuf.dptr = strdup(keystr);
122 keybuf.dsize = strlen(keystr)+1;
123 databuf.dptr = strdup(valstr);
124 databuf.dsize = strlen(valstr)+1;
125 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout \
126 = %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout),
127 (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past"));
129 ret = tdb_store(cache->tdb, keybuf, databuf, 0);
131 SAFE_FREE(keybuf.dptr);
132 SAFE_FREE(databuf.dptr);
139 * Set existing entry to the cache file.
141 * @param keystr string that represents a key of this entry
142 * @param valstr text representation value being cached
143 * @param timeout time when the value is expired
145 * @retval true when entry is successfuly set
146 * @retval false on failure
149 BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
152 TDB_DATA keybuf, databuf;
153 char *old_valstr, *datastr;
156 /* fail completely if get null pointers passed */
157 SMB_ASSERT(keystr && valstr);
159 if (!gencache_init()) return False;
162 * Check whether entry exists in the cache
163 * Don't verify gencache_get exit code, since the entry may be expired
165 gencache_get(keystr, &old_valstr, &old_timeout);
167 if (!(old_valstr && old_timeout)) return False;
169 DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \
170 = %s\n", keystr, old_valstr, ctime(&old_timeout)));
172 asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr);
173 keybuf.dptr = strdup(keystr);
174 keybuf.dsize = strlen(keystr)+1;
175 databuf.dptr = strdup(datastr);
176 databuf.dsize = strlen(datastr)+1;
177 DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr,
178 ctime(&timeout), (int)(timeout - time(NULL)),
179 timeout > time(NULL) ? "ahead" : "in the past"));
182 ret = tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE);
185 SAFE_FREE(old_valstr);
186 SAFE_FREE(keybuf.dptr);
187 SAFE_FREE(databuf.dptr);
194 * Delete one entry from the cache file.
196 * @param keystr string that represents a key of this entry
198 * @retval true upon successful deletion
199 * @retval false in case of failure
202 BOOL gencache_del(const char *keystr)
207 /* fail completely if get null pointers passed */
210 if (!gencache_init()) return False;
212 keybuf.dptr = strdup(keystr);
213 keybuf.dsize = strlen(keystr)+1;
214 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
215 ret = tdb_delete(cache->tdb, keybuf);
217 SAFE_FREE(keybuf.dptr);
223 * Get existing entry from the cache file.
225 * @param keystr string that represents a key of this entry
226 * @param valstr buffer that is allocated and filled with the entry value
227 * buffer's disposing must be done outside
228 * @param timeout pointer to a time_t that is filled with entry's
231 * @retval true when entry is successfuly fetched
232 * @retval False for failure
235 BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
237 TDB_DATA keybuf, databuf;
239 /* fail completely if get null pointers passed */
242 if (!gencache_init())
245 keybuf.dptr = strdup(keystr);
246 keybuf.dsize = strlen(keystr)+1;
247 databuf = tdb_fetch(cache->tdb, keybuf);
248 SAFE_FREE(keybuf.dptr);
250 if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
251 char* entry_buf = strndup(databuf.dptr, databuf.dsize);
256 v = malloc_array_p(char, databuf.dsize - TIMEOUT_LEN);
258 SAFE_FREE(databuf.dptr);
259 sscanf(entry_buf, CACHE_DATA_FMT, (int*)&i, v);
260 SAFE_FREE(entry_buf);
263 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
264 "timeout = %s\n", t > time(NULL) ? "valid" :
265 "expired", keystr, v, ctime(&t)));
275 return t > time(NULL);
278 SAFE_FREE(databuf.dptr);
286 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
295 * Iterate through all entries which key matches to specified pattern
297 * @param fn pointer to the function that will be supplied with each single
298 * matching cache entry (key, value and timeout) as an arguments
299 * @param data void pointer to an arbitrary data that is passed directly to the fn
300 * function on each call
301 * @param keystr_pattern pattern the existing entries' keys are matched to
305 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
306 void* data, const char* keystr_pattern)
308 TDB_LIST_NODE *node, *first_node;
310 char *keystr = NULL, *valstr = NULL, *entry = NULL;
314 /* fail completely if get null pointers passed */
315 SMB_ASSERT(fn && keystr_pattern);
317 if (!gencache_init()) return;
319 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
320 node = tdb_search_keys(cache->tdb, keystr_pattern);
324 /* ensure null termination of the key string */
325 keystr = strndup(node->node_key.dptr, node->node_key.dsize);
328 * We don't use gencache_get function, because we need to iterate through
329 * all of the entries. Validity verification is up to fn routine.
331 databuf = tdb_fetch(cache->tdb, node->node_key);
332 if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
333 SAFE_FREE(databuf.dptr);
338 entry = strndup(databuf.dptr, databuf.dsize);
339 SAFE_FREE(databuf.dptr);
340 valstr = malloc_array_p(char, databuf.dsize - TIMEOUT_LEN);
341 sscanf(entry, CACHE_DATA_FMT, (int*)(&i), valstr);
344 DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
345 keystr, valstr, ctime(&timeout)));
346 fn(keystr, valstr, timeout, data);
354 tdb_search_list_free(first_node);
357 /********************************************************************
359 ********************************************************************/
361 int gencache_lock_entry( const char *key )
363 return tdb_lock_bystring(cache->tdb, key);
366 /********************************************************************
368 ********************************************************************/
370 void gencache_unlock_entry( const char *key )
372 tdb_unlock_bystring(cache->tdb, key);