3 exFAT file system checker.
5 Copyright (C) 2009, 2010 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/>.
27 #define exfat_debug(format, ...)
29 #define MB (1024 * 1024)
31 #define BMAP_GET(bitmap, index) ((bitmap)[(index) / 8] & (1u << ((index) % 8)))
33 uint64_t files_count, directories_count;
35 static uint64_t bytes2mb(uint64_t bytes)
37 return (bytes + MB / 2) / MB;
40 static void sbck(const struct exfat* ef)
42 const uint64_t total = (uint64_t) le32_to_cpu(ef->sb->cluster_count) *
43 CLUSTER_SIZE(*ef->sb);
45 printf("Block size %8u bytes\n", BLOCK_SIZE(*ef->sb));
46 printf("Cluster size %8u bytes\n", CLUSTER_SIZE(*ef->sb));
47 printf("Total space %8"PRIu64" MB\n", bytes2mb(total));
48 printf("Used space %8hhu%%\n", ef->sb->allocated_percent);
51 static void nodeck(struct exfat* ef, struct exfat_node* node)
53 const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
54 cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
55 cluster_t c = node->start_cluster;
59 if (CLUSTER_INVALID(c))
61 char name[EXFAT_NAME_MAX + 1];
63 exfat_get_name(node, name, EXFAT_NAME_MAX);
64 exfat_error("file `%s' has invalid cluster", name);
67 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
69 char name[EXFAT_NAME_MAX + 1];
71 exfat_get_name(node, name, EXFAT_NAME_MAX);
72 exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
74 c = exfat_next_cluster(ef, node, c);
78 static void dirck(struct exfat* ef, const char* path)
80 struct exfat_node* parent;
81 struct exfat_node* node;
82 struct exfat_iterator it;
87 if (exfat_lookup(ef, &parent, path) != 0)
88 exfat_bug("directory `%s' is not found", path);
89 if (!(parent->flags & EXFAT_ATTRIB_DIR))
90 exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags);
92 path_length = strlen(path);
93 entry_path = malloc(path_length + 1 + EXFAT_NAME_MAX);
94 if (entry_path == NULL)
96 exfat_error("out of memory");
99 strcpy(entry_path, path);
100 strcat(entry_path, "/");
102 rc = exfat_opendir(ef, parent, &it);
105 exfat_put_node(ef, parent);
106 exfat_error("failed to open directory `%s'", path);
109 while ((node = exfat_readdir(ef, &it)))
111 exfat_get_name(node, entry_path + path_length + 1, EXFAT_NAME_MAX);
112 exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
113 IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
114 node->size, node->start_cluster);
115 if (node->flags & EXFAT_ATTRIB_DIR)
118 dirck(ef, entry_path);
123 exfat_put_node(ef, node);
125 exfat_closedir(ef, &it);
126 exfat_put_node(ef, parent);
130 static void fsck(struct exfat* ef)
136 int main(int argc, char* argv[])
142 fprintf(stderr, "usage: %s <spec>\n", argv[0]);
145 printf("exfatfsck %u.%u.%u\n",
146 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
148 if (exfat_mount(&ef, argv[1], "ro") != 0)
151 printf("Checking file system on %s.\n", argv[1]);
154 printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
155 directories_count, files_count);
157 fputs("File system checking finished. ", stdout);
158 if (exfat_errors != 0)
160 printf("ERRORS FOUND: %d.\n", exfat_errors);
163 puts("No errors found.");