08c03dc5b79d7d523386ca94c559deb62d604ac6
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto 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
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <time.h>
34
35 #include "folder.h"
36 #include "mh.h"
37 #include "procmsg.h"
38 #include "procheader.h"
39 #include "utils.h"
40 #include "codeconv.h"
41 #include "statusbar.h"
42 #include "gtkutils.h"
43 #include "timing.h"
44
45 /* Define possible missing constants for Windows. */
46 #ifdef G_OS_WIN32
47 # ifndef S_IRGRP
48 # define S_IRGRP 0
49 # define S_IWGRP 0
50 # endif
51 # ifndef S_IROTH
52 # define S_IROTH 0
53 # define S_IWOTH 0
54 # endif
55 #endif
56
57
58 static void     mh_folder_init          (Folder         *folder,
59                                          const gchar    *name,
60                                          const gchar    *path);
61
62 static Folder   *mh_folder_new          (const gchar    *name,
63                                          const gchar    *path);
64 static void     mh_folder_destroy       (Folder         *folder);
65 static gchar   *mh_fetch_msg            (Folder         *folder,
66                                          FolderItem     *item,
67                                          gint            num);
68 static MsgInfo *mh_get_msginfo          (Folder         *folder,
69                                          FolderItem     *item,
70                                          gint            num);
71 static gint     mh_add_msg              (Folder         *folder,
72                                          FolderItem     *dest,
73                                          const gchar    *file,
74                                          MsgFlags       *flags);
75 static gint     mh_add_msgs             (Folder         *folder,
76                                          FolderItem     *dest,
77                                          GSList         *file_list,
78                                          GRelation      *relation);
79 static gint     mh_copy_msg             (Folder         *folder,
80                                          FolderItem     *dest,
81                                          MsgInfo        *msginfo);
82 static gint     mh_copy_msgs            (Folder         *folder, 
83                                          FolderItem     *dest, 
84                                          MsgInfoList    *msglist, 
85                                          GRelation      *relation);
86 static gint     mh_remove_msg           (Folder         *folder,
87                                          FolderItem     *item,
88                                          gint            num);
89 static gint     mh_remove_msgs          (Folder         *folder, 
90                                          FolderItem     *item, 
91                                          MsgInfoList    *msglist, 
92                                          GRelation      *relation);
93 static gint     mh_remove_all_msg       (Folder         *folder,
94                                          FolderItem     *item);
95 static gboolean mh_is_msg_changed       (Folder         *folder,
96                                          FolderItem     *item,
97                                          MsgInfo        *msginfo);
98
99 static gint     mh_get_num_list         (Folder         *folder,
100                                          FolderItem     *item, 
101                                          GSList         **list, 
102                                          gboolean       *old_uids_valid);
103 static gint     mh_scan_tree            (Folder         *folder);
104
105 static gint    mh_create_tree           (Folder         *folder);
106 static FolderItem *mh_create_folder     (Folder         *folder,
107                                          FolderItem     *parent,
108                                          const gchar    *name);
109 static gint    mh_rename_folder         (Folder         *folder,
110                                          FolderItem     *item,
111                                          const gchar    *name);
112 static gint    mh_remove_folder         (Folder         *folder,
113                                          FolderItem     *item);
114
115 static gchar   *mh_get_new_msg_filename         (FolderItem     *dest);
116
117 static MsgInfo *mh_parse_msg                    (const gchar    *file,
118                                                  FolderItem     *item);
119 static void     mh_remove_missing_folder_items  (Folder         *folder);
120 static gchar    *mh_filename_from_utf8          (const gchar    *path);
121 static gchar    *mh_filename_to_utf8            (const gchar    *path);
122 static void     mh_scan_tree_recursive          (FolderItem     *item);
123
124 static gboolean mh_rename_folder_func           (GNode          *node,
125                                                  gpointer        data);
126 static gchar   *mh_item_get_path                (Folder *folder, 
127                                                  FolderItem *item);
128
129 static gboolean mh_scan_required        (Folder         *folder,
130                                          FolderItem     *item);
131 static int mh_item_close                (Folder         *folder,
132                                          FolderItem     *item);
133 #if 0
134 static gint mh_get_flags                (Folder *folder, FolderItem *item,
135                                          MsgInfoList *msginfo_list, GRelation *msgflags);
136 #endif
137 static void mh_write_sequences          (FolderItem     *item, gboolean remove_unseen);
138
139 static FolderClass mh_class;
140
141 FolderClass *mh_get_class(void)
142 {
143         if (mh_class.idstr == NULL) {
144                 mh_class.type = F_MH;
145                 mh_class.idstr = "mh";
146                 mh_class.uistr = "MH";
147                 
148                 /* Folder functions */
149                 mh_class.new_folder = mh_folder_new;
150                 mh_class.destroy_folder = mh_folder_destroy;
151                 mh_class.set_xml = folder_local_set_xml;
152                 mh_class.get_xml = folder_local_get_xml;
153                 mh_class.scan_tree = mh_scan_tree;
154                 mh_class.create_tree = mh_create_tree;
155
156                 /* FolderItem functions */
157                 mh_class.item_get_path = mh_item_get_path;
158                 mh_class.create_folder = mh_create_folder;
159                 mh_class.rename_folder = mh_rename_folder;
160                 mh_class.remove_folder = mh_remove_folder;
161                 mh_class.get_num_list = mh_get_num_list;
162                 mh_class.scan_required = mh_scan_required;
163                 mh_class.close = mh_item_close;
164                 mh_class.get_flags = NULL; /*mh_get_flags */;
165
166                 /* Message functions */
167                 mh_class.get_msginfo = mh_get_msginfo;
168                 mh_class.fetch_msg = mh_fetch_msg;
169                 mh_class.add_msg = mh_add_msg;
170                 mh_class.add_msgs = mh_add_msgs;
171                 mh_class.copy_msg = mh_copy_msg;
172                 mh_class.copy_msgs = mh_copy_msgs;
173                 mh_class.remove_msg = mh_remove_msg;
174                 mh_class.remove_msgs = mh_remove_msgs;
175                 mh_class.remove_all_msg = mh_remove_all_msg;
176                 mh_class.is_msg_changed = mh_is_msg_changed;
177         }
178
179         return &mh_class;
180 }
181
182 static Folder *mh_folder_new(const gchar *name, const gchar *path)
183 {
184         Folder *folder;
185
186         folder = (Folder *)g_new0(MHFolder, 1);
187         folder->klass = &mh_class;
188         mh_folder_init(folder, name, path);
189
190         return folder;
191 }
192
193 static void mh_folder_destroy(Folder *folder)
194 {
195         folder_local_folder_destroy(LOCAL_FOLDER(folder));
196 }
197
198 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
199 {
200         folder_local_folder_init(folder, name, path);
201
202 }
203
204 gboolean mh_scan_required(Folder *folder, FolderItem *item)
205 {
206         gchar *path;
207         struct stat s;
208
209         path = folder_item_get_path(item);
210         g_return_val_if_fail(path != NULL, FALSE);
211
212         if (stat(path, &s) < 0) {
213                 FILE_OP_ERROR(path, "stat");
214                 g_free(path);
215                 return FALSE;
216         }
217
218         if ((s.st_mtime > item->mtime) &&
219                 (s.st_mtime - 3600 != item->mtime)) {
220                 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
221                             path,
222                             (long int) s.st_mtime,
223                             (long int) item->mtime);
224                 g_free(path);
225                 return TRUE;
226         }
227
228         debug_print("MH scan not required: %s (%ld <= %ld)\n",
229                     path,
230                     (long int) s.st_mtime,
231                     (long int) item->mtime);
232         g_free(path);
233         return FALSE;
234 }
235
236 static void mh_get_last_num(Folder *folder, FolderItem *item)
237 {
238         gchar *path;
239         DIR *dp;
240         struct dirent *d;
241         gint max = 0;
242         gint num;
243
244         g_return_if_fail(item != NULL);
245
246         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
247
248         path = folder_item_get_path(item);
249         g_return_if_fail(path != NULL);
250         if (change_dir(path) < 0) {
251                 g_free(path);
252                 return;
253         }
254         g_free(path);
255
256         if ((dp = opendir(".")) == NULL) {
257                 FILE_OP_ERROR(item->path, "opendir");
258                 return;
259         }
260
261         while ((d = readdir(dp)) != NULL) {
262                 if ((num = to_number(d->d_name)) > 0 &&
263                     dirent_is_regular_file(d)) {
264                         if (max < num)
265                                 max = num;
266                 }
267         }
268         closedir(dp);
269
270         debug_print("Last number in dir %s = %d\n", item->path, max);
271         item->last_num = max;
272 }
273
274 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
275 {
276
277         gchar *path;
278         DIR *dp;
279         struct dirent *d;
280         gint num, nummsgs = 0;
281
282         g_return_val_if_fail(item != NULL, -1);
283
284         debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
285
286         *old_uids_valid = TRUE;
287
288         path = folder_item_get_path(item);
289         g_return_val_if_fail(path != NULL, -1);
290         if (change_dir(path) < 0) {
291                 g_free(path);
292                 return -1;
293         }
294         g_free(path);
295
296         if ((dp = opendir(".")) == NULL) {
297                 FILE_OP_ERROR(item->path, "opendir");
298                 return -1;
299         }
300
301         while ((d = readdir(dp)) != NULL) {
302                 if ((num = to_number(d->d_name)) > 0) {
303                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
304                         nummsgs++;
305                 }
306         }
307         closedir(dp);
308
309         mh_set_mtime(item);
310         return nummsgs;
311 }
312
313 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
314 {
315         gchar *path;
316         gchar *file;
317
318         g_return_val_if_fail(item != NULL, NULL);
319         g_return_val_if_fail(num > 0, NULL);
320
321         path = folder_item_get_path(item);
322         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
323
324         if (!is_file_exist(file)) {
325                 g_free(file);
326                 g_free(path);
327                 return NULL;
328         }
329         g_free(path);
330         return file;
331 }
332
333 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
334 {
335         MsgInfo *msginfo;
336         gchar *file;
337
338         g_return_val_if_fail(item != NULL, NULL);
339         if (num <= 0)
340                 return NULL;
341
342         file = mh_fetch_msg(folder, item, num);
343         if (!file) return NULL;
344
345         msginfo = mh_parse_msg(file, item);
346         if (msginfo)
347                 msginfo->msgnum = num;
348
349         g_free(file);
350
351         return msginfo;
352 }
353
354 static gchar *mh_get_new_msg_filename(FolderItem *dest)
355 {
356         gchar *destfile;
357         gchar *destpath;
358
359         destpath = folder_item_get_path(dest);
360         g_return_val_if_fail(destpath != NULL, NULL);
361
362         if (!is_dir_exist(destpath))
363                 make_dir_hier(destpath);
364
365         for (;;) {
366                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
367                                            dest->last_num + 1);
368                 if (is_file_entry_exist(destfile)) {
369                         dest->last_num++;
370                         g_free(destfile);
371                 } else
372                         break;
373         }
374
375         g_free(destpath);
376
377         return destfile;
378 }
379
380 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
381 {
382         gint ret;
383         GSList file_list;
384         MsgFileInfo fileinfo;
385
386         g_return_val_if_fail(file != NULL, -1);
387
388         fileinfo.msginfo = NULL;
389         fileinfo.file = (gchar *)file;
390         fileinfo.flags = flags;
391         file_list.data = &fileinfo;
392         file_list.next = NULL;
393
394         ret = mh_add_msgs(folder, dest, &file_list, NULL);
395         return ret;
396
397  
398 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
399                  GRelation *relation)
400
401         gchar *destfile;
402         GSList *cur;
403         MsgFileInfo *fileinfo;
404
405         g_return_val_if_fail(dest != NULL, -1);
406         g_return_val_if_fail(file_list != NULL, -1);
407
408         if (dest->last_num < 0) {
409                 mh_get_last_num(folder, dest);
410                 if (dest->last_num < 0) return -1;
411         }
412
413         for (cur = file_list; cur != NULL; cur = cur->next) {
414                 fileinfo = (MsgFileInfo *)cur->data;
415
416                 destfile = mh_get_new_msg_filename(dest);
417                 if (destfile == NULL) return -1;
418
419 #ifdef G_OS_UNIX
420                 if (link(fileinfo->file, destfile) < 0) {
421 #endif
422                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
423                                 g_warning(_("can't copy message %s to %s\n"),
424                                           fileinfo->file, destfile);
425                                 g_free(destfile);
426                                 return -1;
427                         }
428 #ifdef G_OS_UNIX
429                 }
430 #endif
431
432                 if (relation != NULL)
433                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
434                 g_free(destfile);
435                 dest->last_num++;
436         }
437         mh_write_sequences(dest, TRUE);
438         return dest->last_num;
439 }
440
441 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
442 {
443         GSList msglist;
444
445         g_return_val_if_fail(msginfo != NULL, -1);
446
447         msglist.data = msginfo;
448         msglist.next = NULL;
449
450         return mh_copy_msgs(folder, dest, &msglist, NULL);      
451 }
452
453 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist, 
454                          GRelation *relation)
455 {
456         gboolean dest_need_scan = FALSE;
457         gchar *srcfile;
458         gchar *destfile;
459         gint filemode = 0;
460         FolderItemPrefs *prefs;
461         MsgInfo *msginfo = NULL;
462         MsgInfoList *cur = NULL;
463         gint curnum = 0, total = 0;
464         gchar *srcpath = NULL;
465         gboolean full_fetch = FALSE;
466         time_t last_mtime = (time_t)0;
467
468         g_return_val_if_fail(dest != NULL, -1);
469         g_return_val_if_fail(msglist != NULL, -1);
470         
471         msginfo = (MsgInfo *)msglist->data;
472
473         g_return_val_if_fail(msginfo != NULL, -1);
474
475         if (msginfo->folder == dest) {
476                 g_warning("the src folder is identical to the dest.\n");
477                 return -1;
478         }
479
480         if (msginfo->folder->folder != dest->folder)
481                 full_fetch = TRUE;
482
483         if (dest->last_num < 0) {
484                 mh_get_last_num(folder, dest);
485                 if (dest->last_num < 0) return -1;
486         }
487
488         prefs = dest->prefs;
489
490         srcpath = folder_item_get_path(msginfo->folder);
491
492         dest_need_scan = mh_scan_required(dest->folder, dest);
493         last_mtime = dest->mtime;
494
495         total = g_slist_length(msglist);
496         if (total > 100) {
497                 if (MSG_IS_MOVE(msginfo->flags))
498                         statusbar_print_all(_("Moving messages..."));
499                 else
500                         statusbar_print_all(_("Copying messages..."));
501         }
502         for (cur = msglist; cur; cur = cur->next) {
503                 msginfo = (MsgInfo *)cur->data;
504                 if (!msginfo) {
505                         goto err_reset_status;
506                 }
507                 if (!full_fetch) {
508                         srcfile = g_strconcat(srcpath, 
509                                 G_DIR_SEPARATOR_S, 
510                                 itos(msginfo->msgnum), NULL);
511                 } else {
512                         srcfile = procmsg_get_message_file(msginfo);
513                 }
514                 if (!srcfile) {
515                         goto err_reset_status;
516                 }
517                 destfile = mh_get_new_msg_filename(dest);
518                 if (!destfile) {
519                         g_free(srcfile);
520                         goto err_reset_status;
521                 }
522
523                 if (total > 100) {
524                         statusbar_progress_all(curnum, total, 100);
525                         if (curnum % 100 == 0)
526                                 GTK_EVENTS_FLUSH();
527                         curnum++;
528                 }
529
530                 debug_print("Copying message %s%c%d to %s ...\n",
531                             msginfo->folder->path, G_DIR_SEPARATOR,
532                             msginfo->msgnum, dest->path);
533
534
535                 if (MSG_IS_MOVE(msginfo->flags)) {
536                         msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
537                         if (move_file(srcfile, destfile, TRUE) < 0) {
538                                 FILE_OP_ERROR(srcfile, "move");
539                                 if (copy_file(srcfile, destfile, TRUE) < 0) {
540                                         FILE_OP_ERROR(srcfile, "copy");
541                                         g_free(srcfile);
542                                         g_free(destfile);
543                                         goto err_reset_status;
544                                 }
545                         } else {
546                                 /* say unlinking's not necessary */
547                                 msginfo->flags.tmp_flags |= MSG_MOVE_DONE;
548                         }
549                 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
550                         FILE_OP_ERROR(srcfile, "copy");
551                         g_free(srcfile);
552                         g_free(destfile);
553                         goto err_reset_status;
554                 } 
555                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
556                         if (chmod(destfile, prefs->folder_chmod) < 0)
557                                 FILE_OP_ERROR(destfile, "chmod");
558
559                         /* for mark file */
560                         filemode = prefs->folder_chmod;
561                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
562                         if (filemode & S_IROTH) filemode |= S_IWOTH;
563                 }
564                 if (relation)
565                         g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
566                 g_free(srcfile);
567                 g_free(destfile);
568                 dest->last_num++;
569         }
570
571         g_free(srcpath);
572         mh_write_sequences(dest, TRUE);
573
574         if (dest->mtime == last_mtime && !dest_need_scan) {
575                 mh_set_mtime(dest);
576         }
577         if (total > 100) {
578                 statusbar_progress_all(0,0,0);
579                 statusbar_pop_all();
580         }
581         return dest->last_num;
582 err_reset_status:
583         g_free(srcpath);
584         mh_write_sequences(dest, TRUE);
585         if (total > 100) {
586                 statusbar_progress_all(0,0,0);
587                 statusbar_pop_all();
588         }
589         return -1;
590
591 }
592
593 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
594 {
595         gboolean need_scan = FALSE;
596         time_t last_mtime = (time_t)0;
597         gchar *file;
598
599         g_return_val_if_fail(item != NULL, -1);
600
601         file = mh_fetch_msg(folder, item, num);
602         g_return_val_if_fail(file != NULL, -1);
603
604         need_scan = mh_scan_required(folder, item);
605         last_mtime = item->mtime;
606
607         if (g_unlink(file) < 0) {
608                 FILE_OP_ERROR(file, "unlink");
609                 g_free(file);
610                 return -1;
611         }
612
613         if (item->mtime == last_mtime && !need_scan) {
614                 mh_set_mtime(item);
615         }
616         g_free(file);
617         return 0;
618 }
619
620 static gint mh_remove_msgs(Folder *folder, FolderItem *item, 
621                     MsgInfoList *msglist, GRelation *relation)
622 {
623         gboolean need_scan = FALSE;
624         gchar *path, *file;
625         time_t last_mtime = (time_t)0;
626         MsgInfoList *cur;
627         gint total = 0, curnum = 0;
628
629         g_return_val_if_fail(item != NULL, -1);
630
631         path = folder_item_get_path(item);
632         
633         need_scan = mh_scan_required(folder, item);
634         last_mtime = item->mtime;
635
636         total = g_slist_length(msglist);
637         if (total > 100) {
638                 statusbar_print_all(_("Deleting messages..."));
639         }
640
641         for (cur = msglist; cur; cur = cur->next) {
642                 MsgInfo *msginfo = (MsgInfo *)cur->data;
643                 if (msginfo == NULL)
644                         continue;
645                 if (MSG_IS_MOVE(msginfo->flags) && MSG_IS_MOVE_DONE(msginfo->flags)) {
646                         msginfo->flags.tmp_flags &= ~MSG_MOVE_DONE;
647                         continue;
648                 }
649                 if (total > 100) {
650                         statusbar_progress_all(curnum, total, 100);
651                         if (curnum % 100 == 0)
652                                 GTK_EVENTS_FLUSH();
653                         curnum++;
654                 }
655
656                 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
657                 if (file == NULL)
658                         continue;
659                 
660                 if (g_unlink(file) < 0) {
661                         g_free(file);
662                         continue;
663                 }
664                 
665                 g_free(file);
666         }
667
668         if (total > 100) {
669                 statusbar_progress_all(0,0,0);
670                 statusbar_pop_all();
671         }
672         if (item->mtime == last_mtime && !need_scan) {
673                 mh_set_mtime(item);
674         }
675
676         g_free(path);
677         return 0;
678 }
679
680 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
681 {
682         gchar *path;
683         gint val;
684
685         g_return_val_if_fail(item != NULL, -1);
686
687         path = folder_item_get_path(item);
688         g_return_val_if_fail(path != NULL, -1);
689         val = remove_all_numbered_files(path);
690         g_free(path);
691
692         mh_write_sequences(item, TRUE);
693
694         return val;
695 }
696
697 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
698                                   MsgInfo *msginfo)
699 {
700         struct stat s;
701
702         if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
703             msginfo->size  != s.st_size || (
704                 (msginfo->mtime - s.st_mtime != 0) &&
705                 (msginfo->mtime - s.st_mtime != 3600) &&
706                 (msginfo->mtime - s.st_mtime != -3600)))
707                 return TRUE;
708
709         return FALSE;
710 }
711
712 static gint mh_scan_tree(Folder *folder)
713 {
714         FolderItem *item;
715         gchar *rootpath;
716
717         g_return_val_if_fail(folder != NULL, -1);
718
719         if (!folder->node) {
720                 item = folder_item_new(folder, folder->name, NULL);
721                 item->folder = folder;
722                 folder->node = item->node = g_node_new(item);
723         } else
724                 item = FOLDER_ITEM(folder->node->data);
725
726         rootpath = folder_item_get_path(item);
727         if (change_dir(rootpath) < 0) {
728                 g_free(rootpath);
729                 return -1;
730         }
731         g_free(rootpath);
732
733         mh_create_tree(folder);
734         mh_remove_missing_folder_items(folder);
735         mh_scan_tree_recursive(item);
736
737         return 0;
738 }
739
740 #define MAKE_DIR_IF_NOT_EXIST(dir) \
741 { \
742         if (!is_dir_exist(dir)) { \
743                 if (is_file_exist(dir)) { \
744                         g_warning("File `%s' already exists.\n" \
745                                     "Can't create folder.", dir); \
746                         return -1; \
747                 } \
748                 if (make_dir_hier(dir) < 0) \
749                         return -1; \
750         } \
751 }
752
753 static gint mh_create_tree(Folder *folder)
754 {
755         gchar *rootpath;
756
757         g_return_val_if_fail(folder != NULL, -1);
758
759         CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
760         rootpath = LOCAL_FOLDER(folder)->rootpath;
761         MAKE_DIR_IF_NOT_EXIST(rootpath);
762         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
763         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
764         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
765         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
766         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
767         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
768
769         return 0;
770 }
771
772 #undef MAKE_DIR_IF_NOT_EXIST
773
774 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
775 {
776         gchar *folder_path, *path;
777         gchar *real_path;
778         g_return_val_if_fail(folder != NULL, NULL);
779         g_return_val_if_fail(item != NULL, NULL);
780
781         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
782         g_return_val_if_fail(folder_path != NULL, NULL);
783
784         /* FIXME: [W32] The code below does not correctly merge
785            relative filenames; there should be a function to handle
786            this.  */
787         if ( !is_relative_filename (folder_path) ) {
788                 if (item->path)
789                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
790                                            item->path, NULL);
791                 else
792                         path = g_strdup(folder_path);
793         } else {
794                 if (item->path)
795                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
796                                            folder_path, G_DIR_SEPARATOR_S,
797                                            item->path, NULL);
798                 else
799                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
800                                            folder_path, NULL);
801         }
802         g_free(folder_path);
803         real_path = mh_filename_from_utf8(path);
804         if (!is_dir_exist(real_path) && is_dir_exist(path)) {
805                 /* mmh, older version did put utf8 filenames instead of
806                  * the correct encoding */
807                 rename(path, real_path);
808                 folder_item_scan(item);
809         }
810
811         g_free(path);
812         return real_path;
813 }
814
815 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
816                                     const gchar *name)
817 {
818         gchar *path, *real_name;
819         gchar *fullpath;
820         FolderItem *new_item;
821         gchar *mh_sequences_filename;
822         FILE *mh_sequences_file;
823
824         g_return_val_if_fail(folder != NULL, NULL);
825         g_return_val_if_fail(parent != NULL, NULL);
826         g_return_val_if_fail(name != NULL, NULL);
827
828         path = folder_item_get_path(parent);
829         if (!is_dir_exist(path)) 
830                 if (make_dir_hier(path) != 0)
831                         return NULL;
832                 
833         real_name = mh_filename_from_utf8(name);
834         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
835         g_free(real_name);
836         g_free(path);
837
838         if (make_dir(fullpath) < 0) {
839                 g_free(fullpath);
840                 return NULL;
841         }
842
843         g_free(fullpath);
844
845         if (parent->path)
846                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
847                                    NULL);
848         else
849                 path = g_strdup(name);
850         new_item = folder_item_new(folder, name, path);
851         folder_item_append(parent, new_item);
852
853         g_free(path);
854
855         path = folder_item_get_path(new_item);
856         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
857                                             ".mh_sequences", NULL);
858         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
859                 fclose(mh_sequences_file);
860         }
861         g_free(mh_sequences_filename);
862         g_free(path);
863
864         return new_item;
865 }
866
867 static gint mh_rename_folder(Folder *folder, FolderItem *item,
868                              const gchar *name)
869 {
870         gchar *real_name;
871         gchar *oldpath;
872         gchar *dirname;
873         gchar *newpath, *utf8newpath;
874         gchar *paths[2];
875
876         g_return_val_if_fail(folder != NULL, -1);
877         g_return_val_if_fail(item != NULL, -1);
878         g_return_val_if_fail(item->path != NULL, -1);
879         g_return_val_if_fail(name != NULL, -1);
880
881         oldpath = folder_item_get_path(item);
882         if (!is_dir_exist(oldpath))
883                 make_dir_hier(oldpath);
884
885         dirname = g_path_get_dirname(oldpath);
886         real_name = mh_filename_from_utf8(name);
887         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
888         g_free(real_name);
889
890         if (g_rename(oldpath, newpath) < 0) {
891                 FILE_OP_ERROR(oldpath, "rename");
892                 g_free(oldpath);
893                 g_free(newpath);
894                 return -1;
895         }
896
897         g_free(oldpath);
898         g_free(newpath);
899
900         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
901                 dirname = g_path_get_dirname(item->path);
902                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
903                                           name, NULL);
904                 g_free(dirname);
905         } else
906                 utf8newpath = g_strdup(name);
907
908         g_free(item->name);
909         item->name = g_strdup(name);
910
911         paths[0] = g_strdup(item->path);
912         paths[1] = utf8newpath;
913         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
914                         mh_rename_folder_func, paths);
915
916         g_free(paths[0]);
917         g_free(paths[1]);
918         return 0;
919 }
920
921 static gint mh_remove_folder(Folder *folder, FolderItem *item)
922 {
923         gchar *path;
924
925         g_return_val_if_fail(folder != NULL, -1);
926         g_return_val_if_fail(item != NULL, -1);
927         g_return_val_if_fail(item->path != NULL, -1);
928
929         path = folder_item_get_path(item);
930         if (remove_dir_recursive(path) < 0) {
931                 g_warning("can't remove directory `%s'\n", path);
932                 g_free(path);
933                 return -1;
934         }
935
936         g_free(path);
937         folder_item_remove(item);
938         return 0;
939 }
940
941 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
942 {
943         MsgInfo *msginfo;
944         MsgFlags flags;
945
946         g_return_val_if_fail(item != NULL, NULL);
947         g_return_val_if_fail(file != NULL, NULL);
948
949         flags.perm_flags = MSG_NEW|MSG_UNREAD;
950         flags.tmp_flags = 0;
951
952         if (folder_has_parent_of_type(item, F_QUEUE)) {
953                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
954         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
955                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
956         }
957
958         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
959         if (!msginfo) return NULL;
960
961         msginfo->msgnum = atoi(file);
962         msginfo->folder = item;
963
964         return msginfo;
965 }
966
967 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
968 {
969         FolderItem *item;
970         gchar *path;
971
972         g_return_val_if_fail(node->data != NULL, FALSE);
973
974         if (G_NODE_IS_ROOT(node))
975                 return FALSE;
976
977         item = FOLDER_ITEM(node->data);
978
979         path = folder_item_get_path(item);
980         if (!is_dir_exist(path)) {
981                 debug_print("folder '%s' not found. removing...\n", path);
982                 folder_item_remove(item);
983         }
984         g_free(path);
985
986         return FALSE;
987 }
988
989 static void mh_remove_missing_folder_items(Folder *folder)
990 {
991         g_return_if_fail(folder != NULL);
992
993         debug_print("searching missing folders...\n");
994
995         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
996                         mh_remove_missing_folder_items_func, folder);
997 }
998
999 static void mh_scan_tree_recursive(FolderItem *item)
1000 {
1001         Folder *folder;
1002 #ifdef G_OS_WIN32
1003         GDir *dir;
1004 #else
1005         DIR *dp;
1006         struct dirent *d;
1007 #endif
1008         const gchar *dir_name;
1009         struct stat s;
1010         gchar *real_path, *entry, *utf8entry, *utf8name;
1011         gint n_msg = 0;
1012
1013         g_return_if_fail(item != NULL);
1014         g_return_if_fail(item->folder != NULL);
1015
1016         folder = item->folder;
1017
1018         real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
1019 #ifdef G_OS_WIN32
1020         dir = g_dir_open(real_path, 0, NULL);
1021         if (!dir) {
1022                 g_warning("failed to open directory: %s\n", real_path);
1023                 g_free(real_path);
1024                 return;
1025         }
1026 #else
1027         dp = opendir(real_path);
1028         if (!dp) {
1029                 FILE_OP_ERROR(real_path, "opendir");
1030                 return;
1031         }
1032 #endif
1033         g_free(real_path);
1034
1035         debug_print("scanning %s ...\n",
1036                     item->path ? item->path
1037                     : LOCAL_FOLDER(item->folder)->rootpath);
1038         if (folder->ui_func)
1039                 folder->ui_func(folder, item, folder->ui_func_data);
1040
1041 #ifdef G_OS_WIN32
1042         while ((dir_name = g_dir_read_name(dir)) != NULL) {
1043 #else
1044         while ((d = readdir(dp)) != NULL) {
1045                 dir_name = d->d_name;
1046 #endif
1047                 if (dir_name[0] == '.') continue;
1048
1049                 utf8name = mh_filename_to_utf8(dir_name);
1050                 if (item->path)
1051                         utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1052                                                 utf8name, NULL);
1053                 else
1054                         utf8entry = g_strdup(utf8name);
1055                 entry = mh_filename_from_utf8(utf8entry);
1056
1057                 if (
1058 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1059                         d->d_type == DT_DIR ||
1060                         (d->d_type == DT_UNKNOWN &&
1061 #endif
1062                         g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
1063 #if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
1064                         )
1065 #endif
1066                    ) {
1067                         FolderItem *new_item = NULL;
1068                         GNode *node;
1069
1070                         node = item->node;
1071                         for (node = node->children; node != NULL; node = node->next) {
1072                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
1073                                 gchar *curpath = mh_filename_from_utf8(cur_item->path);
1074                                 if (!strcmp2(curpath, entry)) {
1075                                         new_item = cur_item;
1076                                         g_free(curpath);
1077                                         break;
1078                                 }
1079                                 g_free(curpath);
1080                         }
1081                         if (!new_item) {
1082                                 debug_print("new folder '%s' found.\n", entry);
1083                                 new_item = folder_item_new(folder, utf8name, utf8entry);
1084                                 folder_item_append(item, new_item);
1085                         }
1086
1087                         if (!item->path) {
1088                                 if (!folder->inbox &&
1089                                     !strcmp(dir_name, INBOX_DIR)) {
1090                                         new_item->stype = F_INBOX;
1091                                         folder->inbox = new_item;
1092                                 } else if (!folder->outbox &&
1093                                            !strcmp(dir_name, OUTBOX_DIR)) {
1094                                         new_item->stype = F_OUTBOX;
1095                                         folder->outbox = new_item;
1096                                 } else if (!folder->draft &&
1097                                            !strcmp(dir_name, DRAFT_DIR)) {
1098                                         new_item->stype = F_DRAFT;
1099                                         folder->draft = new_item;
1100                                 } else if (!folder->queue &&
1101                                            !strcmp(dir_name, QUEUE_DIR)) {
1102                                         new_item->stype = F_QUEUE;
1103                                         folder->queue = new_item;
1104                                 } else if (!folder->trash &&
1105                                            !strcmp(dir_name, TRASH_DIR)) {
1106                                         new_item->stype = F_TRASH;
1107                                         folder->trash = new_item;
1108                                 }
1109                         }
1110
1111                         mh_scan_tree_recursive(new_item);
1112                 } else if (to_number(dir_name) > 0) n_msg++;
1113
1114                 g_free(entry);
1115                 g_free(utf8entry);
1116                 g_free(utf8name);
1117         }
1118
1119 #ifdef G_OS_WIN32
1120         g_dir_close(dir);
1121 #else
1122         closedir(dp);
1123 #endif
1124
1125         mh_set_mtime(item);
1126 }
1127
1128 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1129 {
1130         FolderItem *item = node->data;
1131         gchar **paths = data;
1132         const gchar *oldpath = paths[0];
1133         const gchar *newpath = paths[1];
1134         gchar *base;
1135         gchar *new_itempath;
1136         gint oldpathlen;
1137
1138         oldpathlen = strlen(oldpath);
1139         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1140                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1141                 return TRUE;
1142         }
1143
1144         base = item->path + oldpathlen;
1145         while (*base == G_DIR_SEPARATOR) base++;
1146         if (*base == '\0')
1147                 new_itempath = g_strdup(newpath);
1148         else
1149                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1150                                            NULL);
1151         g_free(item->path);
1152         item->path = new_itempath;
1153
1154         return FALSE;
1155 }
1156
1157 static gchar *mh_filename_from_utf8(const gchar *path)
1158 {
1159         gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1160
1161         if (!real_path) {
1162                 g_warning("mh_filename_from_utf8: failed to convert character set\n");
1163                 real_path = g_strdup(path);
1164         }
1165
1166         return real_path;
1167 }
1168
1169 static gchar *mh_filename_to_utf8(const gchar *path)
1170 {
1171         gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1172         if (!utf8path) {
1173                 g_warning("mh_filename_to_utf8: failed to convert character set\n");
1174                 utf8path = g_strdup(path);
1175         }
1176
1177         return utf8path;
1178 }
1179
1180 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1181 {
1182         MsgInfo *msginfo_a = (MsgInfo *) a;
1183         MsgInfo *msginfo_b = (MsgInfo *) b;
1184
1185         return (msginfo_a->msgnum - msginfo_b->msgnum);
1186 }
1187
1188 static gchar *get_unseen_seq_name(void)
1189 {
1190         static gchar *seq_name = NULL;
1191         if (!seq_name) {
1192                 gchar buf[BUFFSIZE];
1193                 gchar *tmp;
1194                 gchar *profile_path = g_strconcat(
1195                         get_home_dir(), G_DIR_SEPARATOR_S,
1196                         ".mh_profile", NULL);
1197                 FILE *fp = g_fopen(profile_path, "r");
1198                 if (fp) {
1199                         while (fgets(buf, sizeof(buf), fp) != NULL) {
1200                                 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1201                                         gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1202                                         while (*seq_tmp == ' ')
1203                                                 seq_tmp++;
1204                                         seq_name = g_strdup(seq_tmp);
1205                                         seq_name = strretchomp(seq_name);
1206                                         break;
1207                                 }
1208                         }
1209                         fclose(fp);
1210                 }
1211                 if (!seq_name)
1212                         seq_name = g_strdup("unseen");
1213                 tmp = g_strdup_printf("%s:", seq_name);
1214                 g_free(seq_name);
1215                 seq_name = tmp;
1216         }
1217         return seq_name;        
1218 }
1219
1220 #if 0
1221 static gint mh_get_flags(Folder *folder, FolderItem *item,
1222                            MsgInfoList *msginfo_list, GRelation *msgflags)
1223 {
1224         gchar *mh_sequences_filename;
1225         FILE *mh_sequences_file;
1226         gchar buf[BUFFSIZE];
1227         gchar *unseen_list = NULL;
1228         gchar *path;
1229         MsgInfoList *mcur = NULL;
1230 /*
1231         GTimer *timer = g_timer_new();
1232         g_timer_start(timer);
1233 */
1234         if (!item)
1235                 return 0;
1236
1237         /* don't update from .mh_sequences if the item's opened: mails may have
1238          * been marked read/unread and it's not yet written in the file. */     
1239         if (item->opened)
1240                 return 0;
1241
1242         path = folder_item_get_path(item);
1243
1244         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1245                                             ".mh_sequences", NULL);
1246         g_free(path);
1247         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
1248                 while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
1249                         if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
1250                                 unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
1251                                 break;
1252                         }
1253                 }
1254                 fclose(mh_sequences_file);
1255         }
1256         
1257         g_free(mh_sequences_filename);
1258         
1259         if (unseen_list) {
1260                 gchar *cur = NULL;
1261                 gchar *token = NULL, *next = NULL, *boundary = NULL;
1262                 gint num = 0;
1263                 GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1264
1265                 cur = unseen_list = strretchomp(unseen_list);
1266                 debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
1267 next_token:
1268                 while (*cur && *cur == ' ')
1269                         cur++;
1270                 
1271                 if ((next = strchr(cur, ' ')) != NULL) {
1272                         token = cur;
1273                         cur = next+1;
1274                         *next = '\0';
1275                 } else {
1276                         token = cur;
1277                         cur = NULL;
1278                 }
1279                 
1280                 if ((boundary = strchr(token, '-')) != NULL) {
1281                         gchar *start, *end;
1282                         int i;
1283                         start = token;
1284                         end = boundary+1;
1285                         *boundary='\0';
1286                         for (i = atoi(start); i <= atoi(end); i++) {
1287                                 g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
1288                         }
1289                 } else if ((num = atoi(token)) > 0) {
1290                         g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
1291                 }
1292                 
1293                 if (cur)
1294                         goto next_token;
1295                 for (mcur = msginfo_list; mcur; mcur = mcur->next) {
1296                         MsgInfo *msginfo = (MsgInfo *)mcur->data;
1297                         MsgPermFlags flags = msginfo->flags.perm_flags;
1298                         if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
1299                                 flags |= MSG_UNREAD;
1300                         } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
1301                                 flags &= ~(MSG_UNREAD);
1302                         }
1303                         if (flags != msginfo->flags.perm_flags)
1304                                 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
1305                 }
1306                 g_hash_table_destroy(unseen_table);
1307                 g_free(unseen_list);
1308         }
1309 /*
1310         g_timer_stop(timer);
1311         printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1312         g_timer_destroy(timer);
1313 */
1314         return 0;
1315 }
1316 #endif
1317
1318 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1319 {
1320         gchar *mh_sequences_old, *mh_sequences_new;
1321         FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1322         gchar buf[BUFFSIZE];
1323         gchar *path = NULL;
1324         START_TIMING("");
1325
1326         if (!item)
1327                 return;
1328         
1329         path = folder_item_get_path(item);
1330
1331         mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1332                                             ".mh_sequences", NULL);
1333         mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1334                                             ".mh_sequences.new", NULL);
1335         if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1336                 GSList *msglist = folder_item_get_msg_list(item);
1337                 GSList *cur;
1338                 MsgInfo *info = NULL;
1339                 gint start = -1, end = -1;
1340                 gchar *sequence = g_strdup("");
1341                 gint seq_len = 0;
1342                 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1343                 cur = msglist;
1344                 
1345                 /* write the unseen sequence if we don't have to scrap it */
1346                 if (!remove_unseen) do {
1347                         info = (MsgInfo *)(cur ? cur->data:NULL);
1348                         if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1349                                 if (start < 0)
1350                                         start = end = info->msgnum;
1351                                 else
1352                                         end = info->msgnum;
1353                         } else {
1354                                 if (start > 0 && end > 0) {
1355                                         gchar tmp[32];
1356                                         gint tmp_len = 0;
1357                                         if (start != end)
1358                                                 snprintf(tmp, 31, " %d-%d", start, end);
1359                                         else
1360                                                 snprintf(tmp, 31, " %d", start);
1361                                         
1362                                         tmp_len = strlen(tmp);
1363                                         sequence = g_realloc(sequence, seq_len+tmp_len+1);
1364                                         strcpy(sequence+seq_len, tmp);
1365                                         seq_len += tmp_len;
1366
1367                                         start = end = -1;
1368                                 }
1369                         }
1370                         cur = cur ? cur->next:NULL;
1371                 } while (cur || (start > 0 && end > 0));
1372                 if (sequence && *sequence) {
1373                         fprintf(mh_sequences_new_fp, "%s%s\n", 
1374                                         get_unseen_seq_name(), sequence);
1375                         debug_print("wrote unseen sequence: '%s%s'\n", 
1376                                         get_unseen_seq_name(), sequence);
1377                 }
1378                 /* rewrite the rest of the file */
1379                 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1380                         while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1381                                 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1382                                         fprintf(mh_sequences_new_fp, "%s", buf);
1383                         }
1384                         fclose(mh_sequences_old_fp);
1385                 }
1386                 
1387                 fflush(mh_sequences_new_fp);
1388 #if 0
1389                 fsync(fileno(mh_sequences_new_fp));
1390 #endif
1391                 fclose(mh_sequences_new_fp);
1392
1393                 g_rename(mh_sequences_new, mh_sequences_old);
1394                 g_free(sequence);
1395                 procmsg_msg_list_free(msglist);
1396         }
1397         g_free(mh_sequences_old);
1398         g_free(mh_sequences_new);
1399         g_free(path);
1400
1401         END_TIMING();
1402 }
1403
1404 static int mh_item_close(Folder *folder, FolderItem *item)
1405 {
1406         time_t last_mtime = (time_t)0;
1407         gboolean need_scan = mh_scan_required(item->folder, item);
1408         last_mtime = item->mtime;
1409
1410         mh_write_sequences(item, FALSE);
1411
1412         if (item->mtime == last_mtime && !need_scan) {
1413                 mh_set_mtime(item);
1414         }
1415
1416         return 0;
1417 }
1418
1419 void mh_set_mtime(FolderItem *item)
1420 {
1421         struct stat s;
1422         gchar *path = folder_item_get_path(item);
1423
1424         g_return_if_fail(path != NULL);
1425
1426         if (stat(path, &s) < 0) {
1427                 FILE_OP_ERROR(path, "stat");
1428                 g_free(path);
1429                 return;
1430         }
1431
1432         item->mtime = s.st_mtime;
1433         debug_print("MH: forced mtime of %s to %ld\n", item->name, item->mtime);
1434         g_free(path);
1435 }