1 /* 2 * Copyright 2005-2011, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <BeOSBuildCompatibility.h> 8 9 #include "fs_impl.h" 10 11 #include <dirent.h> 12 #include <errno.h> 13 #include <fcntl.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <unistd.h> 17 #include <utime.h> 18 #include <sys/stat.h> 19 #include <sys/time.h> 20 21 #include <map> 22 #include <string> 23 24 #include <fs_attr.h> 25 #include <NodeMonitor.h> // for B_STAT_* flags 26 #include <syscalls.h> 27 28 #include "fs_descriptors.h" 29 #include "NodeRef.h" 30 #include "remapped_functions.h" 31 32 #if defined(HAIKU_HOST_PLATFORM_FREEBSD) 33 # include "fs_freebsd.h" 34 #endif 35 36 37 using namespace std; 38 using namespace BPrivate; 39 40 41 #if defined(HAIKU_HOST_PLATFORM_FREEBSD) 42 # define haiku_host_platform_read haiku_freebsd_read 43 # define haiku_host_platform_write haiku_freebsd_write 44 # define haiku_host_platform_readv haiku_freebsd_readv 45 # define haiku_host_platform_writev haiku_freebsd_writev 46 # define HAIKU_HOST_STAT_ATIM(x) ((x).st_atimespec) 47 # define HAIKU_HOST_STAT_MTIM(x) ((x).st_mtimespec) 48 #elif defined(HAIKU_HOST_PLATFORM_DARWIN) 49 # define haiku_host_platform_read read 50 # define haiku_host_platform_write write 51 # define haiku_host_platform_readv readv 52 # define haiku_host_platform_writev writev 53 # define HAIKU_HOST_STAT_ATIM(x) ((x).st_atimespec) 54 # define HAIKU_HOST_STAT_MTIM(x) ((x).st_mtimespec) 55 #else 56 # define haiku_host_platform_read read 57 # define haiku_host_platform_write write 58 # define haiku_host_platform_readv readv 59 # define haiku_host_platform_writev writev 60 # define HAIKU_HOST_STAT_ATIM(x) ((x).st_atim) 61 # define HAIKU_HOST_STAT_MTIM(x) ((x).st_mtim) 62 #endif 63 64 #define RETURN_AND_SET_ERRNO(err) \ 65 do { \ 66 __typeof(err) __result = (err); \ 67 if (__result < 0) { \ 68 errno = __result; \ 69 return -1; \ 70 } \ 71 return __result; \ 72 } while (0) 73 74 75 #if defined(_HAIKU_BUILD_NO_FUTIMENS) || defined(_HAIKU_BUILD_NO_FUTIMENS) 76 77 template<typename File> 78 static int 79 utimes_helper(File& file, const struct timespec times[2]) 80 { 81 if (times == NULL) 82 return file.SetTimes(NULL); 83 84 timeval timeBuffer[2]; 85 timeBuffer[0].tv_sec = times[0].tv_sec; 86 timeBuffer[0].tv_usec = times[0].tv_nsec / 1000; 87 timeBuffer[1].tv_sec = times[1].tv_sec; 88 timeBuffer[1].tv_usec = times[1].tv_nsec / 1000; 89 90 if (times[0].tv_nsec == UTIME_OMIT || times[1].tv_nsec == UTIME_OMIT) { 91 struct stat st; 92 if (file.GetStat(st) != 0) 93 return -1; 94 95 if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) 96 return 0; 97 98 if (times[0].tv_nsec == UTIME_OMIT) { 99 timeBuffer[0].tv_sec = st.st_atimespec.tv_sec; 100 timeBuffer[0].tv_usec = st.st_atimespec.tv_nsec / 1000; 101 } 102 103 if (times[1].tv_nsec == UTIME_OMIT) { 104 timeBuffer[1].tv_sec = st.st_mtimespec.tv_sec; 105 timeBuffer[1].tv_usec = st.st_mtimespec.tv_nsec / 1000; 106 } 107 } 108 109 if (times[0].tv_nsec == UTIME_NOW || times[1].tv_nsec == UTIME_NOW) { 110 timeval now; 111 gettimeofday(&now, NULL); 112 113 if (times[0].tv_nsec == UTIME_NOW) 114 timeBuffer[0] = now; 115 116 if (times[1].tv_nsec == UTIME_NOW) 117 timeBuffer[1] = now; 118 } 119 120 return file.SetTimes(timeBuffer); 121 } 122 123 #endif // _HAIKU_BUILD_NO_FUTIMENS || _HAIKU_BUILD_NO_FUTIMENS 124 125 126 #ifdef _HAIKU_BUILD_NO_FUTIMENS 127 128 struct FDFile { 129 FDFile(int fd) 130 : 131 fFD(fd) 132 { 133 } 134 135 int GetStat(struct stat& _st) 136 { 137 return fstat(fFD, &_st); 138 } 139 140 int SetTimes(const timeval times[2]) 141 { 142 return futimes(fFD, times); 143 } 144 145 private: 146 int fFD; 147 }; 148 149 150 int 151 futimens(int fd, const struct timespec times[2]) 152 { 153 FDFile file(fd); 154 return utimes_helper(file, times); 155 } 156 157 #endif // _HAIKU_BUILD_NO_FUTIMENS 158 159 #ifdef _HAIKU_BUILD_NO_UTIMENSAT 160 161 struct FDPathFile { 162 FDPathFile(int fd, const char* path, int flag) 163 : 164 fFD(fd), 165 fPath(path), 166 fFlag(flag) 167 { 168 } 169 170 int GetStat(struct stat& _st) 171 { 172 return fstatat(fFD, fPath, &_st, fFlag); 173 } 174 175 int SetTimes(const timeval times[2]) 176 { 177 // TODO: fFlag (AT_SYMLINK_NOFOLLOW) is not supported here! 178 return futimesat(fFD, fPath, times); 179 } 180 181 private: 182 int fFD; 183 const char* fPath; 184 int fFlag; 185 }; 186 187 188 int 189 utimensat(int fd, const char* path, const struct timespec times[2], int flag) 190 { 191 FDPathFile file(fd, path, flag); 192 return utimes_helper(file, times); 193 } 194 195 #endif // _HAIKU_BUILD_NO_UTIMENSAT 196 197 198 static status_t get_path(dev_t device, ino_t node, const char *name, 199 string &path); 200 201 202 // find_dir_entry 203 static status_t 204 find_dir_entry(DIR *dir, const char *path, NodeRef ref, string &name, 205 bool skipDot) 206 { 207 // find the entry 208 bool found = false; 209 while (dirent *entry = readdir(dir)) { 210 if ((strcmp(entry->d_name, ".") == 0 && skipDot) 211 || strcmp(entry->d_name, "..") == 0) { 212 // skip "." and ".." 213 } else /*if (entry->d_ino == ref.node)*/ { 214 // Note: Linux doesn't seem to translate dirent::d_ino of 215 // mount points. Thus we always have to lstat(). 216 // We also need to compare the device, which is generally not 217 // included in the dirent structure. Hence we lstat(). 218 string entryPath(path); 219 entryPath += '/'; 220 entryPath += entry->d_name; 221 struct stat st; 222 if (lstat(entryPath.c_str(), &st) == 0) { 223 if (NodeRef(st) == ref) { 224 name = entry->d_name; 225 found = true; 226 break; 227 } 228 } 229 } 230 } 231 232 if (!found) 233 return B_ENTRY_NOT_FOUND; 234 235 return B_OK; 236 } 237 238 239 // find_dir_entry 240 static status_t 241 find_dir_entry(const char *path, NodeRef ref, string &name, bool skipDot) 242 { 243 // open dir 244 DIR *dir = opendir(path); 245 if (!dir) 246 return errno; 247 248 status_t error = find_dir_entry(dir, path, ref, name, skipDot); 249 250 // close dir 251 closedir(dir); 252 253 return error; 254 } 255 256 257 // normalize_dir_path: Make path absolute and remove redundant entries. 258 static status_t 259 normalize_dir_path(const char *path, string &normalizedPath) 260 { 261 const size_t pathLen = strlen(path); 262 263 // Add CWD to relative paths. 264 if (pathLen == 0 || path[0] != '/') { 265 char pwd[PATH_MAX]; 266 if (getcwd(pwd, sizeof(pwd)) == NULL) 267 return B_ERROR; 268 269 normalizedPath = pwd; 270 } 271 272 const char *end = &path[pathLen]; 273 const char *next; 274 for (const char *ptr = path; ptr < end; ptr = next + 1) { 275 next = (char *)memchr(ptr, '/', end - ptr); 276 if (next == NULL) 277 next = end; 278 279 size_t len = next - ptr; 280 if (len == 2 && ptr[0] == '.' && ptr[1] == '.') { 281 string::size_type pos = normalizedPath.rfind('/'); 282 if (pos != string::npos) 283 normalizedPath.resize(pos); 284 continue; 285 } else if (len == 0 || (len == 1 && ptr[0] == '.')) { 286 continue; 287 } 288 289 if (normalizedPath.length() != 1) 290 normalizedPath += '/'; 291 292 normalizedPath.append(ptr, len); 293 } 294 295 if (normalizedPath.length() == 0) 296 normalizedPath += '/'; 297 298 return B_OK; 299 } 300 301 302 // normalize_entry_path 303 static status_t 304 normalize_entry_path(const char *path, string &normalizedPath) 305 { 306 const char *dirPath = NULL; 307 const char *leafName = NULL; 308 309 string dirPathString; 310 if (const char *lastSlash = strrchr(path, '/')) { 311 // found a slash: decompose into dir path and leaf name 312 leafName = lastSlash + 1; 313 if (leafName[0] == '\0') { 314 // slash is at the end: the whole path is a dir name 315 leafName = NULL; 316 } else { 317 dirPathString = string(path, leafName - path); 318 dirPath = dirPathString.c_str(); 319 } 320 } else { 321 // path contains no slash, so it is a path relative to the current dir 322 dirPath = "."; 323 leafName = path; 324 } 325 326 // catch special case: no leaf, or leaf is a directory 327 if (!leafName || strcmp(leafName, ".") == 0 || strcmp(leafName, "..") == 0) 328 return normalize_dir_path(path, normalizedPath); 329 330 // normalize the dir path 331 status_t error = normalize_dir_path(dirPath, normalizedPath); 332 if (error != B_OK) 333 return error; 334 335 // append the leaf name 336 if (normalizedPath.length() > 1) // don't append "/", if parent is root 337 normalizedPath += '/'; 338 normalizedPath += leafName; 339 340 return B_OK; 341 } 342 343 344 // #pragma mark - 345 346 typedef map<NodeRef, string> DirPathMap; 347 static DirPathMap sDirPathMap; 348 349 // get_path 350 static status_t 351 get_path(const NodeRef *ref, const char *name, string &path) 352 { 353 if (!ref && !name) 354 return B_BAD_VALUE; 355 356 // no ref or absolute path 357 if (!ref || (name && name[0] == '/')) { 358 path = name; 359 return B_OK; 360 } 361 362 // get the dir path 363 if (ref) { 364 DirPathMap::iterator it = sDirPathMap.find(*ref); 365 if (it == sDirPathMap.end()) 366 return B_ENTRY_NOT_FOUND; 367 368 path = it->second; 369 370 // stat the path to check, if it is still valid 371 struct stat st; 372 if (lstat(path.c_str(), &st) < 0) { 373 sDirPathMap.erase(it); 374 return errno; 375 } 376 377 // compare the NodeRef 378 if (NodeRef(st) != *ref) { 379 sDirPathMap.erase(it); 380 return B_ENTRY_NOT_FOUND; 381 } 382 383 // still a directory? 384 if (!S_ISDIR(st.st_mode)) { 385 sDirPathMap.erase(it); 386 return B_NOT_A_DIRECTORY; 387 } 388 } 389 390 // if there's a name, append it 391 if (name) { 392 path += '/'; 393 path += name; 394 } 395 396 return B_OK; 397 } 398 399 // get_path 400 status_t 401 BPrivate::get_path(int fd, const char *name, string &path) 402 { 403 // get the node ref for the fd, if any, and the path part is not absolute 404 if (fd >= 0 && !(name && name[0] == '/')) { 405 // get descriptor 406 Descriptor *descriptor = get_descriptor(fd); 407 if (!descriptor) 408 return B_FILE_ERROR; 409 410 // Handle symlink descriptors here explicitly, so this function can be 411 // used more flexibly. 412 if (SymlinkDescriptor* symlinkDescriptor 413 = dynamic_cast<SymlinkDescriptor*>(descriptor)) { 414 path = symlinkDescriptor->path; 415 if (name == NULL) 416 return B_OK; 417 path += '/'; 418 path += name; 419 return B_OK; 420 } 421 422 // get node ref for the descriptor 423 NodeRef ref; 424 status_t error = descriptor->GetNodeRef(ref); 425 if (error != B_OK) 426 return error; 427 428 return ::get_path(&ref, name, path); 429 430 } else // no descriptor or absolute path 431 return ::get_path((NodeRef*)NULL, name, path); 432 } 433 434 // get_path 435 static status_t 436 get_path(dev_t device, ino_t directory, const char *name, string &path) 437 { 438 NodeRef ref; 439 ref.device = device; 440 ref.node = directory; 441 442 return get_path(&ref, name, path); 443 } 444 445 // add_dir_path 446 static void 447 add_dir_path(const char *path, const NodeRef &ref) 448 { 449 // add the normalized path 450 string normalizedPath; 451 if (normalize_dir_path(path, normalizedPath) == B_OK) 452 sDirPathMap[ref] = normalizedPath; 453 } 454 455 456 // #pragma mark - 457 458 // _kern_entry_ref_to_path 459 status_t 460 _kern_entry_ref_to_path(dev_t device, ino_t node, const char *leaf, 461 char *userPath, size_t pathLength) 462 { 463 // get the path 464 string path; 465 status_t error = get_path(device, node, leaf, path); 466 if (error != B_OK) 467 return error; 468 469 // copy it back to the user buffer 470 if (strlcpy(userPath, path.c_str(), pathLength) >= pathLength) 471 return B_BUFFER_OVERFLOW; 472 473 return B_OK; 474 } 475 476 477 // #pragma mark - 478 479 // _kern_create_dir 480 status_t 481 _kern_create_dir(int fd, const char *path, int perms) 482 { 483 // get a usable path 484 string realPath; 485 status_t error = get_path(fd, path, realPath); 486 if (error != B_OK) 487 return error; 488 489 // mkdir 490 if (mkdir(realPath.c_str(), perms) < 0) 491 return errno; 492 493 return B_OK; 494 } 495 496 // _kern_create_dir_entry_ref 497 status_t 498 _kern_create_dir_entry_ref(dev_t device, ino_t node, const char *name, 499 int perms) 500 { 501 // get a usable path 502 string realPath; 503 status_t error = get_path(device, node, name, realPath); 504 if (error != B_OK) 505 return error; 506 507 // mkdir 508 if (mkdir(realPath.c_str(), perms) < 0) 509 return errno; 510 511 return B_OK; 512 } 513 514 // open_dir 515 static int 516 open_dir(const char *path) 517 { 518 // open the dir 519 DIR *dir = opendir(path); 520 if (!dir) 521 return errno; 522 523 // stat the entry 524 struct stat st; 525 if (stat(path, &st) < 0) { 526 closedir(dir); 527 return errno; 528 } 529 530 if (!S_ISDIR(st.st_mode)) { 531 closedir(dir); 532 return B_NOT_A_DIRECTORY; 533 } 534 535 // cache dir path 536 NodeRef ref(st); 537 add_dir_path(path, ref); 538 539 // create descriptor 540 DirectoryDescriptor *descriptor = new DirectoryDescriptor(dir, ref); 541 return add_descriptor(descriptor); 542 } 543 544 // _kern_open_dir 545 int 546 _kern_open_dir(int fd, const char *path) 547 { 548 // get a usable path 549 string realPath; 550 status_t error = get_path(fd, path, realPath); 551 if (error != B_OK) 552 return error; 553 554 return open_dir(realPath.c_str()); 555 } 556 557 // _kern_open_dir_entry_ref 558 int 559 _kern_open_dir_entry_ref(dev_t device, ino_t node, const char *name) 560 { 561 // get a usable path 562 string realPath; 563 status_t error = get_path(device, node, name, realPath); 564 if (error != B_OK) 565 return error; 566 567 return open_dir(realPath.c_str()); 568 } 569 570 // _kern_open_parent_dir 571 int 572 _kern_open_parent_dir(int fd, char *name, size_t nameLength) 573 { 574 // get a usable path 575 string realPath; 576 status_t error = get_path(fd, NULL, realPath); 577 if (error != B_OK) 578 return error; 579 580 // stat the entry 581 struct stat st; 582 if (stat(realPath.c_str(), &st) < 0) 583 return errno; 584 585 if (!S_ISDIR(st.st_mode)) 586 return B_NOT_A_DIRECTORY; 587 588 // get the entry name 589 realPath += "/.."; 590 string entryName; 591 error = find_dir_entry(realPath.c_str(), NodeRef(st), 592 entryName, false); 593 if (error != B_OK) 594 return error; 595 596 if (strlcpy(name, entryName.c_str(), nameLength) >= nameLength) 597 return B_BUFFER_OVERFLOW; 598 599 // open the parent directory 600 601 return open_dir(realPath.c_str()); 602 } 603 604 // _kern_read_dir 605 ssize_t 606 _kern_read_dir(int fd, struct dirent *buffer, size_t bufferSize, 607 uint32 maxCount) 608 { 609 if (maxCount <= 0) 610 return B_BAD_VALUE; 611 612 // get the descriptor 613 DirectoryDescriptor *descriptor 614 = dynamic_cast<DirectoryDescriptor*>(get_descriptor(fd)); 615 if (!descriptor) 616 return B_FILE_ERROR; 617 618 // get the next entry 619 dirent *entry; 620 errno = 0; 621 if (dynamic_cast<AttrDirDescriptor*>(descriptor)) 622 entry = fs_read_attr_dir(descriptor->dir); 623 else 624 entry = readdir(descriptor->dir); 625 if (!entry) 626 return errno; 627 628 // copy the entry 629 int entryLen = &entry->d_name[strlen(entry->d_name) + 1] - (char*)entry; 630 if (entryLen > (int)bufferSize) 631 return B_BUFFER_OVERFLOW; 632 633 memcpy(buffer, entry, entryLen); 634 635 return 1; 636 } 637 638 // _kern_rewind_dir 639 status_t 640 _kern_rewind_dir(int fd) 641 { 642 // get the descriptor 643 DirectoryDescriptor *descriptor 644 = dynamic_cast<DirectoryDescriptor*>(get_descriptor(fd)); 645 if (!descriptor) 646 return B_FILE_ERROR; 647 648 // rewind 649 if (dynamic_cast<AttrDirDescriptor*>(descriptor)) 650 fs_rewind_attr_dir(descriptor->dir); 651 else 652 rewinddir(descriptor->dir); 653 654 return B_OK; 655 } 656 657 658 // #pragma mark - 659 660 // open_file 661 static int 662 open_file(const char *path, int openMode, int perms) 663 { 664 // stat the node 665 bool exists = true; 666 struct stat st; 667 if (lstat(path, &st) < 0) { 668 exists = false; 669 if (!(openMode & O_CREAT)) 670 return errno; 671 } 672 673 Descriptor *descriptor; 674 if (exists && S_ISLNK(st.st_mode) && (openMode & O_NOTRAVERSE) != 0) { 675 // a symlink not to be followed: create a special descriptor 676 // normalize path first 677 string normalizedPath; 678 status_t error = normalize_entry_path(path, normalizedPath); 679 if (error != B_OK) 680 return error; 681 682 descriptor = new SymlinkDescriptor(normalizedPath.c_str()); 683 } else { 684 // open the file 685 openMode &= ~O_NOTRAVERSE; 686 int newFD = open(path, openMode, perms); 687 if (newFD < 0) 688 return errno; 689 690 descriptor = new FileDescriptor(newFD); 691 } 692 693 // cache path, if this is a directory 694 if (exists && S_ISDIR(st.st_mode)) 695 add_dir_path(path, NodeRef(st)); 696 697 return add_descriptor(descriptor); 698 } 699 700 // _kern_open 701 int 702 _kern_open(int fd, const char *path, int openMode, int perms) 703 { 704 // get a usable path 705 string realPath; 706 status_t error = get_path(fd, path, realPath); 707 if (error != B_OK) 708 return error; 709 710 return open_file(realPath.c_str(), openMode, perms); 711 } 712 713 // _kern_open_entry_ref 714 int 715 _kern_open_entry_ref(dev_t device, ino_t node, const char *name, 716 int openMode, int perms) 717 { 718 // get a usable path 719 string realPath; 720 status_t error = get_path(device, node, name, realPath); 721 if (error != B_OK) 722 return error; 723 724 return open_file(realPath.c_str(), openMode, perms); 725 } 726 727 // _kern_seek 728 off_t 729 _kern_seek(int fd, off_t pos, int seekType) 730 { 731 // get the descriptor 732 FileDescriptor *descriptor 733 = dynamic_cast<FileDescriptor*>(get_descriptor(fd)); 734 if (!descriptor) 735 return B_FILE_ERROR; 736 737 // seek 738 off_t result = lseek(descriptor->fd, pos, seekType); 739 if (result < 0) 740 return errno; 741 742 return result; 743 } 744 745 // _kern_read 746 ssize_t 747 _kern_read(int fd, off_t pos, void *buffer, size_t bufferSize) 748 { 749 // get the descriptor 750 FileDescriptor *descriptor 751 = dynamic_cast<FileDescriptor*>(get_descriptor(fd)); 752 if (!descriptor) 753 return B_FILE_ERROR; 754 755 // seek 756 if (pos != -1) { 757 off_t result = lseek(descriptor->fd, pos, SEEK_SET); 758 if (result < 0) 759 return errno; 760 } 761 762 // read 763 ssize_t bytesRead = haiku_host_platform_read(descriptor->fd, buffer, 764 bufferSize); 765 if (bytesRead < 0) 766 return errno; 767 768 return bytesRead; 769 } 770 771 // _kern_write 772 ssize_t 773 _kern_write(int fd, off_t pos, const void *buffer, size_t bufferSize) 774 { 775 // get the descriptor 776 FileDescriptor *descriptor 777 = dynamic_cast<FileDescriptor*>(get_descriptor(fd)); 778 if (!descriptor) 779 return B_FILE_ERROR; 780 781 // seek 782 if (pos != -1) { 783 off_t result = lseek(descriptor->fd, pos, SEEK_SET); 784 if (result < 0) 785 return errno; 786 } 787 788 // read 789 ssize_t bytesWritten = haiku_host_platform_write(descriptor->fd, buffer, 790 bufferSize); 791 if (bytesWritten < 0) 792 return errno; 793 794 return bytesWritten; 795 } 796 797 // _kern_close 798 status_t 799 _kern_close(int fd) 800 { 801 return delete_descriptor(fd); 802 } 803 804 // _kern_dup 805 int 806 _kern_dup(int fd) 807 { 808 // get the descriptor 809 Descriptor *descriptor = get_descriptor(fd); 810 if (!descriptor) 811 return B_FILE_ERROR; 812 813 // clone it 814 Descriptor *clone = NULL; 815 status_t error = descriptor->Dup(clone); 816 if (error != B_OK) 817 return error; 818 if (clone == NULL) 819 debugger("Dup() succeeded but descriptor is NULL"); 820 821 return add_descriptor(clone); 822 } 823 824 // _kern_fsync 825 status_t 826 _kern_fsync(int fd) 827 { 828 // get the descriptor 829 FileDescriptor *descriptor 830 = dynamic_cast<FileDescriptor*>(get_descriptor(fd)); 831 if (!descriptor) 832 return B_FILE_ERROR; 833 834 // sync 835 if (fsync(descriptor->fd) < 0) 836 return errno; 837 838 return B_OK; 839 } 840 841 // _kern_read_stat 842 status_t 843 _kern_read_stat(int fd, const char *path, bool traverseLink, 844 struct stat *st, size_t statSize) 845 { 846 if (path) { 847 // get a usable path 848 string realPath; 849 status_t error = get_path(fd, path, realPath); 850 if (error != B_OK) 851 return error; 852 853 // stat 854 int result; 855 if (traverseLink) 856 result = stat(realPath.c_str(), st); 857 else 858 result = lstat(realPath.c_str(), st); 859 860 if (result < 0) 861 return errno; 862 } else { 863 Descriptor *descriptor = get_descriptor(fd); 864 if (!descriptor) 865 return B_FILE_ERROR; 866 867 return descriptor->GetStat(traverseLink, st); 868 } 869 870 return B_OK; 871 } 872 873 // _kern_write_stat 874 status_t 875 _kern_write_stat(int fd, const char *path, bool traverseLink, 876 const struct stat *st, size_t statSize, int statMask) 877 { 878 // get a usable path 879 int realFD = -1; 880 string realPath; 881 status_t error; 882 bool isSymlink = false; 883 if (path) { 884 error = get_path(fd, path, realPath); 885 if (error != B_OK) 886 return error; 887 888 // stat it to see, if it is a symlink 889 struct stat tmpStat; 890 if (lstat(realPath.c_str(), &tmpStat) < 0) 891 return errno; 892 893 isSymlink = S_ISLNK(tmpStat.st_mode); 894 895 } else { 896 Descriptor *descriptor = get_descriptor(fd); 897 if (!descriptor) 898 return B_FILE_ERROR; 899 900 if (FileDescriptor *fileFD 901 = dynamic_cast<FileDescriptor*>(descriptor)) { 902 realFD = fileFD->fd; 903 904 } else if (dynamic_cast<DirectoryDescriptor*>(descriptor)) { 905 error = get_path(fd, NULL, realPath); 906 if (error != B_OK) 907 return error; 908 909 } else if (SymlinkDescriptor *linkFD 910 = dynamic_cast<SymlinkDescriptor*>(descriptor)) { 911 realPath = linkFD->path; 912 isSymlink = true; 913 914 } else 915 return B_FILE_ERROR; 916 } 917 918 // We're screwed, if the node to manipulate is a symlink. All the 919 // available functions traverse symlinks. 920 if (isSymlink && !traverseLink) 921 return B_ERROR; 922 923 if (realFD >= 0) { 924 if (statMask & B_STAT_MODE) { 925 if (fchmod(realFD, st->st_mode) < 0) 926 return errno; 927 } 928 929 if (statMask & B_STAT_UID) { 930 if (fchown(realFD, st->st_uid, (gid_t)-1) < 0) 931 return errno; 932 } 933 934 if (statMask & B_STAT_GID) { 935 if (fchown(realFD, (uid_t)-1, st->st_gid) < 0) 936 return errno; 937 } 938 939 if (statMask & B_STAT_SIZE) { 940 if (ftruncate(realFD, st->st_size) < 0) 941 return errno; 942 } 943 944 // The timestamps can only be set via utime(), but that requires a 945 // path we don't have. 946 if (statMask & (B_STAT_ACCESS_TIME | B_STAT_MODIFICATION_TIME 947 | B_STAT_CREATION_TIME | B_STAT_CHANGE_TIME)) { 948 return B_ERROR; 949 } 950 951 return 0; 952 953 } else { 954 if (statMask & B_STAT_MODE) { 955 if (chmod(realPath.c_str(), st->st_mode) < 0) 956 return errno; 957 } 958 959 if (statMask & B_STAT_UID) { 960 if (chown(realPath.c_str(), st->st_uid, (gid_t)-1) < 0) 961 return errno; 962 } 963 964 if (statMask & B_STAT_GID) { 965 if (chown(realPath.c_str(), (uid_t)-1, st->st_gid) < 0) 966 return errno; 967 } 968 969 if (statMask & B_STAT_SIZE) { 970 if (truncate(realPath.c_str(), st->st_size) < 0) 971 return errno; 972 } 973 974 if (statMask & (B_STAT_ACCESS_TIME | B_STAT_MODIFICATION_TIME)) { 975 // Grab the previous mod and access times so we only overwrite 976 // the specified time and not both 977 struct stat oldStat; 978 if (~statMask & (B_STAT_ACCESS_TIME | B_STAT_MODIFICATION_TIME)) { 979 if (stat(realPath.c_str(), &oldStat) < 0) 980 return errno; 981 } 982 983 utimbuf buffer; 984 buffer.actime = (statMask & B_STAT_ACCESS_TIME) ? st->st_atime : oldStat.st_atime; 985 buffer.modtime = (statMask & B_STAT_MODIFICATION_TIME) ? st->st_mtime : oldStat.st_mtime; 986 if (utime(realPath.c_str(), &buffer) < 0) 987 return errno; 988 } 989 990 // not supported 991 if (statMask & (B_STAT_CREATION_TIME | B_STAT_CHANGE_TIME)) 992 return B_ERROR; 993 } 994 995 return B_OK; 996 } 997 998 999 // #pragma mark - 1000 1001 // _kern_create_symlink 1002 status_t 1003 _kern_create_symlink(int fd, const char *path, const char *toPath, int mode) 1004 { 1005 // Note: path must not be NULL, so this will always work. 1006 // get a usable path 1007 string realPath; 1008 status_t error = get_path(fd, path, realPath); 1009 if (error != B_OK) 1010 return error; 1011 1012 // symlink 1013 if (symlink(toPath, realPath.c_str()) < 0) 1014 return errno; 1015 1016 return B_OK; 1017 } 1018 1019 // _kern_read_link 1020 status_t 1021 _kern_read_link(int fd, const char *path, char *buffer, size_t *_bufferSize) 1022 { 1023 // get the path 1024 string realPath; 1025 status_t error = get_path(fd, path, realPath); 1026 if (error != B_OK) 1027 return error; 1028 1029 ssize_t bytesRead = readlink(realPath.c_str(), buffer, *_bufferSize); 1030 if (bytesRead < 0) 1031 return errno; 1032 1033 // On Haiku _kern_read_link will return the length of the link, *not* 1034 // the number of bytes written to the buffer parameter. To emulate that 1035 // here, we use readlink() to read the links contents, and then if it is 1036 // possible that the link contents didn't fit in buffer, we'll fall back 1037 // to lstat() to get the full length of the link. 1038 if (static_cast<size_t>(bytesRead) < *_bufferSize) { 1039 buffer[bytesRead] = '\0'; 1040 *_bufferSize = bytesRead; 1041 } else { 1042 // The number of bytes copied by readlink() tells us that the entire 1043 // link might not have fit into buffer. Fall back to getting the full 1044 // length of the link using lstat. 1045 struct stat linkStat; 1046 if (lstat(realPath.c_str(), &linkStat) != 0) 1047 return errno; 1048 1049 *_bufferSize = linkStat.st_size; 1050 } 1051 1052 return B_OK; 1053 } 1054 1055 // _kern_unlink 1056 status_t 1057 _kern_unlink(int fd, const char *path) 1058 { 1059 // get a usable path 1060 string realPath; 1061 status_t error = get_path(fd, path, realPath); 1062 if (error != B_OK) 1063 return error; 1064 1065 // unlink 1066 if (unlink(realPath.c_str()) < 0) 1067 return errno; 1068 1069 return B_OK; 1070 } 1071 1072 // _kern_rename 1073 status_t 1074 _kern_rename(int oldDir, const char *oldPath, int newDir, const char *newPath) 1075 { 1076 // get usable paths 1077 string realOldPath; 1078 status_t error = get_path(oldDir, oldPath, realOldPath); 1079 if (error != B_OK) 1080 return error; 1081 1082 string realNewPath; 1083 error = get_path(newDir, newPath, realNewPath); 1084 if (error != B_OK) 1085 return error; 1086 1087 // rename 1088 if (rename(realOldPath.c_str(), realNewPath.c_str()) < 0) 1089 return errno; 1090 1091 return B_OK; 1092 } 1093 1094 1095 // #pragma mark - 1096 1097 // _kern_lock_node 1098 status_t 1099 _kern_lock_node(int fd) 1100 { 1101 return B_ERROR; 1102 } 1103 1104 // _kern_unlock_node 1105 status_t 1106 _kern_unlock_node(int fd) 1107 { 1108 return B_ERROR; 1109 } 1110 1111 1112 // #pragma mark - 1113 1114 #if !defined(HAIKU_HOST_PLATFORM_HAIKU) 1115 // read_pos 1116 ssize_t 1117 read_pos(int fd, off_t pos, void *buffer, size_t bufferSize) 1118 { 1119 // seek 1120 off_t result = lseek(fd, pos, SEEK_SET); 1121 if (result < 0) 1122 return errno; 1123 1124 // read 1125 ssize_t bytesRead = haiku_host_platform_read(fd, buffer, bufferSize); 1126 if (bytesRead < 0) { 1127 errno = bytesRead; 1128 return -1; 1129 } 1130 1131 return bytesRead; 1132 } 1133 1134 // write_pos 1135 ssize_t 1136 write_pos(int fd, off_t pos, const void *buffer, size_t bufferSize) 1137 { 1138 // If this is an attribute descriptor, let it do the job. 1139 AttributeDescriptor* descriptor 1140 = dynamic_cast<AttributeDescriptor*>(get_descriptor(fd)); 1141 if (descriptor != NULL) { 1142 status_t error = descriptor->Write(pos, buffer, bufferSize); 1143 if (error != B_OK) { 1144 errno = error; 1145 return -1; 1146 } 1147 1148 return bufferSize; 1149 } 1150 1151 // seek 1152 off_t result = lseek(fd, pos, SEEK_SET); 1153 if (result < 0) 1154 return errno; 1155 1156 // write 1157 ssize_t bytesWritten = haiku_host_platform_write(fd, buffer, bufferSize); 1158 if (bytesWritten < 0) { 1159 errno = bytesWritten; 1160 return -1; 1161 } 1162 1163 return bytesWritten; 1164 } 1165 1166 // readv_pos 1167 ssize_t 1168 readv_pos(int fd, off_t pos, const struct iovec *vec, int count) 1169 { 1170 // seek 1171 off_t result = lseek(fd, pos, SEEK_SET); 1172 if (result < 0) 1173 return errno; 1174 1175 // read 1176 ssize_t bytesRead = haiku_host_platform_readv(fd, vec, count); 1177 if (bytesRead < 0) { 1178 errno = bytesRead; 1179 return -1; 1180 } 1181 1182 return bytesRead; 1183 } 1184 1185 // writev_pos 1186 ssize_t 1187 writev_pos(int fd, off_t pos, const struct iovec *vec, int count) 1188 { 1189 // seek 1190 off_t result = lseek(fd, pos, SEEK_SET); 1191 if (result < 0) 1192 return errno; 1193 1194 // read 1195 ssize_t bytesWritten = haiku_host_platform_writev(fd, vec, count); 1196 if (bytesWritten < 0) { 1197 errno = bytesWritten; 1198 return -1; 1199 } 1200 1201 return bytesWritten; 1202 } 1203 #endif 1204 1205 1206 // #pragma mark - 1207 1208 1209 int 1210 _haiku_build_fchmod(int fd, mode_t mode) 1211 { 1212 return _haiku_build_fchmodat(fd, NULL, mode, AT_SYMLINK_NOFOLLOW); 1213 } 1214 1215 1216 int 1217 _haiku_build_fchmodat(int fd, const char* path, mode_t mode, int flag) 1218 { 1219 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1220 return fchmodat(fd, path, mode, flag); 1221 1222 struct stat st; 1223 st.st_mode = mode; 1224 1225 RETURN_AND_SET_ERRNO(_kern_write_stat(fd, path, 1226 (flag & AT_SYMLINK_NOFOLLOW) == 0, &st, sizeof(st), B_STAT_MODE)); 1227 } 1228 1229 1230 int 1231 _haiku_build_fstat(int fd, struct stat* st) 1232 { 1233 return _haiku_build_fstatat(fd, NULL, st, AT_SYMLINK_NOFOLLOW); 1234 } 1235 1236 1237 int 1238 _haiku_build_fstatat(int fd, const char* path, struct stat* st, int flag) 1239 { 1240 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1241 return fstatat(fd, path, st, flag); 1242 1243 RETURN_AND_SET_ERRNO(_kern_read_stat(fd, path, 1244 (flag & AT_SYMLINK_NOFOLLOW) == 0, st, sizeof(*st))); 1245 } 1246 1247 1248 int 1249 _haiku_build_mkdirat(int fd, const char* path, mode_t mode) 1250 { 1251 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1252 return mkdirat(fd, path, mode); 1253 1254 RETURN_AND_SET_ERRNO(_kern_create_dir(fd, path, mode)); 1255 } 1256 1257 1258 int 1259 _haiku_build_mkfifoat(int fd, const char* path, mode_t mode) 1260 { 1261 return mkfifoat(fd, path, mode); 1262 1263 // TODO: Handle non-system FDs. 1264 } 1265 1266 1267 int 1268 _haiku_build_utimensat(int fd, const char* path, const struct timespec times[2], 1269 int flag) 1270 { 1271 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1272 return utimensat(fd, path, times, flag); 1273 1274 struct stat stat; 1275 status_t status; 1276 uint32 mask = 0; 1277 1278 // Init the stat time fields to the current time, if at least one time is 1279 // supposed to be set to it. 1280 if (times == NULL || times[0].tv_nsec == UTIME_NOW 1281 || times[1].tv_nsec == UTIME_NOW) { 1282 timeval now; 1283 gettimeofday(&now, NULL); 1284 HAIKU_HOST_STAT_ATIM(stat).tv_sec 1285 = HAIKU_HOST_STAT_MTIM(stat).tv_sec = now.tv_sec; 1286 HAIKU_HOST_STAT_ATIM(stat).tv_nsec 1287 = HAIKU_HOST_STAT_MTIM(stat).tv_nsec = now.tv_usec * 1000; 1288 } 1289 1290 if (times != NULL) { 1291 // access time 1292 if (times[0].tv_nsec != UTIME_OMIT) { 1293 mask |= B_STAT_ACCESS_TIME; 1294 1295 if (times[0].tv_nsec != UTIME_NOW) { 1296 if (times[0].tv_nsec < 0 || times[0].tv_nsec > 999999999) 1297 RETURN_AND_SET_ERRNO(EINVAL); 1298 } 1299 1300 HAIKU_HOST_STAT_ATIM(stat) = times[0]; 1301 } 1302 1303 // modified time 1304 if (times[1].tv_nsec != UTIME_OMIT) { 1305 mask |= B_STAT_MODIFICATION_TIME; 1306 1307 if (times[1].tv_nsec != UTIME_NOW) { 1308 if (times[1].tv_nsec < 0 || times[1].tv_nsec > 999999999) 1309 RETURN_AND_SET_ERRNO(EINVAL); 1310 } 1311 1312 HAIKU_HOST_STAT_MTIM(stat) = times[1]; 1313 } 1314 } else 1315 mask |= B_STAT_ACCESS_TIME | B_STAT_MODIFICATION_TIME; 1316 1317 // set the times -- as per spec we even need to do this, if both have 1318 // UTIME_OMIT set 1319 status = _kern_write_stat(fd, path, (flag & AT_SYMLINK_NOFOLLOW) == 0, 1320 &stat, sizeof(struct stat), mask); 1321 1322 RETURN_AND_SET_ERRNO(status); 1323 } 1324 1325 1326 int 1327 _haiku_build_futimens(int fd, const struct timespec times[2]) 1328 { 1329 return _haiku_build_utimensat(fd, NULL, times, AT_SYMLINK_NOFOLLOW); 1330 } 1331 1332 1333 int 1334 _haiku_build_faccessat(int fd, const char* path, int accessMode, int flag) 1335 { 1336 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1337 return faccessat(fd, path, accessMode, flag); 1338 1339 // stat the file 1340 struct stat st; 1341 status_t error = _kern_read_stat(fd, path, false, &st, sizeof(st)); 1342 if (error != B_OK) 1343 RETURN_AND_SET_ERRNO(error); 1344 1345 // get the current user 1346 uid_t uid = (flag & AT_EACCESS) != 0 ? geteuid() : getuid(); 1347 1348 int fileMode = 0; 1349 1350 if (uid == 0) { 1351 // user is root 1352 // root has always read/write permission, but at least one of the 1353 // X bits must be set for execute permission 1354 fileMode = R_OK | W_OK; 1355 if ((st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0) 1356 fileMode |= X_OK; 1357 } else if (st.st_uid == uid) { 1358 // user is node owner 1359 if ((st.st_mode & S_IRUSR) != 0) 1360 fileMode |= R_OK; 1361 if ((st.st_mode & S_IWUSR) != 0) 1362 fileMode |= W_OK; 1363 if ((st.st_mode & S_IXUSR) != 0) 1364 fileMode |= X_OK; 1365 } else if (st.st_gid == ((flag & AT_EACCESS) != 0 ? getegid() : getgid())) { 1366 // user is in owning group 1367 if ((st.st_mode & S_IRGRP) != 0) 1368 fileMode |= R_OK; 1369 if ((st.st_mode & S_IWGRP) != 0) 1370 fileMode |= W_OK; 1371 if ((st.st_mode & S_IXGRP) != 0) 1372 fileMode |= X_OK; 1373 } else { 1374 // user is one of the others 1375 if ((st.st_mode & S_IROTH) != 0) 1376 fileMode |= R_OK; 1377 if ((st.st_mode & S_IWOTH) != 0) 1378 fileMode |= W_OK; 1379 if ((st.st_mode & S_IXOTH) != 0) 1380 fileMode |= X_OK; 1381 } 1382 1383 if ((accessMode & ~fileMode) != 0) 1384 RETURN_AND_SET_ERRNO(EACCES); 1385 1386 return 0; 1387 } 1388 1389 1390 int 1391 _haiku_build_fchdir(int fd) 1392 { 1393 if (is_unknown_or_system_descriptor(fd)) 1394 return fchdir(fd); 1395 1396 RETURN_AND_SET_ERRNO(B_FILE_ERROR); 1397 } 1398 1399 1400 int 1401 _haiku_build_close(int fd) 1402 { 1403 if (get_descriptor(fd) == NULL) 1404 return close(fd); 1405 1406 RETURN_AND_SET_ERRNO(_kern_close(fd)); 1407 } 1408 1409 1410 int 1411 _haiku_build_dup(int fd) 1412 { 1413 if (get_descriptor(fd) == NULL) 1414 return close(fd); 1415 1416 RETURN_AND_SET_ERRNO(_kern_dup(fd)); 1417 } 1418 1419 1420 int 1421 _haiku_build_dup2(int fd1, int fd2) 1422 { 1423 if (is_unknown_or_system_descriptor(fd1)) 1424 return dup2(fd1, fd2); 1425 1426 // TODO: Handle non-system FDs. 1427 RETURN_AND_SET_ERRNO(B_NOT_SUPPORTED); 1428 } 1429 1430 1431 int 1432 _haiku_build_linkat(int toFD, const char* toPath, int pathFD, const char* path, 1433 int flag) 1434 { 1435 return linkat(toFD, toPath, pathFD, path, flag); 1436 1437 // TODO: Handle non-system FDs. 1438 } 1439 1440 1441 int 1442 _haiku_build_unlinkat(int fd, const char* path, int flag) 1443 { 1444 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1445 return unlinkat(fd, path, flag); 1446 1447 RETURN_AND_SET_ERRNO(_kern_unlink(fd, path)); 1448 } 1449 1450 1451 ssize_t 1452 _haiku_build_readlinkat(int fd, const char* path, char* buffer, 1453 size_t bufferSize) 1454 { 1455 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1456 return readlinkat(fd, path, buffer, bufferSize); 1457 1458 status_t error = _kern_read_link(fd, path, buffer, &bufferSize); 1459 if (error != B_OK) 1460 RETURN_AND_SET_ERRNO(error); 1461 1462 return bufferSize; 1463 } 1464 1465 1466 int 1467 _haiku_build_symlinkat(const char* toPath, int fd, const char* symlinkPath) 1468 { 1469 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1470 return symlinkat(toPath, fd, symlinkPath); 1471 1472 RETURN_AND_SET_ERRNO(_kern_create_symlink(fd, symlinkPath, toPath, 1473 S_IRWXU | S_IRWXG | S_IRWXO)); 1474 } 1475 1476 1477 int 1478 _haiku_build_ftruncate(int fd, off_t newSize) 1479 { 1480 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1481 return ftruncate(fd, newSize); 1482 1483 struct stat st; 1484 st.st_size = newSize; 1485 1486 RETURN_AND_SET_ERRNO(_kern_write_stat(fd, NULL, false, &st, sizeof(st), 1487 B_STAT_SIZE)); 1488 } 1489 1490 1491 int 1492 _haiku_build_fchown(int fd, uid_t owner, gid_t group) 1493 { 1494 return _haiku_build_fchownat(fd, NULL, owner, group, AT_SYMLINK_NOFOLLOW); 1495 } 1496 1497 1498 int 1499 _haiku_build_fchownat(int fd, const char* path, uid_t owner, gid_t group, 1500 int flag) 1501 { 1502 if (fd >= 0 && fd != AT_FDCWD && get_descriptor(fd) == NULL) 1503 return fchownat(fd, path, owner, group, flag); 1504 1505 struct stat st; 1506 st.st_uid = owner; 1507 st.st_gid = group; 1508 1509 RETURN_AND_SET_ERRNO(_kern_write_stat(fd, path, 1510 (flag & AT_SYMLINK_NOFOLLOW) == 0, &st, sizeof(st), 1511 B_STAT_UID | B_STAT_GID)); 1512 } 1513 1514 1515 int 1516 _haiku_build_mknodat(int fd, const char* name, mode_t mode, dev_t dev) 1517 { 1518 return mknodat(fd, name, mode, dev); 1519 1520 // TODO: Handle non-system FDs. 1521 } 1522 1523 1524 int 1525 _haiku_build_creat(const char* path, mode_t mode) 1526 { 1527 return _haiku_build_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); 1528 } 1529 1530 1531 int 1532 _haiku_build_open(const char* path, int openMode, mode_t permissions) 1533 { 1534 return _haiku_build_openat(AT_FDCWD, path, openMode, permissions); 1535 } 1536 1537 1538 int 1539 _haiku_build_openat(int fd, const char* path, int openMode, mode_t permissions) 1540 { 1541 // adapt the permissions as required by POSIX 1542 mode_t mask = umask(0); 1543 umask(mask); 1544 permissions &= ~mask; 1545 1546 RETURN_AND_SET_ERRNO(_kern_open(fd, path, openMode, permissions)); 1547 } 1548 1549 1550 int 1551 _haiku_build_fcntl(int fd, int op, int argument) 1552 { 1553 if (is_unknown_or_system_descriptor(fd)) 1554 return fcntl(fd, op, argument); 1555 1556 RETURN_AND_SET_ERRNO(B_NOT_SUPPORTED); 1557 } 1558 1559 1560 int 1561 _haiku_build_renameat(int fromFD, const char* from, int toFD, const char* to) 1562 { 1563 if ((fromFD >= 0 && fromFD != AT_FDCWD && get_descriptor(fromFD) == NULL) 1564 || (toFD >= 0 && toFD != AT_FDCWD && get_descriptor(toFD) == NULL)) { 1565 return renameat(fromFD, from, toFD, to); 1566 } 1567 1568 RETURN_AND_SET_ERRNO(_kern_rename(fromFD, from, toFD, to)); 1569 } 1570