3 exFAT file system checker.
5 Free exFAT implementation.
6 Copyright (C) 2011-2013 Andrew Nayenko
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.
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.
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.
30 #define exfat_debug(format, ...)
32 uint64_t files_count, directories_count;
34 static int nodeck(struct exfat* ef, struct exfat_node* node)
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;
43 if (CLUSTER_INVALID(c))
45 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
47 exfat_get_name(node, name, sizeof(name) - 1);
48 exfat_error("file `%s' has invalid cluster 0x%x", name, c);
52 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
54 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
56 exfat_get_name(node, name, sizeof(name) - 1);
57 exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
60 c = exfat_next_cluster(ef, node, c);
65 static void dirck(struct exfat* ef, const char* path)
67 struct exfat_node* parent;
68 struct exfat_node* node;
69 struct exfat_iterator it;
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)
81 path_length = strlen(path);
82 entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1);
83 if (entry_path == NULL)
85 exfat_error("out of memory");
88 strcpy(entry_path, path);
89 strcat(entry_path, "/");
91 rc = exfat_opendir(ef, parent, &it);
95 exfat_put_node(ef, parent);
98 while ((node = exfat_readdir(ef, &it)))
100 exfat_get_name(node, entry_path + path_length + 1,
101 UTF8_BYTES(EXFAT_NAME_MAX));
102 exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
103 IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
104 node->size, node->start_cluster);
105 if (node->flags & EXFAT_ATTRIB_DIR)
108 dirck(ef, entry_path);
115 exfat_put_node(ef, node);
117 exfat_closedir(ef, &it);
118 exfat_put_node(ef, parent);
122 static void fsck(struct exfat* ef)
124 exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
128 static void usage(const char* prog)
130 fprintf(stderr, "Usage: %s [-V] <device>\n", prog);
134 int main(int argc, char* argv[])
137 const char* spec = NULL;
140 printf("exfatfsck %u.%u.%u\n",
141 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
143 while ((opt = getopt(argc, argv, "V")) != -1)
148 puts("Copyright (C) 2011-2013 Andrew Nayenko");
155 if (argc - optind != 1)
159 if (exfat_mount(&ef, spec, "ro") != 0)
162 printf("Checking file system on %s.\n", spec);
165 printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
166 directories_count, files_count);
168 fputs("File system checking finished. ", stdout);
169 if (exfat_errors != 0)
171 printf("ERRORS FOUND: %d.\n", exfat_errors);
174 puts("No errors found.");