diff options
author | Joel Sing <jsing@cvs.openbsd.org> | 2014-06-09 15:50:09 +0000 |
---|---|---|
committer | Joel Sing <jsing@cvs.openbsd.org> | 2014-06-09 15:50:09 +0000 |
commit | 33183ad5c4c5aa9e817cb101e560f4d2187f317b (patch) | |
tree | 5793dfbec1553b1da1e0001d4cf39ab9df236eb4 /usr.sbin/installboot | |
parent | 4817a413a2eaf27025bced348c456f45da6e294a (diff) |
Change the installboot file copying process so that it carefully
overwrites the existing file, before truncating it to the final length.
This means that we will keep the same inode and potentially retain the
same disk data block allocation between runs.
This will aid users who multiboot since it makes it less likely that the
PBR will change, although as before, there is no guarantee. Obviously if
the second stage boot loader grows or shrinks then the PBR will change
regardless.
Diffstat (limited to 'usr.sbin/installboot')
-rw-r--r-- | usr.sbin/installboot/i386_installboot.c | 5 | ||||
-rw-r--r-- | usr.sbin/installboot/util.c | 27 |
2 files changed, 13 insertions, 19 deletions
diff --git a/usr.sbin/installboot/i386_installboot.c b/usr.sbin/installboot/i386_installboot.c index fff16853700..ad1d65c4ff0 100644 --- a/usr.sbin/installboot/i386_installboot.c +++ b/usr.sbin/installboot/i386_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: i386_installboot.c,v 1.2 2014/04/27 13:41:50 krw Exp $ */ +/* $OpenBSD: i386_installboot.c,v 1.3 2014/06/09 15:50:08 jsing Exp $ */ /* $NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */ /* @@ -137,6 +137,9 @@ md_installboot(int devfd, char *dev) warnx("disklabel type unknown"); bootldr = fileprefix(root, bootldr); + if (verbose) + fprintf(stderr, "%s %s to %s\n", + (nowrite ? "would copy" : "copying"), stage2, bootldr); if (!nowrite) filecopy(stage2, bootldr); diff --git a/usr.sbin/installboot/util.c b/usr.sbin/installboot/util.c index faa44ff3e3f..a8fa8fa085f 100644 --- a/usr.sbin/installboot/util.c +++ b/usr.sbin/installboot/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.3 2014/01/19 03:48:07 jsing Exp $ */ +/* $OpenBSD: util.c,v 1.4 2014/06/09 15:50:08 jsing Exp $ */ /* * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> @@ -32,34 +32,29 @@ void filecopy(const char *srcfile, const char *dstfile) { - char *buf, tempfile[MAXPATHLEN]; struct stat sb; ssize_t sz, n; int sfd, dfd; + char *buf; if ((buf = malloc(BUFSIZE)) == NULL) err(1, "malloc"); sfd = open(srcfile, O_RDONLY); if (sfd == -1) - err(1, "open"); + err(1, "open %s", srcfile); if (fstat(sfd, &sb) == -1) err(1, "fstat"); sz = sb.st_size; - snprintf(tempfile, sizeof(tempfile), "%s.XXXXXXXX", dstfile); - dfd = mkstemp(tempfile); + dfd = open(dstfile, O_WRONLY|O_CREAT); if (dfd == -1) - err(1, "mkstemp"); - - if (chown(tempfile, 0, 0) == -1) + err(1, "open %s", dstfile); + if (fchown(dfd, 0, 0) == -1) err(1, "chown"); - if (chmod(tempfile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) + if (fchmod(dfd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) err(1, "chmod"); - if (verbose) - fprintf(stderr, "Copying %s to %s\n", srcfile, tempfile); - while (sz > 0) { n = MIN(sz, BUFSIZE); if ((n = read(sfd, buf, n)) == -1) @@ -69,15 +64,11 @@ filecopy(const char *srcfile, const char *dstfile) err(1, "write"); } + ftruncate(dfd, sb.st_size); + close(dfd); close(sfd); free(buf); - - if (verbose) - fprintf(stderr, "Renaming %s to %s\n", tempfile, dstfile); - - if (rename(tempfile, dstfile) == -1) - err(1, "rename"); } char * |