3 exFAT file system checker.
5 Free exFAT implementation.
6 Copyright (C) 2011-2018 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.
29 #define exfat_debug(format, ...)
31 uint64_t files_count, directories_count;
33 static int nodeck(struct exfat* ef, struct exfat_node* node)
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;
42 if (CLUSTER_INVALID(*ef->sb, c))
44 char name[EXFAT_UTF8_NAME_BUFFER_MAX];
46 exfat_get_name(node, name);
47 exfat_error("file '%s' has invalid cluster 0x%x", name, c);
51 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
53 char name[EXFAT_UTF8_NAME_BUFFER_MAX];
55 exfat_get_name(node, name);
56 exfat_error("cluster 0x%x of file '%s' is not allocated", c, name);
59 c = exfat_next_cluster(ef, node, c);
64 static void dirck(struct exfat* ef, const char* path)
66 struct exfat_node* parent;
67 struct exfat_node* node;
68 struct exfat_iterator it;
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)
79 exfat_put_node(ef, parent);
83 path_length = strlen(path);
84 entry_path = malloc(path_length + 1 + EXFAT_UTF8_NAME_BUFFER_MAX);
85 if (entry_path == NULL)
87 exfat_put_node(ef, parent);
88 exfat_error("out of memory");
91 strcpy(entry_path, path);
92 strcat(entry_path, "/");
94 rc = exfat_opendir(ef, parent, &it);
98 exfat_put_node(ef, parent);
101 while ((node = exfat_readdir(&it)))
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)
110 dirck(ef, entry_path);
117 exfat_put_node(ef, node);
119 exfat_closedir(ef, &it);
120 exfat_put_node(ef, parent);
124 static void fsck(struct exfat* ef)
126 exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
130 static void usage(const char* prog)
132 fprintf(stderr, "Usage: %s [-V] <device>\n", prog);
136 int main(int argc, char* argv[])
139 const char* spec = NULL;
142 printf("exfatfsck %s\n", VERSION);
144 while ((opt = getopt(argc, argv, "V")) != -1)
149 puts("Copyright (C) 2011-2018 Andrew Nayenko");
156 if (argc - optind != 1)
160 if (exfat_mount(&ef, spec, "ro") != 0)
163 printf("Checking file system on %s.\n", spec);
166 printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
167 directories_count, files_count);
169 fputs("File system checking finished. ", stdout);
170 if (exfat_errors != 0)
172 printf("ERRORS FOUND: %d.\n", exfat_errors);
175 puts("No errors found.");