commit 78c49155dd0a40045b368134a3c7119642998d4e
parent 83429e3280e0bea08037ff7ad359c4949dcc7980
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date: Fri, 25 Sep 2020 13:24:11 +1000
bspatch: move bzip offset opening to one function
Diffstat:
M | bspatch.c | | | 39 | +++++++++++++++++++-------------------- |
1 file changed, 19 insertions(+), 20 deletions(-)
diff --git a/bspatch.c b/bspatch.c
@@ -47,6 +47,22 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:
#include "bsdiff.h"
+static BZFILE *
+bz_openat(const char *path, FILE **f, off_t o)
+{
+ int bzerr;
+ BZFILE *bzf;
+
+ if ((*f = fopen(path, "r")) == NULL)
+ err(1, "fopen(%s)", path);
+ if (fseeko(*f, o, SEEK_SET) == -1)
+ err(1, "fseeko(%s, %lld)", path, (long long)o);
+ if ((bzf = BZ2_bzReadOpen(&bzerr, *f, 0, 0, NULL, 0)) == NULL)
+ errx(1, "BZ2_bzReadOpen returned %d", bzerr);
+
+ return bzf;
+}
+
int
main(int argc, char **argv)
{
@@ -125,26 +141,9 @@ main(int argc, char **argv)
/* Close patch file and re-open it via libbzip2 at the right places */
if (fclose(f))
err(1, "fclose(%s)", argv[3]);
- if ((cpf = fopen(argv[3], "r")) == NULL)
- err(1, "fopen(%s)", argv[3]);
- if (fseeko(cpf, 32, SEEK_SET))
- err(1, "fseeko(%s, %lld)", argv[3], (long long)32);
- if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
- errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
- if ((dpf = fopen(argv[3], "r")) == NULL)
- err(1, "fopen(%s)", argv[3]);
- if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
- err(1, "fseeko(%s, %lld)", argv[3],
- (long long)(32 + bzctrllen));
- if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
- errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
- if ((epf = fopen(argv[3], "r")) == NULL)
- err(1, "fopen(%s)", argv[3]);
- if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
- err(1, "fseeko(%s, %lld)", argv[3],
- (long long)(32 + bzctrllen + bzdatalen));
- if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
- errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
+ cpfbz2 = bz_openat(argv[3], &cpf, 32);
+ dpfbz2 = bz_openat(argv[3], &dpf, 32 + bzctrllen);
+ epfbz2 = bz_openat(argv[3], &epf, 32 + bzctrllen + bzdatalen);
old = readfile(argv[1], &oldsize, &st);
if (fchmod(fileno(newfile), st.st_mode) == -1)