X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=fsck%2Fmain.c;h=c90e3f94b5b1b4ec0cecbb7bc7af73e446056889;hb=98418b03baf43ab1b4392578c03d4cbf7f0f2522;hp=fa266318e46c8723272dde90ceb75988e8316ffd;hpb=946abe4a43999ae0dfd9cbb4ad3d392263903663;p=android-x86%2Fexternal-exfat.git diff --git a/fsck/main.c b/fsck/main.c index fa26631..c90e3f9 100644 --- a/fsck/main.c +++ b/fsck/main.c @@ -1,11 +1,22 @@ /* - * main.c - * exFAT file system checker. - * - * Created by Andrew Nayenko on 02.09.09. - * This software is distributed under the GNU General Public License - * version 3 or any later. - */ + main.c (02.09.09) + exFAT file system checker. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ #include #include @@ -15,52 +26,15 @@ #define exfat_debug(format, ...) -#define MB (1024 * 1024) - -#define BMAP_GET(bitmap, index) ((bitmap)[(index) / 8] & (1u << ((index) % 8))) - uint64_t files_count, directories_count; -static uint64_t bytes2mb(uint64_t bytes) -{ - return (bytes + MB / 2) / MB; -} - -static void sbck(const struct exfat* ef) -{ - const uint32_t block_size = (1 << ef->sb->block_bits); /* in bytes */ - const uint32_t cluster_size = CLUSTER_SIZE(*ef->sb); /* in bytes */ - const uint64_t total = (uint64_t) le32_to_cpu(ef->sb->cluster_count) * - cluster_size; - -#if 0 /* low-level info */ - printf("First block %8"PRIu64"\n", - le64_to_cpu(ef->sb->block_start)); - printf("Blocks count %8"PRIu64"\n", - le64_to_cpu(ef->sb->block_count)); - printf("FAT first block %8u\n", - le32_to_cpu(ef->sb->fat_block_start)); - printf("FAT blocks count %8u\n", - le32_to_cpu(ef->sb->fat_block_count)); - printf("First cluster block %8u\n", - le32_to_cpu(ef->sb->cluster_block_start)); - printf("Clusters count %8u\n", - le32_to_cpu(ef->sb->cluster_count)); - printf("First cluster of root %8u\n", - le32_to_cpu(ef->sb->rootdir_cluster)); -#endif - printf("Block size %8u bytes\n", block_size); - printf("Cluster size %8u bytes\n", cluster_size); - printf("Total space %8"PRIu64" MB\n", bytes2mb(total)); - printf("Used space %8hhu%%\n", ef->sb->allocated_percent); -} - -static void nodeck(struct exfat* ef, struct exfat_node* node) +static int nodeck(struct exfat* ef, struct exfat_node* node) { const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb); cluster_t clusters = (node->size + cluster_size - 1) / cluster_size; cluster_t c = node->start_cluster; - + int rc = 0; + while (clusters--) { if (CLUSTER_INVALID(c)) @@ -68,19 +42,21 @@ static void nodeck(struct exfat* ef, struct exfat_node* node) char name[EXFAT_NAME_MAX + 1]; exfat_get_name(node, name, EXFAT_NAME_MAX); - exfat_error("file `%s' has invalid cluster", name); - return; + exfat_error("file `%s' has invalid cluster 0x%x", name, c); + rc = 1; + break; } if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0) { char name[EXFAT_NAME_MAX + 1]; exfat_get_name(node, name, EXFAT_NAME_MAX); - exfat_error("cluster 0x%x of file `%s' already allocated " - "to another file", c, name); + exfat_error("cluster 0x%x of file `%s' is not allocated", c, name); + rc = 1; } c = exfat_next_cluster(ef, node, c); } + return rc; } static void dirck(struct exfat* ef, const char* path) @@ -89,75 +65,108 @@ static void dirck(struct exfat* ef, const char* path) struct exfat_node* node; struct exfat_iterator it; int rc; - char subpath[EXFAT_NAME_MAX + 1]; + size_t path_length; + char* entry_path; if (exfat_lookup(ef, &parent, path) != 0) exfat_bug("directory `%s' is not found", path); if (!(parent->flags & EXFAT_ATTRIB_DIR)) exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags); + if (nodeck(ef, parent) != 0) + return; + + path_length = strlen(path); + entry_path = malloc(path_length + 1 + EXFAT_NAME_MAX); + if (entry_path == NULL) + { + exfat_error("out of memory"); + return; + } + strcpy(entry_path, path); + strcat(entry_path, "/"); rc = exfat_opendir(ef, parent, &it); if (rc != 0) { - exfat_put_node(parent); + free(entry_path); + exfat_put_node(ef, parent); exfat_error("failed to open directory `%s'", path); return; } while ((node = exfat_readdir(ef, &it))) { - strcpy(subpath, path); - strcat(subpath, "/"); - exfat_get_name(node, subpath + strlen(subpath), - EXFAT_NAME_MAX - strlen(subpath)); - exfat_debug("%s: %s, %llu bytes, cluster %u", subpath, + exfat_get_name(node, entry_path + path_length + 1, EXFAT_NAME_MAX); + exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented", node->size, node->start_cluster); if (node->flags & EXFAT_ATTRIB_DIR) { directories_count++; - dirck(ef, subpath); + dirck(ef, entry_path); } else + { files_count++; - nodeck(ef, node); - exfat_put_node(node); + nodeck(ef, node); + } + exfat_put_node(ef, node); } - exfat_closedir(&it); - exfat_put_node(parent); + exfat_closedir(ef, &it); + exfat_put_node(ef, parent); + free(entry_path); } static void fsck(struct exfat* ef) { - sbck(ef); + exfat_print_info(ef->sb, exfat_count_free_clusters(ef)); dirck(ef, ""); } +static void usage(const char* prog) +{ + fprintf(stderr, "Usage: %s [-v] \n", prog); + exit(1); +} + int main(int argc, char* argv[]) { + char** pp; + const char* spec = NULL; struct exfat ef; - if (argc != 2) + printf("exfatfsck %u.%u.%u\n", + EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); + + for (pp = argv + 1; *pp; pp++) { - fprintf(stderr, "usage: %s \n", argv[0]); - return 1; + if (strcmp(*pp, "-v") == 0) + { + puts("Copyright (C) 2011, 2012 Andrew Nayenko"); + return 0; + } + else if (spec == NULL) + spec = *pp; + else + usage(argv[0]); } - printf("exfatck %u.%u\n", - EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR); + if (spec == NULL) + usage(argv[0]); - if (exfat_mount(&ef, argv[1]) != 0) + if (exfat_mount(&ef, spec, "ro") != 0) return 1; - printf("Checking file system on %s.\n", argv[1]); + printf("Checking file system on %s.\n", spec); fsck(&ef); exfat_unmount(&ef); printf("Totally %"PRIu64" directories and %"PRIu64" files.\n", directories_count, files_count); - fputs("File system checking finished: ", stdout); - if (exfat_errors == 0) - puts("seems OK."); - else - printf("%d ERROR%s FOUND!!!\n", exfat_errors, - exfat_errors > 1 ? "S WERE" : " WAS"); + fputs("File system checking finished. ", stdout); + if (exfat_errors != 0) + { + printf("ERRORS FOUND: %d.\n", exfat_errors); + return 1; + } + puts("No errors found."); return 0; }