stat.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Work around platform bugs in stat.
  2. Copyright (C) 2009-2013 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 Eric Blake */
  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_stat doesn't recurse to
  16. rpl_stat. */
  17. #define __need_system_sys_stat_h
  18. #include <config.h>
  19. /* Get the original definition of stat. 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 (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  24. # if _GL_WINDOWS_64_BIT_ST_SIZE
  25. # undef stat /* avoid warning on mingw64 with _FILE_OFFSET_BITS=64 */
  26. # define stat _stati64
  27. # define REPLACE_FUNC_STAT_DIR 1
  28. # undef REPLACE_FUNC_STAT_FILE
  29. # elif REPLACE_FUNC_STAT_FILE
  30. /* mingw64 has a broken stat() function, based on _stat(), in libmingwex.a.
  31. Bypass it. */
  32. # define stat _stat
  33. # define REPLACE_FUNC_STAT_DIR 1
  34. # undef REPLACE_FUNC_STAT_FILE
  35. # endif
  36. #endif
  37. static int
  38. orig_stat (const char *filename, struct stat *buf)
  39. {
  40. return stat (filename, buf);
  41. }
  42. /* Specification. */
  43. /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
  44. eliminates this include because of the preliminary #include <sys/stat.h>
  45. above. */
  46. #include "sys/stat.h"
  47. #include <errno.h>
  48. #include <limits.h>
  49. #include <stdbool.h>
  50. #include <string.h>
  51. #include "dosname.h"
  52. #include "verify.h"
  53. #if REPLACE_FUNC_STAT_DIR
  54. # include "pathmax.h"
  55. /* The only known systems where REPLACE_FUNC_STAT_DIR is needed also
  56. have a constant PATH_MAX. */
  57. # ifndef PATH_MAX
  58. # error "Please port this replacement to your platform"
  59. # endif
  60. #endif
  61. /* Store information about NAME into ST. Work around bugs with
  62. trailing slashes. Mingw has other bugs (such as st_ino always
  63. being 0 on success) which this wrapper does not work around. But
  64. at least this implementation provides the ability to emulate fchdir
  65. correctly. */
  66. int
  67. rpl_stat (char const *name, struct stat *st)
  68. {
  69. int result = orig_stat (name, st);
  70. #if REPLACE_FUNC_STAT_FILE
  71. /* Solaris 9 mistakenly succeeds when given a non-directory with a
  72. trailing slash. */
  73. if (result == 0 && !S_ISDIR (st->st_mode))
  74. {
  75. size_t len = strlen (name);
  76. if (ISSLASH (name[len - 1]))
  77. {
  78. errno = ENOTDIR;
  79. return -1;
  80. }
  81. }
  82. #endif /* REPLACE_FUNC_STAT_FILE */
  83. #if REPLACE_FUNC_STAT_DIR
  84. if (result == -1 && errno == ENOENT)
  85. {
  86. /* Due to mingw's oddities, there are some directories (like
  87. c:\) where stat() only succeeds with a trailing slash, and
  88. other directories (like c:\windows) where stat() only
  89. succeeds without a trailing slash. But we want the two to be
  90. synonymous, since chdir() manages either style. Likewise, Mingw also
  91. reports ENOENT for names longer than PATH_MAX, when we want
  92. ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
  93. Fortunately, mingw PATH_MAX is small enough for stack
  94. allocation. */
  95. char fixed_name[PATH_MAX + 1] = {0};
  96. size_t len = strlen (name);
  97. bool check_dir = false;
  98. verify (PATH_MAX <= 4096);
  99. if (PATH_MAX <= len)
  100. errno = ENAMETOOLONG;
  101. else if (len)
  102. {
  103. strcpy (fixed_name, name);
  104. if (ISSLASH (fixed_name[len - 1]))
  105. {
  106. check_dir = true;
  107. while (len && ISSLASH (fixed_name[len - 1]))
  108. fixed_name[--len] = '\0';
  109. if (!len)
  110. fixed_name[0] = '/';
  111. }
  112. else
  113. fixed_name[len++] = '/';
  114. result = orig_stat (fixed_name, st);
  115. if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
  116. {
  117. result = -1;
  118. errno = ENOTDIR;
  119. }
  120. }
  121. }
  122. #endif /* REPLACE_FUNC_STAT_DIR */
  123. return result;
  124. }