install: Fix two bugs in stdin code

* Fix case where the source is - and the target exists.

* Only call chflags() (to remove flags that might prevent us from
  replacing an existing target) in the exists case; otherwise,
  to_sb.st_flags is uninitialized.

* Rename the source file in the stdin test case.

* Extend null and stdin test cases to cover the case where the
  target already exists.

Approved by:	re (cperciva)
PR:		297681
MFC after:	1 week
Fixes:		d34870708d ("install: Allow installing stdin")
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D59144

(cherry picked from commit a5ff4125cf)
(cherry picked from commit 203438d0a3)
This commit is contained in:
Dag-Erling Smørgrav authored and Colin Percival committed 2026-08-27 16:27:54 -07:00
1 parent 9c7f4633c5
commit 5805e0ec88
2 files changed
+20 -10

No files matched your search

+17 -7
View File
@@ -567,6 +567,11 @@ null_body() {
atf_check install /dev/null dst/file
atf_check test -f dst/file
atf_check test ! -s dst/file
# what if target already exists?
echo "The Magic Words are Squeamish Ossifrage" >dst/file
atf_check test -s dst/file
atf_check install /dev/null dst/file
atf_check test ! -s dst/file
}
atf_test_case stdin
@@ -575,16 +580,21 @@ stdin_head() {
}
stdin_body() {
atf_check mkdir dst
echo "The Magic Words are Squeamish Ossifrage" >file
atf_check -s exit:71 -e not-empty install - dst <file
echo "The Magic Words are Squeamish Ossifrage" >src
atf_check -s exit:71 -e not-empty install - dst <src
atf_check test ! -e dst/file
atf_check install - dst/file <file
atf_check cmp -s file dst/file
atf_check install - dst/file <src
atf_check cmp -s dst/file src
atf_check rm dst/file
atf_check -s exit:71 -e not-empty install /dev/stdin dst <file
atf_check -s exit:71 -e not-empty install /dev/stdin dst <src
atf_check test ! -e dst/file
atf_check install /dev/stdin dst/file <file
atf_check cmp -s file dst/file
atf_check install /dev/stdin dst/file <src
atf_check cmp -s dst/file src
# what if target already exists?
atf_check install - dst/file </dev/null
atf_check test ! -s dst/file
atf_check install /dev/stdin dst/file <src
atf_check cmp -s dst/file src
}
atf_init_test_cases() {
+3 -3
View File
@@ -421,7 +421,7 @@ main(int argc, char *argv[])
usage();
}
if (!no_target && linkmode == 0) {
if (!no_target && linkmode == 0 && strcmp(*argv, "-") != 0) {
if (stat(*argv, &from_sb))
err(EX_OSERR, "%s", *argv);
if (!S_ISREG(to_sb.st_mode))
@@ -893,7 +893,7 @@ install(const char *from_name, const char *to_name, u_long fset, u_int flags)
if ((to_fd = open(to_name, O_RDONLY)) < 0)
err(EX_OSERR, "%s", to_name);
if (devnull)
files_match = to_sb.st_size == 0;
files_match = (to_sb.st_size == 0);
else if (ispipe)
files_match = false;
else {
@@ -983,7 +983,7 @@ install(const char *from_name, const char *to_name, u_long fset, u_int flags)
if (!files_match) {
#if HAVE_STRUCT_STAT_ST_FLAGS
/* Try to turn off the immutable bits. */
if (to_sb.st_flags & NOCHANGEBITS)
if (exists && (to_sb.st_flags & NOCHANGEBITS))
(void)chflags(to_name, to_sb.st_flags & ~NOCHANGEBITS);
#endif
if (exists && dobackup) {