+1.0.0 (2013-01-19)
+
+* Fixed crash when renaming a file within a single directory and a new name
+differs only in case.
+* Fixed clusters allocation: a cluster beyond valid clusters range could be
+allocated.
+* Fixed crash when a volume is unmounted while some files are open.
+* SConscript now respects AR and RANLIB environment variables.
+* Improved error handling.
+
+Linux:
+
+* Enabled big_writes. This improves write speed (larger block size means less
+switches between kernel- and user-space).
+* Do BLKROGET ioctl to make sure the device is not read-only: after
+"blockdev --setro" kernel still allows to open the device in read-write mode
+but fails writes.
+
+OS X:
+
+* Fixed OS X 10.8 support.
+* Switched to 64-bit inode numbers (now Mac OS X 10.5 or later is required).
+* Switched from unmaintained MacFUSE to OSXFUSE (http://osxfuse.github.com).
+* Fixed device size detection. Now mkfs works.
+* Workarounded some utilities failures due to missing chmod() support.
+* Disabled (senseless) permission checks made by FUSE.
+
0.9.8 (2012-08-09)
* The mkfs utility can now create huge file systems (up to several exabytes).
# SConstruct (10.09.09)
# SConscript for all components.
#
-# 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
import SCons
env = Environment(**ARGUMENTS)
-conf = Configure(env)
+for var in ['PATH', 'SYSROOT']:
+ if var in os.environ:
+ env['ENV'][var] = os.environ[var]
destdir = env.get('DESTDIR', '/sbin');
-targets = []
libs = ['exfat']
+libfuse = 'fuse'
+
+if not env.GetOption('clean'):
+ conf = Configure(env)
+
+ if 'AR' in os.environ:
+ conf.env.Replace(AR = os.environ['AR'])
+ if 'RANLIB' in os.environ:
+ conf.env.Replace(RANLIB = os.environ['RANLIB'])
+ if 'CC' in os.environ:
+ conf.env.Replace(CC = os.environ['CC'])
+ if 'CCFLAGS' in os.environ:
+ conf.env.Replace(CCFLAGS = os.environ['CCFLAGS'])
+ # Set default CCFLAGS for known compilers
+ if not conf.env['CCFLAGS']:
+ if conf.env['CC'] == 'gcc':
+ conf.env.Replace(CCFLAGS = '-Wall -O2 -ggdb -std=c99')
+ elif conf.env['CC'] == 'clang':
+ conf.env.Replace(CCFLAGS = '-Wall -O2 -g -std=c99')
+ if 'CPPFLAGS' in os.environ:
+ conf.env.Replace(CPPFLAGS = os.environ['CPPFLAGS'])
+ conf.env.Append(CPPDEFINES = {'_FILE_OFFSET_BITS' : 64})
+ conf.env.Append(CPPPATH = ['libexfat'])
+ if 'LDFLAGS' in os.environ:
+ conf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
+ conf.env.Append(LIBPATH = ['libexfat'])
+
+ # GNU/Linux requires _BSD_SOURCE define for vsyslog(), _XOPEN_SOURCE >= 500
+ # for pread(), pwrite(), snprintf(), strdup(), etc. Everything needed is
+ # enabled by _GNU_SOURCE.
+ if platform.system() == 'Linux':
+ conf.env.Append(CPPDEFINES = '_GNU_SOURCE');
+
+ # Use 64-bit inode numbers (introduced in Mac OS X 10.5 Leopard). Require
+ # OSXFUSE (http://osxfuse.github.com).
+ if platform.system() == 'Darwin':
+ conf.env.Append(CPPDEFINES = '_DARWIN_USE_64_BIT_INODE')
+ conf.env.Append(CPPDEFINES = {'__DARWIN_UNIX03' : 1})
+ conf.env.Append(CPPPATH = ['/usr/local/include/osxfuse'])
+ conf.env.Append(CFLAGS = '-mmacosx-version-min=10.5')
+ conf.env.Append(LINKFLAGS = '-mmacosx-version-min=10.5')
+ libfuse = 'osxfuse_i64'
+
+ # FreeBSD does not support block devices, only raw devices. Ublio is
+ # required for unaligned I/O and caching.
+ if platform.system() == 'FreeBSD':
+ conf.env.Append(CPPDEFINES = 'USE_UBLIO')
+ libs.append('ublio')
+ conf.env.Append(CPPPATH = ['/usr/local/include'])
+ conf.env.Append(LIBPATH = ['/usr/local/lib'])
+
+ if not conf.CheckCC():
+ print '''
+ A working C compiler is needed very much.
+'''
+ Exit(1)
+
+ if not conf.CheckTypeSize('off_t', '#include <sys/types.h>', 'C', 8):
+ print '''
+ The size of off_t type must be 64 bits. File systems larger than
+ 2 GB will be corrupted with 32-bit off_t.
+'''
+ Exit(1)
+
+ env = conf.Finish()
+
-if 'CC' in os.environ:
- conf.env.Replace(CC = os.environ['CC'])
-if 'CCFLAGS' in os.environ:
- conf.env.Replace(CCFLAGS = os.environ['CCFLAGS'])
-# Set default CCFLAGS for known compilers
-if not conf.env['CCFLAGS']:
- if conf.env['CC'] == 'gcc':
- conf.env.Replace(CCFLAGS = '-Wall -O2 -ggdb -std=c99')
- elif conf.env['CC'] == 'clang':
- conf.env.Replace(CCFLAGS = '-Wall -O2 -g -std=c99')
-if 'CPPFLAGS' in os.environ:
- conf.env.Replace(CPPFLAGS = os.environ['CPPFLAGS'])
-conf.env.Append(CPPDEFINES = {'_FILE_OFFSET_BITS' : 64})
-conf.env.Append(CPPPATH = ['libexfat'])
-if 'LDFLAGS' in os.environ:
- conf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
-conf.env.Append(LIBPATH = ['libexfat'])
-
-# GNU/Linux requires _BSD_SOURCE define for vsyslog(), _XOPEN_SOURCE >= 500 for
-# pread(), pwrite(), snprintf(), strdup(), etc. Everything needed is enabled by
-# _GNU_SOURCE.
-if platform.system() == 'Linux':
- conf.env.Append(CPPDEFINES = '_GNU_SOURCE');
-
-# __DARWIN_64_BIT_INO_T=0 define is needed because since Snow Leopard inode
-# numbers are 64-bit by default, but libfuse operates 32-bit ones. This define
-# forces 32-bit inode declaration in system headers, but it's also possible to
-# link against libfuse_ino64 instead.
-if platform.system() == 'Darwin':
- conf.env.Append(CPPDEFINES = {'__DARWIN_64_BIT_INO_T' : 0})
- conf.env.Append(CPPDEFINES = {'__DARWIN_UNIX03' : 1})
-
-# FreeBSD does not support block devices, only raw devices. Ublio is required
-# for unaligned I/O and caching.
-if platform.system() == 'FreeBSD':
- conf.env.Append(CPPDEFINES = 'USE_UBLIO')
- libs.append('ublio')
- conf.env.Append(CPPPATH = ['/usr/local/include'])
- conf.env.Append(LIBPATH = ['/usr/local/lib'])
-
-env = conf.Finish()
def make_symlink(dir, target, link_name):
workdir = os.getcwd()
return
target = env.Program(output, sources, LIBS = libs)
if alias:
- Alias('install', Install(destdir, target),
- symlink(destdir, os.path.basename(output), alias))
+ Clean(Alias('install', Install(destdir, target),
+ symlink(destdir, os.path.basename(output), alias)),
+ destdir + '/' + alias)
else:
Alias('install', Install(destdir, target))
- targets.append(target)
env.Library('libexfat/exfat', Glob('libexfat/*.c'))
-program('fuse/*.c', 'fuse/mount.exfat-fuse', 'mount.exfat', [libs + ['fuse']])
+program('fuse/*.c', 'fuse/mount.exfat-fuse', 'mount.exfat', [libs + [libfuse]])
program('dump/*.c', 'dump/dumpexfat', None, libs)
program('fsck/*.c', 'fsck/exfatfsck', 'fsck.exfat', libs)
program('mkfs/*.c', 'mkfs/mkexfatfs', 'mkfs.exfat', libs)
program('label/*.c', 'label/exfatlabel', None, libs)
-
-Default(targets)
main.c (08.11.10)
Prints detailed information about exFAT volume.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
struct exfat_dev* dev;
struct exfat_super_block sb;
- dev = exfat_open(spec, 1);
+ dev = exfat_open(spec, EXFAT_MODE_RO);
if (dev == NULL)
return 1;
puts("");
}
-static int dump_full(const char* spec, int used_sectors)
+static int dump_full(const char* spec, bool used_sectors)
{
struct exfat ef;
uint32_t free_clusters;
{
char** pp;
const char* spec = NULL;
- int sb_only = 0;
- int used_sectors = 0;
+ bool sb_only = false;
+ bool used_sectors = false;
printf("dumpexfat %u.%u.%u\n",
EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
for (pp = argv + 1; *pp; pp++)
{
if (strcmp(*pp, "-s") == 0)
- sb_only = 1;
+ sb_only = true;
else if (strcmp(*pp, "-u") == 0)
- used_sectors = 1;
+ used_sectors = true;
else if (strcmp(*pp, "-v") == 0)
{
- puts("Copyright (C) 2011, 2012 Andrew Nayenko");
+ puts("Copyright (C) 2011-2013 Andrew Nayenko");
return 0;
}
else if (spec == NULL)
main.c (02.09.09)
exFAT file system checker.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
uint64_t files_count, directories_count;
-static void nodeck(struct exfat* ef, struct exfat_node* node)
+static int nodeck(struct exfat* ef, struct exfat_node* node)
{
const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb);
cluster_t clusters = (node->size + cluster_size - 1) / cluster_size;
cluster_t c = node->start_cluster;
-
+ int rc = 0;
+
while (clusters--)
{
if (CLUSTER_INVALID(c))
char name[EXFAT_NAME_MAX + 1];
exfat_get_name(node, name, EXFAT_NAME_MAX);
- exfat_error("file `%s' has invalid cluster", name);
- return;
+ exfat_error("file `%s' has invalid cluster 0x%x", name, c);
+ rc = 1;
+ break;
}
if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0)
{
exfat_get_name(node, name, EXFAT_NAME_MAX);
exfat_error("cluster 0x%x of file `%s' is not allocated", c, name);
+ rc = 1;
}
c = exfat_next_cluster(ef, node, c);
}
+ return rc;
}
static void dirck(struct exfat* ef, const char* path)
exfat_bug("directory `%s' is not found", path);
if (!(parent->flags & EXFAT_ATTRIB_DIR))
exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags);
+ if (nodeck(ef, parent) != 0)
+ return;
path_length = strlen(path);
entry_path = malloc(path_length + 1 + EXFAT_NAME_MAX);
dirck(ef, entry_path);
}
else
+ {
files_count++;
- nodeck(ef, node);
+ nodeck(ef, node);
+ }
exfat_put_node(ef, node);
}
exfat_closedir(ef, &it);
{
if (strcmp(*pp, "-v") == 0)
{
- puts("Copyright (C) 2011, 2012 Andrew Nayenko");
+ puts("Copyright (C) 2011-2013 Andrew Nayenko");
return 0;
}
else if (spec == NULL)
main.c (20.01.11)
Prints or changes exFAT volume label.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
{
printf("exfatlabel %u.%u.%u\n", EXFAT_VERSION_MAJOR,
EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
- puts("Copyright (C) 2011, 2012 Andrew Nayenko");
+ puts("Copyright (C) 2011-2013 Andrew Nayenko");
return 0;
}
byteorder.h (12.01.10)
Endianness stuff. exFAT uses little-endian byte order.
- 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
cluster.c (03.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
{
node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster);
if (CLUSTER_INVALID(node->fptr_cluster))
- break;
+ break; /* the caller should handle this and print appropriate
+ error message */
}
node->fptr_index = count;
return node->fptr_cluster;
}
-static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start,
- cluster_t end)
+static cluster_t find_bit_and_set(uint8_t* bitmap, size_t start, size_t end)
{
- const cluster_t mid_start = (start + 7) / 8 * 8;
- const cluster_t mid_end = end / 8 * 8;
- cluster_t c;
- cluster_t byte;
-
- for (c = start; c < mid_start; c++)
- if (BMAP_GET(bitmap, c) == 0)
- {
- BMAP_SET(bitmap, c);
- return c + EXFAT_FIRST_DATA_CLUSTER;
- }
-
- for (byte = mid_start / 8; byte < mid_end / 8; byte++)
- if (bitmap[byte] != 0xff)
- {
- cluster_t bit;
-
- for (bit = 0; bit < 8; bit++)
- if (!(bitmap[byte] & (1u << bit)))
- {
- bitmap[byte] |= (1u << bit);
- return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER;
- }
- }
-
- for (c = mid_end; c < end; c++)
- if (BMAP_GET(bitmap, c) == 0)
- {
- BMAP_SET(bitmap, c);
- return c + EXFAT_FIRST_DATA_CLUSTER;
- }
+ const size_t start_index = start / 8;
+ const size_t end_index = DIV_ROUND_UP(end, 8);
+ size_t i;
+ size_t c;
+ for (i = start_index; i < end_index; i++)
+ {
+ if (bitmap[i] == 0xff)
+ continue;
+ for (c = MAX(i * 8, start); c < MIN((i + 1) * 8, end); c++)
+ if (BMAP_GET(bitmap, c) == 0)
+ {
+ BMAP_SET(bitmap, c);
+ return c + EXFAT_FIRST_DATA_CLUSTER;
+ }
+ }
return EXFAT_CLUSTER_END;
}
{
exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8,
exfat_c2o(ef, ef->cmap.start_cluster));
- ef->cmap.dirty = 0;
+ ef->cmap.dirty = false;
}
static void set_next_cluster(const struct exfat* ef, int contiguous,
return EXFAT_CLUSTER_END;
}
- ef->cmap.dirty = 1;
+ ef->cmap.dirty = true;
return cluster;
}
static void free_cluster(struct exfat* ef, cluster_t cluster)
{
if (CLUSTER_INVALID(cluster))
- exfat_bug("attempting to free invalid cluster");
- if (cluster < EXFAT_FIRST_DATA_CLUSTER ||
- cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
- exfat_bug("bad cluster 0x%x (0x%x)", cluster, ef->cmap.size);
+ exfat_bug("freeing invalid cluster 0x%x", cluster);
+ if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size)
+ exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster,
+ ef->cmap.size);
BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER);
- ef->cmap.dirty = 1;
+ ef->cmap.dirty = true;
}
static void make_noncontiguous(const struct exfat* ef, cluster_t first,
previous = exfat_advance_cluster(ef, node, current - 1);
if (CLUSTER_INVALID(previous))
{
- exfat_error("invalid cluster in file");
+ exfat_error("invalid cluster 0x%x while growing", previous);
return -EIO;
}
}
current - difference - 1);
if (CLUSTER_INVALID(last))
{
- exfat_error("invalid cluster in file");
+ exfat_error("invalid cluster 0x%x while shrinking", last);
return -EIO;
}
previous = exfat_next_cluster(ef, node, last);
{
if (CLUSTER_INVALID(previous))
{
- exfat_error("invalid cluster in file");
+ exfat_error("invalid cluster 0x%x while freeing after shrink",
+ previous);
return -EIO;
}
next = exfat_next_cluster(ef, node, previous);
begin / CLUSTER_SIZE(*ef->sb));
if (CLUSTER_INVALID(cluster))
{
- exfat_error("invalid cluster in file");
+ exfat_error("invalid cluster 0x%x while erasing", cluster);
return -EIO;
}
/* erase from the beginning to the closest cluster boundary */
cluster = exfat_next_cluster(ef, node, cluster);
/* the cluster cannot be invalid because we have just allocated it */
if (CLUSTER_INVALID(cluster))
- exfat_bug("invalid cluster in file");
+ exfat_bug("invalid cluster 0x%x after allocation", cluster);
erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster));
cluster_boundary += CLUSTER_SIZE(*ef->sb);
}
Definitions of structures and constants used in exFAT file system
implementation.
- 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "exfatfs.h"
#define IS_CONTIGUOUS(node) (((node).flags & EXFAT_ATTRIB_CONTIGUOUS) != 0)
#define SECTOR_SIZE(sb) (1 << (sb).sector_bits)
#define CLUSTER_SIZE(sb) (SECTOR_SIZE(sb) << (sb).spc_bits)
-#define CLUSTER_INVALID(c) ((c) > EXFAT_LAST_DATA_CLUSTER)
+#define CLUSTER_INVALID(c) \
+ ((c) < EXFAT_FIRST_DATA_CLUSTER || (c) > EXFAT_LAST_DATA_CLUSTER)
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
le16_t name[EXFAT_NAME_MAX + 1];
};
+enum exfat_mode
+{
+ EXFAT_MODE_RO,
+ EXFAT_MODE_RW,
+ EXFAT_MODE_ANY,
+};
+
struct exfat_dev;
struct exfat
uint32_t size; /* in bits */
uint8_t* chunk;
uint32_t chunk_size; /* in bits */
- int dirty;
+ bool dirty;
}
cmap;
char label[EXFAT_ENAME_MAX * 6 + 1]; /* a character can occupy up to
uid_t uid;
gid_t gid;
int ro;
- int ro_fallback;
- int noatime;
+ bool noatime;
};
/* in-core nodes iterator */
void exfat_debug(const char* format, ...)
__attribute__((format(printf, 1, 2)));
-struct exfat_dev* exfat_open(const char* spec, int ro);
+struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode);
int exfat_close(struct exfat_dev* dev);
int exfat_fsync(struct exfat_dev* dev);
+enum exfat_mode exfat_get_mode(const struct exfat_dev* dev);
+off_t exfat_get_size(const struct exfat_dev* dev);
off_t exfat_seek(struct exfat_dev* dev, off_t offset, int whence);
ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size);
ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size);
exfatfs.h (29.08.09)
Definitions of structures and constants used in exFAT file system.
- 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
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
#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
};
-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;
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);
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_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
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;
}
{
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);
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;
}
{
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);
log.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
lookup.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
return 0;
}
-static int is_last_comp(const char* comp, size_t length)
+static bool is_last_comp(const char* comp, size_t length)
{
const char* p = comp + length;
return get_comp(p, &p) == 0;
}
-static int is_allowed(const char* comp, size_t length)
+static bool is_allowed(const char* comp, size_t length)
{
size_t i;
case '<':
case '>':
case '|':
- return 0;
+ return false;
}
- return 1;
+ return true;
}
int exfat_split(struct exfat* ef, struct exfat_node** parent,
mount.c (22.10.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
return strtol(p, NULL, base);
}
-static int match_option(const char* options, const char* option_name)
+static bool match_option(const char* options, const char* option_name)
{
const char* p;
size_t length = strlen(option_name);
for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name))
if ((p == options || p[-1] == ',') &&
(p[length] == ',' || p[length] == '\0'))
- return 1;
- return 0;
+ return true;
+ return false;
}
static void parse_options(struct exfat* ef, const char* options)
ef->uid = get_int_option(options, "uid", 10, geteuid());
ef->gid = get_int_option(options, "gid", 10, getegid());
- ef->ro = match_option(options, "ro");
ef->noatime = match_option(options, "noatime");
}
int exfat_mount(struct exfat* ef, const char* spec, const char* options)
{
int rc;
+ enum exfat_mode mode;
exfat_tzset();
memset(ef, 0, sizeof(struct exfat));
parse_options(ef, options);
- ef->dev = exfat_open(spec, ef->ro);
+ if (match_option(options, "ro"))
+ mode = EXFAT_MODE_RO;
+ else if (match_option(options, "ro_fallback"))
+ mode = EXFAT_MODE_ANY;
+ else
+ mode = EXFAT_MODE_RW;
+ ef->dev = exfat_open(spec, mode);
if (ef->dev == NULL)
+ return -EIO;
+ if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO)
{
- if (ef->ro || !match_option(options, "ro_fallback"))
- return -EIO;
- ef->dev = exfat_open(spec, 1);
- if (ef->dev == NULL)
- return -EIO;
- exfat_warn("device is write-protected, mounting read-only");
- ef->ro_fallback = ef->ro = 1;
+ if (mode == EXFAT_MODE_ANY)
+ ef->ro = -1;
+ else
+ ef->ro = 1;
}
ef->sb = malloc(sizeof(struct exfat_super_block));
node.c (09.10.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
it->cluster = exfat_next_cluster(ef, parent, it->cluster);
if (CLUSTER_INVALID(it->cluster))
{
- exfat_error("invalid cluster while reading directory");
+ exfat_error("invalid cluster 0x%x while reading directory",
+ it->cluster);
return 1;
}
exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb),
le16_t* namep = NULL;
uint16_t reference_checksum = 0;
uint16_t actual_checksum = 0;
+ uint64_t real_size = 0;
*node = NULL;
}
init_node_meta2(*node, meta2);
actual_checksum = exfat_add_checksum(entry, actual_checksum);
- /* There are two fields that contain file size. Maybe they plan
- to add compression support in the future and one of those
- fields is visible (uncompressed) size and the other is real
- (compressed) size. Anyway, currently it looks like exFAT does
- not support compression and both fields must be equal. */
- if (le64_to_cpu(meta2->real_size) != (*node)->size)
- {
- exfat_error("real size does not equal to size "
- "(%"PRIu64" != %"PRIu64")",
- le64_to_cpu(meta2->real_size), (*node)->size);
- goto error;
- }
+ real_size = le64_to_cpu(meta2->real_size);
/* empty files must be marked as non-contiguous */
if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
{
if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
(*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
{
- char buffer[EXFAT_NAME_MAX + 1];
-
- exfat_get_name(*node, buffer, EXFAT_NAME_MAX);
- exfat_error("directory `%s' has invalid size %"PRIu64" bytes",
- buffer, (*node)->size);
+ exfat_error("directory has invalid size %"PRIu64" bytes",
+ (*node)->size);
goto error;
}
--continuations;
namep += EXFAT_ENAME_MAX;
if (--continuations == 0)
{
+ /*
+ There are two fields that contain file size. Maybe they
+ plan to add compression support in the future and one of
+ those fields is visible (uncompressed) size and the other
+ is real (compressed) size. Anyway, currently it looks like
+ exFAT does not support compression and both fields must be
+ equal.
+
+ There is an exception though: pagefile.sys (its real_size
+ is always 0).
+ */
+ if (real_size != (*node)->size)
+ {
+ char buffer[EXFAT_NAME_MAX + 1];
+
+ exfat_get_name(*node, buffer, EXFAT_NAME_MAX);
+ exfat_error("`%s' real size does not equal to size "
+ "(%"PRIu64" != %"PRIu64")", buffer,
+ real_size, (*node)->size);
+ goto error;
+ }
if (actual_checksum != reference_checksum)
{
- exfat_error("invalid checksum (0x%hx != 0x%hx)",
- actual_checksum, reference_checksum);
+ char buffer[EXFAT_NAME_MAX + 1];
+
+ exfat_get_name(*node, buffer, EXFAT_NAME_MAX);
+ exfat_error("`%s' has invalid checksum (0x%hx != 0x%hx)",
+ buffer, actual_checksum, reference_checksum);
goto error;
}
if (fetch_next_entry(ef, parent, it) != 0)
upcase = (const struct exfat_entry_upcase*) entry;
if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
{
- exfat_error("invalid cluster in upcase table");
+ exfat_error("invalid cluster 0x%x in upcase table",
+ le32_to_cpu(upcase->start_cluster));
goto error;
}
if (le64_to_cpu(upcase->size) == 0 ||
case EXFAT_ENTRY_BITMAP:
bitmap = (const struct exfat_entry_bitmap*) entry;
- if (CLUSTER_INVALID(le32_to_cpu(bitmap->start_cluster)))
+ ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
+ if (CLUSTER_INVALID(ef->cmap.start_cluster))
{
- exfat_error("invalid cluster in clusters bitmap");
+ exfat_error("invalid cluster 0x%x in clusters bitmap",
+ ef->cmap.start_cluster);
goto error;
}
ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
le64_to_cpu(bitmap->size), (ef->cmap.size + 7) / 8);
goto error;
}
- ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
/* FIXME bitmap can be rather big, up to 512 MB */
ef->cmap.chunk_size = ef->cmap.size;
ef->cmap.chunk = malloc(le64_to_cpu(bitmap->size));
return 0;
}
-static void reset_cache(struct exfat* ef, struct exfat_node* node)
+static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
+{
+ node->parent = dir;
+ if (dir->child)
+ {
+ dir->child->prev = node;
+ node->next = dir->child;
+ }
+ dir->child = node;
+}
+
+static void tree_detach(struct exfat_node* node)
{
- struct exfat_node* child;
- struct exfat_node* next;
+ if (node->prev)
+ node->prev->next = node->next;
+ else /* this is the first node in the list */
+ node->parent->child = node->next;
+ if (node->next)
+ node->next->prev = node->prev;
+ node->parent = NULL;
+ node->prev = NULL;
+ node->next = NULL;
+}
- for (child = node->child; child; child = next)
+static void reset_cache(struct exfat* ef, struct exfat_node* node)
+{
+ while (node->child)
{
- reset_cache(ef, child);
- next = child->next;
- free(child);
+ struct exfat_node* p = node->child;
+ reset_cache(ef, p);
+ tree_detach(p);
+ free(p);
}
+ node->flags &= ~EXFAT_ATTRIB_CACHED;
if (node->references != 0)
{
char buffer[EXFAT_NAME_MAX + 1];
exfat_warn("non-zero reference counter (%d) for `%s'",
node->references, buffer);
}
- while (node->references--)
+ while (node->references)
exfat_put_node(ef, node);
- node->child = NULL;
- node->flags &= ~EXFAT_ATTRIB_CACHED;
}
void exfat_reset_cache(struct exfat* ef)
}
}
-static void tree_detach(struct exfat_node* node)
-{
- if (node->prev)
- node->prev->next = node->next;
- else /* this is the first node in the list */
- node->parent->child = node->next;
- if (node->next)
- node->next->prev = node->prev;
- node->parent = NULL;
- node->prev = NULL;
- node->next = NULL;
-}
-
-static void tree_attach(struct exfat_node* dir, struct exfat_node* node)
-{
- node->parent = dir;
- if (dir->child)
- {
- dir->child->prev = node;
- node->next = dir->child;
- }
- dir->child = node;
-}
-
static int shrink_directory(struct exfat* ef, struct exfat_node* dir,
off_t deleted_offset)
{
}
if (existing != NULL)
{
- if (existing->flags & EXFAT_ATTRIB_DIR)
+ /* remove target if it's not the same node as source */
+ if (existing != node)
{
- if (node->flags & EXFAT_ATTRIB_DIR)
- rc = exfat_rmdir(ef, existing);
+ if (existing->flags & EXFAT_ATTRIB_DIR)
+ {
+ if (node->flags & EXFAT_ATTRIB_DIR)
+ rc = exfat_rmdir(ef, existing);
+ else
+ rc = -ENOTDIR;
+ }
else
- rc = -ENOTDIR;
+ {
+ if (!(node->flags & EXFAT_ATTRIB_DIR))
+ rc = exfat_unlink(ef, existing);
+ else
+ rc = -EISDIR;
+ }
+ exfat_put_node(ef, existing);
+ if (rc != 0)
+ {
+ exfat_put_node(ef, dir);
+ exfat_put_node(ef, node);
+ return rc;
+ }
}
else
- {
- if (!(node->flags & EXFAT_ATTRIB_DIR))
- rc = exfat_unlink(ef, existing);
- else
- rc = -EISDIR;
- }
- exfat_put_node(ef, existing);
- if (rc != 0)
- {
- exfat_put_node(ef, dir);
- exfat_put_node(ef, node);
- return rc;
- }
+ exfat_put_node(ef, existing);
}
rc = find_slot(ef, dir, &cluster, &offset,
time.c (03.02.12)
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
utf.c (13.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
utils.c (04.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
version.h (12.06.10)
Version constants.
- 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
#ifndef VERSION_H_INCLUDED
#define VERSION_H_INCLUDED
-#define EXFAT_VERSION_MAJOR 0
-#define EXFAT_VERSION_MINOR 9
-#define EXFAT_VERSION_PATCH 8
+#define EXFAT_VERSION_MAJOR 1
+#define EXFAT_VERSION_MINOR 0
+#define EXFAT_VERSION_PATCH 0
#endif /* ifndef VERSION_H_INCLUDED */
cbm.c (09.11.10)
Clusters Bitmap creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
cbm.h (09.11.10)
Clusters Bitmap creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
fat.c (09.11.10)
File Allocation Table creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
fat.h (09.11.10)
File Allocation Table creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
main.c (15.08.10)
Creates exFAT file system.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
return get_sector_size() << get_spc_bits();
}
-static off_t setup_volume_size(struct exfat_dev* dev)
-{
- off_t size = exfat_seek(dev, 0, SEEK_END);
- if (size == (off_t) -1)
- exfat_error("failed to get volume size");
- return size;
-}
-
static int setup_spc_bits(int sector_bits, int user_defined, off_t volume_size)
{
int i;
{
param.sector_bits = sector_bits;
param.first_sector = first_sector;
-
- param.volume_size = setup_volume_size(dev);
- if (param.volume_size == (off_t) -1)
- return 1;
+ param.volume_size = exfat_get_size(dev);
param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size);
if (param.spc_bits == -1)
}
else if (strcmp(*pp, "-v") == 0)
{
- puts("Copyright (C) 2011, 2012 Andrew Nayenko");
+ puts("Copyright (C) 2011-2013 Andrew Nayenko");
return 0;
}
else if (spec == NULL)
if (spec == NULL)
usage(argv[0]);
- dev = exfat_open(spec, 0);
+ dev = exfat_open(spec, EXFAT_MODE_RW);
if (dev == NULL)
return 1;
if (setup(dev, 9, spc_bits, volume_label, volume_serial,
mkexfat.c (22.04.12)
FS creation engine.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
mkexfat.h (09.11.10)
FS creation engine.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
rootdir.c (09.11.10)
Root directory creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
rootdir.h (09.11.10)
Root directory creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
uct.c (09.11.10)
Upper Case Table creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
uct.h (09.11.10)
Upper Case Table creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
uctc.c (30.04.12)
Upper Case Table contents.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
uctc.h (30.10.10)
Upper Case Table declaration.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
vbr.c (09.11.10)
Volume Boot Record creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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
vbr.h (09.11.10)
Volume Boot Record creation code.
- Copyright (C) 2011, 2012 Andrew Nayenko
+ Copyright (C) 2011-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