diff options
author | Rob Landley <rob@landley.net> | 2019-05-13 01:35:16 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-05-13 01:35:16 -0500 |
commit | e8688a3d4520f1e62586c3f8115646116455d3e6 (patch) | |
tree | cc13ef3c4629979971f280721d53f352ce936e12 | |
parent | 01bb248ca03cecbee45f8bdc1ad562ea2dd87b27 (diff) | |
download | toybox-e8688a3d4520f1e62586c3f8115646116455d3e6.tar.gz |
Fix a couple error paths that don't continue archive create/extract properly.
-rw-r--r-- | toys/posix/tar.c | 21 |
1 files changed, 14 insertions, 7 deletions
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; } } |