3 * FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
5 * Created by Andrew Nayenko on 01.09.09.
6 * This software is distributed under the GNU General Public License
7 * version 3 or any later.
19 #define exfat_debug(format, ...)
21 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
22 #error FUSE 2.6 or later is required
27 static struct exfat_node* get_node(const struct fuse_file_info* fi)
29 return (struct exfat_node*) (size_t) fi->fh;
32 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
34 fi->fh = (uint64_t) (size_t) node;
37 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
39 struct exfat_node* node;
42 exfat_debug("[fuse_exfat_getattr] %s", path);
44 rc = exfat_lookup(&ef, &node, path);
48 exfat_stat(&ef, node, stbuf);
49 exfat_put_node(&ef, node);
53 static int fuse_exfat_truncate(const char* path, off_t size)
55 struct exfat_node* node;
58 exfat_debug("[fuse_exfat_truncate] %s, %"PRIu64, path, (uint64_t) size);
60 rc = exfat_lookup(&ef, &node, path);
64 exfat_truncate(&ef, node, size);
65 exfat_put_node(&ef, node);
69 static int fuse_exfat_readdir(const char* path, void* buffer,
70 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
72 struct exfat_node* parent;
73 struct exfat_node* node;
74 struct exfat_iterator it;
76 char name[EXFAT_NAME_MAX + 1];
78 exfat_debug("[fuse_exfat_readdir] %s", path);
80 rc = exfat_lookup(&ef, &parent, path);
83 if (!(parent->flags & EXFAT_ATTRIB_DIR))
85 exfat_put_node(&ef, parent);
86 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
90 filler(buffer, ".", NULL, 0);
91 filler(buffer, "..", NULL, 0);
93 rc = exfat_opendir(&ef, parent, &it);
96 exfat_put_node(&ef, parent);
97 exfat_error("failed to open directory `%s'", path);
100 while ((node = exfat_readdir(&ef, &it)))
102 exfat_get_name(node, name, EXFAT_NAME_MAX);
103 exfat_debug("[fuse_exfat_readdir] %s: %s, %"PRIu64" bytes, cluster %u",
104 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
105 (uint64_t) node->size, node->start_cluster);
106 filler(buffer, name, NULL, 0);
107 exfat_put_node(&ef, node);
109 exfat_closedir(&ef, &it);
110 exfat_put_node(&ef, parent);
114 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
116 struct exfat_node* node;
119 exfat_debug("[fuse_exfat_open] %s", path);
121 rc = exfat_lookup(&ef, &node, path);
128 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
130 exfat_put_node(&ef, get_node(fi));
134 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
135 off_t offset, struct fuse_file_info* fi)
137 exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
138 return exfat_read(&ef, get_node(fi), buffer, size, offset);
141 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
142 off_t offset, struct fuse_file_info* fi)
144 exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
145 return exfat_write(&ef, get_node(fi), buffer, size, offset);
148 static int fuse_exfat_unlink(const char* path)
150 struct exfat_node* node;
153 exfat_debug("[fuse_exfat_unlink] %s", path);
155 rc = exfat_lookup(&ef, &node, path);
159 rc = exfat_unlink(&ef, node);
160 exfat_put_node(&ef, node);
164 static int fuse_exfat_rmdir(const char* path)
166 struct exfat_node* node;
169 exfat_debug("[fuse_exfat_rmdir] %s", path);
171 rc = exfat_lookup(&ef, &node, path);
175 rc = exfat_rmdir(&ef, node);
176 exfat_put_node(&ef, node);
180 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
182 exfat_debug("[fuse_exfat_mknod] %s", path);
183 return exfat_mknod(&ef, path);
186 static int fuse_exfat_mkdir(const char* path, mode_t mode)
188 exfat_debug("[fuse_exfat_mkdir] %s", path);
189 return exfat_mkdir(&ef, path);
192 static int fuse_exfat_rename(const char* old_path, const char* new_path)
194 exfat_debug("[fuse_exfat_rename] %s => %s", old_path, new_path);
195 return exfat_rename(&ef, old_path, new_path);
198 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
200 struct exfat_node* node;
203 exfat_debug("[fuse_exfat_utimens] %s", path);
205 rc = exfat_lookup(&ef, &node, path);
209 exfat_utimes(node, tv);
210 exfat_put_node(&ef, node);
214 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
216 const uint64_t block_count = le64_to_cpu(ef.sb->block_count);
218 sfs->f_bsize = BLOCK_SIZE(*ef.sb);
219 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
220 sfs->f_blocks = block_count;
221 sfs->f_bavail = block_count - ef.sb->allocated_percent * block_count / 100;
222 sfs->f_bfree = sfs->f_bavail;
223 sfs->f_namemax = EXFAT_NAME_MAX;
226 Below are fake values because in exFAT there is
227 a) no simple way to count files;
228 b) no such thing as inode;
229 So here we assume that inode = cluster.
231 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
232 sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
233 sfs->f_ffree = sfs->f_bavail;
238 static void fuse_exfat_destroy(void* unused)
243 static void usage(const char* prog)
245 fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
249 static struct fuse_operations fuse_exfat_ops =
251 .getattr = fuse_exfat_getattr,
252 .truncate = fuse_exfat_truncate,
253 .readdir = fuse_exfat_readdir,
254 .open = fuse_exfat_open,
255 .release = fuse_exfat_release,
256 .read = fuse_exfat_read,
257 .write = fuse_exfat_write,
258 .unlink = fuse_exfat_unlink,
259 .rmdir = fuse_exfat_rmdir,
260 .mknod = fuse_exfat_mknod,
261 .mkdir = fuse_exfat_mkdir,
262 .rename = fuse_exfat_rename,
263 .utimens = fuse_exfat_utimens,
264 .statfs = fuse_exfat_statfs,
265 .destroy = fuse_exfat_destroy,
268 int main(int argc, char* argv[])
270 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
271 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
272 const char* spec = NULL;
273 const char* mount_point = NULL;
274 const char* mount_options = "";
276 struct fuse_chan* fc = NULL;
277 struct fuse* fh = NULL;
280 printf("FUSE exfat %u.%u.%u\n",
281 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
283 for (pp = argv + 1; *pp; pp++)
285 if (strcmp(*pp, "-o") == 0)
292 else if (strcmp(*pp, "-d") == 0)
294 else if (spec == NULL)
296 else if (mount_point == NULL)
301 if (spec == NULL || mount_point == NULL)
304 /* create arguments for fuse_mount() */
305 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
306 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
307 fuse_opt_add_arg(&mount_args, mount_options) != 0)
310 /* create FUSE mount point */
311 fc = fuse_mount(mount_point, &mount_args);
312 fuse_opt_free_args(&mount_args);
316 /* create arguments for fuse_new() */
317 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
318 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
320 fuse_unmount(mount_point, fc);
324 /* create new FUSE file system */
325 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
326 sizeof(struct fuse_operations), NULL);
327 fuse_opt_free_args(&newfs_args);
330 fuse_unmount(mount_point, fc);
334 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
335 if (fuse_set_signal_handlers(fuse_get_session(fh)))
337 fuse_unmount(mount_point, fc);
342 if (exfat_mount(&ef, spec, mount_options) != 0)
344 fuse_unmount(mount_point, fc);
349 /* go to background unless "-d" option is passed */
350 fuse_daemonize(debug);
355 /* it's quite illogical but fuse_unmount() must be called BEFORE
357 fuse_unmount(mount_point, fc);