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
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
37 typedef struct _GFSCacheKey GFSCacheKey
;
38 typedef struct _GFSCacheData GFSCacheData
;
42 GHashTable
*inode_to_stats
;
56 GObject
*data
; /* The object from the file */
59 /* Details of the file last time we checked it */
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
,
78 static GFSCacheData
*lookup_internal(GFSCache
*cache
, const char *pathname
,
79 FSCacheLookup lookup_type
);
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,
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
112 * 'user_data' will be passed to all of the above functions.
114 GFSCache
*g_fscache_new(GFSLoadFunc load
,
115 GFSUpdateFunc update
,
120 cache
= g_new(GFSCache
, 1);
121 cache
->inode_to_stats
= g_hash_table_new(hash_key
, cmp_stats
);
123 cache
->update
= update
;
124 cache
->user_data
= user_data
;
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
);
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
)
165 data
= lookup_internal(cache
, pathname
,
166 update_details
? FSCACHE_LOOKUP_INIT
167 : FSCACHE_LOOKUP_INSERT
);
175 g_object_unref(data
->data
);
179 /* As g_fscache_lookup, but 'lookup_type' controls what happens if the data
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
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
,
192 g_return_val_if_fail(lookup_type
!= FSCACHE_LOOKUP_INIT
, NULL
);
194 data
= lookup_internal(cache
, pathname
, lookup_type
);
207 g_object_ref(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
)
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
))
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
)
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
))
256 key
.device
= info
.st_dev
;
257 key
.inode
= info
.st_ino
;
259 data
= g_hash_table_lookup(cache
->inode_to_stats
, &key
);
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
273 void g_fscache_purge(GFSCache
*cache
, gint age
)
275 struct PurgeInfo info
;
277 g_return_if_fail(cache
!= NULL
);
281 info
.now
= time(NULL
);
283 g_hash_table_foreach_remove(cache
->inode_to_stats
, purge_hash_entry
,
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
;
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
);
321 static gboolean
purge_hash_entry(gpointer key
, gpointer 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)
331 if (cache_data
->last_lookup
<= info
->now
332 && cache_data
->last_lookup
>= info
->now
- info
->age
)
335 if (cache_data
->data
)
336 g_object_unref(cache_data
->data
);
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
)
354 g_return_val_if_fail(cache
!= NULL
, NULL
);
355 g_return_val_if_fail(pathname
!= NULL
, NULL
);
357 if (mc_stat(pathname
, &info
))
360 key
.device
= info
.st_dev
;
361 key
.inode
= info
.st_ino
;
363 data
= g_hash_table_lookup(cache
->inode_to_stats
, &key
);
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
)
376 /* Is it up-to-date? */
378 if (UPTODATE(data
, info
))
381 if (lookup_type
== FSCACHE_LOOKUP_ONLY_NEW
)
386 cache
->update(data
->data
, pathname
, cache
->user_data
);
390 g_object_unref(data
->data
);
396 GFSCacheKey
*new_key
;
398 if (lookup_type
!= FSCACHE_LOOKUP_CREATE
&&
399 lookup_type
!= FSCACHE_LOOKUP_INIT
)
402 new_key
= g_memdup(&key
, sizeof(key
));
404 data
= g_new(GFSCacheData
, 1);
407 g_hash_table_insert(cache
->inode_to_stats
, new_key
, data
);
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) */
421 data
->data
= cache
->load(pathname
, cache
->user_data
);
424 data
->last_lookup
= time(NULL
);