Real fix for Coverity 1372362.
[claws.git] / src / plugins / libravatar / libravatar_cache.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2014-2015 Ricardo Mones and the Claws Mail Team
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <sys/stat.h>
20
21 #include "libravatar_cache.h"
22 #include "utils.h"
23
24 gchar *libravatar_cache_init(const char *dirs[], gint start, gint end)
25 {
26         gchar *subdir, *rootdir;
27         int i;
28
29         rootdir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
30                                 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
31                                 NULL);
32         if (!is_dir_exist(rootdir)) {
33                 if (make_dir(rootdir) < 0) {
34                         g_warning("cannot create root directory '%s'", rootdir);
35                         g_free(rootdir);
36                         return NULL;
37                 }
38         }
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);
44                                 g_free(subdir);
45                                 g_free(rootdir);
46                                 return NULL;
47                         }
48                 }
49                 g_free(subdir);
50         }
51
52         return rootdir;
53 }
54
55 static void cache_stat_item(gpointer filename, gpointer data)
56 {
57         GStatBuf                s;
58         const gchar             *fname = (const gchar *) filename;
59         AvatarCacheStats        *stats = (AvatarCacheStats *) data;
60
61         if (0 == g_stat(fname, &s)) {
62                 if (S_ISDIR(s.st_mode) != 0) {
63                         stats->dirs++;
64                 }
65                 else if (S_ISREG(s.st_mode) != 0) {
66                         stats->files++;
67                         stats->bytes += s.st_size;
68                 }
69                 else {
70                         stats->others++;
71                 }
72         }
73         else {
74                 g_warning("cannot stat '%s'", fname);
75                 stats->errors++;
76         }
77 }
78
79 static void cache_items_deep_first(const gchar *dir, GSList **items, guint *failed)
80 {
81         const gchar     *d;
82         GDir            *dp;
83         GError          *error = NULL;
84
85         cm_return_if_fail(dir != NULL);
86
87         if ((dp = g_dir_open(dir, 0, &error)) == NULL) {
88                 g_warning("cannot open directory '%s': %s (%d)",
89                                 dir, error->message, error->code);
90                 g_error_free(error);
91                 (*failed)++;
92                 return;
93         }
94         while ((d = g_dir_read_name(dp)) != NULL) {
95                 if (strcmp(d, ".") == 0 || strcmp(d, "..") == 0) {
96                         continue;
97                 }
98                 else {
99                         const gchar *fname = g_strconcat(dir, G_DIR_SEPARATOR_S, d, NULL);
100                         if (is_dir_exist(fname))
101                                 cache_items_deep_first(fname, items, failed);
102                         *items = g_slist_append(*items, (gpointer) fname);
103                 }
104         }
105         g_dir_close(dp);
106 }
107
108 AvatarCacheStats *libravatar_cache_stats()
109 {
110         gchar *rootdir;
111         AvatarCacheStats *stats;
112         GSList *items = NULL;
113         guint errors = 0;
114
115         stats = g_new0(AvatarCacheStats, 1);
116         cm_return_val_if_fail(stats != NULL, NULL);
117
118         rootdir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
119                                 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
120                                 NULL);
121         cache_items_deep_first(rootdir, &items, &errors);
122         stats->errors += errors;
123         g_slist_foreach(items, (GFunc) cache_stat_item, (gpointer) stats);
124         slist_free_strings_full(items);
125         g_free(rootdir);
126
127         return stats;
128 }
129
130 static void cache_delete_item(gpointer filename, gpointer errors)
131 {
132         const gchar *fname = (const gchar *) filename;
133         AvatarCleanupResult *acr = (AvatarCleanupResult *) errors;
134
135         if (!is_dir_exist(fname)) {
136                 if (claws_unlink(fname) < 0) {
137                         g_warning("couldn't delete file '%s'", fname);
138                         (acr->e_unlink)++;
139                 }
140                 else {
141                         (acr->removed)++;
142                 }
143         }
144 }
145
146 AvatarCleanupResult *libravatar_cache_clean()
147 {
148         gchar *rootdir;
149         AvatarCleanupResult *acr;
150         GSList *items = NULL;
151         guint errors = 0;
152
153         acr = g_new0(AvatarCleanupResult, 1);
154         cm_return_val_if_fail(acr != NULL, NULL);
155
156         rootdir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
157                                 LIBRAVATAR_CACHE_DIR, G_DIR_SEPARATOR_S,
158                                 NULL);
159         cache_items_deep_first(rootdir, &items, &errors);
160         acr->e_stat = (gint) errors;
161
162         g_slist_foreach(items, (GFunc) cache_delete_item, (gpointer) acr);
163
164         slist_free_strings_full(items);
165         g_free(rootdir);
166
167         return acr;
168 }