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,
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"
#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>
#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
};
+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;
switch (mode)
{
case EXFAT_MODE_RO:
- dev->fd = open(spec, O_RDONLY);
+ dev->fd = open_ro(spec);
if (dev->fd == -1)
{
free(dev);
dev->mode = EXFAT_MODE_RO;
break;
case EXFAT_MODE_RW:
- dev->fd = open(spec, O_RDWR);
+ dev->fd = open_rw(spec);
if (dev->fd == -1)
{
free(dev);
dev->mode = EXFAT_MODE_RW;
break;
case EXFAT_MODE_ANY:
- dev->fd = open(spec, O_RDWR);
+ dev->fd = open_rw(spec);
if (dev->fd != -1)
{
dev->mode = EXFAT_MODE_RW;
break;
}
- dev->fd = open(spec, O_RDONLY);
+ dev->fd = open_ro(spec);
if (dev->fd != -1)
{
dev->mode = EXFAT_MODE_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;
return 0;
}
-enum exfat_mode exfat_mode(const struct exfat_dev* dev)
+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
}
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,
const char* bufp = buffer;
off_t lsize, loffset, remainder;
- if (offset + size > node->size)
- {
- int rc = exfat_truncate(ef, node, offset + size);
- if (rc != 0)
- return rc;
- }
+ 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;