bsdiff-portable

A more portable version of Colin Percival's bsdiff
git clone git://git.sgregoratto.me/bsdiff-portable
Log | Files | Refs | LICENSE

commit 0fd2652cab5bb7ae8ca7c06130d33b4300953a18
parent de5f7871408d58f48776802dfb9aac0de69052ad
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Wed, 23 Sep 2020 22:21:32 +1000

bspatch: set newfile mode to oldfile mode

Diffstat:
Mbsdiff.c | 6+++---
Mbsdiff.h | 3+--
Mbspatch.c | 17+++++++++++------
Mutil.c | 11+++++++----
4 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/bsdiff.c b/bsdiff.c @@ -32,7 +32,7 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 #endif #include "config.h" -#include <sys/types.h> +#include <sys/stat.h> #include <bzlib.h> #if HAVE_ERR @@ -83,7 +83,7 @@ main(int argc, char **argv) if (pledge("stdio rpath wpath cpath", NULL) == -1) err(1, "pledge"); #endif - old = readfile(argv[1], &oldsize); + old = readfile(argv[1], &oldsize, &(struct stat){0}); if ((I = reallocarray(NULL, oldsize + 1, sizeof(off_t))) == NULL) err(1, "reallocarray"); @@ -94,7 +94,7 @@ main(int argc, char **argv) free(V); - new = readfile(argv[2], &newsize); + new = readfile(argv[2], &newsize, &(struct stat){0}); #if HAVE_PLEDGE && HAVE_UNVEIL if (pledge("stdio wpath cpath", NULL) == -1) err(1, "pledge"); diff --git a/bsdiff.h b/bsdiff.h @@ -42,7 +42,6 @@ off_t search(off_t *, uint8_t *, off_t, uint8_t *, off_t, off_t, off_t, off_t *); void offtout(off_t, uint8_t *); off_t offtin(uint8_t *buf); -uint8_t * readfile(char *, off_t *); +uint8_t * readfile(char *, off_t *, struct stat *); DEAD void usage(void); -uint8_t * readfile(char *, off_t *); #endif /* BSDIFF_H */ diff --git a/bspatch.c b/bspatch.c @@ -31,6 +31,8 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $"); #endif +#include <sys/stat.h> + #include "config.h" #include <bzlib.h> #if HAVE_ERR @@ -51,6 +53,7 @@ main(int argc, char **argv) BZFILE *cpfbz2, *dpfbz2, *epfbz2; int cbz2err, dbz2err, ebz2err; int fd; + struct stat st; off_t oldsize, newsize; off_t bzctrllen, bzdatalen; uint8_t header[32], buf[8]; @@ -64,7 +67,7 @@ main(int argc, char **argv) usage(); #if HAVE_PLEDGE && HAVE_UNVEIL - if (pledge("stdio rpath wpath cpath unveil", NULL) == -1) + if (pledge("stdio rpath wpath cpath fattr unveil", NULL) == -1) err(1, "pledge"); if (unveil(argv[1], "r") == -1) err(1, "unveil(%s, r)", argv[1]); @@ -72,7 +75,7 @@ main(int argc, char **argv) err(1, "unveil(%s, wc)", argv[2]); if (unveil(argv[3], "r") == -1) err(1, "unveil(%s, r)", argv[3]); - if (pledge("stdio rpath wpath cpath", NULL) == -1) + if (pledge("stdio rpath wpath cpath fattr", NULL) == -1) err(1, "pledge"); #endif /* Open patch file */ @@ -82,7 +85,7 @@ main(int argc, char **argv) if ((fd = open(argv[2], O_CREAT|O_TRUNC|O_WRONLY, 0666)) == -1) err(1, "open(%s)", argv[2]); #if HAVE_PLEDGE && HAVE_UNVEIL - if (pledge("stdio rpath", NULL) == -1) + if (pledge("stdio rpath fattr", NULL) == -1) err(1, "pledge"); #endif @@ -142,13 +145,15 @@ main(int argc, char **argv) if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err); - old = readfile(argv[1], &oldsize); - if ((new = reallocarray(NULL, newsize + 1, 1)) == NULL) - err(1, "reallocarray"); + old = readfile(argv[1], &oldsize, &st); + if (fchmod(fd, st.st_mode) == -1) + err(1, "fchmod(%s, %04o)", argv[2], st.st_mode); #if HAVE_PLEDGE && HAVE_UNVEIL if (pledge("stdio", NULL) == -1) err(1, "pledge"); #endif + if ((new = reallocarray(NULL, newsize + 1, 1)) == NULL) + err(1, "reallocarray"); oldpos = newpos = 0; while (newpos < newsize) { diff --git a/util.c b/util.c @@ -254,21 +254,24 @@ usage(void) } uint8_t * -readfile(char *path, off_t *size) +readfile(char *path, off_t *size, struct stat *st) { uint8_t *buf = NULL; int fd; - struct stat st; + + if ((path == NULL || *path == '\0') || + size == NULL || st == NULL) + errx(1, "readfile: bad args"); if ((fd = open(path, O_RDONLY, 0)) == -1) err(1, "%s: open", path); - if (fstat(fd, &st) == -1) + if (fstat(fd, st) == -1) err(1, "%s: fstat", path); /* * Allocate size + 1 bytes to ensure that we never * try to malloc(0) and get a NULL pointer. */ - *size = st.st_size; + *size = st->st_size; if ((buf = reallocarray(NULL, *size + 1, sizeof(off_t))) == NULL) err(1, "%s: reallocarray", path);