diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1999-01-26 04:09:36 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1999-01-26 04:09:36 +0000 |
commit | 42b8d70fdd128763ec3e86e876e993db0579eb5d (patch) | |
tree | c993ef1dd2849d68b3e4b3a3bc02da47b0e1b486 /usr.bin/xinstall | |
parent | e7484c47a1f534710e32bd0b2d3236341f88d4c6 (diff) |
Add a -b(ackup) flag to move any existing file(s) aside by renaming
them to file.old instead of simply deleting them. A different backup
suffix may be chosen with the -B option's argument. hubertf@netbsd.org
Diffstat (limited to 'usr.bin/xinstall')
-rw-r--r-- | usr.bin/xinstall/install.1 | 33 | ||||
-rw-r--r-- | usr.bin/xinstall/xinstall.c | 52 |
2 files changed, 67 insertions, 18 deletions
diff --git a/usr.bin/xinstall/install.1 b/usr.bin/xinstall/install.1 index 025e367d997..855135c8681 100644 --- a/usr.bin/xinstall/install.1 +++ b/usr.bin/xinstall/install.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: install.1,v 1.7 1998/11/11 23:01:51 aaron Exp $ +.\" $OpenBSD: install.1,v 1.8 1999/01/26 04:09:35 millert Exp $ .\" $NetBSD: install.1,v 1.4 1994/11/14 04:57:17 jtc Exp $ .\" .\" Copyright (c) 1987, 1990, 1993 @@ -42,14 +42,16 @@ .Nd install binaries .Sh SYNOPSIS .Nm install -.Op Fl CcpSs +.Op Fl bCcpSs +.Op Fl B Ar suffix .Op Fl f Ar flags .Op Fl g Ar group .Op Fl m Ar mode .Op Fl o Ar owner .Ar file1 file2 .Nm install -.Op Fl CcpSs +.Op Fl bCcpSs +.Op Fl B Ar suffix .Op Fl f Ar flags .Op Fl g Ar group .Op Fl m Ar mode @@ -73,9 +75,30 @@ If the destination is a directory, then the is moved into .Ar directory with its original filename. -If the target file already exists, it is overwritten if permissions -allow. +If the target file already exists, it is +either renamed to +.Ar file.old +if the +.Fl b +option is given +or overwritten +if permissions allow, An alternate backup suffix may be specified via the +.Fl B +option's argument. +.Pp .Bl -tag -width Ds +.It Fl b +Backup any existing files before overwriting them by renaming +them to +.Ar file.old . See +.Fl B +for specifying a different backup suffix. +.It Fl B Ar suffix +Use +.Ar suffix +as the backup suffix if +.Fl b +is given. .It Fl C Copy the file. If the target file already exists and the files are the same, diff --git a/usr.bin/xinstall/xinstall.c b/usr.bin/xinstall/xinstall.c index 192e05fc0a9..6efa1844cfe 100644 --- a/usr.bin/xinstall/xinstall.c +++ b/usr.bin/xinstall/xinstall.c @@ -1,4 +1,4 @@ -/* $OpenBSD: xinstall.c,v 1.18 1998/12/17 17:19:49 millert Exp $ */ +/* $OpenBSD: xinstall.c,v 1.19 1999/01/26 04:09:35 millert Exp $ */ /* $NetBSD: xinstall.c,v 1.9 1995/12/20 10:25:17 jonathan Exp $ */ /* @@ -44,7 +44,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93"; #endif -static char rcsid[] = "$OpenBSD: xinstall.c,v 1.18 1998/12/17 17:19:49 millert Exp $"; +static char rcsid[] = "$OpenBSD: xinstall.c,v 1.19 1999/01/26 04:09:35 millert Exp $"; #endif /* not lint */ #include <sys/param.h> @@ -68,18 +68,20 @@ static char rcsid[] = "$OpenBSD: xinstall.c,v 1.18 1998/12/17 17:19:49 millert E #include "pathnames.h" +#define DIRECTORY 0x01 /* Tell install it's a directory. */ +#define SETFLAGS 0x02 /* Tell install to set flags. */ +#define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND) +#define BACKUP_SUFFIX ".old" + struct passwd *pp; struct group *gp; -int docompare, dodir, dopreserve, dostrip, safecopy; +int dobackup, docompare, dodir, dopreserve, dostrip, safecopy; int mode = S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; char pathbuf[MAXPATHLEN], tempfile[MAXPATHLEN]; +char *suffix = BACKUP_SUFFIX; uid_t uid; gid_t gid; -#define DIRECTORY 0x01 /* Tell install it's a directory. */ -#define SETFLAGS 0x02 /* Tell install to set flags. */ -#define NOCHANGEBITS (UF_IMMUTABLE | UF_APPEND | SF_IMMUTABLE | SF_APPEND) - void copy __P((int, char *, int, char *, off_t, int)); int compare __P((int, const char *, size_t, int, const char *, size_t)); void install __P((char *, char *, u_long, u_int)); @@ -104,11 +106,17 @@ main(argc, argv) char *flags, *to_name, *group = NULL, *owner = NULL; iflags = 0; - while ((ch = getopt(argc, argv, "Ccdf:g:m:o:pSs")) != -1) + while ((ch = getopt(argc, argv, "BbCcdf:g:m:o:pSs")) != -1) switch((char)ch) { case 'C': docompare = 1; break; + case 'B': + suffix = optarg; + /* fall through; -B implies -b */ + case 'b': + dobackup = 1; + break; case 'c': /* For backwards compatibility. */ break; @@ -343,6 +351,17 @@ install(from_name, to_name, fset, flags) /* 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); + if (rename(to_name, backup) < 0) { + 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); @@ -424,8 +443,8 @@ copy(from_fd, from_name, to_fd, to_name, size, sparse) if (!sparse && size <= 8 * 1048576) { volatile size_t siz; - if ((p = mmap(NULL, (size_t)size, PROT_READ, - MAP_PRIVATE, from_fd, (off_t)0)) == (char *)-1) { + if ((p = mmap(NULL, (size_t)size, PROT_READ, MAP_PRIVATE, + from_fd, (off_t)0)) == (char *)-1) { serrno = errno; (void)unlink(to_name); errx(EX_OSERR, "%s: %s", from_name, strerror(serrno)); @@ -596,8 +615,8 @@ void usage() { (void)fprintf(stderr, "\ -usage: install [-CcpSs] [-f flags] [-g group] [-m mode] [-o owner] file1 file2\n\ - install [-CcpSs] [-f flags] [-g group] [-m mode] [-o owner] file1 ... fileN directory\n\ +usage: install [-bCcpSs] [-B suffix] [-f flags] [-g group] [-m mode] [-o owner] file1 file2\n\ + install [-bCcpSs] [-B suffix] [-f flags] [-g group] [-m mode] [-o owner] file1 ... fileN directory\n\ install -d [-g group] [-m mode] [-o owner] directory ...\n"); exit(EX_USAGE); /* NOTREACHED */ @@ -636,6 +655,8 @@ create_newfile(path, sbp) char *path; struct stat *sbp; { + char backup[MAXPATHLEN]; + /* * Unlink now... avoid ETXTBSY errors later. Try and turn * off the append/immutable bits -- if we fail, go ahead, @@ -644,7 +665,12 @@ create_newfile(path, sbp) if (sbp->st_flags & (NOCHANGEBITS)) (void)chflags(path, sbp->st_flags & ~(NOCHANGEBITS)); - (void)unlink(path); + if (dobackup) { + (void)snprintf(backup, MAXPATHLEN, "%s%s", path, suffix); + if (rename(path, backup) < 0) + err(EX_OSERR, "rename: %s to %s", path, backup); + } else + (void)unlink(path); return(open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)); } |