OSDN Git Service

Handle 64-bit offsets correctly on Android
[android-x86/external-exfat.git] / fsck / main.c
1 /*
2         main.c (02.09.09)
3         exFAT file system checker.
4
5         Copyright (C) 2011-2013  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 <exfat.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <exfatfs.h>
25 #include <inttypes.h>
26 #include <unistd.h>
27
28 #define exfat_debug(format, ...)
29
30 uint64_t files_count, directories_count;
31
32 static int nodeck(struct exfat* ef, struct exfat_node* node)
33 {
34         const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
35         cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
36         cluster_t c = node->start_cluster;
37         int rc = 0;
38
39         while (clusters--)
40         {
41                 if (CLUSTER_INVALID(c))
42                 {
43                         char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
44
45                         exfat_get_name(node, name, sizeof(name) - 1);
46                         exfat_error("file `%s' has invalid cluster 0x%x", name, c);
47                         rc = 1;
48                         break;
49                 }
50                 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
51                 {
52                         char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
53
54                         exfat_get_name(node, name, sizeof(name) - 1);
55                         exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
56                         rc = 1;
57                 }
58                 c = exfat_next_cluster(ef, node, c);
59         }
60         return rc;
61 }
62
63 static void dirck(struct exfat* ef, const char* path)
64 {
65         struct exfat_node* parent;
66         struct exfat_node* node;
67         struct exfat_iterator it;
68         int rc;
69         size_t path_length;
70         char* entry_path;
71
72         if (exfat_lookup(ef, &parent, path) != 0)
73                 exfat_bug("directory `%s' is not found", path);
74         if (!(parent->flags & EXFAT_ATTRIB_DIR))
75                 exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags);
76         if (nodeck(ef, parent) != 0)
77                 return;
78
79         path_length = strlen(path);
80         entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1);
81         if (entry_path == NULL)
82         {
83                 exfat_error("out of memory");
84                 return;
85         }
86         strcpy(entry_path, path);
87         strcat(entry_path, "/");
88
89         rc = exfat_opendir(ef, parent, &it);
90         if (rc != 0)
91         {
92                 free(entry_path);
93                 exfat_put_node(ef, parent);
94                 return;
95         }
96         while ((node = exfat_readdir(ef, &it)))
97         {
98                 exfat_get_name(node, entry_path + path_length + 1,
99                                 UTF8_BYTES(EXFAT_NAME_MAX));
100                 exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
101                                 IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
102                                 node->size, node->start_cluster);
103                 if (node->flags & EXFAT_ATTRIB_DIR)
104                 {
105                         directories_count++;
106                         dirck(ef, entry_path);
107                 }
108                 else
109                 {
110                         files_count++;
111                         nodeck(ef, node);
112                 }
113                 exfat_put_node(ef, node);
114         }
115         exfat_closedir(ef, &it);
116         exfat_put_node(ef, parent);
117         free(entry_path);
118 }
119
120 static void fsck(struct exfat* ef)
121 {
122         exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
123         dirck(ef, "");
124 }
125
126 static void usage(const char* prog)
127 {
128         fprintf(stderr, "Usage: %s [-V] <device>\n", prog);
129         exit(1);
130 }
131
132 int main(int argc, char* argv[])
133 {
134         int opt;
135         const char* spec = NULL;
136         struct exfat ef;
137
138         printf("exfatfsck %u.%u.%u\n",
139                         EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
140
141         while ((opt = getopt(argc, argv, "V")) != -1)
142         {
143                 switch (opt)
144                 {
145                 case 'V':
146                         puts("Copyright (C) 2011-2013  Andrew Nayenko");
147                         return 0;
148                 default:
149                         usage(argv[0]);
150                         break;
151                 }
152         }
153         if (argc - optind != 1)
154                 usage(argv[0]);
155         spec = argv[optind];
156
157         if (exfat_mount(&ef, spec, "ro") != 0)
158                 return 1;
159
160         printf("Checking file system on %s.\n", spec);
161         fsck(&ef);
162         exfat_unmount(&ef);
163         printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
164                         directories_count, files_count);
165
166         fputs("File system checking finished. ", stdout);
167         if (exfat_errors != 0)
168         {
169                 printf("ERRORS FOUND: %d.\n", exfat_errors);
170                 return 1;
171         }
172         puts("No errors found.");
173         return 0;
174 }