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