r339: The menu key bindings are now only saved if they actually changed.
[rox-filer.git] / ROX-Filer / src / fscache.c
blob09d8530c1bfee663ab8da42debc181095a5dcd94
1 /*
2 * $Id$
4 * FSCache - a glib-style object for caching files
5 * Copyright (C) 2000, Thomas Leonard, <tal197@users.sourceforge.net>.
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 "fscache.h"
35 #define UPTODATE(data, info) \
36 (data->m_time == info.st_mtime \
37 && data->length == info.st_size \
38 && data->mode == info.st_mode) \
41 /* Static prototypes */
43 static guint hash_key(gconstpointer key);
44 static gint cmp_stats(gconstpointer a, gconstpointer b);
45 static void destroy_hash_entry(gpointer key, gpointer data, gpointer user_data);
46 static gboolean purge_hash_entry(gpointer key, gpointer data,
47 gpointer user_data);
50 struct PurgeInfo
52 GFSCache *cache;
53 gint age;
54 time_t now;
57 /****************************************************************
58 * EXTERNAL INTERFACE *
59 ****************************************************************/
62 /* Create a new GFSCache object and return a pointer to it.
64 * When someone tries to lookup a file which is not in the cache,
65 * load() is called.
66 * It should load the file and return a pointer to an object for the file.
67 * The object should have a ref count of 1.
69 * ref() and unref() should modify the reference counter of the object.
70 * When the counter reaches zero, destroy the object.
72 * getref() returns the current value. This can be used to stop objects
73 * being purged from the cache while they are being used elsewhere, which
74 * is rather wasteful. If NULL then this check isn't done.
76 * update() will be called to update an object which is cached, but
77 * out of date. If NULL, the object will be unref'd and load() used
78 * to make a new one.
80 * 'user_data' will be passed to all of the above functions.
82 GFSCache *g_fscache_new(GFSLoadFunc load,
83 GFSRefFunc ref,
84 GFSRefFunc unref,
85 GFSGetRefFunc getref,
86 GFSUpdateFunc update,
87 gpointer user_data)
89 GFSCache *cache;
91 cache = g_new(GFSCache, 1);
92 cache->inode_to_stats = g_hash_table_new(hash_key, cmp_stats);
93 cache->load = load;
94 cache->ref = ref;
95 cache->unref = unref;
96 cache->getref = getref;
97 cache->update = update;
98 cache->user_data = user_data;
100 return cache;
103 void g_fscache_destroy(GFSCache *cache)
105 g_return_if_fail(cache != NULL);
107 g_hash_table_foreach(cache->inode_to_stats, destroy_hash_entry, cache);
108 g_hash_table_destroy(cache->inode_to_stats);
110 g_free(cache);
113 /* Call the ref() user function for this object */
114 void g_fscache_data_ref(GFSCache *cache, gpointer data)
116 g_return_if_fail(cache != NULL);
118 if (cache->ref)
119 cache->ref(data, cache->user_data);
122 /* Call the unref() user function for this object */
123 void g_fscache_data_unref(GFSCache *cache, gpointer data)
125 g_return_if_fail(cache != NULL);
127 if (cache->unref)
128 cache->unref(data, cache->user_data);
131 /* Find the data for this file in the cache, loading it into
132 * the cache if it isn't there already.
134 * Remember to g_fscache_data_unref() the returned value when
135 * you're done with it.
137 * Returns NULL on failure.
139 gpointer g_fscache_lookup(GFSCache *cache, char *pathname)
141 struct stat info;
142 GFSCacheKey key;
143 GFSCacheData *data;
145 g_return_val_if_fail(cache != NULL, NULL);
146 g_return_val_if_fail(pathname != NULL, NULL);
148 if (mc_stat(pathname, &info))
149 return NULL;
151 key.device = info.st_dev;
152 key.inode = info.st_ino;
154 data = g_hash_table_lookup(cache->inode_to_stats, &key);
156 if (data)
158 /* We've cached this file already - is it up-to-date? */
160 if (UPTODATE(data, info))
161 goto out;
163 /* Out-of-date */
164 if (cache->update)
165 cache->update(data->data, pathname, cache->user_data);
166 else
168 if (cache->unref)
169 cache->unref(data->data, cache->user_data);
170 data->data = NULL;
173 else
175 GFSCacheKey *new_key;
177 new_key = g_memdup(&key, sizeof(key));
179 data = g_new(GFSCacheData, 1);
180 data->data = NULL;
182 g_hash_table_insert(cache->inode_to_stats, new_key, data);
185 data->m_time = info.st_mtime;
186 data->length = info.st_size;
187 data->mode = info.st_mode;
189 if (data->data == NULL)
191 /* Create the object for the file (ie, not an update) */
192 if (cache->load)
193 data->data = cache->load(pathname, cache->user_data);
195 out:
196 if (cache->ref)
197 cache->ref(data->data, cache->user_data);
198 data->last_lookup = time(NULL);
199 return data->data;
202 /* Call the update() function on this item if it's in the cache
203 * AND it's out-of-date.
205 void g_fscache_may_update(GFSCache *cache, char *pathname)
207 GFSCacheKey key;
208 GFSCacheData *data;
209 struct stat info;
211 g_return_if_fail(cache != NULL);
212 g_return_if_fail(pathname != NULL);
213 g_return_if_fail(cache->update != NULL);
215 if (mc_stat(pathname, &info))
216 return;
218 key.device = info.st_dev;
219 key.inode = info.st_ino;
221 data = g_hash_table_lookup(cache->inode_to_stats, &key);
223 if (data && !UPTODATE(data, info))
225 cache->update(data->data, pathname, cache->user_data);
226 data->m_time = info.st_mtime;
227 data->length = info.st_size;
228 data->mode = info.st_mode;
232 /* Call the update() function on this item iff it's in the cache. */
233 void g_fscache_update(GFSCache *cache, char *pathname)
235 GFSCacheKey key;
236 GFSCacheData *data;
237 struct stat info;
239 g_return_if_fail(cache != NULL);
240 g_return_if_fail(pathname != NULL);
241 g_return_if_fail(cache->update != NULL);
243 if (mc_stat(pathname, &info))
244 return;
246 key.device = info.st_dev;
247 key.inode = info.st_ino;
249 data = g_hash_table_lookup(cache->inode_to_stats, &key);
251 if (data)
253 cache->update(data->data, pathname, cache->user_data);
254 data->m_time = info.st_mtime;
255 data->length = info.st_size;
256 data->mode = info.st_mode;
260 /* Remove all cache entries last accessed more than 'age' seconds
261 * ago.
263 void g_fscache_purge(GFSCache *cache, gint age)
265 struct PurgeInfo info;
267 g_return_if_fail(cache != NULL);
269 info.age = age;
270 info.cache = cache;
271 info.now = time(NULL);
273 g_hash_table_foreach_remove(cache->inode_to_stats, purge_hash_entry,
274 (gpointer) &info);
278 /****************************************************************
279 * INTERNAL FUNCTIONS *
280 ****************************************************************/
283 /* Generate a hash number for some stats */
284 static guint hash_key(gconstpointer key)
286 GFSCacheKey *stats = (GFSCacheKey *) key;
288 return stats->inode;
291 /* See if two stats blocks represent the same file */
292 static gint cmp_stats(gconstpointer a, gconstpointer b)
294 GFSCacheKey *c = (GFSCacheKey *) a;
295 GFSCacheKey *d = (GFSCacheKey *) b;
297 return c->device == d->device && c->inode == d->inode;
300 static void destroy_hash_entry(gpointer key, gpointer data, gpointer user_data)
302 GFSCache *cache = (GFSCache *) user_data;
303 GFSCacheData *cache_data = (GFSCacheData *) data;
305 if (cache->unref)
306 cache->unref(cache_data->data, cache->user_data);
308 g_free(key);
309 g_free(data);
312 static gboolean purge_hash_entry(gpointer key, gpointer data,
313 gpointer user_data)
315 struct PurgeInfo *info = (struct PurgeInfo *) user_data;
316 GFSCacheData *cache_data = (GFSCacheData *) data;
317 GFSCache *cache = info->cache;
319 /* It's wasteful to remove an entry if someone else is using it */
320 if (cache->getref &&
321 cache->getref(cache_data->data, cache->user_data) > 1)
322 return FALSE;
324 if (cache_data->last_lookup <= info->now
325 && cache_data->last_lookup >= info->now - info->age)
326 return FALSE;
328 if (cache->unref)
329 cache->unref(cache_data->data, cache->user_data);
331 g_free(key);
332 g_free(data);
334 return TRUE;