+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 (01.09.09)
FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
- 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
#error FUSE 2.6 or later is required
#endif
-const char* default_options = "ro_fallback,allow_other,blkdev";
+const char* default_options = "ro_fallback,allow_other,blkdev,big_writes,"
+ "defer_permissions";
struct exfat ef;
struct exfat_node* node;
int rc;
- exfat_debug("[fuse_exfat_getattr] %s", path);
+ exfat_debug("[%s] %s", __func__, path);
rc = exfat_lookup(&ef, &node, path);
if (rc != 0)
struct exfat_node* node;
int rc;
- exfat_debug("[fuse_exfat_truncate] %s, %"PRIu64, path, (uint64_t) size);
+ exfat_debug("[%s] %s, %"PRId64, __func__, path, size);
rc = exfat_lookup(&ef, &node, path);
if (rc != 0)
int rc;
char name[EXFAT_NAME_MAX + 1];
- exfat_debug("[fuse_exfat_readdir] %s", path);
+ exfat_debug("[%s] %s", __func__, path);
rc = exfat_lookup(&ef, &parent, path);
if (rc != 0)
while ((node = exfat_readdir(&ef, &it)))
{
exfat_get_name(node, name, EXFAT_NAME_MAX);
- exfat_debug("[fuse_exfat_readdir] %s: %s, %"PRIu64" bytes, cluster %u",
+ exfat_debug("[%s] %s: %s, %"PRId64" bytes, cluster 0x%x", __func__,
name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
- (uint64_t) node->size, node->start_cluster);
+ node->size, node->start_cluster);
filler(buffer, name, NULL, 0);
exfat_put_node(&ef, node);
}
struct exfat_node* node;
int rc;
- exfat_debug("[fuse_exfat_open] %s", path);
+ exfat_debug("[%s] %s", __func__, path);
rc = exfat_lookup(&ef, &node, path);
if (rc != 0)
static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
{
+ exfat_debug("[%s] %s", __func__, path);
exfat_put_node(&ef, get_node(fi));
return 0;
}
static int fuse_exfat_read(const char* path, char* buffer, size_t size,
off_t offset, struct fuse_file_info* fi)
{
- exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
- return exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
+ exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
+ if (exfat_generic_pread(&ef, get_node(fi), buffer, size, offset) != size)
+ return EOF;
+ return size;
}
static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
off_t offset, struct fuse_file_info* fi)
{
- exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
- return exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
+ exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
+ if (exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset) != size)
+ return EOF;
+ return size;
}
static int fuse_exfat_unlink(const char* path)
struct exfat_node* node;
int rc;
- exfat_debug("[fuse_exfat_unlink] %s", path);
+ exfat_debug("[%s] %s", __func__, path);
rc = exfat_lookup(&ef, &node, path);
if (rc != 0)
struct exfat_node* node;
int rc;
- exfat_debug("[fuse_exfat_rmdir] %s", path);
+ exfat_debug("[%s] %s", __func__, path);
rc = exfat_lookup(&ef, &node, path);
if (rc != 0)
static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
{
- exfat_debug("[fuse_exfat_mknod] %s", path);
+ exfat_debug("[%s] %s 0%ho", __func__, path, mode);
return exfat_mknod(&ef, path);
}
static int fuse_exfat_mkdir(const char* path, mode_t mode)
{
- exfat_debug("[fuse_exfat_mkdir] %s", path);
+ exfat_debug("[%s] %s 0%ho", __func__, path, mode);
return exfat_mkdir(&ef, path);
}
static int fuse_exfat_rename(const char* old_path, const char* new_path)
{
- exfat_debug("[fuse_exfat_rename] %s => %s", old_path, new_path);
+ exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
return exfat_rename(&ef, old_path, new_path);
}
struct exfat_node* node;
int rc;
- exfat_debug("[fuse_exfat_utimens] %s", path);
+ exfat_debug("[%s] %s", __func__, path);
rc = exfat_lookup(&ef, &node, path);
if (rc != 0)
return 0;
}
+#ifdef __APPLE__
+static int fuse_exfat_chmod(const char* path, mode_t mode)
+{
+ exfat_debug("[%s] %s 0%ho", __func__, path, mode);
+ /* make OS X utilities happy */
+ return 0;
+}
+#endif
+
static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
{
+ exfat_debug("[%s]", __func__);
+
sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
return 0;
}
+static void* fuse_exfat_init(struct fuse_conn_info* fci)
+{
+ exfat_debug("[%s]", __func__);
+#ifdef FUSE_CAP_BIG_WRITES
+ fci->want |= FUSE_CAP_BIG_WRITES;
+#endif
+ return NULL;
+}
+
static void fuse_exfat_destroy(void* unused)
{
+ exfat_debug("[%s]", __func__);
exfat_unmount(&ef);
}
.mkdir = fuse_exfat_mkdir,
.rename = fuse_exfat_rename,
.utimens = fuse_exfat_utimens,
+#ifdef __APPLE__
+ .chmod = fuse_exfat_chmod,
+#endif
.statfs = fuse_exfat_statfs,
+ .init = fuse_exfat_init,
.destroy = fuse_exfat_destroy,
};
static char* add_fsname_option(char* options, const char* spec)
{
- char spec_abs[PATH_MAX];
+ char* spec_abs = realpath(spec, NULL);
- if (realpath(spec, spec_abs) == NULL)
+ if (spec_abs == NULL)
{
free(options);
exfat_error("failed to get absolute path for `%s'", spec);
return NULL;
}
- return add_option(options, "fsname", spec_abs);
+ options = add_option(options, "fsname", spec_abs);
+ free(spec_abs);
+ return options;
}
static char* add_user_option(char* options)
else if (strcmp(*pp, "-v") == 0)
{
free(mount_options);
- puts("Copyright (C) 2010-2012 Andrew Nayenko");
+ puts("Copyright (C) 2010-2013 Andrew Nayenko");
return 0;
}
else if (spec == NULL)
return 1;
}
- if (ef.ro_fallback)
+ if (ef.ro == -1) /* read-only fallback was used */
{
mount_options = add_option(mount_options, "ro", NULL);
if (mount_options == NULL)
}
/* exit session on HUP, TERM and INT signals and ignore PIPE signal */
- if (fuse_set_signal_handlers(fuse_get_session(fh)))
+ if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
{
fuse_unmount(mount_point, fc);
fuse_destroy(fh);
exfat_unmount(&ef);
+ exfat_error("failed to set signal handlers");
return 1;
}
- /* go to background unless "-d" option is passed */
- fuse_daemonize(debug);
-
- /* FUSE main loop */
- fuse_loop(fh);
+ /* go to background (unless "-d" option is passed) and run FUSE
+ main loop */
+ if (fuse_daemonize(debug) == 0)
+ {
+ if (fuse_loop(fh) != 0)
+ exfat_error("FUSE loop failure");
+ }
+ else
+ exfat_error("failed to daemonize");
- /* it's quite illogical but fuse_unmount() must be called BEFORE
- fuse_destroy() */
+ fuse_remove_signal_handlers(fuse_get_session(fh));
+ /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
fuse_unmount(mount_point, fc);
fuse_destroy(fh);
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 */