tar.cpp revision 345a4a88e7ca1d819d0313903ba995f815b4fa50
/* $Id$ */
/** @file
* IPRT - Tar archive I/O.
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
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;
typedef struct RTTARFILELIST
{
char *pszFilename;
typedef RTTARFILELIST *PRTTARFILELIST;
/*******************************************************************************
* Private RTTar helper *
*******************************************************************************/
{
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;
else
return VERR_TAR_CHKSUM_MISMATCH;
}
{
/* Open the target file */
if (RT_FAILURE(rc))
return rc;
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. */
do
{
/* 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 */
}
while(1);
/* Now set all file attributes */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/* Set the mode */
}
}
/* 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 */
/* Write the header first */
if (RT_SUCCESS(rc))
{
size_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. */
do
{
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 */
}
while(1);
}
return rc;
}
{
int rc = VINF_SUCCESS;
/* Seek over the data parts (512 bytes aligned) */
if (offSeek > 0)
return rc;
}
/*******************************************************************************
* Public RTTar interface *
*******************************************************************************/
{
/* Validate input */
{
AssertMsgFailed(("Must supply pszTarFile, pszFile\n"));
return VERR_INVALID_PARAMETER;
}
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
bool fFound = false;
do
{
/* 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;
}
while(1);
rc = VINF_SUCCESS;
/* Something found? */
if (RT_SUCCESS(rc) &&
fFound == false)
return rc;
}
{
/* Validate input */
{
AssertMsgFailed(("Must supply pszTarFile, ppapszFiles, pcFiles!\n"));
return VERR_INVALID_PARAMETER;
}
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
/* Iterate through the tar file record by record. Skip data records as we
* didn't need them. */
do
{
/* 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 */
{
++cCount;
/* Fill our linked list */
if (pCurr)
else
}
if (RT_FAILURE(rc))
break;
}
while(1);
rc = VINF_SUCCESS;
/* On success copy the filenames over to the user provided array pointer */
if (RT_SUCCESS(rc) &&
cCount > 0)
{
size_t i = 0;
while (pCurr)
{
/* Free the linked list node */
}
}
return rc;
}
RTR3DECL(int) RTTarExtract(const char *pszTarFile, const char *pszOutputDir, const char * const *papszFiles, size_t cFiles)
{
/* Validate input */
{
AssertMsgFailed(("Must supply pszTarFile, pszOutputDir, papszFiles!\n"));
return VERR_INVALID_PARAMETER;
}
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
/* Iterate through the tar file record by record. */
do
{
/* 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;
{
{
char *pszTargetName;
if (RT_FAILURE(rc))
break;
/* Fill our linked list */
if (pCurr)
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;
}
}
}
while(1);
rc = VINF_SUCCESS;
/* If we didn't found all files, indicate an error */
while (pCurr)
{
/* If there was a failure during extraction, delete all files which
* were extracted already */
if (RT_FAILURE(rc))
/* Delete the filename string and the list node */
}
return rc;
}
RTR3DECL(int) RTTarExtractIndex(const char *pszTarFile, const char *pszOutputDir, size_t iIndex, char** ppszFileName)
{
/* Validate input */
{
AssertMsgFailed(("Must supply pszTarFile, pszOutputDir!\n"));
return VERR_INVALID_PARAMETER;
}
/* Open the tar file */
if (RT_FAILURE(rc))
return rc;
/* Iterate through the tar file record by record. */
bool fFound = false;
do
{
/* 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;
/* On success copy the filename */
if (RT_SUCCESS(rc) &&
break;
}
++cCount;
}
if (RT_FAILURE(rc))
break;
}
while(1);
rc = VINF_SUCCESS;
/* If we didn't found the index, indicate an error */
if (!fFound)
return rc;
}
{
/* Validate input */
{
AssertMsgFailed(("Must supply pszTarFile, papszFiles!\n"));
return VERR_INVALID_PARAMETER;
}
/* 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 (RT_SUCCESS(rc))
// {
// /* Append the EOF record which is filled all by zeros */
// RTTARRECORD record;
// ASMMemFill32(&record, sizeof(record), 0);
// rc = RTFileWrite(hFile, &record, sizeof(record), NULL);
// }
/* Time to close the new tar archive */
/* Delete the freshly created tar archive on failure */
if (RT_FAILURE(rc))
return rc;
}