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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
*
* Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@aful.org> .
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <config.h>
#include "mh-uid.h"
#include "camel-log.h"
#include "camel-stream.h"
#include "camel-stream-fs.h"
#include "camel-stream-buffered-fs.h"
#include "gmime-utils.h"
#include "md5-utils.h"
#include "mh-utils.h"
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
void
mh_uid_get_for_file (gchar *filename, guchar uid[16])
{
CamelStream *message_stream;
GArray *header_array;
Rfc822Header *cur_header;
int i;
MD5Context ctx;
message_stream = camel_stream_buffered_fs_new_with_name (filename,
CAMEL_STREAM_BUFFERED_FS_READ);
header_array = get_header_array_from_stream (message_stream);
gtk_object_unref (GTK_OBJECT (message_stream));
md5_init (&ctx);
for (i=0; i<header_array->len; i++) {
cur_header = (Rfc822Header *)header_array->data + i;
if (!g_strcasecmp (cur_header->name, "subject")) {
md5_update (&ctx, cur_header->value, strlen (cur_header->value));
} else if (!g_strcasecmp (cur_header->name, "sender")) {
md5_update (&ctx, cur_header->value, strlen (cur_header->value));
} else if (!g_strcasecmp (cur_header->name, "date")) {
md5_update (&ctx, cur_header->value, strlen (cur_header->value));
}
g_free (cur_header->name);
g_free (cur_header->value);
}
g_array_free (header_array, TRUE);
md5_final (uid, &ctx);
}
void
mh_save_uid_list (CamelMhFolder *mh_folder)
{
GArray *uid_array;
MhUidCouple *first_uid_couple;
gchar *directory_path = mh_folder->directory_path;
gchar *uidfile_path;
int fd;
int i;
uidfile_path = g_strdup_printf ("%s/%s", directory_path, ".camel-uid-list");
CAMEL_LOG_FULL_DEBUG ("In the process of writing %s\n", uidfile_path);
fd = open (uidfile_path, O_WRONLY | O_CREAT );
if (!fd) {
CAMEL_LOG_FULL_DEBUG ("could not open file %s for writing. Exiting.\n", uidfile_path);
g_free (uidfile_path);
return;
}
g_free (uidfile_path);
uid_array = mh_folder->uid_array;
first_uid_couple = (MhUidCouple *)uid_array->data;
/* write the number of uid contained in the file */
write (fd, &(uid_array->len), sizeof (guint));
CAMEL_LOG_FULL_DEBUG ("%d entrie present in the list\n", uid_array->len);
/* now write the array of uid self */
write (fd, first_uid_couple, sizeof (MhUidCouple) * uid_array->len);
close (fd);
}
gint
mh_load_uid_list (CamelMhFolder *mh_folder)
{
GArray *new_uid_array;
MhUidCouple *first_uid_couple;
gchar *directory_path = mh_folder->directory_path;
gchar *uidfile_path;
int fd;
guint uid_nb;
struct stat stat_buf;
gint stat_error = 0;
uidfile_path = g_strdup_printf ("%s/%s", directory_path, ".camel-uid-list");
/* tests if file exists */
stat_error = stat (uidfile_path, &stat_buf);
if (!((stat_error != -1) && S_ISREG (stat_buf.st_mode))) {
CAMEL_LOG_FULL_DEBUG ("CamelMhFolder::load_uid_list "
"file %s does not exist. Exiting.\n", uidfile_path);
g_free (uidfile_path);
return -1;
}
fd = open (uidfile_path, O_RDONLY);
g_free (uidfile_path);
if (!fd) return -1;
if (mh_folder->uid_array) g_array_free (mh_folder->uid_array, FALSE);
/* read the number of uids in the file */
read (fd, &uid_nb, sizeof (guint));
CAMEL_LOG_FULL_DEBUG ("reading %d uid_entries\n", uid_nb);
new_uid_array = g_array_new (FALSE, FALSE, sizeof (MhUidCouple));
new_uid_array = g_array_set_size (new_uid_array, uid_nb);
first_uid_couple = (MhUidCouple *)new_uid_array->data;
read (fd, first_uid_couple, sizeof (MhUidCouple) * uid_nb);
mh_folder->uid_array = new_uid_array;
return 1;
}
gint
mh_generate_uid_list (CamelMhFolder *mh_folder)
{
GArray *new_uid_array;
const gchar *directory_path;
struct dirent *dir_entry;
DIR *dir_handle;
gchar *msg_path;
guint msg_count;
MhUidCouple *uid_couple;
guint file_number;
g_assert (mh_folder);
CAMEL_LOG_FULL_DEBUG ("in the process of creating uid list \n");
directory_path = mh_folder->directory_path;
if (!directory_path) {
CAMEL_LOG_FULL_DEBUG ("folder has no directory path. Exiting\n");
return -1;
}
msg_count = camel_folder_get_message_count (CAMEL_FOLDER (mh_folder), NULL);
if (!msg_count) {
CAMEL_LOG_FULL_DEBUG ("no message in %s. Exiting\n", directory_path);
return -1;
}
new_uid_array = g_array_new (FALSE, FALSE, sizeof (MhUidCouple));
new_uid_array = g_array_set_size (new_uid_array, msg_count);
uid_couple = (MhUidCouple *)new_uid_array->data;
dir_handle = opendir (directory_path);
/* read first entry in the directory */
dir_entry = readdir (dir_handle);
while (dir_entry != NULL) {
/* tests if the entry correspond to a message file */
if (mh_is_a_message_file (dir_entry->d_name, directory_path)) {
/* get the uid for this message */
msg_path = g_strdup_printf ("%s/%s", directory_path, dir_entry->d_name);
mh_uid_get_for_file (msg_path, uid_couple->uid);
g_free (msg_path);
/* convert filename into file number */
uid_couple->file_number = atoi (dir_entry->d_name);
uid_couple++;
}
/* read next entry */
dir_entry = readdir (dir_handle);
}
closedir (dir_handle);
mh_folder->uid_array = new_uid_array;
}
|