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 | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 5 | Free exFAT implementation. |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 6 | Copyright (C) 2011-2015 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 7 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 8 | This program is free software; you can redistribute it and/or modify |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 9 | it under the terms of the GNU General Public License as published by |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 10 | the Free Software Foundation, either version 2 of the License, or |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
bigbiff bigbiff | 61cdc02 | 2013-08-08 08:35:06 -0400 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License along |
| 19 | with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | */ |
| 22 | |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 23 | #include <exfat.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 26 | #include <inttypes.h> |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 27 | #include <unistd.h> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 28 | |
| 29 | #define exfat_debug(format, ...) |
| 30 | |
| 31 | uint64_t files_count, directories_count; |
| 32 | |
| 33 | static int nodeck(struct exfat* ef, struct exfat_node* node) |
| 34 | { |
| 35 | const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb); |
| 36 | cluster_t clusters = (node->size + cluster_size - 1) / cluster_size; |
| 37 | cluster_t c = node->start_cluster; |
| 38 | int rc = 0; |
| 39 | |
| 40 | while (clusters--) |
| 41 | { |
| 42 | if (CLUSTER_INVALID(c)) |
| 43 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 44 | char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 45 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 46 | exfat_get_name(node, name, sizeof(name) - 1); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 47 | exfat_error("file '%s' has invalid cluster 0x%x", name, c); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 48 | rc = 1; |
| 49 | break; |
| 50 | } |
| 51 | if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0) |
| 52 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 53 | char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 54 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 55 | exfat_get_name(node, name, sizeof(name) - 1); |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 56 | exfat_error("cluster 0x%x of file '%s' is not allocated", c, name); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 57 | rc = 1; |
| 58 | } |
| 59 | c = exfat_next_cluster(ef, node, c); |
| 60 | } |
| 61 | return rc; |
| 62 | } |
| 63 | |
| 64 | static void dirck(struct exfat* ef, const char* path) |
| 65 | { |
| 66 | struct exfat_node* parent; |
| 67 | struct exfat_node* node; |
| 68 | struct exfat_iterator it; |
| 69 | int rc; |
| 70 | size_t path_length; |
| 71 | char* entry_path; |
| 72 | |
| 73 | if (exfat_lookup(ef, &parent, path) != 0) |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 74 | exfat_bug("directory '%s' is not found", path); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 75 | if (!(parent->flags & EXFAT_ATTRIB_DIR)) |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 76 | exfat_bug("'%s' is not a directory (0x%x)", path, parent->flags); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 77 | if (nodeck(ef, parent) != 0) |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 78 | { |
| 79 | exfat_put_node(ef, parent); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 80 | return; |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 81 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 82 | |
| 83 | path_length = strlen(path); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 84 | entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 85 | if (entry_path == NULL) |
| 86 | { |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 87 | exfat_put_node(ef, parent); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 88 | exfat_error("out of memory"); |
| 89 | return; |
| 90 | } |
| 91 | strcpy(entry_path, path); |
| 92 | strcat(entry_path, "/"); |
| 93 | |
| 94 | rc = exfat_opendir(ef, parent, &it); |
| 95 | if (rc != 0) |
| 96 | { |
| 97 | free(entry_path); |
| 98 | exfat_put_node(ef, parent); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | while ((node = exfat_readdir(ef, &it))) |
| 102 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 103 | exfat_get_name(node, entry_path + path_length + 1, |
| 104 | UTF8_BYTES(EXFAT_NAME_MAX)); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 105 | exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path, |
| 106 | IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented", |
| 107 | node->size, node->start_cluster); |
| 108 | if (node->flags & EXFAT_ATTRIB_DIR) |
| 109 | { |
| 110 | directories_count++; |
| 111 | dirck(ef, entry_path); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | files_count++; |
| 116 | nodeck(ef, node); |
| 117 | } |
| 118 | exfat_put_node(ef, node); |
| 119 | } |
| 120 | exfat_closedir(ef, &it); |
| 121 | exfat_put_node(ef, parent); |
| 122 | free(entry_path); |
| 123 | } |
| 124 | |
| 125 | static void fsck(struct exfat* ef) |
| 126 | { |
| 127 | exfat_print_info(ef->sb, exfat_count_free_clusters(ef)); |
| 128 | dirck(ef, ""); |
| 129 | } |
| 130 | |
| 131 | static void usage(const char* prog) |
| 132 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 133 | fprintf(stderr, "Usage: %s [-V] <device>\n", prog); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 134 | exit(1); |
| 135 | } |
| 136 | |
| 137 | int main(int argc, char* argv[]) |
| 138 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 139 | int opt; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 140 | const char* spec = NULL; |
| 141 | struct exfat ef; |
| 142 | |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 143 | printf("exfatfsck %s\n", VERSION); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 144 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 145 | while ((opt = getopt(argc, argv, "V")) != -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 146 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 147 | switch (opt) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 148 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 149 | case 'V': |
Matt Mower | 09ef1e4 | 2015-12-13 11:29:45 -0600 | [diff] [blame^] | 150 | puts("Copyright (C) 2011-2015 Andrew Nayenko"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 151 | return 0; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 152 | default: |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 153 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 154 | break; |
| 155 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 156 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 157 | if (argc - optind != 1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 158 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 159 | spec = argv[optind]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 160 | |
| 161 | if (exfat_mount(&ef, spec, "ro") != 0) |
| 162 | return 1; |
| 163 | |
| 164 | printf("Checking file system on %s.\n", spec); |
| 165 | fsck(&ef); |
| 166 | exfat_unmount(&ef); |
| 167 | printf("Totally %"PRIu64" directories and %"PRIu64" files.\n", |
| 168 | directories_count, files_count); |
| 169 | |
| 170 | fputs("File system checking finished. ", stdout); |
| 171 | if (exfat_errors != 0) |
| 172 | { |
| 173 | printf("ERRORS FOUND: %d.\n", exfat_errors); |
| 174 | return 1; |
| 175 | } |
| 176 | puts("No errors found."); |
| 177 | return 0; |
| 178 | } |