From 68f4c238caeb10b8ed597de7bf2e8e4764bbda33 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 4 Sep 2023 23:04:47 -0700 Subject: [PATCH] maint: prefer psame_inode, PSAME_INODE, STP_* 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. --- gl/lib/root-dev-ino.h | 2 +- src/cat.c | 4 ++-- src/chown-core.c | 4 ++-- src/copy.c | 22 +++++++++++----------- src/cp-hash.c | 2 +- src/du.c | 2 +- src/head.c | 4 ++-- src/ioblksize.h | 7 +++---- src/ln.c | 2 +- src/ls.c | 10 +++++----- src/od.c | 2 +- src/pwd.c | 4 ++-- src/shred.c | 8 ++++---- src/sort.c | 2 +- src/split.c | 4 ++-- src/stat.c | 6 +++--- src/tail.c | 2 +- src/truncate.c | 2 +- src/wc.c | 3 ++- 19 files changed, 46 insertions(+), 46 deletions(-) diff --git a/gl/lib/root-dev-ino.h b/gl/lib/root-dev-ino.h index dcc4968a0b..41ea4dc37c 100644 --- a/gl/lib/root-dev-ino.h +++ b/gl/lib/root-dev-ino.h @@ -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 \ diff --git a/src/cat.c b/src/cat.c index ac39a48637..9820b169b4 100644 --- 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); diff --git a/src/chown-core.c b/src/chown-core.c index fd718214f5..9669aaa252 100644 --- a/src/chown-core.c +++ b/src/chown-core.c @@ -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)) diff --git a/src/copy.c b/src/copy.c index 66ab006914..f2b84b4f80 100644 --- a/src/copy.c +++ b/src/copy.c @@ -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) diff --git a/src/cp-hash.c b/src/cp-hash.c index b4812745e1..355c03c3c5 100644 --- a/src/cp-hash.c +++ b/src/cp-hash.c @@ -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 diff --git a/src/du.c b/src/du.c index 93616510da..90df5cde04 100644 --- 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))); diff --git a/src/head.c b/src/head.c index da32c886f9..a61584f931 100644 --- a/src/head.c +++ b/src/head.c @@ -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 { diff --git a/src/ioblksize.h b/src/ioblksize.h index 59c16531fb..987629a743 100644 --- a/src/ioblksize.h +++ b/src/ioblksize.h @@ -73,10 +73,10 @@ */ 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) diff --git a/src/ln.c b/src/ln.c index 3b34feca4c..b6b70e2431 100644 --- 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))) diff --git a/src/ls.c b/src/ls.c index 900d993161..31375cc969 100644 --- 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); diff --git a/src/od.c b/src/od.c index 3ad25651ac..48ec3d4f0d 100644 --- 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; diff --git a/src/pwd.c b/src/pwd.c index 7b2e7c73a3..659dada0aa 100644 --- 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; } diff --git a/src/shred.c b/src/shred.c index f3d04b27a3..a7c684f19e 100644 --- a/src/shred.c +++ b/src/shred.c @@ -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. */ diff --git a/src/sort.c b/src/sort.c index e77984590c..abee57d7a0 100644 --- a/src/sort.c +++ b/src/sort.c @@ -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) diff --git a/src/split.c b/src/split.c index a32b2d93e0..d2cd232344 100644 --- a/src/split.c +++ b/src/split.c @@ -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; } diff --git a/src/stat.c b/src/stat.c index 33929b05b8..2705f6522d 100644 --- a/src/stat.c +++ b/src/stat.c @@ -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': { diff --git a/src/tail.c b/src/tail.c index f293551ea0..c2ca25664f 100644 --- a/src/tail.c +++ b/src/tail.c @@ -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); diff --git a/src/truncate.c b/src/truncate.c index 2fe51094d2..d0907e3023 100644 --- a/src/truncate.c +++ b/src/truncate.c @@ -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)) { diff --git a/src/wc.c b/src/wc.c index 41779f55db..6b802f5c7f 100644 --- 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; -- 2.47.3