OSDN Git Service

Fixed debug output in fsck.
[android-x86/external-exfat.git] / fsck / main.c
1 /*
2  *  main.c
3  *  exFAT file system checker.
4  *
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.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <exfat.h>
13 #include <exfatfs.h>
14 #include <inttypes.h>
15
16 #define exfat_debug(format, ...)
17
18 #define MB (1024 * 1024)
19
20 uint64_t files_count, directories_count;
21
22 static uint64_t bytes2mb(uint64_t bytes)
23 {
24         return (bytes + MB / 2) / MB;
25 }
26
27 static void sbck(const struct exfat* ef)
28 {
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;
32
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));
48 #endif
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);
58 }
59
60 static void dirck(struct exfat* ef, const char* path)
61 {
62         struct exfat_node* parent;
63         struct exfat_node* node;
64         struct exfat_iterator it;
65         char subpath[EXFAT_NAME_MAX + 1];
66
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);
71
72         exfat_opendir(parent, &it);
73         while (exfat_readdir(ef, parent, &node, &it) == 0)
74         {
75                 strcpy(subpath, path);
76                 strcat(subpath, "/");
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)
83                 {
84                         directories_count++;
85                         dirck(ef, subpath);
86                 }
87                 else
88                         files_count++;
89                 exfat_put_node(node);
90         }
91         exfat_closedir(&it);
92         exfat_put_node(parent);
93 }
94
95 static void fsck(struct exfat* ef)
96 {
97         sbck(ef);
98         dirck(ef, "");
99 }
100
101 int main(int argc, char* argv[])
102 {
103         struct exfat ef;
104
105         if (argc != 2)
106         {
107                 fprintf(stderr, "usage: %s <spec>\n", argv[0]);
108                 return 1;
109         }
110         printf("exfatck %u.%u\n",
111                         EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
112
113         if (exfat_mount(&ef, argv[1]) != 0)
114                 return 1;
115
116         printf("Checking file system on %s.\n", argv[1]);
117         fsck(&ef);
118         exfat_unmount(&ef);
119         printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
120                         directories_count, files_count);
121
122         fputs("File system checking finished: ", stdout);
123         if (exfat_errors == 0)
124                 puts("seems OK.");
125         else
126                 printf("%d ERROR%s FOUND!!!\n", exfat_errors,
127                                 exfat_errors > 1 ? "S WERE" : " WAS");
128         return 0;
129 }