Skip to content

Commit f31f5cf

Browse files
committed
archive: avoid spawning gzip
As we already link to `zlib` library, we can perform the compression without even requiring gzip on the host machine We modify write_tar_filter_archive() function in archive-tar.c to handle the compression when `gzip -cn` is requested Signed-off-by: Rohit Ashiwal <[email protected]>
1 parent e00dd10 commit f31f5cf

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

archive-tar.c

+25-9
Original file line numberDiff line numberDiff line change
@@ -466,18 +466,34 @@ static int write_tar_filter_archive(const struct archiver *ar,
466466
filter.use_shell = 1;
467467
filter.in = -1;
468468

469-
if (start_command(&filter) < 0)
470-
die_errno(_("unable to start '%s' filter"), argv[0]);
471-
close(1);
472-
if (dup2(filter.in, 1) < 0)
473-
die_errno(_("unable to redirect descriptor"));
474-
close(filter.in);
469+
if (!strcmp("gzip -cn", ar->data)) {
470+
char outmode[4] = "wb\0";
471+
472+
if (args->compression_level >= 0 && args->compression_level <= 9)
473+
outmode[2] = '0' + args->compression_level;
474+
475+
gzip = gzdopen(fileno(stdout), outmode);
476+
if (!gzip)
477+
die(_("Could not gzdopen stdout"));
478+
} else {
479+
if (start_command(&filter) < 0)
480+
die_errno(_("unable to start '%s' filter"), argv[0]);
481+
close(1);
482+
if (dup2(filter.in, 1) < 0)
483+
die_errno(_("unable to redirect descriptor"));
484+
close(filter.in);
485+
}
475486

476487
r = write_tar_archive(ar, args);
477488

478-
close(1);
479-
if (finish_command(&filter) != 0)
480-
die(_("'%s' filter reported error"), argv[0]);
489+
if (gzip) {
490+
if (gzclose(gzip) != Z_OK)
491+
die(_("gzclose failed"));
492+
} else {
493+
close(1);
494+
if (finish_command(&filter) != 0)
495+
die(_("'%s' filter reported error"), argv[0]);
496+
}
481497

482498
strbuf_release(&cmd);
483499
return r;

0 commit comments

Comments
 (0)