stat.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #include <syscall.h>
  13. #include <unistd.h>
  14. extern "C" {
  15. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/umask.html
  16. mode_t umask(mode_t mask)
  17. {
  18. return syscall(SC_umask, mask);
  19. }
  20. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html
  21. int mkdir(char const* pathname, mode_t mode)
  22. {
  23. if (!pathname) {
  24. errno = EFAULT;
  25. return -1;
  26. }
  27. int rc = syscall(SC_mkdir, pathname, strlen(pathname), mode);
  28. __RETURN_WITH_ERRNO(rc, rc, -1);
  29. }
  30. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
  31. int chmod(char const* pathname, mode_t mode)
  32. {
  33. return fchmodat(AT_FDCWD, pathname, mode, 0);
  34. }
  35. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmodat.html
  36. int fchmodat(int dirfd, char const* pathname, mode_t mode, int flags)
  37. {
  38. if (!pathname) {
  39. errno = EFAULT;
  40. return -1;
  41. }
  42. if (flags & ~AT_SYMLINK_NOFOLLOW) {
  43. errno = EINVAL;
  44. return -1;
  45. }
  46. Syscall::SC_chmod_params params {
  47. dirfd,
  48. { pathname, strlen(pathname) },
  49. mode,
  50. !(flags & AT_SYMLINK_NOFOLLOW)
  51. };
  52. int rc = syscall(SC_chmod, &params);
  53. __RETURN_WITH_ERRNO(rc, rc, -1);
  54. }
  55. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmod.html
  56. int fchmod(int fd, mode_t mode)
  57. {
  58. int rc = syscall(SC_fchmod, fd, mode);
  59. __RETURN_WITH_ERRNO(rc, rc, -1);
  60. }
  61. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifo.html
  62. int mkfifo(char const* pathname, mode_t mode)
  63. {
  64. return mknod(pathname, mode | S_IFIFO, 0);
  65. }
  66. static int do_stat(int dirfd, char const* path, struct stat* statbuf, bool follow_symlinks)
  67. {
  68. if (!path) {
  69. errno = EFAULT;
  70. return -1;
  71. }
  72. Syscall::SC_stat_params params { { path, strlen(path) }, statbuf, dirfd, follow_symlinks };
  73. int rc = syscall(SC_stat, &params);
  74. __RETURN_WITH_ERRNO(rc, rc, -1);
  75. }
  76. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/lstat.html
  77. int lstat(char const* path, struct stat* statbuf)
  78. {
  79. return do_stat(AT_FDCWD, path, statbuf, false);
  80. }
  81. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/stat.html
  82. int stat(char const* path, struct stat* statbuf)
  83. {
  84. return do_stat(AT_FDCWD, path, statbuf, true);
  85. }
  86. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstat.html
  87. int fstat(int fd, struct stat* statbuf)
  88. {
  89. int rc = syscall(SC_fstat, fd, statbuf);
  90. __RETURN_WITH_ERRNO(rc, rc, -1);
  91. }
  92. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstatat.html
  93. int fstatat(int fd, char const* path, struct stat* statbuf, int flags)
  94. {
  95. return do_stat(fd, path, statbuf, !(flags & AT_SYMLINK_NOFOLLOW));
  96. }
  97. }