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);
52 static int fuse_exfat_readdir(const char *path, void *buffer,
53 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
55 struct exfat_node* parent;
56 struct exfat_node* node;
57 struct exfat_iterator it;
59 char name[EXFAT_NAME_MAX + 1];
61 exfat_debug("[fuse_exfat_readdir] %s", path);
63 rc = exfat_lookup(&ef, &parent, path);
66 if (!(parent->flags & EXFAT_ATTRIB_DIR))
68 exfat_put_node(parent);
72 filler(buffer, ".", NULL, 0);
73 filler(buffer, "..", NULL, 0);
75 exfat_opendir(parent, &it);
76 while (exfat_readdir(&ef, parent, &node, &it) == 0)
78 exfat_get_name(node, name, EXFAT_NAME_MAX);
79 exfat_debug("[fuse_exfat_readdir] %s: %s, %llu bytes, cluster %u",
80 name, IS_CONTIGUOUS(node) ? "contiguous" : "fragmented",
81 node->size, node->start_cluster);
82 filler(buffer, name, NULL, 0);
86 exfat_put_node(parent);
90 static int fuse_exfat_open(const char *path, struct fuse_file_info *fi)
92 struct exfat_node* node;
95 exfat_debug("[fuse_exfat_open] %s", path);
97 rc = exfat_lookup(&ef, &node, path);
104 static int fuse_exfat_release(const char *path, struct fuse_file_info *fi)
106 exfat_put_node(get_node(fi));
110 static int fuse_exfat_read(const char *path, char *buffer, size_t size,
111 off_t offset, struct fuse_file_info *fi)
113 exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
114 return exfat_read(&ef, get_node(fi), buffer, size, offset);
117 static int fuse_exfat_statfs(const char *path, struct statvfs *sfs)
119 const uint64_t block_count = le64_to_cpu(ef.sb->block_count);
121 sfs->f_bsize = BLOCK_SIZE(*ef.sb);
122 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
123 sfs->f_blocks = block_count;
124 sfs->f_bavail = block_count - ef.sb->allocated_percent * block_count / 100;
125 sfs->f_bfree = sfs->f_bavail;
126 sfs->f_namemax = EXFAT_NAME_MAX;
129 Below are fake values because in exFAT there is
130 a) no simple way to count files;
131 b) no such thing as inode;
132 So here we assume that inode = cluster.
134 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
135 sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
136 sfs->f_ffree = sfs->f_bavail;
141 void fuse_exfat_destroy(void* unused)
146 static void usage(const char* prog)
148 fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
152 static struct fuse_operations fuse_exfat_ops =
154 .getattr = fuse_exfat_getattr,
155 .readdir = fuse_exfat_readdir,
156 .open = fuse_exfat_open,
157 .release = fuse_exfat_release,
158 .read = fuse_exfat_read,
159 .statfs = fuse_exfat_statfs,
160 .destroy = fuse_exfat_destroy,
163 int main(int argc, char* argv[])
165 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
166 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
167 const char* spec = NULL;
168 const char* mount_point = NULL;
169 const char* mount_options = "";
170 struct fuse_chan* fc = NULL;
171 struct fuse* fh = NULL;
174 printf("FUSE exfat %u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
176 for (pp = argv + 1; *pp; pp++)
178 if (strcmp(*pp, "-o") == 0)
185 else if (spec == NULL)
187 else if (mount_point == NULL)
192 if (spec == NULL || mount_point == NULL)
195 /* create arguments for fuse_mount() */
196 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
197 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
198 fuse_opt_add_arg(&mount_args, mount_options) != 0)
201 /* create FUSE mount point */
202 fc = fuse_mount(mount_point, &mount_args);
203 fuse_opt_free_args(&mount_args);
207 /* create arguments for fuse_new() */
208 if (fuse_opt_add_arg(&newfs_args, "") != 0)
210 fuse_unmount(mount_point, fc);
214 /* create new FUSE file system */
215 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
216 sizeof(struct fuse_operations), NULL);
217 fuse_opt_free_args(&newfs_args);
220 fuse_unmount(mount_point, fc);
224 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
225 if (fuse_set_signal_handlers(fuse_get_session(fh)))
227 fuse_unmount(mount_point, fc);
232 if (exfat_mount(&ef, spec) != 0)
234 fuse_unmount(mount_point, fc);
242 /* it's quite illogical but fuse_unmount() must be called BEFORE
244 fuse_unmount(mount_point, fc);