vfsstdfile.cpp revision c7814cf6e1240a519cbec0441e033d0e2470ed00
/* $Id$ */
/** @file
* IPRT - Virtual File System, Standard File Implementation.
*/
/*
* Copyright (C) 2010-2011 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>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Private data of a standard file.
*/
typedef struct RTVFSSTDFILE
{
/** The file handle. */
/** Whether to leave the handle open when the VFS handle is closed. */
bool fLeaveOpen;
} RTVFSSTDFILE;
/** Pointer to the private data of a standard file. */
typedef RTVFSSTDFILE *PRTVFSSTDFILE;
/**
* @interface_method_impl{RTVFSOBJOPS,pfnClose}
*/
{
int rc;
if (!pThis->fLeaveOpen)
else
rc = VINF_SUCCESS;
return rc;
}
/**
* @interface_method_impl{RTVFSOBJOPS,pfnQueryInfo}
*/
static DECLCALLBACK(int) rtVfsStdFile_QueryInfo(void *pvThis, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAddAttr)
{
}
/**
* RTFileRead and RTFileReadAt does not return VINF_EOF or VINF_TRY_AGAIN, this
* function tries to fix this as best as it can.
*
* This fixing can be subject to races if some other thread or process is
* modifying the file size between the read and our size query here.
*
* @returns VINF_SUCCESS, VINF_EOF or VINF_TRY_AGAIN.
* @param pThis The instance data.
* @param off The offset parameter.
* @param cbToRead The number of bytes attempted read .
* @param cbActuallyRead The number of bytes actually read.
*/
DECLINLINE(int) rtVfsStdFile_ReadFixRC(PRTVFSSTDFILE pThis, RTFOFF off, size_t cbToRead, size_t cbActuallyRead)
{
/* If the read returned less bytes than requested, it means the end of the
file has been reached. */
if (cbToRead > cbActuallyRead)
return VINF_EOF;
/* The other case here is the very special zero byte read at the end of the
file, where we're supposed to indicate EOF. */
if (cbToRead > 0)
return VINF_SUCCESS;
if (RT_FAILURE(rc))
return rc;
if (off >= 0)
else
{
if (RT_FAILURE(rc))
return rc;
}
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnRead}
*/
static DECLCALLBACK(int) rtVfsStdFile_Read(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
{
int rc;
{
if (off < 0)
else
}
else
{
rc = VINF_SUCCESS;
{
if (off < 0)
else
if (RT_FAILURE(rc))
break;
if (off >= 0)
break;
}
if (pcbRead)
{
if (rc == VINF_SUCCESS)
}
}
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
*/
static DECLCALLBACK(int) rtVfsStdFile_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
{
int rc;
{
if (off < 0)
else
rc = RTFileWriteAt(pThis->hFile, off, pSgBuf->paSegs[0].pvSeg, pSgBuf->paSegs[0].cbSeg, pcbWritten);
}
else
{
rc = VINF_SUCCESS;
{
cbWrittenSeg = 0;
if (off < 0)
else
{
}
if (RT_FAILURE(rc))
break;
if (pcbWritten)
{
if (cbWrittenSeg != cbSeg)
break;
}
}
if (pcbWritten)
*pcbWritten = cbWritten;
}
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnFlush}
*/
{
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnPollOne}
*/
static DECLCALLBACK(int) rtVfsStdFile_PollOne(void *pvThis, uint32_t fEvents, RTMSINTERVAL cMillies, bool fIntr,
{
int rc;
if (fEvents != RTPOLL_EVT_ERROR)
{
rc = VINF_SUCCESS;
}
else if (fIntr)
else
{
do
while ( rc == VERR_INTERRUPTED
&& !fIntr
if (rc == VERR_INTERRUPTED)
rc = VERR_TIMEOUT;
}
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnTell}
*/
{
if (RT_SUCCESS(rc))
return rc;
}
/**
* @interface_method_impl{RTVFSIOSTREAMOPS,pfnSkip}
*/
{
}
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnMode}
*/
{
if (fMask != ~RTFS_TYPE_MASK)
{
#if 0
if (RT_FAILURE(rc))
return rc;
#else
if (RT_FAILURE(rc))
return rc;
#endif
}
}
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnSetTimes}
*/
static DECLCALLBACK(int) rtVfsStdFile_SetTimes(void *pvThis, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
{
}
/**
* @interface_method_impl{RTVFSOBJSETOPS,pfnSetOwner}
*/
{
#if 0
#else
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* @interface_method_impl{RTVFSFILEOPS,pfnSeek}
*/
static DECLCALLBACK(int) rtVfsStdFile_Seek(void *pvThis, RTFOFF offSeek, unsigned uMethod, PRTFOFF poffActual)
{
if (RT_SUCCESS(rc))
*poffActual = offActual;
return rc;
}
/**
* @interface_method_impl{RTVFSFILEOPS,pfnQuerySize}
*/
{
}
/**
* Standard file operations.
*/
{
{ /* Stream */
{ /* Obj */
"StdFile",
},
0,
NULL /*ZeroFill*/,
},
0,
{ /* ObjSet */
},
};
RTDECL(int) RTVfsFileFromRTFile(RTFILE hFile, uint64_t fOpen, bool fLeaveOpen, PRTVFSFILE phVfsFile)
{
/*
* Check the handle validity.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Set up some fake fOpen flags.
*/
if (!fOpen)
/*
* Create the handle.
*/
rc = RTVfsNewFile(&g_rtVfsStdFileOps, sizeof(RTVFSSTDFILE), fOpen, NIL_RTVFS, NIL_RTVFSLOCK, &hVfsFile, (void **)&pThis);
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
RTDECL(int) RTVfsIoStrmFromRTFile(RTFILE hFile, uint64_t fOpen, bool fLeaveOpen, PRTVFSIOSTREAM phVfsIos)
{
if (RT_SUCCESS(rc))
return rc;
}