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
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)
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
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
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
,
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,
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
80 * 'user_data' will be passed to all of the above functions.
82 GFSCache
*g_fscache_new(GFSLoadFunc load
,
91 cache
= g_new(GFSCache
, 1);
92 cache
->inode_to_stats
= g_hash_table_new(hash_key
, cmp_stats
);
96 cache
->getref
= getref
;
97 cache
->update
= update
;
98 cache
->user_data
= user_data
;
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
);
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
);
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
);
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
)
145 g_return_val_if_fail(cache
!= NULL
, NULL
);
146 g_return_val_if_fail(pathname
!= NULL
, NULL
);
148 if (mc_stat(pathname
, &info
))
151 key
.device
= info
.st_dev
;
152 key
.inode
= info
.st_ino
;
154 data
= g_hash_table_lookup(cache
->inode_to_stats
, &key
);
158 /* We've cached this file already - is it up-to-date? */
160 if (UPTODATE(data
, info
))
165 cache
->update(data
->data
, pathname
, cache
->user_data
);
169 cache
->unref(data
->data
, cache
->user_data
);
175 GFSCacheKey
*new_key
;
177 new_key
= g_memdup(&key
, sizeof(key
));
179 data
= g_new(GFSCacheData
, 1);
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) */
193 data
->data
= cache
->load(pathname
, cache
->user_data
);
197 cache
->ref(data
->data
, cache
->user_data
);
198 data
->last_lookup
= time(NULL
);
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
)
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
))
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
)
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
))
246 key
.device
= info
.st_dev
;
247 key
.inode
= info
.st_ino
;
249 data
= g_hash_table_lookup(cache
->inode_to_stats
, &key
);
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
263 void g_fscache_purge(GFSCache
*cache
, gint age
)
265 struct PurgeInfo info
;
267 g_return_if_fail(cache
!= NULL
);
271 info
.now
= time(NULL
);
273 g_hash_table_foreach_remove(cache
->inode_to_stats
, purge_hash_entry
,
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
;
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
;
306 cache
->unref(cache_data
->data
, cache
->user_data
);
312 static gboolean
purge_hash_entry(gpointer key
, gpointer 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 */
321 cache
->getref(cache_data
->data
, cache
->user_data
) > 1)
324 if (cache_data
->last_lookup
<= info
->now
325 && cache_data
->last_lookup
>= info
->now
- info
->age
)
329 cache
->unref(cache_data
->data
, cache
->user_data
);