]> git.sven.stormbind.net Git - sven/exfat-utils.git/blob - fsck/main.c
Merge branch 'master' of ssh://git.sven.stormbind.net/home/sven/www/sven_htdocs/git...
[sven/exfat-utils.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 uint64_t files_count, directories_count;
30
31 static void nodeck(struct exfat* ef, struct exfat_node* node)
32 {
33         const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
34         cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
35         cluster_t c = node->start_cluster;
36         
37         while (clusters--)
38         {
39                 if (CLUSTER_INVALID(c))
40                 {
41                         char name[EXFAT_NAME_MAX + 1];
42
43                         exfat_get_name(node, name, EXFAT_NAME_MAX);
44                         exfat_error("file `%s' has invalid cluster", name);
45                         return;
46                 }
47                 if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
48                 {
49                         char name[EXFAT_NAME_MAX + 1];
50
51                         exfat_get_name(node, name, EXFAT_NAME_MAX);
52                         exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
53                 }
54                 c = exfat_next_cluster(ef, node, c);
55         }
56 }
57
58 static void dirck(struct exfat* ef, const char* path)
59 {
60         struct exfat_node* parent;
61         struct exfat_node* node;
62         struct exfat_iterator it;
63         int rc;
64         size_t path_length;
65         char* entry_path;
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         path_length = strlen(path);
73         entry_path = malloc(path_length + 1 + EXFAT_NAME_MAX);
74         if (entry_path == NULL)
75         {
76                 exfat_error("out of memory");
77                 return;
78         }
79         strcpy(entry_path, path);
80         strcat(entry_path, "/");
81
82         rc = exfat_opendir(ef, parent, &it);
83         if (rc != 0)
84         {
85                 free(entry_path);
86                 exfat_put_node(ef, parent);
87                 exfat_error("failed to open directory `%s'", path);
88                 return;
89         }
90         while ((node = exfat_readdir(ef, &it)))
91         {
92                 exfat_get_name(node, entry_path + path_length + 1, EXFAT_NAME_MAX);
93                 exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
94                                 IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
95                                 node->size, node->start_cluster);
96                 if (node->flags & EXFAT_ATTRIB_DIR)
97                 {
98                         directories_count++;
99                         dirck(ef, entry_path);
100                 }
101                 else
102                         files_count++;
103                 nodeck(ef, node);
104                 exfat_put_node(ef, node);
105         }
106         exfat_closedir(ef, &it);
107         exfat_put_node(ef, parent);
108         free(entry_path);
109 }
110
111 static void fsck(struct exfat* ef)
112 {
113         exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
114         dirck(ef, "");
115 }
116
117 static void usage(const char* prog)
118 {
119         fprintf(stderr, "Usage: %s [-v] <device>\n", prog);
120         exit(1);
121 }
122
123 int main(int argc, char* argv[])
124 {
125         char** pp;
126         const char* spec = NULL;
127         struct exfat ef;
128
129         printf("exfatfsck %u.%u.%u\n",
130                         EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
131
132         for (pp = argv + 1; *pp; pp++)
133         {
134                 if (strcmp(*pp, "-v") == 0)
135                 {
136                         puts("Copyright (C) 2009  Andrew Nayenko");
137                         return 0;
138                 }
139                 else if (spec == NULL)
140                         spec = *pp;
141                 else
142                         usage(argv[0]);
143         }
144         if (spec == NULL)
145                 usage(argv[0]);
146
147         if (exfat_mount(&ef, spec, "ro") != 0)
148                 return 1;
149
150         printf("Checking file system on %s.\n", spec);
151         fsck(&ef);
152         exfat_unmount(&ef);
153         printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
154                         directories_count, files_count);
155
156         fputs("File system checking finished. ", stdout);
157         if (exfat_errors != 0)
158         {
159                 printf("ERRORS FOUND: %d.\n", exfat_errors);
160                 return 1;
161         }
162         puts("No errors found.");
163         return 0;
164 }