bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | main.c (01.09.09) |
| 3 | FUSE-based exFAT implementation. Requires FUSE 2.6 or later. |
| 4 | |
bigbiff bigbiff | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 5 | Copyright (C) 2010-2013 Andrew Nayenko |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | |
bigbiff bigbiff | 998716f | 2013-03-07 09:59:37 -0500 | [diff] [blame] | 41 | const char* default_options = "ro_fallback,allow_other,blkdev,big_writes," |
| 42 | "defer_permissions"; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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, off64_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 | |
bigbiff bigbiff | 998716f | 2013-03-07 09:59:37 -0500 | [diff] [blame] | 83 | rc = exfat_truncate(&ef, node, size, true); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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, off64_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; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 95 | char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 121 | exfat_get_name(node, name, sizeof(name) - 1); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | off64_t offset, struct fuse_file_info* fi) |
| 157 | { |
Dees_Troy | b8fdac7 | 2013-01-25 19:42:52 +0000 | [diff] [blame] | 158 | ssize_t ret; |
| 159 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 160 | exfat_debug("[%s] %s (%zu bytes)", __func__, path, size); |
Dees_Troy | b8fdac7 | 2013-01-25 19:42:52 +0000 | [diff] [blame] | 161 | ret = exfat_generic_pread(&ef, get_node(fi), buffer, size, offset); |
| 162 | if (ret < 0) |
| 163 | return -EIO; |
| 164 | return ret; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int fuse_exfat_write(const char* path, const char* buffer, size_t size, |
| 168 | off64_t offset, struct fuse_file_info* fi) |
| 169 | { |
Dees_Troy | b8fdac7 | 2013-01-25 19:42:52 +0000 | [diff] [blame] | 170 | ssize_t ret; |
| 171 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 172 | exfat_debug("[%s] %s (%zu bytes)", __func__, path, size); |
Dees_Troy | b8fdac7 | 2013-01-25 19:42:52 +0000 | [diff] [blame] | 173 | ret = exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset); |
| 174 | if (ret < 0) |
| 175 | return -EIO; |
| 176 | return ret; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 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 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 245 | static int fuse_exfat_chmod(const char* path, mode_t mode) |
| 246 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 247 | const mode_t VALID_MODE_MASK = S_IFREG | S_IFDIR | |
| 248 | S_IRWXU | S_IRWXG | S_IRWXO; |
| 249 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 250 | exfat_debug("[%s] %s 0%ho", __func__, path, mode); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 251 | if (mode & ~VALID_MODE_MASK) |
| 252 | return -EPERM; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 253 | return 0; |
| 254 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 255 | |
| 256 | static int fuse_exfat_chown(const char* path, uid_t uid, gid_t gid) |
| 257 | { |
| 258 | exfat_debug("[%s] %s %u:%u", __func__, path, uid, gid); |
| 259 | if (uid != ef.uid || gid != ef.gid) |
| 260 | return -EPERM; |
| 261 | return 0; |
| 262 | } |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 263 | |
| 264 | static int fuse_exfat_statfs(const char* path, struct statvfs* sfs) |
| 265 | { |
| 266 | exfat_debug("[%s]", __func__); |
| 267 | |
| 268 | sfs->f_bsize = CLUSTER_SIZE(*ef.sb); |
| 269 | sfs->f_frsize = CLUSTER_SIZE(*ef.sb); |
| 270 | sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits; |
| 271 | sfs->f_bavail = exfat_count_free_clusters(&ef); |
| 272 | sfs->f_bfree = sfs->f_bavail; |
| 273 | sfs->f_namemax = EXFAT_NAME_MAX; |
| 274 | |
| 275 | /* |
| 276 | Below are fake values because in exFAT there is |
| 277 | a) no simple way to count files; |
| 278 | b) no such thing as inode; |
| 279 | So here we assume that inode = cluster. |
| 280 | */ |
| 281 | sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits; |
| 282 | sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits; |
| 283 | sfs->f_ffree = sfs->f_bavail; |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static void* fuse_exfat_init(struct fuse_conn_info* fci) |
| 289 | { |
| 290 | exfat_debug("[%s]", __func__); |
| 291 | #ifdef FUSE_CAP_BIG_WRITES |
| 292 | fci->want |= FUSE_CAP_BIG_WRITES; |
| 293 | #endif |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | static void fuse_exfat_destroy(void* unused) |
| 298 | { |
| 299 | exfat_debug("[%s]", __func__); |
| 300 | exfat_unmount(&ef); |
| 301 | } |
| 302 | |
| 303 | static void usage(const char* prog) |
| 304 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 305 | fprintf(stderr, "Usage: %s [-d] [-o options] [-V] <device> <dir>\n", prog); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 306 | exit(1); |
| 307 | } |
| 308 | |
| 309 | static struct fuse_operations fuse_exfat_ops = |
| 310 | { |
| 311 | .getattr = fuse_exfat_getattr, |
| 312 | .truncate = fuse_exfat_truncate, |
| 313 | .readdir = fuse_exfat_readdir, |
| 314 | .open = fuse_exfat_open, |
| 315 | .release = fuse_exfat_release, |
| 316 | .read = fuse_exfat_read, |
| 317 | .write = fuse_exfat_write, |
| 318 | .unlink = fuse_exfat_unlink, |
| 319 | .rmdir = fuse_exfat_rmdir, |
| 320 | .mknod = fuse_exfat_mknod, |
| 321 | .mkdir = fuse_exfat_mkdir, |
| 322 | .rename = fuse_exfat_rename, |
| 323 | .utimens = fuse_exfat_utimens, |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 324 | .chmod = fuse_exfat_chmod, |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 325 | .chown = fuse_exfat_chown, |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 326 | .statfs = fuse_exfat_statfs, |
| 327 | .init = fuse_exfat_init, |
| 328 | .destroy = fuse_exfat_destroy, |
| 329 | }; |
| 330 | |
| 331 | static char* add_option(char* options, const char* name, const char* value) |
| 332 | { |
| 333 | size_t size; |
| 334 | |
| 335 | if (value) |
| 336 | size = strlen(options) + strlen(name) + strlen(value) + 3; |
| 337 | else |
| 338 | size = strlen(options) + strlen(name) + 2; |
| 339 | |
| 340 | options = realloc(options, size); |
| 341 | if (options == NULL) |
| 342 | { |
| 343 | exfat_error("failed to reallocate options string"); |
| 344 | return NULL; |
| 345 | } |
| 346 | strcat(options, ","); |
| 347 | strcat(options, name); |
| 348 | if (value) |
| 349 | { |
| 350 | strcat(options, "="); |
| 351 | strcat(options, value); |
| 352 | } |
| 353 | return options; |
| 354 | } |
| 355 | |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 356 | static char* add_user_option(char* options) |
| 357 | { |
| 358 | struct passwd* pw; |
| 359 | |
| 360 | if (getuid() == 0) |
| 361 | return options; |
| 362 | |
| 363 | pw = getpwuid(getuid()); |
| 364 | if (pw == NULL || pw->pw_name == NULL) |
| 365 | { |
| 366 | free(options); |
| 367 | exfat_error("failed to determine username"); |
| 368 | return NULL; |
| 369 | } |
| 370 | return add_option(options, "user", pw->pw_name); |
| 371 | } |
| 372 | |
| 373 | static char* add_blksize_option(char* options, long cluster_size) |
| 374 | { |
| 375 | long page_size = sysconf(_SC_PAGESIZE); |
| 376 | char blksize[20]; |
| 377 | |
| 378 | if (page_size < 1) |
| 379 | page_size = 0x1000; |
| 380 | |
| 381 | snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size)); |
| 382 | return add_option(options, "blksize", blksize); |
| 383 | } |
| 384 | |
| 385 | static char* add_fuse_options(char* options, const char* spec) |
| 386 | { |
bigbiff bigbiff | 998716f | 2013-03-07 09:59:37 -0500 | [diff] [blame] | 387 | options = add_option(options, "fsname", spec); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 388 | if (options == NULL) |
| 389 | return NULL; |
| 390 | options = add_user_option(options); |
| 391 | if (options == NULL) |
| 392 | return NULL; |
| 393 | options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb)); |
| 394 | if (options == NULL) |
| 395 | return NULL; |
| 396 | |
| 397 | return options; |
| 398 | } |
| 399 | |
| 400 | int main(int argc, char* argv[]) |
| 401 | { |
| 402 | struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL); |
| 403 | struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL); |
| 404 | const char* spec = NULL; |
| 405 | const char* mount_point = NULL; |
| 406 | char* mount_options; |
| 407 | int debug = 0; |
| 408 | struct fuse_chan* fc = NULL; |
| 409 | struct fuse* fh = NULL; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 410 | int opt; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 411 | |
| 412 | printf("FUSE exfat %u.%u.%u\n", |
| 413 | EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); |
| 414 | |
| 415 | mount_options = strdup(default_options); |
| 416 | if (mount_options == NULL) |
| 417 | { |
| 418 | exfat_error("failed to allocate options string"); |
| 419 | return 1; |
| 420 | } |
| 421 | |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 422 | while ((opt = getopt(argc, argv, "dno:Vv")) != -1) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 423 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 424 | switch (opt) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 425 | { |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 426 | case 'd': |
| 427 | debug = 1; |
| 428 | break; |
| 429 | case 'n': |
| 430 | break; |
| 431 | case 'o': |
| 432 | mount_options = add_option(mount_options, optarg, NULL); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 433 | if (mount_options == NULL) |
| 434 | return 1; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 435 | break; |
| 436 | case 'V': |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 437 | free(mount_options); |
bigbiff bigbiff | ca829c4 | 2013-01-28 08:14:25 -0500 | [diff] [blame] | 438 | puts("Copyright (C) 2010-2013 Andrew Nayenko"); |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 439 | return 0; |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 440 | case 'v': |
| 441 | break; |
| 442 | default: |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 443 | free(mount_options); |
| 444 | usage(argv[0]); |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 445 | break; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 446 | } |
| 447 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 448 | if (argc - optind != 2) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 449 | { |
| 450 | free(mount_options); |
| 451 | usage(argv[0]); |
| 452 | } |
bigbiff bigbiff | 004e2df | 2013-07-03 14:52:12 -0400 | [diff] [blame] | 453 | spec = argv[optind]; |
| 454 | mount_point = argv[optind + 1]; |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 455 | |
| 456 | if (exfat_mount(&ef, spec, mount_options) != 0) |
| 457 | { |
| 458 | free(mount_options); |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | if (ef.ro == -1) /* read-only fallback was used */ |
| 463 | { |
| 464 | mount_options = add_option(mount_options, "ro", NULL); |
| 465 | if (mount_options == NULL) |
| 466 | { |
| 467 | exfat_unmount(&ef); |
| 468 | return 1; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | mount_options = add_fuse_options(mount_options, spec); |
| 473 | if (mount_options == NULL) |
| 474 | { |
| 475 | exfat_unmount(&ef); |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | /* create arguments for fuse_mount() */ |
| 480 | if (fuse_opt_add_arg(&mount_args, "exfat") != 0 || |
| 481 | fuse_opt_add_arg(&mount_args, "-o") != 0 || |
| 482 | fuse_opt_add_arg(&mount_args, mount_options) != 0) |
| 483 | { |
| 484 | exfat_unmount(&ef); |
| 485 | free(mount_options); |
| 486 | return 1; |
| 487 | } |
| 488 | |
| 489 | free(mount_options); |
| 490 | |
| 491 | /* create FUSE mount point */ |
| 492 | fc = fuse_mount(mount_point, &mount_args); |
| 493 | fuse_opt_free_args(&mount_args); |
| 494 | if (fc == NULL) |
| 495 | { |
| 496 | exfat_unmount(&ef); |
| 497 | return 1; |
| 498 | } |
| 499 | |
| 500 | /* create arguments for fuse_new() */ |
| 501 | if (fuse_opt_add_arg(&newfs_args, "") != 0 || |
| 502 | (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0)) |
| 503 | { |
| 504 | fuse_unmount(mount_point, fc); |
| 505 | exfat_unmount(&ef); |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | /* create new FUSE file system */ |
| 510 | fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops, |
| 511 | sizeof(struct fuse_operations), NULL); |
| 512 | fuse_opt_free_args(&newfs_args); |
| 513 | if (fh == NULL) |
| 514 | { |
| 515 | fuse_unmount(mount_point, fc); |
| 516 | exfat_unmount(&ef); |
| 517 | return 1; |
| 518 | } |
| 519 | |
| 520 | /* exit session on HUP, TERM and INT signals and ignore PIPE signal */ |
| 521 | if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0) |
| 522 | { |
| 523 | fuse_unmount(mount_point, fc); |
| 524 | fuse_destroy(fh); |
| 525 | exfat_unmount(&ef); |
| 526 | exfat_error("failed to set signal handlers"); |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | /* go to background (unless "-d" option is passed) and run FUSE |
| 531 | main loop */ |
| 532 | if (fuse_daemonize(debug) == 0) |
| 533 | { |
| 534 | if (fuse_loop(fh) != 0) |
| 535 | exfat_error("FUSE loop failure"); |
| 536 | } |
| 537 | else |
| 538 | exfat_error("failed to daemonize"); |
| 539 | |
| 540 | fuse_remove_signal_handlers(fuse_get_session(fh)); |
| 541 | /* note that fuse_unmount() must be called BEFORE fuse_destroy() */ |
| 542 | fuse_unmount(mount_point, fc); |
| 543 | fuse_destroy(fh); |
| 544 | return 0; |
| 545 | } |