fileio-posix.cpp revision 631688bc0cb9e4b091b6d199d620be03a03858cb
/* $Id$ */
/** @file
* IPRT - File I/O, POSIX, Part 1.
*/
/*
* Copyright (C) 2006-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 *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_FILE
#include <errno.h>
#include <fcntl.h>
#ifdef _MSC_VER
# include <io.h>
# include <stdio.h>
#else
# include <unistd.h>
#endif
#ifdef RT_OS_LINUX
#endif
# include <io.h>
#endif
#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
#endif
#ifdef RT_OS_SOLARIS
# include <stropts.h>
#endif /* RT_OS_SOLARIS */
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Default file permissions for newly created files. */
#else
# define RT_FILE_PERMISSION (00600)
#endif
{
bool fRc = false;
char const *pszNativePath;
if (RT_SUCCESS(rc))
{
struct stat s;
}
return fRc;
}
{
/*
* Validate input.
*/
*pFile = NIL_RTFILE;
/*
* Merge forced open flags and validate them.
*/
if (RT_FAILURE(rc))
return rc;
#ifndef O_NONBLOCK
if (fOpen & RTFILE_O_NON_BLOCK)
{
return VERR_INVALID_PARAMETER;
}
#endif
/*
* Calculate open mode flags.
*/
int fOpenMode = 0;
#ifdef O_BINARY
#endif
#ifdef O_LARGEFILE
#endif
#ifdef O_NOINHERIT
if (!(fOpen & RTFILE_O_INHERIT))
fOpenMode |= O_NOINHERIT;
#endif
#ifdef O_CLOEXEC
static int s_fHave_O_CLOEXEC = 0; /* {-1,0,1}; since Linux 2.6.23 */
#endif
#ifdef O_NONBLOCK
if (fOpen & RTFILE_O_NON_BLOCK)
fOpenMode |= O_NONBLOCK;
#endif
#ifdef O_SYNC
if (fOpen & RTFILE_O_WRITE_THROUGH)
#endif
#if defined(O_DIRECT) && defined(RT_OS_LINUX)
/* O_DIRECT is mandatory to get async I/O working on Linux. */
if (fOpen & RTFILE_O_ASYNC_IO)
#endif
/* Disable the kernel cache. */
if (fOpen & RTFILE_O_NO_CACHE)
#endif
switch (fOpen & RTFILE_O_ACTION_MASK)
{
case RTFILE_O_OPEN: break;
case RTFILE_O_CREATE_REPLACE: fOpenMode |= O_CREAT | O_TRUNC; break; /** @todo replacing needs fixing, this is *not* a 1:1 mapping! */
}
if (fOpen & RTFILE_O_TRUNCATE)
switch (fOpen & RTFILE_O_ACCESS_MASK)
{
case RTFILE_O_READ:
break;
case RTFILE_O_WRITE:
break;
case RTFILE_O_READWRITE:
break;
default:
return VERR_INVALID_PARAMETER;
}
/* File mode. */
/** @todo sharing! */
/*
*/
char const *pszNativeFilename;
if (RT_FAILURE(rc))
return (rc);
#ifdef O_CLOEXEC
&& s_fHave_O_CLOEXEC == 0)
{
{
s_fHave_O_CLOEXEC = -1;
}
else if (fh >= 0)
}
#endif
if (fh >= 0)
{
iErr = 0;
/*
* Mark the file handle close on exec, unless inherit is specified.
*/
if ( !(fOpen & RTFILE_O_INHERIT)
#ifdef O_NOINHERIT
#endif
#ifdef O_CLOEXEC
&& s_fHave_O_CLOEXEC <= 0
#endif
)
/*
* Switch direct I/O on now if requested and required.
*/
#if defined(RT_OS_DARWIN) \
|| (defined(RT_OS_SOLARIS) && !defined(IN_GUEST))
{
# if defined(RT_OS_DARWIN)
# else
# endif
}
#endif
/*
* Implement / emulate file sharing.
*
* We need another mode which allows skipping this stuff completely
* and do things the UNIX way. So for the present this is just a debug
* aid that can be enabled by developers too lazy to test on Windows.
*/
#if 0 && defined(RT_OS_LINUX)
if (iErr == 0)
{
/* This approach doesn't work because only knfsd checks for these
buggers. :-( */
int iLockOp;
switch (fOpen & RTFILE_O_DENY_MASK)
{
default:
AssertFailed();
case RTFILE_O_DENY_NONE:
case RTFILE_O_DENY_NOT_DELETE:
break;
case RTFILE_O_DENY_READ:
break;
case RTFILE_O_DENY_WRITE:
break;
break;
}
if (iErr != 0)
}
#endif /* 0 && RT_OS_LINUX */
#if defined(DEBUG_bird) && !defined(RT_OS_SOLARIS)
if (iErr == 0)
{
/* This emulation is incomplete but useful. */
switch (fOpen & RTFILE_O_DENY_MASK)
{
default:
AssertFailed();
case RTFILE_O_DENY_NONE:
case RTFILE_O_DENY_NOT_DELETE:
case RTFILE_O_DENY_READ:
break;
case RTFILE_O_DENY_WRITE:
if (fOpen & RTFILE_O_WRITE)
{
if (iErr != 0)
}
break;
}
}
#endif
#ifdef RT_OS_SOLARIS
#endif
/*
* We're done.
*/
if (iErr == 0)
{
LogFlow(("RTFileOpen(%p:{%RTfile}, %p:{%s}, %#llx): returns %Rrc\n",
return VINF_SUCCESS;
}
}
return RTErrConvertFromErrno(iErr);
}
{
|| fAccess == RTFILE_O_WRITE
|| fAccess == RTFILE_O_READWRITE,
}
{
if (hFile == NIL_RTFILE)
return VINF_SUCCESS;
return VINF_SUCCESS;
return RTErrConvertFromErrno(errno);
}
{
if (uNative < 0)
{
*pFile = NIL_RTFILE;
return VERR_INVALID_HANDLE;
}
return VINF_SUCCESS;
}
{
}
{
int fd;
switch (enmStdHandle)
{
case RTHANDLESTD_INPUT: fd = 0; break;
break;
default:
}
if (rc == -1)
return NIL_RTFILE;
}
{
char const *pszNativeFilename;
if (RT_SUCCESS(rc))
{
if (unlink(pszNativeFilename) != 0)
}
return rc;
}
{
static const unsigned aSeekRecode[] =
{
};
/*
* Validate input.
*/
if (uMethod > RTFILE_SEEK_END)
{
return VERR_INVALID_PARAMETER;
}
/* check that within off_t range. */
{
AssertMsgFailed(("64-bit search not supported\n"));
return VERR_NOT_SUPPORTED;
}
if (offCurrent != ~0)
{
if (poffActual)
return VINF_SUCCESS;
}
return RTErrConvertFromErrno(errno);
}
{
if (cbToRead <= 0)
return VINF_SUCCESS;
/*
* Attempt read.
*/
if (cbRead >= 0)
{
if (pcbRead)
/* caller can handle partial read. */
else
{
/* Caller expects all to be read. */
{
if (cbReadPart <= 0)
{
if (cbReadPart == 0)
return VERR_EOF;
return RTErrConvertFromErrno(errno);
}
cbRead += cbReadPart;
}
}
return VINF_SUCCESS;
}
return RTErrConvertFromErrno(errno);
}
{
if (cbToWrite <= 0)
return VINF_SUCCESS;
/*
* Attempt write.
*/
if (cbWritten >= 0)
{
if (pcbWritten)
/* caller can handle partial write. */
*pcbWritten = cbWritten;
else
{
/* Caller expects all to be write. */
{
ssize_t cbWrittenPart = write(RTFileToNative(hFile), (const char *)pvBuf + cbWritten, cbToWrite - cbWritten);
if (cbWrittenPart <= 0)
return RTErrConvertFromErrno(errno);
}
}
return VINF_SUCCESS;
}
return RTErrConvertFromErrno(errno);
}
{
/*
* Validate offset.
*/
&& (cbSize >> 32) != 0)
{
return VERR_NOT_SUPPORTED;
}
#if defined(_MSC_VER) || (defined(RT_OS_OS2) && (!defined(__INNOTEK_LIBC__) || __INNOTEK_LIBC__ < 0x006))
#else
/* This relies on a non-standard feature of FreeBSD, Linux, and OS/2
* LIBC v0.6 and higher. (SuS doesn't define ftruncate() and size bigger
* than the file.)
*/
#endif
return VINF_SUCCESS;
return RTErrConvertFromErrno(errno);
}
{
/*
* Ask fstat() first.
*/
{
#if defined(RT_OS_SOLARIS)
#elif defined(RT_OS_FREEBSD)
#else
#endif
)
return VINF_SUCCESS;
/*
* It could be a block device. Try determin the size by I/O control
* query or seek.
*/
#ifdef RT_OS_DARWIN
{
{
return VINF_SUCCESS;
}
}
/* must be a block device, fail on failure. */
#elif defined(RT_OS_SOLARIS)
{
return VINF_SUCCESS;
}
/* might not be a block device. */
return VINF_SUCCESS;
#elif defined(RT_OS_FREEBSD)
{
return VINF_SUCCESS;
}
/* might not be a block device. */
return VINF_SUCCESS;
#else
/* PORTME! Avoid this path when possible. */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
return rc2;
}
#endif
}
return RTErrConvertFromErrno(errno);
}
{
/*
* Save the current location
*/
if (RT_FAILURE(rc))
return rc;
/*
* Perform a binary search for the max file size.
*/
/** @todo Unfortunately this does not work for certain file system types,
* for instance cifs mounts. Even worse, statvfs.f_fsid returns 0 for such
* file systems. */
//uint64_t offHigh = INT64_MAX;
for (;;)
{
if (cbInterval == 0)
{
if (pcbMax)
}
if (RT_FAILURE(rc))
else
}
}
{
if (hFile != NIL_RTFILE)
{
if (fFlags >= 0)
return true;
}
return false;
}
{
return RTErrConvertFromErrno(errno);
return VINF_SUCCESS;
}
RTR3DECL(int) RTFileIoCtl(RTFILE hFile, unsigned long ulRequest, void *pvData, unsigned cbData, int *piRet)
{
if (piRet)
}
{
/*
* Normalize the mode and call the API.
*/
if (!rtFsModeIsValid(fMode))
return VERR_INVALID_PARAMETER;
{
return rc;
}
return VINF_SUCCESS;
}
{
/*
* Validate input.
*/
AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
/*
* Take common cause with RTPathRename.
*/
LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n",
return rc;
}