[RFC] tar --to-command
Ladislav Michl
Ladislav.Michl at seznam.cz
Fri Jun 18 00:15:50 UTC 2010
Hi,
I needed a convenient way of upgrading device firmware where available RAM is
smaller than image size. Firmware file contains kernel, rootfs image and a
few fpga images. Easiest way seems to pack everything into tar file and write
to appropriate places based on filename (idea comes from Marc Kleine-Budde @
pengutronix). Here is an (simplyfied) example:
#!/bin/sh
FWFILE=$1
#
# Generate command script for tar(1)
#
# get unique filename
WRITECMD=`mktemp /tmp/tarcmd.XXXXXXXX` || exit 1
# output script body (substitution turned off)
cat > $WRITECMD << 'EOF'
#!/bin/sh
case $TAR_FILENAME in
kernel*)
PARTNAME=kernel
;;
rootfs*)
PARTNAME=rootfs
;;
*)
echo "Skipping $TAR_FILENAME."
exit 0;
;;
esac
DEV=`sed -n "s/\([^:]*\):[^\"]*\"$PARTNAME\"/\1/p" < /proc/mtd`
if test -c /dev/$DEV
then
echo "$PARTNAME is $DEV"
else
echo "Unable to find flash device"
exit 1
fi
echo "Erasing $DEV. "
flash_eraseall -q /dev/$DEV >/dev/null 2>&1
if test "$?" -ne 0
then
echo "ERROR!"
exit 1
fi
echo "Writing $TAR_FILENAME to $DEV. "
nandwrite -q -p /dev/$DEV -
if test "$?" -ne 0
then
echo "ERROR!"
exit 1
fi
exit 0
EOF
chmod 755 $WRITECMD
tar --to-command=$WRITECMD -x -f $FWFILE
if test "$?" -ne 0
then
echo "failed."
exit 1
fi
Above is typicaly fed with firmware file over ssh:
ssh -l root <target_ip> "fw_install -" < fwfile
Patch bellow is the very first draw adding support for --to-command into
busybox' tar. Comments and suggestions welcome.
From: Ladislav Michl <ladis at linux-mips.org>
Signed-off-by: Ladislav Michl <ladis at linux-mips.org>
---
archival/libunarchive/data_extract_to_command.c | 62 ++++++++++++++++++++++++
archival/Config.src | 9 +++
archival/libunarchive/Kbuild.src | 1
archival/tar.c | 10 +++
include/unarchive.h | 4 +
5 files changed, 86 insertions(+)
diff --git a/archival/Config.src b/archival/Config.src
index 3dbd3ae..d4c5761 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..7a7c654 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 "b"
+# endif
/* use numeric uid/gid from tar header, not textual */
"numeric-owner\0" No_argument "\xfc"
/* do not restore mode */
@@ -900,10 +905,12 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
IF_FEATURE_SEAMLESS_GZ( "z" )
IF_FEATURE_SEAMLESS_Z( "Z" )
IF_FEATURE_TAR_NOPRESERVE_TIME("m")
+ IF_FEATURE_TAR_TO_COMMAND("b:")
, &base_dir // -C dir
, &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)) // b
#if ENABLE_FEATURE_TAR_LONG_OPTIONS && ENABLE_FEATURE_TAR_FROM
, &excludes // --exclude
#endif
@@ -922,6 +929,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-17 10:19:38.550190750 +0200
+++ b/archival/libunarchive/data_extract_to_command.c 2010-06-18 01:49:18.000000000 +0200
@@ -0,0 +1,62 @@
+/* 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];
+ char *argv[4];
+
+ xpipe(p);
+ pid = vfork();
+ if (pid == 0) {
+ /* Child */
+ xdup2(p[0], STDIN_FILENO);
+ fcntl(p[1], F_SETFD, FD_CLOEXEC);
+
+ xsetenv("TAR_FILENAME", file_header->name);
+ xsetenv("TAR_SIZE", file_header->size);
+
+ argv[0] = (char*)"/bin/sh";
+ argv[1] = (char*)"-c";
+ argv[2] = archive_handle->tar__to_command;
+ argv[3] = NULL;
+
+ execv ("/bin/sh", argv);
+
+ bb_perror_msg_and_die("can't run '%s'", archive_handle->tar__to_command);
+ }
+
+ xclose(p[0]);
+ bb_copyfd_exact_size(archive_handle->src_fd, p[1], file_header->size);
+// wait4pid(pid);
+ xclose(p[1]);
+
+ }
+
+#if ENABLE_FEATURE_TAR_SELINUX
+ if (sctx) {
+ /* reset the context after creating an entry */
+ setfscreatecon(NULL);
+ }
+#endif
+}
More information about the busybox
mailing list