summaryrefslogtreecommitdiff
path: root/camel/camel-data-cache.c
blob: f11a6b12be627e32fecc2f2b2bf4540a07f64572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-message-cache.c: Class for a Camel cache.
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Zucchi <notzed@ximian.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>

#include "camel-data-cache.h"
#include "camel-object.h"
#include "camel-object-bag.h"
#include "camel-stream-mem.h"
#include "camel-file-utils.h"

#define d(x)

#define CAMEL_DATA_CACHE_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), CAMEL_TYPE_DATA_CACHE, CamelDataCachePrivate))

/* how many 'bits' of hash are used to key the toplevel directory */
#define CAMEL_DATA_CACHE_BITS (6)
#define CAMEL_DATA_CACHE_MASK ((1 << CAMEL_DATA_CACHE_BITS)-1)

/* timeout before a cache dir is checked again for expired entries,
 * once an hour should be enough */
#define CAMEL_DATA_CACHE_CYCLE_TIME (60*60)

struct _CamelDataCachePrivate {
	CamelObjectBag *busy_bag;

	gchar *path;

	time_t expire_age;
	time_t expire_access;

	time_t expire_last[1 << CAMEL_DATA_CACHE_BITS];
};

enum {
	PROP_0,
	PROP_PATH
};

G_DEFINE_TYPE (CamelDataCache, camel_data_cache, G_TYPE_OBJECT)

static void
data_cache_set_property (GObject *object,
                         guint property_id,
                         const GValue *value,
                         GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_PATH:
			camel_data_cache_set_path (
				CAMEL_DATA_CACHE (object),
				g_value_get_string (value));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
data_cache_get_property (GObject *object,
                         guint property_id,
                         GValue *value,
                         GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_PATH:
			g_value_set_string (
				value, camel_data_cache_get_path (
				CAMEL_DATA_CACHE (object)));
			return;
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
data_cache_finalize (GObject *object)
{
	CamelDataCachePrivate *priv;

	priv = CAMEL_DATA_CACHE_GET_PRIVATE (object);

	camel_object_bag_destroy (priv->busy_bag);
	g_free (priv->path);

	/* Chain up to parent's finalize() method. */
	G_OBJECT_CLASS (camel_data_cache_parent_class)->finalize (object);
}

static void
camel_data_cache_class_init (CamelDataCacheClass *class)
{
	GObjectClass *object_class;

	g_type_class_add_private (class, sizeof (CamelDataCachePrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->set_property = data_cache_set_property;
	object_class->get_property = data_cache_get_property;
	object_class->finalize = data_cache_finalize;

	g_object_class_install_property (
		object_class,
		PROP_PATH,
		g_param_spec_string (
			"path",
			"Path",
			NULL,
			NULL,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT));
}

static void
camel_data_cache_init (CamelDataCache *data_cache)
{
	CamelObjectBag *busy_bag;

	busy_bag = camel_object_bag_new (
		g_str_hash, g_str_equal,
		(CamelCopyFunc) g_strdup,
		(GFreeFunc) g_free);

	data_cache->priv = CAMEL_DATA_CACHE_GET_PRIVATE (data_cache);
	data_cache->priv->busy_bag = busy_bag;
	data_cache->priv->expire_age = -1;
	data_cache->priv->expire_access = -1;
}

/**
 * camel_data_cache_new:
 * @path: Base path of cache, subdirectories will be created here.
 * @error: return location for a #GError, or %NULL
 *
 * Create a new data cache.
 *
 * Returns: A new cache object, or NULL if the base path cannot
 * be written to.
 **/
CamelDataCache *
camel_data_cache_new (const gchar *path,
                      GError **error)
{
	g_return_val_if_fail (path != NULL, NULL);

	if (g_mkdir_with_parents (path, 0700) == -1) {
		g_set_error (
			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			_("Unable to create cache path"));
		return NULL;
	}

	return g_object_new (CAMEL_TYPE_DATA_CACHE, "path", path, NULL);
}

/**
 * camel_data_cache_get_path:
 * @cdc: a #CamelDataCache
 *
 * Returns the path to the data cache.
 *
 * Returns: the path to the data cache
 *
 * Since: 2.32
 **/
const gchar *
camel_data_cache_get_path (CamelDataCache *cdc)
{
	g_return_val_if_fail (CAMEL_IS_DATA_CACHE (cdc), NULL);

	return cdc->priv->path;
}

/**
 * camel_data_cache_set_path:
 * @cdc: a #CamelDataCache
 * @path: path to the data cache
 *
 * Sets the path to the data cache.
 *
 * Since: 2.32
 **/
void
camel_data_cache_set_path (CamelDataCache *cdc,
                           const gchar *path)
{
	g_return_if_fail (CAMEL_IS_DATA_CACHE (cdc));
	g_return_if_fail (path != NULL);

	if (g_strcmp0 (cdc->priv->path, path) == 0)
		return;

	g_free (cdc->priv->path);
	cdc->priv->path = g_strdup (path);

	g_object_notify (G_OBJECT (cdc), "path");
}

/**
 * camel_data_cache_set_expire_age:
 * @cdc: A #CamelDataCache
 * @when: Timeout for age expiry, or -1 to disable.
 *
 * Set the cache expiration policy for aged entries.
 *
 * Items in the cache older than @when seconds may be
 * flushed at any time.  Items are expired in a lazy
 * manner, so it is indeterminate when the items will
 * physically be removed.
 *
 * Note you can set both an age and an access limit.  The
 * age acts as a hard limit on cache entries.
 **/
void
camel_data_cache_set_expire_age (CamelDataCache *cdc,
                                 time_t when)
{
	cdc->priv->expire_age = when;
}

/**
 * camel_data_cache_set_expire_access:
 * @cdc: A #CamelDataCache
 * @when: Timeout for access, or -1 to disable access expiry.
 *
 * Set the cache expiration policy for access times.
 *
 * Items in the cache which haven't been accessed for @when
 * seconds may be expired at any time.  Items are expired in a lazy
 * manner, so it is indeterminate when the items will
 * physically be removed.
 *
 * Note you can set both an age and an access limit.  The
 * age acts as a hard limit on cache entries.
 **/
void
camel_data_cache_set_expire_access (CamelDataCache *cdc,
                                    time_t when)
{
	cdc->priv->expire_access = when;
}

static void
data_cache_expire (CamelDataCache *cdc,
                   const gchar *path,
                   const gchar *keep,
                   time_t now,
                   gboolean expire_all)
{
	GDir *dir;
	const gchar *dname;
	struct stat st;
	GIOStream *stream;

	dir = g_dir_open (path, 0, NULL);
	if (dir == NULL)
		return;

	while ((dname = g_dir_read_name (dir))) {
		gchar *dpath;

		if (keep && strcmp (dname, keep) == 0)
			continue;

		dpath = g_build_filename (path, dname, NULL);

		if (g_stat (dpath, &st) == 0
		    && S_ISREG (st.st_mode)
		    && (expire_all
			|| (cdc->priv->expire_age != -1 && st.st_mtime + cdc->priv->expire_age < now)
			|| (cdc->priv->expire_access != -1 && st.st_atime + cdc->priv->expire_access < now))) {
			g_unlink (dpath);
			stream = camel_object_bag_get (cdc->priv->busy_bag, dpath);
			if (stream) {
				camel_object_bag_remove (cdc->priv->busy_bag, stream);
				g_object_unref (stream);
			}
		}

		g_free (dpath);
	}
	g_dir_close (dir);
}

/* Since we have to stat the directory anyway, we use this opportunity to
 * lazily expire old data.
 * If it is this directories 'turn', and we haven't done it for CYCLE_TIME seconds,
 * then we perform an expiry run */
static gchar *
data_cache_path (CamelDataCache *cdc,
                 gint create,
                 const gchar *path,
                 const gchar *key)
{
	gchar *dir, *real, *tmp;
	gsize dir_len;
	guint32 hash;

	hash = g_str_hash (key);
	hash = (hash >> 5) &CAMEL_DATA_CACHE_MASK;
	dir_len = strlen (cdc->priv->path) + strlen (path) + 8;
	dir = alloca (dir_len);
	g_snprintf (dir, dir_len, "%s/%s/%02x", cdc->priv->path, path, hash);

	if (g_access (dir, F_OK) == -1) {
		if (create)
			g_mkdir_with_parents (dir, 0700);
	} else if (cdc->priv->expire_age != -1 || cdc->priv->expire_access != -1) {
		time_t now;

		/* This has a race, but at worst we re-run an expire cycle which is safe */
		now = time (NULL);
		if (cdc->priv->expire_last[hash] + CAMEL_DATA_CACHE_CYCLE_TIME < now) {
			cdc->priv->expire_last[hash] = now;
			data_cache_expire (cdc, dir, key, now, FALSE);
		}
	}

	tmp = camel_file_util_safe_filename (key);
	real = g_strdup_printf ("%s/%s", dir, tmp);
	g_free (tmp);

	return real;
}

/**
 * camel_data_cache_add:
 * @cdc: A #CamelDataCache
 * @path: Relative path of item to add.
 * @key: Key of item to add.
 * @error: return location for a #GError, or %NULL
 *
 * Add a new item to the cache, returning a #GIOStream to the new item.
 *
 * The key and the path combine to form a unique key used to store the item.
 *
 * Potentially, expiry processing will be performed while this call is
 * executing.
 *
 * The returned #GIOStream is referenced for thread-safety and must be
 * unreferenced with g_object_unref() when finished with it.
 *
 * Returns: a #GIOStream for the new cache item, or %NULL
 **/
GIOStream *
camel_data_cache_add (CamelDataCache *cdc,
                      const gchar *path,
                      const gchar *key,
                      GError **error)
{
	gchar *real;
	GFileIOStream *stream;
	GFile *file;

	real = data_cache_path (cdc, TRUE, path, key);
	/* need to loop 'cause otherwise we can call bag_add/bag_abort
	 * after bag_reserve returned a pointer, which is an invalid
	 * sequence. */
	do {
		stream = camel_object_bag_reserve (cdc->priv->busy_bag, real);
		if (stream) {
			g_unlink (real);
			camel_object_bag_remove (cdc->priv->busy_bag, stream);
			g_object_unref (stream);
		}
	} while (stream != NULL);

	file = g_file_new_for_path (real);
	stream = g_file_replace_readwrite (
		file, NULL, FALSE, G_FILE_CREATE_PRIVATE, NULL, error);
	g_object_unref (file);

	if (stream != NULL)
		camel_object_bag_add (cdc->priv->busy_bag, real, stream);
	else
		camel_object_bag_abort (cdc->priv->busy_bag, real);

	g_free (real);

	return G_IO_STREAM (stream);
}

/**
 * camel_data_cache_get:
 * @cdc: A #CamelDataCache
 * @path: Path to the (sub) cache the item exists in.
 * @key: Key for the cache item.
 * @error: return location for a #GError, or %NULL
 *
 * Lookup an item in the cache.  If the item exists, a #GIOStream is returned
 * for the item.  The stream may be shared by multiple callers, so ensure the
 * stream is in a valid state through external locking.
 *
 * The returned #GIOStream is referenced for thread-safety and must be
 * unreferenced with g_object_unref() when finished with it.
 *
 * Returns: a #GIOStream for the requested cache item, or %NULL
 **/
GIOStream *
camel_data_cache_get (CamelDataCache *cdc,
                      const gchar *path,
                      const gchar *key,
                      GError **error)
{
	GFileIOStream *stream;
	GFile *file;
	struct stat st;
	gchar *real;

	real = data_cache_path (cdc, FALSE, path, key);
	stream = camel_object_bag_reserve (cdc->priv->busy_bag, real);
	if (stream != NULL)
		goto exit;

	/* An empty cache file is useless.  Return an error. */
	if (g_stat (real, &st) == 0 && st.st_size == 0) {
		g_set_error (
			error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
			"%s: %s", _("Empty cache file"), real);
		camel_object_bag_abort (cdc->priv->busy_bag, real);
		goto exit;
	}

	file = g_file_new_for_path (real);
	stream = g_file_open_readwrite (file, NULL, error);
	g_object_unref (file);

	if (stream != NULL)
		camel_object_bag_add (cdc->priv->busy_bag, real, stream);
	else
		camel_object_bag_abort (cdc->priv->busy_bag, real);

exit:
	g_free (real);

	return G_IO_STREAM (stream);
}

/**
 * camel_data_cache_get_filename:
 * @cdc: A #CamelDataCache
 * @path: Path to the (sub) cache the item exists in.
 * @key: Key for the cache item.
 *
 * Lookup the filename for an item in the cache
 *
 * Returns: The filename for a cache item
 *
 * Since: 2.26
 **/
gchar *
camel_data_cache_get_filename (CamelDataCache *cdc,
                               const gchar *path,
                               const gchar *key)
{
	return data_cache_path (cdc, FALSE, path, key);
}

/**
 * camel_data_cache_remove:
 * @cdc: A #CamelDataCache
 * @path:
 * @key:
 * @error: return location for a #GError, or %NULL
 *
 * Remove/expire a cache item.
 *
 * Returns:
 **/
gint
camel_data_cache_remove (CamelDataCache *cdc,
                         const gchar *path,
                         const gchar *key,
                         GError **error)
{
	GIOStream *stream;
	gchar *real;
	gint ret;

	real = data_cache_path (cdc, FALSE, path, key);
	stream = camel_object_bag_get (cdc->priv->busy_bag, real);
	if (stream) {
		camel_object_bag_remove (cdc->priv->busy_bag, stream);
		g_object_unref (stream);
	}

	/* maybe we were a mem stream */
	if (g_unlink (real) == -1 && errno != ENOENT) {
		g_set_error (
			error, G_IO_ERROR,
			g_io_error_from_errno (errno),
			_("Could not remove cache entry: %s: %s"),
			real, g_strerror (errno));
		ret = -1;
	} else {
		ret = 0;
	}

	g_free (real);

	return ret;
}

/**
 * camel_data_cache_clear:
 * @cdc: a #CamelDataCache
 * @path: Path to the (sub) cache the item exists in.
 *
 * Clear cache's content in @path.
 *
 * Since: 3.2
 **/
void
camel_data_cache_clear (CamelDataCache *cdc,
                        const gchar *path)
{
	gchar *base_dir;
	GDir *dir;
	const gchar *dname;
	struct stat st;

	g_return_if_fail (cdc != NULL);
	g_return_if_fail (path != NULL);

	base_dir = g_build_filename (cdc->priv->path, path, NULL);

	dir = g_dir_open (base_dir, 0, NULL);
	if (dir == NULL) {
		g_free (base_dir);
		return;
	}

	while ((dname = g_dir_read_name (dir))) {
		gchar *dpath;

		dpath = g_build_filename (base_dir, dname, NULL);

		if (g_stat (dpath, &st) == 0
		    && S_ISDIR (st.st_mode)
		    && !g_str_equal (dname, ".")
		    && !g_str_equal (dname, "..")) {
			data_cache_expire (cdc, dpath, NULL, -1, TRUE);
		}

		g_free (dpath);
	}

	g_dir_close (dir);
	g_free (base_dir);
}