fix CID 1596595: Resource leaks, and CID 1596594: (CHECKED_RETURN)
[claws.git] / src / file_checker.c
1 /*
2  * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3  * Copyright (C) 2013-2022 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 <gdk/gdkkeysyms.h>
30 #include <gtk/gtk.h>
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36
37 #include "file-utils.h"
38 #include "utils.h"
39 #include "alertpanel.h"
40 #include "folder.h"
41
42 static gboolean verify_folderlist_xml();
43
44 gboolean check_file_integrity()
45 {
46         if (verify_folderlist_xml() != TRUE)
47                 return FALSE;
48
49         return TRUE;
50 }
51
52 static gboolean verify_folderlist_xml()
53 {
54         GNode *node;
55         static gchar *filename = NULL;
56         static gchar *bak = NULL;
57         time_t date;
58         struct tm *ts;
59         gchar buf[BUFFSIZE];
60         gboolean fileexists, bakexists;
61
62         filename = folder_get_list_path();
63
64         fileexists = is_file_exist(filename);
65
66         bak = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
67                           FOLDER_LIST, ".bak", NULL);
68         bakexists = is_file_exist(bak);
69         
70         if (bakexists) {
71                 date = get_file_mtime(bak);
72                 ts = localtime(&date);
73                 strftime(buf, sizeof(buf), "%a %d-%b-%Y %H:%M %Z", ts);
74         }
75         
76         if (!fileexists && bakexists) {
77                 AlertValue aval;
78                 gchar *msg;
79
80                 msg = g_strdup_printf
81                         (_("The file %s is missing! "
82                            "Do you want to use the backup file from %s?"), FOLDER_LIST,buf);
83                 aval = alertpanel(_("Warning"), msg, NULL, _("_No"), NULL, _("_Yes"),
84                                   NULL, NULL, ALERTFOCUS_FIRST);
85                 g_free(msg);
86                 if (aval != G_ALERTALTERNATE)
87                         return FALSE;
88                 else {
89                         if (copy_file(bak,filename,FALSE) < 0) {
90                                 alertpanel_warning(_("Could not copy %s to %s"),bak,filename);
91                                 return FALSE;
92                         }
93                         g_free(bak);
94                         return TRUE;
95                 }
96         }
97
98         if (fileexists) {
99                 node = xml_parse_file(filename);
100                 if (!node && is_file_exist(bak)) {
101                         AlertValue aval;
102                         gchar *msg;
103
104                         msg = g_strdup_printf
105                                 (_("The file %s is empty or corrupted! "
106                                    "Do you want to use the backup file from %s?"), FOLDER_LIST,buf);
107                         aval = alertpanel(_("Warning"), msg, NULL, _("_No"), NULL, _("_Yes"),
108                                           NULL, NULL, ALERTFOCUS_FIRST);
109                         g_free(msg);
110                         if (aval != G_ALERTALTERNATE)
111                                 return FALSE;
112                         else {
113                                 if (copy_file(bak,filename,FALSE) < 0) {
114                                         alertpanel_warning(_("Could not copy %s to %s"),bak,filename);
115                                         return FALSE;
116                                 }
117                                 g_free(bak);
118                                 return TRUE;
119                         }
120                 }
121                 xml_free_tree(node);
122         }
123
124         return TRUE;
125 }