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 (C) 1999-2008 Novell, Inc. (www.novell.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
35 #include <glib/gstdio.h>
37 #include "camel-file-utils.h"
38 #include "camel-uid-cache.h"
39 #include "camel-win32.h"
47 * camel_uid_cache_new:
48 * @filename: path to load the cache from
50 * Creates a new UID cache, initialized from @filename. If @filename
51 * doesn't already exist, the UID cache will be empty. Otherwise, if
52 * it does exist but can't be read, the function will return %NULL.
54 * Returns: a new UID cache, or %NULL
57 camel_uid_cache_new (const gchar *filename)
61 gchar *dirname, *buf, **uids;
64 dirname = g_path_get_dirname (filename);
65 if (g_mkdir_with_parents (dirname, 0700) == -1) {
72 if ((fd = g_open (filename, O_RDONLY | O_CREAT | O_BINARY, 0666)) == -1)
75 if (fstat (fd, &st) == -1) {
80 buf = g_malloc (st.st_size + 1);
82 if (st.st_size > 0 && camel_read (fd, buf, st.st_size, NULL, NULL) == -1) {
88 buf[st.st_size] = '\0';
92 cache = g_new (CamelUIDCache, 1);
93 cache->uids = g_hash_table_new (g_str_hash, g_str_equal);
94 cache->filename = g_strdup (filename);
100 uids = g_strsplit (buf, "\n", 0);
102 for (i = 0; uids[i]; i++) {
103 struct _uid_state *state;
105 state = g_new (struct _uid_state, 1);
106 state->level = cache->level;
109 g_hash_table_insert (cache->uids, uids[i], state);
118 maybe_write_uid (gpointer key,
122 CamelUIDCache *cache = data;
123 struct _uid_state *state = value;
128 if (state && state->level == cache->level && state->save) {
129 if (camel_write (cache->fd, key, strlen (key), NULL, NULL) == -1 ||
130 camel_write (cache->fd, "\n", 1, NULL, NULL) == -1) {
133 cache->size += strlen (key) + 1;
136 /* keep track of how much space the expired uids would
137 * have taken up in the cache */
138 cache->expired += strlen (key) + 1;
143 * camel_uid_cache_save:
144 * @cache: a CamelUIDCache
146 * Attempts to save @cache back to disk.
148 * Returns: success or failure
151 camel_uid_cache_save (CamelUIDCache *cache)
157 filename = g_strdup_printf ("%s~", cache->filename);
158 if ((fd = g_open (filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)) == -1) {
166 g_hash_table_foreach (cache->uids, maybe_write_uid, cache);
168 if (cache->fd == -1 || fsync (fd) == -1)
175 if (g_rename (filename, cache->filename) == -1)
186 #ifdef ENABLE_SPASMOLYTIC
189 * If our new cache size is larger than the old cache,
190 * even if we haven't finished writing it out
191 * successfully, we should still attempt to replace
192 * the old cache with the new cache because it will at
193 * least avoid re-downloading a few extra messages
194 * than if we just kept the old cache.
196 * Similarly, even if the new cache size is smaller
197 * than the old cache size, but we've expired enough
198 * uids to make up for the difference in size (or
199 * more), then we should replace the old cache with
200 * the new cache as well.
203 if (g_stat (cache->filename, &st) == 0 &&
204 (cache->size > st.st_size || cache->size + cache->expired > st.st_size)) {
205 if (ftruncate (fd, (off_t) cache->size) != -1) {
207 g_rename (filename, cache->filename);
233 free_uid (gpointer key,
242 * camel_uid_cache_destroy:
243 * @cache: a CamelUIDCache
245 * Destroys @cache and frees its data.
248 camel_uid_cache_destroy (CamelUIDCache *cache)
250 g_hash_table_foreach (cache->uids, free_uid, NULL);
251 g_hash_table_destroy (cache->uids);
252 g_free (cache->filename);
257 * camel_uid_cache_get_new_uids:
258 * @cache: a CamelUIDCache
259 * @uids: an array of UIDs
261 * Returns an array of UIDs from @uids that are not in @cache, and
262 * removes UIDs from @cache that aren't in @uids.
264 * Returns: an array of new UIDs, which must be freed with
265 * camel_uid_cache_free_uids().
268 camel_uid_cache_get_new_uids (CamelUIDCache *cache,
276 new_uids = g_ptr_array_new ();
279 for (i = 0; i < uids->len; i++) {
280 struct _uid_state *state;
282 uid = uids->pdata[i];
283 if (g_hash_table_lookup_extended (cache->uids, uid, (gpointer *) &old_uid, (gpointer *) &state)) {
284 g_hash_table_remove (cache->uids, uid);
287 g_ptr_array_add (new_uids, g_strdup (uid));
288 state = g_new (struct _uid_state, 1);
292 state->level = cache->level;
293 g_hash_table_insert (cache->uids, g_strdup (uid), state);
300 * camel_uid_cache_save_uid:
301 * @cache: a CamelUIDCache
302 * @uid: a uid to save
304 * Marks a uid for saving.
307 camel_uid_cache_save_uid (CamelUIDCache *cache,
310 struct _uid_state *state;
313 g_return_if_fail (uid != NULL);
315 if (g_hash_table_lookup_extended (cache->uids, uid, (gpointer *) &old_uid, (gpointer *) &state)) {
317 state->level = cache->level;
319 state = g_new (struct _uid_state, 1);
321 state->level = cache->level;
323 g_hash_table_insert (cache->uids, g_strdup (uid), state);
328 * camel_uid_cache_free_uids:
329 * @uids: an array returned from camel_uid_cache_get_new_uids()
331 * Frees the array of UIDs.
334 camel_uid_cache_free_uids (GPtrArray *uids)
338 for (i = 0; i < uids->len; i++)
339 g_free (uids->pdata[i]);
340 g_ptr_array_free (uids, TRUE);