OSDN Git Service

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