OSDN Git Service

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