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.
18 #define exfat_debug(format, ...)
20 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
21 #error FUSE 2.6 or later is required
26 static struct exfat_node* get_node(const struct fuse_file_info *fi)
28 return (struct exfat_node*) (size_t) fi->fh;
31 static void set_node(struct fuse_file_info *fi, struct exfat_node* node)
33 fi->fh = (uint64_t) (size_t) node;
36 static int fuse_exfat_getattr(const char *path, struct stat *stbuf)
38 struct exfat_node* node;
41 exfat_debug("[fuse_exfat_getattr] %s", path);
43 rc = exfat_lookup(&ef, &node, path);
47 exfat_stat(node, stbuf);
48 exfat_put_node(&ef, node);
52 static int fuse_exfat_truncate(const char *path, off_t size)
54 struct exfat_node* node;
57 exfat_debug("[fuse_exfat_truncate] %s, %llu", path, size);
59 rc = exfat_lookup(&ef, &node, path);
63 exfat_truncate(&ef, node, size);
64 exfat_put_node(&ef, node);
68 static int fuse_exfat_readdir(const char *path, void *buffer,
69 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
71 struct exfat_node* parent;
72 struct exfat_node* node;
73 struct exfat_iterator it;
75 char name[EXFAT_NAME_MAX + 1];
77 exfat_debug("[fuse_exfat_readdir] %s", path);
79 rc = exfat_lookup(&ef, &parent, path);
82 if (!(parent->flags & EXFAT_ATTRIB_DIR))
84 exfat_put_node(&ef, parent);
85 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
89 filler(buffer, ".", NULL, 0);
90 filler(buffer, "..", NULL, 0);
92 rc = exfat_opendir(&ef, parent, &it);
95 exfat_put_node(&ef, parent);
96 exfat_error("failed to open directory `%s'", path);
99 while ((node = exfat_readdir(&ef, &it)))
101 exfat_get_name(node, name, EXFAT_NAME_MAX);
102 exfat_debug("[fuse_exfat_readdir] %s: %s, %llu bytes, cluster %u",
103 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
104 node->size, node->start_cluster);
105 filler(buffer, name, NULL, 0);
106 exfat_put_node(&ef, node);
108 exfat_closedir(&ef, &it);
109 exfat_put_node(&ef, parent);
113 static int fuse_exfat_open(const char *path, struct fuse_file_info *fi)
115 struct exfat_node* node;
118 exfat_debug("[fuse_exfat_open] %s", path);
120 rc = exfat_lookup(&ef, &node, path);
127 static int fuse_exfat_release(const char *path, struct fuse_file_info *fi)
129 exfat_put_node(&ef, get_node(fi));
133 static int fuse_exfat_read(const char *path, char *buffer, size_t size,
134 off_t offset, struct fuse_file_info *fi)
136 exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
137 return exfat_read(&ef, get_node(fi), buffer, size, offset);
140 static int fuse_exfat_write(const char *path, const char *buffer, size_t size,
141 off_t offset, struct fuse_file_info *fi)
143 exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
144 return exfat_write(&ef, get_node(fi), buffer, size, offset);
147 int fuse_exfat_unlink(const char *path)
149 struct exfat_node* node;
152 exfat_debug("[fuse_exfat_unlink] %s", path);
154 rc = exfat_lookup(&ef, &node, path);
158 rc = exfat_unlink(&ef, node);
159 exfat_put_node(&ef, node);
163 int fuse_exfat_rmdir(const char *path)
165 struct exfat_node* node;
168 exfat_debug("[fuse_exfat_rmdir] %s", path);
170 rc = exfat_lookup(&ef, &node, path);
174 rc = exfat_rmdir(&ef, node);
175 exfat_put_node(&ef, node);
179 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
181 exfat_debug("[fuse_exfat_mknod] %s", path);
182 return exfat_mknod(&ef, path);
185 static int fuse_exfat_mkdir(const char* path, mode_t mode)
187 exfat_debug("[fuse_exfat_mkdir] %s", path);
188 return exfat_mkdir(&ef, path);
191 static int fuse_exfat_statfs(const char *path, struct statvfs *sfs)
193 const uint64_t block_count = le64_to_cpu(ef.sb->block_count);
195 sfs->f_bsize = BLOCK_SIZE(*ef.sb);
196 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
197 sfs->f_blocks = block_count;
198 sfs->f_bavail = block_count - ef.sb->allocated_percent * block_count / 100;
199 sfs->f_bfree = sfs->f_bavail;
200 sfs->f_namemax = EXFAT_NAME_MAX;
203 Below are fake values because in exFAT there is
204 a) no simple way to count files;
205 b) no such thing as inode;
206 So here we assume that inode = cluster.
208 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
209 sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
210 sfs->f_ffree = sfs->f_bavail;
215 void fuse_exfat_destroy(void* unused)
220 static void usage(const char* prog)
222 fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
226 static struct fuse_operations fuse_exfat_ops =
228 .getattr = fuse_exfat_getattr,
229 .truncate = fuse_exfat_truncate,
230 .readdir = fuse_exfat_readdir,
231 .open = fuse_exfat_open,
232 .release = fuse_exfat_release,
233 .read = fuse_exfat_read,
234 .write = fuse_exfat_write,
235 .unlink = fuse_exfat_unlink,
236 .rmdir = fuse_exfat_rmdir,
237 .mknod = fuse_exfat_mknod,
238 .mkdir = fuse_exfat_mkdir,
239 .statfs = fuse_exfat_statfs,
240 .destroy = fuse_exfat_destroy,
243 int main(int argc, char* argv[])
245 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
246 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
247 const char* spec = NULL;
248 const char* mount_point = NULL;
249 const char* mount_options = "";
251 struct fuse_chan* fc = NULL;
252 struct fuse* fh = NULL;
255 printf("FUSE exfat %u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
257 for (pp = argv + 1; *pp; pp++)
259 if (strcmp(*pp, "-o") == 0)
266 else if (strcmp(*pp, "-d") == 0)
268 else if (spec == NULL)
270 else if (mount_point == NULL)
275 if (spec == NULL || mount_point == NULL)
278 /* create arguments for fuse_mount() */
279 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
280 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
281 fuse_opt_add_arg(&mount_args, mount_options) != 0)
284 /* create FUSE mount point */
285 fc = fuse_mount(mount_point, &mount_args);
286 fuse_opt_free_args(&mount_args);
290 /* create arguments for fuse_new() */
291 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
292 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
294 fuse_unmount(mount_point, fc);
298 /* create new FUSE file system */
299 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
300 sizeof(struct fuse_operations), NULL);
301 fuse_opt_free_args(&newfs_args);
304 fuse_unmount(mount_point, fc);
308 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
309 if (fuse_set_signal_handlers(fuse_get_session(fh)))
311 fuse_unmount(mount_point, fc);
316 if (exfat_mount(&ef, spec) != 0)
318 fuse_unmount(mount_point, fc);
323 /* go to background unless "-d" option is passed */
324 fuse_daemonize(debug);
329 /* it's quite illogical but fuse_unmount() must be called BEFORE
331 fuse_unmount(mount_point, fc);