tarvfs.cpp revision 43af8389304c77c3d4db525de8907cb74207c380
43af8389304c77c3d4db525de8907cb74207c380vboxsync * IPRT - TAR Virtual Filesystem.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Copyright (C) 2010 Oracle Corporation
43af8389304c77c3d4db525de8907cb74207c380vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
43af8389304c77c3d4db525de8907cb74207c380vboxsync * available from http://www.virtualbox.org. This file is free software;
43af8389304c77c3d4db525de8907cb74207c380vboxsync * you can redistribute it and/or modify it under the terms of the GNU
43af8389304c77c3d4db525de8907cb74207c380vboxsync * General Public License (GPL) as published by the Free Software
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
43af8389304c77c3d4db525de8907cb74207c380vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
43af8389304c77c3d4db525de8907cb74207c380vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * The contents of this file may alternatively be used under the terms
43af8389304c77c3d4db525de8907cb74207c380vboxsync * of the Common Development and Distribution License Version 1.0
43af8389304c77c3d4db525de8907cb74207c380vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
43af8389304c77c3d4db525de8907cb74207c380vboxsync * VirtualBox OSE distribution, in which case the provisions of the
43af8389304c77c3d4db525de8907cb74207c380vboxsync * CDDL are applicable instead of those of the GPL.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * You may elect to license modified versions of this file under the
43af8389304c77c3d4db525de8907cb74207c380vboxsync * terms and conditions of either the GPL or the CDDL or both.
43af8389304c77c3d4db525de8907cb74207c380vboxsync/******************************************************************************
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Header Files *
43af8389304c77c3d4db525de8907cb74207c380vboxsync ******************************************************************************/
43af8389304c77c3d4db525de8907cb74207c380vboxsync/*******************************************************************************
43af8389304c77c3d4db525de8907cb74207c380vboxsync* Structures and Typedefs *
43af8389304c77c3d4db525de8907cb74207c380vboxsync*******************************************************************************/
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Tar directory, character device, block device, fifo socket or symbolic link.
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The stream offset of the (first) header. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The tar header. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The object info with unix attributes. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync/** Pointer to a tar filesystem stream base object. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Tar file represented as a VFS I/O stream.
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The basic tar object data. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The number of bytes in the file. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The current file position. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The number of padding bytes following the file. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** Set if we've reached the end of the file. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The input I/O stream. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync/** Pointer to a the private data of a tar file I/O stream. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Tar filesystem stream private data.
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The input I/O stream. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The current object (referenced). */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** Pointer to the private data if hVfsCurObj is representing a file. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The start offset. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** The offset of the next header. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** Set if we've reached the end of the stream. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** Set if we've encountered a fatal error. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync/** Pointer to a the private data of a tar filesystem stream. */
43af8389304c77c3d4db525de8907cb74207c380vboxsyncDECLINLINE(bool) rtZipTarHdrHasPosixUserName(PCRTZIPTARHDR pTar)
43af8389304c77c3d4db525de8907cb74207c380vboxsync return true;
43af8389304c77c3d4db525de8907cb74207c380vboxsyncDECLINLINE(bool) rtZipTarHdrHasPosixGroupName(PCRTZIPTARHDR pTar)
43af8389304c77c3d4db525de8907cb74207c380vboxsync return true;
43af8389304c77c3d4db525de8907cb74207c380vboxsync * T h e V F S F i l e s y s t e m S t r e a m B i t s.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * T h e V F S F i l e s y s t e m S t r e a m B i t s.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * T h e V F S F i l e s y s t e m S t r e a m B i t s.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSOBJOPS,pfnClose}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssBaseObj_Close(void *pvThis)
43af8389304c77c3d4db525de8907cb74207c380vboxsync /* Currently there is nothing we really have to do here. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssBaseObj_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Copy the desired data.
43af8389304c77c3d4db525de8907cb74207c380vboxsync pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_OWNER;
43af8389304c77c3d4db525de8907cb74207c380vboxsync pObjInfo->Attr.u.UnixOwner.uid = pThis->ObjInfo.Attr.u.Unix.uid;
43af8389304c77c3d4db525de8907cb74207c380vboxsync RTStrCopy(pObjInfo->Attr.u.UnixOwner.szName, sizeof(pObjInfo->Attr.u.UnixOwner.szName), pThis->Hdr.Hdr.uname);
43af8389304c77c3d4db525de8907cb74207c380vboxsync pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_UNIX_GROUP;
43af8389304c77c3d4db525de8907cb74207c380vboxsync pObjInfo->Attr.u.UnixGroup.gid = pThis->ObjInfo.Attr.u.Unix.gid;
43af8389304c77c3d4db525de8907cb74207c380vboxsync RTStrCopy(pObjInfo->Attr.u.UnixGroup.szName, sizeof(pObjInfo->Attr.u.UnixGroup.szName), pThis->Hdr.Hdr.gname);
43af8389304c77c3d4db525de8907cb74207c380vboxsync pObjInfo->Attr.enmAdditional = RTFSOBJATTRADD_EASIZE;
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Tar filesystem stream operations.
43af8389304c77c3d4db525de8907cb74207c380vboxsync "TarFsStream::Obj",
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSOBJOPS,pfnClose}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssIos_Close(void *pvThis)
43af8389304c77c3d4db525de8907cb74207c380vboxsync PRTZIPTARIOSTREAM pThis = (PRTZIPTARIOSTREAM)pvThis;
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssIos_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
43af8389304c77c3d4db525de8907cb74207c380vboxsync PRTZIPTARIOSTREAM pThis = (PRTZIPTARIOSTREAM)pvThis;
43af8389304c77c3d4db525de8907cb74207c380vboxsync return rtZipTarFssBaseObj_QueryInfo(&pThis->BaseObj, pObjInfo, enmAddAttr);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Reads one segment.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @returns IPRT status code.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @param pThis The instance data.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @param pvBuf Where to put the read bytes.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @param cbToRead The number of bytes to read.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @param fBlocking Whether to block or not.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @param pcbRead Where to store the number of bytes actually read.
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic int rtZipTarFssIos_ReadOneSeg(PRTZIPTARIOSTREAM pThis, void *pvBuf, size_t cbToRead, bool fBlocking, size_t *pcbRead)
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Fend of reads beyond the end of the stream here.
43af8389304c77c3d4db525de8907cb74207c380vboxsync uint64_t cbLeft = (uint64_t)(pThis->cbFile - pThis->offFile);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Do the reading.
43af8389304c77c3d4db525de8907cb74207c380vboxsync int rc = RTVfsIoStrmRead(pThis->hVfsIos, pvBuf, cbToRead, fBlocking, pcbRead);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssIos_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
43af8389304c77c3d4db525de8907cb74207c380vboxsync PRTZIPTARIOSTREAM pThis = (PRTZIPTARIOSTREAM)pvThis;
43af8389304c77c3d4db525de8907cb74207c380vboxsync rc = rtZipTarFssIos_ReadOneSeg(pThis, pSgBuf->paSegs[0].pvSeg, pSgBuf->paSegs[0].cbSeg, fBlocking, pcbRead);
43af8389304c77c3d4db525de8907cb74207c380vboxsync for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
43af8389304c77c3d4db525de8907cb74207c380vboxsync rc = rtZipTarFssIos_ReadOneSeg(pThis, pSgBuf->paSegs[iSeg].pvSeg, pSgBuf->paSegs[iSeg].cbSeg, fBlocking, pcbReadSeg);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
43af8389304c77c3d4db525de8907cb74207c380vboxsync /* Cannot write to a read-only I/O stream. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync NOREF(pvThis); NOREF(off); NOREF(pSgBuf); NOREF(fBlocking); NOREF(pcbWritten);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssIos_Flush(void *pvThis)
43af8389304c77c3d4db525de8907cb74207c380vboxsync /* It's a read only stream, nothing dirty to flush. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssIos_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
43af8389304c77c3d4db525de8907cb74207c380vboxsync PRTZIPTARIOSTREAM pThis = (PRTZIPTARIOSTREAM)pvThis;
43af8389304c77c3d4db525de8907cb74207c380vboxsync /* When we've reached the end, read will be set to indicate it. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync int rc = RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, 0, fIntr, pfRetEvents);
43af8389304c77c3d4db525de8907cb74207c380vboxsync return RTVfsIoStrmPoll(pThis->hVfsIos, fEvents, cMillies, fIntr, pfRetEvents);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFssIos_Tell(void *pvThis, PRTFOFF poffActual)
43af8389304c77c3d4db525de8907cb74207c380vboxsync PRTZIPTARIOSTREAM pThis = (PRTZIPTARIOSTREAM)pvThis;
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Standard file operations.
43af8389304c77c3d4db525de8907cb74207c380vboxsync { /* Obj */
43af8389304c77c3d4db525de8907cb74207c380vboxsync "TarFsStream::IoStream",
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSOBJOPS,pfnClose}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFss_Close(void *pvThis)
43af8389304c77c3d4db525de8907cb74207c380vboxsync PRTZIPTARFSSTREAM pThis = (PRTZIPTARFSSTREAM)pvThis;
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFss_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
43af8389304c77c3d4db525de8907cb74207c380vboxsync PRTZIPTARFSSTREAM pThis = (PRTZIPTARFSSTREAM)pvThis;
43af8389304c77c3d4db525de8907cb74207c380vboxsync /* Take the lazy approach here, with the sideffect of providing some info
43af8389304c77c3d4db525de8907cb74207c380vboxsync that is actually kind of useful. */
43af8389304c77c3d4db525de8907cb74207c380vboxsync return RTVfsIoStrmQueryInfo(pThis->hVfsIos, pObjInfo, enmAddAttr);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * @interface_method_impl{RTVFSFSSTREAMOPS,pfnNext}
43af8389304c77c3d4db525de8907cb74207c380vboxsyncstatic DECLCALLBACK(int) rtZipTarFss_Next(void *pvThis, char **ppszName, RTVFSOBJTYPE *penmType, PRTVFSOBJ phVfsObj)
43af8389304c77c3d4db525de8907cb74207c380vboxsync PRTZIPTARFSSTREAM pThis = (PRTZIPTARFSSTREAM)pvThis;
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Dispense with the current object.
43af8389304c77c3d4db525de8907cb74207c380vboxsync pThis->pCurIosData->offFile = pThis->pCurIosData->cbFile;
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Check if we've already reached the end in some way.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Make sure the input stream is in the right place.
43af8389304c77c3d4db525de8907cb74207c380vboxsync while ( off >= 0
43af8389304c77c3d4db525de8907cb74207c380vboxsync int rc = RTVfsIoStrmSkip(pThis->hVfsIos, off - pThis->offNextHdr);
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** @todo Ignore if we're at the end of the stream? */
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Read the next header.
43af8389304c77c3d4db525de8907cb74207c380vboxsync int rc = RTVfsIoStrmRead(pThis->hVfsIos, &Hdr, sizeof(Hdr), true /*fBlocking*/, &cbRead);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Validate the header.
43af8389304c77c3d4db525de8907cb74207c380vboxsync /** @todo continue here... */
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Create an object of the appropriate type.
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Tar filesystem stream operations.
43af8389304c77c3d4db525de8907cb74207c380vboxsync { /* Obj */
43af8389304c77c3d4db525de8907cb74207c380vboxsync "TarFsStream",
43af8389304c77c3d4db525de8907cb74207c380vboxsyncRTDECL(int) RTZipTarFsStreamFromIoStream(RTVFSIOSTREAM hVfsIosIn, PRTVFSFSSTREAM phVfsFss)
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Input validation.
43af8389304c77c3d4db525de8907cb74207c380vboxsync RTFOFF const offStart = RTVfsIoStrmTell(hVfsIosIn);
43af8389304c77c3d4db525de8907cb74207c380vboxsync AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
43af8389304c77c3d4db525de8907cb74207c380vboxsync * Retain the input stream and create a new filesystem stream handle.
43af8389304c77c3d4db525de8907cb74207c380vboxsync int rc = RTVfsNewFsStream(&rtZipTarFssOps, sizeof(*pThis), NIL_RTVFS, NIL_RTSEMRW, &hVfsFss, (void **)&pThis);
43af8389304c77c3d4db525de8907cb74207c380vboxsync /* Don't check if it's a TAR stream here, do that in the
43af8389304c77c3d4db525de8907cb74207c380vboxsync rtZipTarFss_Next. */