3 exFAT file system checker.
5 Copyright (C) 2011-2013 Andrew Nayenko
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.
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.
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/>.
28 #define exfat_debug(format, ...)
30 uint64_t files_count, directories_count;
32 static int nodeck(struct exfat* ef, struct exfat_node* node)
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;
41 if (CLUSTER_INVALID(c))
43 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
45 exfat_get_name(node, name, sizeof(name) - 1);
46 exfat_error("file `%s' has invalid cluster 0x%x", name, c);
50 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
52 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
54 exfat_get_name(node, name, sizeof(name) - 1);
55 exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
58 c = exfat_next_cluster(ef, node, c);
63 static void dirck(struct exfat* ef, const char* path)
65 struct exfat_node* parent;
66 struct exfat_node* node;
67 struct exfat_iterator it;
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)
79 path_length = strlen(path);
80 entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1);
81 if (entry_path == NULL)
83 exfat_error("out of memory");
86 strcpy(entry_path, path);
87 strcat(entry_path, "/");
89 rc = exfat_opendir(ef, parent, &it);
93 exfat_put_node(ef, parent);
94 exfat_error("failed to open directory `%s'", path);
97 while ((node = exfat_readdir(ef, &it)))
99 exfat_get_name(node, entry_path + path_length + 1,
100 UTF8_BYTES(EXFAT_NAME_MAX));
101 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)
107 dirck(ef, entry_path);
114 exfat_put_node(ef, node);
116 exfat_closedir(ef, &it);
117 exfat_put_node(ef, parent);
121 static void fsck(struct exfat* ef)
123 exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
127 static void usage(const char* prog)
129 fprintf(stderr, "Usage: %s [-V] <device>\n", prog);
133 int main(int argc, char* argv[])
136 const char* spec = NULL;
139 printf("exfatfsck %u.%u.%u\n",
140 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
142 while ((opt = getopt(argc, argv, "V")) != -1)
147 puts("Copyright (C) 2011-2013 Andrew Nayenko");
154 if (argc - optind != 1)
158 if (exfat_mount(&ef, spec, "ro") != 0)
161 printf("Checking file system on %s.\n", spec);
164 printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
165 directories_count, files_count);
167 fputs("File system checking finished. ", stdout);
168 if (exfat_errors != 0)
170 printf("ERRORS FOUND: %d.\n", exfat_errors);
173 puts("No errors found.");