OSDN Git Service

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