[RFC] tar --to-command
Ladislav Michl
Ladislav.Michl at seznam.cz
Mon Jun 21 14:19:43 UTC 2010
On Sat, Jun 19, 2010 at 08:41:13PM +0200, Denys Vlasenko wrote:
> On Friday 18 June 2010 11:11, Ladislav Michl wrote:
> + xsetenv("TAR_FILENAME", file_header->name);
> + xsetenv("TAR_SIZE", file_header->size);
>
> This might leak memory on parent side.
Detect NOMMU and unsetenv then?
Other comments addressed in v2 below
---
archival/libunarchive/data_extract_to_command.c | 63 ++++++++++++++++++++++++
archival/Config.src | 9 +++
archival/libunarchive/Kbuild.src | 1
archival/tar.c | 9 +++
include/unarchive.h | 4 +
5 files changed, 86 insertions(+)
diff --git a/archival/Config.src b/archival/Config.src
index 3dbd3ae..3cc88c4 100644
--- a/archival/Config.src
+++ b/archival/Config.src
@@ -280,6 +280,15 @@ config FEATURE_TAR_LONG_OPTIONS
help
Enable use of long options, increases size by about 400 Bytes
+config FEATURE_TAR_TO_COMMAND
+ bool "Support for writing to an external program"
+ default y
+ depends on TAR && FEATURE_TAR_LONG_OPTIONS
+ help
+ If you enable this option you'll be able to instruct tar to send
+ the contents of each extracted file to the standard input of an
+ external program.
+
config FEATURE_TAR_UNAME_GNAME
bool "Enable use of user and group names"
default y
diff --git a/archival/libunarchive/Kbuild.src b/archival/libunarchive/Kbuild.src
index 8185455..ef31b31 100644
--- a/archival/libunarchive/Kbuild.src
+++ b/archival/libunarchive/Kbuild.src
@@ -53,6 +53,7 @@ lib-$(CONFIG_FEATURE_SEAMLESS_BZ2) += open_transformer.o decompress_bunzip2
lib-$(CONFIG_FEATURE_SEAMLESS_LZMA) += open_transformer.o decompress_unlzma.o get_header_tar_lzma.o
lib-$(CONFIG_FEATURE_SEAMLESS_XZ) += open_transformer.o decompress_unxz.o
lib-$(CONFIG_FEATURE_COMPRESS_USAGE) += decompress_bunzip2.o
+lib-$(CONFIG_FEATURE_TAR_TO_COMMAND) += data_extract_to_command.o
ifneq ($(lib-y),)
lib-y += $(COMMON_FILES)
diff --git a/archival/tar.c b/archival/tar.c
index 3a94012..f7b0fe9 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -749,6 +749,7 @@ enum {
IF_FEATURE_SEAMLESS_GZ( OPTBIT_GZIP ,)
IF_FEATURE_SEAMLESS_Z( OPTBIT_COMPRESS ,) // 16th bit
IF_FEATURE_TAR_NOPRESERVE_TIME(OPTBIT_NOPRESERVE_TIME,)
+ IF_FEATURE_TAR_TO_COMMAND(OPTBIT_2COMMAND,)
#if ENABLE_FEATURE_TAR_LONG_OPTIONS
OPTBIT_NUMERIC_OWNER,
OPTBIT_NOPRESERVE_PERM,
@@ -772,6 +773,7 @@ enum {
OPT_GZIP = IF_FEATURE_SEAMLESS_GZ( (1 << OPTBIT_GZIP )) + 0, // z
OPT_COMPRESS = IF_FEATURE_SEAMLESS_Z( (1 << OPTBIT_COMPRESS )) + 0, // Z
OPT_NOPRESERVE_TIME = IF_FEATURE_TAR_NOPRESERVE_TIME((1 << OPTBIT_NOPRESERVE_TIME)) + 0, // m
+ OPT_2COMMAND = IF_FEATURE_TAR_TO_COMMAND( (1 << OPTBIT_2COMMAND )) + 0, // to-command
OPT_NUMERIC_OWNER = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NUMERIC_OWNER )) + 0, // numeric-owner
OPT_NOPRESERVE_PERM = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_NOPRESERVE_PERM)) + 0, // no-same-permissions
OPT_OVERWRITE = IF_FEATURE_TAR_LONG_OPTIONS((1 << OPTBIT_OVERWRITE )) + 0, // overwrite
@@ -813,6 +815,9 @@ static const char tar_longopts[] ALIGN1 =
# if ENABLE_FEATURE_TAR_NOPRESERVE_TIME
"touch\0" No_argument "m"
# endif
+# if ENABLE_FEATURE_TAR_TO_COMMAND
+ "to-command\0" Required_argument "\xfb"
+# endif
/* use numeric uid/gid from tar header, not textual */
"numeric-owner\0" No_argument "\xfc"
/* do not restore mode */
@@ -904,6 +909,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
, &tar_filename // -f filename
IF_FEATURE_TAR_FROM(, &(tar_handle->accept)) // T
IF_FEATURE_TAR_FROM(, &(tar_handle->reject)) // X
+ IF_FEATURE_TAR_TO_COMMAND(, &(tar_handle->tar__to_command))
#if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
, &excludes // --exclude
#endif
@@ -922,6 +928,9 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
if (opt & OPT_2STDOUT)
tar_handle->action_data = data_extract_to_stdout;
+ if (opt & OPT_2COMMAND)
+ tar_handle->action_data = data_extract_to_command;
+
if (opt & OPT_KEEP_OLD)
tar_handle->ah_flags &= ~ARCHIVE_UNLINK_OLD;
diff --git a/include/unarchive.h b/include/unarchive.h
index 8009de2..f3aa05d 100644
--- a/include/unarchive.h
+++ b/include/unarchive.h
@@ -75,6 +75,9 @@ typedef struct archive_handle_t {
char* tar__longname;
char* tar__linkname;
# endif
+#if ENABLE_FEATURE_TAR_TO_COMMAND
+ char* tar__to_command;
+#endif
# if ENABLE_FEATURE_TAR_SELINUX
char* tar__global_sctx;
char* tar__next_file_sctx;
@@ -128,6 +131,7 @@ extern void unpack_ar_archive(archive_handle_t *ar_archive) FAST_FUNC;
extern void data_skip(archive_handle_t *archive_handle) FAST_FUNC;
extern void data_extract_all(archive_handle_t *archive_handle) FAST_FUNC;
extern void data_extract_to_stdout(archive_handle_t *archive_handle) FAST_FUNC;
+extern void data_extract_to_command(archive_handle_t *archive_handle) FAST_FUNC;
extern void header_skip(const file_header_t *file_header) FAST_FUNC;
extern void header_list(const file_header_t *file_header) FAST_FUNC;
--- a/archival/libunarchive/data_extract_to_command.c 2010-06-21 13:23:28.543123649 +0200
+++ b/archival/libunarchive/data_extract_to_command.c 2010-06-21 16:05:43.000000000 +0200
@@ -0,0 +1,63 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+#include "unarchive.h"
+
+void FAST_FUNC data_extract_to_command(archive_handle_t *archive_handle)
+{
+ file_header_t *file_header = archive_handle->file_header;
+
+#if ENABLE_FEATURE_TAR_SELINUX
+ char *sctx = archive_handle->tar__next_file_sctx;
+ if (!sctx)
+ sctx = archive_handle->tar__global_sctx;
+ if (sctx) { /* setfscreatecon is 4 syscalls, avoid if possible */
+ setfscreatecon(sctx);
+ free(archive_handle->tar__next_file_sctx);
+ archive_handle->tar__next_file_sctx = NULL;
+ }
+#endif
+
+ if ((file_header->mode & S_IFMT) == S_IFREG) {
+ pid_t pid;
+ int p[2], status;
+ static const char *sh = "/bin/sh";
+
+ xpipe(p);
+ pid = vfork();
+ if (pid == 0) {
+ /* Child */
+ xdup2(p[0], STDIN_FILENO);
+ close(p[1]);
+
+ xsetenv("TAR_FILENAME", file_header->name);
+ xsetenv("TAR_SIZE", utoa(file_header->size));
+
+ execl(sh, sh + 5, "-c", archive_handle->tar__to_command, NULL);
+
+ bb_perror_msg_and_die("can't run '%s'", archive_handle->tar__to_command);
+ }
+ close(p[0]);
+ bb_copyfd_exact_size(archive_handle->src_fd, p[1], file_header->size);
+ close(p[1]);
+
+ while (wait(&status) == -1)
+ if (errno != EINTR)
+ bb_perror_msg_and_die("error %d waiting for child\n", errno);
+ if (WIFEXITED(status) && WEXITSTATUS(status))
+ bb_perror_msg_and_die("'%s' returned status %d\n",
+ archive_handle->tar__to_command, WEXITSTATUS(status));
+ if (WIFSIGNALED(status))
+ bb_perror_msg_and_die("'%s' terminated on signal %d\n",
+ archive_handle->tar__to_command, WTERMSIG(status));
+ }
+
+#if ENABLE_FEATURE_TAR_SELINUX
+ if (sctx)
+ /* reset the context after creating an entry */
+ setfscreatecon(NULL);
+#endif
+}
More information about the busybox
mailing list