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 static void usage(const char* prog)
143 fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
147 static struct fuse_operations fuse_exfat_ops =
149 .getattr = fuse_exfat_getattr,
150 .readdir = fuse_exfat_readdir,
151 .open = fuse_exfat_open,
152 .release = fuse_exfat_release,
153 .read = fuse_exfat_read,
154 .statfs = fuse_exfat_statfs,
157 int main(int argc, char* argv[])
159 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
160 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
161 const char* spec = NULL;
162 const char* mount_point = NULL;
163 const char* mount_options = "";
164 struct fuse_chan* fc = NULL;
165 struct fuse* fh = NULL;
168 printf("FUSE exfat %u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
170 for (pp = argv + 1; *pp; pp++)
172 if (strcmp(*pp, "-o") == 0)
179 else if (spec == NULL)
181 else if (mount_point == NULL)
186 if (spec == NULL || mount_point == NULL)
189 /* create arguments for fuse_mount() */
190 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
191 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
192 fuse_opt_add_arg(&mount_args, mount_options) != 0)
195 /* create FUSE mount point */
196 fc = fuse_mount(mount_point, &mount_args);
197 fuse_opt_free_args(&mount_args);
201 /* create arguments for fuse_new() */
202 if (fuse_opt_add_arg(&newfs_args, "") != 0)
204 fuse_unmount(mount_point, fc);
208 /* create new FUSE file system */
209 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
210 sizeof(struct fuse_operations), NULL);
211 fuse_opt_free_args(&newfs_args);
214 fuse_unmount(mount_point, fc);
218 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
219 if (fuse_set_signal_handlers(fuse_get_session(fh)))
221 fuse_unmount(mount_point, fc);
226 if (exfat_mount(&ef, spec) != 0)
228 fuse_unmount(mount_point, fc);
236 /* it's quite illogical but fuse_unmount() must be called BEFORE
238 fuse_unmount(mount_point, fc);