fileio.cpp revision 6861ffabf1e22c47f385582a161b16bff1ee6922
/* $Id$ */
/** @file
* IPRT - File I/O.
*/
/*
* Copyright (C) 2006-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 *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Set of forced set open flags for files opened read-only. */
static unsigned g_fOpenReadSet = 0;
/** Set of forced cleared open flags for files opened read-only. */
static unsigned g_fOpenReadMask = 0;
/** Set of forced set open flags for files opened write-only. */
static unsigned g_fOpenWriteSet = 0;
/** Set of forced cleared open flags for files opened write-only. */
static unsigned g_fOpenWriteMask = 0;
/** Set of forced set open flags for files opened read-write. */
static unsigned g_fOpenReadWriteSet = 0;
/** Set of forced cleared open flags for files opened read-write. */
static unsigned g_fOpenReadWriteMask = 0;
/**
* Force the use of open flags for all files opened after the setting is
* changed. The caller is responsible for not causing races with RTFileOpen().
*
* @returns iprt status code.
* @param fSet Open flags to be forced set.
* @param fMask Open flags to be masked out.
*/
{
/*
* For now allow only RTFILE_O_WRITE_THROUGH. The other flags either
* make no sense in this context or are not useful to apply to all files.
*/
return VERR_INVALID_PARAMETER;
switch (fOpenForAccess)
{
case RTFILE_O_READ:
break;
case RTFILE_O_WRITE:
break;
case RTFILE_O_READWRITE:
break;
default:
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}
/**
* Adjusts and validates the flags.
*
* The adjustments are made according to the wishes specified using the RTFileSetForceFlags API.
*
* @returns IPRT status code.
* @param pfOpen Pointer to the user specified flags on input.
* Updated on successful return.
* @internal
*/
{
/*
* Recalc.
*/
switch (fOpen & RTFILE_O_ACCESS_MASK)
{
case RTFILE_O_READ:
fOpen |= g_fOpenReadSet;
fOpen &= ~g_fOpenReadMask;
break;
case RTFILE_O_WRITE:
fOpen |= g_fOpenWriteSet;
fOpen &= ~g_fOpenWriteMask;
break;
case RTFILE_O_READWRITE:
break;
default:
return VERR_INVALID_PARAMETER;
}
/*
* Validate .
*/
AssertMsgReturn(fOpen & RTFILE_O_ACCESS_MASK, ("Missing RTFILE_O_READ/WRITE: fOpen=%#llx\n", fOpen), VERR_INVALID_PARAMETER);
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
AssertMsgReturn(!(fOpen & (~(uint64_t)RTFILE_O_VALID_MASK | RTFILE_O_NON_BLOCK)), ("%#llx\n", fOpen), VERR_INVALID_PARAMETER);
#else
AssertMsgReturn(!(fOpen & ~(uint64_t)RTFILE_O_VALID_MASK), ("%#llx\n", fOpen), VERR_INVALID_PARAMETER);
#endif
AssertMsgReturn((fOpen & (RTFILE_O_TRUNCATE | RTFILE_O_WRITE)) != RTFILE_O_TRUNCATE, ("%#llx\n", fOpen), VERR_INVALID_PARAMETER);
switch (fOpen & RTFILE_O_ACTION_MASK)
{
case 0: /* temporarily */
AssertMsgFailed(("Missing RTFILE_O_OPEN/CREATE*! (continuable assertion)\n"));
fOpen |= RTFILE_O_OPEN;
break;
case RTFILE_O_OPEN:
AssertMsgReturn(!(RTFILE_O_NOT_CONTENT_INDEXED & fOpen), ("%#llx\n", fOpen), VERR_INVALID_PARAMETER);
case RTFILE_O_OPEN_CREATE:
case RTFILE_O_CREATE:
case RTFILE_O_CREATE_REPLACE:
break;
default:
return VERR_INVALID_PARAMETER;
}
switch (fOpen & RTFILE_O_DENY_MASK)
{
case 0: /* temporarily */
AssertMsgFailed(("Missing RTFILE_O_DENY_*! (continuable assertion)\n"));
break;
case RTFILE_O_DENY_NONE:
case RTFILE_O_DENY_READ:
case RTFILE_O_DENY_WRITE:
case RTFILE_O_DENY_NOT_DELETE:
break;
default:
return VERR_INVALID_PARAMETER;
}
/* done */
return VINF_SUCCESS;
}
/**
* Read bytes from a file at a given offset.
* This function may modify the file position.
*
* @returns iprt status code.
* @param File Handle to the file.
* @param off Where to read.
* @param pvBuf Where to put the bytes we read.
* @param cbToRead How much to read.
* @param *pcbRead How much we actually read.
* If NULL an error will be returned for a partial read.
*/
{
if (RT_SUCCESS(rc))
return rc;
}
/**
* Read bytes from a file at a given offset into a S/G buffer.
* This function may modify the file position.
*
* @returns iprt status code.
* @param hFile Handle to the file.
* @param off Where to read.
* @param pSgBuf Pointer to the S/G buffer to read into.
* @param cbToRead How much to read.
* @param pcbRead How much we actually read.
* If NULL an error will be returned for a partial read.
*/
RTR3DECL(int) RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
{
int rc = VINF_SUCCESS;
while (cbToRead)
{
size_t cbThisRead = 0;
if (RT_SUCCESS(rc))
cbRead += cbThisRead;
if ( RT_FAILURE(rc)
|| ( cbThisRead < cbBuf
&& pcbRead))
break;
}
if (pcbRead)
return rc;
}
/**
* Write bytes to a file at a given offset.
* This function may modify the file position.
*
* @returns iprt status code.
* @param File Handle to the file.
* @param off Where to write.
* @param pvBuf What to write.
* @param cbToWrite How much to write.
* @param *pcbWritten How much we actually wrote.
* If NULL an error will be returned for a partial write.
*/
RTR3DECL(int) RTFileWriteAt(RTFILE File, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
{
if (RT_SUCCESS(rc))
return rc;
}
/**
* Write bytes from a S/G buffer to a file at a given offset.
* This function may modify the file position.
*
* @returns iprt status code.
* @param hFile Handle to the file.
* @param off Where to write.
* @param pSgBuf What to write.
* @param cbToWrite How much to write.
* @param pcbWritten How much we actually wrote.
* If NULL an error will be returned for a partial write.
*/
RTR3DECL(int) RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
{
int rc = VINF_SUCCESS;
while (cbToWrite)
{
size_t cbThisWritten = 0;
if (RT_SUCCESS(rc))
if ( RT_FAILURE(rc)
|| ( cbThisWritten < cbBuf
&& pcbWritten))
break;
}
if (pcbWritten)
*pcbWritten = cbWritten;
return rc;
}
/**
* Gets the current file position.
*
* @returns File offset.
* @returns ~0UUL on failure.
* @param File File handle.
*/
{
/*
* Call the seek api to query the stuff.
*/
if (RT_SUCCESS(rc))
return off;
return ~0ULL;
}
/**
* Determine the maximum file size.
*
* @returns The max size of the file.
* -1 on failure, the file position is undefined.
* @param File Handle to the file.
* @see RTFileGetMaxSizeEx.
*/
{
}
{
}
RTDECL(int) RTFileCopyEx(const char *pszSrc, const char *pszDst, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser)
{
/*
* Validate input.
*/
AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
/*
* Open the files.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Call the ByHandles version and let it do the job.
*/
/*
* Close the files regardless of the result.
* Don't bother cleaning up or anything like that.
*/
}
}
return rc;
}
RTDECL(int) RTFileCopyByHandlesEx(RTFILE FileSrc, RTFILE FileDst, PFNRTPROGRESS pfnProgress, void *pvUser)
{
/*
* Validate input.
*/
AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
/*
* Save file offset.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Get the file size.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Allocate buffer.
*/
{
}
else
{
}
if (pbBuf)
{
/*
* Seek to the start of each file
* and set the size of the destination file.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
/*
* Copy loop.
*/
unsigned uPercentage = 0;
{
/* copy block */
if (RT_FAILURE(rc))
break;
if (RT_FAILURE(rc))
break;
/* advance */
{
while (offNextPercent < off)
{
uPercentage++;
}
if (RT_FAILURE(rc))
break;
}
}
#if 0
/*
* Copy OS specific data (EAs and stuff).
*/
#endif
/* 100% */
}
}
}
else
rc = VERR_NO_MEMORY;
/*
* Restore source position.
*/
return rc;
}
{
}
{
}
RTDECL(int) RTFileCompareEx(const char *pszFile1, const char *pszFile2, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser)
{
/*
* Validate input.
*/
AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
/*
* Open the files.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Call the ByHandles version and let it do the job.
*/
/* Clean up */
}
}
return rc;
}
RTDECL(int) RTFileCompareByHandlesEx(RTFILE hFile1, RTFILE hFile2, uint32_t fFlags, PFNRTPROGRESS pfnProgress, void *pvUser)
{
/*
* Validate input.
*/
AssertMsgReturn(!pfnProgress || VALID_PTR(pfnProgress), ("pfnProgress=%p\n", pfnProgress), VERR_INVALID_PARAMETER);
/*
* Compare the file sizes first.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
return VERR_NOT_EQUAL;
/*
* Allocate buffer.
*/
{
}
else
{
}
{
/*
* Seek to the start of each file
* and set the size of the destination file.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Compare loop.
*/
unsigned uPercentage = 0;
{
/* read the blocks */
if (RT_FAILURE(rc))
break;
if (RT_FAILURE(rc))
break;
/* compare */
{
rc = VERR_NOT_EQUAL;
break;
}
/* advance */
{
while (offNextPercent < off)
{
uPercentage++;
}
if (RT_FAILURE(rc))
break;
}
}
#if 0
/*
* Compare OS specific data (EAs and stuff).
*/
if (RT_SUCCESS(rc))
#endif
/* 100% */
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}