=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/xinstall/xinstall.c,v retrieving revision 1.47 retrieving revision 1.48 diff -c -r1.47 -r1.48 *** src/usr.bin/xinstall/xinstall.c 2007/09/05 08:58:34 1.47 --- src/usr.bin/xinstall/xinstall.c 2009/03/27 07:31:27 1.48 *************** *** 1,4 **** ! /* $OpenBSD: xinstall.c,v 1.47 2007/09/05 08:58:34 jsg Exp $ */ /* $NetBSD: xinstall.c,v 1.9 1995/12/20 10:25:17 jonathan Exp $ */ /* --- 1,4 ---- ! /* $OpenBSD: xinstall.c,v 1.48 2009/03/27 07:31:27 phessler Exp $ */ /* $NetBSD: xinstall.c,v 1.9 1995/12/20 10:25:17 jonathan Exp $ */ /* *************** *** 40,46 **** #if 0 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93"; #endif ! static char rcsid[] = "$OpenBSD: xinstall.c,v 1.47 2007/09/05 08:58:34 jsg Exp $"; #endif /* not lint */ #include --- 40,46 ---- #if 0 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93"; #endif ! static char rcsid[] = "$OpenBSD: xinstall.c,v 1.48 2009/03/27 07:31:27 phessler Exp $"; #endif /* not lint */ #include *************** *** 330,394 **** files_match = 1; (void)unlink(tempfile); } - (void) close(temp_fd); } } /* - * Move the new file into place if doing a safe copy - * and the files are different (or just not compared). - */ - if (safecopy && !files_match) { - /* Try to turn off the immutable bits. */ - if (to_sb.st_flags & (NOCHANGEBITS)) - (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS)); - if (dobackup) { - char backup[MAXPATHLEN]; - (void)snprintf(backup, MAXPATHLEN, "%s%s", to_name, - suffix); - /* It is ok for the target file not to exist. */ - if (rename(to_name, backup) < 0 && errno != ENOENT) { - serrno = errno; - unlink(tempfile); - errx(EX_OSERR, "rename: %s to %s: %s", to_name, - backup, strerror(serrno)); - } - } - if (rename(tempfile, to_name) < 0 ) { - serrno = errno; - unlink(tempfile); - errx(EX_OSERR, "rename: %s to %s: %s", tempfile, - to_name, strerror(serrno)); - } - - /* Re-open to_fd so we aren't hosed by the rename(2). */ - (void) close(to_fd); - if ((to_fd = open(to_name, O_RDONLY, 0)) < 0) - err(EX_OSERR, "%s", to_name); - } - - /* * Preserve the timestamp of the source file if necessary. */ if (dopreserve && !files_match) { utb.actime = from_sb.st_atime; utb.modtime = from_sb.st_mtime; ! (void)utime(to_name, &utb); } /* * Set owner, group, mode for target; do the chown first, * chown may lose the setuid bits. */ ! if ((gid != (gid_t)-1 || uid != (uid_t)-1) && fchown(to_fd, uid, gid)) { serrno = errno; ! (void)unlink(to_name); ! errx(EX_OSERR, "%s: chown/chgrp: %s", to_name, strerror(serrno)); } if (fchmod(to_fd, mode)) { serrno = errno; ! (void)unlink(to_name); ! errx(EX_OSERR, "%s: chmod: %s", to_name, strerror(serrno)); } /* --- 330,365 ---- files_match = 1; (void)unlink(tempfile); } } + (void)close(to_fd); + to_fd = temp_fd; } /* * Preserve the timestamp of the source file if necessary. */ if (dopreserve && !files_match) { utb.actime = from_sb.st_atime; utb.modtime = from_sb.st_mtime; ! (void)utime(safecopy ? tempfile : to_name, &utb); } /* * Set owner, group, mode for target; do the chown first, * chown may lose the setuid bits. */ ! if ((gid != (gid_t)-1 || uid != (uid_t)-1) && ! fchown(to_fd, uid, gid)) { serrno = errno; ! (void)unlink(safecopy ? tempfile : to_name); ! errx(EX_OSERR, "%s: chown/chgrp: %s", ! safecopy ? tempfile : to_name, strerror(serrno)); } if (fchmod(to_fd, mode)) { serrno = errno; ! (void)unlink(safecopy ? tempfile : to_name); ! errx(EX_OSERR, "%s: chmod: %s", safecopy ? tempfile : to_name, ! strerror(serrno)); } /* *************** *** 398,409 **** 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", to_name, strerror(errno)); } (void)close(to_fd); if (!devnull) (void)close(from_fd); } /* --- 369,409 ---- 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", ! safecopy ? tempfile :to_name, strerror(errno)); } (void)close(to_fd); if (!devnull) (void)close(from_fd); + + /* + * Move the new file into place if doing a safe copy + * and the files are different (or just not compared). + */ + if (safecopy && !files_match) { + /* Try to turn off the immutable bits. */ + if (to_sb.st_flags & (NOCHANGEBITS)) + (void)chflags(to_name, to_sb.st_flags & ~(NOCHANGEBITS)); + if (dobackup) { + char backup[MAXPATHLEN]; + (void)snprintf(backup, MAXPATHLEN, "%s%s", to_name, + suffix); + /* It is ok for the target file not to exist. */ + if (rename(to_name, backup) < 0 && errno != ENOENT) { + serrno = errno; + unlink(tempfile); + errx(EX_OSERR, "rename: %s to %s: %s", to_name, + backup, strerror(serrno)); + } + } + if (rename(tempfile, to_name) < 0 ) { + serrno = errno; + unlink(tempfile); + errx(EX_OSERR, "rename: %s to %s: %s", tempfile, + to_name, strerror(serrno)); + } + } } /*