tar.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
/* $Id$ */
/** @file
* IPRT - Tar archive I/O.
*/
/*
* Copyright (C) 2009 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 *
*******************************************************************************/
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** @name RTTARRECORD::h::linkflag
* @{ */
/** @} */
typedef union RTTARRECORD
{
char d[512];
struct h
{
char name[100];
char mode[8];
char uid[8];
char gid[8];
char size[12];
char mtime[12];
char chksum[8];
char linkflag;
char linkname[100];
char magic[8];
char uname[32];
char gname[32];
char devmajor[8];
char devminor[8];
} h;
} RTTARRECORD;
typedef RTTARRECORD *PRTTARRECORD;
#if 0 /* not currently used */
typedef struct RTTARFILELIST
{
char *pszFilename;
typedef RTTARFILELIST *PRTTARFILELIST;
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
{
for (size_t i = 0; i < sizeof(RTTARRECORD); ++i)
{
/* Calculate the sum of every byte from the header. The checksum field
* itself is counted as all blanks. */
else
check += ' ';
/* Additional check if all fields are zero, which indicate EOF. */
}
/* EOF? */
if (!zero)
return VERR_EOF;
return VINF_SUCCESS;
}
{
/* EOF? */
if (RT_FAILURE(rc))
return rc;
/* Verify the checksum */
return VINF_SUCCESS;
return VERR_TAR_CHKSUM_MISMATCH;
}
{
/* Open the target file */
int rc = RTFileOpen(&hNewFile, pszTargetName, RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
if (RT_FAILURE(rc))
return rc;
/**@todo r=bird: Use a bigger buffer here, see comment in rtTarCopyFileTo. */
size_t cbAllWritten = 0;
/* Copy the content from hFile over to pszTargetName. This is done block
* wise in 512 byte steps. After this copying is finished hFile will be on
* a 512 byte boundary, regardless if the file copied is 512 byte size
* aligned. */
for (;;)
{
/* Finished already? */
if (cbAllWritten == cbToCopy)
break;
/* Read one block */
if (RT_FAILURE(rc))
break;
/* Check for the last block which has not to be 512 bytes in size. */
/* Write the block */
if (RT_FAILURE(rc))
break;
/* Count how many bytes are written already */
}
/* Now set all file attributes */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/* Set the mode */
}
}
/* Make sure the called doesn't mix truncated tar files with the official
* end indicated by rtTarCalcChkSum. */
/* Delete the freshly created file in the case of an error */
if (RT_FAILURE(rc))
return rc;
}
{
/* Open the source file */
if (RT_FAILURE(rc))
return rc;
/* Get the size of the source file */
if (RT_FAILURE(rc))
{
return rc;
}
/* Get some info from the source file */
/* This isn't critical. Use the defaults if it fails. */
if (RT_SUCCESS(rc))
{
}
/* Fill the header record */
/* Create the checksum out of the new header */
if (RT_SUCCESS(rc))
{
/* Write the header first */
if (RT_SUCCESS(rc))
{
/** @todo r=bird: using a 64KB buffer here instead of 0.5KB would probably be
* a good thing. */
uint64_t cbAllWritten = 0;
/* Copy the content from pszSrcName over to hFile. This is done block
* wise in 512 byte steps. After this copying is finished hFile will be
* on a 512 byte boundary, regardless if the file copied is 512 byte
* size aligned. */
for (;;)
{
if (cbAllWritten >= cbSize)
break;
/* Last record? */
{
/* Initialize with zeros */
}
/* Read one block */
if (RT_FAILURE(rc))
break;
/* Write one block */
if (RT_FAILURE(rc))
break;
/* Count how many bytes are written already */
cbAllWritten += sizeof(record);
}
/* Make sure the called doesn't mix truncated tar files with the
* official end indicated by rtTarCalcChkSum. */
}
}
return rc;
}
{
int rc = VINF_SUCCESS;
/* Seek over the data parts (512 bytes aligned) */
if (offSeek > 0)
return rc;
}
{
/* Validate input */
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
bool fFound = false;
for (;;)
{
/** @todo r=bird: the reading, validation and EOF check done here should be
* moved to a separate helper function. That would make it easiser to
* distinguish genuine-end-of-tar-file and VERR_EOF caused by a
* trunacted file. That said, rtTarSkipData won't return VERR_EOF, at
* least not on unix, since it's not a sin to seek beyond the end of a
* file. */
/* Check for error or EOF. */
if (RT_FAILURE(rc))
break;
/* Check for EOF & data integrity */
if (RT_FAILURE(rc))
break;
/* We support normal files only */
{
{
fFound = true;
break;
}
}
if (RT_FAILURE(rc))
break;
}
rc = VINF_SUCCESS;
/* Something found? */
if ( RT_SUCCESS(rc)
&& !fFound)
return rc;
}
{
/* Validate input */
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
/* Initialize the file name array with one slot */
char **papszFiles = (char**)RTMemAlloc(sizeof(char *));
if (!papszFiles)
{
return VERR_NO_MEMORY;
}
/* Iterate through the tar file record by record. Skip data records as we
* didn't need them. */
for (;;)
{
/* Check for error or EOF. */
if (RT_FAILURE(rc))
break;
/* Check for EOF & data integrity */
if (RT_FAILURE(rc))
break;
/* We support normal files only */
{
if (cFiles >= cFilesAlloc)
{
/* Double the array size, make sure the size doesn't wrap. */
if (!pvNew)
{
rc = VERR_NO_MEMORY;
break;
}
papszFiles = (char **)pvNew;
cFilesAlloc *= 2;
}
/* Duplicate the name */
if (!papszFiles[cFiles])
{
rc = VERR_NO_MEMORY;
break;
}
cFiles++;
}
if (RT_FAILURE(rc))
break;
}
rc = VINF_SUCCESS;
/* Return the file array on success, dispose of it on failure. */
if (RT_SUCCESS(rc))
{
}
else
{
while (cFiles-- > 0)
}
return rc;
}
RTR3DECL(int) RTTarExtractFiles(const char *pszTarFile, const char *pszOutputDir, const char * const *papszFiles, size_t cFiles)
{
/* Validate input */
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
/* Iterate through the tar file record by record. */
if (paExtracted)
{
size_t cExtracted = 0;
for (;;)
{
/* Check for error or EOF. */
if (RT_FAILURE(rc))
break;
/* Check for EOF & data integrity */
if (RT_FAILURE(rc))
break;
/* We support normal files only */
{
bool fFound = false;
{
{
fFound = true;
if (cExtracted < cFiles)
{
char *pszTargetFile;
if (rc > 0)
{
if (RT_SUCCESS(rc))
else
}
else
rc = VERR_NO_MEMORY;
}
else
break;
}
}
if (RT_FAILURE(rc))
break;
/* If the current record isn't a file in the file list we have to
* skip the data */
if (!fFound)
{
if (RT_FAILURE(rc))
break;
}
}
}
rc = VINF_SUCCESS;
/* If we didn't found all files, indicate an error */
/* Cleanup the names of the extracted files, deleting them on failure. */
while (cExtracted-- > 0)
{
if (RT_FAILURE(rc))
}
}
else
return rc;
}
RTR3DECL(int) RTTarExtractByIndex(const char *pszTarFile, const char *pszOutputDir, size_t iIndex, char **ppszFileName)
{
/* Validate input */
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
/* Iterate through the tar file record by record. */
bool fFound = false;
for (;;)
{
/* Check for error or EOF. */
if (RT_FAILURE(rc))
break;
/* Check for EOF & data integrity */
if (RT_FAILURE(rc))
break;
/* We support normal files only */
{
{
fFound = true;
char *pszTargetName;
if (rc > 0)
{
/* On success pass on the filename if requested. */
if ( RT_SUCCESS(rc)
&& ppszFileName)
else
}
else
rc = VERR_NO_MEMORY;
break;
}
}
if (RT_FAILURE(rc))
break;
++iFile;
}
rc = VINF_SUCCESS;
/* If we didn't found the index, indicate an error */
return rc;
}
{
/* Validate input */
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
{
if (RT_FAILURE(rc))
break;
}
/* gtar gives a warning, but the documentation says EOF is indicated by a
* zero block. Disabled for now. */
#if 0
if (RT_SUCCESS(rc))
{
/* Append the EOF record which is filled all by zeros */
}
#endif
/* Time to close the new tar archive */
/* Delete the freshly created tar archive on failure */
if (RT_FAILURE(rc))
return rc;
}