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"
29 #define DBGC_CLASS DBGC_TDB
31 #define TIMEOUT_LEN 12
32 #define CACHE_DATA_FMT "%12u/%s"
34 static struct tdb_wrap *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 asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb");
61 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
63 DEBUG(0, ("Filename allocation failed.\n"));
67 cache = tdb_wrap_open(NULL, cache_fname, 0, TDB_DEFAULT,
68 O_RDWR|O_CREAT, 0644);
70 SAFE_FREE(cache_fname);
72 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
80 * Cache shutdown function. Closes opened cache tdb file.
82 * @return true on successful closing the cache or
83 * false on failure during cache shutdown
86 BOOL gencache_shutdown(void)
88 if (!cache) return False;
89 DEBUG(5, ("Closing cache file\n"));
96 * Set an entry in the cache file. If there's no such
99 * @param keystr string that represents a key of this entry
100 * @param value text representation value being cached
101 * @param timeout time when the value is expired
103 * @retval true when entry is successfuly stored
104 * @retval false on failure
107 BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
110 TDB_DATA keybuf, databuf;
113 /* fail completely if get null pointers passed */
114 SMB_ASSERT(keystr && value);
116 if (!gencache_init()) return False;
118 asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
122 keybuf.dptr = strdup(keystr);
123 keybuf.dsize = strlen(keystr)+1;
124 databuf.dptr = strdup(valstr);
125 databuf.dsize = strlen(valstr)+1;
126 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout \
127 = %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout),
128 (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past"));
130 ret = tdb_store(cache->tdb, keybuf, databuf, 0);
132 SAFE_FREE(keybuf.dptr);
133 SAFE_FREE(databuf.dptr);
140 * Set existing entry to the cache file.
142 * @param keystr string that represents a key of this entry
143 * @param valstr text representation value being cached
144 * @param timeout time when the value is expired
146 * @retval true when entry is successfuly set
147 * @retval false on failure
150 BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
153 TDB_DATA keybuf, databuf;
154 char *old_valstr, *datastr;
157 /* fail completely if get null pointers passed */
158 SMB_ASSERT(keystr && valstr);
160 if (!gencache_init()) return False;
163 * Check whether entry exists in the cache
164 * Don't verify gencache_get exit code, since the entry may be expired
166 gencache_get(keystr, &old_valstr, &old_timeout);
168 if (!(old_valstr && old_timeout)) return False;
170 DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \
171 = %s\n", keystr, old_valstr, ctime(&old_timeout)));
173 asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr);
174 keybuf.dptr = strdup(keystr);
175 keybuf.dsize = strlen(keystr)+1;
176 databuf.dptr = strdup(datastr);
177 databuf.dsize = strlen(datastr)+1;
178 DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr,
179 ctime(&timeout), (int)(timeout - time(NULL)),
180 timeout > time(NULL) ? "ahead" : "in the past"));
183 ret = tdb_store(cache->tdb, keybuf, databuf, TDB_REPLACE);
186 SAFE_FREE(old_valstr);
187 SAFE_FREE(keybuf.dptr);
188 SAFE_FREE(databuf.dptr);
195 * Delete one entry from the cache file.
197 * @param keystr string that represents a key of this entry
199 * @retval true upon successful deletion
200 * @retval false in case of failure
203 BOOL gencache_del(const char *keystr)
208 /* fail completely if get null pointers passed */
211 if (!gencache_init()) return False;
213 keybuf.dptr = strdup(keystr);
214 keybuf.dsize = strlen(keystr)+1;
215 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
216 ret = tdb_delete(cache->tdb, keybuf);
218 SAFE_FREE(keybuf.dptr);
224 * Get existing entry from the cache file.
226 * @param keystr string that represents a key of this entry
227 * @param valstr buffer that is allocated and filled with the entry value
228 * buffer's disposing must be done outside
229 * @param timeout pointer to a time_t that is filled with entry's
232 * @retval true when entry is successfuly fetched
233 * @retval False for failure
236 BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
238 TDB_DATA keybuf, databuf;
240 /* fail completely if get null pointers passed */
243 if (!gencache_init())
246 keybuf.dptr = strdup(keystr);
247 keybuf.dsize = strlen(keystr)+1;
248 databuf = tdb_fetch(cache->tdb, keybuf);
249 SAFE_FREE(keybuf.dptr);
251 if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
252 char* entry_buf = strndup(databuf.dptr, databuf.dsize);
257 v = malloc_array_p(char, databuf.dsize - TIMEOUT_LEN);
259 SAFE_FREE(databuf.dptr);
260 sscanf(entry_buf, CACHE_DATA_FMT, (int*)&i, v);
261 SAFE_FREE(entry_buf);
264 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
265 "timeout = %s\n", t > time(NULL) ? "valid" :
266 "expired", keystr, v, ctime(&t)));
276 return t > time(NULL);
279 SAFE_FREE(databuf.dptr);
287 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
296 * Iterate through all entries which key matches to specified pattern
298 * @param fn pointer to the function that will be supplied with each single
299 * matching cache entry (key, value and timeout) as an arguments
300 * @param data void pointer to an arbitrary data that is passed directly to the fn
301 * function on each call
302 * @param keystr_pattern pattern the existing entries' keys are matched to
306 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
307 void* data, const char* keystr_pattern)
309 TDB_LIST_NODE *node, *first_node;
311 char *keystr = NULL, *valstr = NULL, *entry = NULL;
315 /* fail completely if get null pointers passed */
316 SMB_ASSERT(fn && keystr_pattern);
318 if (!gencache_init()) return;
320 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
321 node = tdb_search_keys(cache->tdb, keystr_pattern);
325 /* ensure null termination of the key string */
326 keystr = strndup(node->node_key.dptr, node->node_key.dsize);
329 * We don't use gencache_get function, because we need to iterate through
330 * all of the entries. Validity verification is up to fn routine.
332 databuf = tdb_fetch(cache->tdb, node->node_key);
333 if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
334 SAFE_FREE(databuf.dptr);
339 entry = strndup(databuf.dptr, databuf.dsize);
340 SAFE_FREE(databuf.dptr);
341 valstr = malloc_array_p(char, databuf.dsize - TIMEOUT_LEN);
342 sscanf(entry, CACHE_DATA_FMT, (int*)(&i), valstr);
345 DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
346 keystr, valstr, ctime(&timeout)));
347 fn(keystr, valstr, timeout, data);
355 tdb_search_list_free(first_node);
358 /********************************************************************
360 ********************************************************************/
362 int gencache_lock_entry( const char *key )
364 return tdb_lock_bystring(cache->tdb, key, 0);
367 /********************************************************************
369 ********************************************************************/
371 void gencache_unlock_entry( const char *key )
373 tdb_unlock_bystring(cache->tdb, key);