OSDN Git Service

Add cleanup on FUSE module 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 void fuse_exfat_destroy(void* unused)
142 {
143         exfat_unmount(&ef);
144 }
145
146 static void usage(const char* prog)
147 {
148         fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
149         exit(1);
150 }
151
152 static struct fuse_operations fuse_exfat_ops =
153 {
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,
161 };
162
163 int main(int argc, char* argv[])
164 {
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;
172         char** pp;
173
174         printf("FUSE exfat %u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
175
176         for (pp = argv + 1; *pp; pp++)
177         {
178                 if (strcmp(*pp, "-o") == 0)
179                 {
180                         pp++;
181                         if (*pp == NULL)
182                                 usage(argv[0]);
183                         mount_options = *pp;
184                 }
185                 else if (spec == NULL)
186                         spec = *pp;
187                 else if (mount_point == NULL)
188                         mount_point = *pp;
189                 else
190                         usage(argv[0]);
191         }
192         if (spec == NULL || mount_point == NULL)
193                 usage(argv[0]);
194
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)
199                 return 1;
200
201         /* create FUSE mount point */
202         fc = fuse_mount(mount_point, &mount_args);
203         fuse_opt_free_args(&mount_args);
204         if (fc == NULL)
205                 return 1;
206
207         /* create arguments for fuse_new() */
208         if (fuse_opt_add_arg(&newfs_args, "") != 0)
209         {
210                 fuse_unmount(mount_point, fc);
211                 return 1;
212         }
213
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);
218         if (fh == NULL)
219         {
220                 fuse_unmount(mount_point, fc);
221                 return 1;
222         }
223
224         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
225         if (fuse_set_signal_handlers(fuse_get_session(fh)))
226         {
227                 fuse_unmount(mount_point, fc);
228                 fuse_destroy(fh);
229                 return 1;
230         }
231
232         if (exfat_mount(&ef, spec) != 0)
233         {
234                 fuse_unmount(mount_point, fc);
235                 fuse_destroy(fh);
236                 return 1;
237         }
238
239         /* FUSE main loop */
240         fuse_loop(fh);
241
242         /* it's quite illogical but fuse_unmount() must be called BEFORE
243            fuse_destroy() */
244         fuse_unmount(mount_point, fc);
245         fuse_destroy(fh);
246         return 0;
247 }