OSDN Git Service

Fix fsck: now it can handle paths or any length.
[android-x86/external-exfat.git] / fsck / main.c
1 /*
2         main.c (02.09.09)
3         exFAT file system checker.
4
5         Copyright (C) 2009, 2010  Andrew Nayenko
6
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.
11
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.
16
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/>.
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <exfat.h>
24 #include <exfatfs.h>
25 #include <inttypes.h>
26
27 #define exfat_debug(format, ...)
28
29 #define MB (1024 * 1024)
30
31 #define BMAP_GET(bitmap, index) ((bitmap)[(index) / 8] & (1u << ((index) % 8)))
32
33 uint64_t files_count, directories_count;
34
35 static uint64_t bytes2mb(uint64_t bytes)
36 {
37         return (bytes + MB / 2) / MB;
38 }
39
40 static void sbck(const struct exfat* ef)
41 {
42         const uint32_t block_size = (1 << ef->sb->block_bits); /* in bytes */
43         const uint32_t cluster_size = CLUSTER_SIZE(*ef->sb); /* in bytes */
44         const uint64_t total = (uint64_t) le32_to_cpu(ef->sb->cluster_count) *
45                 cluster_size;
46
47 #if 0 /* low-level info */
48         printf("First block           %8"PRIu64"\n",
49                         le64_to_cpu(ef->sb->block_start));
50         printf("Blocks count          %8"PRIu64"\n",
51                         le64_to_cpu(ef->sb->block_count));
52         printf("FAT first block       %8u\n",
53                         le32_to_cpu(ef->sb->fat_block_start));
54         printf("FAT blocks count      %8u\n",
55                         le32_to_cpu(ef->sb->fat_block_count));
56         printf("First cluster block   %8u\n",
57                         le32_to_cpu(ef->sb->cluster_block_start));
58         printf("Clusters count        %8u\n",
59                         le32_to_cpu(ef->sb->cluster_count));
60         printf("First cluster of root %8u\n",
61                         le32_to_cpu(ef->sb->rootdir_cluster));
62 #endif
63         printf("Block size            %8u bytes\n", block_size);
64         printf("Cluster size          %8u bytes\n", cluster_size);
65         printf("Total space           %8"PRIu64" MB\n", bytes2mb(total));
66         printf("Used space            %8hhu%%\n", ef->sb->allocated_percent);
67 }
68
69 static void nodeck(struct exfat* ef, struct exfat_node* node)
70 {
71         const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
72         cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
73         cluster_t c = node->start_cluster;
74         
75         while (clusters--)
76         {
77                 if (CLUSTER_INVALID(c))
78                 {
79                         char name[EXFAT_NAME_MAX + 1];
80
81                         exfat_get_name(node, name, EXFAT_NAME_MAX);
82                         exfat_error("file `%s' has invalid cluster", name);
83                         return;
84                 }
85                 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
86                 {
87                         char name[EXFAT_NAME_MAX + 1];
88
89                         exfat_get_name(node, name, EXFAT_NAME_MAX);
90                         exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
91                 }
92                 c = exfat_next_cluster(ef, node, c);
93         }
94 }
95
96 static void dirck(struct exfat* ef, const char* path)
97 {
98         struct exfat_node* parent;
99         struct exfat_node* node;
100         struct exfat_iterator it;
101         int rc;
102         size_t path_length;
103         char* entry_path;
104
105         if (exfat_lookup(ef, &parent, path) != 0)
106                 exfat_bug("directory `%s' is not found", path);
107         if (!(parent->flags & EXFAT_ATTRIB_DIR))
108                 exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags);
109
110         path_length = strlen(path);
111         entry_path = malloc(path_length + 1 + EXFAT_NAME_MAX);
112         if (entry_path == NULL)
113         {
114                 exfat_error("out of memory");
115                 return;
116         }
117         strcpy(entry_path, path);
118         strcat(entry_path, "/");
119
120         rc = exfat_opendir(ef, parent, &it);
121         if (rc != 0)
122         {
123                 exfat_put_node(ef, parent);
124                 exfat_error("failed to open directory `%s'", path);
125                 return;
126         }
127         while ((node = exfat_readdir(ef, &it)))
128         {
129                 exfat_get_name(node, entry_path + path_length + 1, EXFAT_NAME_MAX);
130                 exfat_debug("%s: %s, %llu bytes, cluster %u", subpath,
131                                 IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
132                                 node->size, node->start_cluster);
133                 if (node->flags & EXFAT_ATTRIB_DIR)
134                 {
135                         directories_count++;
136                         dirck(ef, entry_path);
137                 }
138                 else
139                         files_count++;
140                 nodeck(ef, node);
141                 exfat_put_node(ef, node);
142         }
143         exfat_closedir(ef, &it);
144         exfat_put_node(ef, parent);
145         free(entry_path);
146 }
147
148 static void fsck(struct exfat* ef)
149 {
150         sbck(ef);
151         dirck(ef, "");
152 }
153
154 int main(int argc, char* argv[])
155 {
156         struct exfat ef;
157
158         if (argc != 2)
159         {
160                 fprintf(stderr, "usage: %s <spec>\n", argv[0]);
161                 return 1;
162         }
163         printf("exfatck %u.%u.%u\n",
164                         EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
165
166         if (exfat_mount(&ef, argv[1], "") != 0)
167                 return 1;
168
169         printf("Checking file system on %s.\n", argv[1]);
170         fsck(&ef);
171         exfat_unmount(&ef);
172         printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
173                         directories_count, files_count);
174
175         fputs("File system checking finished. ", stdout);
176         if (exfat_errors != 0)
177         {
178                 printf("ERRORS FOUND: %d.\n", exfat_errors);
179                 return 1;
180         }
181         puts("No errors found.");
182         return 0;
183 }