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