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/>.
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
;
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 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);
68 cache_readonly
= True
;
69 DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname
));
74 DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
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
88 BOOL
gencache_shutdown(void)
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
);
96 cache_readonly
= False
;
102 * Set an entry in the cache file. If there's no such
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
)
119 /* fail completely if get null pointers passed */
120 SMB_ASSERT(keystr
&& value
);
122 if (!gencache_init()) return False
;
124 if (cache_readonly
) {
128 asprintf(&valstr
, CACHE_DATA_FMT
, (int)timeout
, value
);
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);
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
)
157 /* fail completely if get null pointers passed */
160 if (!gencache_init()) return False
;
162 if (cache_readonly
) {
166 DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr
));
167 ret
= tdb_delete_bystring(cache
, keystr
);
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
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
)
192 /* fail completely if get null pointers passed */
195 if (!gencache_init()) {
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",
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
);
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
);
229 *valstr
= SMB_STRDUP(endptr
+1);
230 if (*valstr
== NULL
) {
231 SAFE_FREE(databuf
.dptr
);
232 DEBUG(0, ("strdup failed\n"));
237 SAFE_FREE(databuf
.dptr
);
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
;
263 char *keystr
= NULL
, *valstr
= NULL
, *entry
= NULL
;
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
);
280 /* ensure null termination of the key string */
281 keystr
= SMB_STRNDUP((const char *)node
->node_key
.dptr
, node
->node_key
.dsize
);
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
);
297 entry
= SMB_STRNDUP((const char *)databuf
.dptr
, databuf
.dsize
);
299 SAFE_FREE(databuf
.dptr
);
304 SAFE_FREE(databuf
.dptr
);
306 valstr
= (char *)SMB_MALLOC(databuf
.dsize
+ 1 - TIMEOUT_LEN
);
313 asprintf(&fmt
, READ_CACHE_DATA_FMT_TEMPLATE
, (unsigned int)databuf
.dsize
- TIMEOUT_LEN
);
320 status
= sscanf(entry
, fmt
, &u
, valstr
);
324 DEBUG(0,("gencache_iterate: invalid return from sscanf %d\n",status
));
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
);
338 tdb_search_list_free(first_node
);
341 /********************************************************************
343 ********************************************************************/
345 int gencache_lock_entry( const char *key
)
347 if (!gencache_init())
350 return tdb_lock_bystring(cache
, key
);
353 /********************************************************************
355 ********************************************************************/
357 void gencache_unlock_entry( const char *key
)
359 if (!gencache_init())
362 tdb_unlock_bystring(cache
, key
);