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 #define exfat_debug(format, ...)
32 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
33 #error FUSE 2.6 or later is required
38 static struct exfat_node* get_node(const struct fuse_file_info* fi)
40 return (struct exfat_node*) (size_t) fi->fh;
43 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
45 fi->fh = (uint64_t) (size_t) node;
48 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
50 struct exfat_node* node;
53 exfat_debug("[fuse_exfat_getattr] %s", path);
55 rc = exfat_lookup(&ef, &node, path);
59 exfat_stat(&ef, node, stbuf);
60 exfat_put_node(&ef, node);
64 static int fuse_exfat_truncate(const char* path, off_t size)
66 struct exfat_node* node;
69 exfat_debug("[fuse_exfat_truncate] %s, %"PRIu64, path, (uint64_t) size);
71 rc = exfat_lookup(&ef, &node, path);
75 exfat_truncate(&ef, node, size);
76 exfat_put_node(&ef, node);
80 static int fuse_exfat_readdir(const char* path, void* buffer,
81 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
83 struct exfat_node* parent;
84 struct exfat_node* node;
85 struct exfat_iterator it;
87 char name[EXFAT_NAME_MAX + 1];
89 exfat_debug("[fuse_exfat_readdir] %s", path);
91 rc = exfat_lookup(&ef, &parent, path);
94 if (!(parent->flags & EXFAT_ATTRIB_DIR))
96 exfat_put_node(&ef, parent);
97 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
101 filler(buffer, ".", NULL, 0);
102 filler(buffer, "..", NULL, 0);
104 rc = exfat_opendir(&ef, parent, &it);
107 exfat_put_node(&ef, parent);
108 exfat_error("failed to open directory `%s'", path);
111 while ((node = exfat_readdir(&ef, &it)))
113 exfat_get_name(node, name, EXFAT_NAME_MAX);
114 exfat_debug("[fuse_exfat_readdir] %s: %s, %"PRIu64" bytes, cluster %u",
115 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
116 (uint64_t) node->size, node->start_cluster);
117 filler(buffer, name, NULL, 0);
118 exfat_put_node(&ef, node);
120 exfat_closedir(&ef, &it);
121 exfat_put_node(&ef, parent);
125 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
127 struct exfat_node* node;
130 exfat_debug("[fuse_exfat_open] %s", path);
132 rc = exfat_lookup(&ef, &node, path);
139 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
141 exfat_put_node(&ef, get_node(fi));
145 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
146 off_t offset, struct fuse_file_info* fi)
148 exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
149 return exfat_read(&ef, get_node(fi), buffer, size, offset);
152 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
153 off_t offset, struct fuse_file_info* fi)
155 exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
156 return exfat_write(&ef, get_node(fi), buffer, size, offset);
159 static int fuse_exfat_unlink(const char* path)
161 struct exfat_node* node;
164 exfat_debug("[fuse_exfat_unlink] %s", path);
166 rc = exfat_lookup(&ef, &node, path);
170 rc = exfat_unlink(&ef, node);
171 exfat_put_node(&ef, node);
175 static int fuse_exfat_rmdir(const char* path)
177 struct exfat_node* node;
180 exfat_debug("[fuse_exfat_rmdir] %s", path);
182 rc = exfat_lookup(&ef, &node, path);
186 rc = exfat_rmdir(&ef, node);
187 exfat_put_node(&ef, node);
191 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
193 exfat_debug("[fuse_exfat_mknod] %s", path);
194 return exfat_mknod(&ef, path);
197 static int fuse_exfat_mkdir(const char* path, mode_t mode)
199 exfat_debug("[fuse_exfat_mkdir] %s", path);
200 return exfat_mkdir(&ef, path);
203 static int fuse_exfat_rename(const char* old_path, const char* new_path)
205 exfat_debug("[fuse_exfat_rename] %s => %s", old_path, new_path);
206 return exfat_rename(&ef, old_path, new_path);
209 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
211 struct exfat_node* node;
214 exfat_debug("[fuse_exfat_utimens] %s", path);
216 rc = exfat_lookup(&ef, &node, path);
220 exfat_utimes(node, tv);
221 exfat_put_node(&ef, node);
225 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
227 const uint64_t block_count = le64_to_cpu(ef.sb->block_count);
229 sfs->f_bsize = BLOCK_SIZE(*ef.sb);
230 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
231 sfs->f_blocks = block_count;
232 sfs->f_bavail = block_count - ef.sb->allocated_percent * block_count / 100;
233 sfs->f_bfree = sfs->f_bavail;
234 sfs->f_namemax = EXFAT_NAME_MAX;
237 Below are fake values because in exFAT there is
238 a) no simple way to count files;
239 b) no such thing as inode;
240 So here we assume that inode = cluster.
242 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
243 sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
244 sfs->f_ffree = sfs->f_bavail;
249 static void fuse_exfat_destroy(void* unused)
254 static void usage(const char* prog)
256 fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
260 static struct fuse_operations fuse_exfat_ops =
262 .getattr = fuse_exfat_getattr,
263 .truncate = fuse_exfat_truncate,
264 .readdir = fuse_exfat_readdir,
265 .open = fuse_exfat_open,
266 .release = fuse_exfat_release,
267 .read = fuse_exfat_read,
268 .write = fuse_exfat_write,
269 .unlink = fuse_exfat_unlink,
270 .rmdir = fuse_exfat_rmdir,
271 .mknod = fuse_exfat_mknod,
272 .mkdir = fuse_exfat_mkdir,
273 .rename = fuse_exfat_rename,
274 .utimens = fuse_exfat_utimens,
275 .statfs = fuse_exfat_statfs,
276 .destroy = fuse_exfat_destroy,
279 int main(int argc, char* argv[])
281 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
282 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
283 const char* spec = NULL;
284 const char* mount_point = NULL;
285 const char* mount_options = "";
287 struct fuse_chan* fc = NULL;
288 struct fuse* fh = NULL;
291 printf("FUSE exfat %u.%u.%u\n",
292 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
294 for (pp = argv + 1; *pp; pp++)
296 if (strcmp(*pp, "-o") == 0)
303 else if (strcmp(*pp, "-d") == 0)
305 else if (spec == NULL)
307 else if (mount_point == NULL)
312 if (spec == NULL || mount_point == NULL)
315 /* create arguments for fuse_mount() */
316 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
317 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
318 fuse_opt_add_arg(&mount_args, mount_options) != 0)
321 /* create FUSE mount point */
322 fc = fuse_mount(mount_point, &mount_args);
323 fuse_opt_free_args(&mount_args);
327 /* create arguments for fuse_new() */
328 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
329 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
331 fuse_unmount(mount_point, fc);
335 /* create new FUSE file system */
336 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
337 sizeof(struct fuse_operations), NULL);
338 fuse_opt_free_args(&newfs_args);
341 fuse_unmount(mount_point, fc);
345 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
346 if (fuse_set_signal_handlers(fuse_get_session(fh)))
348 fuse_unmount(mount_point, fc);
353 if (exfat_mount(&ef, spec, mount_options) != 0)
355 fuse_unmount(mount_point, fc);
360 /* go to background unless "-d" option is passed */
361 fuse_daemonize(debug);
366 /* it's quite illogical but fuse_unmount() must be called BEFORE
368 fuse_unmount(mount_point, fc);