OSDN Git Service

Fix crash in FUSE module on shutdown.
[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(node, stbuf);
48         exfat_put_node(node);
49         return 0;
50 }
51
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)
54 {
55         struct exfat_node* parent;
56         struct exfat_node* node;
57         struct exfat_iterator it;
58         int rc;
59         char name[EXFAT_NAME_MAX + 1];
60
61         exfat_debug("[fuse_exfat_readdir] %s", path);
62
63         rc = exfat_lookup(&ef, &parent, path);
64         if (rc != 0)
65                 return rc;
66         if (!(parent->flags & EXFAT_ATTRIB_DIR))
67         {
68                 exfat_put_node(parent);
69                 return -ENOTDIR;
70         }
71
72         filler(buffer, ".", NULL, 0);
73         filler(buffer, "..", NULL, 0);
74
75         exfat_opendir(parent, &it);
76         while (exfat_readdir(&ef, parent, &node, &it) == 0)
77         {
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);
83                 exfat_put_node(node);
84         }
85         exfat_closedir(&it);
86         exfat_put_node(parent);
87         return 0;
88 }
89
90 static int fuse_exfat_open(const char *path, struct fuse_file_info *fi)
91 {
92         struct exfat_node* node;
93         int rc;
94
95         exfat_debug("[fuse_exfat_open] %s", path);
96
97         rc = exfat_lookup(&ef, &node, path);
98         if (rc != 0)
99                 return rc;
100         set_node(fi, node);
101         return 0;
102 }
103
104 static int fuse_exfat_release(const char *path, struct fuse_file_info *fi)
105 {
106         exfat_put_node(get_node(fi));
107         return 0;
108 }
109
110 static int fuse_exfat_read(const char *path, char *buffer, size_t size,
111                 off_t offset, struct fuse_file_info *fi)
112 {
113         exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
114         return exfat_read(&ef, get_node(fi), buffer, size, offset);
115 }
116
117 static int fuse_exfat_statfs(const char *path, struct statvfs *sfs)
118 {
119         const uint64_t block_count = le64_to_cpu(ef.sb->block_count);
120
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;
127
128         /*
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.
133         */
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;
137
138         return 0;
139 }
140
141 static void usage(const char* prog)
142 {
143         fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
144         exit(1);
145 }
146
147 static struct fuse_operations fuse_exfat_ops =
148 {
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,
155 };
156
157 int main(int argc, char* argv[])
158 {
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;
166         char** pp;
167
168         printf("FUSE exfat %u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
169
170         for (pp = argv + 1; *pp; pp++)
171         {
172                 if (strcmp(*pp, "-o") == 0)
173                 {
174                         pp++;
175                         if (*pp == NULL)
176                                 usage(argv[0]);
177                         mount_options = *pp;
178                 }
179                 else if (spec == NULL)
180                         spec = *pp;
181                 else if (mount_point == NULL)
182                         mount_point = *pp;
183                 else
184                         usage(argv[0]);
185         }
186         if (spec == NULL || mount_point == NULL)
187                 usage(argv[0]);
188
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)
193                 return 1;
194
195         /* create FUSE mount point */
196         fc = fuse_mount(mount_point, &mount_args);
197         fuse_opt_free_args(&mount_args);
198         if (fc == NULL)
199                 return 1;
200
201         /* create arguments for fuse_new() */
202         if (fuse_opt_add_arg(&newfs_args, "") != 0)
203         {
204                 fuse_unmount(mount_point, fc);
205                 return 1;
206         }
207
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);
212         if (fh == NULL)
213         {
214                 fuse_unmount(mount_point, fc);
215                 return 1;
216         }
217
218         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
219         if (fuse_set_signal_handlers(fuse_get_session(fh)))
220         {
221                 fuse_unmount(mount_point, fc);
222                 fuse_destroy(fh);
223                 return 1;
224         }
225
226         if (exfat_mount(&ef, spec) != 0)
227         {
228                 fuse_unmount(mount_point, fc);
229                 fuse_destroy(fh);
230                 return 1;
231         }
232
233         /* FUSE main loop */
234         fuse_loop(fh);
235
236         /* it's quite illogical but fuse_unmount() must be called BEFORE
237            fuse_destroy() */
238         fuse_unmount(mount_point, fc);
239         fuse_destroy(fh);
240         return 0;
241 }