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