bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | main.c (02.09.09) |
| 3 | exFAT file system checker. |
| 4 | |
bigbiff bigbiff | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 5 | Copyright (C) 2011-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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> |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 26 | #include <unistd.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 43 | char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 44 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 45 | exfat_get_name(node, name, sizeof(name) - 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 52 | char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 53 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 54 | exfat_get_name(node, name, sizeof(name) - 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 80 | entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | exfat_error("failed to open directory `%s'", path); |
| 95 | return; |
| 96 | } |
| 97 | while ((node = exfat_readdir(ef, &it))) |
| 98 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 99 | exfat_get_name(node, entry_path + path_length + 1, |
| 100 | UTF8_BYTES(EXFAT_NAME_MAX)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 101 | exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path, |
| 102 | IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented", |
| 103 | node->size, node->start_cluster); |
| 104 | if (node->flags & EXFAT_ATTRIB_DIR) |
| 105 | { |
| 106 | directories_count++; |
| 107 | dirck(ef, entry_path); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | files_count++; |
| 112 | nodeck(ef, node); |
| 113 | } |
| 114 | exfat_put_node(ef, node); |
| 115 | } |
| 116 | exfat_closedir(ef, &it); |
| 117 | exfat_put_node(ef, parent); |
| 118 | free(entry_path); |
| 119 | } |
| 120 | |
| 121 | static void fsck(struct exfat* ef) |
| 122 | { |
| 123 | exfat_print_info(ef->sb, exfat_count_free_clusters(ef)); |
| 124 | dirck(ef, ""); |
| 125 | } |
| 126 | |
| 127 | static void usage(const char* prog) |
| 128 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 129 | fprintf(stderr, "Usage: %s [-V] <device>\n", prog); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 130 | exit(1); |
| 131 | } |
| 132 | |
| 133 | int main(int argc, char* argv[]) |
| 134 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 135 | int opt; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 136 | const char* spec = NULL; |
| 137 | struct exfat ef; |
| 138 | |
| 139 | printf("exfatfsck %u.%u.%u\n", |
| 140 | EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); |
| 141 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 142 | while ((opt = getopt(argc, argv, "V")) != -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 143 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 144 | switch (opt) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 145 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 146 | case 'V': |
bigbiff bigbiff | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 147 | puts("Copyright (C) 2011-2013 Andrew Nayenko"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 148 | return 0; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 149 | default: |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 150 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 151 | break; |
| 152 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 153 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 154 | if (argc - optind != 1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 155 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 156 | spec = argv[optind]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 157 | |
| 158 | if (exfat_mount(&ef, spec, "ro") != 0) |
| 159 | return 1; |
| 160 | |
| 161 | printf("Checking file system on %s.\n", spec); |
| 162 | fsck(&ef); |
| 163 | exfat_unmount(&ef); |
| 164 | printf("Totally %"PRIu64" directories and %"PRIu64" files.\n", |
| 165 | directories_count, files_count); |
| 166 | |
| 167 | fputs("File system checking finished. ", stdout); |
| 168 | if (exfat_errors != 0) |
| 169 | { |
| 170 | printf("ERRORS FOUND: %d.\n", exfat_errors); |
| 171 | return 1; |
| 172 | } |
| 173 | puts("No errors found."); |
| 174 | return 0; |
| 175 | } |