fileio.cpp revision 6b9d73491d4d47c1ba8034e6f17a1002e63ae6f3
/* $Id$ */
/** @file
* innotek Portable Runtime - File I/O.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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
*/
int rtFileRecalcAndValidateFlags(unsigned *pfOpen)
{
/*
* 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 .
*/
if ( !(fOpen & RTFILE_O_ACCESS_MASK)
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
#else
|| (fOpen & ~RTFILE_O_VALID_MASK)
#endif
)
{
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;
}
/**
* 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;
}
/**
* 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.
*/
{
}
/**
* Determine the maximum file size.
*
* @returns IPRT status code.
* @param File Handle to the file.
* @param pcbMax Where to store the max file size.
* @see RTFileGetMaxSize.
*/
{
/*
* Save the current location
*/
if (RT_FAILURE(rc))
return rc;
/*
* Perform a binary search for the max file size.
*/
/** @todo r=bird: This isn't doing the trick for windows (at least not vista).
* Close to offHigh is returned regardless of NTFS or FAT32.
* We might have to make this code OS specific...
* In the worse case, we'll have to try GetVolumeInformationByHandle on vista and fall
* back on NtQueryVolumeInformationFile(,,,, FileFsAttributeInformation) else where, and
* check for known file system names. (For LAN shares we'll have to figure out the remote
* file system.) */
//uint64_t offHigh = INT64_MAX;
for (;;)
{
if (cbInterval == 0)
{
if (pcbMax)
}
if (RT_FAILURE(rc))
else
}
}
/**
* Copies a file given the handles to both files.
*
* @returns VBox Status code.
*
* @param FileSrc The source file. The file position is unaltered.
* @param FileDst The destination file.
* On successful returns the file position is at the end of the file.
* On failures the file position and size is undefined.
*/
{
}
/**
* Copies a file.
*
* @returns VERR_ALREADY_EXISTS if the destination file exists.
* @returns VBox Status code.
*
* @param pszSrc The path to the source file.
* @param pszDst The path to the destination file.
* This file will be created.
* @param fFlags Flags, any of the RTFILECOPY_FLAGS_ \#defines.
* @param pfnProgress Pointer to callback function for reporting progress.
* @param pvUser User argument to pass to pfnProgress along with the completion precentage.
*/
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.
*/
RTFILE_O_READ | (fFlags & RTFILECOPY_FLAGS_NO_SRC_DENY_WRITE ? 0 : RTFILE_O_DENY_WRITE) | RTFILE_O_OPEN);
if (RT_SUCCESS(rc))
{
RTFILE_O_READ | (fFlags & RTFILECOPY_FLAGS_NO_DST_DENY_WRITE ? 0 : RTFILE_O_DENY_WRITE) | RTFILE_O_OPEN);
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;
}
/**
* Copies a file given the handles to both files and
* provide progress callbacks.
*
* @returns VBox Status code.
*
* @param FileSrc The source file. The file position is unaltered.
* @param FileDst The destination file.
* On successful returns the file position is at the end of the file.
* On failures the file position and size is undefined.
* @param pfnProgress Pointer to callback function for reporting progress.
* @param pvUser User argument to pass to pfnProgress along with the completion precentage.
*/
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;
}