| 1 | /* Work around platform bugs in stat.
|
|---|
| 2 | Copyright (C) 2009-2012 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This program is free software: you can redistribute it and/or modify
|
|---|
| 5 | it under the terms of the GNU General Public License as published by
|
|---|
| 6 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 7 | (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | This program is distributed in the hope that it will be useful,
|
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | You should have received a copy of the GNU General Public License
|
|---|
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|---|
| 16 |
|
|---|
| 17 | /* written by Eric Blake */
|
|---|
| 18 |
|
|---|
| 19 | /* If the user's config.h happens to include <sys/stat.h>, let it include only
|
|---|
| 20 | the system's <sys/stat.h> here, so that orig_stat doesn't recurse to
|
|---|
| 21 | rpl_stat. */
|
|---|
| 22 | #define __need_system_sys_stat_h
|
|---|
| 23 | #include <config.h>
|
|---|
| 24 |
|
|---|
| 25 | /* Get the original definition of stat. It might be defined as a macro. */
|
|---|
| 26 | #include <sys/types.h>
|
|---|
| 27 | #include <sys/stat.h>
|
|---|
| 28 | #undef __need_system_sys_stat_h
|
|---|
| 29 |
|
|---|
| 30 | #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
|
|---|
| 31 | # if _GL_WINDOWS_64_BIT_ST_SIZE
|
|---|
| 32 | # define stat _stati64
|
|---|
| 33 | # define REPLACE_FUNC_STAT_DIR 1
|
|---|
| 34 | # undef REPLACE_FUNC_STAT_FILE
|
|---|
| 35 | # elif REPLACE_FUNC_STAT_FILE
|
|---|
| 36 | /* mingw64 has a broken stat() function, based on _stat(), in libmingwex.a.
|
|---|
| 37 | Bypass it. */
|
|---|
| 38 | # define stat _stat
|
|---|
| 39 | # define REPLACE_FUNC_STAT_DIR 1
|
|---|
| 40 | # undef REPLACE_FUNC_STAT_FILE
|
|---|
| 41 | # endif
|
|---|
| 42 | #endif
|
|---|
| 43 |
|
|---|
| 44 | static inline int
|
|---|
| 45 | orig_stat (const char *filename, struct stat *buf)
|
|---|
| 46 | {
|
|---|
| 47 | return stat (filename, buf);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /* Specification. */
|
|---|
| 51 | /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
|
|---|
| 52 | eliminates this include because of the preliminary #include <sys/stat.h>
|
|---|
| 53 | above. */
|
|---|
| 54 | #include "sys/stat.h"
|
|---|
| 55 |
|
|---|
| 56 | #include <errno.h>
|
|---|
| 57 | #include <limits.h>
|
|---|
| 58 | #include <stdbool.h>
|
|---|
| 59 | #include <string.h>
|
|---|
| 60 | #include "dosname.h"
|
|---|
| 61 | #include "verify.h"
|
|---|
| 62 |
|
|---|
| 63 | #if REPLACE_FUNC_STAT_DIR
|
|---|
| 64 | # include "pathmax.h"
|
|---|
| 65 | /* The only known systems where REPLACE_FUNC_STAT_DIR is needed also
|
|---|
| 66 | have a constant PATH_MAX. */
|
|---|
| 67 | # ifndef PATH_MAX
|
|---|
| 68 | # error "Please port this replacement to your platform"
|
|---|
| 69 | # endif
|
|---|
| 70 | #endif
|
|---|
| 71 |
|
|---|
| 72 | /* Store information about NAME into ST. Work around bugs with
|
|---|
| 73 | trailing slashes. Mingw has other bugs (such as st_ino always
|
|---|
| 74 | being 0 on success) which this wrapper does not work around. But
|
|---|
| 75 | at least this implementation provides the ability to emulate fchdir
|
|---|
| 76 | correctly. */
|
|---|
| 77 |
|
|---|
| 78 | int
|
|---|
| 79 | rpl_stat (char const *name, struct stat *st)
|
|---|
| 80 | {
|
|---|
| 81 | int result = orig_stat (name, st);
|
|---|
| 82 | #if REPLACE_FUNC_STAT_FILE
|
|---|
| 83 | /* Solaris 9 mistakenly succeeds when given a non-directory with a
|
|---|
| 84 | trailing slash. */
|
|---|
| 85 | if (result == 0 && !S_ISDIR (st->st_mode))
|
|---|
| 86 | {
|
|---|
| 87 | size_t len = strlen (name);
|
|---|
| 88 | if (ISSLASH (name[len - 1]))
|
|---|
| 89 | {
|
|---|
| 90 | errno = ENOTDIR;
|
|---|
| 91 | return -1;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | #endif /* REPLACE_FUNC_STAT_FILE */
|
|---|
| 95 | #if REPLACE_FUNC_STAT_DIR
|
|---|
| 96 |
|
|---|
| 97 | if (result == -1 && errno == ENOENT)
|
|---|
| 98 | {
|
|---|
| 99 | /* Due to mingw's oddities, there are some directories (like
|
|---|
| 100 | c:\) where stat() only succeeds with a trailing slash, and
|
|---|
| 101 | other directories (like c:\windows) where stat() only
|
|---|
| 102 | succeeds without a trailing slash. But we want the two to be
|
|---|
| 103 | synonymous, since chdir() manages either style. Likewise, Mingw also
|
|---|
| 104 | reports ENOENT for names longer than PATH_MAX, when we want
|
|---|
| 105 | ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
|
|---|
| 106 | Fortunately, mingw PATH_MAX is small enough for stack
|
|---|
| 107 | allocation. */
|
|---|
| 108 | char fixed_name[PATH_MAX + 1] = {0};
|
|---|
| 109 | size_t len = strlen (name);
|
|---|
| 110 | bool check_dir = false;
|
|---|
| 111 | verify (PATH_MAX <= 4096);
|
|---|
| 112 | if (PATH_MAX <= len)
|
|---|
| 113 | errno = ENAMETOOLONG;
|
|---|
| 114 | else if (len)
|
|---|
| 115 | {
|
|---|
| 116 | strcpy (fixed_name, name);
|
|---|
| 117 | if (ISSLASH (fixed_name[len - 1]))
|
|---|
| 118 | {
|
|---|
| 119 | check_dir = true;
|
|---|
| 120 | while (len && ISSLASH (fixed_name[len - 1]))
|
|---|
| 121 | fixed_name[--len] = '\0';
|
|---|
| 122 | if (!len)
|
|---|
| 123 | fixed_name[0] = '/';
|
|---|
| 124 | }
|
|---|
| 125 | else
|
|---|
| 126 | fixed_name[len++] = '/';
|
|---|
| 127 | result = orig_stat (fixed_name, st);
|
|---|
| 128 | if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
|
|---|
| 129 | {
|
|---|
| 130 | result = -1;
|
|---|
| 131 | errno = ENOTDIR;
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | #endif /* REPLACE_FUNC_STAT_DIR */
|
|---|
| 136 | return result;
|
|---|
| 137 | }
|
|---|