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 = "ro_fallback,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);
146 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
148 exfat_put_node(&ef, get_node(fi));
152 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
153 off_t offset, struct fuse_file_info* fi)
155 exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
156 return exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
159 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
160 off_t offset, struct fuse_file_info* fi)
162 exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
163 return exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
166 static int fuse_exfat_unlink(const char* path)
168 struct exfat_node* node;
171 exfat_debug("[fuse_exfat_unlink] %s", path);
173 rc = exfat_lookup(&ef, &node, path);
177 rc = exfat_unlink(&ef, node);
178 exfat_put_node(&ef, node);
182 static int fuse_exfat_rmdir(const char* path)
184 struct exfat_node* node;
187 exfat_debug("[fuse_exfat_rmdir] %s", path);
189 rc = exfat_lookup(&ef, &node, path);
193 rc = exfat_rmdir(&ef, node);
194 exfat_put_node(&ef, node);
198 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
200 exfat_debug("[fuse_exfat_mknod] %s", path);
201 return exfat_mknod(&ef, path);
204 static int fuse_exfat_mkdir(const char* path, mode_t mode)
206 exfat_debug("[fuse_exfat_mkdir] %s", path);
207 return exfat_mkdir(&ef, path);
210 static int fuse_exfat_rename(const char* old_path, const char* new_path)
212 exfat_debug("[fuse_exfat_rename] %s => %s", old_path, new_path);
213 return exfat_rename(&ef, old_path, new_path);
216 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
218 struct exfat_node* node;
221 exfat_debug("[fuse_exfat_utimens] %s", path);
223 rc = exfat_lookup(&ef, &node, path);
227 exfat_utimes(node, tv);
228 exfat_put_node(&ef, node);
232 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
234 sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
235 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
236 sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
237 sfs->f_bavail = exfat_count_free_clusters(&ef);
238 sfs->f_bfree = sfs->f_bavail;
239 sfs->f_namemax = EXFAT_NAME_MAX;
242 Below are fake values because in exFAT there is
243 a) no simple way to count files;
244 b) no such thing as inode;
245 So here we assume that inode = cluster.
247 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
248 sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
249 sfs->f_ffree = sfs->f_bavail;
254 static void fuse_exfat_destroy(void* unused)
259 static void usage(const char* prog)
261 fprintf(stderr, "Usage: %s [-d] [-o options] [-v] <device> <dir>\n", prog);
265 static struct fuse_operations fuse_exfat_ops =
267 .getattr = fuse_exfat_getattr,
268 .truncate = fuse_exfat_truncate,
269 .readdir = fuse_exfat_readdir,
270 .open = fuse_exfat_open,
271 .release = fuse_exfat_release,
272 .read = fuse_exfat_read,
273 .write = fuse_exfat_write,
274 .unlink = fuse_exfat_unlink,
275 .rmdir = fuse_exfat_rmdir,
276 .mknod = fuse_exfat_mknod,
277 .mkdir = fuse_exfat_mkdir,
278 .rename = fuse_exfat_rename,
279 .utimens = fuse_exfat_utimens,
280 .statfs = fuse_exfat_statfs,
281 .destroy = fuse_exfat_destroy,
284 static char* add_option(char* options, const char* name, const char* value)
289 size = strlen(options) + strlen(name) + strlen(value) + 3;
291 size = strlen(options) + strlen(name) + 2;
293 options = realloc(options, size);
296 exfat_error("failed to reallocate options string");
299 strcat(options, ",");
300 strcat(options, name);
303 strcat(options, "=");
304 strcat(options, value);
309 static char* add_fsname_option(char* options, const char* spec)
311 char spec_abs[PATH_MAX];
313 if (realpath(spec, spec_abs) == NULL)
316 exfat_error("failed to get absolute path for `%s'", spec);
319 return add_option(options, "fsname", spec_abs);
322 static char* add_user_option(char* options)
329 pw = getpwuid(getuid());
330 if (pw == NULL || pw->pw_name == NULL)
333 exfat_error("failed to determine username");
336 return add_option(options, "user", pw->pw_name);
339 static char* add_blksize_option(char* options, long cluster_size)
341 long page_size = sysconf(_SC_PAGESIZE);
347 snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
348 return add_option(options, "blksize", blksize);
351 static char* add_fuse_options(char* options, const char* spec)
353 options = add_fsname_option(options, spec);
356 options = add_user_option(options);
359 options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb));
366 int main(int argc, char* argv[])
368 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
369 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
370 const char* spec = NULL;
371 const char* mount_point = NULL;
374 struct fuse_chan* fc = NULL;
375 struct fuse* fh = NULL;
378 printf("FUSE exfat %u.%u.%u\n",
379 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
381 mount_options = strdup(default_options);
382 if (mount_options == NULL)
384 exfat_error("failed to allocate options string");
388 for (pp = argv + 1; *pp; pp++)
390 if (strcmp(*pp, "-o") == 0)
395 mount_options = add_option(mount_options, *pp, NULL);
396 if (mount_options == NULL)
399 else if (strcmp(*pp, "-d") == 0)
401 else if (strcmp(*pp, "-v") == 0)
404 puts("Copyright (C) 2009 Andrew Nayenko");
407 else if (spec == NULL)
409 else if (mount_point == NULL)
417 if (spec == NULL || mount_point == NULL)
423 if (exfat_mount(&ef, spec, mount_options) != 0)
431 mount_options = add_option(mount_options, "ro", NULL);
432 if (mount_options == NULL)
439 mount_options = add_fuse_options(mount_options, spec);
440 if (mount_options == NULL)
446 /* create arguments for fuse_mount() */
447 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
448 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
449 fuse_opt_add_arg(&mount_args, mount_options) != 0)
458 /* create FUSE mount point */
459 fc = fuse_mount(mount_point, &mount_args);
460 fuse_opt_free_args(&mount_args);
467 /* create arguments for fuse_new() */
468 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
469 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
471 fuse_unmount(mount_point, fc);
476 /* create new FUSE file system */
477 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
478 sizeof(struct fuse_operations), NULL);
479 fuse_opt_free_args(&newfs_args);
482 fuse_unmount(mount_point, fc);
487 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
488 if (fuse_set_signal_handlers(fuse_get_session(fh)))
490 fuse_unmount(mount_point, fc);
496 /* go to background unless "-d" option is passed */
497 fuse_daemonize(debug);
502 /* it's quite illogical but fuse_unmount() must be called BEFORE
504 fuse_unmount(mount_point, fc);