stat.c 4.0 KB

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