r4275: If we can't find libc.so.6 for xattr support, try libc.so in case we only...
[rox-filer.git] / ROX-Filer / src / fscache.c
blobdd15d2142c7b0f6d1351c64d44ba893ad80d054c
1 /*
2 * $Id$
4 * FSCache - a glib-style object for caching files
5 * Copyright (C) 2005, 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, c_time;
61 off_t length;
62 mode_t mode;
65 #define UPTODATE(data, info) \
66 (data->m_time == info.st_mtime \
67 && data->c_time == info.st_ctime \
68 && data->length == info.st_size \
69 && data->mode == info.st_mode) \
72 /* Static prototypes */
74 static guint hash_key(gconstpointer key);
75 static gint cmp_stats(gconstpointer a, gconstpointer b);
76 static void destroy_hash_entry(gpointer key, gpointer data, gpointer user_data);
77 static gboolean purge_hash_entry(gpointer key, gpointer data,
78 gpointer user_data);
79 static GFSCacheData *lookup_internal(GFSCache *cache, const char *pathname,
80 FSCacheLookup lookup_type);
83 struct PurgeInfo
85 GFSCache *cache;
86 gint age;
87 time_t now;
90 /****************************************************************
91 * EXTERNAL INTERFACE *
92 ****************************************************************/
95 /* Create a new GFSCache object and return a pointer to it.
97 * When someone tries to lookup a file which is not in the cache,
98 * load() is called.
99 * It should load the file and return a pointer to an object for the file.
100 * The object should have a ref count of 1.
102 * update() will be called to update an object which is cached, but
103 * out of date. If NULL, the object will be unref'd and load() used
104 * to make a new one.
106 * 'user_data' will be passed to all of the above functions.
108 GFSCache *g_fscache_new(GFSLoadFunc load,
109 GFSUpdateFunc update,
110 gpointer user_data)
112 GFSCache *cache;
114 cache = g_new(GFSCache, 1);
115 cache->inode_to_stats = g_hash_table_new(hash_key, cmp_stats);
116 cache->load = load;
117 cache->update = update;
118 cache->user_data = user_data;
120 return cache;
123 void g_fscache_destroy(GFSCache *cache)
125 g_return_if_fail(cache != NULL);
127 g_hash_table_foreach(cache->inode_to_stats, destroy_hash_entry, NULL);
128 g_hash_table_destroy(cache->inode_to_stats);
130 g_free(cache);
133 /* Find the data for this file in the cache, loading it into
134 * the cache if it isn't there already.
136 * Remember to g_object_unref() the returned value when
137 * you're done with it.
139 * Returns NULL on failure.
141 gpointer g_fscache_lookup(GFSCache *cache, const char *pathname)
143 return g_fscache_lookup_full(cache, pathname,
144 FSCACHE_LOOKUP_CREATE, NULL);
147 /* Force this already-loaded item into the cache. The cache will
148 * ref the object if it wants to keep it.
149 * If update_details is FALSE then the timestamp and size aren't recorded.
150 * Generally, you call this function with update_details = TRUE when you
151 * start loading some data and then with update_details = FALSE when you
152 * put in the loaded object.
154 void g_fscache_insert(GFSCache *cache, const char *pathname, gpointer obj,
155 gboolean update_details)
157 GFSCacheData *data;
159 data = lookup_internal(cache, pathname,
160 update_details ? FSCACHE_LOOKUP_INIT
161 : FSCACHE_LOOKUP_INSERT);
163 if (!data)
164 return;
166 if (obj)
167 g_object_ref(obj);
168 if (data->data)
169 g_object_unref(data->data);
170 data->data = obj;
173 /* As g_fscache_lookup, but 'lookup_type' controls what happens if the data
174 * is out-of-date.
175 * If found is not NULL, use it to indicate whether something is being
176 * returned (a NULL return could indicate that the data is cached, but
177 * the data is NULL).
178 * If returned value is not NULL, value is refd.
180 gpointer g_fscache_lookup_full(GFSCache *cache, const char *pathname,
181 FSCacheLookup lookup_type,
182 gboolean *found)
184 GFSCacheData *data;
186 g_return_val_if_fail(lookup_type != FSCACHE_LOOKUP_INIT, NULL);
188 data = lookup_internal(cache, pathname, lookup_type);
190 if (!data)
192 if (found)
193 *found = FALSE;
194 return NULL;
197 if (found)
198 *found = TRUE;
200 if (data->data)
201 g_object_ref(data->data);
203 return data->data;
206 /* Call the update() function on this item if it's in the cache
207 * AND it's out-of-date.
209 void g_fscache_may_update(GFSCache *cache, const char *pathname)
211 GFSCacheKey key;
212 GFSCacheData *data;
213 struct stat info;
215 g_return_if_fail(cache != NULL);
216 g_return_if_fail(pathname != NULL);
217 g_return_if_fail(cache->update != NULL);
219 if (mc_stat(pathname, &info))
220 return;
222 key.device = info.st_dev;
223 key.inode = info.st_ino;
225 data = g_hash_table_lookup(cache->inode_to_stats, &key);
227 if (data && !UPTODATE(data, info))
229 cache->update(data->data, pathname, cache->user_data);
230 data->m_time = info.st_mtime;
231 data->c_time = info.st_ctime;
232 data->length = info.st_size;
233 data->mode = info.st_mode;
237 /* Call the update() function on this item iff it's in the cache. */
238 void g_fscache_update(GFSCache *cache, const char *pathname)
240 GFSCacheKey key;
241 GFSCacheData *data;
242 struct stat info;
244 g_return_if_fail(cache != NULL);
245 g_return_if_fail(pathname != NULL);
246 g_return_if_fail(cache->update != NULL);
248 if (mc_stat(pathname, &info))
249 return;
251 key.device = info.st_dev;
252 key.inode = info.st_ino;
254 data = g_hash_table_lookup(cache->inode_to_stats, &key);
256 if (data)
258 cache->update(data->data, pathname, cache->user_data);
259 data->m_time = info.st_mtime;
260 data->c_time = info.st_ctime;
261 data->length = info.st_size;
262 data->mode = info.st_mode;
266 /* Remove all cache entries last accessed more than 'age' seconds
267 * ago.
269 void g_fscache_purge(GFSCache *cache, gint age)
271 struct PurgeInfo info;
273 g_return_if_fail(cache != NULL);
275 info.age = age;
276 info.cache = cache;
277 info.now = time(NULL);
279 g_hash_table_foreach_remove(cache->inode_to_stats, purge_hash_entry,
280 (gpointer) &info);
284 /****************************************************************
285 * INTERNAL FUNCTIONS *
286 ****************************************************************/
289 /* Generate a hash number for some stats */
290 static guint hash_key(gconstpointer key)
292 GFSCacheKey *stats = (GFSCacheKey *) key;
294 return stats->inode;
297 /* See if two stats blocks represent the same file */
298 static gint cmp_stats(gconstpointer a, gconstpointer b)
300 GFSCacheKey *c = (GFSCacheKey *) a;
301 GFSCacheKey *d = (GFSCacheKey *) b;
303 return c->device == d->device && c->inode == d->inode;
306 static void destroy_hash_entry(gpointer key, gpointer data, gpointer user_data)
308 GFSCacheData *cache_data = (GFSCacheData *) data;
310 if (cache_data->data)
311 g_object_unref(cache_data->data);
313 g_free(key);
314 g_free(data);
317 static gboolean purge_hash_entry(gpointer key, gpointer data,
318 gpointer user_data)
320 struct PurgeInfo *info = (struct PurgeInfo *) user_data;
321 GFSCacheData *cache_data = (GFSCacheData *) data;
323 /* It's wasteful to remove an entry if someone else is using it */
324 if (cache_data->data && cache_data->data->ref_count > 1)
325 return FALSE;
327 if (cache_data->last_lookup <= info->now
328 && cache_data->last_lookup >= info->now - info->age)
329 return FALSE;
331 if (cache_data->data)
332 g_object_unref(cache_data->data);
334 g_free(key);
335 g_free(data);
337 return TRUE;
340 /* As for g_fscache_lookup_full, but return the GFSCacheData rather than
341 * the data it contains. Doesn't increment the refcount.
343 static GFSCacheData *lookup_internal(GFSCache *cache, const char *pathname,
344 FSCacheLookup lookup_type)
346 struct stat info;
347 GFSCacheKey key;
348 GFSCacheData *data;
350 g_return_val_if_fail(cache != NULL, NULL);
351 g_return_val_if_fail(pathname != NULL, NULL);
353 if (mc_stat(pathname, &info))
354 return NULL;
356 key.device = info.st_dev;
357 key.inode = info.st_ino;
359 data = g_hash_table_lookup(cache->inode_to_stats, &key);
361 if (data)
363 /* We've cached this file already */
365 if (lookup_type == FSCACHE_LOOKUP_PEEK ||
366 lookup_type == FSCACHE_LOOKUP_INSERT)
367 goto out; /* Never update on peeks */
369 if (lookup_type == FSCACHE_LOOKUP_INIT)
370 goto init;
372 /* Is it up-to-date? */
374 if (UPTODATE(data, info))
375 goto out;
377 if (lookup_type == FSCACHE_LOOKUP_ONLY_NEW)
378 return NULL;
380 /* Out-of-date */
381 if (cache->update)
382 cache->update(data->data, pathname, cache->user_data);
383 else
385 if (data->data)
386 g_object_unref(data->data);
387 data->data = NULL;
390 else
392 GFSCacheKey *new_key;
394 if (lookup_type != FSCACHE_LOOKUP_CREATE &&
395 lookup_type != FSCACHE_LOOKUP_INIT)
396 return NULL;
398 new_key = g_memdup(&key, sizeof(key));
400 data = g_new(GFSCacheData, 1);
401 data->data = NULL;
403 g_hash_table_insert(cache->inode_to_stats, new_key, data);
406 init:
407 data->m_time = info.st_mtime;
408 data->c_time = info.st_ctime;
409 data->length = info.st_size;
410 data->mode = info.st_mode;
412 if (data->data == NULL &&
413 lookup_type != FSCACHE_LOOKUP_INIT &&
414 lookup_type != FSCACHE_LOOKUP_INSERT)
416 /* Create the object for the file (ie, not an update) */
417 if (cache->load)
418 data->data = cache->load(pathname, cache->user_data);
420 out:
421 data->last_lookup = time(NULL);
423 return data;