tar.cpp revision 058c3356b392d0c6c48d7f23c6caf30e80d0c5bf
/* $Id$ */
/** @file
* IPRT - Tar archive I/O.
*/
/*
* Copyright (C) 2009-2014 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.
*/
#define RT_USE_TAR_VFS_FOR_ALL_READS // the old code sticks around for a short while for debugging the new.
/******************************************************************************
* Header Files *
******************************************************************************/
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
#endif /* RT_USE_TAR_VFS_FOR_ALL_READS */
#include "tar.h"
/******************************************************************************
* Structures and Typedefs *
******************************************************************************/
/** @name RTTARRECORD::h::linkflag
* @{ */
/** @} */
/**
* A tar file header.
*/
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;
/** Pointer to a tar file header. */
typedef RTTARRECORD *PRTTARRECORD;
/** Pointer to a tar file handle. */
typedef struct RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
/**
* The internal data of a tar handle.
*/
typedef struct RTTARINTERNAL
{
/** The magic (RTTAR_MAGIC). */
/** The handle to the tar file. */
/** The open mode for hTarFile. */
/** Whether a file within the archive is currently open for writing.
* Only one can be open. */
bool fFileOpenForWrite;
/** Whether operating in stream mode. */
bool fStreamMode;
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
/** The file cache of one file. */
#else /* RT_USE_TAR_VFS_FOR_ALL_READS */
/** The tar file VFS handle. */
/** The tar file system VFS handle. */
/** Set if hVfsFss is at the start of the stream and doesn't need rewinding. */
bool fFssAtStart;
/** The current stream object (fStreamMode = true). */
/** The name of the current object (fStreamMode = true). */
char *pszVfsCurName;
#endif /* RT_USE_TAR_VFS_FOR_ALL_READS */
/** Pointer to a the internal data of a tar handle. */
typedef RTTARINTERNAL* PRTTARINTERNAL;
/**
* The internal data of a file within a tar file.
*/
typedef struct RTTARFILEINTERNAL
{
/** The magic (RTTARFILE_MAGIC). */
/** The open mode. */
/** Pointer to back to the tar file. */
/** The name of the file. */
char *pszFilename;
/** The offset into the archive where the file header starts. */
/** The size of the file. */
/** The size set by RTTarFileSetSize(). */
/** The current offset within this file. */
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
/** The link flag. */
char linkflag;
#endif
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
/** The VFS I/O stream (only for reading atm). */
#endif
/** Pointer to the internal data of a tar file. */
typedef RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
/******************************************************************************
* Defined Constants And Macros *
******************************************************************************/
/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
/* RTTAR */
do { \
} while (0)
/* RTTARFILE */
do { \
} while (0)
/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
/* RTTAR */
/* RTTARFILE */
/** Validates a handle and returns (void) if not valid. */
/* RTTAR */
#define RTTAR_VALID_RETURN_VOID(hHandle) \
do { \
} while (0)
/* RTTARFILE */
#define RTTARFILE_VALID_RETURN_VOID(hHandle) \
do { \
} while (0)
/******************************************************************************
* Internal Functions *
******************************************************************************/
{
/*
* Small enough for the standard octal string encoding?
*
* Note! We could actually use the terminator character as well if we liked,
* but let not do that as it's easier to test this way.
*/
else
{
/*
* Base 256 extension. Set the highest bit of the left most character.
* We don't deal with negatives here, cause the size have to be greater
* than zero.
*
* Note! The base-256 extension are never used by gtar or libarchive
* with the "ustar \0" format version, only the later
* "ustar\000" version. However, this shouldn't cause much
* trouble as they are not picky about what they read.
*/
puchField[0] = 0x80;
do
{
cbSize >>= 8;
} while (cchField);
}
}
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
{
{
/*
* The first byte has the bit 7 set to indicate base-256, while bit 6
* is the signed bit. Bits 5:0 are the most significant value bits.
*/
cchField--;
puchField++;
/*
* The remaining bytes are used in full.
*/
while (cchField-- > 0)
{
{
break;
}
}
}
else
if (cbSize < 0)
cbSize = 0;
}
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
/**
* Calculates the TAR header checksums and detects if it's all zeros.
*
* @returns true if all zeros, false if not.
* @param pHdr The header to checksum.
* @param pi32Unsigned Where to store the checksum calculated using
* unsigned chars. This is the one POSIX
* specifies.
* @param pi32Signed Where to store the checksum calculated using
* signed chars.
*
* @remarks The reason why we calculate the checksum as both signed and unsigned
* has to do with various the char C type being signed on some hosts
* and unsigned on others.
*
* @remarks Borrowed from tarvfs.cpp.
*/
{
int32_t i32Unsigned = 0;
/*
* Sum up the entire header.
*/
do
{
i32Unsigned += *(unsigned char *)pch;
/*
* Check if it's all zeros and replace the chksum field with spaces.
*/
bool const fZeroHdr = i32Unsigned == 0;
do
{
i32Unsigned -= *(unsigned char *)pch;
if (pi32Signed)
*pi32Signed = i32Signed;
return fZeroHdr;
}
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
{
/* Check for EOF. EOF is valid in this case, cause it indicates no more
* data in the tar archive. */
return VERR_TAR_END_OF_FILE;
/* Report any other errors */
else if (RT_FAILURE(rc))
return rc;
/* Check for data integrity & an EOF record */
return VERR_TAR_END_OF_FILE;
/* Verify the checksum */
if ( RT_SUCCESS(rc)
{
/* Make sure the strings are zero terminated. */
}
else
return rc;
}
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
DECLINLINE(int) rtTarCreateHeaderRecord(PRTTARRECORD pRecord, const char *pszSrcName, uint64_t cbSize,
{
/** @todo check for field overflows. */
/* Fill the header record */
// RT_ZERO(pRecord); - done by the caller.
/** @todo use RTStrCopy */
return VERR_BUFFER_OVERFLOW;
/* Create the checksum out of the new header */
return VERR_TAR_END_OF_FILE;
/* Format the checksum */
return VINF_SUCCESS;
}
{
*pcbSize = 0;
/* Allocate a reasonably large buffer, fall back on a tiny one.
* Note: has to be 512 byte aligned and >= 512 byte. */
if (!pvTmp)
{
cbTmp = sizeof(RTTARRECORD);
}
return pvTmp;
}
{
/* Allocate a temporary buffer for copying the tar content in blocks. */
if (!pvTmp)
return VERR_NO_MEMORY;
int rc = VINF_SUCCESS;
uint64_t cbAllWritten = 0;
for (;;)
{
if (cbAllWritten >= cbSize)
break;
if (RT_FAILURE(rc))
break;
}
return rc;
}
DECLINLINE(PRTTARFILEINTERNAL) rtCreateTarFileInternal(PRTTARINTERNAL pInt, const char *pszFilename, uint32_t fOpen)
{
if (!pFileInt)
return NULL;
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
#endif
if (!pFileInt->pszFilename)
{
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
#endif
return NULL;
}
return pFileInt;
}
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
/**
* Creates a tar file handle for a read-only VFS stream object.
*
* @returns IPRT status code.
* @param pszName The file name. Automatically freed on failure.
* @param hVfsIos The VFS I/O stream we create the handle around.
* The reference is NOT consumed.
* @param fOpen The open flags.
* @param ppFile Where to return the handle.
*/
static int rtTarFileCreateHandleForReadOnly(char *pszName, RTVFSIOSTREAM hVfsIos, uint32_t fOpen, PRTTARFILEINTERNAL *ppFile)
{
int rc;
if (pNewFile)
{
if (RT_SUCCESS(rc))
{
pNewFile->offCurrent = 0;
return VINF_SUCCESS;
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
{
if (!pNewInt)
return NULL;
if (!pNewInt->pszFilename)
{
return NULL;
}
return pNewInt;
}
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
{
if (pInt)
{
if (pInt->pszFilename)
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
{
}
#endif
}
}
static int rtTarAppendFileFromFile(RTTAR hTar, const char *pszSrcName, const uint64_t cbOverallSize, uint64_t &cbOverallWritten, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
{
/* Open the source file */
if (RT_FAILURE(rc))
return rc;
do
{
/* Get the size of the source file */
if (RT_FAILURE(rc))
break;
if (RT_FAILURE(rc))
break;
/* Get some info from the source file */
/* This isn't critical. Use the defaults if it fails. */
if (RT_SUCCESS(rc))
{
}
/* Set the mode from the other file */
if (RT_FAILURE(rc))
break;
/* Set the modification time from the other file */
if (RT_FAILURE(rc))
break;
/* Set the owner from the other file */
if (RT_FAILURE(rc))
break;
/* Allocate a temporary buffer for copying the tar content in blocks. */
if (!pvTmp)
{
rc = VERR_NO_MEMORY;
break;
}
/* 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 (pfnProgressCallback)
if (cbAllWritten >= cbToCopy)
break;
/* Read one block. Either its the buffer size or the rest of the
* file. */
if (RT_FAILURE(rc))
break;
/* Write one block. */
if (RT_FAILURE(rc))
break;
/* Count how many bytes (of the original file) are written already */
cbAllWritten += cbRead;
}
} while (0);
/* Cleanup */
if (pvTmp)
if (hFile)
return rc;
}
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
{
int rc = VINF_SUCCESS;
/* Seek over the data parts (512 bytes aligned) */
if (offSeek > 0)
return rc;
}
{
/* Assume we are on the file head. */
int rc = VINF_SUCCESS;
bool fFound = false;
for (;;)
{
/* Read & verify a header record */
/* Check for error or EOF. */
if (RT_FAILURE(rc))
break;
/* We support normal files only */
{
{
/* Get the file size */
/* Seek back, to position the file pointer at the start of the header. */
fFound = true;
break;
}
}
if (RT_FAILURE(rc))
break;
}
if (rc == VERR_TAR_END_OF_FILE)
rc = VINF_SUCCESS;
/* Something found? */
if ( RT_SUCCESS(rc)
&& !fFound)
return rc;
}
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
/******************************************************************************
* Public Functions *
******************************************************************************/
{
/*
* Create a tar instance.
*/
if (!pThis)
return VERR_NO_MEMORY;
/*
* Open the tar file.
*/
int rc;
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
pThis->fFssAtStart = false;
if (!(fMode & RTFILE_O_WRITE))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
pThis->fFssAtStart = true;
else
{
}
}
}
else
#endif
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
{
return VINF_SUCCESS;
int rc = VINF_SUCCESS;
/* gtar gives a warning, but the documentation says EOF is indicated by a
* zero block. Disabled for now. */
#if 0
{
/* Append the EOF record which is filled all by zeros */
}
#endif
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
{
}
{
}
{
}
if (pInt->pszVfsCurName)
{
}
#endif /* RT_USE_TAR_VFS_FOR_ALL_READS */
{
}
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
/* Delete any remaining cached file headers. */
if (pInt->pFileCache)
{
}
#endif
return rc;
}
{
return VERR_INVALID_HANDLE;
if (pInt->fStreamMode)
return VERR_INVALID_STATE;
if (fOpen & RTFILE_O_WRITE)
{
return VERR_WRITE_PROTECT;
if (pInt->fFileOpenForWrite)
return VERR_TOO_MANY_OPEN_FILES;
}
int rc = VINF_SUCCESS;
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
if (!(fOpen & RTFILE_O_WRITE))
{
/*
* Rewind the stream if necessary.
*/
if (!pInt->fFssAtStart)
{
{
}
{
if (RT_FAILURE(rc))
return rc;
}
if (RT_FAILURE(rc))
return rc;
}
/*
* Search the file system stream.
*/
pInt->fFssAtStart = false;
for (;;)
{
char *pszName;
return VERR_FILE_NOT_FOUND;
if (RT_FAILURE(rc))
return rc;
{
else
{
}
break;
}
} /* Search loop. */
}
else
#endif /* RT_USE_TAR_VFS_FOR_ALL_READS */
{
if (!pFileInt)
return VERR_NO_MEMORY;
do /* break loop */
{
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
#endif
{
pInt->fFileOpenForWrite = true;
/* If we are in write mode, we also in append mode. Add an dummy
* header at the end of the current file. It will be filled by the
* close operation. */
if (RT_FAILURE(rc))
break;
if (RT_FAILURE(rc))
break;
}
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
else
{
/* We need to be on the start of the file */
if (RT_FAILURE(rc))
break;
/* Search for the file. */
if (RT_FAILURE(rc))
break;
}
#endif
} while (0);
/* Cleanup on failure */
if (RT_FAILURE(rc))
{
if (pFileInt->pszFilename)
}
else
}
return rc;
}
{
/* Already closed? */
if (hFile == NIL_RTTARFILE)
return VINF_SUCCESS;
int rc = VINF_SUCCESS;
/* In read mode: */
{
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
/* In read mode, we want to make sure to stay at the aligned end of this
* file, so the next file could be read immediately. */
/* Check that the file pointer is somewhere within the last open file.
* If we are at the beginning (nothing read yet) nothing will be done.
* something. */
&& offCur < RT_ALIGN(pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize, sizeof(RTTARRECORD)))
{
/* Seek to the next file header. */
uint64_t offNext = RT_ALIGN(pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize, sizeof(RTTARRECORD));
}
#endif
}
{
do
{
/* If the user has called RTTarFileSetSize in the meantime, we have
to make sure the file has the right size. */
{
if (RT_FAILURE(rc))
break;
}
/* If the written size isn't 512 byte aligned, we need to fix this. */
{
/* Note the RTFile method. We didn't increase the cbSize or cbCurrentPos here. */
&record,
NULL);
if (RT_FAILURE(rc))
break;
}
/* Create a header record for the file */
/* Todo: mode, gid, uid, mtime should be setable (or detected myself) */
if (RT_FAILURE(rc))
break;
/* Write this at the start of the file data */
rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart, &record, sizeof(RTTARRECORD), NULL);
if (RT_FAILURE(rc))
break;
}
while (0);
}
/* Now cleanup and delete the handle */
return rc;
}
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
RTR3DECL(int) RTTarFileSeek(RTTARFILE hFile, uint64_t offSeek, unsigned uMethod, uint64_t *poffActual)
{
return VERR_INVALID_STATE;
switch (uMethod)
{
case RTFILE_SEEK_BEGIN:
{
return VERR_SEEK_ON_DEVICE;
break;
}
case RTFILE_SEEK_CURRENT:
{
return VERR_SEEK_ON_DEVICE;
break;
}
case RTFILE_SEEK_END:
{
return VERR_NEGATIVE_SEEK;
break;
}
default: AssertFailedReturn(VERR_INVALID_PARAMETER);
}
if (poffActual)
return VINF_SUCCESS;
}
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
{
return pFileInt->offCurrent;
}
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
{
}
RTR3DECL(int) RTTarFileReadAt(RTTARFILE hFile, uint64_t off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
{
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
int rc = RTVfsIoStrmReadAt(pFileInt->hVfsIos, off, pvBuf, cbToRead, true /*fBlocking*/, &cbTmpRead);
if (RT_SUCCESS(rc))
{
if (pcbRead)
}
else if (pcbRead)
*pcbRead = 0;
#else
/* Check that we not read behind the end of file. If so return immediately. */
{
if (pcbRead)
*pcbRead = 0;
return VINF_SUCCESS; /* ??? VERR_EOF */
}
int rc = RTFileReadAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToCopy, &cbTmpRead);
if (pcbRead)
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
return rc;
}
RTR3DECL(int) RTTarFileWrite(RTTARFILE hFile, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
{
/** @todo Optimize this, by checking the current pos */
}
RTR3DECL(int) RTTarFileWriteAt(RTTARFILE hFile, uint64_t off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
{
return VERR_WRITE_ERROR;
size_t cbTmpWritten = 0;
int rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToWrite, &cbTmpWritten);
if (pcbWritten)
return rc;
}
{
/* Validate input */
return VINF_SUCCESS;
}
{
return VERR_WRITE_ERROR;
/** @todo If cbSize is smaller than pFileInt->cbSize we have to
* truncate the current file. */
return VINF_SUCCESS;
}
{
return VERR_WRITE_ERROR;
/* Convert the mode to an string. */
/* Write it directly into the header */
NULL);
}
{
return VERR_WRITE_ERROR;
/* Convert the time to an string. */
/* Write it directly into the header */
NULL);
}
{
return VERR_WRITE_ERROR;
int rc = VINF_SUCCESS;
{
/* Convert the uid to an string. */
/* Write it directly into the header */
NULL);
if (RT_FAILURE(rc))
return rc;
}
{
/* Convert the gid to an string. */
/* Write it directly into the header */
NULL);
if (RT_FAILURE(rc))
return rc;
}
return rc;
}
/******************************************************************************
* Convenience Functions *
******************************************************************************/
{
/* Validate input */
/* Open the tar file */
int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false /*fStream*/);
if (RT_FAILURE(rc))
return rc;
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
/*
* Enumerate the VFS file system stream.
*/
size_t cFilesAllocated = 0;
char **papszFiles = NULL;
for (;;)
{
char *pszName;
{
return VINF_SUCCESS;
}
if (RT_FAILURE(rc))
break;
if (cFiles >= cFilesAllocated)
{
size_t cNew = !cFilesAllocated ? 64 : cFilesAllocated < _1M ? cFilesAllocated * 2 : cFilesAllocated + _1M;
if (!pvNew)
{
rc = VERR_NO_MEMORY;
break;
}
papszFiles = (char **)pvNew;
}
} /* Search loop. */
/*
* Failed, clean up and return.
*/
if (papszFiles)
{
while (cFiles-- > 0)
}
#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
/* This is done by internal methods, cause we didn't have a RTTARDIR
* interface, yet. This should be fixed someday. */
char **papszFiles = NULL;
do /* break loop */
{
/* Initialize the file name array with one slot */
papszFiles = (char **)RTMemAlloc(sizeof(char *));
if (!papszFiles)
{
rc = VERR_NO_MEMORY;
break;
}
/* Iterate through the tar file record by record. Skip data records as we
* didn't need them. */
for (;;)
{
/* Read & verify a header record */
/* Check for error or EOF. */
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;
}
} while (0);
if (rc == VERR_TAR_END_OF_FILE)
rc = VINF_SUCCESS;
/* Return the file array on success, dispose of it on failure. */
if (RT_SUCCESS(rc))
{
}
else
{
while (cFiles-- > 0)
}
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
return rc;
}
RTR3DECL(int) RTTarCreate(const char *pszTarFile, const char * const *papszFiles, size_t cFiles, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
{
/* Validate input */
int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_CREATE | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE, false /*fStream*/);
if (RT_FAILURE(rc))
return rc;
/* Get the overall size of all files to pack into the tar archive. Only
necessary if there is a progress callback. */
uint64_t cbOverallSize = 0;
if (pfnProgressCallback)
{
if (RT_FAILURE(rc))
break;
cbOverallSize += cbSize;
}
uint64_t cbOverallWritten = 0;
{
rc = rtTarAppendFileFromFile(hTar, papszFiles[i], cbOverallSize, cbOverallWritten, pfnProgressCallback, pvUser);
if (RT_FAILURE(rc))
break;
}
/* Cleanup */
return rc;
}
/******************************************************************************
* Streaming Functions *
******************************************************************************/
{
/* Validate input. */
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
if (!pInt->fStreamMode)
return VERR_INVALID_STATE;
if (!pInt->pszVfsCurName)
{
if (RT_FAILURE(rc))
return rc;
}
if (ppszFilename)
{
if (!*ppszFilename)
return VERR_NO_STR_MEMORY;
}
#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
/* Open and close the file on the current position. This makes sure the
* cache is filled in case we never read something before. On success it
* will return the current filename. */
int rc = RTTarFileOpenCurrentFile(hTar, &hFile, ppszFilename, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
if (RT_SUCCESS(rc))
return rc;
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
}
{
if (!pInt->fStreamMode)
return VERR_INVALID_STATE;
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
/*
* Release the current object.
*/
{
}
if (pInt->pszVfsCurName)
{
}
/*
* Find the next file.
*/
for (;;)
{
char *pszName;
return VERR_TAR_END_OF_FILE;
if ( enmType == RTVFSOBJTYPE_FILE
|| enmType == RTVFSOBJTYPE_DIR)
{
if (enmType == RTVFSOBJTYPE_DIR)
else
{
rc = VINF_SUCCESS;
}
return rc;
}
}
#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
int rc = VINF_SUCCESS;
/* If there is nothing in the cache, it means we never read something. Just
* ask for the current filename to fill the cache. */
if (!pInt->pFileCache)
{
if (RT_FAILURE(rc))
return rc;
}
/* Check that the file pointer is somewhere within the last open file.
* If not we are somehow busted. */
return VERR_INVALID_STATE;
/* Seek to the next file header. */
uint64_t offNext = RT_ALIGN(pInt->pFileCache->offStart + sizeof(RTTARRECORD) + pInt->pFileCache->cbSize, sizeof(RTTARRECORD));
{
if (RT_FAILURE(rc))
return rc;
}
else
{
/* Else delete the last open file cache. Might be recreated below. */
}
/* Again check the current filename to fill the cache with the new value. */
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
}
RTR3DECL(int) RTTarFileOpenCurrentFile(RTTAR hTar, PRTTARFILE phFile, char **ppszFilename, uint32_t fOpen)
{
/* Validate input. */
if (!pInt->fStreamMode)
return VERR_INVALID_STATE;
#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
/*
* Make sure there is a current file (first call w/o RTTarSeekNextFile call).
*/
{
if (pInt->pszVfsCurName)
return -VINF_TAR_DIR_PATH;
if (RT_FAILURE(rc))
return rc;
return -VINF_TAR_DIR_PATH;
}
/*
* Return a copy of the filename if requested.
*/
if (ppszFilename)
{
if (!*ppszFilename)
return VERR_NO_STR_MEMORY;
}
/*
* Create a handle for it.
*/
int rc = rtTarFileCreateHandleForReadOnly(RTStrDup(pInt->pszVfsCurName), pInt->hVfsCur, RTFILE_O_READ, phFile);
{
*ppszFilename = NULL;
}
#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
int rc = VINF_SUCCESS;
/* Is there some cached entry? */
if (pInt->pFileCache)
{
{
/* Else delete the last open file cache. Might be recreated below. */
}
else/* Are we still directly behind that header? */
{
/* Yes, so the streaming can start. Just return the cached file
* structure to the caller. */
if (ppszFilename)
return VINF_TAR_DIR_PATH;
return VINF_SUCCESS;
}
}
do /* break loop */
{
/* Try to read a header entry from the current position. If we aren't
* on a header record, the header checksum will show and an error will
* be returned. */
/* Read & verify a header record */
/* Check for error or EOF. */
if (RT_FAILURE(rc))
break;
/* We support normal files only */
{
if (!pFileInt)
{
rc = VERR_NO_MEMORY;
break;
}
/* Get the file size */
/* The start is -512 from here. */
/* remember the type of a file */
/* Copy the new file structure to our cache. */
if (ppszFilename)
}
} while (0);
if (RT_FAILURE(rc))
{
if (pFileInt)
}
else
#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
return rc;
}