From: Paul Eggert Date: Sun, 3 Aug 2025 22:04:01 +0000 (-0700) Subject: maint: prefer same-inode.h X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c822f4dacce3969b6edce5b03355386508aa887;p=thirdparty%2Fcoreutils.git 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. --- diff --git a/src/cat.c b/src/cat.c index 30a923ccec..9b94ee1247 100644 --- a/src/cat.c +++ b/src/cat.c @@ -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) diff --git a/src/copy.c b/src/copy.c index 147814a8c2..5a3d4e8364 100644 --- a/src/copy.c +++ b/src/copy.c @@ -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)) { diff --git a/src/tail.c b/src/tail.c index 5579b2b711..9046c00f8d 100644 --- a/src/tail.c +++ b/src/tail.c @@ -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)); diff --git a/src/test.c b/src/test.c index 583cf74c9c..0c0785b9ca 100644 --- a/src/test.c +++ b/src/test.c @@ -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: