OSDN Git Service

Relicensed the code from GPLv3+ to GPLv2+.
[android-x86/external-exfat.git] / libexfat / io.c
index 886dd23..052aa8d 100644 (file)
@@ -2,11 +2,12 @@
        io.c (02.09.09)
        exFAT file system implementation library.
 
-       Copyright (C) 2010-2012  Andrew Nayenko
+       Free exFAT implementation.
+       Copyright (C) 2010-2013  Andrew Nayenko
 
-       This program is free software: you can redistribute it and/or modify
+       This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
-       the Free Software Foundation, either version 3 of the License, or
+       the Free Software Foundation, either version 2 of the License, or
        (at your option) any later version.
 
        This program is distributed in the hope that it will be useful,
@@ -14,8 +15,9 @@
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
 
-       You should have received a copy of the GNU General Public License
-       along with this program.  If not, see <http://www.gnu.org/licenses/>.
+       You should have received a copy of the GNU General Public License along
+       with this program; if not, write to the Free Software Foundation, Inc.,
+       51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #include "exfat.h"
@@ -176,6 +178,13 @@ struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
                        exfat_error("failed to get size of `%s'", spec);
                        return NULL;
                }
+               if (exfat_seek(dev, 0, SEEK_SET) == -1)
+               {
+                       close(dev->fd);
+                       free(dev);
+                       exfat_error("failed to seek to the beginning of `%s'", spec);
+                       return NULL;
+               }
        }
 
 #ifdef USE_UBLIO
@@ -334,7 +343,7 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
        }
        if (!ef->ro && !ef->noatime)
                exfat_update_atime(node);
-       return size - remainder;
+       return MIN(size, node->size - offset) - remainder;
 }
 
 ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
@@ -344,9 +353,12 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
        const char* bufp = buffer;
        off_t lsize, loffset, remainder;
 
-       if (offset + size > node->size)
-               if (exfat_truncate(ef, node, offset + size) != 0)
-                       return -1;
+       if (offset > node->size)
+               if (exfat_truncate(ef, node, offset, true) != 0)
+                       return -1;
+       if (offset + size > node->size)
+               if (exfat_truncate(ef, node, offset + size, false) != 0)
+                       return -1;
        if (size == 0)
                return 0;