2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2014-2015 Ricardo Mones and the Claws Mail Team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "libravatar_cache.h"
24 gchar *libravatar_cache_init(const char *dirs[], gint start, gint end)
26 gchar *subdir, *rootdir;
29 rootdir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
30 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
32 if (!is_dir_exist(rootdir)) {
33 if (make_dir(rootdir) < 0) {
34 g_warning("cannot create root directory '%s'", rootdir);
39 for (i = start; i <= end; ++i) {
40 subdir = g_strconcat(rootdir, dirs[i], NULL);
41 if (!is_dir_exist(subdir)) {
42 if (make_dir(subdir) < 0) {
43 g_warning("cannot create directory '%s'", subdir);
55 static void cache_stat_item(gpointer filename, gpointer data)
58 const gchar *fname = (const gchar *) filename;
59 AvatarCacheStats *stats = (AvatarCacheStats *) data;
61 if (0 == g_stat(fname, &s)) {
62 if (S_ISDIR(s.st_mode) != 0) {
65 else if (S_ISREG(s.st_mode) != 0) {
67 stats->bytes += s.st_size;
74 g_warning("cannot stat %s\n", fname);
79 static void cache_items_deep_first(const gchar *dir, GSList **items, guint *failed)
84 cm_return_if_fail(dir != NULL);
86 if ((dp = opendir(dir)) == NULL) {
87 g_warning("cannot open directory %s\n", dir);
91 while ((d = readdir(dp)) != NULL) {
92 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
96 const gchar *fname = g_strconcat(dir, G_DIR_SEPARATOR_S,
98 if (is_dir_exist(fname))
99 cache_items_deep_first(fname, items, failed);
100 *items = g_slist_append(*items, (gpointer) fname);
106 AvatarCacheStats *libravatar_cache_stats()
109 AvatarCacheStats *stats;
110 GSList *items = NULL;
113 stats = g_new0(AvatarCacheStats, 1);
114 cm_return_val_if_fail(stats != NULL, NULL);
116 rootdir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
117 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
119 cache_items_deep_first(rootdir, &items, &errors);
120 stats->errors += errors;
121 g_slist_foreach(items, (GFunc) cache_stat_item, (gpointer) stats);
122 slist_free_strings_full(items);