Extending test-client-custom-summary to try e_book_client_get_contacts_uids()
[platform/upstream/evolution-data-server.git] / camel / camel-uid-cache.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-uid-cache.c: UID caching code. */
3
4 /*
5  * Authors:
6  *  Dan Winship <danw@ximian.com>
7  *
8  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
9  *
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.
13  *
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.
18  *
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
22  * USA
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34
35 #include <glib/gstdio.h>
36
37 #include "camel-file-utils.h"
38 #include "camel-uid-cache.h"
39 #include "camel-win32.h"
40
41 struct _uid_state {
42         gint level;
43         gboolean save;
44 };
45
46 /**
47  * camel_uid_cache_new:
48  * @filename: path to load the cache from
49  *
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.
53  *
54  * Returns: a new UID cache, or %NULL
55  **/
56 CamelUIDCache *
57 camel_uid_cache_new (const gchar *filename)
58 {
59         CamelUIDCache *cache;
60         struct stat st;
61         gchar *dirname, *buf, **uids;
62         gint fd, i;
63
64         dirname = g_path_get_dirname (filename);
65         if (g_mkdir_with_parents (dirname, 0700) == -1) {
66                 g_free (dirname);
67                 return NULL;
68         }
69
70         g_free (dirname);
71
72         if ((fd = g_open (filename, O_RDONLY | O_CREAT | O_BINARY, 0666)) == -1)
73                 return NULL;
74
75         if (fstat (fd, &st) == -1) {
76                 close (fd);
77                 return NULL;
78         }
79
80         buf = g_malloc (st.st_size + 1);
81
82         if (st.st_size > 0 && camel_read (fd, buf, st.st_size, NULL, NULL) == -1) {
83                 close (fd);
84                 g_free (buf);
85                 return NULL;
86         }
87
88         buf[st.st_size] = '\0';
89
90         close (fd);
91
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);
95         cache->level = 1;
96         cache->expired = 0;
97         cache->size = 0;
98         cache->fd = -1;
99
100         uids = g_strsplit (buf, "\n", 0);
101         g_free (buf);
102         for (i = 0; uids[i]; i++) {
103                 struct _uid_state *state;
104
105                 state = g_new (struct _uid_state, 1);
106                 state->level = cache->level;
107                 state->save = TRUE;
108
109                 g_hash_table_insert (cache->uids, uids[i], state);
110         }
111
112         g_free (uids);
113
114         return cache;
115 }
116
117 static void
118 maybe_write_uid (gpointer key,
119                  gpointer value,
120                  gpointer data)
121 {
122         CamelUIDCache *cache = data;
123         struct _uid_state *state = value;
124
125         if (cache->fd == -1)
126                 return;
127
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) {
131                         cache->fd = -1;
132                 } else {
133                         cache->size += strlen (key) + 1;
134                 }
135         } else {
136                 /* keep track of how much space the expired uids would
137                  * have taken up in the cache */
138                 cache->expired += strlen (key) + 1;
139         }
140 }
141
142 /**
143  * camel_uid_cache_save:
144  * @cache: a CamelUIDCache
145  *
146  * Attempts to save @cache back to disk.
147  *
148  * Returns: success or failure
149  **/
150 gboolean
151 camel_uid_cache_save (CamelUIDCache *cache)
152 {
153         gchar *filename;
154         gint errnosav;
155         gint fd;
156
157         filename = g_strdup_printf ("%s~", cache->filename);
158         if ((fd = g_open (filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)) == -1) {
159                 g_free (filename);
160                 return FALSE;
161         }
162
163         cache->fd = fd;
164         cache->size = 0;
165         cache->expired = 0;
166         g_hash_table_foreach (cache->uids, maybe_write_uid, cache);
167
168         if (cache->fd == -1 || fsync (fd) == -1)
169                 goto exception;
170
171         close (fd);
172         fd = -1;
173         cache->fd = -1;
174
175         if (g_rename (filename, cache->filename) == -1)
176                 goto exception;
177
178         g_free (filename);
179
180         return TRUE;
181
182  exception:
183
184         errnosav = errno;
185
186 #ifdef ENABLE_SPASMOLYTIC
187         if (fd != -1) {
188                 /*
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.
195                  *
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.
201                  */
202
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) {
206                                 close (fd);
207                                 g_rename (filename, cache->filename);
208                                 g_free (filename);
209                                 cache->expired = 0;
210                                 cache->size = 0;
211                                 cache->fd = -1;
212
213                                 return TRUE;
214                         }
215                 }
216         }
217 #endif
218
219         if (fd != -1) {
220                 close (fd);
221                 cache->fd = -1;
222         }
223
224         g_unlink (filename);
225         g_free (filename);
226
227         errno = errnosav;
228
229         return FALSE;
230 }
231
232 static void
233 free_uid (gpointer key,
234           gpointer value,
235           gpointer data)
236 {
237         g_free (key);
238         g_free (value);
239 }
240
241 /**
242  * camel_uid_cache_destroy:
243  * @cache: a CamelUIDCache
244  *
245  * Destroys @cache and frees its data.
246  **/
247 void
248 camel_uid_cache_destroy (CamelUIDCache *cache)
249 {
250         g_hash_table_foreach (cache->uids, free_uid, NULL);
251         g_hash_table_destroy (cache->uids);
252         g_free (cache->filename);
253         g_free (cache);
254 }
255
256 /**
257  * camel_uid_cache_get_new_uids:
258  * @cache: a CamelUIDCache
259  * @uids: an array of UIDs
260  *
261  * Returns an array of UIDs from @uids that are not in @cache, and
262  * removes UIDs from @cache that aren't in @uids.
263  *
264  * Returns: an array of new UIDs, which must be freed with
265  * camel_uid_cache_free_uids().
266  **/
267 GPtrArray *
268 camel_uid_cache_get_new_uids (CamelUIDCache *cache,
269                               GPtrArray *uids)
270 {
271         GPtrArray *new_uids;
272         gpointer old_uid;
273         gchar *uid;
274         gint i;
275
276         new_uids = g_ptr_array_new ();
277         cache->level++;
278
279         for (i = 0; i < uids->len; i++) {
280                 struct _uid_state *state;
281
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);
285                         g_free (old_uid);
286                 } else {
287                         g_ptr_array_add (new_uids, g_strdup (uid));
288                         state = g_new (struct _uid_state, 1);
289                         state->save = FALSE;
290                 }
291
292                 state->level = cache->level;
293                 g_hash_table_insert (cache->uids, g_strdup (uid), state);
294         }
295
296         return new_uids;
297 }
298
299 /**
300  * camel_uid_cache_save_uid:
301  * @cache: a CamelUIDCache
302  * @uid: a uid to save
303  *
304  * Marks a uid for saving.
305  **/
306 void
307 camel_uid_cache_save_uid (CamelUIDCache *cache,
308                           const gchar *uid)
309 {
310         struct _uid_state *state;
311         gpointer old_uid;
312
313         g_return_if_fail (uid != NULL);
314
315         if (g_hash_table_lookup_extended (cache->uids, uid, (gpointer *) &old_uid, (gpointer *) &state)) {
316                 state->save = TRUE;
317                 state->level = cache->level;
318         } else {
319                 state = g_new (struct _uid_state, 1);
320                 state->save = TRUE;
321                 state->level = cache->level;
322
323                 g_hash_table_insert (cache->uids, g_strdup (uid), state);
324         }
325 }
326
327 /**
328  * camel_uid_cache_free_uids:
329  * @uids: an array returned from camel_uid_cache_get_new_uids()
330  *
331  * Frees the array of UIDs.
332  **/
333 void
334 camel_uid_cache_free_uids (GPtrArray *uids)
335 {
336         gint i;
337
338         for (i = 0; i < uids->len; i++)
339                 g_free (uids->pdata[i]);
340         g_ptr_array_free (uids, TRUE);
341 }