]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer psame_inode, PSAME_INODE, STP_*
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 5 Sep 2023 06:04:47 +0000 (23:04 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 5 Sep 2023 06:12:02 +0000 (23:12 -0700)
Prefer psame_inode, PSAME_INODE, STP_NBLOCKS, and STP_BLKSIZE,
which take addresses of objects, to their counterparts that
take the whole objects.  In some cases the whole objects might
not be initialized, which would be undefined behavior strictly
speaking.
* gl/lib/root-dev-ino.h (ROOT_DEV_INO_CHECK):
* src/cp-hash.c (src_to_dest_compare):
* src/ls.c (dev_ino_compare):
* src/pwd.c (robust_getcwd):
Prefer PSAME_INODE to SAME_INODE.
* src/chown-core.c (restricted_chown):
* src/copy.c (copy_reg, same_file_ok, source_is_dst_backup)
(copy_internal):
* src/ln.c (do_link):
* src/pwd.c (logical_getcwd):
* src/sort.c (avoid_trashing_input):
* src/split.c (create):
* src/stat.c (find_bind_mount):
Prefer psame_inode to SAME_INODE.
* src/copy.c (infer_scantype):
* src/du.c (process_file):
* src/ls.c (gobble_file, print_long_format)
(print_file_name_and_frills, length_of_file_name_and_frills):
* src/stat.c (print_stat):
Prefer STP_NBLOCKS to ST_NBLOCKS.
* src/copy.c (copy_reg):
* src/head.c (elide_tail_bytes_file, elide_tail_lines_file):
* src/ioblksize.h (io_blksize):
* src/od.c (skip):
* src/shred.c (do_wipefd):
* src/stat.c (print_stat):
* src/tail.c (tail_bytes):
* src/truncate.c (do_ftruncate):
* src/wc.c (wc):
Prefer STP_BLKSIZE to ST_BLKSIZE.
* src/ioblksize.h (io_blksize):
Arg is now struct stat const *, not struct stat.  All callers changed.

19 files changed:
gl/lib/root-dev-ino.h
src/cat.c
src/chown-core.c
src/copy.c
src/cp-hash.c
src/du.c
src/head.c
src/ioblksize.h
src/ln.c
src/ls.c
src/od.c
src/pwd.c
src/shred.c
src/sort.c
src/split.c
src/stat.c
src/tail.c
src/truncate.c
src/wc.c

index dcc4968a0b9c8695403b48e4fe9494860f39955e..41ea4dc37c031b460dda37e2e91c8afb994c92ef 100644 (file)
@@ -28,7 +28,7 @@ get_root_dev_ino (struct dev_ino *root_d_i) _GL_ATTRIBUTE_NONNULL ();
    --preserve-root and --no-preserve-root options.  */
 
 # define ROOT_DEV_INO_CHECK(Root_dev_ino, Dir_statbuf) \
-    (Root_dev_ino && SAME_INODE (*Dir_statbuf, *Root_dev_ino))
+    (Root_dev_ino && PSAME_INODE (Dir_statbuf, Root_dev_ino))
 
 # define ROOT_DEV_INO_WARN(Dirname)                                    \
   do                                                                   \
index ac39a486374fafa2f000d659a84d318ed1b06ab1..9820b169b467f6d0cb29f649e5ce0765921c2c9f 100644 (file)
--- a/src/cat.c
+++ b/src/cat.c
@@ -644,7 +644,7 @@ main (int argc, char **argv)
     error (EXIT_FAILURE, errno, _("standard output"));
 
   /* Optimal size of i/o operations of output.  */
-  idx_t outsize = io_blksize (stat_buf);
+  idx_t outsize = io_blksize (&stat_buf);
 
   /* Device and I-node number of the output.  */
   dev_t out_dev = stat_buf.st_dev;
@@ -698,7 +698,7 @@ main (int argc, char **argv)
         }
 
       /* Optimal size of i/o operations of input.  */
-      idx_t insize = io_blksize (stat_buf);
+      idx_t insize = io_blksize (&stat_buf);
 
       fdadvise (input_desc, 0, 0, FADVISE_SEQUENTIAL);
 
index fd718214f516f5cafcf278a75bc21faa7624ea78..9669aaa25272cd5dc504cc0ce7678f2e4c77ab3d 100644 (file)
@@ -43,7 +43,7 @@ enum RCH_status
     /* required_uid and/or required_gid are specified, but don't match */
     RC_excluded,
 
-    /* SAME_INODE check failed */
+    /* The file was replaced by another file during the requested change.  */
     RC_inode_changed,
 
     /* open/fchown isn't needed, isn't safe, or doesn't work due to
@@ -255,7 +255,7 @@ restricted_chown (int cwd_fd, char const *file,
 
   if (fstat (fd, &st) != 0)
     status = RC_error;
-  else if (! SAME_INODE (*orig_st, st))
+  else if (! psame_inode (orig_st, &st))
     status = RC_inode_changed;
   else if ((required_uid == (uid_t) -1 || required_uid == st.st_uid)
            && (required_gid == (gid_t) -1 || required_gid == st.st_gid))
index 66ab006914f6bd36b653c81178a31c141e31e21c..f2b84b4f80734c8b82b3dce6ae0c06e85d38c82f 100644 (file)
@@ -1134,7 +1134,7 @@ infer_scantype (int fd, struct stat const *sb,
      suggests the file is sparse.  */
   if (! (HAVE_STRUCT_STAT_ST_BLOCKS
          && S_ISREG (sb->st_mode)
-         && ST_NBLOCKS (*sb) < sb->st_size / ST_NBLOCKSIZE))
+         && STP_NBLOCKS (sb) < sb->st_size / ST_NBLOCKSIZE))
     return PLAIN_SCANTYPE;
 
 #ifdef SEEK_HOLE
@@ -1265,7 +1265,7 @@ copy_reg (char const *src_name, char const *dst_name,
 
   /* Compare the source dev/ino from the open file to the incoming,
      saved ones obtained via a previous call to stat.  */
-  if (! SAME_INODE (*src_sb, src_open_sb))
+  if (! psame_inode (src_sb, &src_open_sb))
     {
       error (0, 0,
              _("skipping file %s, as it was replaced while being copied"),
@@ -1544,8 +1544,8 @@ copy_reg (char const *src_name, char const *dst_name,
   if (data_copy_required)
     {
       /* Choose a suitable buffer size; it may be adjusted later.  */
-      size_t buf_size = io_blksize (sb);
-      size_t hole_size = ST_BLKSIZE (sb);
+      size_t buf_size = io_blksize (&sb);
+      size_t hole_size = STP_BLKSIZE (&sb);
 
       /* Deal with sparse files.  */
       enum scantype scantype = infer_scantype (source_desc, &src_open_sb,
@@ -1573,7 +1573,7 @@ copy_reg (char const *src_name, char const *dst_name,
              Note we read in multiples of the reported block size
              to support (unusual) devices that have this constraint.  */
           size_t blcm_max = MIN (SIZE_MAX, SSIZE_MAX);
-          size_t blcm = buffer_lcm (io_blksize (src_open_sb), buf_size,
+          size_t blcm = buffer_lcm (io_blksize (&src_open_sb), buf_size,
                                     blcm_max);
 
           /* Do not bother with a buffer larger than the input file, plus one
@@ -1743,7 +1743,7 @@ same_file_ok (char const *src_name, struct stat const *src_sb,
   struct stat tmp_src_sb;
 
   bool same_link;
-  bool same = SAME_INODE (*src_sb, *dst_sb);
+  bool same = psame_inode (src_sb, dst_sb);
 
   *return_now = false;
 
@@ -1804,7 +1804,7 @@ same_file_ok (char const *src_name, struct stat const *src_sb,
       src_sb_link = &tmp_src_sb;
       dst_sb_link = &tmp_dst_sb;
 
-      same_link = SAME_INODE (*src_sb_link, *dst_sb_link);
+      same_link = psame_inode (src_sb_link, dst_sb_link);
 
       /* If both are symlinks, then it's ok, but only if the destination
          will be unlinked before being opened.  This is like the test
@@ -1892,7 +1892,7 @@ same_file_ok (char const *src_name, struct stat const *src_sb,
      hard links to the same file.  */
   if (!S_ISLNK (src_sb_link->st_mode) && !S_ISLNK (dst_sb_link->st_mode))
     {
-      if (!SAME_INODE (*src_sb_link, *dst_sb_link))
+      if (!psame_inode (src_sb_link, dst_sb_link))
         return true;
 
       /* If they are the same file, it's ok if we're making hard links.  */
@@ -1953,7 +1953,7 @@ same_file_ok (char const *src_name, struct stat const *src_sb,
       else if (fstatat (dst_dirfd, dst_relname, &tmp_dst_sb, 0) != 0)
         return true;
 
-      if ( ! SAME_INODE (tmp_src_sb, tmp_dst_sb))
+      if (!psame_inode (&tmp_src_sb, &tmp_dst_sb))
         return true;
 
       if (x->hard_link)
@@ -2172,7 +2172,7 @@ source_is_dst_backup (char const *srcbase, struct stat const *src_st,
   struct stat dst_back_sb;
   int dst_back_status = fstatat (dst_dirfd, dst_back, &dst_back_sb, 0);
   free (dst_back);
-  return dst_back_status == 0 && SAME_INODE (*src_st, dst_back_sb);
+  return dst_back_status == 0 && psame_inode (src_st, &dst_back_sb);
 }
 
 /* Copy the file SRC_NAME to the file DST_NAME aka DST_DIRFD+DST_RELNAME.
@@ -3041,7 +3041,7 @@ skip:
                             || stat (".", &dot_sb) != 0
                             || (fstatat (dst_dirfd, dst_parent, &dst_parent_sb,
                                          0) != 0)
-                            || SAME_INODE (dot_sb, dst_parent_sb));
+                            || psame_inode (&dot_sb, &dst_parent_sb));
           free (dst_parent);
 
           if (! in_current_dir)
index b4812745e14ea0fb481d196b5582460aa55f716c..355c03c3c588eab8d5b9a6d49355e7b002f6cafa 100644 (file)
@@ -65,7 +65,7 @@ src_to_dest_compare (void const *x, void const *y)
 {
   struct Src_to_dest const *a = x;
   struct Src_to_dest const *b = y;
-  return SAME_INODE (*a, *b) ? true : false;
+  return PSAME_INODE (a, b);
 }
 
 static void
index 93616510dabeccf92b17b759537d3a0f5667f4aa..90df5cde046a46a0c86c2cf995c4f374acb9ee8c 100644 (file)
--- a/src/du.c
+++ b/src/du.c
@@ -587,7 +587,7 @@ process_file (FTS *fts, FTSENT *ent)
   duinfo_set (&dui,
               (apparent_size
                ? (usable_st_size (sb) ? MAX (0, sb->st_size) : 0)
-               : (uintmax_t) ST_NBLOCKS (*sb) * ST_NBLOCKSIZE),
+               : (uintmax_t) STP_NBLOCKS (sb) * ST_NBLOCKSIZE),
               (time_type == time_mtime ? get_stat_mtime (sb)
                : time_type == time_atime ? get_stat_atime (sb)
                : get_stat_ctime (sb)));
index da32c886f944108a5f27cd3097660f9bf851f679..a61584f931da5d3505f29840ea22551014f3bbd6 100644 (file)
@@ -466,7 +466,7 @@ elide_tail_bytes_file (char const *filename, int fd, uintmax_t n_elide,
                        struct stat const *st, off_t current_pos)
 {
   off_t size = st->st_size;
-  if (presume_input_pipe || current_pos < 0 || size <= ST_BLKSIZE (*st))
+  if (presume_input_pipe || current_pos < 0 || size <= STP_BLKSIZE (st))
     return elide_tail_bytes_pipe (filename, fd, n_elide, current_pos);
   else
     {
@@ -756,7 +756,7 @@ elide_tail_lines_file (char const *filename, int fd, uintmax_t n_elide,
                        struct stat const *st, off_t current_pos)
 {
   off_t size = st->st_size;
-  if (presume_input_pipe || current_pos < 0 || size <= ST_BLKSIZE (*st))
+  if (presume_input_pipe || current_pos < 0 || size <= STP_BLKSIZE (st))
     return elide_tail_lines_pipe (filename, fd, n_elide, current_pos);
   else
     {
index 59c16531fb77fd6a80b2a374419e8b78cf037743..987629a7439b1174c76e6c2b67e7cccd87b7e0de 100644 (file)
  */
 enum { IO_BUFSIZE = 128 * 1024 };
 static inline idx_t
-io_blksize (struct stat sb)
+io_blksize (struct stat const *st)
 {
   /* Treat impossible blocksizes as if they were IO_BUFSIZE.  */
-  idx_t blocksize = ST_BLKSIZE (sb) <= 0 ? IO_BUFSIZE : ST_BLKSIZE (sb);
+  idx_t blocksize = STP_BLKSIZE (st) <= 0 ? IO_BUFSIZE : STP_BLKSIZE (st);
 
   /* Use a blocksize of at least IO_BUFSIZE bytes, keeping it a
      multiple of the original blocksize.  */
@@ -88,8 +88,7 @@ io_blksize (struct stat sb)
      This misinformation can cause coreutils to use wrong-sized blocks.
      Work around some of the performance bug by substituting the next
      power of two when the reported blocksize is not a power of two.  */
-  if (S_ISREG (sb.st_mode)
-      && blocksize & (blocksize - 1))
+  if (S_ISREG (st->st_mode) && blocksize & (blocksize - 1))
     {
       int leading_zeros = count_leading_zeros_ll (blocksize);
       if (IDX_MAX < ULLONG_MAX || leading_zeros)
index 3b34feca4c56b7519a83ebf2e1981ae8d78dc6af..b6b70e24315e4264d1cfa49d2890e28b25469559 100644 (file)
--- a/src/ln.c
+++ b/src/ln.c
@@ -259,7 +259,7 @@ do_link (char const *source, int destdir_fd, char const *dest_base,
                   if (source_status != 0)
                     source_status = stat (source, &source_stats);
                   if (source_status == 0
-                      && SAME_INODE (source_stats, dest_stats)
+                      && psame_inode (&source_stats, &dest_stats)
                       && (source_stats.st_nlink == 1
                           || same_nameat (AT_FDCWD, source,
                                           destdir_fd, dest_base)))
index 900d993161565ed9f000cd02a69e0df1c3ee1ec9..31375cc9697ef01149fac28f3844c06e6e661ba7 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -1403,7 +1403,7 @@ dev_ino_compare (void const *x, void const *y)
 {
   struct dev_ino const *a = x;
   struct dev_ino const *b = y;
-  return SAME_INODE (*a, *b) ? true : false;
+  return PSAME_INODE (a, b);
 }
 
 static void
@@ -3610,7 +3610,7 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
       else
         f->filetype = normal;
 
-      blocks = ST_NBLOCKS (f->stat);
+      blocks = STP_NBLOCKS (&f->stat);
       if (format == long_format || print_block_size)
         {
           char buf[LONGEST_HUMAN_READABLE + 1];
@@ -4378,7 +4378,7 @@ print_long_format (const struct fileinfo *f)
       char const *blocks =
         (! f->stat_ok
          ? "?"
-         : human_readable (ST_NBLOCKS (f->stat), hbuf, human_output_opts,
+         : human_readable (STP_NBLOCKS (&f->stat), hbuf, human_output_opts,
                            ST_NBLOCKSIZE, output_block_size));
       int blocks_width = mbswidth (blocks, MBSWIDTH_FLAGS);
       for (int pad = blocks_width < 0 ? 0 : block_size_width - blocks_width;
@@ -4894,7 +4894,7 @@ print_file_name_and_frills (const struct fileinfo *f, size_t start_col)
   if (print_block_size)
     printf ("%*s ", format == with_commas ? 0 : block_size_width,
             ! f->stat_ok ? "?"
-            : human_readable (ST_NBLOCKS (f->stat), buf, human_output_opts,
+            : human_readable (STP_NBLOCKS (&f->stat), buf, human_output_opts,
                               ST_NBLOCKSIZE, output_block_size));
 
   if (print_scontext)
@@ -5126,7 +5126,7 @@ length_of_file_name_and_frills (const struct fileinfo *f)
   if (print_block_size)
     len += 1 + (format == with_commas
                 ? strlen (! f->stat_ok ? "?"
-                          : human_readable (ST_NBLOCKS (f->stat), buf,
+                          : human_readable (STP_NBLOCKS (&f->stat), buf,
                                             human_output_opts, ST_NBLOCKSIZE,
                                             output_block_size))
                 : block_size_width);
index 3ad25651ac41b9e69a2a64154120027ee2d37594..48ec3d4f0d6e6389a0f9ec881022d2dea5f8c725 100644 (file)
--- a/src/od.c
+++ b/src/od.c
@@ -1031,7 +1031,7 @@ skip (uintmax_t n_skip)
              when st_size is no greater than the block size, because
              some kernels report nonsense small file sizes for
              proc-like file systems.  */
-          if (usable_size && ST_BLKSIZE (file_stats) < file_stats.st_size)
+          if (usable_size && STP_BLKSIZE (&file_stats) < file_stats.st_size)
             {
               if ((uintmax_t) file_stats.st_size < n_skip)
                 n_skip -= file_stats.st_size;
index 7b2e7c73a35f99dbf76b87f553bf4196b54aac5a..659dada0aa8a0b0e7c2e442fdcd72edb89035bb4 100644 (file)
--- a/src/pwd.c
+++ b/src/pwd.c
@@ -280,7 +280,7 @@ robust_getcwd (struct file_name *file_name)
   while (true)
     {
       /* If we've reached the root, we're done.  */
-      if (SAME_INODE (dot_sb, *root_dev_ino))
+      if (PSAME_INODE (&dot_sb, root_dev_ino))
         break;
 
       find_dir_entry (&dot_sb, file_name, height++);
@@ -315,7 +315,7 @@ logical_getcwd (void)
     }
 
   /* System call validation.  */
-  if (stat (wd, &st1) == 0 && stat (".", &st2) == 0 && SAME_INODE (st1, st2))
+  if (stat (wd, &st1) == 0 && stat (".", &st2) == 0 && psame_inode (&st1, &st2))
     return wd;
   return nullptr;
 }
index f3d04b27a3ceda3746c249b14eaf137b3d727fd4..a7c684f19e799f8d4dceea9fbbaa76cae977b36f 100644 (file)
@@ -862,12 +862,12 @@ do_wipefd (int fd, char const *qname, struct randint_source *s,
           if (! flags->exact)
             {
               /* Round up to the nearest block size to clear slack space.  */
-              off_t remainder = size % ST_BLKSIZE (st);
-              if (size && size < ST_BLKSIZE (st))
+              off_t remainder = size % STP_BLKSIZE (&st);
+              if (size && size < STP_BLKSIZE (&st))
                 i_size = size;
               if (remainder != 0)
                 {
-                  off_t size_incr = ST_BLKSIZE (st) - remainder;
+                  off_t size_incr = STP_BLKSIZE (&st) - remainder;
                   size += MIN (size_incr, OFF_T_MAX - size);
                 }
             }
@@ -887,7 +887,7 @@ do_wipefd (int fd, char const *qname, struct randint_source *s,
         }
     }
   else if (S_ISREG (st.st_mode)
-           && st.st_size < MIN (ST_BLKSIZE (st), size))
+           && st.st_size < MIN (STP_BLKSIZE (&st), size))
     i_size = st.st_size;
 
   /* Schedule the passes in random order. */
index e77984590c6de2360ecb4ff6f194ff7b0af86e8b..abee57d7a071271b00f3f4144409d102a62af072 100644 (file)
@@ -3806,7 +3806,7 @@ avoid_trashing_input (struct sortfile *files, size_t ntemps,
                     ? fstat (STDIN_FILENO, &instat)
                     : stat (files[i].name, &instat))
                    == 0)
-                  && SAME_INODE (instat, *outst));
+                  && psame_inode (&instat, outst));
         }
 
       if (same)
index a32b2d93e06eaa7581a40c93f5ca74d16c1026ea..d2cd2323448d52afc4d395bdf9190627af1aaa56 100644 (file)
@@ -486,7 +486,7 @@ create (char const *name)
       struct stat out_stat_buf;
       if (fstat (fd, &out_stat_buf) != 0)
         error (EXIT_FAILURE, errno, _("failed to stat %s"), quoteaf (name));
-      if (SAME_INODE (in_stat_buf, out_stat_buf))
+      if (psame_inode (&in_stat_buf, &out_stat_buf))
         error (EXIT_FAILURE, 0, _("%s would overwrite input; aborting"),
                quoteaf (name));
       bool regularish
@@ -1626,7 +1626,7 @@ main (int argc, char **argv)
 
   if (in_blk_size == 0)
     {
-      in_blk_size = io_blksize (in_stat_buf);
+      in_blk_size = io_blksize (&in_stat_buf);
       if (SYS_BUFSIZE_MAX < in_blk_size)
         in_blk_size = SYS_BUFSIZE_MAX;
     }
index 33929b05b8e6dcc1644537f2b5f7a6128c0e7f4e..2705f6522dcd7eebbe735b2cb9a4c94a963b7603 100644 (file)
@@ -972,7 +972,7 @@ find_bind_mount (char const * name)
           struct stat dev_stats;
 
           if (stat (me->me_devname, &dev_stats) == 0
-              && SAME_INODE (name_stats, dev_stats))
+              && psame_inode (&name_stats, &dev_stats))
             {
               bind_mount = me->me_devname;
               break;
@@ -1603,10 +1603,10 @@ print_stat (char *pformat, size_t prefix_len, char mod, char m,
       out_uint (pformat, prefix_len, ST_NBLOCKSIZE);
       break;
     case 'b':
-      out_uint (pformat, prefix_len, ST_NBLOCKS (*statbuf));
+      out_uint (pformat, prefix_len, STP_NBLOCKS (statbuf));
       break;
     case 'o':
-      out_uint (pformat, prefix_len, ST_BLKSIZE (*statbuf));
+      out_uint (pformat, prefix_len, STP_BLKSIZE (statbuf));
       break;
     case 'w':
       {
index f293551ea00e9b4143f296c5d5597ffd78c006dc..c2ca25664f38db34169a76fcc36f7e61f0e44727 100644 (file)
@@ -1841,7 +1841,7 @@ tail_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
           else if ((current_pos = lseek (fd, -n_bytes, SEEK_END)) != -1)
             end_pos = current_pos + n_bytes;
         }
-      if (end_pos <= (off_t) ST_BLKSIZE (stats))
+      if (end_pos <= (off_t) STP_BLKSIZE (&stats))
         return pipe_bytes (pretty_filename, fd, n_bytes, read_pos);
       if (current_pos == -1)
         current_pos = xlseek (fd, 0, SEEK_CUR, pretty_filename);
index 2fe51094d2cc36e9a28b17794c14e926f43d8b9b..d0907e3023914d1e191e5aeaf2d88f34815f1d71 100644 (file)
@@ -115,7 +115,7 @@ do_ftruncate (int fd, char const *fname, off_t ssize, off_t rsize,
     }
   if (block_mode)
     {
-      ptrdiff_t blksize = ST_BLKSIZE (sb);
+      ptrdiff_t blksize = STP_BLKSIZE (&sb);
       intmax_t ssize0 = ssize;
       if (ckd_mul (&ssize, ssize, blksize))
         {
index 41779f55dbaa109462267c0d7293237d555047f7..6b802f5c7fb6462e8d7205baede91e6126fc1be0 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -409,7 +409,8 @@ wc (int fd, char const *file_x, struct fstatus *fstatus, off_t current_pos)
             }
           else
             {
-              off_t hi_pos = end_pos - end_pos % (ST_BLKSIZE (fstatus->st) + 1);
+              off_t hi_pos = (end_pos
+                              - end_pos % (STP_BLKSIZE (&fstatus->st) + 1));
               if (0 <= current_pos && current_pos < hi_pos
                   && 0 <= lseek (fd, hi_pos, SEEK_CUR))
                 bytes = hi_pos - current_pos;