3 FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
5 Copyright (C) 2010-2012 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/>.
21 #define FUSE_USE_VERSION 26
31 #include <sys/types.h>
35 #define exfat_debug(format, ...)
37 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
38 #error FUSE 2.6 or later is required
41 const char* default_options = "ro_fallback,allow_other,blkdev";
45 static struct exfat_node* get_node(const struct fuse_file_info* fi)
47 return (struct exfat_node*) (size_t) fi->fh;
50 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
52 fi->fh = (uint64_t) (size_t) node;
55 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
57 struct exfat_node* node;
60 exfat_debug("[fuse_exfat_getattr] %s", path);
62 rc = exfat_lookup(&ef, &node, path);
66 exfat_stat(&ef, node, stbuf);
67 exfat_put_node(&ef, node);
71 static int fuse_exfat_truncate(const char* path, off_t size)
73 struct exfat_node* node;
76 exfat_debug("[fuse_exfat_truncate] %s, %"PRIu64, path, (uint64_t) size);
78 rc = exfat_lookup(&ef, &node, path);
82 rc = exfat_truncate(&ef, node, size);
83 exfat_put_node(&ef, node);
87 static int fuse_exfat_readdir(const char* path, void* buffer,
88 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
90 struct exfat_node* parent;
91 struct exfat_node* node;
92 struct exfat_iterator it;
94 char name[EXFAT_NAME_MAX + 1];
96 exfat_debug("[fuse_exfat_readdir] %s", path);
98 rc = exfat_lookup(&ef, &parent, path);
101 if (!(parent->flags & EXFAT_ATTRIB_DIR))
103 exfat_put_node(&ef, parent);
104 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
108 filler(buffer, ".", NULL, 0);
109 filler(buffer, "..", NULL, 0);
111 rc = exfat_opendir(&ef, parent, &it);
114 exfat_put_node(&ef, parent);
115 exfat_error("failed to open directory `%s'", path);
118 while ((node = exfat_readdir(&ef, &it)))
120 exfat_get_name(node, name, EXFAT_NAME_MAX);
121 exfat_debug("[fuse_exfat_readdir] %s: %s, %"PRIu64" bytes, cluster %u",
122 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
123 (uint64_t) node->size, node->start_cluster);
124 filler(buffer, name, NULL, 0);
125 exfat_put_node(&ef, node);
127 exfat_closedir(&ef, &it);
128 exfat_put_node(&ef, parent);
132 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
134 struct exfat_node* node;
137 exfat_debug("[fuse_exfat_open] %s", path);
139 rc = exfat_lookup(&ef, &node, path);
147 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
149 exfat_put_node(&ef, get_node(fi));
153 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
154 off_t offset, struct fuse_file_info* fi)
156 exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
157 return exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
160 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
161 off_t offset, struct fuse_file_info* fi)
163 exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
164 return exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
167 static int fuse_exfat_unlink(const char* path)
169 struct exfat_node* node;
172 exfat_debug("[fuse_exfat_unlink] %s", path);
174 rc = exfat_lookup(&ef, &node, path);
178 rc = exfat_unlink(&ef, node);
179 exfat_put_node(&ef, node);
183 static int fuse_exfat_rmdir(const char* path)
185 struct exfat_node* node;
188 exfat_debug("[fuse_exfat_rmdir] %s", path);
190 rc = exfat_lookup(&ef, &node, path);
194 rc = exfat_rmdir(&ef, node);
195 exfat_put_node(&ef, node);
199 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
201 exfat_debug("[fuse_exfat_mknod] %s", path);
202 return exfat_mknod(&ef, path);
205 static int fuse_exfat_mkdir(const char* path, mode_t mode)
207 exfat_debug("[fuse_exfat_mkdir] %s", path);
208 return exfat_mkdir(&ef, path);
211 static int fuse_exfat_rename(const char* old_path, const char* new_path)
213 exfat_debug("[fuse_exfat_rename] %s => %s", old_path, new_path);
214 return exfat_rename(&ef, old_path, new_path);
217 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
219 struct exfat_node* node;
222 exfat_debug("[fuse_exfat_utimens] %s", path);
224 rc = exfat_lookup(&ef, &node, path);
228 exfat_utimes(node, tv);
229 exfat_put_node(&ef, node);
234 static int fuse_exfat_chmod(const char* path, mode_t mode)
236 exfat_debug("[fuse_exfat_chmod] %s 0%ho", path, mode);
237 /* make OS X utilities happy */
242 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
244 sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
245 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
246 sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
247 sfs->f_bavail = exfat_count_free_clusters(&ef);
248 sfs->f_bfree = sfs->f_bavail;
249 sfs->f_namemax = EXFAT_NAME_MAX;
252 Below are fake values because in exFAT there is
253 a) no simple way to count files;
254 b) no such thing as inode;
255 So here we assume that inode = cluster.
257 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
258 sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
259 sfs->f_ffree = sfs->f_bavail;
264 static void fuse_exfat_destroy(void* unused)
269 static void usage(const char* prog)
271 fprintf(stderr, "Usage: %s [-d] [-o options] [-v] <device> <dir>\n", prog);
275 static struct fuse_operations fuse_exfat_ops =
277 .getattr = fuse_exfat_getattr,
278 .truncate = fuse_exfat_truncate,
279 .readdir = fuse_exfat_readdir,
280 .open = fuse_exfat_open,
281 .release = fuse_exfat_release,
282 .read = fuse_exfat_read,
283 .write = fuse_exfat_write,
284 .unlink = fuse_exfat_unlink,
285 .rmdir = fuse_exfat_rmdir,
286 .mknod = fuse_exfat_mknod,
287 .mkdir = fuse_exfat_mkdir,
288 .rename = fuse_exfat_rename,
289 .utimens = fuse_exfat_utimens,
291 .chmod = fuse_exfat_chmod,
293 .statfs = fuse_exfat_statfs,
294 .destroy = fuse_exfat_destroy,
297 static char* add_option(char* options, const char* name, const char* value)
302 size = strlen(options) + strlen(name) + strlen(value) + 3;
304 size = strlen(options) + strlen(name) + 2;
306 options = realloc(options, size);
309 exfat_error("failed to reallocate options string");
312 strcat(options, ",");
313 strcat(options, name);
316 strcat(options, "=");
317 strcat(options, value);
322 static char* add_fsname_option(char* options, const char* spec)
324 char spec_abs[PATH_MAX];
326 if (realpath(spec, spec_abs) == NULL)
329 exfat_error("failed to get absolute path for `%s'", spec);
332 return add_option(options, "fsname", spec_abs);
335 static char* add_user_option(char* options)
342 pw = getpwuid(getuid());
343 if (pw == NULL || pw->pw_name == NULL)
346 exfat_error("failed to determine username");
349 return add_option(options, "user", pw->pw_name);
352 static char* add_blksize_option(char* options, long cluster_size)
354 long page_size = sysconf(_SC_PAGESIZE);
360 snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
361 return add_option(options, "blksize", blksize);
364 static char* add_fuse_options(char* options, const char* spec)
366 options = add_fsname_option(options, spec);
369 options = add_user_option(options);
372 options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb));
379 int main(int argc, char* argv[])
381 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
382 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
383 const char* spec = NULL;
384 const char* mount_point = NULL;
387 struct fuse_chan* fc = NULL;
388 struct fuse* fh = NULL;
391 printf("FUSE exfat %u.%u.%u\n",
392 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
394 mount_options = strdup(default_options);
395 if (mount_options == NULL)
397 exfat_error("failed to allocate options string");
401 for (pp = argv + 1; *pp; pp++)
403 if (strcmp(*pp, "-o") == 0)
408 mount_options = add_option(mount_options, *pp, NULL);
409 if (mount_options == NULL)
412 else if (strcmp(*pp, "-d") == 0)
414 else if (strcmp(*pp, "-v") == 0)
417 puts("Copyright (C) 2010-2012 Andrew Nayenko");
420 else if (spec == NULL)
422 else if (mount_point == NULL)
430 if (spec == NULL || mount_point == NULL)
436 if (exfat_mount(&ef, spec, mount_options) != 0)
442 if (ef.ro == -1) /* read-only fallback was used */
444 mount_options = add_option(mount_options, "ro", NULL);
445 if (mount_options == NULL)
452 mount_options = add_fuse_options(mount_options, spec);
453 if (mount_options == NULL)
459 /* create arguments for fuse_mount() */
460 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
461 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
462 fuse_opt_add_arg(&mount_args, mount_options) != 0)
471 /* create FUSE mount point */
472 fc = fuse_mount(mount_point, &mount_args);
473 fuse_opt_free_args(&mount_args);
480 /* create arguments for fuse_new() */
481 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
482 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
484 fuse_unmount(mount_point, fc);
489 /* create new FUSE file system */
490 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
491 sizeof(struct fuse_operations), NULL);
492 fuse_opt_free_args(&newfs_args);
495 fuse_unmount(mount_point, fc);
500 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
501 if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
503 fuse_unmount(mount_point, fc);
506 exfat_error("failed to set signal handlers");
510 /* go to background (unless "-d" option is passed) and run FUSE
512 if (fuse_daemonize(debug) == 0)
514 if (fuse_loop(fh) != 0)
515 exfat_error("FUSE loop failure");
518 exfat_error("failed to daemonize");
520 fuse_remove_signal_handlers(fuse_get_session(fh));
521 /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
522 fuse_unmount(mount_point, fc);