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