]> git.sven.stormbind.net Git - sven/exfat-utils.git/blobdiff - fsck/main.c
releasing package exfat-utils version 1.3.0-2
[sven/exfat-utils.git] / fsck / main.c
index ded76b20c2ef78bda2ccebf5f0455db03e8b30fa..af40c76ad2b098cf113356b732fea4025ccee89b 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system checker.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2015  Andrew Nayenko
+       Copyright (C) 2011-2018  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
@@ -33,26 +33,26 @@ uint64_t files_count, directories_count;
 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 clusters = DIV_ROUND_UP(node->size, cluster_size);
        cluster_t c = node->start_cluster;
        int rc = 0;
 
        while (clusters--)
        {
-               if (CLUSTER_INVALID(c))
+               if (CLUSTER_INVALID(*ef->sb, c))
                {
-                       char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
+                       char name[EXFAT_UTF8_NAME_BUFFER_MAX];
 
-                       exfat_get_name(node, name, sizeof(name) - 1);
+                       exfat_get_name(node, name);
                        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[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
+                       char name[EXFAT_UTF8_NAME_BUFFER_MAX];
 
-                       exfat_get_name(node, name, sizeof(name) - 1);
+                       exfat_get_name(node, name);
                        exfat_error("cluster 0x%x of file '%s' is not allocated", c, name);
                        rc = 1;
                }
@@ -72,8 +72,8 @@ static void dirck(struct exfat* ef, const char* 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 (!(parent->attrib & EXFAT_ATTRIB_DIR))
+               exfat_bug("'%s' is not a directory (%#hx)", path, parent->attrib);
        if (nodeck(ef, parent) != 0)
        {
                exfat_put_node(ef, parent);
@@ -81,7 +81,7 @@ static void dirck(struct exfat* ef, const char* path)
        }
 
        path_length = strlen(path);
-       entry_path = malloc(path_length + 1 + UTF8_BYTES(EXFAT_NAME_MAX) + 1);
+       entry_path = malloc(path_length + 1 + EXFAT_UTF8_NAME_BUFFER_MAX);
        if (entry_path == NULL)
        {
                exfat_put_node(ef, parent);
@@ -98,14 +98,13 @@ static void dirck(struct exfat* ef, const char* path)
                exfat_put_node(ef, parent);
                return;
        }
-       while ((node = exfat_readdir(ef, &it)))
+       while ((node = exfat_readdir(&it)))
        {
-               exfat_get_name(node, entry_path + path_length + 1,
-                               UTF8_BYTES(EXFAT_NAME_MAX));
+               exfat_get_name(node, entry_path + path_length + 1);
                exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path,
-                               IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
+                               node->is_contiguous ? "contiguous" : "fragmented",
                                node->size, node->start_cluster);
-               if (node->flags & EXFAT_ATTRIB_DIR)
+               if (node->attrib & EXFAT_ATTRIB_DIR)
                {
                        directories_count++;
                        dirck(ef, entry_path);
@@ -115,39 +114,67 @@ static void dirck(struct exfat* ef, const char* path)
                        files_count++;
                        nodeck(ef, node);
                }
+               exfat_flush_node(ef, node);
                exfat_put_node(ef, node);
        }
        exfat_closedir(ef, &it);
+       exfat_flush_node(ef, parent);
        exfat_put_node(ef, parent);
        free(entry_path);
 }
 
-static void fsck(struct exfat* ef)
+static void fsck(struct exfat* ef, const char* spec, const char* options)
 {
+       if (exfat_mount(ef, spec, options) != 0)
+       {
+               fputs("File system checking stopped. ", stdout);
+               return;
+       }
+
        exfat_print_info(ef->sb, exfat_count_free_clusters(ef));
        dirck(ef, "");
+       exfat_unmount(ef);
+
+       printf("Totally %"PRIu64" directories and %"PRIu64" files.\n",
+                       directories_count, files_count);
+       fputs("File system checking finished. ", stdout);
 }
 
 static void usage(const char* prog)
 {
-       fprintf(stderr, "Usage: %s [-V] <device>\n", prog);
+       fprintf(stderr, "Usage: %s [-a | -n | -p | -y] <device>\n", prog);
+       fprintf(stderr, "       %s -V\n", prog);
        exit(1);
 }
 
 int main(int argc, char* argv[])
 {
        int opt;
+       const char* options;
        const char* spec = NULL;
        struct exfat ef;
 
        printf("exfatfsck %s\n", VERSION);
 
-       while ((opt = getopt(argc, argv, "V")) != -1)
+       if (isatty(STDIN_FILENO))
+               options = "repair=1";
+       else
+               options = "repair=0";
+
+       while ((opt = getopt(argc, argv, "anpVy")) != -1)
        {
                switch (opt)
                {
+               case 'a':
+               case 'p':
+               case 'y':
+                       options = "repair=2";
+                       break;
+               case 'n':
+                       options = "repair=0,ro";
+                       break;
                case 'V':
-                       puts("Copyright (C) 2011-2015  Andrew Nayenko");
+                       puts("Copyright (C) 2011-2018  Andrew Nayenko");
                        return 0;
                default:
                        usage(argv[0]);
@@ -158,19 +185,12 @@ int main(int argc, char* argv[])
                usage(argv[0]);
        spec = argv[optind];
 
-       if (exfat_mount(&ef, spec, "ro") != 0)
-               return 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);
+       fsck(&ef, spec, options);
        if (exfat_errors != 0)
        {
-               printf("ERRORS FOUND: %d.\n", exfat_errors);
+               printf("ERRORS FOUND: %d, FIXED: %d.\n",
+                               exfat_errors, exfat_errors_fixed);
                return 1;
        }
        puts("No errors found.");