vfsmemory.cpp revision f4a0d55d0df98b8c6d9316681994aec8afce5491
/* $Id$ */
/** @file
* IPRT - Virtual File System, Memory Backed VFS.
*/
/*
* Copyright (C) 2010 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/vfslowlevel.h>
/*******************************************************************************
* Header Files *
*******************************************************************************/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The max extent size. */
#define RTVFSMEM_MAX_EXTENT_SIZE _2M
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Memory base object info.
*/
typedef struct RTVFSMEMBASE
{
/** The basic object info. */
} RTVFSMEMBASE;
/**
* Memory file extent.
*
* This stores part of the file content.
*/
typedef struct RTVFSMEMEXTENT
{
/** Extent list entry. */
/** The offset of this extent within the file. */
/** The size of the this extent. */
/** The data. */
/** Pointer to a memory file extent. */
typedef RTVFSMEMEXTENT *PRTVFSMEMEXTENT;
/**
* Memory file.
*/
typedef struct RTVFSMEMFILE
{
/** The base info. */
/** The current file position. */
/** Pointer to the current file extent. */
/** Linked list of file extents - RTVFSMEMEXTENT. */
/** The current extent size.
* This is slowly grown to RTVFSMEM_MAX_EXTENT_SIZE as the file grows. */
} RTVFSMEMFILE;
/** Pointer to a memory file. */
typedef RTVFSMEMFILE *PRTVFSMEMFILE;
/**
* @interface_method_impl{RTVFSOBJOPS,pfnClose}
*/
{
/*
* Free the extent list.
*/
{
}
return VINF_SUCCESS;
}
/**
* @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
*/
static DECLCALLBACK(int) rtVfsMemFile_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
switch (enmAddAttr)
{
case RTFSOBJATTRADD_NOTHING:
case RTFSOBJATTRADD_UNIX:
return VINF_SUCCESS;
default:
return VERR_NOT_SUPPORTED;
}
}
/**
* The slow paths of rtVfsMemFile_LocateExtent.
*
* @copydoc rtVfsMemFile_LocateExtent
*/
static PRTVFSMEMEXTENT rtVfsMemFile_LocateExtentSlow(PRTVFSMEMFILE pThis, uint64_t off, bool *pfHit)
{
/*
* Search from the start or the previously used extent. The heuristics
* are very very simple, but whatever.
*/
{
if (!pExtent)
{
*pfHit = false;
return NULL;
}
}
{
{
*pfHit = false;
return pExtent;
}
}
*pfHit = true;
return pExtent;
}
/**
* Locates the extent covering the specified offset, or then one before it.
*
* @returns The closest extent. NULL if off is 0 and there are no extent
* covering byte 0 yet.
* @param pThis The memory file.
* @param off The offset (0-positive).
* @param pfHit Where to indicate whether the extent is a
* direct hit (@c true) or just a closest match
* (@c false).
*/
DECLINLINE(PRTVFSMEMEXTENT) rtVfsMemFile_LocateExtent(PRTVFSMEMFILE pThis, uint64_t off, bool *pfHit)
{
/*
* The most likely case is that we're hitting the extent we used in the
* previous access or the one immediately following it.
*/
if (!pExtent)
{
if ( !pExtent
}
*pfHit = true;
return pExtent;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
*/
static DECLCALLBACK(int) rtVfsMemFile_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
{
/*
* Find the current position and check if it's within the file.
*/
{
if (pcbRead)
{
*pcbRead = 0;
return VINF_EOF;
}
return VERR_EOF;
}
{
if (!pcbRead)
return VERR_EOF;
}
else
/*
* Ok, we've got a valid stretch within the file. Do the reading.
*/
if (cbLeftToRead > 0)
{
bool fHit;
for (;;)
{
/*
* Do we hit an extent covering the the current file surface?
*/
if (fHit)
{
if (cbThisRead >= cbLeftToRead)
if (!cbLeftToRead)
break;
pbDst += cbThisRead;
if ( pNext
{
continue;
}
fHit = false;
}
/*
* No extent of this portion (sparse file).
*/
else if (pExtent)
else
if ( !pNext
else
if (!cbLeftToRead)
break;
pbDst += cbThisRead;
/* Go on and read content from the next extent. */
fHit = true;
}
}
return VINF_SUCCESS;
}
/**
* Allocates a new extent covering the ground at @a offUnsigned.
*
* @returns Pointer to the new extent on success, NULL if we're out of memory.
* @param pThis The memory file.
* @param offUnsigned The location to allocate the extent at.
* @param cbToWrite The number of bytes we're interested in writing
* starting at @a offUnsigned.
* @param pPrev The extention before @a offUnsigned. NULL if
* none.
*/
static PRTVFSMEMEXTENT rtVfsMemFile_AllocExtent(PRTVFSMEMFILE pThis, uint64_t offUnsigned, size_t cbToWrite,
{
/*
* Adjust the extent size if we haven't reached the max size yet.
*/
{
if (cbToWrite >= RTVFSMEM_MAX_EXTENT_SIZE)
{
cbNextExtent *= 2;
else
{
/* Make it a power of two (seeRTVfsMemorizeIoStreamAsFile). */
cbNextExtent = _4K;
cbNextExtent *= 2;
}
}
}
/*
* Figure out the size and position of the extent we're adding.
*/
if (pNext)
{
if (cbMaxExtent < cbExtent)
}
/*
* Allocate, initialize and insert the new extent.
*/
if (pNew)
{
if (pPrev)
else
}
/** @todo retry with minimum size. */
return pNew;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
*/
static DECLCALLBACK(int) rtVfsMemFile_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
{
/*
* Validate the write and set up the write loop.
*/
if (!cbLeftToWrite)
return VINF_SUCCESS; /* pcbWritten is already 0. */
return VERR_OUT_OF_RANGE;
int rc = VINF_SUCCESS;
bool fHit;
for (;;)
{
/*
* If we didn't hit an extent, allocate one (unless it's all zeros).
*/
if (!fHit)
{
/* Skip leading zeros if there is a whole bunch of them. */
if (!pbSrcNZ)
{
cbLeftToWrite = 0;
break;
}
{
offUnsigned += cbZeros;
cbLeftToWrite -= cbZeros;
break;
}
fHit = true;
if (!pExtent)
{
rc = VERR_NO_MEMORY;
break;
}
}
/*
* Copy the source data into the current extent.
*/
if (cbThisWrite > cbLeftToWrite)
if (!cbLeftToWrite)
break;
pbSrc += cbThisWrite;
/*
* Advance to the next extent.
*/
else
fHit = false;
}
/*
* Update the state, set return value and return.
* Note! There must be no alternative exit path from the loop above.
*/
if (pcbWritten)
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
*/
static DECLCALLBACK(int) rtVfsMemFile_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
{
int rc;
if (fEvents != RTPOLL_EVT_ERROR)
{
rc = VINF_SUCCESS;
}
else
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
*/
static DECLCALLBACK(int) rtVfsMemFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
{
if (pAccessTime)
if (pModificationTime)
if (pChangeTime)
if (pBirthTime)
return VINF_SUCCESS;
}
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
*/
{
return VINF_SUCCESS;
}
/**
* @interface_method_impl{RTVFSFILEOPS,pfnSeek}
*/
static DECLCALLBACK(int) rtVfsMemFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
{
/*
* Seek relative to which position.
*/
switch (uMethod)
{
case RTFILE_SEEK_BEGIN:
offWrt = 0;
break;
case RTFILE_SEEK_CURRENT:
break;
case RTFILE_SEEK_END:
break;
default:
return VERR_INTERNAL_ERROR_5;
}
/*
* Calc new position, take care to stay without bounds.
*/
if (offSeek == 0)
else if (offSeek > 0)
{
|| offNew > RTFOFF_MAX)
offNew = RTFOFF_MAX;
}
else
offNew = 0;
/*
* Update the state and set return value.
*/
*poffActual = offNew;
return VINF_SUCCESS;
}
/**
* @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
*/
{
}
/**
* Standard file operations.
*/
{
{ /* Stream */
{ /* Obj */
"MemFile",
},
NULL /*Skip*/,
NULL /*ZeroFill*/,
},
/*RTVFSIOFILEOPS_FEAT_NO_AT_OFFSET*/ 0,
{ /* ObjSet */
},
};
RTDECL(int) RTVfsMemorizeIoStreamAsFile(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, PRTVFSFILE phVfsFile)
{
/*
* Create a memory file instance and try set the extension size to match
* the length of the I/O stream.
*/
if (RT_SUCCESS(rc))
{
rc = RTVfsNewFile(&g_rtVfsStdFileOps, sizeof(*pThis), fFlags, NIL_RTVFS, NIL_RTVFSLOCK, &hVfsFile, (void **)&pThis);
if (RT_SUCCESS(rc))
{
else
/*
* Copy the stream.
*/
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
return rc;
}