OSDN Git Service

Initialize libexfat before FUSE arguments construction.
[android-x86/external-exfat.git] / fuse / main.c
1 /*
2         main.c (01.09.09)
3         FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
4
5         Copyright (C) 2009, 2010  Andrew Nayenko
6
7         This program is free software: you can redistribute it and/or modify
8         it under the terms of the GNU General Public License as published by
9         the Free Software Foundation, either version 3 of the License, or
10         (at your option) any later version.
11
12         This program is distributed in the hope that it will be useful,
13         but WITHOUT ANY WARRANTY; without even the implied warranty of
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15         GNU General Public License for more details.
16
17         You should have received a copy of the GNU General Public License
18         along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <fuse.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <exfat.h>
28 #include <inttypes.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <pwd.h>
32 #include <unistd.h>
33
34 #define exfat_debug(format, ...)
35
36 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
37         #error FUSE 2.6 or later is required
38 #endif
39
40 const char* default_options = "allow_other,blkdev";
41
42 struct exfat ef;
43
44 static struct exfat_node* get_node(const struct fuse_file_info* fi)
45 {
46         return (struct exfat_node*) (size_t) fi->fh;
47 }
48
49 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
50 {
51         fi->fh = (uint64_t) (size_t) node;
52 }
53
54 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
55 {
56         struct exfat_node* node;
57         int rc;
58
59         exfat_debug("[fuse_exfat_getattr] %s", path);
60
61         rc = exfat_lookup(&ef, &node, path);
62         if (rc != 0)
63                 return rc;
64
65         exfat_stat(&ef, node, stbuf);
66         exfat_put_node(&ef, node);
67         return 0;
68 }
69
70 static int fuse_exfat_truncate(const char* path, off_t size)
71 {
72         struct exfat_node* node;
73         int rc;
74
75         exfat_debug("[fuse_exfat_truncate] %s, %"PRIu64, path, (uint64_t) size);
76
77         rc = exfat_lookup(&ef, &node, path);
78         if (rc != 0)
79                 return rc;
80
81         rc = exfat_truncate(&ef, node, size);
82         exfat_put_node(&ef, node);
83         return rc;
84 }
85
86 static int fuse_exfat_readdir(const char* path, void* buffer,
87                 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
88 {
89         struct exfat_node* parent;
90         struct exfat_node* node;
91         struct exfat_iterator it;
92         int rc;
93         char name[EXFAT_NAME_MAX + 1];
94
95         exfat_debug("[fuse_exfat_readdir] %s", path);
96
97         rc = exfat_lookup(&ef, &parent, path);
98         if (rc != 0)
99                 return rc;
100         if (!(parent->flags & EXFAT_ATTRIB_DIR))
101         {
102                 exfat_put_node(&ef, parent);
103                 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
104                 return -ENOTDIR;
105         }
106
107         filler(buffer, ".", NULL, 0);
108         filler(buffer, "..", NULL, 0);
109
110         rc = exfat_opendir(&ef, parent, &it);
111         if (rc != 0)
112         {
113                 exfat_put_node(&ef, parent);
114                 exfat_error("failed to open directory `%s'", path);
115                 return rc;
116         }
117         while ((node = exfat_readdir(&ef, &it)))
118         {
119                 exfat_get_name(node, name, EXFAT_NAME_MAX);
120                 exfat_debug("[fuse_exfat_readdir] %s: %s, %"PRIu64" bytes, cluster %u",
121                                 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
122                                 (uint64_t) node->size, node->start_cluster);
123                 filler(buffer, name, NULL, 0);
124                 exfat_put_node(&ef, node);
125         }
126         exfat_closedir(&ef, &it);
127         exfat_put_node(&ef, parent);
128         return 0;
129 }
130
131 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
132 {
133         struct exfat_node* node;
134         int rc;
135
136         exfat_debug("[fuse_exfat_open] %s", path);
137
138         rc = exfat_lookup(&ef, &node, path);
139         if (rc != 0)
140                 return rc;
141         set_node(fi, node);
142         return 0;
143 }
144
145 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
146 {
147         exfat_put_node(&ef, get_node(fi));
148         return 0;
149 }
150
151 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
152                 off_t offset, struct fuse_file_info* fi)
153 {
154         exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
155         return exfat_read(&ef, get_node(fi), buffer, size, offset);
156 }
157
158 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
159                 off_t offset, struct fuse_file_info* fi)
160 {
161         exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
162         return exfat_write(&ef, get_node(fi), buffer, size, offset);
163 }
164
165 static int fuse_exfat_unlink(const char* path)
166 {
167         struct exfat_node* node;
168         int rc;
169
170         exfat_debug("[fuse_exfat_unlink] %s", path);
171
172         rc = exfat_lookup(&ef, &node, path);
173         if (rc != 0)
174                 return rc;
175
176         rc = exfat_unlink(&ef, node);
177         exfat_put_node(&ef, node);
178         return rc;
179 }
180
181 static int fuse_exfat_rmdir(const char* path)
182 {
183         struct exfat_node* node;
184         int rc;
185
186         exfat_debug("[fuse_exfat_rmdir] %s", path);
187
188         rc = exfat_lookup(&ef, &node, path);
189         if (rc != 0)
190                 return rc;
191
192         rc = exfat_rmdir(&ef, node);
193         exfat_put_node(&ef, node);
194         return rc;
195 }
196
197 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
198 {
199         exfat_debug("[fuse_exfat_mknod] %s", path);
200         return exfat_mknod(&ef, path);
201 }
202
203 static int fuse_exfat_mkdir(const char* path, mode_t mode)
204 {
205         exfat_debug("[fuse_exfat_mkdir] %s", path);
206         return exfat_mkdir(&ef, path);
207 }
208
209 static int fuse_exfat_rename(const char* old_path, const char* new_path)
210 {
211         exfat_debug("[fuse_exfat_rename] %s => %s", old_path, new_path);
212         return exfat_rename(&ef, old_path, new_path);
213 }
214
215 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
216 {
217         struct exfat_node* node;
218         int rc;
219
220         exfat_debug("[fuse_exfat_utimens] %s", path);
221
222         rc = exfat_lookup(&ef, &node, path);
223         if (rc != 0)
224                 return rc;
225
226         exfat_utimes(node, tv);
227         exfat_put_node(&ef, node);
228         return 0;
229 }
230
231 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
232 {
233         sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
234         sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
235         sfs->f_blocks = le64_to_cpu(ef.sb->block_count) >> ef.sb->bpc_bits;
236         sfs->f_bavail = exfat_count_free_clusters(&ef);
237         sfs->f_bfree = sfs->f_bavail;
238         sfs->f_namemax = EXFAT_NAME_MAX;
239
240         /*
241            Below are fake values because in exFAT there is
242            a) no simple way to count files;
243            b) no such thing as inode;
244            So here we assume that inode = cluster.
245         */
246         sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
247         sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
248         sfs->f_ffree = sfs->f_bavail;
249
250         return 0;
251 }
252
253 static void fuse_exfat_destroy(void* unused)
254 {
255         exfat_unmount(&ef);
256 }
257
258 static void usage(const char* prog)
259 {
260         fprintf(stderr, "Usage: %s [-d] [-o options] <device> <dir>\n", prog);
261         exit(1);
262 }
263
264 static struct fuse_operations fuse_exfat_ops =
265 {
266         .getattr        = fuse_exfat_getattr,
267         .truncate       = fuse_exfat_truncate,
268         .readdir        = fuse_exfat_readdir,
269         .open           = fuse_exfat_open,
270         .release        = fuse_exfat_release,
271         .read           = fuse_exfat_read,
272         .write          = fuse_exfat_write,
273         .unlink         = fuse_exfat_unlink,
274         .rmdir          = fuse_exfat_rmdir,
275         .mknod          = fuse_exfat_mknod,
276         .mkdir          = fuse_exfat_mkdir,
277         .rename         = fuse_exfat_rename,
278         .utimens        = fuse_exfat_utimens,
279         .statfs         = fuse_exfat_statfs,
280         .destroy        = fuse_exfat_destroy,
281 };
282
283 static char* add_option(char* options, const char* name, const char* value)
284 {
285         size_t size;
286
287         if (value)
288                 size = strlen(options) + strlen(name) + strlen(value) + 3;
289         else
290                 size = strlen(options) + strlen(name) + 2;
291
292         options = realloc(options, size);
293         if (options == NULL)
294         {
295                 exfat_error("failed to reallocate options string");
296                 return NULL;
297         }
298         strcat(options, ",");
299         strcat(options, name);
300         if (value)
301         {
302                 strcat(options, "=");
303                 strcat(options, value);
304         }
305         return options;
306 }
307
308 static char* add_fsname_option(char* options, const char* spec)
309 {
310         char spec_abs[PATH_MAX];
311
312         if (realpath(spec, spec_abs) == NULL)
313         {
314                 free(options);
315                 exfat_error("failed to get absolute path for `%s'", spec);
316                 return NULL;
317         }
318         return add_option(options, "fsname", spec_abs);
319 }
320
321 static char* add_user_option(char* options)
322 {
323         struct passwd* pw;
324
325         if (getuid() == 0)
326                 return options;
327
328         pw = getpwuid(getuid());
329         if (pw == NULL || pw->pw_name == NULL)
330         {
331                 free(options);
332                 exfat_error("failed to determine username");
333                 return NULL;
334         }
335         return add_option(options, "user", pw->pw_name);
336 }
337
338 int main(int argc, char* argv[])
339 {
340         struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
341         struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
342         const char* spec = NULL;
343         const char* mount_point = NULL;
344         char* mount_options;
345         int debug = 0;
346         struct fuse_chan* fc = NULL;
347         struct fuse* fh = NULL;
348         char** pp;
349
350         printf("FUSE exfat %u.%u.%u\n",
351                         EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
352
353         mount_options = strdup(default_options);
354         if (mount_options == NULL)
355         {
356                 exfat_error("failed to allocate options string");
357                 return 1;
358         }
359
360         for (pp = argv + 1; *pp; pp++)
361         {
362                 if (strcmp(*pp, "-o") == 0)
363                 {
364                         pp++;
365                         if (*pp == NULL)
366                                 usage(argv[0]);
367                         mount_options = add_option(mount_options, *pp, NULL);
368                         if (mount_options == NULL)
369                                 return 1;
370                 }
371                 else if (strcmp(*pp, "-d") == 0)
372                         debug = 1;
373                 else if (spec == NULL)
374                         spec = *pp;
375                 else if (mount_point == NULL)
376                         mount_point = *pp;
377                 else
378                 {
379                         free(mount_options);
380                         usage(argv[0]);
381                 }
382         }
383         if (spec == NULL || mount_point == NULL)
384         {
385                 free(mount_options);
386                 usage(argv[0]);
387         }
388
389         if (exfat_mount(&ef, spec, mount_options) != 0)
390         {
391                 free(mount_options);
392                 return 1;
393         }
394
395         mount_options = add_fsname_option(mount_options, spec);
396         if (mount_options == NULL)
397         {
398                 exfat_unmount(&ef);
399                 return 1;
400         }
401         mount_options = add_user_option(mount_options);
402         if (mount_options == NULL)
403         {
404                 exfat_unmount(&ef);
405                 return 1;
406         }
407
408         /* create arguments for fuse_mount() */
409         if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
410                 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
411                 fuse_opt_add_arg(&mount_args, mount_options) != 0)
412         {
413                 exfat_unmount(&ef);
414                 free(mount_options);
415                 return 1;
416         }
417
418         free(mount_options);
419
420         /* create FUSE mount point */
421         fc = fuse_mount(mount_point, &mount_args);
422         fuse_opt_free_args(&mount_args);
423         if (fc == NULL)
424         {
425                 exfat_unmount(&ef);
426                 return 1;
427         }
428
429         /* create arguments for fuse_new() */
430         if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
431                 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
432         {
433                 fuse_unmount(mount_point, fc);
434                 exfat_unmount(&ef);
435                 return 1;
436         }
437
438         /* create new FUSE file system */
439         fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
440                         sizeof(struct fuse_operations), NULL);
441         fuse_opt_free_args(&newfs_args);
442         if (fh == NULL)
443         {
444                 fuse_unmount(mount_point, fc);
445                 exfat_unmount(&ef);
446                 return 1;
447         }
448
449         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
450         if (fuse_set_signal_handlers(fuse_get_session(fh)))
451         {
452                 fuse_unmount(mount_point, fc);
453                 fuse_destroy(fh);
454                 exfat_unmount(&ef);
455                 return 1;
456         }
457
458         /* go to background unless "-d" option is passed */
459         fuse_daemonize(debug);
460
461         /* FUSE main loop */
462         fuse_loop(fh);
463
464         /* it's quite illogical but fuse_unmount() must be called BEFORE
465            fuse_destroy() */
466         fuse_unmount(mount_point, fc);
467         fuse_destroy(fh);
468         return 0;
469 }