fileio.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* $Id$ */
/** @file
* InnoTek Portable Runtime - File I/O.
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* 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 .
*/
|| !(fOpen & RTFILE_O_ACCESS_MASK)
)
{
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.
*/
RTR3DECL(int) RTFileReadAt(RTFILE File, RTFOFF off, void *pvBuf, unsigned cbToRead, unsigned *pcbRead)
{
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, unsigned cbToWrite, unsigned *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;
}
/**
* 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 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, 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;
}
/**
* 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;
}