1 /* 2 * Copyright 2003-2013, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <boot/vfs.h> 8 9 #include <errno.h> 10 #include <fcntl.h> 11 #include <string.h> 12 #include <sys/uio.h> 13 #include <unistd.h> 14 15 #include <StorageDefs.h> 16 17 #include <AutoDeleter.h> 18 19 #include <boot/platform.h> 20 #include <boot/partitions.h> 21 #include <boot/stdio.h> 22 #include <boot/stage2.h> 23 #include <syscall_utils.h> 24 25 #include "RootFileSystem.h" 26 #include "file_systems/packagefs/packagefs.h" 27 28 29 using namespace boot; 30 31 //#define TRACE_VFS 32 #ifdef TRACE_VFS 33 # define TRACE(x) dprintf x 34 #else 35 # define TRACE(x) ; 36 #endif 37 38 39 class Descriptor { 40 public: 41 Descriptor(Node *node, void *cookie); 42 ~Descriptor(); 43 44 ssize_t ReadAt(off_t pos, void *buffer, size_t bufferSize); 45 ssize_t Read(void *buffer, size_t bufferSize); 46 ssize_t WriteAt(off_t pos, const void *buffer, size_t bufferSize); 47 ssize_t Write(const void *buffer, size_t bufferSize); 48 49 status_t Stat(struct stat &stat); 50 51 off_t Offset() const { return fOffset; } 52 int32 RefCount() const { return fRefCount; } 53 54 status_t Acquire(); 55 status_t Release(); 56 57 Node *GetNode() const { return fNode; } 58 59 private: 60 Node *fNode; 61 void *fCookie; 62 off_t fOffset; 63 int32 fRefCount; 64 }; 65 66 #define MAX_VFS_DESCRIPTORS 64 67 68 NodeList gBootDevices; 69 NodeList gPartitions; 70 RootFileSystem *gRoot; 71 static Descriptor *sDescriptors[MAX_VFS_DESCRIPTORS]; 72 static Node *sBootDevice; 73 74 75 Node::Node() 76 : 77 fRefCount(1) 78 { 79 } 80 81 82 Node::~Node() 83 { 84 } 85 86 87 status_t 88 Node::Open(void **_cookie, int mode) 89 { 90 TRACE(("%p::Open()\n", this)); 91 return Acquire(); 92 } 93 94 95 status_t 96 Node::Close(void *cookie) 97 { 98 TRACE(("%p::Close()\n", this)); 99 return Release(); 100 } 101 102 103 status_t 104 Node::ReadLink(char* buffer, size_t bufferSize) 105 { 106 return B_BAD_VALUE; 107 } 108 109 110 status_t 111 Node::GetName(char *nameBuffer, size_t bufferSize) const 112 { 113 return B_ERROR; 114 } 115 116 117 status_t 118 Node::GetFileMap(struct file_map_run *runs, int32 *count) 119 { 120 return B_ERROR; 121 } 122 123 124 int32 125 Node::Type() const 126 { 127 return 0; 128 } 129 130 131 off_t 132 Node::Size() const 133 { 134 return 0LL; 135 } 136 137 138 ino_t 139 Node::Inode() const 140 { 141 return 0; 142 } 143 144 145 status_t 146 Node::Acquire() 147 { 148 fRefCount++; 149 TRACE(("%p::Acquire(), fRefCount = %ld\n", this, fRefCount)); 150 return B_OK; 151 } 152 153 154 status_t 155 Node::Release() 156 { 157 TRACE(("%p::Release(), fRefCount = %ld\n", this, fRefCount)); 158 if (--fRefCount == 0) { 159 TRACE(("delete node: %p\n", this)); 160 delete this; 161 return 1; 162 } 163 164 return B_OK; 165 } 166 167 168 // #pragma mark - 169 170 171 ConsoleNode::ConsoleNode() 172 : Node() 173 { 174 } 175 176 177 ssize_t 178 ConsoleNode::Read(void *buffer, size_t bufferSize) 179 { 180 return ReadAt(NULL, -1, buffer, bufferSize); 181 } 182 183 184 ssize_t 185 ConsoleNode::Write(const void *buffer, size_t bufferSize) 186 { 187 return WriteAt(NULL, -1, buffer, bufferSize); 188 } 189 190 191 // #pragma mark - 192 193 194 Directory::Directory() 195 : Node() 196 { 197 } 198 199 200 ssize_t 201 Directory::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize) 202 { 203 return B_ERROR; 204 } 205 206 207 ssize_t 208 Directory::WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize) 209 { 210 return B_ERROR; 211 } 212 213 214 int32 215 Directory::Type() const 216 { 217 return S_IFDIR; 218 } 219 220 221 Node* 222 Directory::Lookup(const char* name, bool traverseLinks) 223 { 224 Node* node = LookupDontTraverse(name); 225 if (node == NULL) 226 return NULL; 227 228 if (!traverseLinks || !S_ISLNK(node->Type())) 229 return node; 230 231 // the node is a symbolic link, so we have to resolve the path 232 char linkPath[B_PATH_NAME_LENGTH]; 233 status_t error = node->ReadLink(linkPath, sizeof(linkPath)); 234 235 node->Release(); 236 // we don't need this one anymore 237 238 if (error != B_OK) 239 return NULL; 240 241 // let open_from() do the real work 242 int fd = open_from(this, linkPath, O_RDONLY); 243 if (fd < 0) 244 return NULL; 245 246 node = get_node_from(fd); 247 if (node != NULL) 248 node->Acquire(); 249 250 close(fd); 251 return node; 252 } 253 254 255 status_t 256 Directory::CreateFile(const char *name, mode_t permissions, Node **_node) 257 { 258 return EROFS; 259 } 260 261 262 // #pragma mark - 263 264 265 MemoryDisk::MemoryDisk(const uint8* data, size_t size, const char* name) 266 : Node(), 267 fData(data), 268 fSize(size) 269 { 270 strlcpy(fName, name, sizeof(fName)); 271 } 272 273 274 ssize_t 275 MemoryDisk::ReadAt(void* cookie, off_t pos, void* buffer, size_t bufferSize) 276 { 277 if (pos >= fSize) 278 return 0; 279 280 if (pos + bufferSize > fSize) 281 bufferSize = fSize - pos; 282 283 memcpy(buffer, fData + pos, bufferSize); 284 return bufferSize; 285 } 286 287 288 ssize_t 289 MemoryDisk::WriteAt(void* cookie, off_t pos, const void* buffer, 290 size_t bufferSize) 291 { 292 return B_NOT_ALLOWED; 293 } 294 295 296 off_t 297 MemoryDisk::Size() const 298 { 299 return fSize; 300 } 301 302 303 status_t 304 MemoryDisk::GetName(char *nameBuffer, size_t bufferSize) const 305 { 306 if (!nameBuffer) 307 return B_BAD_VALUE; 308 309 strlcpy(nameBuffer, fName, bufferSize); 310 return B_OK; 311 } 312 313 314 // #pragma mark - 315 316 317 Descriptor::Descriptor(Node *node, void *cookie) 318 : 319 fNode(node), 320 fCookie(cookie), 321 fOffset(0), 322 fRefCount(1) 323 { 324 } 325 326 327 Descriptor::~Descriptor() 328 { 329 } 330 331 332 ssize_t 333 Descriptor::Read(void *buffer, size_t bufferSize) 334 { 335 ssize_t bytesRead = fNode->ReadAt(fCookie, fOffset, buffer, bufferSize); 336 if (bytesRead > B_OK) 337 fOffset += bytesRead; 338 339 return bytesRead; 340 } 341 342 343 ssize_t 344 Descriptor::ReadAt(off_t pos, void *buffer, size_t bufferSize) 345 { 346 return fNode->ReadAt(fCookie, pos, buffer, bufferSize); 347 } 348 349 350 ssize_t 351 Descriptor::Write(const void *buffer, size_t bufferSize) 352 { 353 ssize_t bytesWritten = fNode->WriteAt(fCookie, fOffset, buffer, bufferSize); 354 if (bytesWritten > B_OK) 355 fOffset += bytesWritten; 356 357 return bytesWritten; 358 } 359 360 361 ssize_t 362 Descriptor::WriteAt(off_t pos, const void *buffer, size_t bufferSize) 363 { 364 return fNode->WriteAt(fCookie, pos, buffer, bufferSize); 365 } 366 367 368 status_t 369 Descriptor::Stat(struct stat &stat) 370 { 371 stat.st_mode = fNode->Type(); 372 stat.st_size = fNode->Size(); 373 stat.st_ino = fNode->Inode(); 374 375 return B_OK; 376 } 377 378 379 status_t 380 Descriptor::Acquire() 381 { 382 fRefCount++; 383 return B_OK; 384 } 385 386 387 status_t 388 Descriptor::Release() 389 { 390 if (--fRefCount == 0) { 391 status_t status = fNode->Close(fCookie); 392 if (status != B_OK) 393 return status; 394 } 395 396 return B_OK; 397 } 398 399 400 // #pragma mark - 401 402 403 BootVolume::BootVolume() 404 : 405 fRootDirectory(NULL), 406 fSystemDirectory(NULL), 407 fPackaged(false) 408 { 409 } 410 411 412 BootVolume::~BootVolume() 413 { 414 Unset(); 415 } 416 417 418 status_t 419 BootVolume::SetTo(Directory* rootDirectory) 420 { 421 Unset(); 422 423 if (rootDirectory == NULL) 424 return B_BAD_VALUE; 425 426 fRootDirectory = rootDirectory; 427 fRootDirectory->Acquire(); 428 429 // find the system directory 430 Node* systemNode = fRootDirectory->Lookup("system", true); 431 if (systemNode == NULL || !S_ISDIR(systemNode->Type())) { 432 if (systemNode != NULL) 433 systemNode->Release(); 434 Unset(); 435 return B_ENTRY_NOT_FOUND; 436 } 437 438 fSystemDirectory = static_cast<Directory*>(systemNode); 439 440 // try opening the system package 441 int packageFD = _OpenSystemPackage(); 442 fPackaged = packageFD >= 0; 443 if (!fPackaged) 444 return B_OK; 445 446 // the system is packaged -- mount the packagefs 447 Directory* packageRootDirectory; 448 status_t error = packagefs_mount_file(packageFD, packageRootDirectory); 449 close(packageFD); 450 if (error != B_OK) { 451 Unset(); 452 return error; 453 } 454 455 fSystemDirectory->Release(); 456 fSystemDirectory = packageRootDirectory; 457 458 return B_OK; 459 } 460 461 462 void 463 BootVolume::Unset() 464 { 465 if (fRootDirectory != NULL) { 466 fRootDirectory->Release(); 467 fRootDirectory = NULL; 468 } 469 470 if (fSystemDirectory != NULL) { 471 fSystemDirectory->Release(); 472 fSystemDirectory = NULL; 473 } 474 475 fPackaged = false; 476 } 477 478 479 int 480 BootVolume::_OpenSystemPackage() 481 { 482 // open the packages directory 483 Node* packagesNode = fSystemDirectory->Lookup("packages", false); 484 if (packagesNode == NULL) 485 return -1; 486 MethodDeleter<Node, status_t> packagesNodeReleaser(packagesNode, 487 &Node::Release); 488 489 if (!S_ISDIR(packagesNode->Type())) 490 return -1; 491 Directory* packageDirectory = (Directory*)packagesNode; 492 493 // search for the system package 494 int fd = -1; 495 void* cookie; 496 if (packageDirectory->Open(&cookie, O_RDONLY) == B_OK) { 497 char name[B_FILE_NAME_LENGTH]; 498 while (packageDirectory->GetNextEntry(cookie, name, sizeof(name)) 499 == B_OK) { 500 // The name must end with ".hpkg". 501 size_t nameLength = strlen(name); 502 if (nameLength < 6 || strcmp(name + nameLength - 5, ".hpkg") != 0) 503 continue; 504 505 // The name must either be "haiku.hpkg" or start with "haiku-". 506 if (strcmp(name, "haiku.hpkg") == 0 507 || strncmp(name, "haiku-", 6) == 0) { 508 fd = open_from(packageDirectory, name, O_RDONLY); 509 break; 510 } 511 } 512 packageDirectory->Close(cookie); 513 } 514 515 return fd; 516 } 517 518 519 // #pragma mark - 520 521 522 status_t 523 vfs_init(stage2_args *args) 524 { 525 gRoot = new(nothrow) RootFileSystem(); 526 if (gRoot == NULL) 527 return B_NO_MEMORY; 528 529 return B_OK; 530 } 531 532 533 status_t 534 register_boot_file_system(BootVolume& bootVolume) 535 { 536 Directory* rootDirectory = bootVolume.RootDirectory(); 537 gRoot->AddLink("boot", rootDirectory); 538 539 Partition *partition; 540 status_t status = gRoot->GetPartitionFor(rootDirectory, &partition); 541 if (status != B_OK) { 542 dprintf("register_boot_file_system(): could not locate boot volume in " 543 "root!\n"); 544 return status; 545 } 546 547 gBootVolume.SetInt64(BOOT_VOLUME_PARTITION_OFFSET, 548 partition->offset); 549 if (bootVolume.IsPackaged()) 550 gBootVolume.SetBool(BOOT_VOLUME_PACKAGED, true); 551 552 Node *device = get_node_from(partition->FD()); 553 if (device == NULL) { 554 dprintf("register_boot_file_system(): could not get boot device!\n"); 555 return B_ERROR; 556 } 557 558 return platform_register_boot_device(device); 559 } 560 561 562 /*! Gets the boot device, scans all of its partitions, gets the 563 boot partition, and mounts its file system. 564 565 \param args The stage 2 arguments. 566 \param _bootVolume On success set to the boot volume. 567 \return \c B_OK on success, another error code otherwise. 568 */ 569 status_t 570 get_boot_file_system(stage2_args* args, BootVolume& _bootVolume) 571 { 572 Node *device; 573 status_t error = platform_add_boot_device(args, &gBootDevices); 574 if (error != B_OK) 575 return error; 576 577 // the boot device must be the first device in the list 578 device = gBootDevices.First(); 579 580 error = add_partitions_for(device, false, true); 581 if (error != B_OK) 582 return error; 583 584 Partition *partition; 585 error = platform_get_boot_partition(args, device, &gPartitions, &partition); 586 if (error != B_OK) 587 return error; 588 589 Directory *fileSystem; 590 error = partition->Mount(&fileSystem, true); 591 if (error != B_OK) { 592 // this partition doesn't contain any known file system; we 593 // don't need it anymore 594 gPartitions.Remove(partition); 595 delete partition; 596 return error; 597 } 598 599 // init the BootVolume 600 error = _bootVolume.SetTo(fileSystem); 601 if (error != B_OK) 602 return error; 603 604 sBootDevice = device; 605 return B_OK; 606 } 607 608 609 /** Mounts all file systems recognized on the given device by 610 * calling the add_partitions_for() function on them. 611 */ 612 613 status_t 614 mount_file_systems(stage2_args *args) 615 { 616 // mount other partitions on boot device (if any) 617 NodeIterator iterator = gPartitions.GetIterator(); 618 619 Partition *partition = NULL; 620 while ((partition = (Partition *)iterator.Next()) != NULL) { 621 // don't scan known partitions again 622 if (partition->IsFileSystem()) 623 continue; 624 625 // remove the partition if it doesn't contain a (known) file system 626 if (partition->Scan(true) != B_OK && !partition->IsFileSystem()) { 627 gPartitions.Remove(partition); 628 delete partition; 629 } 630 } 631 632 // add all block devices the platform has for us 633 634 status_t status = platform_add_block_devices(args, &gBootDevices); 635 if (status < B_OK) 636 return status; 637 638 iterator = gBootDevices.GetIterator(); 639 Node *device = NULL, *last = NULL; 640 while ((device = iterator.Next()) != NULL) { 641 // don't scan former boot device again 642 if (device == sBootDevice) 643 continue; 644 645 if (add_partitions_for(device, true) == B_OK) { 646 // ToDo: we can't delete the object here, because it must 647 // be removed from the list before we know that it was 648 // deleted. 649 650 /* // if the Release() deletes the object, we need to skip it 651 if (device->Release() > 0) { 652 list_remove_item(&gBootDevices, device); 653 device = last; 654 } 655 */ 656 (void)last; 657 } 658 last = device; 659 } 660 661 if (gPartitions.IsEmpty()) 662 return B_ENTRY_NOT_FOUND; 663 664 #if 0 665 void *cookie; 666 if (gRoot->Open(&cookie, O_RDONLY) == B_OK) { 667 Directory *directory; 668 while (gRoot->GetNextNode(cookie, (Node **)&directory) == B_OK) { 669 char name[256]; 670 if (directory->GetName(name, sizeof(name)) == B_OK) 671 printf(":: %s (%p)\n", name, directory); 672 673 void *subCookie; 674 if (directory->Open(&subCookie, O_RDONLY) == B_OK) { 675 while (directory->GetNextEntry(subCookie, name, sizeof(name)) == B_OK) { 676 printf("\t%s\n", name); 677 } 678 directory->Close(subCookie); 679 } 680 } 681 gRoot->Close(cookie); 682 } 683 #endif 684 685 return B_OK; 686 } 687 688 689 /*! Resolves \a directory + \a path to a node. 690 Note that \a path will be modified by the function. 691 */ 692 static status_t 693 get_node_for_path(Directory *directory, char *path, Node **_node) 694 { 695 directory->Acquire(); 696 // balance Acquire()/Release() calls 697 698 while (true) { 699 Node *nextNode; 700 char *nextPath; 701 702 // walk to find the next path component ("path" will point to a single 703 // path component), and filter out multiple slashes 704 for (nextPath = path + 1; nextPath[0] != '\0' && nextPath[0] != '/'; nextPath++); 705 706 if (*nextPath == '/') { 707 *nextPath = '\0'; 708 do 709 nextPath++; 710 while (*nextPath == '/'); 711 } 712 713 nextNode = directory->Lookup(path, true); 714 directory->Release(); 715 716 if (nextNode == NULL) 717 return B_ENTRY_NOT_FOUND; 718 719 path = nextPath; 720 if (S_ISDIR(nextNode->Type())) 721 directory = (Directory *)nextNode; 722 else if (path[0]) 723 return B_NOT_ALLOWED; 724 725 // are we done? 726 if (path[0] == '\0') { 727 *_node = nextNode; 728 return B_OK; 729 } 730 } 731 732 return B_ENTRY_NOT_FOUND; 733 } 734 735 736 // #pragma mark - 737 738 739 static Descriptor * 740 get_descriptor(int fd) 741 { 742 if (fd < 0 || fd >= MAX_VFS_DESCRIPTORS) 743 return NULL; 744 745 return sDescriptors[fd]; 746 } 747 748 749 static void 750 free_descriptor(int fd) 751 { 752 if (fd >= MAX_VFS_DESCRIPTORS) 753 return; 754 755 delete sDescriptors[fd]; 756 sDescriptors[fd] = NULL; 757 } 758 759 760 /** Reserves an entry of the descriptor table and 761 * assigns the given node to it. 762 */ 763 764 int 765 open_node(Node *node, int mode) 766 { 767 if (node == NULL) 768 return B_ERROR; 769 770 // get free descriptor 771 772 int fd = 0; 773 for (; fd < MAX_VFS_DESCRIPTORS; fd++) { 774 if (sDescriptors[fd] == NULL) 775 break; 776 } 777 if (fd == MAX_VFS_DESCRIPTORS) 778 return B_ERROR; 779 780 TRACE(("got descriptor %d for node %p\n", fd, node)); 781 782 // we got a free descriptor entry, now try to open the node 783 784 void *cookie; 785 status_t status = node->Open(&cookie, mode); 786 if (status < B_OK) 787 return status; 788 789 TRACE(("could open node at %p\n", node)); 790 791 Descriptor *descriptor = new(nothrow) Descriptor(node, cookie); 792 if (descriptor == NULL) 793 return B_NO_MEMORY; 794 795 sDescriptors[fd] = descriptor; 796 797 return fd; 798 } 799 800 801 int 802 dup(int fd) 803 { 804 Descriptor *descriptor = get_descriptor(fd); 805 if (descriptor == NULL) 806 RETURN_AND_SET_ERRNO(B_FILE_ERROR); 807 808 descriptor->Acquire(); 809 RETURN_AND_SET_ERRNO(fd); 810 } 811 812 813 ssize_t 814 read_pos(int fd, off_t offset, void *buffer, size_t bufferSize) 815 { 816 Descriptor *descriptor = get_descriptor(fd); 817 if (descriptor == NULL) 818 RETURN_AND_SET_ERRNO(B_FILE_ERROR); 819 820 RETURN_AND_SET_ERRNO(descriptor->ReadAt(offset, buffer, bufferSize)); 821 } 822 823 824 ssize_t 825 pread(int fd, void* buffer, size_t bufferSize, off_t offset) 826 { 827 return read_pos(fd, offset, buffer, bufferSize); 828 } 829 830 831 ssize_t 832 read(int fd, void *buffer, size_t bufferSize) 833 { 834 Descriptor *descriptor = get_descriptor(fd); 835 if (descriptor == NULL) 836 RETURN_AND_SET_ERRNO(B_FILE_ERROR); 837 838 RETURN_AND_SET_ERRNO(descriptor->Read(buffer, bufferSize)); 839 } 840 841 842 ssize_t 843 write_pos(int fd, off_t offset, const void *buffer, size_t bufferSize) 844 { 845 Descriptor *descriptor = get_descriptor(fd); 846 if (descriptor == NULL) 847 RETURN_AND_SET_ERRNO(B_FILE_ERROR); 848 849 RETURN_AND_SET_ERRNO(descriptor->WriteAt(offset, buffer, bufferSize)); 850 } 851 852 853 ssize_t 854 write(int fd, const void *buffer, size_t bufferSize) 855 { 856 Descriptor *descriptor = get_descriptor(fd); 857 if (descriptor == NULL) 858 RETURN_AND_SET_ERRNO(B_FILE_ERROR); 859 860 RETURN_AND_SET_ERRNO(descriptor->Write(buffer, bufferSize)); 861 } 862 863 864 ssize_t 865 writev(int fd, const struct iovec* vecs, size_t count) 866 { 867 size_t totalWritten = 0; 868 869 for (size_t i = 0; i < count; i++) { 870 ssize_t written = write(fd, vecs[i].iov_base, vecs[i].iov_len); 871 if (written < 0) 872 return totalWritten == 0 ? written : totalWritten; 873 874 totalWritten += written; 875 876 if ((size_t)written != vecs[i].iov_len) 877 break; 878 } 879 880 return totalWritten; 881 } 882 883 884 int 885 open(const char *name, int mode, ...) 886 { 887 mode_t permissions = 0; 888 if ((mode & O_CREAT) != 0) { 889 va_list args; 890 va_start(args, mode); 891 permissions = va_arg(args, int) /*& ~__gUmask*/; 892 // adapt the permissions as required by POSIX 893 va_end(args); 894 } 895 896 // we always start at the top (there is no notion of a current directory (yet?)) 897 RETURN_AND_SET_ERRNO(open_from(gRoot, name, mode, permissions)); 898 } 899 900 901 int 902 open_from(Directory *directory, const char *name, int mode, mode_t permissions) 903 { 904 if (name[0] == '/') { 905 // ignore the directory and start from root if we are asked to do that 906 directory = gRoot; 907 name++; 908 } 909 910 char path[B_PATH_NAME_LENGTH]; 911 if (strlcpy(path, name, sizeof(path)) >= sizeof(path)) 912 return B_NAME_TOO_LONG; 913 914 Node *node; 915 status_t error = get_node_for_path(directory, path, &node); 916 if (error != B_OK) { 917 if (error != B_ENTRY_NOT_FOUND) 918 return error; 919 920 if ((mode & O_CREAT) == 0) 921 return B_ENTRY_NOT_FOUND; 922 923 // try to resolve the parent directory 924 strlcpy(path, name, sizeof(path)); 925 if (char* lastSlash = strrchr(path, '/')) { 926 if (lastSlash[1] == '\0') 927 return B_ENTRY_NOT_FOUND; 928 929 lastSlash = '\0'; 930 name = lastSlash + 1; 931 932 // resolve the directory 933 if (get_node_for_path(directory, path, &node) != B_OK) 934 return B_ENTRY_NOT_FOUND; 935 936 if (node->Type() != S_IFDIR) { 937 node->Release(); 938 return B_NOT_A_DIRECTORY; 939 } 940 941 directory = static_cast<Directory*>(node); 942 } else 943 directory->Acquire(); 944 945 // create the file 946 error = directory->CreateFile(name, permissions, &node); 947 directory->Release(); 948 949 if (error != B_OK) 950 return error; 951 } else if ((mode & O_EXCL) != 0) { 952 node->Release(); 953 return B_FILE_EXISTS; 954 } 955 956 int fd = open_node(node, mode); 957 958 node->Release(); 959 return fd; 960 } 961 962 963 /** Since we don't have directory functions yet, this 964 * function is needed to get the contents of a directory. 965 * It should be removed once readdir() & co. are in place. 966 */ 967 968 Node * 969 get_node_from(int fd) 970 { 971 Descriptor *descriptor = get_descriptor(fd); 972 if (descriptor == NULL) 973 return NULL; 974 975 return descriptor->GetNode(); 976 } 977 978 979 int 980 close(int fd) 981 { 982 Descriptor *descriptor = get_descriptor(fd); 983 if (descriptor == NULL) 984 RETURN_AND_SET_ERRNO(B_FILE_ERROR); 985 986 status_t status = descriptor->Release(); 987 if (!descriptor->RefCount()) 988 free_descriptor(fd); 989 990 RETURN_AND_SET_ERRNO(status); 991 } 992 993 994 // ToDo: remove this kludge when possible 995 int 996 #if defined(fstat) && !defined(main) 997 _fstat(int fd, struct stat *stat, size_t /*statSize*/) 998 #else 999 fstat(int fd, struct stat *stat) 1000 #endif 1001 { 1002 if (stat == NULL) 1003 RETURN_AND_SET_ERRNO(B_BAD_VALUE); 1004 1005 Descriptor *descriptor = get_descriptor(fd); 1006 if (descriptor == NULL) 1007 RETURN_AND_SET_ERRNO(B_FILE_ERROR); 1008 1009 RETURN_AND_SET_ERRNO(descriptor->Stat(*stat)); 1010 } 1011