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