OSDN Git Service

OS X: correctly detect device size.
[android-x86/external-exfat.git] / libexfat / io.c
index 7d416ed..e9672fb 100644 (file)
 #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>
+#ifdef __APPLE__
+#include <sys/disk.h>
+#endif
 #ifdef USE_UBLIO
 #include <sys/uio.h>
 #include <ublio.h>
 struct exfat_dev
 {
        int fd;
+       enum exfat_mode mode;
+       off_t size; /* in bytes */
 #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;
@@ -59,14 +88,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);
@@ -84,6 +146,42 @@ struct exfat_dev* exfat_open(const char* spec, int ro)
                return NULL;
        }
 
+#ifdef __APPLE__
+       if (S_ISBLK(stbuf.st_mode))
+       {
+               uint32_t block_size = 0;
+               uint64_t blocks = 0;
+
+               if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0)
+               {
+                       close(dev->fd);
+                       free(dev);
+                       exfat_error("failed to get block size");
+                       return NULL;
+               }
+               if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0)
+               {
+                       close(dev->fd);
+                       free(dev);
+                       exfat_error("failed to get blocks count");
+                       return NULL;
+               }
+               dev->size = blocks * block_size;
+       }
+       else
+#endif
+       {
+               /* works for Linux, FreeBSD, Solaris */
+               dev->size = exfat_seek(dev, 0, SEEK_END);
+               if (dev->size <= 0)
+               {
+                       close(dev->fd);
+                       free(dev);
+                       exfat_error("failed to get device size");
+                       return NULL;
+               }
+       }
+
 #ifdef USE_UBLIO
        memset(&up, 0, sizeof(struct ublio_param));
        up.up_blocksize = 256 * 1024;
@@ -135,6 +233,16 @@ int exfat_fsync(struct exfat_dev* dev)
        return 0;
 }
 
+enum exfat_mode exfat_get_mode(const struct exfat_dev* dev)
+{
+       return dev->mode;
+}
+
+off_t exfat_get_size(const struct exfat_dev* dev)
+{
+       return dev->size;
+}
+
 off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence)
 {
 #ifdef USE_UBLIO