OSDN Git Service

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