OSDN Git Service

4fafd7098bc2cb06651c9abefd7d5cb6cff7f447
[android-x86/external-exfat.git] / fuse / main.c
1 /*
2  *  main.c
3  *  FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
4  *
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.
8  */
9
10 #include <fuse.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <exfat.h>
17
18 #define exfat_debug(format, ...)
19
20 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
21         #error FUSE 2.6 or later is required
22 #endif
23
24 struct exfat ef;
25
26 static struct exfat_node* get_node(const struct fuse_file_info* fi)
27 {
28         return (struct exfat_node*) (size_t) fi->fh;
29 }
30
31 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
32 {
33         fi->fh = (uint64_t) (size_t) node;
34 }
35
36 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
37 {
38         struct exfat_node* node;
39         int rc;
40
41         exfat_debug("[fuse_exfat_getattr] %s", path);
42
43         rc = exfat_lookup(&ef, &node, path);
44         if (rc != 0)
45                 return rc;
46
47         exfat_stat(&ef, node, stbuf);
48         exfat_put_node(&ef, node);
49         return 0;
50 }
51
52 static int fuse_exfat_truncate(const char* path, off_t size)
53 {
54         struct exfat_node* node;
55         int rc;
56
57         exfat_debug("[fuse_exfat_truncate] %s, %llu", path, size);
58
59         rc = exfat_lookup(&ef, &node, path);
60         if (rc != 0)
61                 return rc;
62
63         exfat_truncate(&ef, node, size);
64         exfat_put_node(&ef, node);
65         return 0;
66 }
67
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)
70 {
71         struct exfat_node* parent;
72         struct exfat_node* node;
73         struct exfat_iterator it;
74         int rc;
75         char name[EXFAT_NAME_MAX + 1];
76
77         exfat_debug("[fuse_exfat_readdir] %s", path);
78
79         rc = exfat_lookup(&ef, &parent, path);
80         if (rc != 0)
81                 return rc;
82         if (!(parent->flags & EXFAT_ATTRIB_DIR))
83         {
84                 exfat_put_node(&ef, parent);
85                 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
86                 return -ENOTDIR;
87         }
88
89         filler(buffer, ".", NULL, 0);
90         filler(buffer, "..", NULL, 0);
91
92         rc = exfat_opendir(&ef, parent, &it);
93         if (rc != 0)
94         {
95                 exfat_put_node(&ef, parent);
96                 exfat_error("failed to open directory `%s'", path);
97                 return rc;
98         }
99         while ((node = exfat_readdir(&ef, &it)))
100         {
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);
107         }
108         exfat_closedir(&ef, &it);
109         exfat_put_node(&ef, parent);
110         return 0;
111 }
112
113 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
114 {
115         struct exfat_node* node;
116         int rc;
117
118         exfat_debug("[fuse_exfat_open] %s", path);
119
120         rc = exfat_lookup(&ef, &node, path);
121         if (rc != 0)
122                 return rc;
123         set_node(fi, node);
124         return 0;
125 }
126
127 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
128 {
129         exfat_put_node(&ef, get_node(fi));
130         return 0;
131 }
132
133 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
134                 off_t offset, struct fuse_file_info* fi)
135 {
136         exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
137         return exfat_read(&ef, get_node(fi), buffer, size, offset);
138 }
139
140 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
141                 off_t offset, struct fuse_file_info* fi)
142 {
143         exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
144         return exfat_write(&ef, get_node(fi), buffer, size, offset);
145 }
146
147 static int fuse_exfat_unlink(const char* path)
148 {
149         struct exfat_node* node;
150         int rc;
151
152         exfat_debug("[fuse_exfat_unlink] %s", path);
153
154         rc = exfat_lookup(&ef, &node, path);
155         if (rc != 0)
156                 return rc;
157
158         rc = exfat_unlink(&ef, node);
159         exfat_put_node(&ef, node);
160         return rc;
161 }
162
163 static int fuse_exfat_rmdir(const char* path)
164 {
165         struct exfat_node* node;
166         int rc;
167
168         exfat_debug("[fuse_exfat_rmdir] %s", path);
169
170         rc = exfat_lookup(&ef, &node, path);
171         if (rc != 0)
172                 return rc;
173
174         rc = exfat_rmdir(&ef, node);
175         exfat_put_node(&ef, node);
176         return rc;
177 }
178
179 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
180 {
181         exfat_debug("[fuse_exfat_mknod] %s", path);
182         return exfat_mknod(&ef, path);
183 }
184
185 static int fuse_exfat_mkdir(const char* path, mode_t mode)
186 {
187         exfat_debug("[fuse_exfat_mkdir] %s", path);
188         return exfat_mkdir(&ef, path);
189 }
190
191 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
192 {
193         struct exfat_node* node;
194         int rc;
195
196         exfat_debug("[fuse_exfat_utimens] %s", path);
197
198         rc = exfat_lookup(&ef, &node, path);
199         if (rc != 0)
200                 return rc;
201
202         exfat_utimes(node, tv);
203         exfat_put_node(&ef, node);
204         return 0;
205 }
206
207 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
208 {
209         const uint64_t block_count = le64_to_cpu(ef.sb->block_count);
210
211         sfs->f_bsize = BLOCK_SIZE(*ef.sb);
212         sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
213         sfs->f_blocks = block_count;
214         sfs->f_bavail = block_count - ef.sb->allocated_percent * block_count / 100;
215         sfs->f_bfree = sfs->f_bavail;
216         sfs->f_namemax = EXFAT_NAME_MAX;
217
218         /*
219            Below are fake values because in exFAT there is
220            a) no simple way to count files;
221            b) no such thing as inode;
222            So here we assume that inode = cluster.
223         */
224         sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
225         sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
226         sfs->f_ffree = sfs->f_bavail;
227
228         return 0;
229 }
230
231 static void fuse_exfat_destroy(void* unused)
232 {
233         exfat_unmount(&ef);
234 }
235
236 static void usage(const char* prog)
237 {
238         fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
239         exit(1);
240 }
241
242 static struct fuse_operations fuse_exfat_ops =
243 {
244         .getattr        = fuse_exfat_getattr,
245         .truncate       = fuse_exfat_truncate,
246         .readdir        = fuse_exfat_readdir,
247         .open           = fuse_exfat_open,
248         .release        = fuse_exfat_release,
249         .read           = fuse_exfat_read,
250         .write          = fuse_exfat_write,
251         .unlink         = fuse_exfat_unlink,
252         .rmdir          = fuse_exfat_rmdir,
253         .mknod          = fuse_exfat_mknod,
254         .mkdir          = fuse_exfat_mkdir,
255         .utimens        = fuse_exfat_utimens,
256         .statfs         = fuse_exfat_statfs,
257         .destroy        = fuse_exfat_destroy,
258 };
259
260 int main(int argc, char* argv[])
261 {
262         struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
263         struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
264         const char* spec = NULL;
265         const char* mount_point = NULL;
266         const char* mount_options = "";
267         int debug = 0;
268         struct fuse_chan* fc = NULL;
269         struct fuse* fh = NULL;
270         char** pp;
271
272         printf("FUSE exfat %u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
273
274         for (pp = argv + 1; *pp; pp++)
275         {
276                 if (strcmp(*pp, "-o") == 0)
277                 {
278                         pp++;
279                         if (*pp == NULL)
280                                 usage(argv[0]);
281                         mount_options = *pp;
282                 }
283                 else if (strcmp(*pp, "-d") == 0)
284                         debug = 1;
285                 else if (spec == NULL)
286                         spec = *pp;
287                 else if (mount_point == NULL)
288                         mount_point = *pp;
289                 else
290                         usage(argv[0]);
291         }
292         if (spec == NULL || mount_point == NULL)
293                 usage(argv[0]);
294
295         /* create arguments for fuse_mount() */
296         if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
297                 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
298                 fuse_opt_add_arg(&mount_args, mount_options) != 0)
299                 return 1;
300
301         /* create FUSE mount point */
302         fc = fuse_mount(mount_point, &mount_args);
303         fuse_opt_free_args(&mount_args);
304         if (fc == NULL)
305                 return 1;
306
307         /* create arguments for fuse_new() */
308         if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
309                 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
310         {
311                 fuse_unmount(mount_point, fc);
312                 return 1;
313         }
314
315         /* create new FUSE file system */
316         fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
317                         sizeof(struct fuse_operations), NULL);
318         fuse_opt_free_args(&newfs_args);
319         if (fh == NULL)
320         {
321                 fuse_unmount(mount_point, fc);
322                 return 1;
323         }
324
325         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
326         if (fuse_set_signal_handlers(fuse_get_session(fh)))
327         {
328                 fuse_unmount(mount_point, fc);
329                 fuse_destroy(fh);
330                 return 1;
331         }
332
333         if (exfat_mount(&ef, spec, mount_options) != 0)
334         {
335                 fuse_unmount(mount_point, fc);
336                 fuse_destroy(fh);
337                 return 1;
338         }
339
340         /* go to background unless "-d" option is passed */
341         fuse_daemonize(debug);
342
343         /* FUSE main loop */
344         fuse_loop(fh);
345
346         /* it's quite illogical but fuse_unmount() must be called BEFORE
347            fuse_destroy() */
348         fuse_unmount(mount_point, fc);
349         fuse_destroy(fh);
350         return 0;
351 }