blob: 6771096f34a20e9a8d59cbba7e8818c2be70bee3 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
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
41const char* default_options = "ro_fallback,allow_other,blkdev,big_writes";
42
43struct exfat ef;
44
45static struct exfat_node* get_node(const struct fuse_file_info* fi)
46{
47 return (struct exfat_node*) (size_t) fi->fh;
48}
49
50static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
51{
52 fi->fh = (uint64_t) (size_t) node;
53}
54
55static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
56{
57 struct exfat_node* node;
58 int rc;
59
60 exfat_debug("[%s] %s", __func__, 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
71static int fuse_exfat_truncate(const char* path, off64_t size)
72{
73 struct exfat_node* node;
74 int rc;
75
76 exfat_debug("[%s] %s, %"PRId64, __func__, path, 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
87static int fuse_exfat_readdir(const char* path, void* buffer,
88 fuse_fill_dir_t filler, off64_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("[%s] %s", __func__, 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("[%s] %s: %s, %"PRId64" bytes, cluster 0x%x", __func__,
122 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
123 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
132static 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("[%s] %s", __func__, 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
147static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
148{
149 exfat_debug("[%s] %s", __func__, path);
150 exfat_put_node(&ef, get_node(fi));
151 return 0;
152}
153
154static int fuse_exfat_read(const char* path, char* buffer, size_t size,
155 off64_t offset, struct fuse_file_info* fi)
156{
157 exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
158 if (exfat_generic_pread(&ef, get_node(fi), buffer, size, offset) != size)
159 return EOF;
160 return size;
161}
162
163static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
164 off64_t offset, struct fuse_file_info* fi)
165{
166 exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
167 if (exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset) != size)
168 return EOF;
169 return size;
170}
171
172static int fuse_exfat_unlink(const char* path)
173{
174 struct exfat_node* node;
175 int rc;
176
177 exfat_debug("[%s] %s", __func__, path);
178
179 rc = exfat_lookup(&ef, &node, path);
180 if (rc != 0)
181 return rc;
182
183 rc = exfat_unlink(&ef, node);
184 exfat_put_node(&ef, node);
185 return rc;
186}
187
188static int fuse_exfat_rmdir(const char* path)
189{
190 struct exfat_node* node;
191 int rc;
192
193 exfat_debug("[%s] %s", __func__, path);
194
195 rc = exfat_lookup(&ef, &node, path);
196 if (rc != 0)
197 return rc;
198
199 rc = exfat_rmdir(&ef, node);
200 exfat_put_node(&ef, node);
201 return rc;
202}
203
204static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
205{
206 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
207 return exfat_mknod(&ef, path);
208}
209
210static int fuse_exfat_mkdir(const char* path, mode_t mode)
211{
212 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
213 return exfat_mkdir(&ef, path);
214}
215
216static int fuse_exfat_rename(const char* old_path, const char* new_path)
217{
218 exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
219 return exfat_rename(&ef, old_path, new_path);
220}
221
222static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
223{
224 struct exfat_node* node;
225 int rc;
226
227 exfat_debug("[%s] %s", __func__, path);
228
229 rc = exfat_lookup(&ef, &node, path);
230 if (rc != 0)
231 return rc;
232
233 exfat_utimes(node, tv);
234 exfat_put_node(&ef, node);
235 return 0;
236}
237
238#ifdef __APPLE__
239static int fuse_exfat_chmod(const char* path, mode_t mode)
240{
241 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
242 /* make OS X utilities happy */
243 return 0;
244}
245#endif
246
247static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
248{
249 exfat_debug("[%s]", __func__);
250
251 sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
252 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
253 sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
254 sfs->f_bavail = exfat_count_free_clusters(&ef);
255 sfs->f_bfree = sfs->f_bavail;
256 sfs->f_namemax = EXFAT_NAME_MAX;
257
258 /*
259 Below are fake values because in exFAT there is
260 a) no simple way to count files;
261 b) no such thing as inode;
262 So here we assume that inode = cluster.
263 */
264 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
265 sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
266 sfs->f_ffree = sfs->f_bavail;
267
268 return 0;
269}
270
271static void* fuse_exfat_init(struct fuse_conn_info* fci)
272{
273 exfat_debug("[%s]", __func__);
274#ifdef FUSE_CAP_BIG_WRITES
275 fci->want |= FUSE_CAP_BIG_WRITES;
276#endif
277 return NULL;
278}
279
280static void fuse_exfat_destroy(void* unused)
281{
282 exfat_debug("[%s]", __func__);
283 exfat_unmount(&ef);
284}
285
286static void usage(const char* prog)
287{
288 fprintf(stderr, "Usage: %s [-d] [-o options] [-v] <device> <dir>\n", prog);
289 exit(1);
290}
291
292static struct fuse_operations fuse_exfat_ops =
293{
294 .getattr = fuse_exfat_getattr,
295 .truncate = fuse_exfat_truncate,
296 .readdir = fuse_exfat_readdir,
297 .open = fuse_exfat_open,
298 .release = fuse_exfat_release,
299 .read = fuse_exfat_read,
300 .write = fuse_exfat_write,
301 .unlink = fuse_exfat_unlink,
302 .rmdir = fuse_exfat_rmdir,
303 .mknod = fuse_exfat_mknod,
304 .mkdir = fuse_exfat_mkdir,
305 .rename = fuse_exfat_rename,
306 .utimens = fuse_exfat_utimens,
307#ifdef __APPLE__
308 .chmod = fuse_exfat_chmod,
309#endif
310 .statfs = fuse_exfat_statfs,
311 .init = fuse_exfat_init,
312 .destroy = fuse_exfat_destroy,
313};
314
315static char* add_option(char* options, const char* name, const char* value)
316{
317 size_t size;
318
319 if (value)
320 size = strlen(options) + strlen(name) + strlen(value) + 3;
321 else
322 size = strlen(options) + strlen(name) + 2;
323
324 options = realloc(options, size);
325 if (options == NULL)
326 {
327 exfat_error("failed to reallocate options string");
328 return NULL;
329 }
330 strcat(options, ",");
331 strcat(options, name);
332 if (value)
333 {
334 strcat(options, "=");
335 strcat(options, value);
336 }
337 return options;
338}
339
340static char* add_fsname_option(char* options, const char* spec)
341{
342 char spec_abs[PATH_MAX];
343
344 if (realpath(spec, spec_abs) == NULL)
345 {
346 free(options);
347 exfat_error("failed to get absolute path for `%s'", spec);
348 return NULL;
349 }
350 return add_option(options, "fsname", spec_abs);
351}
352
353static 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
370static 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
382static char* add_fuse_options(char* options, const char* spec)
383{
384 options = add_fsname_option(options, 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
397int 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 char** pp;
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 for (pp = argv + 1; *pp; pp++)
420 {
421 if (strcmp(*pp, "-o") == 0)
422 {
423 pp++;
424 if (*pp == NULL)
425 usage(argv[0]);
426 mount_options = add_option(mount_options, *pp, NULL);
427 if (mount_options == NULL)
428 return 1;
429 }
430 else if (strcmp(*pp, "-d") == 0)
431 debug = 1;
432 else if (strcmp(*pp, "-v") == 0)
433 {
434 free(mount_options);
435 puts("Copyright (C) 2010-2012 Andrew Nayenko");
436 return 0;
437 }
438 else if (spec == NULL)
439 spec = *pp;
440 else if (mount_point == NULL)
441 mount_point = *pp;
442 else
443 {
444 free(mount_options);
445 usage(argv[0]);
446 }
447 }
448 if (spec == NULL || mount_point == NULL)
449 {
450 free(mount_options);
451 usage(argv[0]);
452 }
453
454 if (exfat_mount(&ef, spec, mount_options) != 0)
455 {
456 free(mount_options);
457 return 1;
458 }
459
460 if (ef.ro == -1) /* read-only fallback was used */
461 {
462 mount_options = add_option(mount_options, "ro", NULL);
463 if (mount_options == NULL)
464 {
465 exfat_unmount(&ef);
466 return 1;
467 }
468 }
469
470 mount_options = add_fuse_options(mount_options, spec);
471 if (mount_options == NULL)
472 {
473 exfat_unmount(&ef);
474 return 1;
475 }
476
477 /* create arguments for fuse_mount() */
478 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
479 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
480 fuse_opt_add_arg(&mount_args, mount_options) != 0)
481 {
482 exfat_unmount(&ef);
483 free(mount_options);
484 return 1;
485 }
486
487 free(mount_options);
488
489 /* create FUSE mount point */
490 fc = fuse_mount(mount_point, &mount_args);
491 fuse_opt_free_args(&mount_args);
492 if (fc == NULL)
493 {
494 exfat_unmount(&ef);
495 return 1;
496 }
497
498 /* create arguments for fuse_new() */
499 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
500 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
501 {
502 fuse_unmount(mount_point, fc);
503 exfat_unmount(&ef);
504 return 1;
505 }
506
507 /* create new FUSE file system */
508 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
509 sizeof(struct fuse_operations), NULL);
510 fuse_opt_free_args(&newfs_args);
511 if (fh == NULL)
512 {
513 fuse_unmount(mount_point, fc);
514 exfat_unmount(&ef);
515 return 1;
516 }
517
518 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
519 if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
520 {
521 fuse_unmount(mount_point, fc);
522 fuse_destroy(fh);
523 exfat_unmount(&ef);
524 exfat_error("failed to set signal handlers");
525 return 1;
526 }
527
528 /* go to background (unless "-d" option is passed) and run FUSE
529 main loop */
530 if (fuse_daemonize(debug) == 0)
531 {
532 if (fuse_loop(fh) != 0)
533 exfat_error("FUSE loop failure");
534 }
535 else
536 exfat_error("failed to daemonize");
537
538 fuse_remove_signal_handlers(fuse_get_session(fh));
539 /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
540 fuse_unmount(mount_point, fc);
541 fuse_destroy(fh);
542 return 0;
543}