maint: prefer same-inode.h

This does not change behavior on POSIX platforms; it’s mostly to
make it clearer when we’re looking for file identity.
* src/cat.c (main):
* src/copy.c (struct dir_list, is_ancestor, copy_internal):
* src/tail.c (struct File_spec, record_open_fd, recheck)
(tail_forever_inotify, tail_file):
* src/test.c (binary_operator):
Use psame_inode, PSAME_INODE, or SAME_INODE instead of comparing
device and inode numbers by hand.
This commit is contained in:
Paul Eggert committed 2025-08-03 19:48:06 -07:00
1 parent 01e4d2983a
commit 5c822f4dac
4 files changed
+28 -22

No files matched your search

+8 -5
View File
@@ -645,14 +645,17 @@ main (int argc, char **argv)
idx_t outsize = io_blksize (&stat_buf);
/* Device, I-node number and lazily-acquired flags of the output. */
dev_t out_dev;
ino_t out_ino;
struct
{
dev_t st_dev;
ino_t st_ino;
} out_id;
int out_flags = -2;
bool have_out_dev = ! (S_TYPEISSHM (&stat_buf) || S_TYPEISTMO (&stat_buf));
if (have_out_dev)
{
out_dev = stat_buf.st_dev;
out_ino = stat_buf.st_ino;
out_id.st_dev = stat_buf.st_dev;
out_id.st_ino = stat_buf.st_ino;
}
/* True if the output is a regular file. */
@@ -714,7 +717,7 @@ main (int argc, char **argv)
if (! (S_ISFIFO (stat_buf.st_mode) || S_ISSOCK (stat_buf.st_mode)
|| S_TYPEISSHM (&stat_buf) || S_TYPEISTMO (&stat_buf))
&& have_out_dev
&& stat_buf.st_dev == out_dev && stat_buf.st_ino == out_ino)
&& SAME_INODE (stat_buf, out_id))
{
off_t in_pos = lseek (input_desc, 0, SEEK_CUR);
if (0 <= in_pos)
+5 -5
View File
@@ -109,8 +109,8 @@
struct dir_list
{
struct dir_list *parent;
ino_t ino;
dev_t dev;
ino_t st_ino;
dev_t st_dev;
};
/* Initial size of the cp.dest_info hash table. */
@@ -690,7 +690,7 @@ is_ancestor (const struct stat *sb, const struct dir_list *ancestors)
{
while (ancestors != 0)
{
if (ancestors->ino == sb->st_ino && ancestors->dev == sb->st_dev)
if (PSAME_INODE (ancestors, sb))
return true;
ancestors = ancestors->parent;
}
@@ -2903,8 +2903,8 @@ skip:
dir = alloca (sizeof *dir);
dir->parent = ancestors;
dir->ino = src_sb.st_ino;
dir->dev = src_sb.st_dev;
dir->st_ino = src_sb.st_ino;
dir->st_dev = src_sb.st_dev;
if (new_dst || !S_ISDIR (dst_sb.st_mode))
{
+8 -8
View File
@@ -125,8 +125,8 @@ struct File_spec
/* Attributes of the file the last time we checked. */
struct timespec mtime;
dev_t dev;
ino_t ino;
dev_t st_dev;
ino_t st_ino;
mode_t mode;
/* If a regular file, the file's read position the last time we
@@ -413,8 +413,8 @@ record_open_fd (struct File_spec *f, int fd,
{
f->fd = fd;
f->mtime = get_stat_mtime (st);
f->dev = st->st_dev;
f->ino = st->st_ino;
f->st_dev = st->st_dev;
f->st_ino = st->st_ino;
f->mode = st->st_mode;
if (S_ISREG (st->st_mode))
f->read_pos = (read_pos < 0
@@ -1061,7 +1061,7 @@ recheck (struct File_spec *f, bool blocking)
_("%s has appeared; following new file"),
quoteaf (f->prettyname));
}
else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
else if (!SAME_INODE (*f, new_stats))
{
/* File has been replaced (e.g., via log rotation) --
tail the new one. */
@@ -1583,7 +1583,7 @@ tail_forever_inotify (int wd, struct File_spec *f, int n_files,
struct stat stats;
if (! (stat (f[i].name, &stats) < 0
|| (f[i].dev == stats.st_dev && f[i].ino == stats.st_ino)))
|| SAME_INODE (f[i], stats)))
{
error (0, errno, _("%s was replaced"),
quoteaf (f[i].prettyname));
@@ -1988,8 +1988,8 @@ tail_file (struct File_spec *f, count_t n_files, count_t n_units)
f->fd = -1;
f->errnum = errno;
f->ignore = ! reopen_inaccessible_files;
f->ino = 0;
f->dev = 0;
f->st_dev = 0;
f->st_ino = 0;
}
error (0, errno, _("cannot open %s for reading"),
quoteaf (f->prettyname));
+7 -4
View File
@@ -287,7 +287,6 @@ static bool
binary_operator (bool l_is_l, enum binop bop)
{
int op;
struct stat stat_buf, stat_spare;
if (l_is_l)
advance (false);
@@ -339,9 +338,13 @@ binary_operator (bool l_is_l, enum binop bop)
case EF_BINOP:
if (l_is_l | r_is_l)
test_syntax_error (_("-ef does not accept -l"));
return (stat (argv[op - 1], &stat_buf) == 0
&& stat (argv[op + 1], &stat_spare) == 0
&& SAME_INODE (stat_buf, stat_spare));
else
{
struct stat st[2];
return (stat (argv[op - 1], &st[0]) == 0
&& stat (argv[op + 1], &st[1]) == 0
&& psame_inode (&st[0], &st[1]));
}
case EQ_STRING_BINOP:
case NE_STRING_BINOP: