tarcmd.cpp revision 7bc0784812dcf63751c5eebfdd1c5d24ac26b389
/* $Id$ */
/** @file
* IPRT - A mini TAR Command.
*/
/*
* Copyright (C) 2010-2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/buildconfig.h>
#include <iprt/initterm.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define RTZIPTARCMD_OPT_DELETE 1000
#define RTZIPTARCMD_OPT_OWNER 1001
#define RTZIPTARCMD_OPT_GROUP 1002
#define RTZIPTARCMD_OPT_UTC 1003
#define RTZIPTARCMD_OPT_PREFIX 1004
#define RTZIPTARCMD_OPT_FILE_MODE_AND_MASK 1005
#define RTZIPTARCMD_OPT_FILE_MODE_OR_MASK 1006
#define RTZIPTARCMD_OPT_DIR_MODE_AND_MASK 1007
#define RTZIPTARCMD_OPT_DIR_MODE_OR_MASK 1008
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* IPRT TAR option structure.
*/
typedef struct RTZIPTARCMDOPS
{
/** The operation (Acdrtux or RTZIPTARCMD_OPT_DELETE). */
int iOperation;
/** The long operation option name. */
const char *pszOperation;
/** The directory to change into when packing and unpacking. */
const char *pszDirectory;
/** The tar file name. */
const char *pszFile;
/** Whether we're verbose or quiet. */
bool fVerbose;
/** Whether to preserve the original file owner when restoring. */
bool fPreserveOwner;
/** Whether to preserve the original file group when restoring. */
bool fPreserveGroup;
/** Whether to skip restoring the modification time (only time stored by the
* traditional TAR format). */
bool fNoModTime;
/** The compressor/decompressor method to employ (0, z or j). */
char chZipper;
/** The owner to set. NULL if not applicable.
* Always resolved into uidOwner for extraction. */
const char *pszOwner;
/** The owner ID to set. NIL_RTUID if not applicable. */
/** The group to set. NULL if not applicable.
* Always resolved into gidGroup for extraction. */
const char *pszGroup;
/** The group ID to set. NIL_RTGUID if not applicable. */
/** Display the modification times in UTC instead of local time. */
bool fDisplayUtc;
/** File mode AND mask. */
/** File mode OR mask. */
/** Directory mode AND mask. */
/** Directory mode OR mask. */
/** What to prefix all names with when creating, adding, whatever. */
const char *pszPrefix;
/** The number of files(, directories or whatever) specified. */
/** Array of files(, directories or whatever).
* Terminated by a NULL entry. */
const char * const *papszFiles;
/** Pointer to the IPRT tar options. */
typedef RTZIPTARCMDOPS *PRTZIPTARCMDOPS;
/**
* Callback used by rtZipTarDoWithMembers
*
* @returns rcExit or RTEXITCODE_FAILURE.
* @param pOpts The tar options.
* @param hVfsObj The tar object to display
* @param pszName The name.
* @param rcExit The current exit code.
*/
typedef RTEXITCODE (*PFNDOWITHMEMBER)(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit);
/**
* Checks if @a pszName is a member of @a papszNames, optionally returning the
* index.
*
* @returns true if the name is in the list, otherwise false.
* @param pszName The name to find.
* @param papszNames The array of names.
* @param piName Where to optionally return the array index.
*/
static bool rtZipTarCmdIsNameInArray(const char *pszName, const char * const *papszNames, uint32_t *piName)
{
{
if (piName)
return true;
}
return false;
}
/**
* Opens the input archive specified by the options.
*
* @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE + printed message.
* @param pOpts The options.
* @param phVfsFss Where to return the TAR filesystem stream handle.
*/
{
int rc;
/*
* Open the input file.
*/
{
const char *pszError;
&hVfsIos,
&pszError);
if (RT_FAILURE(rc))
{
return RTMsgErrorExit(RTEXITCODE_FAILURE,
"RTVfsChainOpenIoStream failed with rc=%Rrc:\n"
" '%s'\n",
" %*s^\n",
return RTMsgErrorExit(RTEXITCODE_FAILURE,
}
}
else
{
true /*fLeaveOpen*/,
&hVfsIos);
if (RT_FAILURE(rc))
}
/*
* Pass it thru a decompressor?
*/
{
/* no */
case '\0':
rc = VINF_SUCCESS;
break;
/* gunzip */
case 'z':
if (RT_FAILURE(rc))
break;
/* bunzip2 */
case 'j':
RTMsgError("bzip2 is not supported by this build");
break;
/* bug */
default:
break;
}
if (RT_FAILURE(rc))
{
return RTEXITCODE_FAILURE;
}
if (hVfsIosDecomp != NIL_RTVFSIOSTREAM)
{
}
/*
* Open the tar filesystem stream.
*/
if (RT_FAILURE(rc))
return RTEXITCODE_SUCCESS;
}
/**
* Worker for the --list and --extract commands.
*
* @returns The appropriate exit code.
* @param pOpts The tar options.
* @param pfnCallback The command specific callback.
*/
{
/*
* Allocate a bitmap to go with the file list. This will be used to
* indicate which files we've processed and which not.
*/
{
if (!pbmFound)
}
/*
* Open the input archive.
*/
if (rcExit == RTEXITCODE_SUCCESS)
{
/*
* Process the stream.
*/
for (;;)
{
/*
* Retrive the next object.
*/
char *pszName;
if (RT_FAILURE(rc))
{
break;
}
/*
* Should we process this entry?
*/
{
if (pbmFound)
}
/*
* Release the current object and string.
*/
}
/*
* Complain about any files we didn't find.
*/
{
}
}
return rcExit;
}
/**
* Checks if the name contains any escape sequences.
*
* An escape sequence would generally be one or more '..' references. On DOS
* like system, something that would make up a drive letter reference is also
* considered an escape sequence.
*
* @returns true / false.
* @param pszName The name to consider.
*/
static bool rtZipTarHasEscapeSequence(const char *pszName)
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
if (pszName[0] == ':')
return true;
#endif
while (*pszName)
{
while (RTPATH_IS_SEP(*pszName))
pszName++;
if ( pszName[0] == '.'
return true;
pszName++;
}
return false;
}
/**
* Queries the user ID to use when extracting a member.
*
* @returns rcExit or RTEXITCODE_FAILURE.
* @param pOpts The tar options.
* @param pUser The user info.
* @param pszName The file name to use when complaining.
* @param rcExit The current exit code.
* @param pUid Where to return the user ID.
*/
static RTEXITCODE rtZipTarQueryExtractOwner(PRTZIPTARCMDOPS pOpts, PCRTFSOBJINFO pOwner, const char *pszName, RTEXITCODE rcExit,
{
else if (pOpts->fPreserveGroup)
{
else
{
}
}
else
return rcExit;
}
/**
* Queries the group ID to use when extracting a member.
*
* @returns rcExit or RTEXITCODE_FAILURE.
* @param pOpts The tar options.
* @param pGroup The group info.
* @param pszName The file name to use when complaining.
* @param rcExit The current exit code.
* @param pGid Where to return the group ID.
*/
static RTEXITCODE rtZipTarQueryExtractGroup(PRTZIPTARCMDOPS pOpts, PCRTFSOBJINFO pGroup, const char *pszName, RTEXITCODE rcExit,
{
else if (pOpts->fPreserveGroup)
{
else
{
}
}
else
return rcExit;
}
/**
* Extracts a file.
*
* Since we can restore permissions and attributes more efficiently by working
* directly on the file handle, we have special code path for files.
*
* @returns rcExit or RTEXITCODE_FAILURE.
* @param pOpts The tar options.
* @param hVfsObj The tar object to display
* @param rcExit The current exit code.
* @param pUnixInfo The unix fs object info.
* @param pOwner The owner info.
* @param pGroup The group info.
*/
static RTEXITCODE rtZipTarCmdExtractFile(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, RTEXITCODE rcExit,
{
/*
* Open the destination file and create a stream object for it.
*/
uint32_t fOpen = RTFILE_O_READWRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_ACCESS_ATTR_DEFAULT
if (RT_FAILURE(rc))
if (RT_SUCCESS(rc))
{
/*
* Pump the data thru.
*/
if (RT_SUCCESS(rc))
{
/*
* Correct the file mode and other attributes.
*/
if (!pOpts->fNoModTime)
{
if (RT_FAILURE(rc))
}
#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
|| pOpts->fPreserveOwner
|| pOpts->fPreserveGroup)
{
{
if (RT_FAILURE(rc))
}
}
#endif
if (RT_FAILURE(rc))
}
else
}
else
rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating I/O stream for file: %Rrc", pszDst, rc);
return rcExit;
}
/**
* @callback_method_impl{PFNDOWITHMEMBER, Implements --extract.}
*/
static RTEXITCODE rtZipTarCmdExtractCallback(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit)
{
/*
* Query all the information.
*/
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
return RTMsgErrorExit(RTEXITCODE_FAILURE,
"RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
if (RT_FAILURE(rc))
return RTMsgErrorExit(RTEXITCODE_FAILURE,
"RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
const char *pszLinkType = NULL;
char szTarget[RTPATH_MAX];
szTarget[0] = '\0';
if (hVfsSymlink != NIL_RTVFSSYMLINK)
{
if (RT_FAILURE(rc))
if (!szTarget[0])
}
/*
* Construct the path to the extracted member.
*/
char szDst[RTPATH_MAX];
if (RT_FAILURE(rc))
return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Failed to construct destination path for: %Rrc", pszName, rc);
/*
* Extract according to the type.
*/
{
case RTFS_TYPE_FILE:
case RTFS_TYPE_DIRECTORY:
if (RT_FAILURE(rc))
break;
case RTFS_TYPE_SYMLINK:
if (RT_FAILURE(rc))
break;
case RTFS_TYPE_FIFO:
case RTFS_TYPE_DEV_CHAR:
case RTFS_TYPE_DEV_BLOCK:
case RTFS_TYPE_SOCKET:
case RTFS_TYPE_WHITEOUT:
default:
}
/*
* Set other attributes as requested .
* .
* Note! File extraction does get here.
*/
if (!pOpts->fNoModTime)
{
rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error changing modification time: %Rrc.", pszName, rc);
}
#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
|| pOpts->fPreserveOwner
|| pOpts->fPreserveGroup)
{
{
if (RT_FAILURE(rc))
}
}
#endif
{
else
fMode = (UnixInfo.Attr.fMode & (pOpts->fFileModeAndMask | RTFS_TYPE_MASK)) | pOpts->fFileModeOrMask;
if (RT_FAILURE(rc))
}
return rcExit;
}
/**
* @callback_method_impl{PFNDOWITHMEMBER, Implements --list.}
*/
static RTEXITCODE rtZipTarCmdListCallback(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit)
{
/*
* This is very simple in non-verbose mode.
*/
{
return rcExit;
}
/*
* Query all the information.
*/
if (RT_FAILURE(rc))
{
rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsObjQueryInfo returned %Rrc on '%s'", rc, pszName);
}
if (RT_FAILURE(rc))
{
"RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
}
if (RT_FAILURE(rc))
{
"RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
}
const char *pszLinkType = NULL;
char szTarget[RTPATH_MAX];
szTarget[0] = '\0';
if (hVfsSymlink != NIL_RTVFSSYMLINK)
{
if (RT_FAILURE(rc))
}
/*
* Translate the mode mask.
*/
char szMode[16];
{
default: szMode[0] = '?'; break;
}
szMode[0] = 'h';
/*
* Make sure we've got valid owner and group strings.
*/
/*
* Format the modification time.
*/
char szModTime[32];
if (!pOpts->fDisplayUtc)
else
if (!pTime)
/*
* Format the size and figure how much space is needed between the
*/
char szSize[64];
{
case RTFS_TYPE_DEV_CHAR:
case RTFS_TYPE_DEV_BLOCK:
break;
default:
break;
}
+ 1
: 0;
/*
* Go to press.
*/
if (pszLinkType)
RTPrintf("%s %s/%s%*s %s %s %s %s %s\n",
cchPad, "",
szTarget);
else
RTPrintf("%s %s/%s%*s %s %s %s\n",
cchPad, "",
pszName);
return rcExit;
}
/**
* Display usage.
*
* @param pszProgName The program name.
*/
static void rtZipTarUsage(const char *pszProgName)
{
/*
* 0 1 2 3 4 5 6 7 8
* 012345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
RTPrintf("Usage: %s [options]\n"
"\n",
RTPrintf("Operations:\n"
" -A, --concatenate, --catenate\n"
" Append the content of one tar archive to another. (not impl)\n"
" -c, --create\n"
" Create a new tar archive. (not impl)\n"
" -d, --diff, --compare\n"
" Compare atar archive with the file system. (not impl)\n"
" -r, --append\n"
" Append more files to the tar archive. (not impl)\n"
" -t, --list\n"
" List the contents of the tar archive.\n"
" -u, --update\n"
" Update the archive, adding files that are newer than the\n"
" ones in the archive. (not impl)\n"
" -x, --extract, --get\n"
" Extract the files from the tar archive.\n"
" --delete\n"
" Delete files from the tar archive.\n"
"\n"
);
RTPrintf("Basic Options:\n"
" -C <dir>, --directory <dir> (-A, -C, -d, -r, -u, -x)\n"
" Sets the base directory for input and output file members.\n"
" This does not apply to --file, even if it preceeds it.\n"
" -f <archive>, --file <archive> (all)\n"
" which is is the default.\n"
" -v, --verbose (all)\n"
" Verbose operation.\n"
" -p, --preserve-permissions (-x)\n"
" Preserve all permissions when extracting. Must be used\n"
" before the mode mask options as it will change some of these.\n"
" -j, --bzip2 (all)\n"
" Compress/decompress the archive with bzip2.\n"
" -z, --gzip, --gunzip, --ungzip (all)\n"
" Compress/decompress the archive with gzip.\n"
"\n");
RTPrintf("Misc Options:\n"
" Set the owner of extracted and archived files to the user specified.\n"
" Set the group of extracted and archived files to the group specified.\n"
" --utc (-t)\n"
" Display timestamps as UTC instead of local time.\n"
"\n");
RTPrintf("IPRT Options:\n"
" --prefix <dir-prefix> (-A, -C, -d, -r, -u)\n"
" Directory prefix to give the members added to the archive.\n"
" --file-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
" Restrict the access mode of regular and special files.\n"
" --file-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
" Include the given access mode for regular and special files.\n"
" --dir-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
" Restrict the access mode of directories.\n"
" --dir-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
" Include the given access mode for directories.\n"
"\n");
RTPrintf("Standard Options:\n"
" -h, -?, --help\n"
" Display this help text.\n"
" -V, --version\n"
" Display version number.\n");
}
{
/*
* Parse the command line.
*
* N.B. This is less flexible that your regular tar program in that it
* requires the operation to be specified as an option. On the other
* hand, you can specify it where ever you like in the command line.
*/
static const RTGETOPTDEF s_aOptions[] =
{
/* operations */
/* basic options */
/* other options. */
/* IPRT extensions */
{ "--file-mode-and-mask", RTZIPTARCMD_OPT_FILE_MODE_AND_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
{ "--file-mode-or-mask", RTZIPTARCMD_OPT_FILE_MODE_OR_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
{ "--dir-mode-and-mask", RTZIPTARCMD_OPT_DIR_MODE_AND_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
{ "--dir-mode-or-mask", RTZIPTARCMD_OPT_DIR_MODE_OR_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
};
if (RT_FAILURE(rc))
#if 0
if (RTPermIsSuperUser())
{
Opts.fPreserveOwner = true;
Opts.fPreserveGroup = true;
}
#endif
&& rc != VINF_GETOPT_NOT_OPTION)
{
switch (rc)
{
/* operations */
case 'A':
case 'c':
case 'd':
case 'r':
case 't':
case 'u':
case 'x':
case RTZIPTARCMD_OPT_DELETE:
if (Opts.iOperation)
break;
/* basic options */
case 'C':
if (Opts.pszDirectory)
break;
case 'f':
break;
case 'v':
break;
case 'p':
Opts.fPreserveOwner = true;
Opts.fPreserveGroup = true;
break;
case 'j':
case 'z':
break;
case RTZIPTARCMD_OPT_OWNER:
return RTMsgErrorExit(RTEXITCODE_SYNTAX,
if (RT_SUCCESS(rc))
{
}
break;
case RTZIPTARCMD_OPT_GROUP:
return RTMsgErrorExit(RTEXITCODE_SYNTAX,
if (RT_SUCCESS(rc))
{
}
break;
case RTZIPTARCMD_OPT_UTC:
Opts.fDisplayUtc = true;
break;
/* iprt extensions */
case RTZIPTARCMD_OPT_PREFIX:
break;
break;
break;
break;
break;
/* Standard bits. */
case 'h':
return RTEXITCODE_SUCCESS;
case 'V':
return RTEXITCODE_SUCCESS;
default:
}
}
if (rc == VINF_GETOPT_NOT_OPTION)
{
/* this is kind of ugly. */
}
/*
* Post proceess the options.
*/
if (Opts.iOperation == 0)
{
}
return RTMsgErrorExit(RTEXITCODE_FAILURE, "The use of --owner with %s has not implemented yet", Opts.pszOperation);
return RTMsgErrorExit(RTEXITCODE_FAILURE, "The use of --group with %s has not implemented yet", Opts.pszOperation);
/*
* Do the job.
*/
switch (Opts.iOperation)
{
case 't':
case 'x':
case 'A':
case 'c':
case 'd':
case 'r':
case 'u':
case RTZIPTARCMD_OPT_DELETE:
return RTMsgErrorExit(RTEXITCODE_FAILURE, "The operation %s is not implemented yet", Opts.pszOperation);
default:
}
}