stat.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Work around platform bugs in stat.
  2. Copyright (C) 2009, 2010 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 Lesser 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* written by Eric Blake */
  14. #include <config.h>
  15. /* Get the original definition of stat. It might be defined as a macro. */
  16. #define __need_system_sys_stat_h
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #undef __need_system_sys_stat_h
  20. static inline int
  21. orig_stat (const char *filename, struct stat *buf)
  22. {
  23. return stat (filename, buf);
  24. }
  25. /* Specification. */
  26. #include <sys/stat.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #include <stdbool.h>
  30. #include <string.h>
  31. /* Store information about NAME into ST. Work around bugs with
  32. trailing slashes. Mingw has other bugs (such as st_ino always
  33. being 0 on success) which this wrapper does not work around. But
  34. at least this implementation provides the ability to emulate fchdir
  35. correctly. */
  36. int
  37. rpl_stat (char const *name, struct stat *st)
  38. {
  39. int result = orig_stat (name, st);
  40. #if REPLACE_FUNC_STAT_FILE
  41. /* Solaris 9 mistakenly succeeds when given a non-directory with a
  42. trailing slash. */
  43. if (result == 0 && !S_ISDIR (st->st_mode))
  44. {
  45. size_t len = strlen (name);
  46. if (ISSLASH (name[len - 1]))
  47. {
  48. errno = ENOTDIR;
  49. return -1;
  50. }
  51. }
  52. #endif /* REPLACE_FUNC_STAT_FILE */
  53. #if REPLACE_FUNC_STAT_DIR
  54. if (result == -1 && errno == ENOENT)
  55. {
  56. /* Due to mingw's oddities, there are some directories (like
  57. c:\) where stat() only succeeds with a trailing slash, and
  58. other directories (like c:\windows) where stat() only
  59. succeeds without a trailing slash. But we want the two to be
  60. synonymous, since chdir() manages either style. Likewise, Mingw also
  61. reports ENOENT for names longer than PATH_MAX, when we want
  62. ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
  63. Fortunately, mingw PATH_MAX is small enough for stack
  64. allocation. */
  65. char fixed_name[PATH_MAX + 1] = {0};
  66. size_t len = strlen (name);
  67. bool check_dir = false;
  68. if (PATH_MAX <= len)
  69. errno = ENAMETOOLONG;
  70. else if (len)
  71. {
  72. strcpy (fixed_name, name);
  73. if (ISSLASH (fixed_name[len - 1]))
  74. {
  75. check_dir = true;
  76. while (len && ISSLASH (fixed_name[len - 1]))
  77. fixed_name[--len] = '\0';
  78. if (!len)
  79. fixed_name[0] = '/';
  80. }
  81. else
  82. fixed_name[len++] = '/';
  83. result = orig_stat (fixed_name, st);
  84. if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
  85. {
  86. result = -1;
  87. errno = ENOTDIR;
  88. }
  89. }
  90. }
  91. #endif /* REPLACE_FUNC_STAT_DIR */
  92. return result;
  93. }