OSDN Git Service

Use -V instead of -v option to print version for consistency with other utilities.
[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
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, true);
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         ssize_t ret;
159
160         exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
161         ret = exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
162         if (ret < 0)
163                 return -EIO;
164         return ret;
165 }
166
167 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
168                 off_t offset, struct fuse_file_info* fi)
169 {
170         ssize_t ret;
171
172         exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
173         ret = exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
174         if (ret < 0)
175                 return -EIO;
176         return ret;
177 }
178
179 static int fuse_exfat_unlink(const char* path)
180 {
181         struct exfat_node* node;
182         int rc;
183
184         exfat_debug("[%s] %s", __func__, path);
185
186         rc = exfat_lookup(&ef, &node, path);
187         if (rc != 0)
188                 return rc;
189
190         rc = exfat_unlink(&ef, node);
191         exfat_put_node(&ef, node);
192         return rc;
193 }
194
195 static int fuse_exfat_rmdir(const char* path)
196 {
197         struct exfat_node* node;
198         int rc;
199
200         exfat_debug("[%s] %s", __func__, path);
201
202         rc = exfat_lookup(&ef, &node, path);
203         if (rc != 0)
204                 return rc;
205
206         rc = exfat_rmdir(&ef, node);
207         exfat_put_node(&ef, node);
208         return rc;
209 }
210
211 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
212 {
213         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
214         return exfat_mknod(&ef, path);
215 }
216
217 static int fuse_exfat_mkdir(const char* path, mode_t mode)
218 {
219         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
220         return exfat_mkdir(&ef, path);
221 }
222
223 static int fuse_exfat_rename(const char* old_path, const char* new_path)
224 {
225         exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
226         return exfat_rename(&ef, old_path, new_path);
227 }
228
229 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
230 {
231         struct exfat_node* node;
232         int rc;
233
234         exfat_debug("[%s] %s", __func__, path);
235
236         rc = exfat_lookup(&ef, &node, path);
237         if (rc != 0)
238                 return rc;
239
240         exfat_utimes(node, tv);
241         exfat_put_node(&ef, node);
242         return 0;
243 }
244
245 #ifdef __APPLE__
246 static int fuse_exfat_chmod(const char* path, mode_t mode)
247 {
248         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
249         /* make OS X utilities happy */
250         return 0;
251 }
252 #endif
253
254 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
255 {
256         exfat_debug("[%s]", __func__);
257
258         sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
259         sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
260         sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
261         sfs->f_bavail = exfat_count_free_clusters(&ef);
262         sfs->f_bfree = sfs->f_bavail;
263         sfs->f_namemax = EXFAT_NAME_MAX;
264
265         /*
266            Below are fake values because in exFAT there is
267            a) no simple way to count files;
268            b) no such thing as inode;
269            So here we assume that inode = cluster.
270         */
271         sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
272         sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
273         sfs->f_ffree = sfs->f_bavail;
274
275         return 0;
276 }
277
278 static void* fuse_exfat_init(struct fuse_conn_info* fci)
279 {
280         exfat_debug("[%s]", __func__);
281 #ifdef FUSE_CAP_BIG_WRITES
282         fci->want |= FUSE_CAP_BIG_WRITES;
283 #endif
284         return NULL;
285 }
286
287 static void fuse_exfat_destroy(void* unused)
288 {
289         exfat_debug("[%s]", __func__);
290         exfat_unmount(&ef);
291 }
292
293 static void usage(const char* prog)
294 {
295         fprintf(stderr, "Usage: %s [-d] [-o options] [-V] <device> <dir>\n", prog);
296         exit(1);
297 }
298
299 static struct fuse_operations fuse_exfat_ops =
300 {
301         .getattr        = fuse_exfat_getattr,
302         .truncate       = fuse_exfat_truncate,
303         .readdir        = fuse_exfat_readdir,
304         .open           = fuse_exfat_open,
305         .release        = fuse_exfat_release,
306         .read           = fuse_exfat_read,
307         .write          = fuse_exfat_write,
308         .unlink         = fuse_exfat_unlink,
309         .rmdir          = fuse_exfat_rmdir,
310         .mknod          = fuse_exfat_mknod,
311         .mkdir          = fuse_exfat_mkdir,
312         .rename         = fuse_exfat_rename,
313         .utimens        = fuse_exfat_utimens,
314 #ifdef __APPLE__
315         .chmod          = fuse_exfat_chmod,
316 #endif
317         .statfs         = fuse_exfat_statfs,
318         .init           = fuse_exfat_init,
319         .destroy        = fuse_exfat_destroy,
320 };
321
322 static char* add_option(char* options, const char* name, const char* value)
323 {
324         size_t size;
325
326         if (value)
327                 size = strlen(options) + strlen(name) + strlen(value) + 3;
328         else
329                 size = strlen(options) + strlen(name) + 2;
330
331         options = realloc(options, size);
332         if (options == NULL)
333         {
334                 exfat_error("failed to reallocate options string");
335                 return NULL;
336         }
337         strcat(options, ",");
338         strcat(options, name);
339         if (value)
340         {
341                 strcat(options, "=");
342                 strcat(options, value);
343         }
344         return options;
345 }
346
347 static char* add_user_option(char* options)
348 {
349         struct passwd* pw;
350
351         if (getuid() == 0)
352                 return options;
353
354         pw = getpwuid(getuid());
355         if (pw == NULL || pw->pw_name == NULL)
356         {
357                 free(options);
358                 exfat_error("failed to determine username");
359                 return NULL;
360         }
361         return add_option(options, "user", pw->pw_name);
362 }
363
364 static char* add_blksize_option(char* options, long cluster_size)
365 {
366         long page_size = sysconf(_SC_PAGESIZE);
367         char blksize[20];
368
369         if (page_size < 1)
370                 page_size = 0x1000;
371
372         snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
373         return add_option(options, "blksize", blksize);
374 }
375
376 static char* add_fuse_options(char* options, const char* spec)
377 {
378         options = add_option(options, "fsname", spec);
379         if (options == NULL)
380                 return NULL;
381         options = add_user_option(options);
382         if (options == NULL)
383                 return NULL;
384         options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb));
385         if (options == NULL)
386                 return NULL;
387
388         return options;
389 }
390
391 int main(int argc, char* argv[])
392 {
393         struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
394         struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
395         const char* spec = NULL;
396         const char* mount_point = NULL;
397         char* mount_options;
398         int debug = 0;
399         struct fuse_chan* fc = NULL;
400         struct fuse* fh = NULL;
401         char** pp;
402
403         printf("FUSE exfat %u.%u.%u\n",
404                         EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
405
406         mount_options = strdup(default_options);
407         if (mount_options == NULL)
408         {
409                 exfat_error("failed to allocate options string");
410                 return 1;
411         }
412
413         for (pp = argv + 1; *pp; pp++)
414         {
415                 if (strcmp(*pp, "-o") == 0)
416                 {
417                         pp++;
418                         if (*pp == NULL)
419                                 usage(argv[0]);
420                         mount_options = add_option(mount_options, *pp, NULL);
421                         if (mount_options == NULL)
422                                 return 1;
423                 }
424                 else if (strcmp(*pp, "-d") == 0)
425                         debug = 1;
426                 else if (strcmp(*pp, "-V") == 0)
427                 {
428                         free(mount_options);
429                         puts("Copyright (C) 2010-2013  Andrew Nayenko");
430                         return 0;
431                 }
432                 else if (strcmp(*pp, "-n") == 0)
433                         /* ignore */ ;
434                 else if (spec == NULL)
435                         spec = *pp;
436                 else if (mount_point == NULL)
437                         mount_point = *pp;
438                 else
439                 {
440                         free(mount_options);
441                         usage(argv[0]);
442                 }
443         }
444         if (spec == NULL || mount_point == NULL)
445         {
446                 free(mount_options);
447                 usage(argv[0]);
448         }
449
450         if (exfat_mount(&ef, spec, mount_options) != 0)
451         {
452                 free(mount_options);
453                 return 1;
454         }
455
456         if (ef.ro == -1) /* read-only fallback was used */
457         {
458                 mount_options = add_option(mount_options, "ro", NULL);
459                 if (mount_options == NULL)
460                 {
461                         exfat_unmount(&ef);
462                         return 1;
463                 }
464         }
465
466         mount_options = add_fuse_options(mount_options, spec);
467         if (mount_options == NULL)
468         {
469                 exfat_unmount(&ef);
470                 return 1;
471         }
472
473         /* create arguments for fuse_mount() */
474         if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
475                 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
476                 fuse_opt_add_arg(&mount_args, mount_options) != 0)
477         {
478                 exfat_unmount(&ef);
479                 free(mount_options);
480                 return 1;
481         }
482
483         free(mount_options);
484
485         /* create FUSE mount point */
486         fc = fuse_mount(mount_point, &mount_args);
487         fuse_opt_free_args(&mount_args);
488         if (fc == NULL)
489         {
490                 exfat_unmount(&ef);
491                 return 1;
492         }
493
494         /* create arguments for fuse_new() */
495         if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
496                 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
497         {
498                 fuse_unmount(mount_point, fc);
499                 exfat_unmount(&ef);
500                 return 1;
501         }
502
503         /* create new FUSE file system */
504         fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
505                         sizeof(struct fuse_operations), NULL);
506         fuse_opt_free_args(&newfs_args);
507         if (fh == NULL)
508         {
509                 fuse_unmount(mount_point, fc);
510                 exfat_unmount(&ef);
511                 return 1;
512         }
513
514         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
515         if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
516         {
517                 fuse_unmount(mount_point, fc);
518                 fuse_destroy(fh);
519                 exfat_unmount(&ef);
520                 exfat_error("failed to set signal handlers");
521                 return 1;
522         }
523
524         /* go to background (unless "-d" option is passed) and run FUSE
525            main loop */
526         if (fuse_daemonize(debug) == 0)
527         {
528                 if (fuse_loop(fh) != 0)
529                         exfat_error("FUSE loop failure");
530         }
531         else
532                 exfat_error("failed to daemonize");
533
534         fuse_remove_signal_handlers(fuse_get_session(fh));
535         /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
536         fuse_unmount(mount_point, fc);
537         fuse_destroy(fh);
538         return 0;
539 }