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