r1996: Cope slightly better with invalid filenames in various places (reported by
[rox-filer.git] / ROX-Filer / src / fscache.c
blobf668a61ca9eefee88ee0a2a49ddbd5dd2ecb0cb8
1 /*
2 * $Id$
4 * FSCache - a glib-style object for caching files
5 * Copyright (C) 2002, the ROX-Filer team.
7 * A cache object holds stat details about files in a hash table, along
8 * with user-specified data. When you want to read in a file try to
9 * get the data via the cache - if the file is cached AND has not been
10 * modified since it was last loaded the cached copy is returned, else the
11 * file is reloaded.
13 * The actual data need not be the raw file contents - a user specified
14 * function loads the file and associates data with the file in the cache.
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
19 * any later version.
21 * This program is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24 * more details.
26 * You should have received a copy of the GNU General Public License along with
27 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
28 * Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "config.h"
33 #include "global.h"
35 #include "fscache.h"
37 typedef struct _GFSCacheKey GFSCacheKey;
38 typedef struct _GFSCacheData GFSCacheData;
40 struct _GFSCache
42 GHashTable *inode_to_stats;
43 GFSLoadFunc load;
44 GFSUpdateFunc update;
45 gpointer user_data;
48 struct _GFSCacheKey
50 dev_t device;
51 ino_t inode;
54 struct _GFSCacheData
56 GObject *data; /* The object from the file */
57 time_t last_lookup;
59 /* Details of the file last time we checked it */
60 time_t m_time;
61 off_t length;
62 mode_t mode;
65 #define UPTODATE(data, info) \
66 (data->m_time == info.st_mtime \
67 && data->length == info.st_size \
68 && data->mode == info.st_mode) \
71 /* Static prototypes */
73 static guint hash_key(gconstpointer key);
74 static gint cmp_stats(gconstpointer a, gconstpointer b);
75 static void destroy_hash_entry(gpointer key, gpointer data, gpointer user_data);
76 static gboolean purge_hash_entry(gpointer key, gpointer data,
77 gpointer user_data);
78 static GFSCacheData *lookup_internal(GFSCache *cache, const char *pathname,
79 FSCacheLookup lookup_type);
82 struct PurgeInfo
84 GFSCache *cache;
85 gint age;
86 time_t now;
89 /****************************************************************
90 * EXTERNAL INTERFACE *
91 ****************************************************************/
94 /* Create a new GFSCache object and return a pointer to it.
96 * When someone tries to lookup a file which is not in the cache,
97 * load() is called.
98 * It should load the file and return a pointer to an object for the file.
99 * The object should have a ref count of 1.
101 * ref() and unref() should modify the reference counter of the object.
102 * When the counter reaches zero, destroy the object.
104 * getref() returns the current value. This can be used to stop objects
105 * being purged from the cache while they are being used elsewhere, which
106 * is rather wasteful. If NULL then this check isn't done.
108 * update() will be called to update an object which is cached, but
109 * out of date. If NULL, the object will be unref'd and load() used
110 * to make a new one.
112 * 'user_data' will be passed to all of the above functions.
114 GFSCache *g_fscache_new(GFSLoadFunc load,
115 GFSUpdateFunc update,
116 gpointer user_data)
118 GFSCache *cache;
120 cache = g_new(GFSCache, 1);
121 cache->inode_to_stats = g_hash_table_new(hash_key, cmp_stats);
122 cache->load = load;
123 cache->update = update;
124 cache->user_data = user_data;
126 return cache;
129 void g_fscache_destroy(GFSCache *cache)
131 g_return_if_fail(cache != NULL);
133 g_hash_table_foreach(cache->inode_to_stats, destroy_hash_entry, NULL);
134 g_hash_table_destroy(cache->inode_to_stats);
136 g_free(cache);
139 /* Find the data for this file in the cache, loading it into
140 * the cache if it isn't there already.
142 * Remember to g_object_unref() the returned value when
143 * you're done with it.
145 * Returns NULL on failure.
147 gpointer g_fscache_lookup(GFSCache *cache, const char *pathname)
149 return g_fscache_lookup_full(cache, pathname,
150 FSCACHE_LOOKUP_CREATE, NULL);
153 /* Force this already-loaded item into the cache. The cache will
154 * ref the object if it wants to keep it.
155 * If update_details is FALSE then the timestamp and size aren't recorded.
156 * Generally, you call this function with update_details = TRUE when you
157 * start loading some data and then with update_details = FALSE when you
158 * put in the loaded object.
160 void g_fscache_insert(GFSCache *cache, const char *pathname, gpointer obj,
161 gboolean update_details)
163 GFSCacheData *data;
165 data = lookup_internal(cache, pathname,
166 update_details ? FSCACHE_LOOKUP_INIT
167 : FSCACHE_LOOKUP_INSERT);
169 if (!data)
170 return;
172 if (obj)
173 g_object_ref(obj);
174 if (data->data)
175 g_object_unref(data->data);
176 data->data = obj;
179 /* As g_fscache_lookup, but 'lookup_type' controls what happens if the data
180 * is out-of-date.
181 * If found is not NULL, use it to indicate whether something is being
182 * returned (a NULL return could indicate that the data is cached, but
183 * the data is NULL).
184 * If returned value is not NULL, value is refd.
186 gpointer g_fscache_lookup_full(GFSCache *cache, const char *pathname,
187 FSCacheLookup lookup_type,
188 gboolean *found)
190 GFSCacheData *data;
192 g_return_val_if_fail(lookup_type != FSCACHE_LOOKUP_INIT, NULL);
194 data = lookup_internal(cache, pathname, lookup_type);
196 if (!data)
198 if (found)
199 *found = FALSE;
200 return NULL;
203 if (found)
204 *found = TRUE;
206 if (data->data)
207 g_object_ref(data->data);
209 return data->data;
212 /* Call the update() function on this item if it's in the cache
213 * AND it's out-of-date.
215 void g_fscache_may_update(GFSCache *cache, const char *pathname)
217 GFSCacheKey key;
218 GFSCacheData *data;
219 struct stat info;
221 g_return_if_fail(cache != NULL);
222 g_return_if_fail(pathname != NULL);
223 g_return_if_fail(cache->update != NULL);
225 if (mc_stat(pathname, &info))
226 return;
228 key.device = info.st_dev;
229 key.inode = info.st_ino;
231 data = g_hash_table_lookup(cache->inode_to_stats, &key);
233 if (data && !UPTODATE(data, info))
235 cache->update(data->data, pathname, cache->user_data);
236 data->m_time = info.st_mtime;
237 data->length = info.st_size;
238 data->mode = info.st_mode;
242 /* Call the update() function on this item iff it's in the cache. */
243 void g_fscache_update(GFSCache *cache, const char *pathname)
245 GFSCacheKey key;
246 GFSCacheData *data;
247 struct stat info;
249 g_return_if_fail(cache != NULL);
250 g_return_if_fail(pathname != NULL);
251 g_return_if_fail(cache->update != NULL);
253 if (mc_stat(pathname, &info))
254 return;
256 key.device = info.st_dev;
257 key.inode = info.st_ino;
259 data = g_hash_table_lookup(cache->inode_to_stats, &key);
261 if (data)
263 cache->update(data->data, pathname, cache->user_data);
264 data->m_time = info.st_mtime;
265 data->length = info.st_size;
266 data->mode = info.st_mode;
270 /* Remove all cache entries last accessed more than 'age' seconds
271 * ago.
273 void g_fscache_purge(GFSCache *cache, gint age)
275 struct PurgeInfo info;
277 g_return_if_fail(cache != NULL);
279 info.age = age;
280 info.cache = cache;
281 info.now = time(NULL);
283 g_hash_table_foreach_remove(cache->inode_to_stats, purge_hash_entry,
284 (gpointer) &info);
288 /****************************************************************
289 * INTERNAL FUNCTIONS *
290 ****************************************************************/
293 /* Generate a hash number for some stats */
294 static guint hash_key(gconstpointer key)
296 GFSCacheKey *stats = (GFSCacheKey *) key;
298 return stats->inode;
301 /* See if two stats blocks represent the same file */
302 static gint cmp_stats(gconstpointer a, gconstpointer b)
304 GFSCacheKey *c = (GFSCacheKey *) a;
305 GFSCacheKey *d = (GFSCacheKey *) b;
307 return c->device == d->device && c->inode == d->inode;
310 static void destroy_hash_entry(gpointer key, gpointer data, gpointer user_data)
312 GFSCacheData *cache_data = (GFSCacheData *) data;
314 if (cache_data->data)
315 g_object_unref(cache_data->data);
317 g_free(key);
318 g_free(data);
321 static gboolean purge_hash_entry(gpointer key, gpointer data,
322 gpointer user_data)
324 struct PurgeInfo *info = (struct PurgeInfo *) user_data;
325 GFSCacheData *cache_data = (GFSCacheData *) data;
327 /* It's wasteful to remove an entry if someone else is using it */
328 if (cache_data->data && cache_data->data->ref_count > 1)
329 return FALSE;
331 if (cache_data->last_lookup <= info->now
332 && cache_data->last_lookup >= info->now - info->age)
333 return FALSE;
335 if (cache_data->data)
336 g_object_unref(cache_data->data);
338 g_free(key);
339 g_free(data);
341 return TRUE;
344 /* As for g_fscache_lookup_full, but return the GFSCacheData rather than
345 * the data it contains. Doesn't increment the refcount.
347 static GFSCacheData *lookup_internal(GFSCache *cache, const char *pathname,
348 FSCacheLookup lookup_type)
350 struct stat info;
351 GFSCacheKey key;
352 GFSCacheData *data;
354 g_return_val_if_fail(cache != NULL, NULL);
355 g_return_val_if_fail(pathname != NULL, NULL);
357 if (mc_stat(pathname, &info))
358 return NULL;
360 key.device = info.st_dev;
361 key.inode = info.st_ino;
363 data = g_hash_table_lookup(cache->inode_to_stats, &key);
365 if (data)
367 /* We've cached this file already */
369 if (lookup_type == FSCACHE_LOOKUP_PEEK ||
370 lookup_type == FSCACHE_LOOKUP_INSERT)
371 goto out; /* Never update on peeks */
373 if (lookup_type == FSCACHE_LOOKUP_INIT)
374 goto init;
376 /* Is it up-to-date? */
378 if (UPTODATE(data, info))
379 goto out;
381 if (lookup_type == FSCACHE_LOOKUP_ONLY_NEW)
382 return NULL;
384 /* Out-of-date */
385 if (cache->update)
386 cache->update(data->data, pathname, cache->user_data);
387 else
389 if (data->data)
390 g_object_unref(data->data);
391 data->data = NULL;
394 else
396 GFSCacheKey *new_key;
398 if (lookup_type != FSCACHE_LOOKUP_CREATE &&
399 lookup_type != FSCACHE_LOOKUP_INIT)
400 return NULL;
402 new_key = g_memdup(&key, sizeof(key));
404 data = g_new(GFSCacheData, 1);
405 data->data = NULL;
407 g_hash_table_insert(cache->inode_to_stats, new_key, data);
410 init:
411 data->m_time = info.st_mtime;
412 data->length = info.st_size;
413 data->mode = info.st_mode;
415 if (data->data == NULL &&
416 lookup_type != FSCACHE_LOOKUP_INIT &&
417 lookup_type != FSCACHE_LOOKUP_INSERT)
419 /* Create the object for the file (ie, not an update) */
420 if (cache->load)
421 data->data = cache->load(pathname, cache->user_data);
423 out:
424 data->last_lookup = time(NULL);
426 return data;