]> git.sven.stormbind.net Git - sven/fuse-exfat.git/blob - fsck/main.c
Build-Depend on libfuse3-dev. Supported upstream starting with 1.4.0.
[sven/fuse-exfat.git] / fsck / main.c
1 /*
2         main.c (02.09.09)
3         exFAT file system checker.
4
5         Free exFAT implementation.
6         Copyright (C) 2011-2023  Andrew Nayenko
7
8         This program is free software; you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License along
19         with this program; if not, write to the Free Software Foundation, Inc.,
20         51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <exfat.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #define exfat_debug(format, ...) do {} while (0)
31
32 uint64_t files_count, directories_count;
33
34 static int nodeck(struct exfat* ef, struct exfat_node* node)
35 {
36         const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
37         cluster_t clusters = DIV_ROUND_UP(node->size, cluster_size);
38         cluster_t c = node->start_cluster;
39         int rc = 0;
40
41         while (clusters--)
42         {
43                 if (CLUSTER_INVALID(*ef->sb, c))
44                 {
45                         char name[EXFAT_UTF8_NAME_BUFFER_MAX];
46
47                         exfat_get_name(node, name);
48                         exfat_error("file '%s' has invalid cluster 0x%x", name, c);
49                         rc = 1;
50                         break;
51                 }
52                 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
53                 {
54                         char name[EXFAT_UTF8_NAME_BUFFER_MAX];
55
56                         exfat_get_name(node, name);
57                         exfat_error("cluster 0x%x of file '%s' is not allocated", c, name);
58                         rc = 1;
59                 }
60                 c = exfat_next_cluster(ef, node, c);
61         }
62         return rc;
63 }
64
65 static void dirck(struct exfat* ef, const char* path)
66 {
67         struct exfat_node* parent;
68         struct exfat_node* node;
69         struct exfat_iterator it;
70         int rc;
71         size_t path_length;
72         char* entry_path;
73
74         if (exfat_lookup(ef, &parent, path) != 0)
75                 exfat_bug("directory '%s' is not found", path);
76         if (!(parent->attrib & EXFAT_ATTRIB_DIR))
77                 exfat_bug("'%s' is not a directory (%#hx)", path, parent->attrib);
78         if (nodeck(ef, parent) != 0)
79         {
80                 exfat_put_node(ef, parent);
81                 return;
82         }
83
84         path_length = strlen(path);
85         entry_path = malloc(path_length + 1 + EXFAT_UTF8_NAME_BUFFER_MAX);
86         if (entry_path == NULL)
87         {
88                 exfat_put_node(ef, parent);
89                 exfat_error("out of memory");
90                 return;
91         }
92         strcpy(entry_path, path);
93         strcat(entry_path, "/");
94
95         rc = exfat_opendir(ef, parent, &it);
96         if (rc != 0)
97         {
98                 free(entry_path);
99                 exfat_put_node(ef, parent);
100                 return;
101         }
102         while ((node = exfat_readdir(&it)))
103         {
104                 exfat_get_name(node, entry_path + path_length + 1);
105                 exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
106                                 node->is_contiguous ? "contiguous" : "fragmented",
107                                 node->size, node->start_cluster);
108                 if (node->attrib & EXFAT_ATTRIB_DIR)
109                 {
110                         directories_count++;
111                         dirck(ef, entry_path);
112                 }
113                 else
114                 {
115                         files_count++;
116                         nodeck(ef, node);
117                 }
118                 exfat_flush_node(ef, node);
119                 exfat_put_node(ef, node);
120         }
121         exfat_closedir(ef, &it);
122         exfat_flush_node(ef, parent);
123         exfat_put_node(ef, parent);
124         free(entry_path);
125 }
126
127 static bool fsck(struct exfat* ef, const char* spec, const char* options)
128 {
129         int rc;
130
131         rc = exfat_mount(ef, spec, options);
132         if (rc == -ENODEV)
133                 return false; /* failed to open the device, checking haven't started */
134         if (rc != 0)
135         {
136                 fputs("File system checking stopped. ", stdout);
137                 return true;
138         }
139
140         exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
141         exfat_soil_super_block(ef);
142         dirck(ef, "");
143         exfat_unmount(ef);
144
145         printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
146                         directories_count, files_count);
147         fputs("File system checking finished. ", stdout);
148         return true;
149 }
150
151 static void usage(const char* prog)
152 {
153         fprintf(stderr, "Usage: %s [-a | -n | -p | -y] <device>\n", prog);
154         fprintf(stderr, "       %s -V\n", prog);
155         exit(1);
156 }
157
158 int main(int argc, char* argv[])
159 {
160         int opt;
161         const char* options;
162         const char* spec = NULL;
163         struct exfat ef;
164
165         printf("exfatfsck %s\n", VERSION);
166
167         if (isatty(STDIN_FILENO))
168                 options = "repair=1";
169         else
170                 options = "repair=0";
171
172         while ((opt = getopt(argc, argv, "anpVy")) != -1)
173         {
174                 switch (opt)
175                 {
176                 case 'a':
177                 case 'p':
178                 case 'y':
179                         options = "repair=2";
180                         break;
181                 case 'n':
182                         options = "repair=0,ro";
183                         break;
184                 case 'V':
185                         puts("Copyright (C) 2011-2023  Andrew Nayenko");
186                         return 0;
187                 default:
188                         usage(argv[0]);
189                         break;
190                 }
191         }
192         if (argc - optind != 1)
193                 usage(argv[0]);
194         spec = argv[optind];
195
196         printf("Checking file system on %s.\n", spec);
197         if (!fsck(&ef, spec, options))
198                 return 1;
199         if (exfat_errors != 0)
200         {
201                 printf("ERRORS FOUND: %d, FIXED: %d.\n",
202                                 exfat_errors, exfat_errors_fixed);
203                 return 1;
204         }
205         puts("No errors found.");
206         return 0;
207 }