OSDN Git Service

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