From 12d12f751fcceb9ba2770a0f5f097e7f60c99f14 Mon Sep 17 00:00:00 2001 From: espie Date: Thu, 14 Feb 2019 11:51:42 +0000 Subject: properly track file names and fds in case the installed file was already there, so that mode-changing operations still work. This fixes affected ports. work by tedu@ and I, okay tedu@ --- usr.bin/xinstall/xinstall.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/usr.bin/xinstall/xinstall.c b/usr.bin/xinstall/xinstall.c index dcd09c6902a..c4cec097387 100644 --- a/usr.bin/xinstall/xinstall.c +++ b/usr.bin/xinstall/xinstall.c @@ -1,4 +1,4 @@ -/* $OpenBSD: xinstall.c,v 1.70 2019/02/14 11:50:00 espie Exp $ */ +/* $OpenBSD: xinstall.c,v 1.71 2019/02/14 11:51:42 espie Exp $ */ /* $NetBSD: xinstall.c,v 1.9 1995/12/20 10:25:17 jonathan Exp $ */ /* @@ -222,6 +222,7 @@ install(char *from_name, char *to_name, u_long fset, u_int flags) struct timespec ts[2]; int devnull, from_fd, to_fd, serrno, files_match = 0; char *p; + char *target_name = tempfile; (void)memset((void *)&from_sb, 0, sizeof(from_sb)); (void)memset((void *)&to_sb, 0, sizeof(to_sb)); @@ -311,10 +312,14 @@ install(char *from_name, char *to_name, u_long fset, u_int flags) } else { files_match = 1; (void)unlink(tempfile); + target_name = to_name; + (void)close(temp_fd); } } - (void)close(to_fd); - to_fd = temp_fd; + if (!files_match) { + (void)close(to_fd); + to_fd = temp_fd; + } } /* @@ -333,13 +338,15 @@ install(char *from_name, char *to_name, u_long fset, u_int flags) if ((gid != (gid_t)-1 || uid != (uid_t)-1) && fchown(to_fd, uid, gid)) { serrno = errno; - (void)unlink(tempfile); - errx(1, "%s: chown/chgrp: %s", tempfile, strerror(serrno)); + if (target_name == tempfile) + (void)unlink(target_name); + errx(1, "%s: chown/chgrp: %s", target_name, strerror(serrno)); } if (fchmod(to_fd, mode)) { serrno = errno; - (void)unlink(tempfile); - errx(1, "%s: chmod: %s", tempfile, strerror(serrno)); + if (target_name == tempfile) + (void)unlink(target_name); + errx(1, "%s: chmod: %s", target_name, strerror(serrno)); } /* @@ -349,7 +356,7 @@ install(char *from_name, char *to_name, u_long fset, u_int flags) if (fchflags(to_fd, flags & SETFLAGS ? fset : from_sb.st_flags & ~UF_NODUMP)) { if (errno != EOPNOTSUPP || (from_sb.st_flags & ~UF_NODUMP) != 0) - warnx("%s: chflags: %s", tempfile, strerror(errno)); + warnx("%s: chflags: %s", target_name, strerror(errno)); } if (flags & USEFSYNC) -- cgit v1.2.3-59-g8ed1b