diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2000-04-26 15:32:29 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2000-04-26 15:32:29 +0000 |
commit | a7455ec326f2226a8b8af4a7a2af0a1be449718b (patch) | |
tree | 4706ba458d5eff794aeb012673ba2d320e187d2e /usr.sbin/pkg_install | |
parent | 3406d8ba0503e30fd3bdcbe77357139547837e9d (diff) |
Don't ever let pkg_add destroy existing files.
Diffstat (limited to 'usr.sbin/pkg_install')
-rw-r--r-- | usr.sbin/pkg_install/add/extract.c | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/usr.sbin/pkg_install/add/extract.c b/usr.sbin/pkg_install/add/extract.c index 7cbbb96e829..676a275a6d7 100644 --- a/usr.sbin/pkg_install/add/extract.c +++ b/usr.sbin/pkg_install/add/extract.c @@ -1,7 +1,7 @@ -/* $OpenBSD: extract.c,v 1.9 2000/03/24 00:20:04 espie Exp $ */ +/* $OpenBSD: extract.c,v 1.10 2000/04/26 15:32:28 espie Exp $ */ #ifndef lint -static const char *rcsid = "$OpenBSD: extract.c,v 1.9 2000/03/24 00:20:04 espie Exp $"; +static const char *rcsid = "$OpenBSD: extract.c,v 1.10 2000/04/26 15:32:28 espie Exp $"; #endif /* @@ -77,6 +77,24 @@ rollback(char *name, char *home, plist_t *start, plist_t *stop) } } +static int +preserve(const char *fname) +{ + char copy[FILENAME_MAX]; + int i; + + for (i = 0; i < 50; i++) { + snprintf(copy, sizeof(copy), "%s-%d", fname, i); + if (fexists(copy)) + continue; + if (rename(fname, copy) == 0) { + warnx("conflict: renamed existing %s to %s", fname, copy); + return 0; + } + } + return -1; +} + void extract_plist(char *home, package_t *pkg) { @@ -84,7 +102,6 @@ extract_plist(char *home, package_t *pkg) char *last_file; char *where_args, *perm_args, *last_chdir; int maxargs, where_count = 0, perm_count = 0, add_count; - Boolean preserve; maxargs = sysconf(_SC_ARG_MAX) / 2; /* Just use half the argument space */ where_args = alloca(maxargs); @@ -102,7 +119,6 @@ extract_plist(char *home, package_t *pkg) perm_args[0] = 0; last_chdir = 0; - preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE; /* Reset the world */ Owner = NULL; @@ -137,19 +153,10 @@ extract_plist(char *home, package_t *pkg) /* first try to rename it into place */ snprintf(try, FILENAME_MAX, "%s/%s", Directory, p->name); if (fexists(try)) { - (void)chflags(try, 0); /* XXX hack - if truly immutable, rename fails */ - if (preserve && PkgName) { - char pf[FILENAME_MAX]; - - if (make_preserve_name(pf, FILENAME_MAX, PkgName, try)) { - if (rename(try, pf)) { - warnx( - "unable to back up %s to %s, aborting pkg_add", - try, pf); - rollback(PkgName, home, pkg->head, p); - return; - } - } + if (preserve(try) == -1) { + warnx("unable to back up %s, aborting pkg_add", try); + rollback(PkgName, home, pkg->head, p); + return; } } if (rename(p->name, try) == 0) { |