X-Git-Url: http://git.sven.stormbind.net/?p=sven%2Fexfat-utils.git;a=blobdiff_plain;f=libexfat%2Fio.c;h=1a555b969b8df04455afcfebd37122ad8c2d7d6f;hp=f0beddc97b0ae424c683e858a937be3379fee097;hb=refs%2Ftags%2Fupstream%2F1.0.0;hpb=70a4b10edcf53a90140e6dd80ccaa045f3647ad7 diff --git a/libexfat/io.c b/libexfat/io.c index f0beddc..1a555b9 100644 --- a/libexfat/io.c +++ b/libexfat/io.c @@ -2,7 +2,7 @@ io.c (02.09.09) exFAT file system implementation library. - Copyright (C) 2010-2012 Andrew Nayenko + Copyright (C) 2010-2013 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 @@ -21,30 +21,55 @@ #include "exfat.h" #include #include -#include #include +#include #include #include #include +#ifdef __APPLE__ +#include +#endif #ifdef USE_UBLIO #include #include #endif -#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; + 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 +84,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 +142,49 @@ struct exfat_dev* exfat_open(const char* spec, int ro) return NULL; } +#ifdef __APPLE__ + if (!S_ISREG(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 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 memset(&up, 0, sizeof(struct ublio_param)); up.up_blocksize = 256 * 1024; @@ -135,6 +236,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 @@ -208,7 +319,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; } @@ -218,7 +329,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); @@ -241,18 +352,15 @@ ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node, off_t lsize, loffset, remainder; if (offset + size > node->size) - { - int rc = exfat_truncate(ef, node, offset + size); - if (rc != 0) - return rc; - } + if (exfat_truncate(ef, node, offset + size) != 0) + return -1; if (size == 0) return 0; 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; } @@ -262,7 +370,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);