sdcard.c revision 3a34d5a28621c265f18df25d6f9d30624758afd0
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdio.h> 18#include <stdlib.h> 19#include <string.h> 20#include <unistd.h> 21#include <errno.h> 22#include <fcntl.h> 23#include <sys/mount.h> 24#include <sys/stat.h> 25#include <sys/statfs.h> 26#include <sys/uio.h> 27#include <dirent.h> 28#include <ctype.h> 29 30#include <private/android_filesystem_config.h> 31 32#include "fuse.h" 33 34/* README 35 * 36 * What is this? 37 * 38 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style 39 * directory permissions (all files are given fixed owner, group, and 40 * permissions at creation, owner, group, and permissions are not 41 * changeable, symlinks and hardlinks are not createable, etc. 42 * 43 * usage: sdcard <path> <uid> <gid> 44 * 45 * It must be run as root, but will change to uid/gid as soon as it 46 * mounts a filesystem on /mnt/sdcard. It will refuse to run if uid or 47 * gid are zero. 48 * 49 * 50 * Things I believe to be true: 51 * 52 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK, 53 * CREAT) must bump that node's refcount 54 * - don't forget that FORGET can forget multiple references (req->nlookup) 55 * - if an op that returns a fuse_entry fails writing the reply to the 56 * kernel, you must rollback the refcount to reflect the reference the 57 * kernel did not actually acquire 58 * 59 */ 60 61#define FUSE_TRACE 0 62 63#if FUSE_TRACE 64#define TRACE(x...) fprintf(stderr,x) 65#else 66#define TRACE(x...) do {} while (0) 67#endif 68 69#define ERROR(x...) fprintf(stderr,x) 70 71#define FUSE_UNKNOWN_INO 0xffffffff 72 73#define MOUNT_POINT "/mnt/sdcard" 74 75struct handle { 76 struct node *node; 77 int fd; 78}; 79 80struct dirhandle { 81 struct node *node; 82 DIR *d; 83}; 84 85struct node { 86 __u64 nid; 87 __u64 gen; 88 89 struct node *next; /* per-dir sibling list */ 90 struct node *child; /* first contained file by this dir */ 91 struct node *all; /* global node list */ 92 struct node *parent; /* containing directory */ 93 94 __u32 refcount; 95 __u32 namelen; 96 97 char *name; 98}; 99 100struct fuse { 101 __u64 next_generation; 102 __u64 next_node_id; 103 104 int fd; 105 106 struct node *all; 107 108 struct node root; 109 char rootpath[1024]; 110}; 111 112/* true if file names should be squashed to lower case */ 113static int force_lower_case = 0; 114static unsigned uid = -1; 115static unsigned gid = -1; 116 117#define PATH_BUFFER_SIZE 1024 118 119/* 120 * Get the real-life absolute path to a node. 121 * node: start at this node 122 * buf: storage for returned string 123 * name: append this string to path if set 124 */ 125char *node_get_path(struct node *node, char *buf, const char *name) 126{ 127 char *out = buf + PATH_BUFFER_SIZE - 1; 128 int len; 129 out[0] = 0; 130 131 if (name) { 132 len = strlen(name); 133 goto start; 134 } 135 136 while (node) { 137 name = node->name; 138 len = node->namelen; 139 node = node->parent; 140 start: 141 if ((len + 1) > (out - buf)) 142 return 0; 143 out -= len; 144 memcpy(out, name, len); 145 out --; 146 out[0] = '/'; 147 } 148 149 return out; 150} 151 152void attr_from_stat(struct fuse_attr *attr, struct stat *s) 153{ 154 attr->ino = s->st_ino; 155 attr->size = s->st_size; 156 attr->blocks = s->st_blocks; 157 attr->atime = s->st_atime; 158 attr->mtime = s->st_mtime; 159 attr->ctime = s->st_ctime; 160 attr->atimensec = s->st_atime_nsec; 161 attr->mtimensec = s->st_mtime_nsec; 162 attr->ctimensec = s->st_ctime_nsec; 163 attr->mode = s->st_mode; 164 attr->nlink = s->st_nlink; 165 166 /* force permissions to something reasonable: 167 * world readable 168 * writable by the sdcard group 169 */ 170 if (attr->mode & 0100) { 171 attr->mode = (attr->mode & (~0777)) | 0775; 172 } else { 173 attr->mode = (attr->mode & (~0777)) | 0664; 174 } 175 176 /* all files owned by root.sdcard */ 177 attr->uid = 0; 178 attr->gid = AID_SDCARD_RW; 179} 180 181int node_get_attr(struct node *node, struct fuse_attr *attr) 182{ 183 int res; 184 struct stat s; 185 char *path, buffer[PATH_BUFFER_SIZE]; 186 187 path = node_get_path(node, buffer, 0); 188 res = lstat(path, &s); 189 if (res < 0) { 190 ERROR("lstat('%s') errno %d\n", path, errno); 191 return -1; 192 } 193 194 attr_from_stat(attr, &s); 195 attr->ino = node->nid; 196 197 return 0; 198} 199 200static void add_node_to_parent(struct node *node, struct node *parent) { 201 node->parent = parent; 202 node->next = parent->child; 203 parent->child = node; 204 parent->refcount++; 205} 206 207struct node *node_create(struct node *parent, const char *name, __u64 nid, __u64 gen) 208{ 209 struct node *node; 210 int namelen = strlen(name); 211 212 node = calloc(1, sizeof(struct node)); 213 if (node == 0) { 214 return 0; 215 } 216 node->name = malloc(namelen + 1); 217 if (node->name == 0) { 218 free(node); 219 return 0; 220 } 221 222 node->nid = nid; 223 node->gen = gen; 224 add_node_to_parent(node, parent); 225 memcpy(node->name, name, namelen + 1); 226 node->namelen = namelen; 227 228 return node; 229} 230 231static char *rename_node(struct node *node, const char *name) 232{ 233 node->namelen = strlen(name); 234 char *newname = realloc(node->name, node->namelen + 1); 235 if (newname == 0) 236 return 0; 237 node->name = newname; 238 memcpy(node->name, name, node->namelen + 1); 239 return node->name; 240} 241 242void fuse_init(struct fuse *fuse, int fd, const char *path) 243{ 244 fuse->fd = fd; 245 fuse->next_node_id = 2; 246 fuse->next_generation = 0; 247 248 fuse->all = &fuse->root; 249 250 fuse->root.nid = FUSE_ROOT_ID; /* 1 */ 251 fuse->root.next = 0; 252 fuse->root.child = 0; 253 fuse->root.parent = 0; 254 255 fuse->root.all = 0; 256 fuse->root.refcount = 2; 257 258 fuse->root.name = 0; 259 rename_node(&fuse->root, path); 260} 261 262static inline void *id_to_ptr(__u64 nid) 263{ 264 return (void *) nid; 265} 266 267static inline __u64 ptr_to_id(void *ptr) 268{ 269 return (__u64) ptr; 270} 271 272 273struct node *lookup_by_inode(struct fuse *fuse, __u64 nid) 274{ 275 if (nid == FUSE_ROOT_ID) { 276 return &fuse->root; 277 } else { 278 return id_to_ptr(nid); 279 } 280} 281 282struct node *lookup_child_by_name(struct node *node, const char *name) 283{ 284 for (node = node->child; node; node = node->next) { 285 if (!strcmp(name, node->name)) { 286 return node; 287 } 288 } 289 return 0; 290} 291 292struct node *lookup_child_by_inode(struct node *node, __u64 nid) 293{ 294 for (node = node->child; node; node = node->next) { 295 if (node->nid == nid) { 296 return node; 297 } 298 } 299 return 0; 300} 301 302static void dec_refcount(struct node *node) { 303 if (node->refcount > 0) { 304 node->refcount--; 305 TRACE("dec_refcount %p(%s) -> %d\n", node, node->name, node->refcount); 306 } else { 307 ERROR("Zero refcnt %p\n", node); 308 } 309 } 310 311static struct node *remove_child(struct node *parent, __u64 nid) 312{ 313 struct node *prev = 0; 314 struct node *node; 315 316 for (node = parent->child; node; node = node->next) { 317 if (node->nid == nid) { 318 if (prev) { 319 prev->next = node->next; 320 } else { 321 parent->child = node->next; 322 } 323 node->next = 0; 324 node->parent = 0; 325 dec_refcount(parent); 326 return node; 327 } 328 prev = node; 329 } 330 return 0; 331} 332 333struct node *node_lookup(struct fuse *fuse, struct node *parent, const char *name, 334 struct fuse_attr *attr) 335{ 336 int res; 337 struct stat s; 338 char *path, buffer[PATH_BUFFER_SIZE]; 339 struct node *node; 340 341 path = node_get_path(parent, buffer, name); 342 /* XXX error? */ 343 344 res = lstat(path, &s); 345 if (res < 0) 346 return 0; 347 348 node = lookup_child_by_name(parent, name); 349 if (!node) { 350 node = node_create(parent, name, fuse->next_node_id++, fuse->next_generation++); 351 if (!node) 352 return 0; 353 node->nid = ptr_to_id(node); 354 node->all = fuse->all; 355 fuse->all = node; 356 } 357 358 attr_from_stat(attr, &s); 359 attr->ino = node->nid; 360 361 return node; 362} 363 364void node_release(struct node *node) 365{ 366 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount); 367 dec_refcount(node); 368 if (node->refcount == 0) { 369 if (node->parent->child == node) { 370 node->parent->child = node->parent->child->next; 371 } else { 372 struct node *node2; 373 374 node2 = node->parent->child; 375 while (node2->next != node) 376 node2 = node2->next; 377 node2->next = node->next; 378 } 379 380 TRACE("DESTROY %p (%s)\n", node, node->name); 381 382 node_release(node->parent); 383 384 node->parent = 0; 385 node->next = 0; 386 387 /* TODO: remove debugging - poison memory */ 388 memset(node->name, 0xef, node->namelen); 389 free(node->name); 390 memset(node, 0xfc, sizeof(*node)); 391 free(node); 392 } 393} 394 395void fuse_status(struct fuse *fuse, __u64 unique, int err) 396{ 397 struct fuse_out_header hdr; 398 hdr.len = sizeof(hdr); 399 hdr.error = err; 400 hdr.unique = unique; 401 if (err) { 402// ERROR("*** %d ***\n", err); 403 } 404 write(fuse->fd, &hdr, sizeof(hdr)); 405} 406 407void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len) 408{ 409 struct fuse_out_header hdr; 410 struct iovec vec[2]; 411 int res; 412 413 hdr.len = len + sizeof(hdr); 414 hdr.error = 0; 415 hdr.unique = unique; 416 417 vec[0].iov_base = &hdr; 418 vec[0].iov_len = sizeof(hdr); 419 vec[1].iov_base = data; 420 vec[1].iov_len = len; 421 422 res = writev(fuse->fd, vec, 2); 423 if (res < 0) { 424 ERROR("*** REPLY FAILED *** %d\n", errno); 425 } 426} 427 428void lookup_entry(struct fuse *fuse, struct node *node, 429 const char *name, __u64 unique) 430{ 431 struct fuse_entry_out out; 432 433 memset(&out, 0, sizeof(out)); 434 435 node = node_lookup(fuse, node, name, &out.attr); 436 if (!node) { 437 fuse_status(fuse, unique, -ENOENT); 438 return; 439 } 440 441 node->refcount++; 442// fprintf(stderr,"ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount); 443 out.nodeid = node->nid; 444 out.generation = node->gen; 445 out.entry_valid = 10; 446 out.attr_valid = 10; 447 448 fuse_reply(fuse, unique, &out, sizeof(out)); 449} 450 451static int name_needs_normalizing(const char* name) { 452 char ch; 453 while ((ch = *name++) != 0) { 454 if (ch != tolower(ch)) 455 return 1; 456 } 457 return 0; 458} 459 460static void normalize_name(char *name) 461{ 462 if (force_lower_case) { 463 char ch; 464 while ((ch = *name) != 0) 465 *name++ = tolower(ch); 466 } 467} 468 469static void recursive_fix_files(const char* path) { 470 DIR* dir; 471 struct dirent* entry; 472 char pathbuf[PATH_MAX]; 473 char oldpath[PATH_MAX]; 474 int pathLength = strlen(path); 475 int pathRemaining; 476 char* fileSpot; 477 478 if (pathLength >= sizeof(pathbuf) - 1) { 479 ERROR("path too long: %s\n", path); 480 return; 481 } 482 strcpy(pathbuf, path); 483 if (pathbuf[pathLength - 1] != '/') { 484 pathbuf[pathLength++] = '/'; 485 } 486 fileSpot = pathbuf + pathLength; 487 pathRemaining = sizeof(pathbuf) - pathLength - 1; 488 489 dir = opendir(path); 490 if (!dir) { 491 ERROR("opendir %s failed: %s", path, strerror(errno)); 492 return; 493 } 494 495 while ((entry = readdir(dir))) { 496 const char* name = entry->d_name; 497 int nameLength; 498 499 // ignore "." and ".." 500 if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) { 501 continue; 502 } 503 504 nameLength = strlen(name); 505 if (nameLength > pathRemaining) { 506 ERROR("path %s/%s too long\n", path, name); 507 continue; 508 } 509 strcpy(fileSpot, name); 510 511 // make sure owner and group are correct 512 chown(pathbuf, uid, gid); 513 514 if (name_needs_normalizing(name)) { 515 /* rename file to lower case file name */ 516 strlcpy(oldpath, pathbuf, sizeof(oldpath)); 517 normalize_name(pathbuf); 518 rename(oldpath, pathbuf); 519 } 520 521 if (entry->d_type == DT_DIR) { 522 /* recurse to subdirectories */ 523 recursive_fix_files(pathbuf); 524 } 525 } 526 closedir(dir); 527} 528 529void handle_fuse_request(struct fuse *fuse, struct fuse_in_header *hdr, void *data, unsigned len) 530{ 531 struct node *node; 532 533 if ((len < sizeof(*hdr)) || (hdr->len != len)) { 534 ERROR("malformed header\n"); 535 return; 536 } 537 538 len -= hdr->len; 539 540 if (hdr->nodeid) { 541 node = lookup_by_inode(fuse, hdr->nodeid); 542 if (!node) { 543 fuse_status(fuse, hdr->unique, -ENOENT); 544 return; 545 } 546 } else { 547 node = 0; 548 } 549 550 switch (hdr->opcode) { 551 case FUSE_LOOKUP: { /* bytez[] -> entry_out */ 552 normalize_name((char*) data); 553 TRACE("LOOKUP %llx %s\n", hdr->nodeid, (char*) data); 554 lookup_entry(fuse, node, (char*) data, hdr->unique); 555 return; 556 } 557 case FUSE_FORGET: { 558 struct fuse_forget_in *req = data; 559 TRACE("FORGET %llx (%s) #%lld\n", hdr->nodeid, node->name, req->nlookup); 560 /* no reply */ 561 while (req->nlookup--) 562 node_release(node); 563 return; 564 } 565 case FUSE_GETATTR: { /* getattr_in -> attr_out */ 566 struct fuse_getattr_in *req = data; 567 struct fuse_attr_out out; 568 569 TRACE("GETATTR flags=%x fh=%llx\n", req->getattr_flags, req->fh); 570 571 memset(&out, 0, sizeof(out)); 572 node_get_attr(node, &out.attr); 573 out.attr_valid = 10; 574 575 fuse_reply(fuse, hdr->unique, &out, sizeof(out)); 576 return; 577 } 578 case FUSE_SETATTR: { /* setattr_in -> attr_out */ 579 struct fuse_setattr_in *req = data; 580 struct fuse_attr_out out; 581 char *path, buffer[PATH_BUFFER_SIZE]; 582 int res = 0; 583 584 TRACE("SETATTR fh=%llx id=%llx valid=%x\n", 585 req->fh, hdr->nodeid, req->valid); 586 587 /* XXX: incomplete implementation -- truncate only. chmod/chown 588 * should NEVER be implemented. */ 589 590 path = node_get_path(node, buffer, 0); 591 if (req->valid & FATTR_SIZE) 592 res = truncate(path, req->size); 593 594 memset(&out, 0, sizeof(out)); 595 node_get_attr(node, &out.attr); 596 out.attr_valid = 10; 597 598 if (res) 599 fuse_status(fuse, hdr->unique, -errno); 600 else 601 fuse_reply(fuse, hdr->unique, &out, sizeof(out)); 602 return; 603 } 604// case FUSE_READLINK: 605// case FUSE_SYMLINK: 606 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */ 607 struct fuse_mknod_in *req = data; 608 char *path, buffer[PATH_BUFFER_SIZE]; 609 char *name = ((char*) data) + sizeof(*req); 610 int res; 611 612 normalize_name(name); 613 614 TRACE("MKNOD %s @ %llx\n", name, hdr->nodeid); 615 path = node_get_path(node, buffer, name); 616 617 req->mode = (req->mode & (~0777)) | 0664; 618 res = mknod(path, req->mode, req->rdev); /* XXX perm?*/ 619 if (res < 0) { 620 fuse_status(fuse, hdr->unique, -errno); 621 } else { 622 lookup_entry(fuse, node, name, hdr->unique); 623 } 624 return; 625 } 626 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */ 627 struct fuse_mkdir_in *req = data; 628 struct fuse_entry_out out; 629 char *path, buffer[PATH_BUFFER_SIZE]; 630 char *name = ((char*) data) + sizeof(*req); 631 int res; 632 633 normalize_name(name); 634 635 TRACE("MKDIR %s @ %llx 0%o\n", name, hdr->nodeid, req->mode); 636 path = node_get_path(node, buffer, name); 637 638 req->mode = (req->mode & (~0777)) | 0775; 639 res = mkdir(path, req->mode); 640 if (res < 0) { 641 fuse_status(fuse, hdr->unique, -errno); 642 } else { 643 lookup_entry(fuse, node, name, hdr->unique); 644 } 645 return; 646 } 647 case FUSE_UNLINK: { /* bytez[] -> */ 648 char *path, buffer[PATH_BUFFER_SIZE]; 649 int res; 650 normalize_name((char*) data); 651 TRACE("UNLINK %s @ %llx\n", (char*) data, hdr->nodeid); 652 path = node_get_path(node, buffer, (char*) data); 653 res = unlink(path); 654 fuse_status(fuse, hdr->unique, res ? -errno : 0); 655 return; 656 } 657 case FUSE_RMDIR: { /* bytez[] -> */ 658 char *path, buffer[PATH_BUFFER_SIZE]; 659 int res; 660 normalize_name((char*) data); 661 TRACE("RMDIR %s @ %llx\n", (char*) data, hdr->nodeid); 662 path = node_get_path(node, buffer, (char*) data); 663 res = rmdir(path); 664 fuse_status(fuse, hdr->unique, res ? -errno : 0); 665 return; 666 } 667 case FUSE_RENAME: { /* rename_in, oldname, newname -> */ 668 struct fuse_rename_in *req = data; 669 char *oldname = ((char*) data) + sizeof(*req); 670 char *newname = oldname + strlen(oldname) + 1; 671 char *oldpath, oldbuffer[PATH_BUFFER_SIZE]; 672 char *newpath, newbuffer[PATH_BUFFER_SIZE]; 673 struct node *target; 674 struct node *newparent; 675 int res; 676 677 normalize_name(oldname); 678 normalize_name(newname); 679 680 TRACE("RENAME %s->%s @ %llx\n", oldname, newname, hdr->nodeid); 681 682 target = lookup_child_by_name(node, oldname); 683 if (!target) { 684 fuse_status(fuse, hdr->unique, -ENOENT); 685 return; 686 } 687 oldpath = node_get_path(node, oldbuffer, oldname); 688 689 newparent = lookup_by_inode(fuse, req->newdir); 690 if (!newparent) { 691 fuse_status(fuse, hdr->unique, -ENOENT); 692 return; 693 } 694 newpath = node_get_path(newparent, newbuffer, newname); 695 696 if (!remove_child(node, target->nid)) { 697 ERROR("RENAME remove_child not found"); 698 fuse_status(fuse, hdr->unique, -ENOENT); 699 return; 700 } 701 if (!rename_node(target, newname)) { 702 fuse_status(fuse, hdr->unique, -ENOMEM); 703 return; 704 } 705 add_node_to_parent(target, newparent); 706 707 res = rename(oldpath, newpath); 708 TRACE("RENAME result %d\n", res); 709 710 fuse_status(fuse, hdr->unique, res ? -errno : 0); 711 return; 712 } 713// case FUSE_LINK: 714 case FUSE_OPEN: { /* open_in -> open_out */ 715 struct fuse_open_in *req = data; 716 struct fuse_open_out out; 717 char *path, buffer[PATH_BUFFER_SIZE]; 718 struct handle *h; 719 720 h = malloc(sizeof(*h)); 721 if (!h) { 722 fuse_status(fuse, hdr->unique, -ENOMEM); 723 return; 724 } 725 726 normalize_name(buffer); 727 path = node_get_path(node, buffer, 0); 728 TRACE("OPEN %llx '%s' 0%o fh=%p\n", hdr->nodeid, path, req->flags, h); 729 h->fd = open(path, req->flags); 730 if (h->fd < 0) { 731 ERROR("ERROR\n"); 732 fuse_status(fuse, hdr->unique, errno); 733 free(h); 734 return; 735 } 736 out.fh = ptr_to_id(h); 737 out.open_flags = 0; 738 out.padding = 0; 739 fuse_reply(fuse, hdr->unique, &out, sizeof(out)); 740 return; 741 } 742 case FUSE_READ: { /* read_in -> byte[] */ 743 char buffer[128 * 1024]; 744 struct fuse_read_in *req = data; 745 struct handle *h = id_to_ptr(req->fh); 746 int res; 747 TRACE("READ %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset); 748 if (req->size > sizeof(buffer)) { 749 fuse_status(fuse, hdr->unique, -EINVAL); 750 return; 751 } 752 res = pread64(h->fd, buffer, req->size, req->offset); 753 if (res < 0) { 754 fuse_status(fuse, hdr->unique, errno); 755 return; 756 } 757 fuse_reply(fuse, hdr->unique, buffer, res); 758 return; 759 } 760 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */ 761 struct fuse_write_in *req = data; 762 struct fuse_write_out out; 763 struct handle *h = id_to_ptr(req->fh); 764 int res; 765 TRACE("WRITE %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset); 766 res = pwrite64(h->fd, ((char*) data) + sizeof(*req), req->size, req->offset); 767 if (res < 0) { 768 fuse_status(fuse, hdr->unique, errno); 769 return; 770 } 771 out.size = res; 772 fuse_reply(fuse, hdr->unique, &out, sizeof(out)); 773 goto oops; 774 } 775 case FUSE_STATFS: { /* getattr_in -> attr_out */ 776 struct statfs stat; 777 struct fuse_statfs_out out; 778 int res; 779 780 TRACE("STATFS\n"); 781 782 if (statfs(fuse->root.name, &stat)) { 783 fuse_status(fuse, hdr->unique, -errno); 784 return; 785 } 786 787 memset(&out, 0, sizeof(out)); 788 out.st.blocks = stat.f_blocks; 789 out.st.bfree = stat.f_bfree; 790 out.st.bavail = stat.f_bavail; 791 out.st.files = stat.f_files; 792 out.st.ffree = stat.f_ffree; 793 out.st.bsize = stat.f_bsize; 794 out.st.namelen = stat.f_namelen; 795 out.st.frsize = stat.f_frsize; 796 fuse_reply(fuse, hdr->unique, &out, sizeof(out)); 797 return; 798 } 799 case FUSE_RELEASE: { /* release_in -> */ 800 struct fuse_release_in *req = data; 801 struct handle *h = id_to_ptr(req->fh); 802 TRACE("RELEASE %p(%d)\n", h, h->fd); 803 close(h->fd); 804 free(h); 805 fuse_status(fuse, hdr->unique, 0); 806 return; 807 } 808// case FUSE_FSYNC: 809// case FUSE_SETXATTR: 810// case FUSE_GETXATTR: 811// case FUSE_LISTXATTR: 812// case FUSE_REMOVEXATTR: 813 case FUSE_FLUSH: 814 fuse_status(fuse, hdr->unique, 0); 815 return; 816 case FUSE_OPENDIR: { /* open_in -> open_out */ 817 struct fuse_open_in *req = data; 818 struct fuse_open_out out; 819 char *path, buffer[PATH_BUFFER_SIZE]; 820 struct dirhandle *h; 821 822 h = malloc(sizeof(*h)); 823 if (!h) { 824 fuse_status(fuse, hdr->unique, -ENOMEM); 825 return; 826 } 827 828 normalize_name(buffer); 829 path = node_get_path(node, buffer, 0); 830 TRACE("OPENDIR %llx '%s'\n", hdr->nodeid, path); 831 h->d = opendir(path); 832 if (h->d == 0) { 833 ERROR("ERROR\n"); 834 fuse_status(fuse, hdr->unique, -errno); 835 free(h); 836 return; 837 } 838 out.fh = ptr_to_id(h); 839 fuse_reply(fuse, hdr->unique, &out, sizeof(out)); 840 return; 841 } 842 case FUSE_READDIR: { 843 struct fuse_read_in *req = data; 844 char buffer[8192]; 845 struct fuse_dirent *fde = (struct fuse_dirent*) buffer; 846 struct dirent *de; 847 struct dirhandle *h = id_to_ptr(req->fh); 848 TRACE("READDIR %p\n", h); 849 de = readdir(h->d); 850 if (!de) { 851 fuse_status(fuse, hdr->unique, 0); 852 return; 853 } 854 fde->ino = FUSE_UNKNOWN_INO; 855 fde->off = 0; 856 fde->type = de->d_type; 857 fde->namelen = strlen(de->d_name); 858 memcpy(fde->name, de->d_name, fde->namelen + 1); 859 fuse_reply(fuse, hdr->unique, fde, 860 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen)); 861 return; 862 } 863 case FUSE_RELEASEDIR: { /* release_in -> */ 864 struct fuse_release_in *req = data; 865 struct dirhandle *h = id_to_ptr(req->fh); 866 TRACE("RELEASEDIR %p\n",h); 867 closedir(h->d); 868 free(h); 869 fuse_status(fuse, hdr->unique, 0); 870 return; 871 } 872// case FUSE_FSYNCDIR: 873 case FUSE_INIT: { /* init_in -> init_out */ 874 struct fuse_init_in *req = data; 875 struct fuse_init_out out; 876 877 TRACE("INIT ver=%d.%d maxread=%d flags=%x\n", 878 req->major, req->minor, req->max_readahead, req->flags); 879 880 out.major = FUSE_KERNEL_VERSION; 881 out.minor = FUSE_KERNEL_MINOR_VERSION; 882 out.max_readahead = req->max_readahead; 883 out.flags = FUSE_ATOMIC_O_TRUNC; 884 out.max_background = 32; 885 out.congestion_threshold = 32; 886 out.max_write = 256 * 1024; 887 888 fuse_reply(fuse, hdr->unique, &out, sizeof(out)); 889 return; 890 } 891 default: { 892 struct fuse_out_header h; 893 ERROR("NOTIMPL op=%d uniq=%llx nid=%llx\n", 894 hdr->opcode, hdr->unique, hdr->nodeid); 895 896 oops: 897 h.len = sizeof(h); 898 h.error = -ENOSYS; 899 h.unique = hdr->unique; 900 write(fuse->fd, &h, sizeof(h)); 901 break; 902 } 903 } 904} 905 906void handle_fuse_requests(struct fuse *fuse) 907{ 908 unsigned char req[256 * 1024 + 128]; 909 int len; 910 911 for (;;) { 912 len = read(fuse->fd, req, 8192); 913 if (len < 0) { 914 if (errno == EINTR) 915 continue; 916 ERROR("handle_fuse_requests: errno=%d\n", errno); 917 return; 918 } 919 handle_fuse_request(fuse, (void*) req, (void*) (req + sizeof(struct fuse_in_header)), len); 920 } 921} 922 923static int usage() 924{ 925 ERROR("usage: sdcard [-l -f] <path> <uid> <gid>\n\n\t-l force file names to lower case when creating new files\n\t-f fix up file system before starting (repairs bad file name case and group ownership)\n"); 926 return -1; 927} 928 929int main(int argc, char **argv) 930{ 931 struct fuse fuse; 932 char opts[256]; 933 int fd; 934 int res; 935 const char *path = NULL; 936 int fix_files = 0; 937 int i; 938 939 for (i = 1; i < argc; i++) { 940 char* arg = argv[i]; 941 if (arg[0] == '-') { 942 if (!strcmp(arg, "-l")) { 943 force_lower_case = 1; 944 } else if (!strcmp(arg, "-f")) { 945 fix_files = 1; 946 } else { 947 return usage(); 948 } 949 } else { 950 if (!path) 951 path = arg; 952 else if (uid == -1) 953 uid = strtoul(arg, 0, 10); 954 else if (gid == -1) 955 gid = strtoul(arg, 0, 10); 956 else { 957 ERROR("too many arguments\n"); 958 return usage(); 959 } 960 } 961 } 962 963 if (!path) { 964 ERROR("no path specified\n"); 965 return usage(); 966 } 967 if (uid <= 0 || gid <= 0) { 968 ERROR("uid and gid must be nonzero\n"); 969 return usage(); 970 } 971 972 /* cleanup from previous instance, if necessary */ 973 umount2(MOUNT_POINT, 2); 974 975 fd = open("/dev/fuse", O_RDWR); 976 if (fd < 0){ 977 ERROR("cannot open fuse device (%d)\n", errno); 978 return -1; 979 } 980 981 sprintf(opts, "fd=%i,rootmode=40000,default_permissions,allow_other," 982 "user_id=%d,group_id=%d", fd, uid, gid); 983 984 res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts); 985 if (res < 0) { 986 ERROR("cannot mount fuse filesystem (%d)\n", errno); 987 return -1; 988 } 989 990 if (fix_files) 991 recursive_fix_files(path); 992 993 if (setgid(gid) < 0) { 994 ERROR("cannot setgid!\n"); 995 return -1; 996 } 997 if (setuid(uid) < 0) { 998 ERROR("cannot setuid!\n"); 999 return -1; 1000 } 1001 1002 fuse_init(&fuse, fd, path); 1003 1004 umask(0); 1005 handle_fuse_requests(&fuse); 1006 1007 return 0; 1008} 1009