Import upstream version 1.26
[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-2011 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 #include "dosname.h"
43
44 /* Store information about NAME into ST.  Work around bugs with
45    trailing slashes.  Mingw has other bugs (such as st_ino always
46    being 0 on success) which this wrapper does not work around.  But
47    at least this implementation provides the ability to emulate fchdir
48    correctly.  */
49
50 int
51 rpl_stat (char const *name, struct stat *st)
52 {
53   int result = orig_stat (name, st);
54 #if REPLACE_FUNC_STAT_FILE
55   /* Solaris 9 mistakenly succeeds when given a non-directory with a
56      trailing slash.  */
57   if (result == 0 && !S_ISDIR (st->st_mode))
58     {
59       size_t len = strlen (name);
60       if (ISSLASH (name[len - 1]))
61         {
62           errno = ENOTDIR;
63           return -1;
64         }
65     }
66 #endif /* REPLACE_FUNC_STAT_FILE */
67 #if REPLACE_FUNC_STAT_DIR
68   if (result == -1 && errno == ENOENT)
69     {
70       /* Due to mingw's oddities, there are some directories (like
71          c:\) where stat() only succeeds with a trailing slash, and
72          other directories (like c:\windows) where stat() only
73          succeeds without a trailing slash.  But we want the two to be
74          synonymous, since chdir() manages either style.  Likewise, Mingw also
75          reports ENOENT for names longer than PATH_MAX, when we want
76          ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
77          Fortunately, mingw PATH_MAX is small enough for stack
78          allocation.  */
79       char fixed_name[PATH_MAX + 1] = {0};
80       size_t len = strlen (name);
81       bool check_dir = false;
82       if (PATH_MAX <= len)
83         errno = ENAMETOOLONG;
84       else if (len)
85         {
86           strcpy (fixed_name, name);
87           if (ISSLASH (fixed_name[len - 1]))
88             {
89               check_dir = true;
90               while (len && ISSLASH (fixed_name[len - 1]))
91                 fixed_name[--len] = '\0';
92               if (!len)
93                 fixed_name[0] = '/';
94             }
95           else
96             fixed_name[len++] = '/';
97           result = orig_stat (fixed_name, st);
98           if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
99             {
100               result = -1;
101               errno = ENOTDIR;
102             }
103         }
104     }
105 #endif /* REPLACE_FUNC_STAT_DIR */
106   return result;
107 }