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.
27 #define DBGC_CLASS DBGC_TDB
29 #define TIMEOUT_LEN 12
30 #define CACHE_DATA_FMT "%12u/%s"
32 static TDB_CONTEXT *cache;
36 * @brief Generic, persistent and shared between processes cache mechanism
37 * for use by various parts of the Samba code
43 * Cache initialisation function. Opens cache tdb file or creates
44 * it if does not exist.
46 * @return true on successful initialisation of the cache or
50 BOOL gencache_init(void)
52 char* cache_fname = NULL;
54 /* skip file open if it's already opened */
55 if (cache) return True;
57 asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb");
59 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
61 DEBUG(0, ("Filename allocation failed.\n"));
65 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
66 O_RDWR|O_CREAT, 0644);
68 SAFE_FREE(cache_fname);
70 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
78 * Cache shutdown function. Closes opened cache tdb file.
80 * @return true on successful closing the cache or
81 * false on failure during cache shutdown
84 BOOL gencache_shutdown(void)
86 /* tdb_close routine returns -1 on error */
87 if (!cache) return False;
88 DEBUG(5, ("Closing cache file\n"));
89 return tdb_close(cache) != -1;
94 * Set an entry in the cache file. If there's no such
97 * @param keystr string that represents a key of this entry
98 * @param value text representation value being cached
99 * @param timeout time when the value is expired
101 * @retval true when entry is successfuly stored
102 * @retval false on failure
105 BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
108 TDB_DATA keybuf, databuf;
111 /* fail completely if get null pointers passed */
112 SMB_ASSERT(keystr && value);
114 if (!gencache_init()) return False;
116 asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
117 keybuf.dptr = strdup(keystr);
118 keybuf.dsize = strlen(keystr)+1;
119 databuf.dptr = strdup(valstr);
120 databuf.dsize = strlen(valstr)+1;
121 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout \
122 = %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout),
123 (int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past"));
125 ret = tdb_store(cache, keybuf, databuf, 0);
127 SAFE_FREE(keybuf.dptr);
128 SAFE_FREE(databuf.dptr);
135 * Set existing entry to the cache file.
137 * @param keystr string that represents a key of this entry
138 * @param valstr text representation value being cached
139 * @param timeout time when the value is expired
141 * @retval true when entry is successfuly set
142 * @retval false on failure
145 BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
148 TDB_DATA keybuf, databuf;
149 char *old_valstr, *datastr;
152 /* fail completely if get null pointers passed */
153 SMB_ASSERT(keystr && valstr);
155 if (!gencache_init()) return False;
158 * Check whether entry exists in the cache
159 * Don't verify gencache_get exit code, since the entry may be expired
161 gencache_get(keystr, &old_valstr, &old_timeout);
163 if (!(old_valstr && old_timeout)) return False;
165 DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \
166 = %s\n", keystr, old_valstr, ctime(&old_timeout)));
168 asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr);
169 keybuf.dptr = strdup(keystr);
170 keybuf.dsize = strlen(keystr)+1;
171 databuf.dptr = strdup(datastr);
172 databuf.dsize = strlen(datastr)+1;
173 DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr,
174 ctime(&timeout), (int)(timeout - time(NULL)),
175 timeout > time(NULL) ? "ahead" : "in the past"));
178 ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE);
181 SAFE_FREE(old_valstr);
182 SAFE_FREE(keybuf.dptr);
183 SAFE_FREE(databuf.dptr);
190 * Delete one entry from the cache file.
192 * @param keystr string that represents a key of this entry
194 * @retval true upon successful deletion
195 * @retval false in case of failure
198 BOOL gencache_del(const char *keystr)
203 /* fail completely if get null pointers passed */
206 if (!gencache_init()) return False;
208 keybuf.dptr = strdup(keystr);
209 keybuf.dsize = strlen(keystr)+1;
210 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
211 ret = tdb_delete(cache, keybuf);
213 SAFE_FREE(keybuf.dptr);
219 * Get existing entry from the cache file.
221 * @param keystr string that represents a key of this entry
222 * @param valstr buffer that is allocated and filled with the entry value
223 * buffer's disposing must be done outside
224 * @param timeout pointer to a time_t that is filled with entry's
227 * @retval true when entry is successfuly fetched
228 * @retval False for failure
231 BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
233 TDB_DATA keybuf, databuf;
235 /* fail completely if get null pointers passed */
238 if (!gencache_init())
241 keybuf.dptr = strdup(keystr);
242 keybuf.dsize = strlen(keystr)+1;
243 databuf = tdb_fetch(cache, keybuf);
245 if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
246 char* entry_buf = strndup(databuf.dptr, databuf.dsize);
250 v = (char*)malloc(sizeof(char) *
251 (databuf.dsize - TIMEOUT_LEN));
253 SAFE_FREE(databuf.dptr);
254 sscanf(entry_buf, CACHE_DATA_FMT, (int*)&t, v);
255 SAFE_FREE(entry_buf);
257 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
258 "timeout = %s\n", t > time(NULL) ? "valid" :
259 "expired", keystr, v, ctime(&t)));
269 return t > time(NULL);
272 SAFE_FREE(databuf.dptr);
280 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
289 * Iterate through all entries which key matches to specified pattern
291 * @param fn pointer to the function that will be supplied with each single
292 * matching cache entry (key, value and timeout) as an arguments
293 * @param data void pointer to an arbitrary data that is passed directly to the fn
294 * function on each call
295 * @param keystr_pattern pattern the existing entries' keys are matched to
299 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
300 void* data, const char* keystr_pattern)
302 TDB_LIST_NODE *node, *first_node;
304 char *keystr = NULL, *valstr = NULL, *entry = NULL;
307 /* fail completely if get null pointers passed */
308 SMB_ASSERT(fn && keystr_pattern);
310 if (!gencache_init()) return;
312 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
313 node = tdb_search_keys(cache, keystr_pattern);
317 /* ensure null termination of the key string */
318 node->node_key.dptr[node->node_key.dsize] = '\0';
319 keystr = node->node_key.dptr;
322 * We don't use gencache_get function, because we need to iterate through
323 * all of the entries. Validity verification is up to fn routine.
325 databuf = tdb_fetch(cache, node->node_key);
326 if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
327 SAFE_FREE(databuf.dptr);
330 entry = strndup(databuf.dptr, databuf.dsize);
331 SAFE_FREE(databuf.dptr);
332 valstr = (char*)malloc(sizeof(char) * (databuf.dsize - TIMEOUT_LEN));
333 sscanf(entry, CACHE_DATA_FMT, (int*)(&timeout), valstr);
335 DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
336 keystr, valstr, ctime(&timeout)));
337 fn(keystr, valstr, timeout, data);
344 tdb_search_list_free(first_node);