path-win.cpp revision 6e378fb4b746a4db875850789272bff866c1b354
/* $Id$ */
/** @file
* IPRT - Path manipulation.
*/
/*
* 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_PATH
#include <Windows.h>
#include <Shlobj.h>
/* Needed for lazy loading SHGetFolderPathW in RTPathUserDocuments(). */
typedef FNSHGETFOLDERPATHW *PFNSHGETFOLDERPATHW;
/**
* Get the real (no symlinks, no . or .. components) path, must exist.
*
* @returns iprt status code.
* @param pszPath The path to resolve.
* @param pszRealPath Where to store the real path.
* @param cchRealPath Size of the buffer.
*/
{
/*
* Convert to UTF-16, call Win32 APIs, convert back.
*/
if (!RT_SUCCESS(rc))
return (rc);
{
/* Check that it exists. (Use RTPathAbs() to just resolve the name.) */
if (dwAttr != INVALID_FILE_ATTRIBUTES)
else
}
else if (rc <= 0)
else
return rc;
}
/**
* Get the absolute path (no symlinks, no . or .. components), doesn't have to exit.
*
* @returns iprt status code.
* @param pszPath The path to resolve.
* @param pszAbsPath Where to store the absolute path.
* @param cchAbsPath Size of the buffer.
*/
{
/*
* Validation.
*/
if (RT_UNLIKELY(!*pszPath))
return VERR_INVALID_PARAMETER;
/*
* Convert to UTF-16, call Win32 API, convert back.
*/
if (!RT_SUCCESS(rc))
return (rc);
{
if (RT_SUCCESS(rc))
{
/*
* Remove trailing slash if the path may be pointing to a directory.
* (See posix variant.)
*/
if ( cch > 1
}
}
else if (rc <= 0)
else
return rc;
}
/**
* Gets the user home directory.
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
{
/*
* There are multiple definitions for what WE think of as user home...
*/
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
{
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
{
/* %HOMEDRIVE%%HOMEPATH% */
return VERR_PATH_NOT_FOUND;
|| !(dwAttr & FILE_ATTRIBUTE_DIRECTORY))
return VERR_PATH_NOT_FOUND;
}
}
/*
* Convert and return.
*/
}
{
/*
* Validate input
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
{
/*
* Convert and return.
*/
}
}
}
return VERR_PATH_NOT_FOUND;
}
RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
{
}
RTR3DECL(int) RTPathQueryInfoEx(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs, uint32_t fFlags)
{
/*
* Validate input.
*/
("Invalid enmAdditionalAttribs=%p\n", enmAdditionalAttribs),
/*
* Query file info.
*/
if (RT_FAILURE(rc))
return rc;
{
/* Fallback to FindFileFirst in case of sharing violation. */
if (GetLastError() == ERROR_SHARING_VIOLATION)
{
if (hDir == INVALID_HANDLE_VALUE)
{
return rc;
}
}
else
{
return rc;
}
}
/*
* Getting the information for the link target is a bit annoying and
* subject to the same access violation mess as above.. :/
*/
/** @todo we're too lazy wrt to error paths here... */
if ( (fFlags & RTPATH_F_FOLLOW_LINK)
{
NULL,
NULL);
if (hFinal != INVALID_HANDLE_VALUE)
{
{
}
}
else if (GetLastError() != ERROR_SHARING_VIOLATION)
{
return rc;
}
}
/*
* Setup the returned data.
*/
pObjInfo->Attr.fMode = rtFsModeFromDos((Data.dwFileAttributes << RTFS_DOS_SHIFT) & RTFS_DOS_MASK_NT,
/*
* Requested attributes (we cannot provide anything actually).
*/
switch (enmAdditionalAttribs)
{
case RTFSOBJATTRADD_NOTHING:
break;
case RTFSOBJATTRADD_UNIX:
break;
break;
break;
case RTFSOBJATTRADD_EASIZE:
break;
default:
AssertMsgFailed(("Impossible!\n"));
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
{
return RTPathSetTimesEx(pszPath, pAccessTime, pModificationTime, pChangeTime, pBirthTime, RTPATH_F_ON_LINK);
}
RTR3DECL(int) RTPathSetTimesEx(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
{
/*
* Validate input.
*/
/*
* Convert the path.
*/
if (RT_SUCCESS(rc))
{
if (fFlags & RTPATH_F_FOLLOW_LINK)
FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
NULL, /* security attribs */
OPEN_EXISTING, /* dwCreationDisposition */
NULL);
else
{
/** @todo Symlink: Test RTPathSetTimesEx on Windows. (The code is disabled
* because it's not tested yet.) */
#if 0 //def FILE_FLAG_OPEN_REPARSE_POINT
FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
NULL, /* security attribs */
OPEN_EXISTING, /* dwCreationDisposition */
NULL);
#endif
FILE_WRITE_ATTRIBUTES, /* dwDesiredAccess */
NULL, /* security attribs */
OPEN_EXISTING, /* dwCreationDisposition */
NULL);
}
if (hFile != INVALID_HANDLE_VALUE)
{
/*
* Check if it's a no-op.
*/
else
{
/*
* Convert the input and call the API.
*/
if (pBirthTime)
if (pAccessTime)
if (pModificationTime)
rc = VINF_SUCCESS;
else
{
Log(("RTPathSetTimes('%s', %p, %p, %p, %p): SetFileTime failed with lasterr %d (%Rrc)\n",
}
}
}
else
{
}
}
LogFlow(("RTPathSetTimes(%p:{%s}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}, %p:{%RDtimespec}): return %Rrc\n",
return rc;
}
/**
* Internal worker for RTFileRename and RTFileMove.
*
* @returns iprt status code.
* @param pszSrc The source filename.
* @param pszDst The destination filename.
* @param fFlags The windows MoveFileEx flags.
* @param fFileType The filetype. We use the RTFMODE filetypes here. If it's 0,
* anything goes. If it's RTFS_TYPE_DIRECTORY we'll check that the
* source is a directory. If Its RTFS_TYPE_FILE we'll check that it's
* not a directory (we are NOT checking whether it's a file).
*/
DECLHIDDEN(int) rtPathWin32MoveRename(const char *pszSrc, const char *pszDst, uint32_t fFlags, RTFMODE fFileType)
{
/*
* Convert the strings.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* Check object type if requested.
* This is open to race conditions.
*/
if (fFileType)
{
if (dwAttr == INVALID_FILE_ATTRIBUTES)
else if (RTFS_IS_DIRECTORY(fFileType))
else
}
if (RT_SUCCESS(rc))
{
rc = VINF_SUCCESS;
else
{
Log(("MoveFileExW('%s', '%s', %#x, %RTfmode): fails with rc=%Rrc & lasterr=%d\n",
}
}
}
}
return rc;
}
{
/*
* Validate input.
*/
AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
/*
* Call the worker.
*/
int rc = rtPathWin32MoveRename(pszSrc, pszDst, fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0, 0);
LogFlow(("RTPathRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n", pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
return rc;
}
{
}
{
/*
* Validate input.
*/
AssertPtrReturn(pszPath, false);
AssertReturn(*pszPath, false);
/*
* Try query file info.
*/
if (RT_SUCCESS(rc))
{
}
else
if (dwAttr == INVALID_FILE_ATTRIBUTES)
return false;
#ifdef FILE_ATTRIBUTE_REPARSE_POINT
if ( (fFlags & RTPATH_F_FOLLOW_LINK)
&& (dwAttr & FILE_ATTRIBUTE_REPARSE_POINT))
{
AssertFailed();
/** @todo Symlinks: RTPathExists+RTPathExistsEx is misbehaving on symbolic
* links on Windows. */
}
#endif
return true;
}
{
int rc;
/*
* GetCurrentDirectory may in some cases omit the drive letter, according
* to MSDN, thus the GetFullPathName call.
*/
{
else
}
else
return rc;
}
{
/*
* Validate input.
*/
/*
* This interface is almost identical to the Windows API.
*/
if (RT_SUCCESS(rc))
{
/** @todo improve the slash stripping a bit? */
if ( cwc >= 2
if (!SetCurrentDirectoryW(pwszPath))
}
return rc;
}