3 FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
5 Free exFAT implementation.
6 Copyright (C) 2010-2017 Andrew Nayenko
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #define FUSE_USE_VERSION 26
33 #include <sys/types.h>
38 #define exfat_debug(format, ...)
41 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
42 #error FUSE 2.6 or later is required
45 const char* default_options = "ro_fallback,allow_other,blkdev,big_writes,"
46 "default_permissions";
50 static struct exfat_node* get_node(const struct fuse_file_info* fi)
52 return (struct exfat_node*) (size_t) fi->fh;
55 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
57 fi->fh = (uint64_t) (size_t) node;
61 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
63 struct exfat_node* node;
66 exfat_debug("[%s] %s", __func__, path);
68 rc = exfat_lookup(&ef, &node, path);
72 exfat_stat(&ef, node, stbuf);
73 exfat_put_node(&ef, node);
77 static int fuse_exfat_truncate(const char* path, off_t size)
79 struct exfat_node* node;
82 exfat_debug("[%s] %s, %"PRId64, __func__, path, size);
84 rc = exfat_lookup(&ef, &node, path);
88 rc = exfat_truncate(&ef, node, size, true);
91 exfat_flush_node(&ef, node); /* ignore return code */
92 exfat_put_node(&ef, node);
95 rc = exfat_flush_node(&ef, node);
96 exfat_put_node(&ef, node);
100 static int fuse_exfat_readdir(const char* path, void* buffer,
101 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
103 struct exfat_node* parent;
104 struct exfat_node* node;
105 struct exfat_iterator it;
107 char name[EXFAT_UTF8_NAME_BUFFER_MAX];
109 exfat_debug("[%s] %s", __func__, path);
111 rc = exfat_lookup(&ef, &parent, path);
114 if (!(parent->attrib & EXFAT_ATTRIB_DIR))
116 exfat_put_node(&ef, parent);
117 exfat_error("'%s' is not a directory (%#hx)", path, parent->attrib);
121 filler(buffer, ".", NULL, 0);
122 filler(buffer, "..", NULL, 0);
124 rc = exfat_opendir(&ef, parent, &it);
127 exfat_put_node(&ef, parent);
128 exfat_error("failed to open directory '%s'", path);
131 while ((node = exfat_readdir(&ef, &it)))
133 exfat_get_name(node, name);
134 exfat_debug("[%s] %s: %s, %"PRId64" bytes, cluster 0x%x", __func__,
135 name, node->is_contiguous ? "contiguous" : "fragmented",
136 node->size, node->start_cluster);
137 filler(buffer, name, NULL, 0);
138 exfat_put_node(&ef, node);
140 exfat_closedir(&ef, &it);
141 exfat_put_node(&ef, parent);
145 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
147 struct exfat_node* node;
150 exfat_debug("[%s] %s", __func__, path);
152 rc = exfat_lookup(&ef, &node, path);
159 static int fuse_exfat_create(const char* path, mode_t mode,
160 struct fuse_file_info* fi)
162 struct exfat_node* node;
165 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
167 rc = exfat_mknod(&ef, path);
170 rc = exfat_lookup(&ef, &node, path);
177 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
180 This handler is called by FUSE on close() syscall. If the FUSE
181 implementation does not call flush handler, we will flush node here.
182 But in this case we will not be able to return an error to the caller.
183 See fuse_exfat_flush() below.
185 exfat_debug("[%s] %s", __func__, path);
186 exfat_flush_node(&ef, get_node(fi));
187 exfat_put_node(&ef, get_node(fi));
188 return 0; /* FUSE ignores this return value */
191 static int fuse_exfat_flush(const char* path, struct fuse_file_info* fi)
194 This handler may be called by FUSE on close() syscall. FUSE also deals
195 with removals of open files, so we don't free clusters on close but
196 only on rmdir and unlink. If the FUSE implementation does not call this
197 handler we will flush node on release. See fuse_exfat_relase() above.
199 exfat_debug("[%s] %s", __func__, path);
200 return exfat_flush_node(&ef, get_node(fi));
203 static int fuse_exfat_fsync(const char* path, int datasync,
204 struct fuse_file_info *fi)
208 exfat_debug("[%s] %s", __func__, path);
209 rc = exfat_flush_nodes(&ef);
212 rc = exfat_flush(&ef);
215 return exfat_fsync(ef.dev);
218 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
219 off_t offset, struct fuse_file_info* fi)
221 exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
222 return exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
225 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
226 off_t offset, struct fuse_file_info* fi)
228 exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
229 return exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
232 static int fuse_exfat_unlink(const char* path)
234 struct exfat_node* node;
237 exfat_debug("[%s] %s", __func__, path);
239 rc = exfat_lookup(&ef, &node, path);
243 rc = exfat_unlink(&ef, node);
244 exfat_put_node(&ef, node);
247 return exfat_cleanup_node(&ef, node);
250 static int fuse_exfat_rmdir(const char* path)
252 struct exfat_node* node;
255 exfat_debug("[%s] %s", __func__, path);
257 rc = exfat_lookup(&ef, &node, path);
261 rc = exfat_rmdir(&ef, node);
262 exfat_put_node(&ef, node);
265 return exfat_cleanup_node(&ef, node);
268 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
270 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
271 return exfat_mknod(&ef, path);
274 static int fuse_exfat_mkdir(const char* path, mode_t mode)
276 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
277 return exfat_mkdir(&ef, path);
280 static int fuse_exfat_rename(const char* old_path, const char* new_path)
282 exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
283 return exfat_rename(&ef, old_path, new_path);
286 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
288 struct exfat_node* node;
291 exfat_debug("[%s] %s", __func__, path);
293 rc = exfat_lookup(&ef, &node, path);
297 exfat_utimes(node, tv);
298 rc = exfat_flush_node(&ef, node);
299 exfat_put_node(&ef, node);
303 static int fuse_exfat_chmod(const char* path, mode_t mode)
305 const mode_t VALID_MODE_MASK = S_IFREG | S_IFDIR |
306 S_IRWXU | S_IRWXG | S_IRWXO;
308 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
309 if (mode & ~VALID_MODE_MASK)
314 static int fuse_exfat_chown(const char* path, uid_t uid, gid_t gid)
316 exfat_debug("[%s] %s %u:%u", __func__, path, uid, gid);
317 if (uid != ef.uid || gid != ef.gid)
322 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
324 exfat_debug("[%s]", __func__);
326 sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
327 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
328 sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
329 sfs->f_bavail = exfat_count_free_clusters(&ef);
330 sfs->f_bfree = sfs->f_bavail;
331 sfs->f_namemax = EXFAT_NAME_MAX;
334 Below are fake values because in exFAT there is
335 a) no simple way to count files;
336 b) no such thing as inode;
337 So here we assume that inode = cluster.
339 sfs->f_files = le32_to_cpu(ef.sb->cluster_count);
340 sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
341 sfs->f_ffree = sfs->f_bavail;
346 static void* fuse_exfat_init(struct fuse_conn_info* fci)
348 exfat_debug("[%s]", __func__);
349 #ifdef FUSE_CAP_BIG_WRITES
350 fci->want |= FUSE_CAP_BIG_WRITES;
355 static void fuse_exfat_destroy(void* unused)
357 exfat_debug("[%s]", __func__);
361 static void usage(const char* prog)
363 fprintf(stderr, "Usage: %s [-d] [-o options] [-V] <device> <dir>\n", prog);
367 static struct fuse_operations fuse_exfat_ops =
369 .getattr = fuse_exfat_getattr,
370 .truncate = fuse_exfat_truncate,
371 .readdir = fuse_exfat_readdir,
372 .open = fuse_exfat_open,
373 .create = fuse_exfat_create,
374 .release = fuse_exfat_release,
375 .flush = fuse_exfat_flush,
376 .fsync = fuse_exfat_fsync,
377 .fsyncdir = fuse_exfat_fsync,
378 .read = fuse_exfat_read,
379 .write = fuse_exfat_write,
380 .unlink = fuse_exfat_unlink,
381 .rmdir = fuse_exfat_rmdir,
382 .mknod = fuse_exfat_mknod,
383 .mkdir = fuse_exfat_mkdir,
384 .rename = fuse_exfat_rename,
385 .utimens = fuse_exfat_utimens,
386 .chmod = fuse_exfat_chmod,
387 .chown = fuse_exfat_chown,
388 .statfs = fuse_exfat_statfs,
389 .init = fuse_exfat_init,
390 .destroy = fuse_exfat_destroy,
393 static char* add_option(char* options, const char* name, const char* value)
396 char* optionsf = options;
399 size = strlen(options) + strlen(name) + strlen(value) + 3;
401 size = strlen(options) + strlen(name) + 2;
403 options = realloc(options, size);
407 exfat_error("failed to reallocate options string");
410 strcat(options, ",");
411 strcat(options, name);
414 strcat(options, "=");
415 strcat(options, value);
420 static void escape(char* escaped, const char* orig)
424 if (*orig == ',' || *orig == '\\')
427 while ((*escaped++ = *orig++));
430 static char* add_fsname_option(char* options, const char* spec)
432 /* escaped string cannot be more than twice as big as the original one */
433 char* escaped = malloc(strlen(spec) * 2 + 1);
438 exfat_error("failed to allocate escaped string for %s", spec);
442 /* on some platforms (e.g. Android, Solaris) device names can contain
444 escape(escaped, spec);
445 options = add_option(options, "fsname", escaped);
450 static char* add_user_option(char* options)
457 pw = getpwuid(getuid());
458 if (pw == NULL || pw->pw_name == NULL)
461 exfat_error("failed to determine username");
464 return add_option(options, "user", pw->pw_name);
467 static char* add_blksize_option(char* options, long cluster_size)
469 long page_size = sysconf(_SC_PAGESIZE);
475 snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
476 return add_option(options, "blksize", blksize);
479 static char* add_fuse_options(char* options, const char* spec)
481 options = add_fsname_option(options, spec);
484 options = add_user_option(options);
487 options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb));
494 int main(int argc, char* argv[])
496 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
497 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
498 const char* spec = NULL;
499 const char* mount_point = NULL;
502 struct fuse_chan* fc = NULL;
503 struct fuse* fh = NULL;
506 printf("FUSE exfat %s\n", VERSION);
508 mount_options = strdup(default_options);
509 if (mount_options == NULL)
511 exfat_error("failed to allocate options string");
515 while ((opt = getopt(argc, argv, "dno:Vv")) != -1)
525 mount_options = add_option(mount_options, optarg, NULL);
526 if (mount_options == NULL)
531 puts("Copyright (C) 2010-2017 Andrew Nayenko");
541 if (argc - optind != 2)
547 mount_point = argv[optind + 1];
549 if (exfat_mount(&ef, spec, mount_options) != 0)
555 if (ef.ro == -1) /* read-only fallback was used */
557 mount_options = add_option(mount_options, "ro", NULL);
558 if (mount_options == NULL)
565 mount_options = add_fuse_options(mount_options, spec);
566 if (mount_options == NULL)
572 /* create arguments for fuse_mount() */
573 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
574 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
575 fuse_opt_add_arg(&mount_args, mount_options) != 0)
584 /* create FUSE mount point */
585 fc = fuse_mount(mount_point, &mount_args);
586 fuse_opt_free_args(&mount_args);
593 /* create arguments for fuse_new() */
594 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
595 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
597 fuse_unmount(mount_point, fc);
602 /* create new FUSE file system */
603 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
604 sizeof(struct fuse_operations), NULL);
605 fuse_opt_free_args(&newfs_args);
608 fuse_unmount(mount_point, fc);
613 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
614 if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
616 fuse_unmount(mount_point, fc);
619 exfat_error("failed to set signal handlers");
623 /* go to background (unless "-d" option is passed) and run FUSE
625 if (fuse_daemonize(debug) == 0)
627 if (fuse_loop(fh) != 0)
628 exfat_error("FUSE loop failure");
631 exfat_error("failed to daemonize");
633 fuse_remove_signal_handlers(fuse_get_session(fh));
634 /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
635 fuse_unmount(mount_point, fc);