3 * exFAT file system checker.
5 * Created by Andrew Nayenko on 02.09.09.
6 * This software is distributed under the GNU General Public License
7 * version 3 or any later.
16 #define exfat_debug(format, ...)
18 #define MB (1024 * 1024)
20 uint64_t files_count, directories_count;
22 static uint64_t bytes2mb(uint64_t bytes)
24 return (bytes + MB / 2) / MB;
27 static void sbck(const struct exfat* ef)
29 const uint32_t block_size = (1 << ef->sb->block_bits); /* in bytes */
30 const uint32_t cluster_size = CLUSTER_SIZE(*ef->sb); /* in bytes */
31 const uint64_t total = le32_to_cpu(ef->sb->cluster_count) * cluster_size;
33 #if 0 /* low-level info */
34 printf("First block %8"PRIu64"\n",
35 le64_to_cpu(ef->sb->block_start));
36 printf("Blocks count %8"PRIu64"\n",
37 le64_to_cpu(ef->sb->block_count));
38 printf("FAT first block %8u\n",
39 le32_to_cpu(ef->sb->fat_block_start));
40 printf("FAT blocks count %8u\n",
41 le32_to_cpu(ef->sb->fat_block_count));
42 printf("First cluster block %8u\n",
43 le32_to_cpu(ef->sb->cluster_block_start));
44 printf("Clusters count %8u\n",
45 le32_to_cpu(ef->sb->cluster_count));
46 printf("First cluster of root %8u\n",
47 le32_to_cpu(ef->sb->rootdir_cluster));
49 printf("Block size %8u bytes\n", block_size);
50 printf("Cluster size %8u bytes\n", cluster_size);
51 printf("Total space %8"PRIu64" MB\n", bytes2mb(total));
52 printf("Used space %8"PRIu64" MB (%hhu%%)\n",
53 bytes2mb(total * ef->sb->allocated_percent / 100),
54 ef->sb->allocated_percent);
55 printf("Free space %8"PRIu64" MB (%hhu%%)\n",
56 bytes2mb(total * (100 - ef->sb->allocated_percent) / 100),
57 100 - ef->sb->allocated_percent);
60 static void dirck(struct exfat* ef, const char* path)
62 struct exfat_node* parent;
63 struct exfat_node* node;
64 struct exfat_iterator it;
65 char subpath[EXFAT_NAME_MAX + 1];
67 if (exfat_lookup(ef, &parent, path) != 0)
68 exfat_bug("directory `%s' is not found", path);
69 if (!(parent->flags & EXFAT_ATTRIB_DIR))
70 exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags);
72 exfat_opendir(parent, &it);
73 while (exfat_readdir(ef, parent, &node, &it) == 0)
75 strcpy(subpath, path);
77 exfat_get_name(node, subpath + strlen(subpath),
78 EXFAT_NAME_MAX - strlen(subpath));
79 exfat_debug("%s: %s, %llu bytes, cluster %u", subpath,
80 IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
81 node->size, node->start_cluster);
82 if (node->flags & EXFAT_ATTRIB_DIR)
92 exfat_put_node(parent);
95 static void fsck(struct exfat* ef)
101 int main(int argc, char* argv[])
107 fprintf(stderr, "usage: %s <spec>\n", argv[0]);
110 printf("exfatck %u.%u\n",
111 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
113 if (exfat_mount(&ef, argv[1]) != 0)
116 printf("Checking file system on %s.\n", argv[1]);
119 printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
120 directories_count, files_count);
122 fputs("File system checking finished: ", stdout);
123 if (exfat_errors == 0)
126 printf("%d ERROR%s FOUND!!!\n", exfat_errors,
127 exfat_errors > 1 ? "S WERE" : " WAS");