]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer same-inode.h
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 3 Aug 2025 22:04:01 +0000 (15:04 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 4 Aug 2025 02:48:06 +0000 (19:48 -0700)
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.

src/cat.c
src/copy.c
src/tail.c
src/test.c

index 30a923ccec2ff1655840a92b08d573b307c94bc1..9b94ee12478d64380d0ba32001a5f54f5bd7d8b2 100644 (file)
--- 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)
index 147814a8c2c32a07a5ef631c2dc5d00181ee08ca..5a3d4e8364711743e1e5e7f10e505d8ee4a2f252 100644 (file)
 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))
         {
index 5579b2b71149baeb7927df0214f3d90a68beed99..9046c00f8d16e6975e6533638a3fff60f3d75b86 100644 (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));
index 583cf74c9c69e13b09081b053a7f4a5efcc6fc45..0c0785b9ca507fa4e81fdf16ede71f8a3eaa8abe 100644 (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: