blob: 91f75459427b5742a2b5f58bd8c23cf64d49e3bc [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 main.c (02.09.09)
3 exFAT file system checker.
4
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04005 Free exFAT implementation.
bigbiff bigbiffca829c42013-01-28 08:14:25 -05006 Copyright (C) 2011-2013 Andrew Nayenko
bigbiff bigbiff9c754052013-01-09 09:09:08 -05007
bigbiff bigbiff61cdc022013-08-08 08:35:06 -04008 This program is free software; you can redistribute it and/or modify
bigbiff bigbiff9c754052013-01-09 09:09:08 -05009 it under the terms of the GNU General Public License as published by
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040010 the Free Software Foundation, either version 2 of the License, or
bigbiff bigbiff9c754052013-01-09 09:09:08 -050011 (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
bigbiff bigbiff61cdc022013-08-08 08:35:06 -040018 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.
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021*/
22
23#include <stdio.h>
24#include <string.h>
25#include <exfat.h>
26#include <exfatfs.h>
27#include <inttypes.h>
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040028#include <unistd.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050029
30#define exfat_debug(format, ...)
31
32uint64_t files_count, directories_count;
33
34static int nodeck(struct exfat* ef, struct exfat_node* node)
35{
36 const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
37 cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
38 cluster_t c = node->start_cluster;
39 int rc = 0;
40
41 while (clusters--)
42 {
43 if (CLUSTER_INVALID(c))
44 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040045 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
bigbiff bigbiff9c754052013-01-09 09:09:08 -050046
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040047 exfat_get_name(node, name, sizeof(name) - 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050048 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 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040054 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
bigbiff bigbiff9c754052013-01-09 09:09:08 -050055
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040056 exfat_get_name(node, name, sizeof(name) - 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050057 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
65static 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->flags & EXFAT_ATTRIB_DIR))
77 exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags);
78 if (nodeck(ef, parent) != 0)
79 return;
80
81 path_length = strlen(path);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -040082 entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1);
bigbiff bigbiff9c754052013-01-09 09:09:08 -050083 if (entry_path == NULL)
84 {
85 exfat_error("out of memory");
86 return;
87 }
88 strcpy(entry_path, path);
89 strcat(entry_path, "/");
90
91 rc = exfat_opendir(ef, parent, &it);
92 if (rc != 0)
93 {
94 free(entry_path);
95 exfat_put_node(ef, parent);
96 exfat_error("failed to open directory `%s'", path);
97 return;
98 }
99 while ((node = exfat_readdir(ef, &it)))
100 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400101 exfat_get_name(node, entry_path + path_length + 1,
102 UTF8_BYTES(EXFAT_NAME_MAX));
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500103 exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
104 IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
105 node->size, node->start_cluster);
106 if (node->flags & EXFAT_ATTRIB_DIR)
107 {
108 directories_count++;
109 dirck(ef, entry_path);
110 }
111 else
112 {
113 files_count++;
114 nodeck(ef, node);
115 }
116 exfat_put_node(ef, node);
117 }
118 exfat_closedir(ef, &it);
119 exfat_put_node(ef, parent);
120 free(entry_path);
121}
122
123static void fsck(struct exfat* ef)
124{
125 exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
126 dirck(ef, "");
127}
128
129static void usage(const char* prog)
130{
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400131 fprintf(stderr, "Usage: %s [-V] <device>\n", prog);
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500132 exit(1);
133}
134
135int main(int argc, char* argv[])
136{
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400137 int opt;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500138 const char* spec = NULL;
139 struct exfat ef;
140
141 printf("exfatfsck %u.%u.%u\n",
142 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
143
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400144 while ((opt = getopt(argc, argv, "V")) != -1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500145 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400146 switch (opt)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500147 {
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400148 case 'V':
bigbiff bigbiffca829c42013-01-28 08:14:25 -0500149 puts("Copyright (C) 2011-2013 Andrew Nayenko");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500150 return 0;
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400151 default:
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500152 usage(argv[0]);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400153 break;
154 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500155 }
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400156 if (argc - optind != 1)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500157 usage(argv[0]);
bigbiff bigbiff004e2df2013-07-03 14:52:12 -0400158 spec = argv[optind];
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500159
160 if (exfat_mount(&ef, spec, "ro") != 0)
161 return 1;
162
163 printf("Checking file system on %s.\n", spec);
164 fsck(&ef);
165 exfat_unmount(&ef);
166 printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
167 directories_count, files_count);
168
169 fputs("File system checking finished. ", stdout);
170 if (exfat_errors != 0)
171 {
172 printf("ERRORS FOUND: %d.\n", exfat_errors);
173 return 1;
174 }
175 puts("No errors found.");
176 return 0;
177}