Wow. Such data, so leak, very unfreed
[claws.git] / src / plugins / libravatar / libravatar_missing.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 <stdio.h>
20
21 #include "libravatar_missing.h"
22 #include "libravatar_prefs.h"
23 #include "utils.h"
24
25 /**
26  * Loads the hash table of md5sum → time from the given filename.
27  *
28  * @param filename  Name of the hash table filename.
29  *
30  * @return A hash table with the entries not expired contained in
31  *         the given filename or NULL if failed to load.
32  */
33 GHashTable *missing_load_from_file(const gchar *filename)
34 {
35         FILE *file = fopen(filename, "r");
36         time_t t;
37         long long unsigned seen;
38         gchar md5sum[33];
39         GHashTable *table = NULL;
40         int r = 0, a = 0, d = 0;
41
42         if (file == NULL) {
43                 if (!is_file_exist(filename)) { /* first run, return an empty table */
44                         return g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
45                 }
46                 g_warning("cannot open '%s' for reading", filename);
47                 return NULL;
48         }
49         t = time(NULL);
50         if (t == (time_t)-1) {
51                 g_warning("cannot get time!");
52                 goto close_exit;
53         }
54
55         table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
56
57         while ((r = fscanf(file, "%s %llu\n", md5sum, &seen)) != EOF) {
58                 if (t - (time_t)seen <= LIBRAVATAR_MISSING_TIME) {
59                         time_t *value = g_malloc0(sizeof(time_t));
60                         if (value == NULL) {
61                                 g_warning("cannot allocate memory");
62                                 g_hash_table_destroy(table);
63                                 table = NULL;
64                                 goto close_exit;
65                         }
66                         *value = (time_t)seen;
67                         g_hash_table_insert(table, g_strdup(md5sum), value);
68                 } else
69                         d++;
70                 a++;
71         }
72
73 close_exit:
74         if (fclose(file) != 0)
75                 g_warning("error closing '%s'", filename);
76
77         debug_print("Read %d missing avatar entries, %d obsolete entries discarded\n", a, d);
78         return table;
79 }
80
81 /**
82  * Saves a hash table item.
83  *
84  * @param key    Hash table key, a md5sum string.
85  * @param vakue  Hash table value, a time_t.
86  * @param data   User data, a pointer to the open FILE being written.
87  */
88 static void missing_save_item(gpointer key, gpointer value, gpointer data)
89 {
90         FILE *file = (FILE *)data;
91         gchar *line = g_strdup_printf("%s %llu\n", (gchar *)key, *((long long unsigned *)value));
92         if (fputs(line, file) < 0)
93                 g_warning("error saving missing item");
94         g_free(line);
95 }
96
97 /**
98  * Saves a hash table of md5sum → time to a given file name.
99  *
100  * @param table     The table to save.
101  * @param filename  The name of the file where table data will be saved.
102  *
103  * @return 0 on success, -1 if there was some problem saving.
104  */
105 gint missing_save_to_file(GHashTable *table, const gchar *filename)
106 {
107         FILE *file = fopen(filename, "w");
108
109         if (file == NULL) {
110                 g_warning("cannot open '%s' for writing", filename);
111                 return -1;
112         }
113
114         g_hash_table_foreach(table, missing_save_item, (gpointer)file);
115         debug_print("Saved %u missing avatar entries\n", g_hash_table_size(table));
116
117         if (fclose(file) != 0) {
118                 g_warning("error closing '%s'", filename);
119                 return -1;
120         }
121
122         return 0;
123 }
124
125 /**
126  * Adds a md5sum to a md5sum → time hash table.
127  * If the md5sum is already in the table its time is updated.
128  *
129  * @param table  The table to use.
130  * @param md5    The md5sum to add or update.
131  */
132 void missing_add_md5(GHashTable *table, const gchar *md5)
133 {
134         time_t t = time(NULL);
135
136         if (t == (time_t)-1) {
137                 g_warning("cannot get time!");
138                 return;
139         }
140
141         time_t *seen = g_hash_table_lookup(table, md5);
142         if (seen == NULL) {
143                 seen = g_malloc0(sizeof(time_t));
144                 if (seen == NULL) {
145                         g_warning("cannot allocate memory");
146                         return;
147                 }
148                 *seen = t;
149                 g_hash_table_insert(table, g_strdup(md5), seen);
150                 debug_print("New md5 %s added with time %llu\n", md5, (long long unsigned)t);
151         } else {
152                 *seen = t; /* just update */
153                 debug_print("Updated md5 %s with time %llu\n", md5, (long long unsigned)t);
154         }
155 }
156
157 /**
158  * Check if a md5sum is in hash table and not expired.
159  *
160  * @param table   The table to check against.
161  * @param md5     The md5sum to check.
162  *
163  * @return TRUE if the md5sum is in the table and is not expired,
164            FALSE otherwise.
165  */
166 gboolean is_missing_md5(GHashTable *table, const gchar *md5)
167 {
168         time_t t;
169         time_t *seen = (time_t *)g_hash_table_lookup(table, md5);
170
171         if (seen == NULL)
172                 return FALSE;
173
174         t = time(NULL);
175         if (t != (time_t)-1) {
176                 if (t - *seen <= LIBRAVATAR_MISSING_TIME) {
177                         debug_print("Found missing md5 %s\n", md5);
178                         return TRUE;
179                 }
180         }
181         return FALSE;
182 }
183