| 1 |
/* System dependent declarations. |
| 2 |
|
| 3 |
Copyright (C) 1988-1989, 1992-1995, 1998, 2001-2002, 2004, 2006, 2009-2013, |
| 4 |
2015-2025 Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This file is part of GNU DIFF. |
| 7 |
|
| 8 |
This program is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#define SYSTEM_INLINE _GL_EXTERN_INLINE |
| 22 |
#include "system.h" |
| 23 |
|
| 24 |
/* Do struct stat *S, *T describe the same file? */ |
| 25 |
bool |
| 26 |
same_file (struct stat const *s, struct stat const *t) |
| 27 |
{ |
| 28 |
if (! SAME_INODE (*s, *t)) |
| 29 |
{ |
| 30 |
# if HAVE_STRUCT_STAT_ST_RDEV |
| 31 |
/* Two character special files describe the same device if st_rdev |
| 32 |
is the same, and likewise for block special devices. |
| 33 |
They have the same contents, so treat them as the same. */ |
| 34 |
if (((S_ISCHR (s->st_mode) && S_ISCHR (t->st_mode)) |
| 35 |
|| (S_ISBLK (s->st_mode) && S_ISBLK (t->st_mode))) |
| 36 |
&& s->st_rdev == t->st_rdev) |
| 37 |
return true; |
| 38 |
# endif |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
/* Although POSIX says that two files are identical if st_ino and st_dev |
| 43 |
are the same, all too many file systems incorrectly assign the same |
| 44 |
(device, inode) pair to two distinct files, including: |
| 45 |
|
| 46 |
- GNU/Linux NFS servers that export all local file systems as a |
| 47 |
single NFS file system, if a local (device, inode) pair collides |
| 48 |
with another one after hashing. |
| 49 |
|
| 50 |
- GNU/Linux NFS servers that export Btrfs file systems with subvolumes, |
| 51 |
if the Btrfs (subvolume, inode) hashing function collides. |
| 52 |
See <https://lwn.net/Articles/866709/>. |
| 53 |
|
| 54 |
- Qemu virtio-fs before Qemu 5.2 (2020); see |
| 55 |
<https://bugzilla.redhat.com/show_bug.cgi?id=1795362>. |
| 56 |
|
| 57 |
- Network Appliance NFS servers in snapshot directories; see |
| 58 |
Network Appliance bug #195. |
| 59 |
|
| 60 |
- ClearCase MVFS; see bug id ATRia04618. |
| 61 |
|
| 62 |
Check whether two files that purport to be the same have the same |
| 63 |
attributes, to work around instances of this common bug. |
| 64 |
|
| 65 |
Birthtime is special as st_birthtime is not portable, but when |
| 66 |
either birthtime is available comparing them should be definitive. */ |
| 67 |
|
| 68 |
#if (defined HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC_TV_NSEC \ |
| 69 |
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIM_TV_NSEC \ |
| 70 |
|| defined HAVE_STRUCT_STAT_ST_BIRTHTIMENSEC \ |
| 71 |
|| (defined _WIN32 && ! defined __CYGWIN__)) |
| 72 |
/* If either file has a birth time, comparing them is definitive. */ |
| 73 |
struct timespec sbirth = get_stat_birthtime (s); |
| 74 |
struct timespec tbirth = get_stat_birthtime (t); |
| 75 |
if (0 <= sbirth.tv_nsec || 0 <= tbirth.tv_nsec) |
| 76 |
return timespec_cmp (sbirth, tbirth) == 0; |
| 77 |
#endif |
| 78 |
|
| 79 |
/* Fall back on comparing other easily-obtainable attributes. |
| 80 |
Do not inspect all attributes, only attributes useful in checking |
| 81 |
for the bug. Check attributes most likely to differ first. |
| 82 |
|
| 83 |
It's possible for two distinct files on a buggy file system to have |
| 84 |
the same attributes, but it's not worth slowing down all |
| 85 |
implementations (or complicating the configuration) to cater to |
| 86 |
these rare cases in buggy implementations. |
| 87 |
|
| 88 |
It's also possible for the same file to appear to be two different |
| 89 |
files, e.g., because its permissions were changed between the two |
| 90 |
stat calls. In that case cmp and diff will do extra work |
| 91 |
to determine that the file contents are the same. */ |
| 92 |
|
| 93 |
return (get_stat_ctime_ns (s) == get_stat_ctime_ns (t) |
| 94 |
&& get_stat_mtime_ns (s) == get_stat_mtime_ns (t) |
| 95 |
&& s->st_ctime == t->st_ctime |
| 96 |
&& s->st_mtime == t->st_mtime |
| 97 |
&& s->st_size == t->st_size |
| 98 |
&& s->st_mode == t->st_mode |
| 99 |
&& s->st_uid == t->st_uid |
| 100 |
&& s->st_gid == t->st_gid |
| 101 |
&& s->st_nlink == t->st_nlink); |
| 102 |
} |
| 103 |
|
| 104 |
/* Use this for code that could be used if diff ever cares about |
| 105 |
st_size for symlinks, which it doesn't now. */ |
| 106 |
#define care_about_symlink_size false |
| 107 |
|
| 108 |
/* Return the number of bytes in the file described by *S, |
| 109 |
or -1 if this cannot be determined reliably. */ |
| 110 |
off_t |
| 111 |
stat_size (struct stat const *s) |
| 112 |
{ |
| 113 |
mode_t mode = s->st_mode; |
| 114 |
off_t size = s->st_size; |
| 115 |
if (size < 0) |
| 116 |
return -1; |
| 117 |
if (! (S_ISREG (mode) || (care_about_symlink_size && S_ISLNK (mode)) |
| 118 |
|| S_TYPEISSHM (s) || S_TYPEISTMO (s))) |
| 119 |
return -1; |
| 120 |
|
| 121 |
#if (defined __linux__ || defined __CYGWIN__ || defined __FreeBSD__ \ |
| 122 |
|| defined __NetBSD__ || defined _AIX) |
| 123 |
/* On some systems, /proc files with size zero are suspect. */ |
| 124 |
if (size == 0) |
| 125 |
{ |
| 126 |
static dev_t proc_dev; |
| 127 |
if (!proc_dev) |
| 128 |
{ |
| 129 |
struct stat st; |
| 130 |
st.st_dev = 0; |
| 131 |
lstat ("/proc/self", &st); |
| 132 |
proc_dev = st.st_dev; |
| 133 |
} |
| 134 |
if (proc_dev && s->st_dev == proc_dev) |
| 135 |
return -1; |
| 136 |
} |
| 137 |
#endif |
| 138 |
#if care_about_symlink_size && (defined __linux__ || defined __ANDROID__) |
| 139 |
/* Symlinks have suspect sizes on Linux kernels before 5.15, |
| 140 |
due to bugs in fscrypt. */ |
| 141 |
if (S_ISLNK (mode)) |
| 142 |
{ |
| 143 |
static signed char symlink_size_ok; |
| 144 |
if (! symlink_size_ok) |
| 145 |
{ |
| 146 |
struct utsname name; |
| 147 |
uname (&name); |
| 148 |
char *p = name.release; |
| 149 |
symlink_size_ok = ((p[1] != '.' || '5' < p[0] |
| 150 |
|| (p[0] == '5' |
| 151 |
&& ('1' <= p[2] && p[2] <= '9') |
| 152 |
&& ('0' <= p[3] && p[3] <= '9') |
| 153 |
&& ('5' <= p[3] |
| 154 |
|| ('0' <= p[4] && p[4] <= '9')))) |
| 155 |
? 1 : -1); |
| 156 |
} |
| 157 |
if (symlink_size_ok < 0) |
| 158 |
return -1; |
| 159 |
} |
| 160 |
#endif |
| 161 |
|
| 162 |
return size; |
| 163 |
} |