1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-uid-cache.c: UID caching code. */
6 * Dan Winship <danw@ximian.com>
8 * Copyright 2000 Ximian, Inc. (www.ximian.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU Lesser General Public
12 * License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
36 #include <glib/gstdio.h>
38 #include <libedataserver/e-data-server-util.h>
40 #include "camel-file-utils.h"
41 #include "camel-private.h"
42 #include "camel-uid-cache.h"
51 * camel_uid_cache_new:
52 * @filename: path to load the cache from
54 * Creates a new UID cache, initialized from @filename. If @filename
55 * doesn't already exist, the UID cache will be empty. Otherwise, if
56 * it does exist but can't be read, the function will return %NULL.
58 * Return value: a new UID cache, or %NULL
61 camel_uid_cache_new (const char *filename)
65 char *dirname, *buf, **uids;
68 dirname = g_path_get_dirname (filename);
69 if (g_mkdir_with_parents (dirname, 0777) == -1) {
76 if ((fd = g_open (filename, O_RDONLY | O_CREAT | O_BINARY, 0666)) == -1)
79 if (fstat (fd, &st) == -1) {
84 buf = g_malloc (st.st_size + 1);
86 if (st.st_size > 0 && camel_read (fd, buf, st.st_size) == -1) {
92 buf[st.st_size] = '\0';
96 cache = g_new (CamelUIDCache, 1);
97 cache->uids = g_hash_table_new (g_str_hash, g_str_equal);
98 cache->filename = g_strdup (filename);
104 uids = g_strsplit (buf, "\n", 0);
106 for (i = 0; uids[i]; i++) {
107 struct _uid_state *state;
109 state = g_new (struct _uid_state, 1);
110 state->level = cache->level;
113 g_hash_table_insert (cache->uids, uids[i], state);
123 maybe_write_uid (gpointer key, gpointer value, gpointer data)
125 CamelUIDCache *cache = data;
126 struct _uid_state *state = value;
131 if (state && state->level == cache->level && state->save) {
132 if (camel_write (cache->fd, key, strlen (key)) == -1 ||
133 camel_write (cache->fd, "\n", 1) == -1) {
136 cache->size += strlen (key) + 1;
139 /* keep track of how much space the expired uids would
140 * have taken up in the cache */
141 cache->expired += strlen (key) + 1;
147 * camel_uid_cache_save:
148 * @cache: a CamelUIDCache
150 * Attempts to save @cache back to disk.
152 * Return value: success or failure
155 camel_uid_cache_save (CamelUIDCache *cache)
161 filename = g_strdup_printf ("%s~", cache->filename);
162 if ((fd = g_open (filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)) == -1) {
170 g_hash_table_foreach (cache->uids, maybe_write_uid, cache);
172 if (fsync (fd) == -1)
179 if (g_rename (filename, cache->filename) == -1)
190 #ifdef ENABLE_SPASMOLYTIC
193 * If our new cache size is larger than the old cache,
194 * even if we haven't finished writing it out
195 * successfully, we should still attempt to replace
196 * the old cache with the new cache because it will at
197 * least avoid re-downloading a few extra messages
198 * than if we just kept the old cache.
200 * Similarly, even if the new cache size is smaller
201 * than the old cache size, but we've expired enough
202 * uids to make up for the difference in size (or
203 * more), then we should replace the old cache with
204 * the new cache as well.
207 if (g_stat (cache->filename, &st) == 0 &&
208 (cache->size > st.st_size || cache->size + cache->expired > st.st_size)) {
209 if (ftruncate (fd, (off_t) cache->size) != -1) {
212 goto overwrite; /* FIXME: no such label */
232 free_uid (gpointer key, gpointer value, gpointer data)
240 * camel_uid_cache_destroy:
241 * @cache: a CamelUIDCache
243 * Destroys @cache and frees its data.
246 camel_uid_cache_destroy (CamelUIDCache *cache)
248 g_hash_table_foreach (cache->uids, free_uid, NULL);
249 g_hash_table_destroy (cache->uids);
250 g_free (cache->filename);
256 * camel_uid_cache_get_new_uids:
257 * @cache: a CamelUIDCache
258 * @uids: an array of UIDs
260 * Returns an array of UIDs from @uids that are not in @cache, and
261 * removes UIDs from @cache that aren't in @uids.
263 * Return value: an array of new UIDs, which must be freed with
264 * camel_uid_cache_free_uids().
267 camel_uid_cache_get_new_uids (CamelUIDCache *cache, GPtrArray *uids)
274 new_uids = g_ptr_array_new ();
277 for (i = 0; i < uids->len; i++) {
278 struct _uid_state *state;
280 uid = uids->pdata[i];
281 if (g_hash_table_lookup_extended (cache->uids, uid, (void **)&old_uid, (void **)&state)) {
282 g_hash_table_remove (cache->uids, uid);
285 g_ptr_array_add (new_uids, g_strdup (uid));
286 state = g_new (struct _uid_state, 1);
290 state->level = cache->level;
291 g_hash_table_insert (cache->uids, g_strdup (uid), state);
299 * camel_uid_cache_save_uid:
300 * @cache: a CamelUIDCache
301 * @uid: a uid to save
303 * Marks a uid for saving.
306 camel_uid_cache_save_uid (CamelUIDCache *cache, const char *uid)
308 struct _uid_state *state;
311 g_return_if_fail (uid != NULL);
313 if (g_hash_table_lookup_extended (cache->uids, uid, (void **)&old_uid, (void **)&state)) {
315 state->level = cache->level;
317 state = g_new (struct _uid_state, 1);
319 state->level = cache->level;
321 g_hash_table_insert (cache->uids, g_strdup (uid), state);
327 * camel_uid_cache_free_uids:
328 * @uids: an array returned from camel_uid_cache_get_new_uids()
330 * Frees the array of UIDs.
333 camel_uid_cache_free_uids (GPtrArray *uids)
337 for (i = 0; i < uids->len; i++)
338 g_free (uids->pdata[i]);
339 g_ptr_array_free (uids, TRUE);