| 1 |
/* Work around a bug of lstat on some systems |
| 2 |
|
| 3 |
Copyright (C) 1997-2006, 2008-2010 Free Software Foundation, Inc. |
| 4 |
Copyright (c) 2013, 2015 Piotr Roszatycki <[email protected]> |
| 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 Jim Meyering */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
#if defined(HAVE_LSTAT) && !defined(LSTAT_FOLLOWS_SLASHED_SYMLINK) |
| 24 |
|
| 25 |
/* Get the original definition of lstat. It might be defined as a macro. */ |
| 26 |
# define __need_system_sys_stat_h |
| 27 |
# define _BSD_SOURCE 1 |
| 28 |
# include <sys/types.h> |
| 29 |
# include <sys/stat.h> |
| 30 |
# undef __need_system_sys_stat_h |
| 31 |
|
| 32 |
static inline int |
| 33 |
orig_lstat (const char *filename, struct stat *buf) |
| 34 |
{ |
| 35 |
return lstat (filename, buf); |
| 36 |
} |
| 37 |
|
| 38 |
/* Specification. */ |
| 39 |
# include <sys/stat.h> |
| 40 |
|
| 41 |
# include <string.h> |
| 42 |
# include <errno.h> |
| 43 |
# include "libfakechroot.h" |
| 44 |
|
| 45 |
/* lstat works differently on Linux and Solaris systems. POSIX (see |
| 46 |
`pathname resolution' in the glossary) requires that programs like |
| 47 |
`ls' take into consideration the fact that FILE has a trailing slash |
| 48 |
when FILE is a symbolic link. On Linux and Solaris 10 systems, the |
| 49 |
lstat function already has the desired semantics (in treating |
| 50 |
`lstat ("symlink/", sbuf)' just like `lstat ("symlink/.", sbuf)', |
| 51 |
but on Solaris 9 and earlier it does not. |
| 52 |
|
| 53 |
If FILE has a trailing slash and specifies a symbolic link, |
| 54 |
then use stat() to get more info on the referent of FILE. |
| 55 |
If the referent is a non-directory, then set errno to ENOTDIR |
| 56 |
and return -1. Otherwise, return stat's result. */ |
| 57 |
|
| 58 |
LOCAL int |
| 59 |
rpl_lstat (const char *file, struct stat *sbuf) |
| 60 |
{ |
| 61 |
size_t len; |
| 62 |
int lstat_result = orig_lstat (file, sbuf); |
| 63 |
|
| 64 |
debug("rpl_lstat(\"%s\", &sbuf)", file); |
| 65 |
|
| 66 |
if (lstat_result != 0) |
| 67 |
return lstat_result; |
| 68 |
|
| 69 |
/* This replacement file can blindly check against '/' rather than |
| 70 |
using the ISSLASH macro, because all platforms with '\\' either |
| 71 |
lack symlinks (mingw) or have working lstat (cygwin) and thus do |
| 72 |
not compile this file. 0 len should have already been filtered |
| 73 |
out above, with a failure return of ENOENT. */ |
| 74 |
len = strlen (file); |
| 75 |
if (file[len - 1] != '/' || S_ISDIR (sbuf->st_mode)) |
| 76 |
return 0; |
| 77 |
|
| 78 |
/* At this point, a trailing slash is only permitted on |
| 79 |
symlink-to-dir; but it should have found information on the |
| 80 |
directory, not the symlink. Call stat() to get info about the |
| 81 |
link's referent. Our replacement stat guarantees valid results, |
| 82 |
even if the symlink is not pointing to a directory. */ |
| 83 |
if (!S_ISLNK (sbuf->st_mode)) |
| 84 |
{ |
| 85 |
errno = ENOTDIR; |
| 86 |
return -1; |
| 87 |
} |
| 88 |
return stat (file, sbuf); |
| 89 |
} |
| 90 |
|
| 91 |
#else |
| 92 |
typedef int empty_translation_unit; |
| 93 |
#endif /* HAVE_LSTAT */ |