stat.cpp 3.2 KB

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