]> git.cworth.org Git - tar/blob - gnu/stat.c
upstream: Fix extraction of device nodes.
[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, 2010 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 #include <config.h>
22
23 /* Get the original definition of stat.  It might be defined as a macro.  */
24 #define __need_system_sys_stat_h
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #undef __need_system_sys_stat_h
28
29 static inline int
30 orig_stat (const char *filename, struct stat *buf)
31 {
32   return stat (filename, buf);
33 }
34
35 /* Specification.  */
36 #include <sys/stat.h>
37
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdbool.h>
41 #include <string.h>
42
43 /* Store information about NAME into ST.  Work around bugs with
44    trailing slashes.  Mingw has other bugs (such as st_ino always
45    being 0 on success) which this wrapper does not work around.  But
46    at least this implementation provides the ability to emulate fchdir
47    correctly.  */
48
49 int
50 rpl_stat (char const *name, struct stat *st)
51 {
52   int result = orig_stat (name, st);
53 #if REPLACE_FUNC_STAT_FILE
54   /* Solaris 9 mistakenly succeeds when given a non-directory with a
55      trailing slash.  */
56   if (result == 0 && !S_ISDIR (st->st_mode))
57     {
58       size_t len = strlen (name);
59       if (ISSLASH (name[len - 1]))
60         {
61           errno = ENOTDIR;
62           return -1;
63         }
64     }
65 #endif /* REPLACE_FUNC_STAT_FILE */
66 #if REPLACE_FUNC_STAT_DIR
67   if (result == -1 && errno == ENOENT)
68     {
69       /* Due to mingw's oddities, there are some directories (like
70          c:\) where stat() only succeeds with a trailing slash, and
71          other directories (like c:\windows) where stat() only
72          succeeds without a trailing slash.  But we want the two to be
73          synonymous, since chdir() manages either style.  Likewise, Mingw also
74          reports ENOENT for names longer than PATH_MAX, when we want
75          ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
76          Fortunately, mingw PATH_MAX is small enough for stack
77          allocation.  */
78       char fixed_name[PATH_MAX + 1] = {0};
79       size_t len = strlen (name);
80       bool check_dir = false;
81       if (PATH_MAX <= len)
82         errno = ENAMETOOLONG;
83       else if (len)
84         {
85           strcpy (fixed_name, name);
86           if (ISSLASH (fixed_name[len - 1]))
87             {
88               check_dir = true;
89               while (len && ISSLASH (fixed_name[len - 1]))
90                 fixed_name[--len] = '\0';
91               if (!len)
92                 fixed_name[0] = '/';
93             }
94           else
95             fixed_name[len++] = '/';
96           result = orig_stat (fixed_name, st);
97           if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
98             {
99               result = -1;
100               errno = ENOTDIR;
101             }
102         }
103     }
104 #endif /* REPLACE_FUNC_STAT_DIR */
105   return result;
106 }