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