]> git.sven.stormbind.net Git - sven/exfat-utils.git/commitdiff
New upstream version 1.2.6 upstream/1.2.6
authorSven Hoexter <sven.hoexter@rewe-digital.com>
Mon, 15 May 2017 15:34:12 +0000 (17:34 +0200)
committerSven Hoexter <sven.hoexter@rewe-digital.com>
Mon, 15 May 2017 15:34:12 +0000 (17:34 +0200)
47 files changed:
ChangeLog
Makefile.am
Makefile.in
configure
configure.ac
dump/Makefile.am
dump/Makefile.in
dump/main.c
fsck/Makefile.am
fsck/Makefile.in
fsck/main.c
label/Makefile.am
label/Makefile.in
label/main.c
libexfat/Makefile.am
libexfat/Makefile.in
libexfat/byteorder.h
libexfat/cluster.c
libexfat/compiler.h
libexfat/exfat.h
libexfat/exfatfs.h
libexfat/io.c
libexfat/log.c
libexfat/lookup.c
libexfat/mount.c
libexfat/node.c
libexfat/platform.h
libexfat/time.c
libexfat/utf.c
libexfat/utils.c
mkfs/Makefile.am
mkfs/Makefile.in
mkfs/cbm.c
mkfs/cbm.h
mkfs/fat.c
mkfs/fat.h
mkfs/main.c
mkfs/mkexfat.c
mkfs/mkexfat.h
mkfs/rootdir.c
mkfs/rootdir.h
mkfs/uct.c
mkfs/uct.h
mkfs/uctc.c
mkfs/uctc.h
mkfs/vbr.c
mkfs/vbr.h

index 639972e8ccb5359e9f4151b9ee674f2bd56f920c..445cd34c35bed9a8f39d3186a84e6bcce6c3b909 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,15 @@
+1.2.6 (2017-01-28)
+
+* Operations with directories (except initial listing) now make less
+read/write system calls.
+* Fixed handling of files with optional tail entries (0xe0-0xff): videoclip
+files created by Sony cameras were missing.
+* Write operations now correctly return ENOSPC (instead of EIO) when there is
+no free disk space left.
+* Fixed max file name length: it's 255 16-bit code units (not 256).
+
 1.2.5 (2016-12-05)
 1.2.5 (2016-12-05)
+
 * Added an option for dumpexfat to show file fragments [Daniel Drake].
 * Fixed crash when directory starts with an invalid cluster.
 * Daylight saving time in now properly reflected in file timestamps.
 * Added an option for dumpexfat to show file fragments [Daniel Drake].
 * Fixed crash when directory starts with an invalid cluster.
 * Daylight saving time in now properly reflected in file timestamps.
index 1fe87badc9c41b4b217bd50db6646f531d56d80d..aa621d6db9b076a12cbe5847918c6241a1da83ca 100644 (file)
@@ -3,7 +3,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2010-2016  Andrew Nayenko
+#      Copyright (C) 2010-2017  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
 #
 #      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
index 06190f8134af76f628bf48ca0bfe894d4940a941..f16c91fd43f2e22099fa5487cee10e155b2b5311 100644 (file)
@@ -19,7 +19,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2010-2016  Andrew Nayenko
+#      Copyright (C) 2010-2017  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
 #
 #      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
index 2ec2bb99027cd6f4981905231a8306a6b73c7083..c9a8d7e2ef540702ef89c1cebd121cdbf37dbb44 100755 (executable)
--- a/configure
+++ b/configure
@@ -1,6 +1,6 @@
 #! /bin/sh
 # Guess values for system-dependent variables and create Makefiles.
 #! /bin/sh
 # Guess values for system-dependent variables and create Makefiles.
-# Generated by GNU Autoconf 2.69 for Free exFAT implementation 1.2.5.
+# Generated by GNU Autoconf 2.69 for Free exFAT implementation 1.2.6.
 #
 # Report bugs to <relan@users.noreply.github.com>.
 #
 #
 # Report bugs to <relan@users.noreply.github.com>.
 #
@@ -579,8 +579,8 @@ MAKEFLAGS=
 # Identity of this package.
 PACKAGE_NAME='Free exFAT implementation'
 PACKAGE_TARNAME='exfat-utils'
 # Identity of this package.
 PACKAGE_NAME='Free exFAT implementation'
 PACKAGE_TARNAME='exfat-utils'
-PACKAGE_VERSION='1.2.5'
-PACKAGE_STRING='Free exFAT implementation 1.2.5'
+PACKAGE_VERSION='1.2.6'
+PACKAGE_STRING='Free exFAT implementation 1.2.6'
 PACKAGE_BUGREPORT='relan@users.noreply.github.com'
 PACKAGE_URL='https://github.com/relan/exfat'
 
 PACKAGE_BUGREPORT='relan@users.noreply.github.com'
 PACKAGE_URL='https://github.com/relan/exfat'
 
@@ -1228,7 +1228,7 @@ if test "$ac_init_help" = "long"; then
   # Omit some internal or obsolete options to make the list less imposing.
   # This message is too long to be a string in the A/UX 3.1 sh.
   cat <<_ACEOF
   # Omit some internal or obsolete options to make the list less imposing.
   # This message is too long to be a string in the A/UX 3.1 sh.
   cat <<_ACEOF
-\`configure' configures Free exFAT implementation 1.2.5 to adapt to many kinds of systems.
+\`configure' configures Free exFAT implementation 1.2.6 to adapt to many kinds of systems.
 
 Usage: $0 [OPTION]... [VAR=VALUE]...
 
 
 Usage: $0 [OPTION]... [VAR=VALUE]...
 
@@ -1294,7 +1294,7 @@ fi
 
 if test -n "$ac_init_help"; then
   case $ac_init_help in
 
 if test -n "$ac_init_help"; then
   case $ac_init_help in
-     short | recursive ) echo "Configuration of Free exFAT implementation 1.2.5:";;
+     short | recursive ) echo "Configuration of Free exFAT implementation 1.2.6:";;
    esac
   cat <<\_ACEOF
 
    esac
   cat <<\_ACEOF
 
@@ -1386,7 +1386,7 @@ fi
 test -n "$ac_init_help" && exit $ac_status
 if $ac_init_version; then
   cat <<\_ACEOF
 test -n "$ac_init_help" && exit $ac_status
 if $ac_init_version; then
   cat <<\_ACEOF
-Free exFAT implementation configure 1.2.5
+Free exFAT implementation configure 1.2.6
 generated by GNU Autoconf 2.69
 
 Copyright (C) 2012 Free Software Foundation, Inc.
 generated by GNU Autoconf 2.69
 
 Copyright (C) 2012 Free Software Foundation, Inc.
@@ -1441,7 +1441,7 @@ cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
 
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
 
-It was created by Free exFAT implementation $as_me 1.2.5, which was
+It was created by Free exFAT implementation $as_me 1.2.6, which was
 generated by GNU Autoconf 2.69.  Invocation command line was
 
   $ $0 $@
 generated by GNU Autoconf 2.69.  Invocation command line was
 
   $ $0 $@
@@ -2304,7 +2304,7 @@ fi
 
 # Define the identity of the package.
  PACKAGE='exfat-utils'
 
 # Define the identity of the package.
  PACKAGE='exfat-utils'
- VERSION='1.2.5'
+ VERSION='1.2.6'
 
 
 cat >>confdefs.h <<_ACEOF
 
 
 cat >>confdefs.h <<_ACEOF
@@ -4611,7 +4611,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 # report actual input values of CONFIG_FILES etc. instead of their
 # values after options handling.
 ac_log="
 # report actual input values of CONFIG_FILES etc. instead of their
 # values after options handling.
 ac_log="
-This file was extended by Free exFAT implementation $as_me 1.2.5, which was
+This file was extended by Free exFAT implementation $as_me 1.2.6, which was
 generated by GNU Autoconf 2.69.  Invocation command line was
 
   CONFIG_FILES    = $CONFIG_FILES
 generated by GNU Autoconf 2.69.  Invocation command line was
 
   CONFIG_FILES    = $CONFIG_FILES
@@ -4678,7 +4678,7 @@ _ACEOF
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 ac_cs_version="\\
 cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 ac_cs_version="\\
-Free exFAT implementation config.status 1.2.5
+Free exFAT implementation config.status 1.2.6
 configured by $0, generated by GNU Autoconf 2.69,
   with options \\"\$ac_cs_config\\"
 
 configured by $0, generated by GNU Autoconf 2.69,
   with options \\"\$ac_cs_config\\"
 
index c2f2e5f4439ab4f2969c777a57078dc0276eff73..be2e0482ea3319e4c6437f413649eeaa62c7af3b 100644 (file)
@@ -3,7 +3,7 @@
 #      Autoconf source.
 #
 #      Free exFAT implementation.
 #      Autoconf source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2010-2016  Andrew Nayenko
+#      Copyright (C) 2010-2017  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
 #
 #      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
@@ -21,7 +21,7 @@
 #
 
 AC_INIT([Free exFAT implementation],
 #
 
 AC_INIT([Free exFAT implementation],
-       [1.2.5],
+       [1.2.6],
        [relan@users.noreply.github.com],
        [exfat-utils],
        [https://github.com/relan/exfat])
        [relan@users.noreply.github.com],
        [exfat-utils],
        [https://github.com/relan/exfat])
index f9a79dd8dd59b8f74ebd546dbb9407d61bcee490..fa95e2f718005c1a641a3145907e11c612b40b19 100644 (file)
@@ -3,7 +3,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2011-2016  Andrew Nayenko
+#      Copyright (C) 2011-2017  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
 #
 #      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
index b85985ca056f868b6874ca4879d96676d800aa26..e9d163a5d04b22d5b78551b96b2cc692b98f322f 100644 (file)
@@ -19,7 +19,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2011-2016  Andrew Nayenko
+#      Copyright (C) 2011-2017  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
 #
 #      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
index fda5b9b35b5ca125463659d86eade510486259b1..420a951d60a75987a8f3f28c0b38da459be604e1 100644 (file)
@@ -3,7 +3,7 @@
        Prints detailed information about exFAT volume.
 
        Free exFAT implementation.
        Prints detailed information about exFAT volume.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
@@ -226,7 +226,7 @@ int main(int argc, char* argv[])
                        break;
                case 'V':
                        printf("dumpexfat %s\n", VERSION);
                        break;
                case 'V':
                        printf("dumpexfat %s\n", VERSION);
-                       puts("Copyright (C) 2011-2016  Andrew Nayenko");
+                       puts("Copyright (C) 2011-2017  Andrew Nayenko");
                        return 0;
                default:
                        usage(argv[0]);
                        return 0;
                default:
                        usage(argv[0]);
index dbb8121fbf8c201b628d47ba864cdc8a255014a8..0d15a198b162d2c142fd27c3519d945705fe9d49 100644 (file)
@@ -3,7 +3,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2011-2016  Andrew Nayenko
+#      Copyright (C) 2011-2017  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
 #
 #      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
index e45c02fbad9a7e866516b854ba9b6711ea62a5a4..65eb1b1391783a7d28202595ab4a2f0e45864832 100644 (file)
@@ -19,7 +19,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2011-2016  Andrew Nayenko
+#      Copyright (C) 2011-2017  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
 #
 #      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
index cda1965225c259abe482e35a321551b19a82786d..b413c58bfe4f0074917afbeb214df9a656e7cf3e 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system checker.
 
        Free exFAT implementation.
        exFAT file system checker.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
@@ -41,18 +41,18 @@ static int nodeck(struct exfat* ef, struct exfat_node* node)
        {
                if (CLUSTER_INVALID(c))
                {
        {
                if (CLUSTER_INVALID(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)
                {
                        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;
                }
                        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 (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);
        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);
        }
 
        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);
        if (entry_path == NULL)
        {
                exfat_put_node(ef, parent);
@@ -100,12 +100,11 @@ static void dirck(struct exfat* ef, const char* path)
        }
        while ((node = exfat_readdir(ef, &it)))
        {
        }
        while ((node = exfat_readdir(ef, &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,
                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);
                                node->size, node->start_cluster);
-               if (node->flags & EXFAT_ATTRIB_DIR)
+               if (node->attrib & EXFAT_ATTRIB_DIR)
                {
                        directories_count++;
                        dirck(ef, entry_path);
                {
                        directories_count++;
                        dirck(ef, entry_path);
@@ -147,7 +146,7 @@ int main(int argc, char* argv[])
                switch (opt)
                {
                case 'V':
                switch (opt)
                {
                case 'V':
-                       puts("Copyright (C) 2011-2016  Andrew Nayenko");
+                       puts("Copyright (C) 2011-2017  Andrew Nayenko");
                        return 0;
                default:
                        usage(argv[0]);
                        return 0;
                default:
                        usage(argv[0]);
index c610f2ffa7594c1043a25bb7417f328b166db2fd..0fa6c9f8afc148f36d9de429b8551668c8ccede3 100644 (file)
@@ -3,7 +3,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2011-2016  Andrew Nayenko
+#      Copyright (C) 2011-2017  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
 #
 #      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
index df1438724f05d2836e925f37c51ae2087e4c9c8a..e4fe5372e1446b394988821acbddaaa9c4b5a3c0 100644 (file)
@@ -19,7 +19,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2011-2016  Andrew Nayenko
+#      Copyright (C) 2011-2017  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
 #
 #      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
index a083f3018965e2f9a8e8e7dba7c19908a3c2fcb2..7c2739a42b501a95fd91d3df714316b5c477b400 100644 (file)
@@ -3,7 +3,7 @@
        Prints or changes exFAT volume label.
 
        Free exFAT implementation.
        Prints or changes exFAT volume label.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
@@ -34,7 +34,7 @@ int main(int argc, char* argv[])
                if (strcmp(*pp, "-V") == 0)
                {
                        printf("exfatlabel %s\n", VERSION);
                if (strcmp(*pp, "-V") == 0)
                {
                        printf("exfatlabel %s\n", VERSION);
-                       puts("Copyright (C) 2011-2016  Andrew Nayenko");
+                       puts("Copyright (C) 2011-2017  Andrew Nayenko");
                        return 0;
                }
 
                        return 0;
                }
 
index 102d89d6b14a3aa6e7bd75b36df9fc1cab093c27..08ed34798a6fc357972b0df0e06e75c1fd8180ff 100644 (file)
@@ -3,7 +3,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2010-2016  Andrew Nayenko
+#      Copyright (C) 2010-2017  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
 #
 #      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
index 164e661e7750fccd51472df741fcaaf98a8fa56b..9936f056ba8b3cae604dc68ad3166afa2803f0b2 100644 (file)
@@ -19,7 +19,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2010-2016  Andrew Nayenko
+#      Copyright (C) 2010-2017  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
 #
 #      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
index e417ad6a3fb424a2728e74e3625784321a75268c..173b2506beea275a2c080a849f0d72d56c8c3523 100644 (file)
@@ -3,7 +3,7 @@
        Endianness stuff. exFAT uses little-endian byte order.
 
        Free exFAT implementation.
        Endianness stuff. exFAT uses little-endian byte order.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
index 34f027cc3270a8aa214e56b496bdc3530d80f4a3..1d3923355534cfb371ed243bc219fdbdd51dc1d4 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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,7 +79,7 @@ cluster_t exfat_next_cluster(const struct exfat* ef,
        if (cluster < EXFAT_FIRST_DATA_CLUSTER)
                exfat_bug("bad cluster 0x%x", cluster);
 
        if (cluster < EXFAT_FIRST_DATA_CLUSTER)
                exfat_bug("bad cluster 0x%x", cluster);
 
-       if (IS_CONTIGUOUS(*node))
+       if (node->is_contiguous)
                return cluster + 1;
        fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
                + cluster * sizeof(cluster_t);
                return cluster + 1;
        fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start))
                + cluster * sizeof(cluster_t);
@@ -270,7 +270,7 @@ static int grow_file(struct exfat* ef, struct exfat_node* node,
                node->fptr_cluster = node->start_cluster = previous;
                allocated = 1;
                /* file consists of only one cluster, so it's contiguous */
                node->fptr_cluster = node->start_cluster = previous;
                allocated = 1;
                /* file consists of only one cluster, so it's contiguous */
-               node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
+               node->is_contiguous = true;
        }
 
        while (allocated < difference)
        }
 
        while (allocated < difference)
@@ -282,22 +282,22 @@ static int grow_file(struct exfat* ef, struct exfat_node* node,
                                shrink_file(ef, node, current + allocated, allocated);
                        return -ENOSPC;
                }
                                shrink_file(ef, node, current + allocated, allocated);
                        return -ENOSPC;
                }
-               if (next != previous - 1 && IS_CONTIGUOUS(*node))
+               if (next != previous - 1 && node->is_contiguous)
                {
                        /* it's a pity, but we are not able to keep the file contiguous
                           anymore */
                        if (!make_noncontiguous(ef, node->start_cluster, previous))
                                return -EIO;
                {
                        /* it's a pity, but we are not able to keep the file contiguous
                           anymore */
                        if (!make_noncontiguous(ef, node->start_cluster, previous))
                                return -EIO;
-                       node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS;
-                       node->flags |= EXFAT_ATTRIB_DIRTY;
+                       node->is_contiguous = false;
+                       node->is_dirty = true;
                }
                }
-               if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next))
+               if (!set_next_cluster(ef, node->is_contiguous, previous, next))
                        return -EIO;
                previous = next;
                allocated++;
        }
 
                        return -EIO;
                previous = next;
                allocated++;
        }
 
-       if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
+       if (!set_next_cluster(ef, node->is_contiguous, previous,
                        EXFAT_CLUSTER_END))
                return -EIO;
        return 0;
                        EXFAT_CLUSTER_END))
                return -EIO;
        return 0;
@@ -327,7 +327,7 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
                        return -EIO;
                }
                previous = exfat_next_cluster(ef, node, last);
                        return -EIO;
                }
                previous = exfat_next_cluster(ef, node, last);
-               if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), last,
+               if (!set_next_cluster(ef, node->is_contiguous, last,
                                EXFAT_CLUSTER_END))
                        return -EIO;
        }
                                EXFAT_CLUSTER_END))
                        return -EIO;
        }
@@ -335,7 +335,7 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
        {
                previous = node->start_cluster;
                node->start_cluster = EXFAT_CLUSTER_FREE;
        {
                previous = node->start_cluster;
                node->start_cluster = EXFAT_CLUSTER_FREE;
-               node->flags |= EXFAT_ATTRIB_DIRTY;
+               node->is_dirty = true;
        }
        node->fptr_index = 0;
        node->fptr_cluster = node->start_cluster;
        }
        node->fptr_index = 0;
        node->fptr_cluster = node->start_cluster;
@@ -350,7 +350,7 @@ static int shrink_file(struct exfat* ef, struct exfat_node* node,
                        return -EIO;
                }
                next = exfat_next_cluster(ef, node, previous);
                        return -EIO;
                }
                next = exfat_next_cluster(ef, node, previous);
-               if (!set_next_cluster(ef, IS_CONTIGUOUS(*node), previous,
+               if (!set_next_cluster(ef, node->is_contiguous, previous,
                                EXFAT_CLUSTER_FREE))
                        return -EIO;
                free_cluster(ef, previous);
                                EXFAT_CLUSTER_FREE))
                        return -EIO;
                free_cluster(ef, previous);
@@ -434,7 +434,7 @@ int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size,
 
        exfat_update_mtime(node);
        node->size = size;
 
        exfat_update_mtime(node);
        node->size = size;
-       node->flags |= EXFAT_ATTRIB_DIRTY;
+       node->is_dirty = true;
        return 0;
 }
 
        return 0;
 }
 
index b590d01902637271a774da3f798427a6ee18e1bd..e4f40de90baf3443a20333a2451dd7153875b3c8 100644 (file)
@@ -4,7 +4,7 @@
        showstopper.
 
        Free exFAT implementation.
        showstopper.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
index 75cb6e674ca415151e6ce7c74fefbc65b2e6b6c6..21b760e2c1e0294d4c4321ecdc82af690cc77107 100644 (file)
@@ -4,7 +4,7 @@
        implementation.
 
        Free exFAT implementation.
        implementation.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
 #include <sys/stat.h>
 #include <sys/types.h>
 
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#define EXFAT_NAME_MAX 256
-#define EXFAT_ATTRIB_CONTIGUOUS 0x10000
-#define EXFAT_ATTRIB_CACHED     0x20000
-#define EXFAT_ATTRIB_DIRTY      0x40000
-#define EXFAT_ATTRIB_UNLINKED   0x80000
-#define IS_CONTIGUOUS(node) (((node).flags & EXFAT_ATTRIB_CONTIGUOUS) != 0)
+#define EXFAT_NAME_MAX 255
+/* UTF-16 encodes code points up to U+FFFF as single 16-bit code units.
+   UTF-8 uses up to 3 bytes (i.e. 8-bit code units) to encode code points
+   up to U+FFFF. One additional character is for null terminator. */
+#define EXFAT_UTF8_NAME_BUFFER_MAX (EXFAT_NAME_MAX * 3 + 1)
+#define EXFAT_UTF8_ENAME_BUFFER_MAX (EXFAT_ENAME_MAX * 3 + 1)
+
 #define SECTOR_SIZE(sb) (1 << (sb).sector_bits)
 #define CLUSTER_SIZE(sb) (SECTOR_SIZE(sb) << (sb).spc_bits)
 #define CLUSTER_INVALID(c) \
 #define SECTOR_SIZE(sb) (1 << (sb).sector_bits)
 #define CLUSTER_SIZE(sb) (SECTOR_SIZE(sb) << (sb).spc_bits)
 #define CLUSTER_INVALID(c) \
@@ -49,7 +50,6 @@
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 #define DIV_ROUND_UP(x, d) (((x) + (d) - 1) / (d))
 #define ROUND_UP(x, d) (DIV_ROUND_UP(x, d) * (d))
 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 #define DIV_ROUND_UP(x, d) (((x) + (d) - 1) / (d))
 #define ROUND_UP(x, d) (DIV_ROUND_UP(x, d) * (d))
-#define UTF8_BYTES(c) ((c) * 6) /* UTF-8 character can occupy up to 6 bytes */
 
 #define BMAP_SIZE(count) (ROUND_UP(count, sizeof(bitmap_t) * 8) / 8)
 #define BMAP_BLOCK(index) ((index) / sizeof(bitmap_t) / 8)
 
 #define BMAP_SIZE(count) (ROUND_UP(count, sizeof(bitmap_t) * 8) / 8)
 #define BMAP_BLOCK(index) ((index) / sizeof(bitmap_t) / 8)
@@ -75,10 +75,14 @@ struct exfat_node
        int references;
        uint32_t fptr_index;
        cluster_t fptr_cluster;
        int references;
        uint32_t fptr_index;
        cluster_t fptr_cluster;
-       cluster_t entry_cluster;
        off_t entry_offset;
        cluster_t start_cluster;
        off_t entry_offset;
        cluster_t start_cluster;
-       int flags;
+       uint16_t attrib;
+       uint8_t continuations;
+       bool is_contiguous : 1;
+       bool is_cached : 1;
+       bool is_dirty : 1;
+       bool is_unlinked : 1;
        uint64_t size;
        time_t mtime, atime;
        le16_t name[EXFAT_NAME_MAX + 1];
        uint64_t size;
        time_t mtime, atime;
        le16_t name[EXFAT_NAME_MAX + 1];
@@ -108,7 +112,7 @@ struct exfat
                bool dirty;
        }
        cmap;
                bool dirty;
        }
        cmap;
-       char label[UTF8_BYTES(EXFAT_ENAME_MAX) + 1];
+       char label[EXFAT_UTF8_ENAME_BUFFER_MAX];
        void* zero_cluster;
        int dmask, fmask;
        uid_t uid;
        void* zero_cluster;
        int dmask, fmask;
        uid_t uid;
@@ -177,14 +181,15 @@ int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b);
 
 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
                struct stat* stbuf);
 
 void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
                struct stat* stbuf);
-void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n);
+void exfat_get_name(const struct exfat_node* node,
+               char buffer[EXFAT_UTF8_NAME_BUFFER_MAX]);
 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry);
 uint16_t exfat_add_checksum(const void* entry, uint16_t sum);
 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry);
 uint16_t exfat_add_checksum(const void* entry, uint16_t sum);
-le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
-               const struct exfat_entry_meta2* meta2, const le16_t* name);
+le16_t exfat_calc_checksum(const struct exfat_entry* entries, int n);
 uint32_t exfat_vbr_start_checksum(const void* sector, size_t size);
 uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum);
 uint32_t exfat_vbr_start_checksum(const void* sector, size_t size);
 uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum);
-le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name);
+le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name,
+               size_t length);
 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb);
 void exfat_print_info(const struct exfat_super_block* sb,
                uint32_t free_clusters);
 void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb);
 void exfat_print_info(const struct exfat_super_block* sb,
                uint32_t free_clusters);
index c155312e15ed928430fc6cf24ced8aadd38a6dbd..84c6f97c453eae8a28d054e539042d299a15de8d 100644 (file)
@@ -3,7 +3,7 @@
        Definitions of structures and constants used in exFAT file system.
 
        Free exFAT implementation.
        Definitions of structures and constants used in exFAT file system.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
@@ -78,6 +78,9 @@ STATIC_ASSERT(sizeof(struct exfat_super_block) == 512);
 #define EXFAT_ENTRY_FILE      (0x05 | EXFAT_ENTRY_VALID)
 #define EXFAT_ENTRY_FILE_INFO (0x00 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED)
 #define EXFAT_ENTRY_FILE_NAME (0x01 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED)
 #define EXFAT_ENTRY_FILE      (0x05 | EXFAT_ENTRY_VALID)
 #define EXFAT_ENTRY_FILE_INFO (0x00 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED)
 #define EXFAT_ENTRY_FILE_NAME (0x01 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED)
+#define EXFAT_ENTRY_FILE_TAIL (0x00 | EXFAT_ENTRY_VALID \
+                                    | EXFAT_ENTRY_CONTINUED \
+                                    | EXFAT_ENTRY_OPTIONAL)
 
 struct exfat_entry                                     /* common container for all entries */
 {
 
 struct exfat_entry                                     /* common container for all entries */
 {
index 60f28e2b88ac42740fc709fe4fb8e0673ae2f035..514383ca4bedb64bf12ed4a28c6e4bbf3c87d6f6 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
@@ -292,7 +292,7 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
        if (CLUSTER_INVALID(cluster))
        {
                exfat_error("invalid cluster 0x%x while reading", cluster);
        if (CLUSTER_INVALID(cluster))
        {
                exfat_error("invalid cluster 0x%x while reading", cluster);
-               return -1;
+               return -EIO;
        }
 
        loffset = offset % CLUSTER_SIZE(*ef->sb);
        }
 
        loffset = offset % CLUSTER_SIZE(*ef->sb);
@@ -302,21 +302,21 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
                if (CLUSTER_INVALID(cluster))
                {
                        exfat_error("invalid cluster 0x%x while reading", cluster);
                if (CLUSTER_INVALID(cluster))
                {
                        exfat_error("invalid cluster 0x%x while reading", cluster);
-                       return -1;
+                       return -EIO;
                }
                lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
                if (exfat_pread(ef->dev, bufp, lsize,
                                        exfat_c2o(ef, cluster) + loffset) < 0)
                {
                        exfat_error("failed to read cluster %#x", cluster);
                }
                lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
                if (exfat_pread(ef->dev, bufp, lsize,
                                        exfat_c2o(ef, cluster) + loffset) < 0)
                {
                        exfat_error("failed to read cluster %#x", cluster);
-                       return -1;
+                       return -EIO;
                }
                bufp += lsize;
                loffset = 0;
                remainder -= lsize;
                cluster = exfat_next_cluster(ef, node, cluster);
        }
                }
                bufp += lsize;
                loffset = 0;
                remainder -= lsize;
                cluster = exfat_next_cluster(ef, node, cluster);
        }
-       if (!ef->ro && !ef->noatime)
+       if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
                exfat_update_atime(node);
        return MIN(size, node->size - offset) - remainder;
 }
                exfat_update_atime(node);
        return MIN(size, node->size - offset) - remainder;
 }
@@ -324,16 +324,23 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
                const void* buffer, size_t size, off_t offset)
 {
 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
                const void* buffer, size_t size, off_t offset)
 {
+       int rc;
        cluster_t cluster;
        const char* bufp = buffer;
        off_t lsize, loffset, remainder;
 
        if (offset > node->size)
        cluster_t cluster;
        const char* bufp = buffer;
        off_t lsize, loffset, remainder;
 
        if (offset > node->size)
-               if (exfat_truncate(ef, node, offset, true) != 0)
-                       return -1;
+       {
+               rc = exfat_truncate(ef, node, offset, true);
+               if (rc != 0)
+                       return rc;
+       }
        if (offset + size > node->size)
        if (offset + size > node->size)
-               if (exfat_truncate(ef, node, offset + size, false) != 0)
-                       return -1;
+       {
+               rc = exfat_truncate(ef, node, offset + size, false);
+               if (rc != 0)
+                       return rc;
+       }
        if (size == 0)
                return 0;
 
        if (size == 0)
                return 0;
 
@@ -341,7 +348,7 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
        if (CLUSTER_INVALID(cluster))
        {
                exfat_error("invalid cluster 0x%x while writing", cluster);
        if (CLUSTER_INVALID(cluster))
        {
                exfat_error("invalid cluster 0x%x while writing", cluster);
-               return -1;
+               return -EIO;
        }
 
        loffset = offset % CLUSTER_SIZE(*ef->sb);
        }
 
        loffset = offset % CLUSTER_SIZE(*ef->sb);
@@ -351,20 +358,23 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
                if (CLUSTER_INVALID(cluster))
                {
                        exfat_error("invalid cluster 0x%x while writing", cluster);
                if (CLUSTER_INVALID(cluster))
                {
                        exfat_error("invalid cluster 0x%x while writing", cluster);
-                       return -1;
+                       return -EIO;
                }
                lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
                if (exfat_pwrite(ef->dev, bufp, lsize,
                                exfat_c2o(ef, cluster) + loffset) < 0)
                {
                        exfat_error("failed to write cluster %#x", cluster);
                }
                lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
                if (exfat_pwrite(ef->dev, bufp, lsize,
                                exfat_c2o(ef, cluster) + loffset) < 0)
                {
                        exfat_error("failed to write cluster %#x", cluster);
-                       return -1;
+                       return -EIO;
                }
                bufp += lsize;
                loffset = 0;
                remainder -= lsize;
                cluster = exfat_next_cluster(ef, node, cluster);
        }
                }
                bufp += lsize;
                loffset = 0;
                remainder -= lsize;
                cluster = exfat_next_cluster(ef, node, cluster);
        }
-       exfat_update_mtime(node);
+       if (!(node->attrib & EXFAT_ATTRIB_DIR))
+               /* directory's mtime should be updated by the caller only when it
+                  creates or removes something in this directory */
+               exfat_update_mtime(node);
        return size - remainder;
 }
        return size - remainder;
 }
index e37b0d8d6f5dfe89dba9bf26bfdeedd1b1fb0066..5d99d30a91ca6b00b12bdb0a5da45e2a80a00460 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
index 30b64108df127d8b112de346fa019cf1e51b36d7..4b27cc54e2679b201080b4c3ffa901147779309f 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
@@ -86,7 +86,7 @@ static int lookup_name(struct exfat* ef, struct exfat_node* parent,
 
        *node = NULL;
 
 
        *node = NULL;
 
-       rc = utf8_to_utf16(buffer, name, EXFAT_NAME_MAX, n);
+       rc = utf8_to_utf16(buffer, name, EXFAT_NAME_MAX + 1, n);
        if (rc != 0)
                return rc;
 
        if (rc != 0)
                return rc;
 
@@ -194,7 +194,7 @@ int exfat_split(struct exfat* ef, struct exfat_node** parent,
                                exfat_put_node(ef, *parent);
                                return -ENOENT;
                        }
                                exfat_put_node(ef, *parent);
                                return -ENOENT;
                        }
-                       rc = utf8_to_utf16(name, p, EXFAT_NAME_MAX, n);
+                       rc = utf8_to_utf16(name, p, EXFAT_NAME_MAX + 1, n);
                        if (rc != 0)
                        {
                                exfat_put_node(ef, *parent);
                        if (rc != 0)
                        {
                                exfat_put_node(ef, *parent);
index b1ce6543884d3155b4552863781e3c79c4c2bba2..f907f290eb41c4e7f985d7c12f6048025bd66169 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
@@ -287,7 +287,7 @@ int exfat_mount(struct exfat* ef, const char* spec, const char* options)
                return -ENOMEM;
        }
        memset(ef->root, 0, sizeof(struct exfat_node));
                return -ENOMEM;
        }
        memset(ef->root, 0, sizeof(struct exfat_node));
-       ef->root->flags = EXFAT_ATTRIB_DIR;
+       ef->root->attrib = EXFAT_ATTRIB_DIR;
        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->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');
index 90002eb9a03f2b0d59a1a44eed3f339d55656a83..a3f00af1ba0d700a43d18397731fbd754eb13640 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
 #include <string.h>
 #include <inttypes.h>
 
 #include <string.h>
 #include <inttypes.h>
 
-/* on-disk nodes iterator */
-struct iterator
-{
-       cluster_t cluster;
-       off_t offset;
-       char* chunk;
-};
+#define EXFAT_ENTRY_NONE (-1)
 
 struct exfat_node* exfat_get_node(struct exfat_node* node)
 {
 
 struct exfat_node* exfat_get_node(struct exfat_node* node)
 {
@@ -43,19 +37,19 @@ struct exfat_node* exfat_get_node(struct exfat_node* node)
 
 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
 {
 
 void exfat_put_node(struct exfat* ef, struct exfat_node* node)
 {
-       char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
+       char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
 
        --node->references;
        if (node->references < 0)
        {
 
        --node->references;
        if (node->references < 0)
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_get_name(node, buffer);
                exfat_bug("reference counter of '%s' is below zero", buffer);
        }
        else if (node->references == 0 && node != ef->root)
        {
                exfat_bug("reference counter of '%s' is below zero", buffer);
        }
        else if (node->references == 0 && node != ef->root)
        {
-               if (node->flags & EXFAT_ATTRIB_DIRTY)
+               if (node->is_dirty)
                {
                {
-                       exfat_get_name(node, buffer, sizeof(buffer) - 1);
+                       exfat_get_name(node, buffer);
                        exfat_warn("dirty node '%s' with zero references", buffer);
                }
        }
                        exfat_warn("dirty node '%s' with zero references", buffer);
                }
        }
@@ -73,7 +67,7 @@ int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
                exfat_bug("unable to cleanup a node with %d references",
                                node->references);
 
                exfat_bug("unable to cleanup a node with %d references",
                                node->references);
 
-       if (node->flags & EXFAT_ATTRIB_UNLINKED)
+       if (node->is_unlinked)
        {
                /* free all clusters and node structure itself */
                rc = exfat_truncate(ef, node, 0, true);
        {
                /* free all clusters and node structure itself */
                rc = exfat_truncate(ef, node, 0, true);
@@ -83,87 +77,44 @@ int exfat_cleanup_node(struct exfat* ef, struct exfat_node* node)
        return rc;
 }
 
        return rc;
 }
 
-/**
- * Cluster + offset from the beginning of the directory to absolute offset.
- */
-static off_t co2o(struct exfat* ef, cluster_t cluster, off_t offset)
-{
-       return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb);
-}
-
-static int opendir(struct exfat* ef, const struct exfat_node* dir,
-               struct iterator* it)
+static int read_entries(struct exfat* ef, struct exfat_node* dir,
+               struct exfat_entry* entries, int n, off_t offset)
 {
 {
-       char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
-
-       if (!(dir->flags & EXFAT_ATTRIB_DIR))
-       {
-               exfat_get_name(dir, buffer, sizeof(buffer) - 1);
-               exfat_bug("'%s' is not a directory", buffer);
-       }
-       if (CLUSTER_INVALID(dir->start_cluster))
-       {
-               exfat_get_name(dir, buffer, sizeof(buffer) - 1);
-               exfat_error("'%s' directory starts with invalid cluster %#x", buffer,
-                               dir->start_cluster);
-               return -EIO;
-       }
-       it->cluster = dir->start_cluster;
-       it->offset = 0;
-       it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
-       if (it->chunk == NULL)
-       {
-               exfat_error("failed to allocate memory for directory cluster");
-               return -ENOMEM;
-       }
-       if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
-                       exfat_c2o(ef, it->cluster)) < 0)
-       {
-               free(it->chunk);
-               exfat_get_name(dir, buffer, sizeof(buffer) - 1);
-               exfat_error("failed to read '%s' directory cluster %#x", buffer,
-                               it->cluster);
+       ssize_t size;
+
+       if (!(dir->attrib & EXFAT_ATTRIB_DIR))
+               exfat_bug("attempted to read entries from a file");
+
+       size = exfat_generic_pread(ef, dir, entries,
+                       sizeof(struct exfat_entry[n]), offset);
+       if (size == sizeof(struct exfat_entry[n]))
+               return 0; /* success */
+       if (size == 0)
+               return -ENOENT;
+       if (size < 0)
                return -EIO;
                return -EIO;
-       }
-       return 0;
+       exfat_error("read %zd bytes instead of %zu bytes", size,
+                       sizeof(struct exfat_entry[n]));
+       return -EIO;
 }
 
 }
 
-static void closedir(struct iterator* it)
+static int write_entries(struct exfat* ef, struct exfat_node* dir,
+               const struct exfat_entry* entries, int n, off_t offset)
 {
 {
-       it->cluster = 0;
-       it->offset = 0;
-       free(it->chunk);
-       it->chunk = NULL;
-}
+       ssize_t size;
 
 
-static bool fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
-               struct iterator* it)
-{
-       /* move iterator to the next entry in the directory */
-       it->offset += sizeof(struct exfat_entry);
-       /* fetch the next cluster if needed */
-       if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
-       {
-               /* reached the end of directory; the caller should check this
-                  condition too */
-               if (it->offset >= parent->size)
-                       return true;
-               it->cluster = exfat_next_cluster(ef, parent, it->cluster);
-               if (CLUSTER_INVALID(it->cluster))
-               {
-                       exfat_error("invalid cluster 0x%x while reading directory",
-                                       it->cluster);
-                       return false;
-               }
-               if (exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
-                               exfat_c2o(ef, it->cluster)) < 0)
-               {
-                       exfat_error("failed to read the next directory cluster %#x",
-                                       it->cluster);
-                       return false;
-               }
-       }
-       return true;
+       if (!(dir->attrib & EXFAT_ATTRIB_DIR))
+               exfat_bug("attempted to write entries into a file");
+
+       size = exfat_generic_pwrite(ef, dir, entries,
+                       sizeof(struct exfat_entry[n]), offset);
+       if (size == sizeof(struct exfat_entry[n]))
+               return 0; /* success */
+       if (size < 0)
+               return -EIO;
+       exfat_error("wrote %zd bytes instead of %zu bytes", size,
+                       sizeof(struct exfat_entry[n]));
+       return -EIO;
 }
 
 static struct exfat_node* allocate_node(void)
 }
 
 static struct exfat_node* allocate_node(void)
@@ -181,7 +132,8 @@ static struct exfat_node* allocate_node(void)
 static void init_node_meta1(struct exfat_node* node,
                const struct exfat_entry_meta1* meta1)
 {
 static void init_node_meta1(struct exfat_node* node,
                const struct exfat_entry_meta1* meta1)
 {
-       node->flags = le16_to_cpu(meta1->attrib);
+       node->attrib = le16_to_cpu(meta1->attrib);
+       node->continuations = meta1->continuations;
        node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
                        meta1->mtime_cs);
        /* there is no centiseconds field for atime */
        node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime,
                        meta1->mtime_cs);
        /* there is no centiseconds field for atime */
@@ -194,32 +146,80 @@ static void init_node_meta2(struct exfat_node* node,
        node->size = le64_to_cpu(meta2->size);
        node->start_cluster = le32_to_cpu(meta2->start_cluster);
        node->fptr_cluster = node->start_cluster;
        node->size = le64_to_cpu(meta2->size);
        node->start_cluster = le32_to_cpu(meta2->start_cluster);
        node->fptr_cluster = node->start_cluster;
-       if (meta2->flags & EXFAT_FLAG_CONTIGUOUS)
-               node->flags |= EXFAT_ATTRIB_CONTIGUOUS;
+       node->is_contiguous = ((meta2->flags & EXFAT_FLAG_CONTIGUOUS) != 0);
 }
 
 }
 
-static const struct exfat_entry* get_entry_ptr(const struct exfat* ef,
-               const struct iterator* it)
+static void init_node_name(struct exfat_node* node,
+               const struct exfat_entry* entries, int n)
 {
 {
-       return (const struct exfat_entry*)
-                       (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb));
+       int i;
+
+       for (i = 0; i < n; i++)
+               memcpy(node->name + i * EXFAT_ENAME_MAX,
+                               ((const struct exfat_entry_name*) &entries[i])->name,
+                               EXFAT_ENAME_MAX * sizeof(le16_t));
+}
+
+static bool check_entries(const struct exfat_entry* entry, int n)
+{
+       int previous = EXFAT_ENTRY_NONE;
+       int current;
+       int i;
+
+       /* check transitions between entries types */
+       for (i = 0; i < n + 1; previous = current, i++)
+       {
+               bool valid = false;
+
+               current = (i < n) ? entry[i].type : EXFAT_ENTRY_NONE;
+               switch (previous)
+               {
+               case EXFAT_ENTRY_NONE:
+                       valid = (current == EXFAT_ENTRY_FILE);
+                       break;
+               case EXFAT_ENTRY_FILE:
+                       valid = (current == EXFAT_ENTRY_FILE_INFO);
+                       break;
+               case EXFAT_ENTRY_FILE_INFO:
+                       valid = (current == EXFAT_ENTRY_FILE_NAME);
+                       break;
+               case EXFAT_ENTRY_FILE_NAME:
+                       valid = (current == EXFAT_ENTRY_FILE_NAME ||
+                                current == EXFAT_ENTRY_NONE ||
+                                current >= EXFAT_ENTRY_FILE_TAIL);
+                       break;
+               case EXFAT_ENTRY_FILE_TAIL ... 0xff:
+                       valid = (current >= EXFAT_ENTRY_FILE_TAIL ||
+                                current == EXFAT_ENTRY_NONE);
+                       break;
+               }
+
+               if (!valid)
+               {
+                       exfat_error("unexpected entry type %#x after %#x at %d/%d",
+                                       current, previous, i, n);
+                       return false;
+               }
+       }
+       return true;
 }
 
 }
 
-static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
-               uint16_t reference_checksum, uint64_t valid_size, int cluster_size)
+static bool check_node(const struct exfat_node* node, le16_t actual_checksum,
+               int cluster_size, const struct exfat_entry_meta1* meta1,
+               const struct exfat_entry_meta2* meta2)
 {
 {
-       char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
+       char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
        bool ret = true;
 
        /*
           Validate checksum first. If it's invalid all other fields probably
           contain just garbage.
        */
        bool ret = true;
 
        /*
           Validate checksum first. If it's invalid all other fields probably
           contain just garbage.
        */
-       if (actual_checksum != reference_checksum)
+       if (le16_to_cpu(actual_checksum) != le16_to_cpu(meta1->checksum))
        {
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_get_name(node, buffer);
                exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
                exfat_error("'%s' has invalid checksum (%#hx != %#hx)", buffer,
-                               actual_checksum, reference_checksum);
+                               le16_to_cpu(actual_checksum), le16_to_cpu(meta1->checksum));
                ret = false;
        }
 
                ret = false;
        }
 
@@ -229,48 +229,49 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
           cannot be greater than file size. See SetFileValidData() function
           description in MSDN.
        */
           cannot be greater than file size. See SetFileValidData() function
           description in MSDN.
        */
-       if (valid_size > node->size)
+       if (le64_to_cpu(meta2->valid_size) > node->size)
        {
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_get_name(node, buffer);
                exfat_error("'%s' has valid size (%"PRIu64") greater than size "
                exfat_error("'%s' has valid size (%"PRIu64") greater than size "
-                               "(%"PRIu64")", buffer, valid_size, node->size);
+                               "(%"PRIu64")", buffer, le64_to_cpu(meta2->valid_size),
+                               node->size);
                ret = false;
        }
 
        /*
           Empty file must have zero start cluster. Non-empty file must start
           with a valid cluster. Directories cannot be empty (i.e. must always
                ret = false;
        }
 
        /*
           Empty file must have zero start cluster. Non-empty file must start
           with a valid cluster. Directories cannot be empty (i.e. must always
-          have a valid start cluster), but we will check this later in opendir()
-          to give user a chance to read current directory.
+          have a valid start cluster), but we will check this later while
+          reading that directory to give user a chance to read this directory.
        */
        if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
        {
        */
        if (node->size == 0 && node->start_cluster != EXFAT_CLUSTER_FREE)
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_get_name(node, buffer);
                exfat_error("'%s' is empty but start cluster is %#x", buffer,
                                node->start_cluster);
                ret = false;
        }
        if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
        {
                exfat_error("'%s' is empty but start cluster is %#x", buffer,
                                node->start_cluster);
                ret = false;
        }
        if (node->size > 0 && CLUSTER_INVALID(node->start_cluster))
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_get_name(node, buffer);
                exfat_error("'%s' points to invalid cluster %#x", buffer,
                                node->start_cluster);
                ret = false;
        }
 
        /* Empty file or directory must be marked as non-contiguous. */
                exfat_error("'%s' points to invalid cluster %#x", buffer,
                                node->start_cluster);
                ret = false;
        }
 
        /* Empty file or directory must be marked as non-contiguous. */
-       if (node->size == 0 && (node->flags & EXFAT_ATTRIB_CONTIGUOUS))
+       if (node->size == 0 && node->is_contiguous)
        {
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
-               exfat_error("'%s' is empty but marked as contiguous (%#x)", buffer,
-                               node->flags);
+               exfat_get_name(node, buffer);
+               exfat_error("'%s' is empty but marked as contiguous (%#hx)", buffer,
+                               node->attrib);
                ret = false;
        }
 
        /* Directory size must be aligned on at cluster boundary. */
                ret = false;
        }
 
        /* Directory size must be aligned on at cluster boundary. */
-       if ((node->flags & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
+       if ((node->attrib & EXFAT_ATTRIB_DIR) && node->size % cluster_size != 0)
        {
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_get_name(node, buffer);
                exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
                                node->size, cluster_size);
                ret = false;
                exfat_error("'%s' directory size %"PRIu64" is not divisible by %d", buffer,
                                node->size, cluster_size);
                ret = false;
@@ -279,6 +280,74 @@ static bool check_node(const struct exfat_node* node, uint16_t actual_checksum,
        return ret;
 }
 
        return ret;
 }
 
+static int parse_file_entries(struct exfat* ef, struct exfat_node* parent,
+               struct exfat_node* node, const struct exfat_entry* entries, int n)
+{
+       const struct exfat_entry_meta1* meta1;
+       const struct exfat_entry_meta2* meta2;
+       int mandatory_entries;
+
+       if (!check_entries(entries, n))
+               return -EIO;
+
+       meta1 = (const struct exfat_entry_meta1*) &entries[0];
+       if (meta1->continuations < 2)
+       {
+               exfat_error("too few continuations (%hhu)", meta1->continuations);
+               return -EIO;
+       }
+       meta2 = (const struct exfat_entry_meta2*) &entries[1];
+       if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
+       {
+               exfat_error("unknown flags in meta2 (%#hhx)", meta2->flags);
+               return -EIO;
+       }
+       mandatory_entries = 2 + DIV_ROUND_UP(meta2->name_length, EXFAT_ENAME_MAX);
+       if (meta1->continuations < mandatory_entries - 1)
+       {
+               exfat_error("too few continuations (%hhu < %d)",
+                               meta1->continuations, mandatory_entries - 1);
+               return -EIO;
+       }
+
+       init_node_meta1(node, meta1);
+       init_node_meta2(node, meta2);
+       init_node_name(node, entries + 2, mandatory_entries - 2);
+
+       if (!check_node(node, exfat_calc_checksum(entries, n),
+                       CLUSTER_SIZE(*ef->sb), meta1, meta2))
+               return -EIO;
+
+       return 0;
+}
+
+static int parse_file_entry(struct exfat* ef, struct exfat_node* parent,
+               struct exfat_node** node, off_t* offset, int n)
+{
+       struct exfat_entry entries[n];
+       int rc;
+
+       rc = read_entries(ef, parent, entries, n, *offset);
+       if (rc != 0)
+               return rc;
+
+       /* a new node has zero references */
+       *node = allocate_node();
+       if (*node == NULL)
+               return -ENOMEM;
+       (*node)->entry_offset = *offset;
+
+       rc = parse_file_entries(ef, parent, *node, entries, n);
+       if (rc != 0)
+       {
+               free(*node);
+               return rc;
+       }
+
+       *offset += sizeof(struct exfat_entry[n]);
+       return 0;
+}
+
 static void decompress_upcase(uint16_t* output, const le16_t* source,
                size_t size)
 {
 static void decompress_upcase(uint16_t* output, const le16_t* source,
                size_t size)
 {
@@ -300,135 +369,43 @@ static void decompress_upcase(uint16_t* output, const le16_t* source,
 }
 
 /*
 }
 
 /*
- * Reads one entry in directory at position pointed by iterator and fills
- * node structure.
+ * Read one entry in a directory at offset position and build a new node
+ * structure.
  */
  */
-static int readdir(struct exfat* ef, const struct exfat_node* parent,
-               struct exfat_node** node, struct iterator* it)
+static int readdir(struct exfat* ef, struct exfat_node* parent,
+               struct exfat_node** node, off_t* offset)
 {
 {
-       int rc = -EIO;
-       const struct exfat_entry* entry;
+       int rc;
+       struct exfat_entry entry;
        const struct exfat_entry_meta1* meta1;
        const struct exfat_entry_meta1* meta1;
-       const struct exfat_entry_meta2* meta2;
-       const struct exfat_entry_name* file_name;
        const struct exfat_entry_upcase* upcase;
        const struct exfat_entry_bitmap* bitmap;
        const struct exfat_entry_label* label;
        const struct exfat_entry_upcase* upcase;
        const struct exfat_entry_bitmap* bitmap;
        const struct exfat_entry_label* label;
-       uint8_t continuations = 0;
-       le16_t* namep = NULL;
-       uint16_t reference_checksum = 0;
-       uint16_t actual_checksum = 0;
-       uint64_t valid_size = 0;
        uint64_t upcase_size = 0;
        le16_t* upcase_comp = NULL;
 
        uint64_t upcase_size = 0;
        le16_t* upcase_comp = NULL;
 
-       *node = NULL;
-
        for (;;)
        {
        for (;;)
        {
-               if (it->offset >= parent->size)
-               {
-                       if (continuations != 0)
-                       {
-                               exfat_error("expected %hhu continuations", continuations);
-                               goto error;
-                       }
-                       return -ENOENT; /* that's OK, means end of directory */
-               }
+               rc = read_entries(ef, parent, &entry, 1, *offset);
+               if (rc != 0)
+                       return rc;
 
 
-               entry = get_entry_ptr(ef, it);
-               switch (entry->type)
+               switch (entry.type)
                {
                case EXFAT_ENTRY_FILE:
                {
                case EXFAT_ENTRY_FILE:
-                       if (continuations != 0)
-                       {
-                               exfat_error("expected %hhu continuations before new entry",
-                                               continuations);
-                               goto error;
-                       }
-                       meta1 = (const struct exfat_entry_meta1*) entry;
-                       continuations = meta1->continuations;
-                       /* each file entry must have at least 2 continuations:
-                          info and name */
-                       if (continuations < 2)
-                       {
-                               exfat_error("too few continuations (%hhu)", continuations);
-                               goto error;
-                       }
-                       if (continuations > 1 +
-                                       DIV_ROUND_UP(EXFAT_NAME_MAX, EXFAT_ENAME_MAX))
-                       {
-                               exfat_error("too many continuations (%hhu)", continuations);
-                               goto error;
-                       }
-                       reference_checksum = le16_to_cpu(meta1->checksum);
-                       actual_checksum = exfat_start_checksum(meta1);
-                       *node = allocate_node();
-                       if (*node == NULL)
-                       {
-                               rc = -ENOMEM;
-                               goto error;
-                       }
-                       /* new node has zero reference counter */
-                       (*node)->entry_cluster = it->cluster;
-                       (*node)->entry_offset = it->offset;
-                       init_node_meta1(*node, meta1);
-                       namep = (*node)->name;
-                       break;
-
-               case EXFAT_ENTRY_FILE_INFO:
-                       if (continuations < 2)
-                       {
-                               exfat_error("unexpected continuation (%hhu)",
-                                               continuations);
-                               goto error;
-                       }
-                       meta2 = (const struct exfat_entry_meta2*) entry;
-                       if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
-                       {
-                               exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
-                               goto error;
-                       }
-                       init_node_meta2(*node, meta2);
-                       actual_checksum = exfat_add_checksum(entry, actual_checksum);
-                       valid_size = le64_to_cpu(meta2->valid_size);
-                       --continuations;
-                       break;
-
-               case EXFAT_ENTRY_FILE_NAME:
-                       if (continuations == 0)
-                       {
-                               exfat_error("unexpected continuation");
-                               goto error;
-                       }
-                       file_name = (const struct exfat_entry_name*) entry;
-                       actual_checksum = exfat_add_checksum(entry, actual_checksum);
-
-                       memcpy(namep, file_name->name,
-                                       MIN(EXFAT_ENAME_MAX,
-                                               ((*node)->name + EXFAT_NAME_MAX - namep)) *
-                                       sizeof(le16_t));
-                       namep += EXFAT_ENAME_MAX;
-                       if (--continuations == 0)
-                       {
-                               if (!check_node(*node, actual_checksum, reference_checksum,
-                                               valid_size, CLUSTER_SIZE(*ef->sb)))
-                                       goto error;
-                               if (!fetch_next_entry(ef, parent, it))
-                                       goto error;
-                               return 0; /* entry completed */
-                       }
-                       break;
+                       meta1 = (const struct exfat_entry_meta1*) &entry;
+                       return parse_file_entry(ef, parent, node, offset,
+                                       1 + meta1->continuations);
 
                case EXFAT_ENTRY_UPCASE:
                        if (ef->upcase != NULL)
                                break;
 
                case EXFAT_ENTRY_UPCASE:
                        if (ef->upcase != NULL)
                                break;
-                       upcase = (const struct exfat_entry_upcase*) entry;
+                       upcase = (const struct exfat_entry_upcase*) &entry;
                        if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
                        {
                                exfat_error("invalid cluster 0x%x in upcase table",
                                                le32_to_cpu(upcase->start_cluster));
                        if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
                        {
                                exfat_error("invalid cluster 0x%x in upcase table",
                                                le32_to_cpu(upcase->start_cluster));
-                               goto error;
+                               return -EIO;
                        }
                        upcase_size = le64_to_cpu(upcase->size);
                        if (upcase_size == 0 ||
                        }
                        upcase_size = le64_to_cpu(upcase->size);
                        if (upcase_size == 0 ||
@@ -437,15 +414,14 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                        {
                                exfat_error("bad upcase table size (%"PRIu64" bytes)",
                                                upcase_size);
                        {
                                exfat_error("bad upcase table size (%"PRIu64" bytes)",
                                                upcase_size);
-                               goto error;
+                               return -EIO;
                        }
                        upcase_comp = malloc(upcase_size);
                        if (upcase_comp == NULL)
                        {
                                exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
                                                upcase_size);
                        }
                        upcase_comp = malloc(upcase_size);
                        if (upcase_comp == NULL)
                        {
                                exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
                                                upcase_size);
-                               rc = -ENOMEM;
-                               goto error;
+                               return -ENOMEM;
                        }
 
                        /* read compressed upcase table */
                        }
 
                        /* read compressed upcase table */
@@ -457,7 +433,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                                                "(%"PRIu64" bytes starting at cluster %#x)",
                                                upcase_size,
                                                le32_to_cpu(upcase->start_cluster));
                                                "(%"PRIu64" bytes starting at cluster %#x)",
                                                upcase_size,
                                                le32_to_cpu(upcase->start_cluster));
-                               goto error;
+                               return -EIO;
                        }
 
                        /* decompress upcase table */
                        }
 
                        /* decompress upcase table */
@@ -466,8 +442,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                        {
                                free(upcase_comp);
                                exfat_error("failed to allocate decompressed upcase table");
                        {
                                free(upcase_comp);
                                exfat_error("failed to allocate decompressed upcase table");
-                               rc = -ENOMEM;
-                               goto error;
+                               return -ENOMEM;
                        }
                        decompress_upcase(ef->upcase, upcase_comp,
                                        upcase_size / sizeof(uint16_t));
                        }
                        decompress_upcase(ef->upcase, upcase_comp,
                                        upcase_size / sizeof(uint16_t));
@@ -475,13 +450,13 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                        break;
 
                case EXFAT_ENTRY_BITMAP:
                        break;
 
                case EXFAT_ENTRY_BITMAP:
-                       bitmap = (const struct exfat_entry_bitmap*) entry;
+                       bitmap = (const struct exfat_entry_bitmap*) &entry;
                        ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
                        if (CLUSTER_INVALID(ef->cmap.start_cluster))
                        {
                                exfat_error("invalid cluster 0x%x in clusters bitmap",
                                                ef->cmap.start_cluster);
                        ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
                        if (CLUSTER_INVALID(ef->cmap.start_cluster))
                        {
                                exfat_error("invalid cluster 0x%x in clusters bitmap",
                                                ef->cmap.start_cluster);
-                               goto error;
+                               return -EIO;
                        }
                        ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
                                EXFAT_FIRST_DATA_CLUSTER;
                        }
                        ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
                                EXFAT_FIRST_DATA_CLUSTER;
@@ -491,7 +466,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                                                " (expected at least %u)",
                                                le64_to_cpu(bitmap->size),
                                                DIV_ROUND_UP(ef->cmap.size, 8));
                                                " (expected at least %u)",
                                                le64_to_cpu(bitmap->size),
                                                DIV_ROUND_UP(ef->cmap.size, 8));
-                               goto error;
+                               return -EIO;
                        }
                        /* FIXME bitmap can be rather big, up to 512 MB */
                        ef->cmap.chunk_size = ef->cmap.size;
                        }
                        /* FIXME bitmap can be rather big, up to 512 MB */
                        ef->cmap.chunk_size = ef->cmap.size;
@@ -500,8 +475,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                        {
                                exfat_error("failed to allocate clusters bitmap chunk "
                                                "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
                        {
                                exfat_error("failed to allocate clusters bitmap chunk "
                                                "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
-                               rc = -ENOMEM;
-                               goto error;
+                               return -ENOMEM;
                        }
 
                        if (exfat_pread(ef->dev, ef->cmap.chunk,
                        }
 
                        if (exfat_pread(ef->dev, ef->cmap.chunk,
@@ -511,66 +485,45 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
                                exfat_error("failed to read clusters bitmap "
                                                "(%"PRIu64" bytes starting at cluster %#x)",
                                                le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
                                exfat_error("failed to read clusters bitmap "
                                                "(%"PRIu64" bytes starting at cluster %#x)",
                                                le64_to_cpu(bitmap->size), ef->cmap.start_cluster);
-                               goto error;
+                               return -EIO;
                        }
                        break;
 
                case EXFAT_ENTRY_LABEL:
                        }
                        break;
 
                case EXFAT_ENTRY_LABEL:
-                       label = (const struct exfat_entry_label*) entry;
+                       label = (const struct exfat_entry_label*) &entry;
                        if (label->length > EXFAT_ENAME_MAX)
                        {
                                exfat_error("too long label (%hhu chars)", label->length);
                        if (label->length > EXFAT_ENAME_MAX)
                        {
                                exfat_error("too long label (%hhu chars)", label->length);
-                               goto error;
+                               return -EIO;
                        }
                        if (utf16_to_utf8(ef->label, label->name,
                        }
                        if (utf16_to_utf8(ef->label, label->name,
-                                               sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
-                               goto error;
+                                               sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
+                               return -EIO;
                        break;
 
                default:
                        break;
 
                default:
-                       if (!(entry->type & EXFAT_ENTRY_VALID))
+                       if (!(entry.type & EXFAT_ENTRY_VALID))
                                break; /* deleted entry, ignore it */
                                break; /* deleted entry, ignore it */
-                       if (!(entry->type & EXFAT_ENTRY_OPTIONAL))
-                       {
-                               exfat_error("unknown entry type %#hhx", entry->type);
-                               goto error;
-                       }
-                       /* optional entry, warn and skip */
-                       exfat_warn("unknown entry type %#hhx", entry->type);
-                       if (continuations == 0)
-                       {
-                               exfat_error("unexpected continuation");
-                               goto error;
-                       }
-                       --continuations;
-                       break;
-               }
 
 
-               if (!fetch_next_entry(ef, parent, it))
-                       goto error;
+                       exfat_error("unknown entry type %#hhx", entry.type);
+                       return -EIO;
+               }
+               *offset += sizeof(entry);
        }
        /* we never reach here */
        }
        /* we never reach here */
-
-error:
-       free(*node);
-       *node = NULL;
-       return rc;
 }
 
 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
 {
 }
 
 int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
 {
-       struct iterator it;
+       off_t offset = 0;
        int rc;
        struct exfat_node* node;
        struct exfat_node* current = NULL;
 
        int rc;
        struct exfat_node* node;
        struct exfat_node* current = NULL;
 
-       if (dir->flags & EXFAT_ATTRIB_CACHED)
+       if (dir->is_cached)
                return 0; /* already cached */
 
                return 0; /* already cached */
 
-       rc = opendir(ef, dir, &it);
-       if (rc != 0)
-               return rc;
-       while ((rc = readdir(ef, dir, &node, &it)) == 0)
+       while ((rc = readdir(ef, dir, &node, &offset)) == 0)
        {
                node->parent = dir;
                if (current != NULL)
        {
                node->parent = dir;
                if (current != NULL)
@@ -583,7 +536,6 @@ int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
 
                current = node;
        }
 
                current = node;
        }
-       closedir(&it);
 
        if (rc != -ENOENT)
        {
 
        if (rc != -ENOENT)
        {
@@ -597,7 +549,7 @@ int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
                return rc;
        }
 
                return rc;
        }
 
-       dir->flags |= EXFAT_ATTRIB_CACHED;
+       dir->is_cached = true;
        return 0;
 }
 
        return 0;
 }
 
@@ -627,7 +579,7 @@ static void tree_detach(struct exfat_node* node)
 
 static void reset_cache(struct exfat* ef, struct exfat_node* node)
 {
 
 static void reset_cache(struct exfat* ef, struct exfat_node* node)
 {
-       char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
+       char buffer[EXFAT_UTF8_NAME_BUFFER_MAX];
 
        while (node->child)
        {
 
        while (node->child)
        {
@@ -636,16 +588,16 @@ static void reset_cache(struct exfat* ef, struct exfat_node* node)
                tree_detach(p);
                free(p);
        }
                tree_detach(p);
                free(p);
        }
-       node->flags &= ~EXFAT_ATTRIB_CACHED;
+       node->is_cached = false;
        if (node->references != 0)
        {
        if (node->references != 0)
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_get_name(node, buffer);
                exfat_warn("non-zero reference counter (%d) for '%s'",
                                node->references, buffer);
        }
                exfat_warn("non-zero reference counter (%d) for '%s'",
                                node->references, buffer);
        }
-       if (node != ef->root && (node->flags & EXFAT_ATTRIB_DIRTY))
+       if (node != ef->root && node->is_dirty)
        {
        {
-               exfat_get_name(node, buffer, sizeof(buffer) - 1);
+               exfat_get_name(node, buffer);
                exfat_bug("node '%s' is dirty", buffer);
        }
        while (node->references)
                exfat_bug("node '%s' is dirty", buffer);
        }
        while (node->references)
@@ -657,32 +609,14 @@ void exfat_reset_cache(struct exfat* ef)
        reset_cache(ef, ef->root);
 }
 
        reset_cache(ef, ef->root);
 }
 
-static bool next_entry(struct exfat* ef, const struct exfat_node* parent,
-               cluster_t* cluster, off_t* offset)
-{
-       *offset += sizeof(struct exfat_entry);
-       if (*offset % CLUSTER_SIZE(*ef->sb) == 0)
-       {
-               *cluster = exfat_next_cluster(ef, parent, *cluster);
-               if (CLUSTER_INVALID(*cluster))
-               {
-                       exfat_error("invalid cluster %#x while getting next entry",
-                                       *cluster);
-                       return false;
-               }
-       }
-       return true;
-}
-
 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
 {
 int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
 {
-       cluster_t cluster;
-       off_t offset;
-       off_t meta1_offset, meta2_offset;
-       struct exfat_entry_meta1 meta1;
-       struct exfat_entry_meta2 meta2;
+       struct exfat_entry entries[1 + node->continuations];
+       struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
+       struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
+       int rc;
 
 
-       if (!(node->flags & EXFAT_ATTRIB_DIRTY))
+       if (!node->is_dirty)
                return 0; /* no need to flush */
 
        if (ef->ro)
                return 0; /* no need to flush */
 
        if (ef->ro)
@@ -691,92 +625,65 @@ int exfat_flush_node(struct exfat* ef, struct exfat_node* node)
        if (node->parent == NULL)
                return 0; /* do not flush unlinked node */
 
        if (node->parent == NULL)
                return 0; /* do not flush unlinked node */
 
-       cluster = node->entry_cluster;
-       offset = node->entry_offset;
-       meta1_offset = co2o(ef, cluster, offset);
-       if (!next_entry(ef, node->parent, &cluster, &offset))
+       rc = read_entries(ef, node->parent, entries, 1 + node->continuations,
+                       node->entry_offset);
+       if (rc != 0)
+               return rc;
+       if (!check_entries(entries, 1 + node->continuations))
                return -EIO;
                return -EIO;
-       meta2_offset = co2o(ef, cluster, offset);
 
 
-       if (exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
-       {
-               exfat_error("failed to read meta1 entry on flush");
-               return -EIO;
-       }
-       if (meta1.type != EXFAT_ENTRY_FILE)
-               exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
-       meta1.attrib = cpu_to_le16(node->flags);
-       exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
-       exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);
-
-       if (exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
-       {
-               exfat_error("failed to read meta2 entry on flush");
-               return -EIO;
-       }
-       if (meta2.type != EXFAT_ENTRY_FILE_INFO)
-               exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
-       meta2.size = meta2.valid_size = cpu_to_le64(node->size);
-       meta2.start_cluster = cpu_to_le32(node->start_cluster);
-       meta2.flags = EXFAT_FLAG_ALWAYS1;
+       meta1->attrib = cpu_to_le16(node->attrib);
+       exfat_unix2exfat(node->mtime, &meta1->mdate, &meta1->mtime,
+                       &meta1->mtime_cs);
+       exfat_unix2exfat(node->atime, &meta1->adate, &meta1->atime, NULL);
+       meta2->size = meta2->valid_size = cpu_to_le64(node->size);
+       meta2->start_cluster = cpu_to_le32(node->start_cluster);
+       meta2->flags = EXFAT_FLAG_ALWAYS1;
        /* empty files must not be marked as contiguous */
        /* empty files must not be marked as contiguous */
-       if (node->size != 0 && IS_CONTIGUOUS(*node))
-               meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
+       if (node->size != 0 && node->is_contiguous)
+               meta2->flags |= EXFAT_FLAG_CONTIGUOUS;
        /* name hash remains unchanged, no need to recalculate it */
 
        /* name hash remains unchanged, no need to recalculate it */
 
-       meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
-
-       if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset) < 0)
-       {
-               exfat_error("failed to write meta1 entry on flush");
-               return -EIO;
-       }
-       if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset) < 0)
-       {
-               exfat_error("failed to write meta2 entry on flush");
-               return -EIO;
-       }
+       meta1->checksum = exfat_calc_checksum(entries, 1 + node->continuations);
+       rc = write_entries(ef, node->parent, entries, 1 + node->continuations,
+                       node->entry_offset);
+       if (rc != 0)
+               return rc;
 
 
-       node->flags &= ~EXFAT_ATTRIB_DIRTY;
+       node->is_dirty = false;
        return exfat_flush(ef);
 }
 
        return exfat_flush(ef);
 }
 
-static bool erase_entry(struct exfat* ef, struct exfat_node* node)
+static int erase_entries(struct exfat* ef, struct exfat_node* dir, int n,
+               off_t offset)
 {
 {
-       cluster_t cluster = node->entry_cluster;
-       off_t offset = node->entry_offset;
-       int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX);
-       uint8_t entry_type;
+       struct exfat_entry entries[n];
+       int rc;
+       int i;
 
 
-       entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID;
-       if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
-       {
-               exfat_error("failed to erase meta1 entry");
-               return false;
-       }
+       rc = read_entries(ef, dir, entries, n, offset);
+       if (rc != 0)
+               return rc;
+       for (i = 0; i < n; i++)
+               entries[i].type &= ~EXFAT_ENTRY_VALID;
+       return write_entries(ef, dir, entries, n, offset);
+}
 
 
-       if (!next_entry(ef, node->parent, &cluster, &offset))
-               return false;
-       entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID;
-       if (exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)) < 0)
-       {
-               exfat_error("failed to erase meta2 entry");
-               return false;
-       }
+static int erase_node(struct exfat* ef, struct exfat_node* node)
+{
+       int rc;
 
 
-       while (name_entries--)
+       exfat_get_node(node->parent);
+       rc = erase_entries(ef, node->parent, 1 + node->continuations,
+                       node->entry_offset);
+       if (rc != 0)
        {
        {
-               if (!next_entry(ef, node->parent, &cluster, &offset))
-                       return false;
-               entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID;
-               if (exfat_pwrite(ef->dev, &entry_type, 1,
-                               co2o(ef, cluster, offset)) < 0)
-               {
-                       exfat_error("failed to erase name entry");
-                       return false;
-               }
+               exfat_put_node(ef, node->parent);
+               return rc;
        }
        }
-       return true;
+       rc = exfat_flush_node(ef, node->parent);
+       exfat_put_node(ef, node->parent);
+       return rc;
 }
 
 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
 }
 
 static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
@@ -787,9 +694,9 @@ static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
        uint64_t entries = 0;
        uint64_t new_size;
 
        uint64_t entries = 0;
        uint64_t new_size;
 
-       if (!(dir->flags & EXFAT_ATTRIB_DIR))
+       if (!(dir->attrib & EXFAT_ATTRIB_DIR))
                exfat_bug("attempted to shrink a file");
                exfat_bug("attempted to shrink a file");
-       if (!(dir->flags & EXFAT_ATTRIB_CACHED))
+       if (!dir->is_cached)
                exfat_bug("attempted to shrink uncached directory");
 
        for (last_node = node = dir->child; node; node = node->next)
                exfat_bug("attempted to shrink uncached directory");
 
        for (last_node = node = dir->child; node; node = node->next)
@@ -831,21 +738,22 @@ static int delete(struct exfat* ef, struct exfat_node* node)
        int rc;
 
        exfat_get_node(parent);
        int rc;
 
        exfat_get_node(parent);
-       if (!erase_entry(ef, node))
+       rc = erase_node(ef, node);
+       if (rc != 0)
        {
                exfat_put_node(ef, parent);
        {
                exfat_put_node(ef, parent);
-               return -EIO;
+               return rc;
        }
        }
-       exfat_update_mtime(parent);
        tree_detach(node);
        rc = shrink_directory(ef, parent, deleted_offset);
        tree_detach(node);
        rc = shrink_directory(ef, parent, deleted_offset);
-       node->flags |= EXFAT_ATTRIB_UNLINKED;
+       node->is_unlinked = true;
        if (rc != 0)
        {
                exfat_flush_node(ef, parent);
                exfat_put_node(ef, parent);
                return rc;
        }
        if (rc != 0)
        {
                exfat_flush_node(ef, parent);
                exfat_put_node(ef, parent);
                return rc;
        }
+       exfat_update_mtime(parent);
        rc = exfat_flush_node(ef, parent);
        exfat_put_node(ef, parent);
        return rc;
        rc = exfat_flush_node(ef, parent);
        exfat_put_node(ef, parent);
        return rc;
@@ -853,7 +761,7 @@ static int delete(struct exfat* ef, struct exfat_node* node)
 
 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
 {
 
 int exfat_unlink(struct exfat* ef, struct exfat_node* node)
 {
-       if (node->flags & EXFAT_ATTRIB_DIR)
+       if (node->attrib & EXFAT_ATTRIB_DIR)
                return -EISDIR;
        return delete(ef, node);
 }
                return -EISDIR;
        return delete(ef, node);
 }
@@ -862,7 +770,7 @@ int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
 {
        int rc;
 
 {
        int rc;
 
-       if (!(node->flags & EXFAT_ATTRIB_DIR))
+       if (!(node->attrib & EXFAT_ATTRIB_DIR))
                return -ENOTDIR;
        /* check that directory is empty */
        rc = exfat_cache_directory(ef, node);
                return -ENOTDIR;
        /* check that directory is empty */
        rc = exfat_cache_directory(ef, node);
@@ -873,130 +781,143 @@ int exfat_rmdir(struct exfat* ef, struct exfat_node* node)
        return delete(ef, node);
 }
 
        return delete(ef, node);
 }
 
-static int grow_directory(struct exfat* ef, struct exfat_node* dir,
-               uint64_t asize, uint32_t difference)
+static int check_slot(struct exfat* ef, struct exfat_node* dir, off_t offset,
+               int n)
 {
 {
-       return exfat_truncate(ef, dir,
-                       DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb))
-                               * CLUSTER_SIZE(*ef->sb), true);
+       struct exfat_entry entries[n];
+       int rc;
+       size_t i;
+
+       /* Root directory contains entries, that don't have any nodes associated
+          with them (clusters bitmap, upper case table, label). We need to be
+          careful not to overwrite them. */
+       if (dir != ef->root)
+               return 0;
+
+       rc = read_entries(ef, dir, entries, n, offset);
+       if (rc != 0)
+               return rc;
+       for (i = 0; i < n; i++)
+               if (entries[i].type & EXFAT_ENTRY_VALID)
+                       return -EINVAL;
+       return 0;
 }
 
 static int find_slot(struct exfat* ef, struct exfat_node* dir,
 }
 
 static int find_slot(struct exfat* ef, struct exfat_node* dir,
-               cluster_t* cluster, off_t* offset, int subentries)
+               off_t* offset, int n)
 {
 {
-       struct iterator it;
-       int rc;
-       const struct exfat_entry* entry;
+       bitmap_t* dmap;
+       struct exfat_node* p;
+       size_t i;
        int contiguous = 0;
 
        int contiguous = 0;
 
-       rc = opendir(ef, dir, &it);
-       if (rc != 0)
-               return rc;
-       for (;;)
+       if (!dir->is_cached)
+               exfat_bug("directory is not cached");
+
+       /* build a bitmap of valid entries in the directory */
+       dmap = calloc(BMAP_SIZE(dir->size / sizeof(struct exfat_entry)),
+                       sizeof(bitmap_t));
+       if (dmap == NULL)
+       {
+               exfat_error("failed to allocate directory bitmap (%"PRIu64")",
+                               dir->size / sizeof(struct exfat_entry));
+               return -ENOMEM;
+       }
+       for (p = dir->child; p != NULL; p = p->next)
+               for (i = 0; i < 1 + p->continuations; i++)
+                       BMAP_SET(dmap, p->entry_offset / sizeof(struct exfat_entry) + i);
+
+       /* find a slot in the directory entries bitmap */
+       for (i = 0; i < dir->size / sizeof(struct exfat_entry); i++)
        {
        {
-               if (contiguous == 0)
+               if (BMAP_GET(dmap, i) == 0)
                {
                {
-                       *cluster = it.cluster;
-                       *offset = it.offset;
+                       if (contiguous++ == 0)
+                               *offset = (off_t) i * sizeof(struct exfat_entry);
+                       if (contiguous == n)
+                               /* suitable slot is found, check that it's not occupied */
+                               switch (check_slot(ef, dir, *offset, n))
+                               {
+                               case 0:
+                                       free(dmap);
+                                       return 0;
+                               case -EIO:
+                                       free(dmap);
+                                       return -EIO;
+                               case -EINVAL:
+                                       /* slot is occupied, continue searching */
+                                       contiguous = 0;
+                                       break;
+                               }
                }
                }
-               entry = get_entry_ptr(ef, &it);
-               if (entry->type & EXFAT_ENTRY_VALID)
-                       contiguous = 0;
                else
                else
-                       contiguous++;
-               if (contiguous == subentries)
-                       break;  /* suitable slot is found */
-               if (it.offset + sizeof(struct exfat_entry) >= dir->size)
-               {
-                       rc = grow_directory(ef, dir, dir->size,
-                                       (subentries - contiguous) * sizeof(struct exfat_entry));
-                       if (rc != 0)
-                       {
-                               closedir(&it);
-                               return rc;
-                       }
-               }
-               if (!fetch_next_entry(ef, dir, &it))
-               {
-                       closedir(&it);
-                       return -EIO;
-               }
+                       contiguous = 0;
        }
        }
-       closedir(&it);
-       return 0;
+       free(dmap);
+
+       /* no suitable slots found, extend the directory */
+       if (contiguous == 0)
+               *offset = dir->size;
+       return exfat_truncate(ef, dir,
+                       ROUND_UP(dir->size + sizeof(struct exfat_entry[n - contiguous]),
+                                       CLUSTER_SIZE(*ef->sb)),
+                       true);
 }
 
 }
 
-static int write_entry(struct exfat* ef, struct exfat_node* dir,
+static int commit_entry(struct exfat* ef, struct exfat_node* dir,
                const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
 {
        struct exfat_node* node;
                const le16_t* name, cluster_t cluster, off_t offset, uint16_t attrib)
 {
        struct exfat_node* node;
-       struct exfat_entry_meta1 meta1;
-       struct exfat_entry_meta2 meta2;
        const size_t name_length = utf16_length(name);
        const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
        const size_t name_length = utf16_length(name);
        const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
+       struct exfat_entry entries[2 + name_entries];
+       struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
+       struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
        int i;
        int i;
+       int rc;
 
 
-       node = allocate_node();
-       if (node == NULL)
-               return -ENOMEM;
-       node->entry_cluster = cluster;
-       node->entry_offset = offset;
-       memcpy(node->name, name, name_length * sizeof(le16_t));
+       memset(entries, 0, sizeof(struct exfat_entry[2]));
+
+       meta1->type = EXFAT_ENTRY_FILE;
+       meta1->continuations = 1 + name_entries;
+       meta1->attrib = cpu_to_le16(attrib);
+       exfat_unix2exfat(time(NULL), &meta1->crdate, &meta1->crtime,
+                       &meta1->crtime_cs);
+       meta1->adate = meta1->mdate = meta1->crdate;
+       meta1->atime = meta1->mtime = meta1->crtime;
+       meta1->mtime_cs = meta1->crtime_cs; /* there is no atime_cs */
+
+       meta2->type = EXFAT_ENTRY_FILE_INFO;
+       meta2->flags = EXFAT_FLAG_ALWAYS1;
+       meta2->name_length = name_length;
+       meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
+       meta2->start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
 
 
-       memset(&meta1, 0, sizeof(meta1));
-       meta1.type = EXFAT_ENTRY_FILE;
-       meta1.continuations = 1 + name_entries;
-       meta1.attrib = cpu_to_le16(attrib);
-       exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime,
-                       &meta1.crtime_cs);
-       meta1.adate = meta1.mdate = meta1.crdate;
-       meta1.atime = meta1.mtime = meta1.crtime;
-       meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */
-
-       memset(&meta2, 0, sizeof(meta2));
-       meta2.type = EXFAT_ENTRY_FILE_INFO;
-       meta2.flags = EXFAT_FLAG_ALWAYS1;
-       meta2.name_length = name_length;
-       meta2.name_hash = exfat_calc_name_hash(ef, node->name);
-       meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE);
-
-       meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);
-
-       if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
-                       co2o(ef, cluster, offset)) < 0)
-       {
-               exfat_error("failed to write meta1 entry");
-               return -EIO;
-       }
-       if (!next_entry(ef, dir, &cluster, &offset))
-               return -EIO;
-       if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
-                       co2o(ef, cluster, offset)) < 0)
-       {
-               exfat_error("failed to write meta2 entry");
-               return -EIO;
-       }
        for (i = 0; i < name_entries; i++)
        {
        for (i = 0; i < name_entries; i++)
        {
-               struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
-               memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX,
-                               MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
-                               sizeof(le16_t));
-               if (!next_entry(ef, dir, &cluster, &offset))
-                       return -EIO;
-               if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
-                               co2o(ef, cluster, offset)) < 0)
-               {
-                       exfat_error("failed to write name entry");
-                       return -EIO;
-               }
+               struct exfat_entry_name* name_entry;
+
+               name_entry = (struct exfat_entry_name*) &entries[2 + i];
+               name_entry->type = EXFAT_ENTRY_FILE_NAME;
+               name_entry->__unknown = 0;
+               memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
+                               EXFAT_ENAME_MAX * sizeof(le16_t));
        }
 
        }
 
-       init_node_meta1(node, &meta1);
-       init_node_meta2(node, &meta2);
+       meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
+       rc = write_entries(ef, dir, entries, 2 + name_entries, offset);
+       if (rc != 0)
+               return rc;
+
+       node = allocate_node();
+       if (node == NULL)
+               return -ENOMEM;
+       node->entry_offset = offset;
+       memcpy(node->name, name, name_length * sizeof(le16_t));
+       init_node_meta1(node, meta1);
+       init_node_meta2(node, meta2);
 
        tree_attach(dir, node);
 
        tree_attach(dir, node);
-       exfat_update_mtime(dir);
        return 0;
 }
 
        return 0;
 }
 
@@ -1019,19 +940,20 @@ static int create(struct exfat* ef, const char* path, uint16_t attrib)
                return -EEXIST;
        }
 
                return -EEXIST;
        }
 
-       rc = find_slot(ef, dir, &cluster, &offset,
+       rc = find_slot(ef, dir, &offset,
                        2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
        if (rc != 0)
        {
                exfat_put_node(ef, dir);
                return rc;
        }
                        2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
        if (rc != 0)
        {
                exfat_put_node(ef, dir);
                return rc;
        }
-       rc = write_entry(ef, dir, name, cluster, offset, attrib);
+       rc = commit_entry(ef, dir, name, cluster, offset, attrib);
        if (rc != 0)
        {
                exfat_put_node(ef, dir);
                return rc;
        }
        if (rc != 0)
        {
                exfat_put_node(ef, dir);
                return rc;
        }
+       exfat_update_mtime(dir);
        rc = exfat_flush_node(ef, dir);
        exfat_put_node(ef, dir);
        return rc;
        rc = exfat_flush_node(ef, dir);
        exfat_put_node(ef, dir);
        return rc;
@@ -1076,69 +998,45 @@ static int rename_entry(struct exfat* ef, struct exfat_node* dir,
                struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
                off_t new_offset)
 {
                struct exfat_node* node, const le16_t* name, cluster_t new_cluster,
                off_t new_offset)
 {
-       struct exfat_entry_meta1 meta1;
-       struct exfat_entry_meta2 meta2;
-       cluster_t old_cluster = node->entry_cluster;
-       off_t old_offset = node->entry_offset;
        const size_t name_length = utf16_length(name);
        const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
        const size_t name_length = utf16_length(name);
        const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX);
+       struct exfat_entry entries[2 + name_entries];
+       struct exfat_entry_meta1* meta1 = (struct exfat_entry_meta1*) &entries[0];
+       struct exfat_entry_meta2* meta2 = (struct exfat_entry_meta2*) &entries[1];
+       int rc;
        int i;
 
        int i;
 
-       if (exfat_pread(ef->dev, &meta1, sizeof(meta1),
-                       co2o(ef, old_cluster, old_offset)) < 0)
-       {
-               exfat_error("failed to read meta1 entry on rename");
-               return -EIO;
-       }
-       if (!next_entry(ef, node->parent, &old_cluster, &old_offset))
-               return -EIO;
-       if (exfat_pread(ef->dev, &meta2, sizeof(meta2),
-                       co2o(ef, old_cluster, old_offset)) < 0)
-       {
-               exfat_error("failed to read meta2 entry on rename");
-               return -EIO;
-       }
-       meta1.continuations = 1 + name_entries;
-       meta2.name_hash = exfat_calc_name_hash(ef, name);
-       meta2.name_length = name_length;
-       meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name);
+       rc = read_entries(ef, node->parent, entries, 2, node->entry_offset);
+       if (rc != 0)
+               return rc;
 
 
-       if (!erase_entry(ef, node))
-               return -EIO;
+       meta1->continuations = 1 + name_entries;
+       meta2->name_length = name_length;
+       meta2->name_hash = exfat_calc_name_hash(ef, name, name_length);
 
 
-       node->entry_cluster = new_cluster;
-       node->entry_offset = new_offset;
+       rc = erase_node(ef, node);
+       if (rc != 0)
+               return rc;
 
 
-       if (exfat_pwrite(ef->dev, &meta1, sizeof(meta1),
-                       co2o(ef, new_cluster, new_offset)) < 0)
-       {
-               exfat_error("failed to write meta1 entry on rename");
-               return -EIO;
-       }
-       if (!next_entry(ef, dir, &new_cluster, &new_offset))
-               return -EIO;
-       if (exfat_pwrite(ef->dev, &meta2, sizeof(meta2),
-                       co2o(ef, new_cluster, new_offset)) < 0)
-       {
-               exfat_error("failed to write meta2 entry on rename");
-               return -EIO;
-       }
+       node->entry_offset = new_offset;
+       node->continuations = 1 + name_entries;
 
        for (i = 0; i < name_entries; i++)
        {
 
        for (i = 0; i < name_entries; i++)
        {
-               struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
-               memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
+               struct exfat_entry_name* name_entry;
+
+               name_entry = (struct exfat_entry_name*) &entries[2 + i];
+               name_entry->type = EXFAT_ENTRY_FILE_NAME;
+               name_entry->__unknown = 0;
+               memcpy(name_entry->name, name + i * EXFAT_ENAME_MAX,
                                EXFAT_ENAME_MAX * sizeof(le16_t));
                                EXFAT_ENAME_MAX * sizeof(le16_t));
-               if (!next_entry(ef, dir, &new_cluster, &new_offset))
-                       return -EIO;
-               if (exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry),
-                               co2o(ef, new_cluster, new_offset)) < 0)
-               {
-                       exfat_error("failed to write name entry on rename");
-                       return -EIO;
-               }
        }
 
        }
 
+       meta1->checksum = exfat_calc_checksum(entries, 2 + name_entries);
+       rc = write_entries(ef, dir, entries, 2 + name_entries, new_offset);
+       if (rc != 0)
+               return rc;
+
        memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
        tree_detach(node);
        tree_attach(dir, node);
        memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t));
        tree_detach(node);
        tree_attach(dir, node);
@@ -1167,7 +1065,7 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
        }
 
        /* check that target is not a subdirectory of the source */
        }
 
        /* check that target is not a subdirectory of the source */
-       if (node->flags & EXFAT_ATTRIB_DIR)
+       if (node->attrib & EXFAT_ATTRIB_DIR)
        {
                struct exfat_node* p;
 
        {
                struct exfat_node* p;
 
@@ -1187,16 +1085,16 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
                /* remove target if it's not the same node as source */
                if (existing != node)
                {
                /* remove target if it's not the same node as source */
                if (existing != node)
                {
-                       if (existing->flags & EXFAT_ATTRIB_DIR)
+                       if (existing->attrib & EXFAT_ATTRIB_DIR)
                        {
                        {
-                               if (node->flags & EXFAT_ATTRIB_DIR)
+                               if (node->attrib & EXFAT_ATTRIB_DIR)
                                        rc = exfat_rmdir(ef, existing);
                                else
                                        rc = -ENOTDIR;
                        }
                        else
                        {
                                        rc = exfat_rmdir(ef, existing);
                                else
                                        rc = -ENOTDIR;
                        }
                        else
                        {
-                               if (!(node->flags & EXFAT_ATTRIB_DIR))
+                               if (!(node->attrib & EXFAT_ATTRIB_DIR))
                                        rc = exfat_unlink(ef, existing);
                                else
                                        rc = -EISDIR;
                                        rc = exfat_unlink(ef, existing);
                                else
                                        rc = -EISDIR;
@@ -1223,7 +1121,7 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
                        exfat_put_node(ef, existing);
        }
 
                        exfat_put_node(ef, existing);
        }
 
-       rc = find_slot(ef, dir, &cluster, &offset,
+       rc = find_slot(ef, dir, &offset,
                        2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
        if (rc != 0)
        {
                        2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX));
        if (rc != 0)
        {
@@ -1232,8 +1130,16 @@ int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path)
                return rc;
        }
        rc = rename_entry(ef, dir, node, name, cluster, offset);
                return rc;
        }
        rc = rename_entry(ef, dir, node, name, cluster, offset);
+       if (rc != 0)
+       {
+               exfat_put_node(ef, dir);
+               exfat_put_node(ef, node);
+               return rc;
+       }
+       rc = exfat_flush_node(ef, dir);
        exfat_put_node(ef, dir);
        exfat_put_node(ef, node);
        exfat_put_node(ef, dir);
        exfat_put_node(ef, node);
+       /* node itself is not marked as dirty, no need to flush it */
        return rc;
 }
 
        return rc;
 }
 
@@ -1241,19 +1147,19 @@ void exfat_utimes(struct exfat_node* node, const struct timespec tv[2])
 {
        node->atime = tv[0].tv_sec;
        node->mtime = tv[1].tv_sec;
 {
        node->atime = tv[0].tv_sec;
        node->mtime = tv[1].tv_sec;
-       node->flags |= EXFAT_ATTRIB_DIRTY;
+       node->is_dirty = true;
 }
 
 void exfat_update_atime(struct exfat_node* node)
 {
        node->atime = time(NULL);
 }
 
 void exfat_update_atime(struct exfat_node* node)
 {
        node->atime = time(NULL);
-       node->flags |= EXFAT_ATTRIB_DIRTY;
+       node->is_dirty = true;
 }
 
 void exfat_update_mtime(struct exfat_node* node)
 {
        node->mtime = time(NULL);
 }
 
 void exfat_update_mtime(struct exfat_node* node)
 {
        node->mtime = time(NULL);
-       node->flags |= EXFAT_ATTRIB_DIRTY;
+       node->is_dirty = true;
 }
 
 const char* exfat_get_label(struct exfat* ef)
 }
 
 const char* exfat_get_label(struct exfat* ef)
@@ -1261,36 +1167,19 @@ const char* exfat_get_label(struct exfat* ef)
        return ef->label;
 }
 
        return ef->label;
 }
 
-static int find_label(struct exfat* ef, cluster_t* cluster, off_t* offset)
+static int find_label(struct exfat* ef, off_t* offset)
 {
 {
-       struct iterator it;
+       struct exfat_entry entry;
        int rc;
 
        int rc;
 
-       rc = opendir(ef, ef->root, &it);
-       if (rc != 0)
-               return rc;
-
-       for (;;)
+       for (*offset = 0; ; *offset += sizeof(entry))
        {
        {
-               if (it.offset >= ef->root->size)
-               {
-                       closedir(&it);
-                       return -ENOENT;
-               }
+               rc = read_entries(ef, ef->root, &entry, 1, *offset);
+               if (rc != 0)
+                       return rc;
 
 
-               if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL)
-               {
-                       *cluster = it.cluster;
-                       *offset = it.offset;
-                       closedir(&it);
+               if (entry.type == EXFAT_ENTRY_LABEL)
                        return 0;
                        return 0;
-               }
-
-               if (!fetch_next_entry(ef, ef->root, &it))
-               {
-                       closedir(&it);
-                       return -EIO;
-               }
        }
 }
 
        }
 }
 
@@ -1298,18 +1187,17 @@ int exfat_set_label(struct exfat* ef, const char* label)
 {
        le16_t label_utf16[EXFAT_ENAME_MAX + 1];
        int rc;
 {
        le16_t label_utf16[EXFAT_ENAME_MAX + 1];
        int rc;
-       cluster_t cluster;
        off_t offset;
        struct exfat_entry_label entry;
 
        memset(label_utf16, 0, sizeof(label_utf16));
        off_t offset;
        struct exfat_entry_label entry;
 
        memset(label_utf16, 0, sizeof(label_utf16));
-       rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
+       rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
        if (rc != 0)
                return rc;
 
        if (rc != 0)
                return rc;
 
-       rc = find_label(ef, &cluster, &offset);
+       rc = find_label(ef, &offset);
        if (rc == -ENOENT)
        if (rc == -ENOENT)
-               rc = find_slot(ef, ef->root, &cluster, &offset, 1);
+               rc = find_slot(ef, ef->root, &offset, 1);
        if (rc != 0)
                return rc;
 
        if (rc != 0)
                return rc;
 
@@ -1319,12 +1207,10 @@ int exfat_set_label(struct exfat* ef, const char* label)
        if (entry.length == 0)
                entry.type ^= EXFAT_ENTRY_VALID;
 
        if (entry.length == 0)
                entry.type ^= EXFAT_ENTRY_VALID;
 
-       if (exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label),
-                       co2o(ef, cluster, offset)) < 0)
-       {
-               exfat_error("failed to write label entry");
-               return -EIO;
-       }
+       rc = write_entries(ef, ef->root, (struct exfat_entry*) &entry, 1, offset);
+       if (rc != 0)
+               return rc;
+
        strcpy(ef->label, label);
        return 0;
 }
        strcpy(ef->label, label);
        return 0;
 }
index 4c5a0df59d42a9b3add8a6a888088a0e9a2aeae9..b3505f4b6f142c2b50819b856d93c16ff2a213e9 100644 (file)
@@ -4,7 +4,7 @@
        same kernel can use different libc implementations.
 
        Free exFAT implementation.
        same kernel can use different libc implementations.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
index 93c1d67c9550d04bfd900e065c117ab5cb724401..3ef99e8dacd8e973c145d50c4be1a390cb7c1ac0 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
index 98ae52dbebc19445048025e39304ce719584581d..d8968dbdcf6b377ec9d7236091908e6eb8de401a 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
@@ -108,7 +108,7 @@ int utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
        char* outp = output;
        wchar_t wc;
 
        char* outp = output;
        wchar_t wc;
 
-       while (inp - input < insize && le16_to_cpu(*inp))
+       while (inp - input < insize)
        {
                inp = utf16_to_wchar(inp, &wc, insize - (inp - input));
                if (inp == NULL)
        {
                inp = utf16_to_wchar(inp, &wc, insize - (inp - input));
                if (inp == NULL)
@@ -122,6 +122,13 @@ int utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
                        exfat_error("name is too long");
                        return -ENAMETOOLONG;
                }
                        exfat_error("name is too long");
                        return -ENAMETOOLONG;
                }
+               if (wc == 0)
+                       return 0;
+       }
+       if (outp - output >= outsize)
+       {
+               exfat_error("name is too long");
+               return -ENAMETOOLONG;
        }
        *outp = '\0';
        return 0;
        }
        *outp = '\0';
        return 0;
@@ -202,7 +209,7 @@ int utf8_to_utf16(le16_t* output, const char* input, size_t outsize,
        le16_t* outp = output;
        wchar_t wc;
 
        le16_t* outp = output;
        wchar_t wc;
 
-       while (inp - input < insize && *inp)
+       while (inp - input < insize)
        {
                inp = utf8_to_wchar(inp, &wc, insize - (inp - input));
                if (inp == NULL)
        {
                inp = utf8_to_wchar(inp, &wc, insize - (inp - input));
                if (inp == NULL)
@@ -216,6 +223,13 @@ int utf8_to_utf16(le16_t* output, const char* input, size_t outsize,
                        exfat_error("name is too long");
                        return -ENAMETOOLONG;
                }
                        exfat_error("name is too long");
                        return -ENAMETOOLONG;
                }
+               if (wc == 0)
+                       break;
+       }
+       if (outp - output >= outsize)
+       {
+               exfat_error("name is too long");
+               return -ENAMETOOLONG;
        }
        *outp = cpu_to_le16(0);
        return 0;
        }
        *outp = cpu_to_le16(0);
        return 0;
index 0d3b809218c37acd4c347bfd86614dd97997b49f..c080d4c4288d3392c58064a3d613c5047e32f2e4 100644 (file)
@@ -3,7 +3,7 @@
        exFAT file system implementation library.
 
        Free exFAT implementation.
        exFAT file system implementation library.
 
        Free exFAT implementation.
-       Copyright (C) 2010-2016  Andrew Nayenko
+       Copyright (C) 2010-2017  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
 
        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
@@ -29,7 +29,7 @@ void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
                struct stat* stbuf)
 {
        memset(stbuf, 0, sizeof(struct stat));
                struct stat* stbuf)
 {
        memset(stbuf, 0, sizeof(struct stat));
-       if (node->flags & EXFAT_ATTRIB_DIR)
+       if (node->attrib & EXFAT_ATTRIB_DIR)
                stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
        else
                stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
                stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask);
        else
                stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask);
@@ -37,8 +37,7 @@ void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
        stbuf->st_uid = ef->uid;
        stbuf->st_gid = ef->gid;
        stbuf->st_size = node->size;
        stbuf->st_uid = ef->uid;
        stbuf->st_gid = ef->gid;
        stbuf->st_size = node->size;
-       stbuf->st_blocks = DIV_ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) *
-               CLUSTER_SIZE(*ef->sb) / 512;
+       stbuf->st_blocks = ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) / 512;
        stbuf->st_mtime = node->mtime;
        stbuf->st_atime = node->atime;
        /* set ctime to mtime to ensure we don't break programs that rely on ctime
        stbuf->st_mtime = node->mtime;
        stbuf->st_atime = node->atime;
        /* set ctime to mtime to ensure we don't break programs that rely on ctime
@@ -46,12 +45,28 @@ void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
        stbuf->st_ctime = node->mtime;
 }
 
        stbuf->st_ctime = node->mtime;
 }
 
-void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n)
+void exfat_get_name(const struct exfat_node* node,
+               char buffer[EXFAT_UTF8_NAME_BUFFER_MAX])
 {
 {
-       if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0)
+       if (utf16_to_utf8(buffer, node->name, EXFAT_UTF8_NAME_BUFFER_MAX,
+                               EXFAT_NAME_MAX) != 0)
                exfat_bug("failed to convert name to UTF-8");
 }
 
                exfat_bug("failed to convert name to UTF-8");
 }
 
+static uint16_t add_checksum_byte(uint16_t sum, uint8_t byte)
+{
+       return ((sum << 15) | (sum >> 1)) + byte;
+}
+
+static uint16_t add_checksum_bytes(uint16_t sum, const void* buffer, size_t n)
+{
+       int i;
+
+       for (i = 0; i < n; i++)
+               sum = add_checksum_byte(sum, ((const uint8_t*) buffer)[i]);
+       return sum;
+}
+
 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
 {
        uint16_t sum = 0;
 uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
 {
        uint16_t sum = 0;
@@ -59,36 +74,23 @@ uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry)
 
        for (i = 0; i < sizeof(struct exfat_entry); i++)
                if (i != 2 && i != 3) /* skip checksum field itself */
 
        for (i = 0; i < sizeof(struct exfat_entry); i++)
                if (i != 2 && i != 3) /* skip checksum field itself */
-                       sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
+                       sum = add_checksum_byte(sum, ((const uint8_t*) entry)[i]);
        return sum;
 }
 
 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
 {
        return sum;
 }
 
 uint16_t exfat_add_checksum(const void* entry, uint16_t sum)
 {
-       int i;
-
-       for (i = 0; i < sizeof(struct exfat_entry); i++)
-               sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i];
-       return sum;
+       return add_checksum_bytes(sum, entry, sizeof(struct exfat_entry));
 }
 
 }
 
-le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
-               const struct exfat_entry_meta2* meta2, const le16_t* name)
+le16_t exfat_calc_checksum(const struct exfat_entry* entries, int n)
 {
        uint16_t checksum;
 {
        uint16_t checksum;
-       const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX);
        int i;
 
        int i;
 
-       checksum = exfat_start_checksum(meta1);
-       checksum = exfat_add_checksum(meta2, checksum);
-       for (i = 0; i < name_entries; i++)
-       {
-               struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0};
-               memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX,
-                               MIN(EXFAT_ENAME_MAX, EXFAT_NAME_MAX - i * EXFAT_ENAME_MAX) *
-                               sizeof(le16_t));
-               checksum = exfat_add_checksum(&name_entry, checksum);
-       }
+       checksum = exfat_start_checksum((const struct exfat_entry_meta1*) entries);
+       for (i = 1; i < n; i++)
+               checksum = exfat_add_checksum(entries + i, checksum);
        return cpu_to_le16(checksum);
 }
 
        return cpu_to_le16(checksum);
 }
 
@@ -113,10 +115,10 @@ uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum)
        return sum;
 }
 
        return sum;
 }
 
-le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
+le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name,
+               size_t length)
 {
        size_t i;
 {
        size_t i;
-       size_t length = utf16_length(name);
        uint16_t hash = 0;
 
        for (i = 0; i < length; i++)
        uint16_t hash = 0;
 
        for (i = 0; i < length; i++)
index b411a96e0051923d3c578569a14b15d01f1524a2..f4a10ccb71844b199cb4a3a8c3a4fd6a79fe5d8a 100644 (file)
@@ -3,7 +3,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2011-2016  Andrew Nayenko
+#      Copyright (C) 2011-2017  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
 #
 #      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
index 0e4840f00439f205cc8cceb6865fc627e30975b4..9a5dc3808107b9ff58d3273d223cf4bcc011e8c6 100644 (file)
@@ -19,7 +19,7 @@
 #      Automake source.
 #
 #      Free exFAT implementation.
 #      Automake source.
 #
 #      Free exFAT implementation.
-#      Copyright (C) 2011-2016  Andrew Nayenko
+#      Copyright (C) 2011-2017  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
 #
 #      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
index 57e57845f541d21f2e3598098dc1b8d6204bf134..fb854e74487714e361cb1e8f17606c43f82e0577 100644 (file)
@@ -3,7 +3,7 @@
        Clusters Bitmap creation code.
 
        Free exFAT implementation.
        Clusters Bitmap creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index cc5a7db35e04fa70129a1392aa547a2ccc94ef89..76a31a957cd24c4947d717bb809bf8434975d692 100644 (file)
@@ -3,7 +3,7 @@
        Clusters Bitmap creation code.
 
        Free exFAT implementation.
        Clusters Bitmap creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 0ca3f3a4770bf11ae98caa6fa4c2a796e089e0e4..be3a6c3a325ddf63d53f20123c0b611eeac1e36a 100644 (file)
@@ -3,7 +3,7 @@
        File Allocation Table creation code.
 
        Free exFAT implementation.
        File Allocation Table creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index fce809c085d61fb3b97faf418f01b6d781f69410..4a6e80c13a73ba303ecabdc7b00ef85fb1e8a9fa 100644 (file)
@@ -3,7 +3,7 @@
        File Allocation Table creation code.
 
        Free exFAT implementation.
        File Allocation Table creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index a164eebf82826b49af31a21bc0f54d7af1191172..02ac9c7cca6af193428028c473b58f7baac9cdec 100644 (file)
@@ -3,7 +3,7 @@
        Creates exFAT file system.
 
        Free exFAT implementation.
        Creates exFAT file system.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
@@ -136,7 +136,7 @@ static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
        memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
        if (s == NULL)
                return 0;
        memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
        if (s == NULL)
                return 0;
-       return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
+       return utf8_to_utf16(label, s, EXFAT_ENAME_MAX + 1, strlen(s));
 }
 
 static uint32_t setup_volume_serial(uint32_t user_defined)
 }
 
 static uint32_t setup_volume_serial(uint32_t user_defined)
@@ -228,7 +228,7 @@ int main(int argc, char* argv[])
                        }
                        break;
                case 'V':
                        }
                        break;
                case 'V':
-                       puts("Copyright (C) 2011-2016  Andrew Nayenko");
+                       puts("Copyright (C) 2011-2017  Andrew Nayenko");
                        return 0;
                default:
                        usage(argv[0]);
                        return 0;
                default:
                        usage(argv[0]);
index 07a48d2eeb4c358735abc4e27393607527196519..801f36729c6eb28548da3c0f70e71b1712776439 100644 (file)
@@ -3,7 +3,7 @@
        FS creation engine.
 
        Free exFAT implementation.
        FS creation engine.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index b15922c37eb4ddce672ad3539a260a8e70fbcd2c..e8e0a44ccdcec2b043b4c07f6c0eaa3b1e2640f5 100644 (file)
@@ -3,7 +3,7 @@
        FS creation engine.
 
        Free exFAT implementation.
        FS creation engine.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 8341039924194127fc07c7fd911935dc3be2f6f4..a8cc834139f73f5871e0a406d0c1d948e251f7d4 100644 (file)
@@ -3,7 +3,7 @@
        Root directory creation code.
 
        Free exFAT implementation.
        Root directory creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 26be1d6c3478ac437f06ba899dfa85785af24f4f..70ad02dac26017ce6f16cbd8972310d12f90a3c5 100644 (file)
@@ -3,7 +3,7 @@
        Root directory creation code.
 
        Free exFAT implementation.
        Root directory creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 7d708fb2ae3d8dbb4fe149546edd5d7a9f46977e..a70e8973636b6a8890d152e60fdd090d2309f656 100644 (file)
@@ -3,7 +3,7 @@
        Upper Case Table creation code.
 
        Free exFAT implementation.
        Upper Case Table creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 731f0cbc1fe66f99f1f736b336b1c7cba469c66e..e705371a9e7e7dc97dfd6cf36d36b3bdcdf173a3 100644 (file)
@@ -3,7 +3,7 @@
        Upper Case Table creation code.
 
        Free exFAT implementation.
        Upper Case Table creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 08da19c39d4f30886103c1477e8e1d066a9cfff0..7e52dee2c085a7ecbc44875dc679c90441a4f5da 100644 (file)
@@ -3,7 +3,7 @@
        Upper Case Table contents.
 
        Free exFAT implementation.
        Upper Case Table contents.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 7ca31e7575b3fcb5db2704fdceedc48c94b3ed3b..0100f7c08f140d8c7a9b6bbda2908ef521075c55 100644 (file)
@@ -3,7 +3,7 @@
        Upper Case Table declaration.
 
        Free exFAT implementation.
        Upper Case Table declaration.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 481fd63241e0cb7f17217d7820bd571f73d5109e..81ab2bab371843c681d1a856cf516b1dfe66e364 100644 (file)
@@ -3,7 +3,7 @@
        Volume Boot Record creation code.
 
        Free exFAT implementation.
        Volume Boot Record creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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
index 9cc038f78a26702ac22b1be30521f94983ebd60d..69b46c5cb262a2b3792c412bc7ffc59cec76b49a 100644 (file)
@@ -3,7 +3,7 @@
        Volume Boot Record creation code.
 
        Free exFAT implementation.
        Volume Boot Record creation code.
 
        Free exFAT implementation.
-       Copyright (C) 2011-2016  Andrew Nayenko
+       Copyright (C) 2011-2017  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
 
        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