From e8688a3d4520f1e62586c3f8115646116455d3e6 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 13 May 2019 01:35:16 -0500 Subject: Fix a couple error paths that don't continue archive create/extract properly. --- toys/posix/tar.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'toys/posix') diff --git a/toys/posix/tar.c b/toys/posix/tar.c index 656bbc58..8b481cc9 100644 --- a/toys/posix/tar.c +++ b/toys/posix/tar.c @@ -297,6 +297,16 @@ static int add_to_tar(struct dirtree *node) strncpy(hdr.gname, TT.group ? TT.group : gr->gr_name, sizeof(hdr.gname)); } + TT.sparselen = 0; + if (hdr.type == '0') { + // Before we write the header, make sure we can read the file + if ((fd = open(name, O_RDONLY)) < 0) { + perror_msg("can't open '%s'", name); + + return 0; + } + } + itoo(hdr.chksum, sizeof(hdr.chksum)-1, cksum(&hdr)); hdr.chksum[7] = ' '; @@ -305,12 +315,9 @@ static int add_to_tar(struct dirtree *node) // Write header and data to archive xwrite(TT.fd, &hdr, 512); if (hdr.type == '0') { - if ((fd = open(name, O_RDONLY)) < 0) perror_msg("can't open '%s'", name); - else { - xsendfile_pad(fd, TT.fd, st->st_size); - if (st->st_size%512) writeall(TT.fd, toybuf, (512-(st->st_size%512))); - close(fd); - } + xsendfile_pad(fd, TT.fd, st->st_size); + if (st->st_size%512) writeall(TT.fd, toybuf, (512-(st->st_size%512))); + close(fd); } done: free(name); @@ -379,7 +386,7 @@ static void sendfile_sparse(int fd) while (len) { // first/last 512 bytes used, rest left zeroes j = (len>3072) ? 3072 : len; - if (len != writeall(fd, toybuf+512, len)) goto error; + if (j != writeall(fd, toybuf+512, j)) goto error; len -= j; } } -- cgit v1.2.3