vbsf.cpp revision c9a2df6a889ca4118cc41c73ef67f4e559731dca
/* $Id$ */
/** @file
* Shared Folders - VBox Shared Folders.
*/
/*
* 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.
*/
#ifdef UNITTEST
# include "testcase/tstSharedFolderService.h"
#endif
#include "mappings.h"
#include "vbsf.h"
#include "shflhandle.h"
#ifdef RT_OS_DARWIN
#endif
#ifdef UNITTEST
# include "teststubs.h"
#endif
// never follow symbolic links */
//#define SHFL_RT_LINK(pClient) ((pClient)->fu32Flags & SHFL_CF_SYMLINKS ? RTPATH_F_ON_LINK : RTPATH_F_FOLLOW_LINK)
/**
* @todo find a better solution for supporting the execute bit for non-windows
* guests on windows host. Search for "0111" to find all the relevant places.
*/
{
/* Do not strip root. */
char *s = pszFullPath + cbFullPathRoot;
char *delimSecondLast = NULL;
for (;;)
{
cp = RTStrGetCp(s);
{
break;
}
if (cp == RTPATH_DELIMITER)
{
{
}
delimLast = s;
}
s = RTStrNextCp(s);
}
if (cp == 0)
{
if (delimLast + 1 == s)
{
if (delimSecondLast)
{
*delimSecondLast = 0;
}
else if (delimLast)
{
*delimLast = 0;
}
}
else
{
if (delimLast)
{
*delimLast = 0;
}
}
}
}
{
int rc = VERR_FILE_NOT_FOUND;
char szWildCard[4];
cbDirEntry = 4096;
if (pDirEntry == 0)
{
AssertFailed();
return VERR_NO_MEMORY;
}
/** @todo this is quite inefficient, especially for directories with many files */
*(pszStartComponent-1) = 0;
szWildCard[0] = RTPATH_DELIMITER;
szWildCard[2] = 0;
if (RT_FAILURE(rc))
goto end;
for (;;)
{
rc = RTDirReadEx(hSearch, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
if (rc == VERR_NO_MORE_FILES)
break;
if ( rc != VINF_SUCCESS
&& rc != VWRN_NO_DIRENT_INFO)
{
AssertFailed();
if ( rc == VERR_NO_TRANSLATION
|| rc == VERR_INVALID_UTF8_ENCODING)
continue;
break;
}
{
rc = VINF_SUCCESS;
break;
}
}
end:
if (RT_FAILURE(rc))
if (pDirEntry)
if (hSearch)
return rc;
}
{
int rc = VINF_SUCCESS;
/* The pUtf8Path is what the guest sent. Verify that the path is within the root.
* Count '..' and other path components and check that we do not go over the root.
*/
size_t i = 0;
int cComponents = 0; /* How many normal path components. */
int cParentDirs = 0; /* How many '..' components. */
for (;;)
{
/* Skip leading path delimiters. */
while ( i < cbPath
i++;
if (i >= cbPath)
break;
/* Check if that is a dot component. */
int cDots = 0;
{
cDots++;
i++;
}
{
cParentDirs++;
}
else if ( cDots == 1
{
/* Single dot, nothing changes. */
}
else
{
/* Skip this component. */
while ( i < cbPath
i++;
cComponents++;
}
/* Verify counters for every component. */
if (cParentDirs > cComponents)
{
break;
}
}
return rc;
}
bool fWildCard = false, bool fPreserveLastComponent = false)
{
int rc = VINF_SUCCESS;
char *pszFullPath = NULL;
if ( !pszRoot
{
Log(("vbsfBuildFullPath: invalid root!\n"));
return VERR_INVALID_PARAMETER;
}
{
/* Verify that the path is under the root directory. */
if (RT_SUCCESS(rc))
{
if (!utf8FullPath)
{
rc = VERR_NO_MEMORY;
*ppszFullPath = NULL;
}
else
{
if (pcbFullPathRoot)
}
}
else
{
}
}
else
{
#ifdef RT_OS_DARWIN
/** @todo This belongs in rtPathToNative or in the windows shared folder file system driver...
* The question is simply whether the NFD normalization is actually applied on a (virtual) file
* system level in darwin, or just by the user mode application libs. */
// Is 8 times length enough for decomposed in worst case...?
if (!pPath)
{
rc = VERR_NO_MEMORY;
return rc;
}
rangeCharacters.location = 0;
#endif
/* Client sends us UCS2, so convert it to UTF8. */
Log(("Root %s path %.*ls\n", pszRoot, pPath->u16Length/sizeof(pPath->String.ucs2[0]), pPath->String.ucs2));
/* Allocate buffer that will be able to contain the root prefix and
* the pPath converted to UTF8. Expect a 2 bytes UCS2 to be converted
* to 8 bytes UTF8 in the worst case.
*/
if (!pszFullPath)
{
rc = VERR_NO_MEMORY;
}
else
{
char *pszDst = pszFullPath;
{
cbDst++;
}
if (pcbFullPathRoot)
{
/* Convert and copy components. */
/* Correct path delimiters */
{
while (*pwszSrc)
{
pwszSrc++;
}
}
if (*pwszSrc == RTPATH_DELIMITER)
pwszSrc++; /* we already appended a delimiter to the first part */
if (RT_FAILURE(rc))
{
AssertFailed();
#ifdef RT_OS_DARWIN
#endif
return rc;
}
/* Verify that the path is under the root directory. */
if (RT_FAILURE(rc))
{
#ifdef RT_OS_DARWIN
#endif
return rc;
}
}
/* Nul terminate the string */
*pszDst = 0;
}
#ifdef RT_OS_DARWIN
#endif
}
if (RT_SUCCESS(rc))
{
/* When the host file system is case sensitive and the guest expects a case insensitive fs, then problems can occur */
{
char *pszLastComponent = NULL;
if (fWildCard || fPreserveLastComponent)
{
/* strip off the last path component, that has to be preserved: contains the wildcard(s) or a 'rename' target. */
while (pszSrc > pszFullPath)
{
if (*pszSrc == RTPATH_DELIMITER)
break;
pszSrc--;
}
if (*pszSrc == RTPATH_DELIMITER)
{
bool fHaveWildcards = false;
while (*psz)
{
{
fHaveWildcards = true;
break;
}
psz++;
}
if (fHaveWildcards || fPreserveLastComponent)
{
*pszLastComponent = 0;
}
}
}
/** @todo don't check when creating files or directories; waste of time */
{
/* Find partial path that's valid */
while (pszSrc > pszFullPath)
{
if (*pszSrc == RTPATH_DELIMITER)
{
*pszSrc = 0;
if (rc == VINF_SUCCESS)
{
#ifdef DEBUG
*pszSrc = 0;
#endif
break;
}
}
pszSrc--;
}
if ( *pszSrc == RTPATH_DELIMITER
&& RT_SUCCESS(rc))
{
pszSrc++;
for (;;)
{
bool fEndOfString = true;
while (*pszEnd)
{
if (*pszEnd == RTPATH_DELIMITER)
break;
pszEnd++;
}
if (*pszEnd == RTPATH_DELIMITER)
{
fEndOfString = false;
*pszEnd = 0;
}
else
{
/* path component is invalid; try to correct the casing */
if (RT_FAILURE(rc))
{
if (!fEndOfString)
break;
}
}
if (fEndOfString)
break;
}
if (RT_FAILURE(rc))
}
else
}
if (pszLastComponent)
/* might be a new file so don't fail here! */
rc = VINF_SUCCESS;
}
}
return rc;
}
static void vbsfFreeFullPath(char *pszFullPath)
{
}
/**
*
* @returns iprt status code
* @param fShflFlags shared folder create flags
* @param fMode file attributes
* @retval pfOpen iprt create flags
*/
static int vbsfConvertFileOpenFlags(unsigned fShflFlags, RTFMODE fMode, SHFLHANDLE handleInitial, uint32_t *pfOpen)
{
int rc = VINF_SUCCESS;
if ( (fMode & RTFS_DOS_MASK) != 0
&& (fMode & RTFS_UNIX_MASK) == 0)
{
* @todo this is based on rtFsModeNormalize/rtFsModeFromDos.
* May be better to use RTFsModeNormalize here.
*/
/* x for directories. */
if (fMode & RTFS_DOS_DIRECTORY)
/* writable? */
if (!(fMode & RTFS_DOS_READONLY))
/* Set the requested mode using only allowed bits. */
}
else
{
/* Old linux and solaris additions did not initialize the Info.Attr.fMode field
* and it contained random bits from stack. Detect this using the handle field value
* passed from the guest: old additions set it (incorrectly) to 0, new additions
* set it to SHFL_HANDLE_NIL(~0).
*/
if (handleInitial == 0)
{
/* Old additions. Do nothing, use default mode. */
}
else
{
/* New additions or Windows additions. Set the requested mode using only allowed bits.
* Note: Windows guest set RTFS_UNIX_MASK bits to 0, which means a default mode
* will be set in fOpen.
*/
}
}
{
default:
case SHFL_CF_ACCESS_NONE:
{
/** @todo treat this as read access, but theoretically this could be a no access request. */
fOpen |= RTFILE_O_READ;
Log(("FLAG: SHFL_CF_ACCESS_NONE\n"));
break;
}
case SHFL_CF_ACCESS_READ:
{
fOpen |= RTFILE_O_READ;
Log(("FLAG: SHFL_CF_ACCESS_READ\n"));
break;
}
case SHFL_CF_ACCESS_WRITE:
{
fOpen |= RTFILE_O_WRITE;
Log(("FLAG: SHFL_CF_ACCESS_WRITE\n"));
break;
}
case SHFL_CF_ACCESS_READWRITE:
{
Log(("FLAG: SHFL_CF_ACCESS_READWRITE\n"));
break;
}
}
if (fShflFlags & SHFL_CF_ACCESS_APPEND)
{
fOpen |= RTFILE_O_APPEND;
}
{
default:
case SHFL_CF_ACCESS_ATTR_NONE:
{
Log(("FLAG: SHFL_CF_ACCESS_ATTR_NONE\n"));
break;
}
case SHFL_CF_ACCESS_ATTR_READ:
{
Log(("FLAG: SHFL_CF_ACCESS_ATTR_READ\n"));
break;
}
{
Log(("FLAG: SHFL_CF_ACCESS_ATTR_WRITE\n"));
break;
}
{
Log(("FLAG: SHFL_CF_ACCESS_ATTR_READWRITE\n"));
break;
}
}
/* Sharing mask */
{
default:
case SHFL_CF_ACCESS_DENYNONE:
Log(("FLAG: SHFL_CF_ACCESS_DENYNONE\n"));
break;
case SHFL_CF_ACCESS_DENYREAD:
Log(("FLAG: SHFL_CF_ACCESS_DENYREAD\n"));
break;
case SHFL_CF_ACCESS_DENYWRITE:
Log(("FLAG: SHFL_CF_ACCESS_DENYWRITE\n"));
break;
case SHFL_CF_ACCESS_DENYALL:
Log(("FLAG: SHFL_CF_ACCESS_DENYALL\n"));
break;
}
{
{
Log(("FLAGS: SHFL_CF_ACT_OPEN_IF_EXISTS and SHFL_CF_ACT_CREATE_IF_NEW\n"));
}
{
fOpen |= RTFILE_O_OPEN;
Log(("FLAGS: SHFL_CF_ACT_OPEN_IF_EXISTS and SHFL_CF_ACT_FAIL_IF_NEW\n"));
}
else
{
}
break;
{
fOpen |= RTFILE_O_CREATE;
Log(("FLAGS: SHFL_CF_ACT_FAIL_IF_EXISTS and SHFL_CF_ACT_CREATE_IF_NEW\n"));
}
else
{
}
break;
{
Log(("FLAGS: SHFL_CF_ACT_REPLACE_IF_EXISTS and SHFL_CF_ACT_CREATE_IF_NEW\n"));
}
{
Log(("FLAGS: SHFL_CF_ACT_REPLACE_IF_EXISTS and SHFL_CF_ACT_FAIL_IF_NEW\n"));
}
else
{
}
break;
{
Log(("FLAGS: SHFL_CF_ACT_OVERWRITE_IF_EXISTS and SHFL_CF_ACT_CREATE_IF_NEW\n"));
}
{
Log(("FLAGS: SHFL_CF_ACT_OVERWRITE_IF_EXISTS and SHFL_CF_ACT_FAIL_IF_NEW\n"));
}
else
{
}
break;
default:
Log(("FLAG: SHFL_CF_ACT_MASK_IF_EXISTS - invalid parameter\n"));
}
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Open a file or create and open a new one.
*
* @returns IPRT status code
* @param pClient Data structure describing the client accessing the shared folder
* @param pszPath Path to the file or folder on the host.
* @param pParms->Info When a new file is created this specifies the initial parameters.
* When a file is created or overwritten, it also specifies the
* initial size.
* @retval pParms->Handle On success the (shared folder) handle of the file opened or
* created
* @retval pParms->Info On success the parameters of the file opened or created
*/
{
SHFLFILEHANDLE *pHandle = 0;
/* Open or create a file. */
bool fNoError = false;
static int cErrors;
int rc = vbsfConvertFileOpenFlags(pParms->CreateFlags, pParms->Info.Attr.fMode, pParms->Handle, &fOpen);
if (RT_SUCCESS(rc))
{
}
if (SHFL_HANDLE_NIL != handle)
{
}
if (0 != pHandle)
{
}
if (RT_FAILURE(rc))
{
switch (rc)
{
case VERR_FILE_NOT_FOUND:
/* This actually isn't an error, so correct the rc before return later,
because the driver (VBoxSF.sys) expects rc = VINF_SUCCESS and checks the result code. */
fNoError = true;
break;
case VERR_PATH_NOT_FOUND:
/* This actually isn't an error, so correct the rc before return later,
because the driver (VBoxSF.sys) expects rc = VINF_SUCCESS and checks the result code. */
fNoError = true;
break;
case VERR_ALREADY_EXISTS:
/** @todo Possible race left here. */
{
#ifdef RT_OS_WINDOWS
#endif
}
/* This actually isn't an error, so correct the rc before return later,
because the driver (VBoxSF.sys) expects rc = VINF_SUCCESS and checks the result code. */
fNoError = true;
break;
case VERR_TOO_MANY_OPEN_FILES:
if (cErrors < 32)
{
#if defined RT_OS_LINUX || RT_OS_SOLARIS
if (cErrors < 1)
LogRel(("SharedFolders host service: Try to increase the limit for open files (ulimit -n)\n"));
#endif
cErrors++;
}
break;
default:
}
}
if (RT_SUCCESS(rc))
{
/** @note The shared folder status code is very approximate, as the runtime
* does not really provide this information. */
created when we eliminated the race. */
{
/* For now, we do not treat a failure here as fatal. */
/* @todo Also set the size for SHFL_CF_ACT_CREATE_IF_NEW if
SHFL_CF_ACT_FAIL_IF_EXISTS is set. */
}
if ( ( SHFL_CF_ACT_FAIL_IF_EXISTS
{
}
#if 0
/* @todo */
/* Set new attributes. */
{
);
}
#endif
/* Get file information */
if (RT_SUCCESS(rc))
{
#ifdef RT_OS_WINDOWS
#endif
}
}
if (RT_FAILURE(rc))
{
if ( (0 != pHandle)
{
}
if (SHFL_HANDLE_NIL != handle)
{
}
}
else
{
}
/* Report the driver that all is okay, we're done here */
if (fNoError)
rc = VINF_SUCCESS;
return rc;
}
/**
* Open a folder or create and open a new one.
*
* @returns IPRT status code
* @param pszPath Path to the file or folder on the host.
* @retval pParms->Handle On success the (shared folder) handle of the folder opened or
* created
* @retval pParms->Info On success the parameters of the folder opened or created
*
* @note folders are created with fMode = 0777
*/
{
int rc = VERR_NO_MEMORY;
if (0 != pHandle)
{
rc = VINF_SUCCESS;
/** @todo Can anyone think of a sensible, race-less way to do this? Although
I suspect that the race is inherent, due to the API available... */
/* Try to create the folder first if "create if new" is specified. If this
fails, and "open if exists" is specified, then we ignore the failure and try
to open the folder anyway. */
{
/** @todo render supplied attributes.
* bird: The guest should specify this. For windows guests RTFS_DOS_DIRECTORY should suffice. */
if (RT_FAILURE(rc))
{
switch (rc)
{
case VERR_ALREADY_EXISTS:
break;
case VERR_PATH_NOT_FOUND:
break;
default:
}
}
}
if ( RT_SUCCESS(rc)
{
/* Open the directory now */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
}
else
{
switch (rc)
{
case VERR_FILE_NOT_FOUND: /* Does this make sense? */
break;
case VERR_PATH_NOT_FOUND:
break;
case VERR_ACCESS_DENIED:
break;
default:
}
}
}
}
if (RT_FAILURE(rc))
{
if ( (0 != pHandle)
{
}
if (SHFL_HANDLE_NIL != handle)
{
}
}
else
{
}
return rc;
}
{
int rc = VINF_SUCCESS;
LogFlow(("vbsfCloseDir: Handle = %08X Search Handle = %08X\n",
{
}
return rc;
}
{
int rc = VINF_SUCCESS;
LogFlow(("vbsfCloseFile: Handle = %08X\n",
return rc;
}
/**
* Look up file or folder information by host path.
*
* @returns iprt status code (currently VINF_SUCCESS)
* @param pszFullPath The path of the file to be looked up
* @retval pParms->Result Status of the operation (success or error)
* @retval pParms->Info On success, information returned about the file
*/
{
int rc;
LogFlow(("SHFL_CF_LOOKUP\n"));
/* Client just wants to know if the object exists. */
switch (rc)
{
case VINF_SUCCESS:
{
#ifdef RT_OS_WINDOWS
#endif
break;
}
case VERR_FILE_NOT_FOUND:
{
rc = VINF_SUCCESS;
break;
}
case VERR_PATH_NOT_FOUND:
{
rc = VINF_SUCCESS;
break;
}
}
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_CREATE API. Located here as a form of API
* documentation. */
{
/* Simple opening of an existing file. */
/* Simple opening of an existing directory. */
/* If the number or types of parameters are wrong the API should fail. */
/* Add tests as required... */
}
#endif
/**
* Create or open a file or folder. Perform character set and case
* conversion on the file name if necessary.
*
* @returns IPRT status code, but see note below
* @param pClient Data structure describing the client accessing the shared
* folder
* @param root The index of the shared folder in the table of mappings.
* The host path of the shared folder is found using this.
* @param pPath The path of the file or folder relative to the host path
* indexed by root.
* @param cbPath Presumably the length of the path in pPath. Actually
* ignored, as pPath contains a length parameter.
* @param pParms->Info If a new file is created or an old one overwritten, set
* these attributes
* @retval pParms->Handle Shared folder handle to the newly opened file
* @retval pParms->Info Attributes of the file or folder opened
*
* @note This function returns success if a "non-exceptional" error occurred,
* such as "no such file". In this case, the caller should check the
* pParms->Result return value and whether pParms->Handle is valid.
*/
int vbsfCreate(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint32_t cbPath, SHFLCREATEPARMS *pParms)
{
int rc = VINF_SUCCESS;
LogFlow(("vbsfCreate: pClient = %p, pPath = %p, cbPath = %d, pParms = %p CreateFlags=%x\n",
/* Check the client access rights to the root. */
/** @todo */
/* Build a host full path for the given path, handle file name case issues (if the guest
* expects case-insensitive paths but the host is case-sensitive) and convert ucs2 to utf8 if
* necessary.
*/
char *pszFullPath = NULL;
uint32_t cbFullPathRoot = 0;
if (RT_SUCCESS(rc))
{
/* Reset return value in case client forgot to do so.
* pParms->Handle must not be reset here, as it is used
* in vbsfOpenFile to detect old additions.
*/
{
}
else
{
/* Query path information. */
if (RT_SUCCESS(rc))
{
/* Mark it as a directory in case the caller didn't. */
/**
* @todo I left this in in order not to change the behaviour of the
* function too much. Is it really needed, and should it really be
* here?
*/
{
}
/**
* @todo This should be in the Windows Guest Additions, as no-one else
* needs it.
*/
{
}
}
rc = VINF_SUCCESS;
/* Note: do not check the SHFL_CF_ACCESS_WRITE here, only check if the open operation
* will cause changes.
*
* Actual operations (write, set attr, etc), which can write to a shared folder, have
* the check and will return VERR_WRITE_PROTECT if the folder is not writable.
*/
)
{
/* is the guest allowed to write to this share? */
bool fWritable;
}
if (RT_SUCCESS(rc))
{
{
}
else
{
}
}
else
{
}
}
/* free the path string */
}
Log(("vbsfCreate: handle = %RX64 rc = %Rrc result=%x\n", (uint64_t)pParms->Handle, rc, pParms->Result));
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_CLOSE API. Located here as a form of API
* documentation. */
{
/* If the API parameters are invalid the API should fail. */
/* Add tests as required... */
}
#endif
{
int rc = VINF_SUCCESS;
LogFlow(("vbsfClose: pClient = %p, Handle = %RX64\n",
{
case SHFL_HF_TYPE_DIR:
{
break;
}
case SHFL_HF_TYPE_FILE:
{
break;
}
default:
return VERR_INVALID_HANDLE;
}
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_READ API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Basic reading from a file. */
/* Add tests as required... */
}
#endif
int vbsfRead (SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint64_t offset, uint32_t *pcbBuffer, uint8_t *pBuffer)
{
int rc;
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
if (*pcbBuffer == 0)
return VINF_SUCCESS; /* @todo correct? */
if (rc != VINF_SUCCESS)
{
return rc;
}
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_WRITE API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Simple test of writing to a file. */
/* Add tests as required... */
}
#endif
int vbsfWrite(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint64_t offset, uint32_t *pcbBuffer, uint8_t *pBuffer)
{
int rc;
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* Is the guest allowed to write to this share?
* XXX Actually this check was still done in vbsfCreate() -- RTFILE_O_WRITE cannot be set if vbsfMappingsQueryWritable() failed. */
bool fWritable;
return VERR_WRITE_PROTECT;
if (*pcbBuffer == 0)
return VINF_SUCCESS; /** @todo correct? */
if (rc != VINF_SUCCESS)
{
return rc;
}
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_FLUSH API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Simple opening and flushing of a file. */
/* Add tests as required... */
}
#endif
{
int rc = VINF_SUCCESS;
if (pHandle == 0)
{
AssertFailed();
return VERR_INVALID_HANDLE;
}
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_LIST API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Test listing an empty directory (simple edge case). */
/* Add tests as required... */
}
#endif
int vbsfDirList(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, SHFLSTRING *pPath, uint32_t flags,
{
int rc = VINF_SUCCESS;
bool fUtf8;
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
cbDirEntry = 4096;
if (pDirEntry == 0)
{
AssertFailed();
return VERR_NO_MEMORY;
}
cbBufferOrg = *pcbBuffer;
*pcbBuffer = 0;
*pcFiles = 0;
if (pPath)
{
{
/* Build a host full path for the given path
* and convert ucs2 to utf8 if necessary.
*/
char *pszFullPath = NULL;
if (RT_SUCCESS(rc))
{
/* free the path string */
if (RT_FAILURE(rc))
goto end;
}
else
goto end;
}
}
while (cbBufferOrg)
{
/* Do we still have a valid last entry for the active search? If so, then return it here */
{
}
else
{
rc = RTDirReadEx(DirHandle, pDirEntry, &cbDirEntrySize, RTFSOBJATTRADD_NOTHING, SHFL_RT_LINK(pClient));
if (rc == VERR_NO_MORE_FILES)
{
*pIndex = 0; /* listing completed */
break;
}
if ( rc != VINF_SUCCESS
&& rc != VWRN_NO_DIRENT_INFO)
{
//AssertFailed();
if ( rc == VERR_NO_TRANSLATION
|| rc == VERR_INVALID_UTF8_ENCODING)
continue;
break;
}
}
if (fUtf8)
else
/* Overestimating, but that's ok */
if (cbBufferOrg < cbNeeded)
{
/* No room, so save this directory entry, or else it's lost forever */
if (*pcFiles == 0)
{
AssertFailed();
return VINF_BUFFER_OVERFLOW; /* Return directly and don't free pDirEntry */
}
return VINF_SUCCESS; /* Return directly and don't free pDirEntry */
}
#ifdef RT_OS_WINDOWS
#endif
pSFDEntry->cucShortName = 0;
if (fUtf8)
{
}
else
{
#ifdef RT_OS_DARWIN
/** @todo This belongs in rtPathToNative or in the windows shared folder file system driver...
* The question is simply whether the NFD normalization is actually applied on a (virtual) file
* system level in darwin, or just by the user mode application libs. */
{
// Convert to
// Normalization Form C (composed Unicode). We need this because
// Mac OS X file system uses NFD (Normalization Form D :decomposed Unicode)
// while most other OS', server-side programs usually expect NFC.
rangeCharacters.location = 0;
}
#endif
// adjust cbNeeded (it was overestimated before)
}
*pcFiles += 1;
/* Free the saved last entry, that we've just returned */
{
}
if (flags & SHFL_LIST_RETURN_ONE)
break; /* we're done */
}
end:
if (pDirEntry)
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_READLINK API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Add tests as required... */
}
#endif
int vbsfReadLink(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint32_t cbPath, uint8_t *pBuffer, uint32_t cbBuffer)
{
int rc = VINF_SUCCESS;
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* Build a host full path for the given path, handle file name case issues
* (if the guest expects case-insensitive paths but the host is
* case-sensitive) and convert ucs2 to utf8 if necessary.
*/
char *pszFullPath = NULL;
uint32_t cbFullPathRoot = 0;
if (RT_SUCCESS(rc))
{
/* free the path string */
}
return rc;
}
int vbsfQueryFileInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
{
int rc = VINF_SUCCESS;
|| pcbBuffer == 0
|| pObjInfo == 0
|| *pcbBuffer < sizeof(SHFLFSOBJINFO))
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* @todo other options */
*pcbBuffer = 0;
if (type == SHFL_HF_TYPE_DIR)
{
}
else
{
#ifdef RT_OS_WINDOWS
#endif
}
if (rc == VINF_SUCCESS)
{
*pcbBuffer = sizeof(SHFLFSOBJINFO);
}
else
AssertFailed();
return rc;
}
static int vbsfSetFileInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
{
int rc = VINF_SUCCESS;
|| pcbBuffer == 0
|| pBuffer == 0
|| *pcbBuffer < sizeof(SHFLFSOBJINFO))
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
*pcbBuffer = 0;
/* Change only the time values that are not zero */
if (type == SHFL_HF_TYPE_DIR)
{
);
}
else
{
);
}
if (rc != VINF_SUCCESS)
{
/* temporary hack */
rc = VINF_SUCCESS;
}
if (type == SHFL_HF_TYPE_FILE)
{
/* Change file attributes if necessary */
{
#ifndef RT_OS_WINDOWS
/* Don't allow the guest to clear the own bit, otherwise the guest wouldn't be
* able to access this file anymore. Only for guests, which set the UNIX mode. */
if (fMode & RTFS_UNIX_MASK)
fMode |= RTFS_UNIX_IRUSR;
#endif
if (rc != VINF_SUCCESS)
{
/* silent failure, because this tends to fail with e.g. windows guest & linux host */
rc = VINF_SUCCESS;
}
}
}
/* TODO: mode for directories */
if (rc == VINF_SUCCESS)
{
rc = vbsfQueryFileInfo(pClient, root, Handle, SHFL_INFO_GET|SHFL_INFO_FILE, &bufsize, (uint8_t *)pSFDEntry);
if (rc == VINF_SUCCESS)
{
*pcbBuffer = sizeof(SHFLFSOBJINFO);
}
else
AssertFailed();
}
return rc;
}
static int vbsfSetEndOfFile(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
{
int rc = VINF_SUCCESS;
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
*pcbBuffer = 0;
if (flags & SHFL_INFO_SIZE)
{
if (rc != VINF_SUCCESS)
AssertFailed();
}
else
AssertFailed();
if (rc == VINF_SUCCESS)
{
/* Query the new object info and return it */
if (rc == VINF_SUCCESS)
{
#ifdef RT_OS_WINDOWS
#endif
*pcbBuffer = sizeof(SHFLFSOBJINFO);
}
else
AssertFailed();
}
return rc;
}
int vbsfQueryVolumeInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
{
int rc = VINF_SUCCESS;
char *pszFullPath = NULL;
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* @todo other options */
*pcbBuffer = 0;
if (RT_SUCCESS(rc))
{
rc = RTFsQuerySizes(pszFullPath, &pSFDEntry->ullTotalAllocationBytes, &pSFDEntry->ullAvailableAllocationBytes, &pSFDEntry->ulBytesPerAllocationUnit, &pSFDEntry->ulBytesPerSector);
if (rc != VINF_SUCCESS)
goto exit;
if (rc != VINF_SUCCESS)
goto exit;
if (rc != VINF_SUCCESS)
goto exit;
*pcbBuffer = sizeof(SHFLVOLINFO);
}
else AssertFailed();
exit:
/* free the path string */
return rc;
}
int vbsfQueryFSInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
{
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
if (flags & SHFL_INFO_FILE)
if (flags & SHFL_INFO_VOLUME)
AssertFailed();
return VERR_INVALID_PARAMETER;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_INFORMATION API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Basic get and set file size test. */
/* Basic get and set dir atime test. */
/* Basic get and set file atime test. */
/* Basic set end of file. */
/* Add tests as required... */
}
#endif
int vbsfSetFSInfo(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint32_t flags, uint32_t *pcbBuffer, uint8_t *pBuffer)
{
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* is the guest allowed to write to this share? */
bool fWritable;
return VERR_WRITE_PROTECT;
if (flags & SHFL_INFO_FILE)
if (flags & SHFL_INFO_SIZE)
// if (flags & SHFL_INFO_VOLUME)
// return vbsfVolumeInfo(pClient, root, Handle, flags, pcbBuffer, pBuffer);
AssertFailed();
return VERR_INVALID_PARAMETER;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_LOCK API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Simple file locking and unlocking test. */
/* Add tests as required... */
}
#endif
int vbsfLock(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint64_t offset, uint64_t length, uint32_t flags)
{
int rc;
if (pHandle == 0)
{
AssertFailed();
return VERR_INVALID_HANDLE;
}
|| (flags & SHFL_LOCK_ENTIRE)
)
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* Lock type */
switch(flags & SHFL_LOCK_MODE_MASK)
{
case SHFL_LOCK_SHARED:
break;
case SHFL_LOCK_EXCLUSIVE:
break;
default:
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* Lock wait type */
if (flags & SHFL_LOCK_WAIT)
else
#ifdef RT_OS_WINDOWS
if (rc != VINF_SUCCESS)
Log(("RTFileLock %RTfile %RX64 %RX64 failed with %Rrc\n", pHandle->file.Handle, offset, length, rc));
#else
rc = VINF_SUCCESS;
#endif
return rc;
}
int vbsfUnlock(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLHANDLE Handle, uint64_t offset, uint64_t length, uint32_t flags)
{
int rc;
if (pHandle == 0)
{
return VERR_INVALID_HANDLE;
}
|| (flags & SHFL_LOCK_ENTIRE)
)
{
return VERR_INVALID_PARAMETER;
}
#ifdef RT_OS_WINDOWS
if (rc != VINF_SUCCESS)
Log(("RTFileUnlock %RTfile %RX64 %RTX64 failed with %Rrc\n", pHandle->file.Handle, offset, length, rc));
#else
rc = VINF_SUCCESS;
#endif
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_REMOVE API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Add tests as required... */
}
#endif
int vbsfRemove(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pPath, uint32_t cbPath, uint32_t flags)
{
int rc = VINF_SUCCESS;
/* Validate input */
|| cbPath == 0
|| pPath == 0)
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* Build a host full path for the given path
* and convert ucs2 to utf8 if necessary.
*/
char *pszFullPath = NULL;
if (RT_SUCCESS(rc))
{
/* is the guest allowed to write to this share? */
bool fWritable;
if (RT_SUCCESS(rc))
{
if (flags & SHFL_REMOVE_SYMLINK)
else if (flags & SHFL_REMOVE_FILE)
else
}
#ifndef DEBUG_dmik
// VERR_ACCESS_DENIED for example?
// Assert(rc == VINF_SUCCESS || rc == VERR_DIR_NOT_EMPTY);
#endif
/* free the path string */
}
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_RENAME API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Add tests as required... */
}
#endif
int vbsfRename(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pSrc, SHFLSTRING *pDest, uint32_t flags)
{
int rc = VINF_SUCCESS;
/* Validate input */
|| pSrc == 0
|| pDest == 0)
{
AssertFailed();
return VERR_INVALID_PARAMETER;
}
/* Build a host full path for the given path
* and convert ucs2 to utf8 if necessary.
*/
char *pszFullPathSrc = NULL;
char *pszFullPathDest = NULL;
if (rc != VINF_SUCCESS)
return rc;
if (RT_SUCCESS (rc))
{
/* is the guest allowed to write to this share? */
bool fWritable;
if (RT_SUCCESS(rc))
{
if (flags & SHFL_RENAME_FILE)
{
rc = RTFileMove(pszFullPathSrc, pszFullPathDest, (flags & SHFL_RENAME_REPLACE_IF_EXISTS) ? RTFILEMOVE_FLAGS_REPLACE : 0);
}
else
{
/* NT ignores the REPLACE flag and simply return and already exists error. */
rc = RTDirRename(pszFullPathSrc, pszFullPathDest, (flags & SHFL_RENAME_REPLACE_IF_EXISTS) ? RTPATHRENAME_FLAGS_REPLACE : 0);
}
}
#ifndef DEBUG_dmik
#endif
/* free the path string */
}
/* free the path string */
return rc;
}
#ifdef UNITTEST
/** Unit test the SHFL_FN_SYMLINK API. Located here as a form of API
* documentation. */
{
/* If the number or types of parameters are wrong the API should fail. */
/* Add tests as required... */
}
#endif
int vbsfSymlink(SHFLCLIENTDATA *pClient, SHFLROOT root, SHFLSTRING *pNewPath, SHFLSTRING *pOldPath, SHFLFSOBJINFO *pInfo)
{
int rc = VINF_SUCCESS;
char *pszFullNewPath = NULL;
char *pszOldPath = NULL;
/* XXX: no support for UCS2 at the moment. */
return VERR_NOT_IMPLEMENTED;
if (rc != VINF_SUCCESS)
return rc;
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
}
return rc;
}
/*
* Clean up our mess by freeing all handles that are still valid.
*
*/
{
for (int i=0; i<SHFLHANDLE_MAX; i++)
{
{
Log(("Open handle %08x\n", i));
}
}
return VINF_SUCCESS;
}