fstatat.c 4.4 KB

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