OSDN Git Service

On Linux do BLKROGET ioctl to make sure the device is not read-only: after "blockdev...
[android-x86/external-exfat.git] / libexfat / io.c
index c9c1e2b..e89349b 100644 (file)
@@ -2,7 +2,7 @@
        io.c (02.09.09)
        exFAT file system implementation library.
 
-       Copyright (C) 2009, 2010  Andrew Nayenko
+       Copyright (C) 2010-2012  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
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#define _XOPEN_SOURCE 500 /* for pread() and pwrite() in Linux */
 #include "exfat.h"
 #include <inttypes.h>
 #include <sys/types.h>
 #include <sys/uio.h>
 #include <sys/stat.h>
+#include <sys/mount.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <string.h>
 #include <ublio.h>
 #endif
 
-#if _FILE_OFFSET_BITS != 64
+#if !defined(_FILE_OFFSET_BITS) || (_FILE_OFFSET_BITS != 64)
        #error You should define _FILE_OFFSET_BITS=64
 #endif
 
 struct exfat_dev
 {
        int fd;
+       enum exfat_mode mode;
 #ifdef USE_UBLIO
        off_t pos;
        ublio_filehandle_t ufh;
 #endif
 };
 
-struct exfat_dev* exfat_open(const char* spec, int ro)
+static int open_ro(const char* spec)
+{
+       return open(spec, O_RDONLY);
+}
+
+static int open_rw(const char* spec)
+{
+       int fd = open(spec, O_RDWR);
+#ifdef __linux__
+       int ro = 0;
+
+       /*
+          This ioctl is needed because after "blockdev --setro" kernel still
+          allows to open the device in read-write mode but fails writes.
+       */
+       if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro)
+       {
+               close(fd);
+               return -1;
+       }
+#endif
+       return fd;
+}
+
+struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode)
 {
        struct exfat_dev* dev;
        struct stat stbuf;
@@ -60,14 +85,47 @@ struct exfat_dev* exfat_open(const char* spec, int ro)
                return NULL;
        }
 
-       dev->fd = open(spec, ro ? O_RDONLY : O_RDWR);
-       if (dev->fd < 0)
+       switch (mode)
        {
+       case EXFAT_MODE_RO:
+               dev->fd = open_ro(spec);
+               if (dev->fd == -1)
+               {
+                       free(dev);
+                       exfat_error("failed to open `%s' in read-only mode", spec);
+                       return NULL;
+               }
+               dev->mode = EXFAT_MODE_RO;
+               break;
+       case EXFAT_MODE_RW:
+               dev->fd = open_rw(spec);
+               if (dev->fd == -1)
+               {
+                       free(dev);
+                       exfat_error("failed to open `%s' in read-write mode", spec);
+                       return NULL;
+               }
+               dev->mode = EXFAT_MODE_RW;
+               break;
+       case EXFAT_MODE_ANY:
+               dev->fd = open_rw(spec);
+               if (dev->fd != -1)
+               {
+                       dev->mode = EXFAT_MODE_RW;
+                       break;
+               }
+               dev->fd = open_ro(spec);
+               if (dev->fd != -1)
+               {
+                       dev->mode = EXFAT_MODE_RO;
+                       exfat_warn("`%s' is write-protected, mounting read-only", spec);
+                       break;
+               }
                free(dev);
-               exfat_error("failed to open `%s' in read-%s mode", spec,
-                               ro ? "only" : "write");
+               exfat_error("failed to open `%s'", spec);
                return NULL;
        }
+
        if (fstat(dev->fd, &stbuf) != 0)
        {
                close(dev->fd);
@@ -136,6 +194,11 @@ int exfat_fsync(struct exfat_dev* dev)
        return 0;
 }
 
+enum exfat_mode exfat_mode(const struct exfat_dev* dev)
+{
+       return dev->mode;
+}
+
 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
 {
 #ifdef USE_UBLIO
@@ -209,7 +272,7 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
        cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
        if (CLUSTER_INVALID(cluster))
        {
-               exfat_error("got invalid cluster");
+               exfat_error("invalid cluster 0x%x while reading", cluster);
                return -1;
        }
 
@@ -219,7 +282,7 @@ ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
        {
                if (CLUSTER_INVALID(cluster))
                {
-                       exfat_error("got invalid cluster");
+                       exfat_error("invalid cluster 0x%x while reading", cluster);
                        return -1;
                }
                lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
@@ -253,7 +316,7 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
        cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
        if (CLUSTER_INVALID(cluster))
        {
-               exfat_error("got invalid cluster");
+               exfat_error("invalid cluster 0x%x while writing", cluster);
                return -1;
        }
 
@@ -263,7 +326,7 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
        {
                if (CLUSTER_INVALID(cluster))
                {
-                       exfat_error("got invalid cluster");
+                       exfat_error("invalid cluster 0x%x while writing", cluster);
                        return -1;
                }
                lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);