]> git.sven.stormbind.net Git - sven/fuse-exfat.git/blobdiff - libexfat/mount.c
releasing package fuse-exfat version 1.4.0-2
[sven/fuse-exfat.git] / libexfat / mount.c
index 3b18b122e43ea087ebf401b014d3bf3f509ccc29..86fb84db2445f56615f965579e3cd69b04d536d7 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2018  Andrew Nayenko
+       Copyright (C) 2010-2023  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
@@ -79,18 +79,6 @@ static int get_int_option(const char* options, const char* option_name,
        return strtol(p, NULL, base);
 }
 
-static bool match_option(const char* options, const char* option_name)
-{
-       const char* p;
-       size_t length = strlen(option_name);
-
-       for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
-               if ((p == options || p[-1] == ',') &&
-                               (p[length] == ',' || p[length] == '\0'))
-                       return true;
-       return false;
-}
-
 static void parse_options(struct exfat* ef, const char* options)
 {
        int opt_umask;
@@ -102,16 +90,29 @@ static void parse_options(struct exfat* ef, const char* options)
        ef->uid = get_int_option(options, "uid", 10, geteuid());
        ef->gid = get_int_option(options, "gid", 10, getegid());
 
-       ef->noatime = match_option(options, "noatime");
+       ef->noatime = exfat_match_option(options, "noatime");
+
+       switch (get_int_option(options, "repair", 10, 0))
+       {
+       case 1:
+               ef->repair = EXFAT_REPAIR_ASK;
+               break;
+       case 2:
+               ef->repair = EXFAT_REPAIR_YES;
+               break;
+       default:
+               ef->repair = EXFAT_REPAIR_NO;
+               break;
+       }
 }
 
-static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector,
-               off_t sector_size)
+static bool verify_vbr_checksum(const struct exfat* ef, void* sector)
 {
+       off_t sector_size = SECTOR_SIZE(*ef->sb);
        uint32_t vbr_checksum;
-       int i;
+       size_t i;
 
-       if (exfat_pread(dev, sector, sector_size, 0) < 0)
+       if (exfat_pread(ef->dev, sector, sector_size, 0) < 0)
        {
                exfat_error("failed to read boot sector");
                return false;
@@ -119,7 +120,7 @@ static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector,
        vbr_checksum = exfat_vbr_start_checksum(sector, sector_size);
        for (i = 1; i < 11; i++)
        {
-               if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
+               if (exfat_pread(ef->dev, sector, sector_size, i * sector_size) < 0)
                {
                        exfat_error("failed to read VBR sector");
                        return false;
@@ -127,7 +128,7 @@ static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector,
                vbr_checksum = exfat_vbr_add_checksum(sector, sector_size,
                                vbr_checksum);
        }
-       if (exfat_pread(dev, sector, sector_size, i * sector_size) < 0)
+       if (exfat_pread(ef->dev, sector, sector_size, i * sector_size) < 0)
        {
                exfat_error("failed to read VBR checksum sector");
                return false;
@@ -137,7 +138,8 @@ static bool verify_vbr_checksum(struct exfat_dev* dev, void* sector,
                {
                        exfat_error("invalid VBR checksum 0x%x (expected 0x%x)",
                                        le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum);
-                       return false;
+                       if (!EXFAT_REPAIR(invalid_vbr_checksum, ef, sector, vbr_checksum))
+                               return false;
                }
        return true;
 }
@@ -152,11 +154,8 @@ static int commit_super_block(const struct exfat* ef)
        return exfat_fsync(ef->dev);
 }
 
-static int prepare_super_block(const struct exfat* ef)
+int exfat_soil_super_block(const struct exfat* ef)
 {
-       if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
-               exfat_warn("volume was not unmounted cleanly");
-
        if (ef->ro)
                return 0;
 
@@ -191,15 +190,15 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
 
        parse_options(ef, options);
 
-       if (match_option(options, "ro"))
+       if (exfat_match_option(options, "ro"))
                mode = EXFAT_MODE_RO;
-       else if (match_option(options, "ro_fallback"))
+       else if (exfat_match_option(options, "ro_fallback"))
                mode = EXFAT_MODE_ANY;
        else
                mode = EXFAT_MODE_RW;
        ef->dev = exfat_open(spec, mode);
        if (ef->dev == NULL)
-               return -EIO;
+               return -ENODEV;
        if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
        {
                if (mode == EXFAT_MODE_ANY)
@@ -252,7 +251,7 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
                return -ENOMEM;
        }
        /* use zero_cluster as a temporary buffer for VBR checksum verification */
-       if (!verify_vbr_checksum(ef->dev, ef->zero_cluster, SECTOR_SIZE(*ef->sb)))
+       if (!verify_vbr_checksum(ef, ef->zero_cluster))
        {
                exfat_free(ef);
                return -EIO;
@@ -272,7 +271,7 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
                return -EIO;
        }
        if (le64_to_cpu(ef->sb->sector_count) * SECTOR_SIZE(*ef->sb) >
-                       exfat_get_size(ef->dev))
+                       (uint64_t) exfat_get_size(ef->dev))
        {
                /* this can cause I/O errors later but we don't fail mounting to let
                   user rescue data */
@@ -291,6 +290,8 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
                exfat_free(ef);
                return -EIO;
        }
+       if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED)
+               exfat_warn("volume was not unmounted cleanly");
 
        ef->root = malloc(sizeof(struct exfat_node));
        if (ef->root == NULL)
@@ -304,7 +305,7 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
        ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster);
        ef->root->fptr_cluster = ef->root->start_cluster;
        ef->root->name[0] = cpu_to_le16('\0');
-       ef->root->size = rootdir_size(ef);
+       ef->root->valid_size = ef->root->size = rootdir_size(ef);
        if (ef->root->size == 0)
        {
                exfat_free(ef);
@@ -330,9 +331,6 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
                goto error;
        }
 
-       if (prepare_super_block(ef) != 0)
-               goto error;
-
        return 0;
 
 error: