]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - fsck/main.c
New upstream version 1.3.0
[sven/exfat-utils.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-2018  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
29 #define exfat_debug(format, ...)
30
31 uint64_t files_count, directories_count;
32
33 static int nodeck(struct exfat* ef, struct exfat_node* node)
34 {
35         const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
36         cluster_t clusters = DIV_ROUND_UP(node->size, cluster_size);
37         cluster_t c = node->start_cluster;
38         int rc = 0;
39
40         while (clusters--)
41         {
42                 if (CLUSTER_INVALID(*ef->sb, c))
43                 {
44                         char name[EXFAT_UTF8_NAME_BUFFER_MAX];
45
46                         exfat_get_name(node, name);
47                         exfat_error("file '%s' has invalid cluster 0x%x", name, c);
48                         rc = 1;
49                         break;
50                 }
51                 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
52                 {
53                         char name[EXFAT_UTF8_NAME_BUFFER_MAX];
54
55                         exfat_get_name(node, name);
56                         exfat_error("cluster 0x%x of file '%s' is not allocated", c, name);
57                         rc = 1;
58                 }
59                 c = exfat_next_cluster(ef, node, c);
60         }
61         return rc;
62 }
63
64 static void dirck(struct exfat* ef, const char* path)
65 {
66         struct exfat_node* parent;
67         struct exfat_node* node;
68         struct exfat_iterator it;
69         int rc;
70         size_t path_length;
71         char* entry_path;
72
73         if (exfat_lookup(ef, &parent, path) != 0)
74                 exfat_bug("directory '%s' is not found", path);
75         if (!(parent->attrib & EXFAT_ATTRIB_DIR))
76                 exfat_bug("'%s' is not a directory (%#hx)", path, parent->attrib);
77         if (nodeck(ef, parent) != 0)
78         {
79                 exfat_put_node(ef, parent);
80                 return;
81         }
82
83         path_length = strlen(path);
84         entry_path = malloc(path_length + 1 + EXFAT_UTF8_NAME_BUFFER_MAX);
85         if (entry_path == NULL)
86         {
87                 exfat_put_node(ef, parent);
88                 exfat_error("out of memory");
89                 return;
90         }
91         strcpy(entry_path, path);
92         strcat(entry_path, "/");
93
94         rc = exfat_opendir(ef, parent, &it);
95         if (rc != 0)
96         {
97                 free(entry_path);
98                 exfat_put_node(ef, parent);
99                 return;
100         }
101         while ((node = exfat_readdir(&it)))
102         {
103                 exfat_get_name(node, entry_path + path_length + 1);
104                 exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
105                                 node->is_contiguous ? "contiguous" : "fragmented",
106                                 node->size, node->start_cluster);
107                 if (node->attrib & EXFAT_ATTRIB_DIR)
108                 {
109                         directories_count++;
110                         dirck(ef, entry_path);
111                 }
112                 else
113                 {
114                         files_count++;
115                         nodeck(ef, node);
116                 }
117                 exfat_flush_node(ef, node);
118                 exfat_put_node(ef, node);
119         }
120         exfat_closedir(ef, &it);
121         exfat_flush_node(ef, parent);
122         exfat_put_node(ef, parent);
123         free(entry_path);
124 }
125
126 static void fsck(struct exfat* ef, const char* spec, const char* options)
127 {
128         if (exfat_mount(ef, spec, options) != 0)
129         {
130                 fputs("File system checking stopped. ", stdout);
131                 return;
132         }
133
134         exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
135         dirck(ef, "");
136         exfat_unmount(ef);
137
138         printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
139                         directories_count, files_count);
140         fputs("File system checking finished. ", stdout);
141 }
142
143 static void usage(const char* prog)
144 {
145         fprintf(stderr, "Usage: %s [-a | -n | -p | -y] <device>\n", prog);
146         fprintf(stderr, "       %s -V\n", prog);
147         exit(1);
148 }
149
150 int main(int argc, char* argv[])
151 {
152         int opt;
153         const char* options;
154         const char* spec = NULL;
155         struct exfat ef;
156
157         printf("exfatfsck %s\n", VERSION);
158
159         if (isatty(STDIN_FILENO))
160                 options = "repair=1";
161         else
162                 options = "repair=0";
163
164         while ((opt = getopt(argc, argv, "anpVy")) != -1)
165         {
166                 switch (opt)
167                 {
168                 case 'a':
169                 case 'p':
170                 case 'y':
171                         options = "repair=2";
172                         break;
173                 case 'n':
174                         options = "repair=0,ro";
175                         break;
176                 case 'V':
177                         puts("Copyright (C) 2011-2018  Andrew Nayenko");
178                         return 0;
179                 default:
180                         usage(argv[0]);
181                         break;
182                 }
183         }
184         if (argc - optind != 1)
185                 usage(argv[0]);
186         spec = argv[optind];
187
188         printf("Checking file system on %s.\n", spec);
189         fsck(&ef, spec, options);
190         if (exfat_errors != 0)
191         {
192                 printf("ERRORS FOUND: %d, FIXED: %d.\n",
193                                 exfat_errors, exfat_errors_fixed);
194                 return 1;
195         }
196         puts("No errors found.");
197         return 0;
198 }