fstatat.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Work around an fstatat bug on Solaris 9.
  2. Copyright (C) 2006, 2009-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 Paul Eggert and Jim Meyering. */
  14. /* If the user's config.h happens to include <sys/stat.h>, let it include only
  15. the system's <sys/stat.h> here, so that orig_fstatat doesn't recurse to
  16. rpl_fstatat. */
  17. #define __need_system_sys_stat_h
  18. #include <config.h>
  19. /* Get the original definition of fstatat. It might be defined as a macro. */
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #undef __need_system_sys_stat_h
  23. #if HAVE_FSTATAT && HAVE_WORKING_FSTATAT_ZERO_FLAG
  24. static int
  25. orig_fstatat (int fd, char const *filename, struct stat *buf, int flags)
  26. {
  27. return fstatat (fd, filename, buf, flags);
  28. }
  29. #endif
  30. #ifdef __osf__
  31. /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
  32. eliminates this include because of the preliminary #include <sys/stat.h>
  33. above. */
  34. # include "sys/stat.h"
  35. #else
  36. # include <sys/stat.h>
  37. #endif
  38. #include "stat-time.h"
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #if HAVE_FSTATAT && HAVE_WORKING_FSTATAT_ZERO_FLAG
  44. # ifndef LSTAT_FOLLOWS_SLASHED_SYMLINK
  45. # define LSTAT_FOLLOWS_SLASHED_SYMLINK 0
  46. # endif
  47. static int
  48. normal_fstatat (int fd, char const *file, struct stat *st, int flag)
  49. {
  50. return stat_time_normalize (orig_fstatat (fd, file, st, flag), st);
  51. }
  52. /* fstatat should always follow symbolic links that end in /, but on
  53. Solaris 9 it doesn't if AT_SYMLINK_NOFOLLOW is specified.
  54. Likewise, trailing slash on a non-directory should be an error.
  55. These are the same problems that lstat.c and stat.c address, so
  56. solve it in a similar way.
  57. AIX 7.1 fstatat (AT_FDCWD, ..., 0) always fails, which is a bug.
  58. Work around this bug if FSTATAT_AT_FDCWD_0_BROKEN is nonzero. */
  59. int
  60. rpl_fstatat (int fd, char const *file, struct stat *st, int flag)
  61. {
  62. int result = normal_fstatat (fd, file, st, flag);
  63. size_t len;
  64. if (LSTAT_FOLLOWS_SLASHED_SYMLINK || result != 0)
  65. return result;
  66. len = strlen (file);
  67. if (flag & AT_SYMLINK_NOFOLLOW)
  68. {
  69. /* Fix lstat behavior. */
  70. if (file[len - 1] != '/' || S_ISDIR (st->st_mode))
  71. return 0;
  72. if (!S_ISLNK (st->st_mode))
  73. {
  74. errno = ENOTDIR;
  75. return -1;
  76. }
  77. result = normal_fstatat (fd, file, st, flag & ~AT_SYMLINK_NOFOLLOW);
  78. }
  79. /* Fix stat behavior. */
  80. if (result == 0 && !S_ISDIR (st->st_mode) && file[len - 1] == '/')
  81. {
  82. errno = ENOTDIR;
  83. return -1;
  84. }
  85. return result;
  86. }
  87. #else /* ! (HAVE_FSTATAT && HAVE_WORKING_FSTATAT_ZERO_FLAG) */
  88. /* On mingw, the gnulib <sys/stat.h> defines 'stat' as a function-like
  89. macro; but using it in AT_FUNC_F2 causes compilation failure
  90. because the preprocessor sees a use of a macro that requires two
  91. arguments but is only given one. Hence, we need an inline
  92. forwarder to get past the preprocessor. */
  93. static int
  94. stat_func (char const *name, struct stat *st)
  95. {
  96. return stat (name, st);
  97. }
  98. /* Likewise, if there is no native 'lstat', then the gnulib
  99. <sys/stat.h> defined it as stat, which also needs adjustment. */
  100. # if !HAVE_LSTAT
  101. # undef lstat
  102. # define lstat stat_func
  103. # endif
  104. /* Replacement for Solaris' function by the same name.
  105. <https://www.google.com/search?q=fstatat+site:docs.oracle.com>
  106. First, try to simulate it via l?stat ("/proc/self/fd/FD/FILE").
  107. Failing that, simulate it via save_cwd/fchdir/(stat|lstat)/restore_cwd.
  108. If either the save_cwd or the restore_cwd fails (relatively unlikely),
  109. then give a diagnostic and exit nonzero.
  110. Otherwise, this function works just like Solaris' fstatat. */
  111. # define AT_FUNC_NAME fstatat
  112. # define AT_FUNC_F1 lstat
  113. # define AT_FUNC_F2 stat_func
  114. # define AT_FUNC_USE_F1_COND AT_SYMLINK_NOFOLLOW
  115. # define AT_FUNC_POST_FILE_PARAM_DECLS , struct stat *st, int flag
  116. # define AT_FUNC_POST_FILE_ARGS , st
  117. # include "at-func.c"
  118. # undef AT_FUNC_NAME
  119. # undef AT_FUNC_F1
  120. # undef AT_FUNC_F2
  121. # undef AT_FUNC_USE_F1_COND
  122. # undef AT_FUNC_POST_FILE_PARAM_DECLS
  123. # undef AT_FUNC_POST_FILE_ARGS
  124. #endif /* !HAVE_FSTATAT */