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