tar: code shrink, better help text

function                                             old     new   delta
tar_main                                             994    1013     +19
packed_usage                                       31893   31863     -30
writeTarFile                                         250     207     -43
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 19/-73)            Total: -54 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-11-13 01:40:28 +01:00
parent 02e93b3a28
commit 931cf64ae7

View File

@ -49,7 +49,7 @@
//config: tarballs. Currently it works only on files (not pipes etc). //config: tarballs. Currently it works only on files (not pipes etc).
//config: //config:
//config:config FEATURE_TAR_FROM //config:config FEATURE_TAR_FROM
//config: bool "Enable -X (exclude from) and -T (include from) options)" //config: bool "Enable -X (exclude from) and -T (include from) options"
//config: default y //config: default y
//config: depends on TAR //config: depends on TAR
//config: help //config: help
@ -156,7 +156,9 @@ typedef struct TarBallInfo {
int tarFd; /* Open-for-write file descriptor int tarFd; /* Open-for-write file descriptor
* for the tarball */ * for the tarball */
int verboseFlag; /* Whether to print extra stuff or not */ int verboseFlag; /* Whether to print extra stuff or not */
# if ENABLE_FEATURE_TAR_FROM
const llist_t *excludeList; /* List of files to not include */ const llist_t *excludeList; /* List of files to not include */
# endif
HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */ HardLinkInfo *hlInfoHead; /* Hard Link Tracking Information */
HardLinkInfo *hlInfo; /* Hard Link Info for the current file */ HardLinkInfo *hlInfo; /* Hard Link Info for the current file */
//TODO: save only st_dev + st_ino //TODO: save only st_dev + st_ino
@ -655,44 +657,41 @@ static void NOINLINE vfork_compressor(int tar_fd, const char *gzip)
# if !SEAMLESS_COMPRESSION # if !SEAMLESS_COMPRESSION
/* Do not pass gzip flag to writeTarFile() */ /* Do not pass gzip flag to writeTarFile() */
#define writeTarFile(tar_fd, verboseFlag, recurseFlags, include, exclude, gzip) \ #define writeTarFile(tbInfo, recurseFlags, filelist, gzip) \
writeTarFile(tar_fd, verboseFlag, recurseFlags, include, exclude) writeTarFile(tbInfo, recurseFlags, filelist)
# endif # endif
/* gcc 4.2.1 inlines it, making code bigger */ /* gcc 4.2.1 inlines it, making code bigger */
static NOINLINE int writeTarFile(int tar_fd, int verboseFlag, static NOINLINE int writeTarFile(
int recurseFlags, const llist_t *include, struct TarBallInfo *tbInfo,
const llist_t *exclude, const char *gzip) int recurseFlags,
const llist_t *filelist,
const char *gzip)
{ {
int errorFlag = FALSE; int errorFlag = FALSE;
struct TarBallInfo tbInfo;
tbInfo.hlInfoHead = NULL; /*tbInfo->hlInfoHead = NULL; - already is */
tbInfo.tarFd = tar_fd;
tbInfo.verboseFlag = verboseFlag;
/* Store the stat info for the tarball's file, so /* Store the stat info for the tarball's file, so
* can avoid including the tarball into itself.... */ * can avoid including the tarball into itself.... */
xfstat(tbInfo.tarFd, &tbInfo.tarFileStatBuf, "can't stat tar file"); xfstat(tbInfo->tarFd, &tbInfo->tarFileStatBuf, "can't stat tar file");
# if SEAMLESS_COMPRESSION # if SEAMLESS_COMPRESSION
if (gzip) if (gzip)
vfork_compressor(tbInfo.tarFd, gzip); vfork_compressor(tbInfo->tarFd, gzip);
# endif # endif
tbInfo.excludeList = exclude;
/* Read the directory/files and iterate over them one at a time */ /* Read the directory/files and iterate over them one at a time */
while (include) { while (filelist) {
if (!recursive_action(include->data, recurseFlags, if (!recursive_action(filelist->data, recurseFlags,
writeFileToTarball, writeFileToTarball, &tbInfo, 0) writeFileToTarball, writeFileToTarball, tbInfo, 0)
) { ) {
errorFlag = TRUE; errorFlag = TRUE;
} }
include = include->link; filelist = filelist->link;
} }
/* Write two empty blocks to the end of the archive */ /* Write two empty blocks to the end of the archive */
memset(block_buf, 0, 2*TAR_BLOCK_SIZE); memset(block_buf, 0, 2*TAR_BLOCK_SIZE);
xwrite(tbInfo.tarFd, block_buf, 2*TAR_BLOCK_SIZE); xwrite(tbInfo->tarFd, block_buf, 2*TAR_BLOCK_SIZE);
/* To be pedantically correct, we would check if the tarball /* To be pedantically correct, we would check if the tarball
* is smaller than 20 tar blocks, and pad it if it was smaller, * is smaller than 20 tar blocks, and pad it if it was smaller,
@ -700,11 +699,11 @@ static NOINLINE int writeTarFile(int tar_fd, int verboseFlag,
* so is considered a waste of space */ * so is considered a waste of space */
/* Close so the child process (if any) will exit */ /* Close so the child process (if any) will exit */
close(tbInfo.tarFd); close(tbInfo->tarFd);
/* Hang up the tools, close up shop, head home */ /* Hang up the tools, close up shop, head home */
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
freeHardLinkInfo(&tbInfo.hlInfoHead); freeHardLinkInfo(&tbInfo->hlInfoHead);
if (errorFlag) if (errorFlag)
bb_error_msg("error exit delayed from previous errors"); bb_error_msg("error exit delayed from previous errors");
@ -721,18 +720,22 @@ static NOINLINE int writeTarFile(int tar_fd, int verboseFlag,
# endif # endif
return errorFlag; return errorFlag;
} }
#else /* !FEATURE_TAR_CREATE */ #else /* !FEATURE_TAR_CREATE */
# define writeTarFile(...) 0 # define writeTarFile(...) 0
#endif #endif
#if ENABLE_FEATURE_TAR_FROM #if ENABLE_FEATURE_TAR_FROM
static llist_t *append_file_list_to_list(llist_t *list) static llist_t *append_file_list_to_list(llist_t *list)
{ {
FILE *src_stream;
char *line;
llist_t *newlist = NULL; llist_t *newlist = NULL;
while (list) { while (list) {
FILE *src_stream;
char *line;
src_stream = xfopen_stdin(llist_pop(&list)); src_stream = xfopen_stdin(llist_pop(&list));
while ((line = xmalloc_fgetline(src_stream)) != NULL) { while ((line = xmalloc_fgetline(src_stream)) != NULL) {
/* kill trailing '/' unless the string is just "/" */ /* kill trailing '/' unless the string is just "/" */
@ -758,7 +761,7 @@ static llist_t *append_file_list_to_list(llist_t *list)
//usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m") //usage: IF_FEATURE_TAR_NOPRESERVE_TIME("m")
//usage: "vO] " //usage: "vO] "
//usage: "[-f TARFILE] [-C DIR] " //usage: "[-f TARFILE] [-C DIR] "
//usage: IF_FEATURE_TAR_FROM("[-T FILE] [-X FILE] "IF_FEATURE_TAR_LONG_OPTIONS("[--exclude PATTERN] ")) //usage: IF_FEATURE_TAR_FROM("[-T FILE] [-X FILE] "IF_FEATURE_TAR_LONG_OPTIONS("[--exclude PATTERN]... "))
//usage: "[FILE]..." //usage: "[FILE]..."
//usage:#define tar_full_usage "\n\n" //usage:#define tar_full_usage "\n\n"
//usage: IF_FEATURE_TAR_CREATE("Create, extract, ") //usage: IF_FEATURE_TAR_CREATE("Create, extract, ")
@ -1170,13 +1173,10 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
if (base_dir) if (base_dir)
xchdir(base_dir); xchdir(base_dir);
//if (SEAMLESS_COMPRESSION)
// /* We need to know whether child (gzip/bzip/etc) exits abnormally */
// signal(SIGCHLD, check_errors_in_children);
#if ENABLE_FEATURE_TAR_CREATE #if ENABLE_FEATURE_TAR_CREATE
/* Create an archive */ /* Create an archive */
if (opt & OPT_CREATE) { if (opt & OPT_CREATE) {
struct TarBallInfo *tbInfo;
# if SEAMLESS_COMPRESSION # if SEAMLESS_COMPRESSION
const char *zipMode = NULL; const char *zipMode = NULL;
if (opt & OPT_COMPRESS) if (opt & OPT_COMPRESS)
@ -1189,13 +1189,19 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
zipMode = "lzma"; zipMode = "lzma";
if (opt & OPT_XZ) if (opt & OPT_XZ)
zipMode = "xz"; zipMode = "xz";
# endif
tbInfo = xzalloc(sizeof(*tbInfo));
tbInfo->tarFd = tar_handle->src_fd;
tbInfo->verboseFlag = verboseFlag;
# if ENABLE_FEATURE_TAR_FROM
tbInfo->excludeList = tar_handle->reject;
# endif # endif
/* NB: writeTarFile() closes tar_handle->src_fd */ /* NB: writeTarFile() closes tar_handle->src_fd */
return writeTarFile(tar_handle->src_fd, verboseFlag, return writeTarFile(tbInfo,
(opt & OPT_DEREFERENCE ? ACTION_FOLLOWLINKS : 0) (opt & OPT_DEREFERENCE ? ACTION_FOLLOWLINKS : 0)
| (opt & OPT_NORECURSION ? 0 : ACTION_RECURSE), | (opt & OPT_NORECURSION ? 0 : ACTION_RECURSE),
tar_handle->accept, tar_handle->accept,
tar_handle->reject, zipMode); zipMode);
} }
#endif #endif