openat.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* provide a replacement openat function
  2. Copyright (C) 2004-2006, 2008-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* written by Jim Meyering */
  14. #ifndef _GL_HEADER_OPENAT
  15. #define _GL_HEADER_OPENAT
  16. #include <fcntl.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #include <stdbool.h>
  21. #ifndef _GL_INLINE_HEADER_BEGIN
  22. #error "Please include config.h first."
  23. #endif
  24. _GL_INLINE_HEADER_BEGIN
  25. #if !HAVE_OPENAT
  26. int openat_permissive (int fd, char const *file, int flags, mode_t mode,
  27. int *cwd_errno);
  28. bool openat_needs_fchdir (void);
  29. #else
  30. # define openat_permissive(Fd, File, Flags, Mode, Cwd_errno) \
  31. openat (Fd, File, Flags, Mode)
  32. # define openat_needs_fchdir() false
  33. #endif
  34. _Noreturn void openat_restore_fail (int);
  35. _Noreturn void openat_save_fail (int);
  36. /* Using these function names makes application code
  37. slightly more readable than it would be with
  38. fchownat (..., 0) or fchownat (..., AT_SYMLINK_NOFOLLOW). */
  39. #if GNULIB_CHOWNAT
  40. # ifndef CHOWNAT_INLINE
  41. # define CHOWNAT_INLINE _GL_INLINE
  42. # endif
  43. CHOWNAT_INLINE int
  44. chownat (int fd, char const *file, uid_t owner, gid_t group)
  45. {
  46. return fchownat (fd, file, owner, group, 0);
  47. }
  48. CHOWNAT_INLINE int
  49. lchownat (int fd, char const *file, uid_t owner, gid_t group)
  50. {
  51. return fchownat (fd, file, owner, group, AT_SYMLINK_NOFOLLOW);
  52. }
  53. #endif
  54. #if GNULIB_CHMODAT
  55. # ifndef CHMODAT_INLINE
  56. # define CHMODAT_INLINE _GL_INLINE
  57. # endif
  58. CHMODAT_INLINE int
  59. chmodat (int fd, char const *file, mode_t mode)
  60. {
  61. return fchmodat (fd, file, mode, 0);
  62. }
  63. CHMODAT_INLINE int
  64. lchmodat (int fd, char const *file, mode_t mode)
  65. {
  66. return fchmodat (fd, file, mode, AT_SYMLINK_NOFOLLOW);
  67. }
  68. #endif
  69. #if GNULIB_STATAT
  70. # ifndef STATAT_INLINE
  71. # define STATAT_INLINE _GL_INLINE
  72. # endif
  73. STATAT_INLINE int
  74. statat (int fd, char const *name, struct stat *st)
  75. {
  76. return fstatat (fd, name, st, 0);
  77. }
  78. STATAT_INLINE int
  79. lstatat (int fd, char const *name, struct stat *st)
  80. {
  81. return fstatat (fd, name, st, AT_SYMLINK_NOFOLLOW);
  82. }
  83. #endif
  84. /* For now, there are no wrappers named laccessat or leuidaccessat,
  85. since gnulib doesn't support faccessat(,AT_SYMLINK_NOFOLLOW) and
  86. since access rights on symlinks are of limited utility. Likewise,
  87. wrappers are not provided for accessat or euidaccessat, so as to
  88. avoid dragging in -lgen on some platforms. */
  89. _GL_INLINE_HEADER_END
  90. #endif /* _GL_HEADER_OPENAT */