blob: e865641c268ba4070b1411d9ac7434add04c82c5 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 main.c (02.09.09)
3 exFAT file system checker.
4
bigbiff bigbiffca829c42013-01-28 08:14:25 -05005 Copyright (C) 2011-2013 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05006
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <stdio.h>
22#include <string.h>
23#include <exfat.h>
24#include <exfatfs.h>
25#include <inttypes.h>
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040026#include <unistd.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050027
28#define exfat_debug(format, ...)
29
30uint64_t files_count, directories_count;
31
32static int nodeck(struct exfat* ef, struct exfat_node* node)
33{
34 const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
35 cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
36 cluster_t c = node->start_cluster;
37 int rc = 0;
38
39 while (clusters--)
40 {
41 if (CLUSTER_INVALID(c))
42 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040043 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
bigbiff bigbiff9c754052013-01-09 09:09:08 -050044
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040045 exfat_get_name(node, name, sizeof(name) - 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050046 exfat_error("file `%s' has invalid cluster 0x%x", name, c);
47 rc = 1;
48 break;
49 }
50 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
51 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040052 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
bigbiff bigbiff9c754052013-01-09 09:09:08 -050053
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040054 exfat_get_name(node, name, sizeof(name) - 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050055 exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
56 rc = 1;
57 }
58 c = exfat_next_cluster(ef, node, c);
59 }
60 return rc;
61}
62
63static void dirck(struct exfat* ef, const char* path)
64{
65 struct exfat_node* parent;
66 struct exfat_node* node;
67 struct exfat_iterator it;
68 int rc;
69 size_t path_length;
70 char* entry_path;
71
72 if (exfat_lookup(ef, &parent, path) != 0)
73 exfat_bug("directory `%s' is not found", path);
74 if (!(parent->flags & EXFAT_ATTRIB_DIR))
75 exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags);
76 if (nodeck(ef, parent) != 0)
77 return;
78
79 path_length = strlen(path);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040080 entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050081 if (entry_path == NULL)
82 {
83 exfat_error("out of memory");
84 return;
85 }
86 strcpy(entry_path, path);
87 strcat(entry_path, "/");
88
89 rc = exfat_opendir(ef, parent, &it);
90 if (rc != 0)
91 {
92 free(entry_path);
93 exfat_put_node(ef, parent);
94 exfat_error("failed to open directory `%s'", path);
95 return;
96 }
97 while ((node = exfat_readdir(ef, &it)))
98 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040099 exfat_get_name(node, entry_path + path_length + 1,
100 UTF8_BYTES(EXFAT_NAME_MAX));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500101 exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
102 IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
103 node->size, node->start_cluster);
104 if (node->flags & EXFAT_ATTRIB_DIR)
105 {
106 directories_count++;
107 dirck(ef, entry_path);
108 }
109 else
110 {
111 files_count++;
112 nodeck(ef, node);
113 }
114 exfat_put_node(ef, node);
115 }
116 exfat_closedir(ef, &it);
117 exfat_put_node(ef, parent);
118 free(entry_path);
119}
120
121static void fsck(struct exfat* ef)
122{
123 exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
124 dirck(ef, "");
125}
126
127static void usage(const char* prog)
128{
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400129 fprintf(stderr, "Usage: %s [-V] <device>\n", prog);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500130 exit(1);
131}
132
133int main(int argc, char* argv[])
134{
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400135 int opt;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500136 const char* spec = NULL;
137 struct exfat ef;
138
139 printf("exfatfsck %u.%u.%u\n",
140 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
141
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400142 while ((opt = getopt(argc, argv, "V")) != -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500143 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400144 switch (opt)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500145 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400146 case 'V':
bigbiff bigbiffca829c42013-01-28 08:14:25 -0500147 puts("Copyright (C) 2011-2013 Andrew Nayenko");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500148 return 0;
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400149 default:
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500150 usage(argv[0]);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400151 break;
152 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500153 }
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400154 if (argc - optind != 1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500155 usage(argv[0]);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400156 spec = argv[optind];
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500157
158 if (exfat_mount(&ef, spec, "ro") != 0)
159 return 1;
160
161 printf("Checking file system on %s.\n", spec);
162 fsck(&ef);
163 exfat_unmount(&ef);
164 printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
165 directories_count, files_count);
166
167 fputs("File system checking finished. ", stdout);
168 if (exfat_errors != 0)
169 {
170 printf("ERRORS FOUND: %d.\n", exfat_errors);
171 return 1;
172 }
173 puts("No errors found.");
174 return 0;
175}