3 FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
5 Copyright (C) 2009, 2010 Andrew Nayenko
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
34 #define exfat_debug(format, ...)
36 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
37 #error FUSE 2.6 or later is required
40 const char* default_options = "allow_other,blkdev";
44 static struct exfat_node* get_node(const struct fuse_file_info* fi)
46 return (struct exfat_node*) (size_t) fi->fh;
49 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
51 fi->fh = (uint64_t) (size_t) node;
54 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
56 struct exfat_node* node;
59 exfat_debug("[fuse_exfat_getattr] %s", path);
61 rc = exfat_lookup(&ef, &node, path);
65 exfat_stat(&ef, node, stbuf);
66 exfat_put_node(&ef, node);
70 static int fuse_exfat_truncate(const char* path, off_t size)
72 struct exfat_node* node;
75 exfat_debug("[fuse_exfat_truncate] %s, %"PRIu64, path, (uint64_t) size);
77 rc = exfat_lookup(&ef, &node, path);
81 rc = exfat_truncate(&ef, node, size);
82 exfat_put_node(&ef, node);
86 static int fuse_exfat_readdir(const char* path, void* buffer,
87 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
89 struct exfat_node* parent;
90 struct exfat_node* node;
91 struct exfat_iterator it;
93 char name[EXFAT_NAME_MAX + 1];
95 exfat_debug("[fuse_exfat_readdir] %s", path);
97 rc = exfat_lookup(&ef, &parent, path);
100 if (!(parent->flags & EXFAT_ATTRIB_DIR))
102 exfat_put_node(&ef, parent);
103 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
107 filler(buffer, ".", NULL, 0);
108 filler(buffer, "..", NULL, 0);
110 rc = exfat_opendir(&ef, parent, &it);
113 exfat_put_node(&ef, parent);
114 exfat_error("failed to open directory `%s'", path);
117 while ((node = exfat_readdir(&ef, &it)))
119 exfat_get_name(node, name, EXFAT_NAME_MAX);
120 exfat_debug("[fuse_exfat_readdir] %s: %s, %"PRIu64" bytes, cluster %u",
121 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
122 (uint64_t) node->size, node->start_cluster);
123 filler(buffer, name, NULL, 0);
124 exfat_put_node(&ef, node);
126 exfat_closedir(&ef, &it);
127 exfat_put_node(&ef, parent);
131 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
133 struct exfat_node* node;
136 exfat_debug("[fuse_exfat_open] %s", path);
138 rc = exfat_lookup(&ef, &node, path);
145 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
147 exfat_put_node(&ef, get_node(fi));
151 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
152 off_t offset, struct fuse_file_info* fi)
154 exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
155 return exfat_read(&ef, get_node(fi), buffer, size, offset);
158 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
159 off_t offset, struct fuse_file_info* fi)
161 exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
162 return exfat_write(&ef, get_node(fi), buffer, size, offset);
165 static int fuse_exfat_unlink(const char* path)
167 struct exfat_node* node;
170 exfat_debug("[fuse_exfat_unlink] %s", path);
172 rc = exfat_lookup(&ef, &node, path);
176 rc = exfat_unlink(&ef, node);
177 exfat_put_node(&ef, node);
181 static int fuse_exfat_rmdir(const char* path)
183 struct exfat_node* node;
186 exfat_debug("[fuse_exfat_rmdir] %s", path);
188 rc = exfat_lookup(&ef, &node, path);
192 rc = exfat_rmdir(&ef, node);
193 exfat_put_node(&ef, node);
197 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
199 exfat_debug("[fuse_exfat_mknod] %s", path);
200 return exfat_mknod(&ef, path);
203 static int fuse_exfat_mkdir(const char* path, mode_t mode)
205 exfat_debug("[fuse_exfat_mkdir] %s", path);
206 return exfat_mkdir(&ef, path);
209 static int fuse_exfat_rename(const char* old_path, const char* new_path)
211 exfat_debug("[fuse_exfat_rename] %s => %s", old_path, new_path);
212 return exfat_rename(&ef, old_path, new_path);
215 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
217 struct exfat_node* node;
220 exfat_debug("[fuse_exfat_utimens] %s", path);
222 rc = exfat_lookup(&ef, &node, path);
226 exfat_utimes(node, tv);
227 exfat_put_node(&ef, node);
231 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
233 sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
234 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
235 sfs->f_blocks = le64_to_cpu(ef.sb->block_count) >> ef.sb->bpc_bits;
236 sfs->f_bavail = exfat_count_free_clusters(&ef);
237 sfs->f_bfree = sfs->f_bavail;
238 sfs->f_namemax = EXFAT_NAME_MAX;
241 Below are fake values because in exFAT there is
242 a) no simple way to count files;
243 b) no such thing as inode;
244 So here we assume that inode = cluster.
246 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
247 sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
248 sfs->f_ffree = sfs->f_bavail;
253 static void fuse_exfat_destroy(void* unused)
258 static void usage(const char* prog)
260 fprintf(stderr, "Usage: %s [-d] [-o options] <device> <dir>\n", prog);
264 static struct fuse_operations fuse_exfat_ops =
266 .getattr = fuse_exfat_getattr,
267 .truncate = fuse_exfat_truncate,
268 .readdir = fuse_exfat_readdir,
269 .open = fuse_exfat_open,
270 .release = fuse_exfat_release,
271 .read = fuse_exfat_read,
272 .write = fuse_exfat_write,
273 .unlink = fuse_exfat_unlink,
274 .rmdir = fuse_exfat_rmdir,
275 .mknod = fuse_exfat_mknod,
276 .mkdir = fuse_exfat_mkdir,
277 .rename = fuse_exfat_rename,
278 .utimens = fuse_exfat_utimens,
279 .statfs = fuse_exfat_statfs,
280 .destroy = fuse_exfat_destroy,
283 static char* add_option(char* options, const char* name, const char* value)
288 size = strlen(options) + strlen(name) + strlen(value) + 3;
290 size = strlen(options) + strlen(name) + 2;
292 options = realloc(options, size);
295 exfat_error("failed to reallocate options string");
298 strcat(options, ",");
299 strcat(options, name);
302 strcat(options, "=");
303 strcat(options, value);
308 static char* add_fsname_option(char* options, const char* spec)
310 char spec_abs[PATH_MAX];
312 if (realpath(spec, spec_abs) == NULL)
315 exfat_error("failed to get absolute path for `%s'", spec);
318 return add_option(options, "fsname", spec_abs);
321 static char* add_user_option(char* options)
328 pw = getpwuid(getuid());
329 if (pw == NULL || pw->pw_name == NULL)
332 exfat_error("failed to determine username");
335 return add_option(options, "user", pw->pw_name);
338 static char* add_blksize_option(char* options, long cluster_size)
340 long page_size = sysconf(_SC_PAGESIZE);
346 snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
347 return add_option(options, "blksize", blksize);
350 int main(int argc, char* argv[])
352 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
353 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
354 const char* spec = NULL;
355 const char* mount_point = NULL;
358 struct fuse_chan* fc = NULL;
359 struct fuse* fh = NULL;
362 printf("FUSE exfat %u.%u.%u\n",
363 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
365 mount_options = strdup(default_options);
366 if (mount_options == NULL)
368 exfat_error("failed to allocate options string");
372 for (pp = argv + 1; *pp; pp++)
374 if (strcmp(*pp, "-o") == 0)
379 mount_options = add_option(mount_options, *pp, NULL);
380 if (mount_options == NULL)
383 else if (strcmp(*pp, "-d") == 0)
385 else if (spec == NULL)
387 else if (mount_point == NULL)
395 if (spec == NULL || mount_point == NULL)
401 if (exfat_mount(&ef, spec, mount_options) != 0)
407 mount_options = add_fsname_option(mount_options, spec);
408 if (mount_options == NULL)
413 mount_options = add_user_option(mount_options);
414 if (mount_options == NULL)
419 mount_options = add_blksize_option(mount_options, CLUSTER_SIZE(*ef.sb));
420 if (mount_options == NULL)
426 /* create arguments for fuse_mount() */
427 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
428 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
429 fuse_opt_add_arg(&mount_args, mount_options) != 0)
438 /* create FUSE mount point */
439 fc = fuse_mount(mount_point, &mount_args);
440 fuse_opt_free_args(&mount_args);
447 /* create arguments for fuse_new() */
448 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
449 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
451 fuse_unmount(mount_point, fc);
456 /* create new FUSE file system */
457 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
458 sizeof(struct fuse_operations), NULL);
459 fuse_opt_free_args(&newfs_args);
462 fuse_unmount(mount_point, fc);
467 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
468 if (fuse_set_signal_handlers(fuse_get_session(fh)))
470 fuse_unmount(mount_point, fc);
476 /* go to background unless "-d" option is passed */
477 fuse_daemonize(debug);
482 /* it's quite illogical but fuse_unmount() must be called BEFORE
484 fuse_unmount(mount_point, fc);