r18311: Simplify gencache_get by using strtol instead of sscanf
[Samba.git] / source / lib / gencache.c
blob871d1d1d8020ad41b4a389c5a8c02a92c57ed53c
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
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.
24 #include "includes.h"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_TDB
29 #define TIMEOUT_LEN 12
30 #define CACHE_DATA_FMT "%12u/%s"
31 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
33 static TDB_CONTEXT *cache;
35 /**
36 * @file gencache.c
37 * @brief Generic, persistent and shared between processes cache mechanism
38 * for use by various parts of the Samba code
40 **/
43 /**
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
48 * false on failure
49 **/
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");
59 if (cache_fname == NULL) {
60 DEBUG(0, ("Filename allocation failed.\n"));
61 return False;
64 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
66 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
67 O_RDWR|O_CREAT, 0644);
69 SAFE_FREE(cache_fname);
70 if (!cache) {
71 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
72 return False;
74 return True;
78 /**
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
83 **/
85 BOOL gencache_shutdown(void)
87 int ret;
88 /* tdb_close routine returns -1 on error */
89 if (!cache) return False;
90 DEBUG(5, ("Closing cache file\n"));
91 ret = tdb_close(cache);
92 cache = NULL;
93 return ret != -1;
97 /**
98 * Set an entry in the cache file. If there's no such
99 * one, then add it.
101 * @param keystr string that represents a key of this entry
102 * @param value text representation value being cached
103 * @param timeout time when the value is expired
105 * @retval true when entry is successfuly stored
106 * @retval false on failure
109 BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
111 int ret;
112 TDB_DATA keybuf, databuf;
113 char* valstr = NULL;
115 /* fail completely if get null pointers passed */
116 SMB_ASSERT(keystr && value);
118 if (!gencache_init()) return False;
120 asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
121 if (!valstr)
122 return False;
124 keybuf.dptr = CONST_DISCARD(char *, keystr);
125 keybuf.dsize = strlen(keystr)+1;
126 databuf.dptr = valstr;
127 databuf.dsize = strlen(valstr)+1;
128 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
129 " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout),
130 (int)(timeout - time(NULL)),
131 timeout > time(NULL) ? "ahead" : "in the past"));
133 ret = tdb_store(cache, keybuf, databuf, 0);
134 SAFE_FREE(valstr);
136 return ret == 0;
140 * Delete one entry from the cache file.
142 * @param keystr string that represents a key of this entry
144 * @retval true upon successful deletion
145 * @retval false in case of failure
148 BOOL gencache_del(const char *keystr)
150 int ret;
151 TDB_DATA keybuf;
153 /* fail completely if get null pointers passed */
154 SMB_ASSERT(keystr);
156 if (!gencache_init()) return False;
158 keybuf.dptr = CONST_DISCARD(char *, keystr);
159 keybuf.dsize = strlen(keystr)+1;
160 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
161 ret = tdb_delete(cache, keybuf);
163 return ret == 0;
168 * Get existing entry from the cache file.
170 * @param keystr string that represents a key of this entry
171 * @param valstr buffer that is allocated and filled with the entry value
172 * buffer's disposing must be done outside
173 * @param timeout pointer to a time_t that is filled with entry's
174 * timeout
176 * @retval true when entry is successfuly fetched
177 * @retval False for failure
180 BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
182 TDB_DATA keybuf, databuf;
183 time_t t;
184 char *endptr;
186 /* fail completely if get null pointers passed */
187 SMB_ASSERT(keystr);
189 if (!gencache_init()) {
190 return False;
193 keybuf.dptr = CONST_DISCARD(char *, keystr);
194 keybuf.dsize = strlen(keystr)+1;
195 databuf = tdb_fetch(cache, keybuf);
197 if (databuf.dptr == NULL) {
198 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
199 keystr));
200 return False;
203 t = strtol(databuf.dptr, &endptr, 10);
205 if ((endptr == NULL) || (*endptr != '/')) {
206 DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
207 SAFE_FREE(databuf.dptr);
208 return False;
211 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
212 "timeout = %s", t > time(NULL) ? "valid" :
213 "expired", keystr, endptr+1, ctime(&t)));
215 if (valstr) {
216 *valstr = SMB_STRDUP(endptr+1);
217 if (*valstr == NULL) {
218 SAFE_FREE(databuf.dptr);
219 DEBUG(0, ("strdup failed\n"));
220 return False;
224 SAFE_FREE(databuf.dptr);
226 if (timeout) {
227 *timeout = t;
230 return t > time(NULL);
235 * Iterate through all entries which key matches to specified pattern
237 * @param fn pointer to the function that will be supplied with each single
238 * matching cache entry (key, value and timeout) as an arguments
239 * @param data void pointer to an arbitrary data that is passed directly to the fn
240 * function on each call
241 * @param keystr_pattern pattern the existing entries' keys are matched to
245 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
246 void* data, const char* keystr_pattern)
248 TDB_LIST_NODE *node, *first_node;
249 TDB_DATA databuf;
250 char *keystr = NULL, *valstr = NULL, *entry = NULL;
251 time_t timeout = 0;
252 int status;
253 unsigned u;
255 /* fail completely if get null pointers passed */
256 SMB_ASSERT(fn && keystr_pattern);
258 if (!gencache_init()) return;
260 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
261 node = tdb_search_keys(cache, keystr_pattern);
262 first_node = node;
264 while (node) {
265 char *fmt;
267 /* ensure null termination of the key string */
268 keystr = SMB_STRNDUP(node->node_key.dptr, node->node_key.dsize);
269 if (!keystr) {
270 break;
274 * We don't use gencache_get function, because we need to iterate through
275 * all of the entries. Validity verification is up to fn routine.
277 databuf = tdb_fetch(cache, node->node_key);
278 if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
279 SAFE_FREE(databuf.dptr);
280 SAFE_FREE(keystr);
281 node = node->next;
282 continue;
284 entry = SMB_STRNDUP(databuf.dptr, databuf.dsize);
285 if (!entry) {
286 SAFE_FREE(databuf.dptr);
287 SAFE_FREE(keystr);
288 break;
291 SAFE_FREE(databuf.dptr);
293 valstr = (char *)SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN);
294 if (!valstr) {
295 SAFE_FREE(entry);
296 SAFE_FREE(keystr);
297 break;
300 asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN);
301 if (!fmt) {
302 SAFE_FREE(valstr);
303 SAFE_FREE(entry);
304 SAFE_FREE(keystr);
305 break;
307 status = sscanf(entry, fmt, &u, valstr);
308 SAFE_FREE(fmt);
310 if ( status != 2 ) {
311 DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status));
313 timeout = u;
315 DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
316 keystr, valstr, ctime(&timeout)));
317 fn(keystr, valstr, timeout, data);
319 SAFE_FREE(valstr);
320 SAFE_FREE(entry);
321 SAFE_FREE(keystr);
322 node = node->next;
325 tdb_search_list_free(first_node);
328 /********************************************************************
329 lock a key
330 ********************************************************************/
332 int gencache_lock_entry( const char *key )
334 if (!gencache_init())
335 return -1;
337 return tdb_lock_bystring(cache, key);
340 /********************************************************************
341 unlock a key
342 ********************************************************************/
344 void gencache_unlock_entry( const char *key )
346 if (!gencache_init())
347 return;
349 tdb_unlock_bystring(cache, key);
350 return;