]> git.sven.stormbind.net Git - sven/exfat-utils.git/blobdiff - dump/main.c
New upstream version 1.2.5
[sven/exfat-utils.git] / dump / main.c
index 357557d6ee2af4dfa0a4dda46f85d1b9d835c42f..fda5b9b35b5ca125463659d86eade510486259b1 100644 (file)
@@ -2,11 +2,12 @@
        main.c (08.11.10)
        Prints detailed information about exFAT volume.
 
        main.c (08.11.10)
        Prints detailed information about exFAT volume.
 
-       Copyright (C) 2010  Andrew Nayenko
+       Free exFAT implementation.
+       Copyright (C) 2011-2016  Andrew Nayenko
 
 
-       This program is free software: you can redistribute it and/or modify
+       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
        it under the terms of the GNU General Public License as published by
-       the Free Software Foundation, either version 3 of the License, or
+       the Free Software Foundation, either version 2 of the License, or
        (at your option) any later version.
 
        This program is distributed in the hope that it will be useful,
        (at your option) any later version.
 
        This program is distributed in the hope that it will be useful,
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
 
        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 <http://www.gnu.org/licenses/>.
+       You should have received a copy of the GNU General Public License along
+       with this program; if not, write to the Free Software Foundation, Inc.,
+       51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 */
 
+#include <exfat.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <string.h>
-#include <exfat.h>
 
 static void print_generic_info(const struct exfat_super_block* sb)
 {
 
 static void print_generic_info(const struct exfat_super_block* sb)
 {
@@ -73,24 +75,23 @@ static void print_other_info(const struct exfat_super_block* sb)
 
 static int dump_sb(const char* spec)
 {
 
 static int dump_sb(const char* spec)
 {
-       int fd;
+       struct exfat_dev* dev;
        struct exfat_super_block sb;
 
        struct exfat_super_block sb;
 
-       fd = exfat_open(spec, 1);
-       if (fd < 0)
+       dev = exfat_open(spec, EXFAT_MODE_RO);
+       if (dev == NULL)
                return 1;
 
                return 1;
 
-       if (read(fd, &sb, sizeof(struct exfat_super_block))
-                       != sizeof(struct exfat_super_block))
+       if (exfat_read(dev, &sb, sizeof(struct exfat_super_block)) < 0)
        {
        {
-               close(fd);
-               exfat_error("failed to read from `%s'", spec);
+               exfat_close(dev);
+               exfat_error("failed to read from '%s'", spec);
                return 1;
        }
        if (memcmp(sb.oem_name, "EXFAT   ", sizeof(sb.oem_name)) != 0)
        {
                return 1;
        }
        if (memcmp(sb.oem_name, "EXFAT   ", sizeof(sb.oem_name)) != 0)
        {
-               close(fd);
-               exfat_error("exFAT file system is not found on `%s'", spec);
+               exfat_close(dev);
+               exfat_error("exFAT file system is not found on '%s'", spec);
                return 1;
        }
 
                return 1;
        }
 
@@ -99,7 +100,7 @@ static int dump_sb(const char* spec)
        print_cluster_info(&sb);
        print_other_info(&sb);
 
        print_cluster_info(&sb);
        print_other_info(&sb);
 
-       close(fd);
+       exfat_close(dev);
        return 0;
 }
 
        return 0;
 }
 
@@ -113,7 +114,7 @@ static void dump_sectors(struct exfat* ef)
        puts("");
 }
 
        puts("");
 }
 
-static int dump_full(const char* spec, int used_sectors)
+static int dump_full(const char* spec, bool used_sectors)
 {
        struct exfat ef;
        uint32_t free_clusters;
 {
        struct exfat ef;
        uint32_t free_clusters;
@@ -139,40 +140,104 @@ static int dump_full(const char* spec, int used_sectors)
        return 0;
 }
 
        return 0;
 }
 
+static int dump_file_fragments(const char* spec, const char* path)
+{
+       struct exfat ef;
+       struct exfat_node* node;
+       cluster_t cluster;
+       cluster_t next_cluster;
+       cluster_t fragment_start_cluster;
+       off_t remainder;
+       off_t fragment_size = 0;
+       int rc = 0;
+
+       if (exfat_mount(&ef, spec, "ro") != 0)
+               return 1;
+
+       rc = exfat_lookup(&ef, &node, path);
+       if (rc != 0)
+       {
+               exfat_unmount(&ef);
+               exfat_error("'%s': %s", path, strerror(-rc));
+               return 1;
+       }
+
+       cluster = fragment_start_cluster = node->start_cluster;
+       remainder = node->size;
+       while (remainder > 0)
+       {
+               off_t lsize;
+
+               if (CLUSTER_INVALID(cluster))
+               {
+                       exfat_error("'%s' has invalid cluster %#x", path, cluster);
+                       rc = 1;
+                       break;
+               }
+
+               lsize = MIN(CLUSTER_SIZE(*ef.sb), remainder);
+               fragment_size += lsize;
+               remainder -= lsize;
+
+               next_cluster = exfat_next_cluster(&ef, node, cluster);
+               if (next_cluster != cluster + 1 || remainder == 0)
+               {
+                       /* next cluster is not contiguous or this is EOF */
+                       printf("%"PRIu64" %"PRIu64"\n",
+                                       exfat_c2o(&ef, fragment_start_cluster), fragment_size);
+                       /* start a new fragment */
+                       fragment_start_cluster = next_cluster;
+                       fragment_size = 0;
+               }
+               cluster = next_cluster;
+       }
+
+       exfat_put_node(&ef, node);
+       exfat_unmount(&ef);
+       return rc;
+}
+
 static void usage(const char* prog)
 {
 static void usage(const char* prog)
 {
-       fprintf(stderr, "Usage: %s [-s] [-u] [-v] <device>\n", prog);
+       fprintf(stderr, "Usage: %s [-s] [-u] [-f file] [-V] <device>\n", prog);
        exit(1);
 }
 
 int main(int argc, char* argv[])
 {
        exit(1);
 }
 
 int main(int argc, char* argv[])
 {
-       char** pp;
+       int opt;
        const char* spec = NULL;
        const char* spec = NULL;
-       int sb_only = 0;
-       int used_sectors = 0;
-
-       printf("dumpexfat %u.%u.%u\n",
-                       EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
+       bool sb_only = false;
+       bool used_sectors = false;
+       const char* file_path = NULL;
 
 
-       for (pp = argv + 1; *pp; pp++)
+       while ((opt = getopt(argc, argv, "suf:V")) != -1)
        {
        {
-               if (strcmp(*pp, "-s") == 0)
-                       sb_only = 1;
-               else if (strcmp(*pp, "-u") == 0)
-                       used_sectors = 1;
-               else if (strcmp(*pp, "-v") == 0)
+               switch (opt)
                {
                {
-                       puts("Copyright (C) 2010  Andrew Nayenko");
+               case 's':
+                       sb_only = true;
+                       break;
+               case 'u':
+                       used_sectors = true;
+                       break;
+               case 'f':
+                       file_path = optarg;
+                       break;
+               case 'V':
+                       printf("dumpexfat %s\n", VERSION);
+                       puts("Copyright (C) 2011-2016  Andrew Nayenko");
                        return 0;
                        return 0;
-               }
-               else if (spec == NULL)
-                       spec = *pp;
-               else
+               default:
                        usage(argv[0]);
                        usage(argv[0]);
+               }
        }
        }
-       if (spec == NULL)
+       if (argc - optind != 1)
                usage(argv[0]);
                usage(argv[0]);
+       spec = argv[optind];
+
+       if (file_path)
+               return dump_file_fragments(spec, file_path);
 
        if (sb_only)
                return dump_sb(spec);
 
        if (sb_only)
                return dump_sb(spec);