r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / lib / gencache.c
blobc58546da9c0948051d4b34fcaf9d68811973bc22
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 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/>.
23 #include "includes.h"
25 #undef DBGC_CLASS
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"
32 static TDB_CONTEXT *cache;
33 static BOOL cache_readonly;
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 cache_fname = lock_path("gencache.tdb");
60 DEBUG(5, ("Opening cache file at %s\n", cache_fname));
62 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
63 O_RDWR|O_CREAT, 0644);
65 if (!cache && (errno == EACCES)) {
66 cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDONLY, 0644);
67 if (cache) {
68 cache_readonly = True;
69 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
73 if (!cache) {
74 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
75 return False;
77 return True;
81 /**
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
86 **/
88 BOOL gencache_shutdown(void)
90 int ret;
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);
95 cache = NULL;
96 cache_readonly = False;
97 return ret != -1;
102 * Set an entry in the cache file. If there's no such
103 * one, then add it.
105 * @param keystr string that represents a key of this entry
106 * @param value text representation value being cached
107 * @param timeout time when the value is expired
109 * @retval true when entry is successfuly stored
110 * @retval false on failure
113 BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
115 int ret;
116 TDB_DATA databuf;
117 char* valstr = NULL;
119 /* fail completely if get null pointers passed */
120 SMB_ASSERT(keystr && value);
122 if (!gencache_init()) return False;
124 if (cache_readonly) {
125 return False;
128 asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
129 if (!valstr)
130 return False;
132 databuf = string_term_tdb_data(valstr);
133 DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
134 " %s (%d seconds %s)\n", keystr, value,ctime(&timeout),
135 (int)(timeout - time(NULL)),
136 timeout > time(NULL) ? "ahead" : "in the past"));
138 ret = tdb_store_bystring(cache, keystr, databuf, 0);
139 SAFE_FREE(valstr);
141 return ret == 0;
145 * Delete one entry from the cache file.
147 * @param keystr string that represents a key of this entry
149 * @retval true upon successful deletion
150 * @retval false in case of failure
153 BOOL gencache_del(const char *keystr)
155 int ret;
157 /* fail completely if get null pointers passed */
158 SMB_ASSERT(keystr);
160 if (!gencache_init()) return False;
162 if (cache_readonly) {
163 return False;
166 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
167 ret = tdb_delete_bystring(cache, keystr);
169 return ret == 0;
174 * Get existing entry from the cache file.
176 * @param keystr string that represents a key of this entry
177 * @param valstr buffer that is allocated and filled with the entry value
178 * buffer's disposing must be done outside
179 * @param timeout pointer to a time_t that is filled with entry's
180 * timeout
182 * @retval true when entry is successfuly fetched
183 * @retval False for failure
186 BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
188 TDB_DATA databuf;
189 time_t t;
190 char *endptr;
192 /* fail completely if get null pointers passed */
193 SMB_ASSERT(keystr);
195 if (!gencache_init()) {
196 return False;
199 databuf = tdb_fetch_bystring(cache, keystr);
201 if (databuf.dptr == NULL) {
202 DEBUG(10, ("Cache entry with key = %s couldn't be found\n",
203 keystr));
204 return False;
207 t = strtol((const char *)databuf.dptr, &endptr, 10);
209 if ((endptr == NULL) || (*endptr != '/')) {
210 DEBUG(2, ("Invalid gencache data format: %s\n", databuf.dptr));
211 SAFE_FREE(databuf.dptr);
212 return False;
215 DEBUG(10, ("Returning %s cache entry: key = %s, value = %s, "
216 "timeout = %s", t > time(NULL) ? "valid" :
217 "expired", keystr, endptr+1, ctime(&t)));
219 if (t <= time(NULL)) {
221 /* We're expired, delete the entry */
222 tdb_delete_bystring(cache, keystr);
224 SAFE_FREE(databuf.dptr);
225 return False;
228 if (valstr) {
229 *valstr = SMB_STRDUP(endptr+1);
230 if (*valstr == NULL) {
231 SAFE_FREE(databuf.dptr);
232 DEBUG(0, ("strdup failed\n"));
233 return False;
237 SAFE_FREE(databuf.dptr);
239 if (timeout) {
240 *timeout = t;
243 return True;
248 * Iterate through all entries which key matches to specified pattern
250 * @param fn pointer to the function that will be supplied with each single
251 * matching cache entry (key, value and timeout) as an arguments
252 * @param data void pointer to an arbitrary data that is passed directly to the fn
253 * function on each call
254 * @param keystr_pattern pattern the existing entries' keys are matched to
258 void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
259 void* data, const char* keystr_pattern)
261 TDB_LIST_NODE *node, *first_node;
262 TDB_DATA databuf;
263 char *keystr = NULL, *valstr = NULL, *entry = NULL;
264 time_t timeout = 0;
265 int status;
266 unsigned u;
268 /* fail completely if get null pointers passed */
269 SMB_ASSERT(fn && keystr_pattern);
271 if (!gencache_init()) return;
273 DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
274 node = tdb_search_keys(cache, keystr_pattern);
275 first_node = node;
277 while (node) {
278 char *fmt;
280 /* ensure null termination of the key string */
281 keystr = SMB_STRNDUP((const char *)node->node_key.dptr, node->node_key.dsize);
282 if (!keystr) {
283 break;
287 * We don't use gencache_get function, because we need to iterate through
288 * all of the entries. Validity verification is up to fn routine.
290 databuf = tdb_fetch(cache, node->node_key);
291 if (!databuf.dptr || databuf.dsize <= TIMEOUT_LEN) {
292 SAFE_FREE(databuf.dptr);
293 SAFE_FREE(keystr);
294 node = node->next;
295 continue;
297 entry = SMB_STRNDUP((const char *)databuf.dptr, databuf.dsize);
298 if (!entry) {
299 SAFE_FREE(databuf.dptr);
300 SAFE_FREE(keystr);
301 break;
304 SAFE_FREE(databuf.dptr);
306 valstr = (char *)SMB_MALLOC(databuf.dsize + 1 - TIMEOUT_LEN);
307 if (!valstr) {
308 SAFE_FREE(entry);
309 SAFE_FREE(keystr);
310 break;
313 asprintf(&fmt, READ_CACHE_DATA_FMT_TEMPLATE, (unsigned int)databuf.dsize - TIMEOUT_LEN);
314 if (!fmt) {
315 SAFE_FREE(valstr);
316 SAFE_FREE(entry);
317 SAFE_FREE(keystr);
318 break;
320 status = sscanf(entry, fmt, &u, valstr);
321 SAFE_FREE(fmt);
323 if ( status != 2 ) {
324 DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status));
326 timeout = u;
328 DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
329 keystr, valstr, ctime(&timeout)));
330 fn(keystr, valstr, timeout, data);
332 SAFE_FREE(valstr);
333 SAFE_FREE(entry);
334 SAFE_FREE(keystr);
335 node = node->next;
338 tdb_search_list_free(first_node);
341 /********************************************************************
342 lock a key
343 ********************************************************************/
345 int gencache_lock_entry( const char *key )
347 if (!gencache_init())
348 return -1;
350 return tdb_lock_bystring(cache, key);
353 /********************************************************************
354 unlock a key
355 ********************************************************************/
357 void gencache_unlock_entry( const char *key )
359 if (!gencache_init())
360 return;
362 tdb_unlock_bystring(cache, key);
363 return;