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
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 */
60 time_t m_time
, c_time
;
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
,
79 static GFSCacheData
*lookup_internal(GFSCache
*cache
, const char *pathname
,
80 FSCacheLookup lookup_type
);
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,
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
106 * 'user_data' will be passed to all of the above functions.
108 GFSCache
*g_fscache_new(GFSLoadFunc load
,
109 GFSUpdateFunc update
,
114 cache
= g_new(GFSCache
, 1);
115 cache
->inode_to_stats
= g_hash_table_new(hash_key
, cmp_stats
);
117 cache
->update
= update
;
118 cache
->user_data
= user_data
;
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
);
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
)
159 data
= lookup_internal(cache
, pathname
,
160 update_details
? FSCACHE_LOOKUP_INIT
161 : FSCACHE_LOOKUP_INSERT
);
169 g_object_unref(data
->data
);
173 /* As g_fscache_lookup, but 'lookup_type' controls what happens if the data
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
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
,
186 g_return_val_if_fail(lookup_type
!= FSCACHE_LOOKUP_INIT
, NULL
);
188 data
= lookup_internal(cache
, pathname
, lookup_type
);
201 g_object_ref(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
)
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
))
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
)
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
))
251 key
.device
= info
.st_dev
;
252 key
.inode
= info
.st_ino
;
254 data
= g_hash_table_lookup(cache
->inode_to_stats
, &key
);
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
269 void g_fscache_purge(GFSCache
*cache
, gint age
)
271 struct PurgeInfo info
;
273 g_return_if_fail(cache
!= NULL
);
277 info
.now
= time(NULL
);
279 g_hash_table_foreach_remove(cache
->inode_to_stats
, purge_hash_entry
,
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
;
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
);
317 static gboolean
purge_hash_entry(gpointer key
, gpointer 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)
327 if (cache_data
->last_lookup
<= info
->now
328 && cache_data
->last_lookup
>= info
->now
- info
->age
)
331 if (cache_data
->data
)
332 g_object_unref(cache_data
->data
);
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
)
350 g_return_val_if_fail(cache
!= NULL
, NULL
);
351 g_return_val_if_fail(pathname
!= NULL
, NULL
);
353 if (mc_stat(pathname
, &info
))
356 key
.device
= info
.st_dev
;
357 key
.inode
= info
.st_ino
;
359 data
= g_hash_table_lookup(cache
->inode_to_stats
, &key
);
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
)
372 /* Is it up-to-date? */
374 if (UPTODATE(data
, info
))
377 if (lookup_type
== FSCACHE_LOOKUP_ONLY_NEW
)
382 cache
->update(data
->data
, pathname
, cache
->user_data
);
386 g_object_unref(data
->data
);
392 GFSCacheKey
*new_key
;
394 if (lookup_type
!= FSCACHE_LOOKUP_CREATE
&&
395 lookup_type
!= FSCACHE_LOOKUP_INIT
)
398 new_key
= g_memdup(&key
, sizeof(key
));
400 data
= g_new(GFSCacheData
, 1);
403 g_hash_table_insert(cache
->inode_to_stats
, new_key
, data
);
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) */
418 data
->data
= cache
->load(pathname
, cache
->user_data
);
421 data
->last_lookup
= time(NULL
);