path-win.cpp revision 7b29be8fbe386e6bd8f9ce2547bcb6d8ed2587d0
/* $Id$ */
/** @file
* IPRT - Path manipulation.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_PATH
#include <Windows.h>
/**
* 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.
*/
{
/*
* Convert to UTF-16, call Win32 API, convert back.
*/
if (!RT_SUCCESS(rc))
return (rc);
else if (rc <= 0)
else
return rc;
}
/**
* Gets the program path.
*
* @returns iprt status code.
* @param pszPath Buffer where to store the path.
* @param cchPath Buffer size in bytes.
*/
{
/*
* First time only.
*/
if (!g_szrtProgramPath[0])
{
{
return RTErrConvertFromWin32(GetLastError());
}
}
/*
* Calc the length and check if there is space before copying.
*/
{
return VINF_SUCCESS;
}
return VERR_BUFFER_OVERFLOW;
}
/**
* 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.
*/
}
RTR3DECL(int) RTPathQueryInfo(const char *pszPath, PRTFSOBJINFO pObjInfo, RTFSOBJATTRADD enmAdditionalAttribs)
{
/*
* Validate input.
*/
if (!pszPath)
{
return VERR_INVALID_PARAMETER;
}
if (!pObjInfo)
{
return VERR_INVALID_PARAMETER;
}
{
return VERR_INVALID_PARAMETER;
}
/*
* Query file info.
*/
#ifndef RT_DONT_CONVERT_FILENAMES
if (RT_FAILURE(rc))
return rc;
{
return rc;
}
#else
return RTErrConvertFromWin32(GetLastError());
#endif
/*
* 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_EASIZE:
break;
case RTFSOBJATTRADD_UNIX:
break;
case RTFSOBJATTRADD_NOTHING:
break;
default:
AssertMsgFailed(("Impossible!\n"));
return VERR_INTERNAL_ERROR;
}
return VINF_SUCCESS;
}
RTR3DECL(int) RTPathSetTimes(const char *pszPath, PCRTTIMESPEC pAccessTime, PCRTTIMESPEC pModificationTime,
{
/*
* Validate input.
*/
AssertMsgReturn(!pAccessTime || VALID_PTR(pAccessTime), ("%p\n", pAccessTime), VERR_INVALID_POINTER);
AssertMsgReturn(!pModificationTime || VALID_PTR(pModificationTime), ("%p\n", pModificationTime), VERR_INVALID_POINTER);
AssertMsgReturn(!pChangeTime || VALID_PTR(pChangeTime), ("%p\n", pChangeTime), VERR_INVALID_POINTER);
/*
* Convert the path.
*/
if (RT_SUCCESS(rc))
{
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 (%Vrc)\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).
*/
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;
}
/**
* Checks if the path exists.
*
* Symbolic links will all be attempted resolved.
*
* @returns true if it exists and false if it doesn't
* @param pszPath The path to check.
*/
{
/*
* Validate input.
*/
AssertPtrReturn(pszPath, false);
AssertReturn(*pszPath, false);
/*
* Try query file info.
*/
#ifndef RT_DONT_CONVERT_FILENAMES
if (RT_SUCCESS(rc))
{
}
#else
int rc = VINF_SUCCESS;
#endif
return RT_SUCCESS(rc);
}
{
/*
* Validate input.
*/
/*
* This interface is almost identical to the Windows API.
*/
#ifndef RT_DONT_CONVERT_FILENAMES
if (RT_SUCCESS(rc))
{
/** @todo improove the slash stripping a bit? */
if ( cwc >= 2
if (!SetCurrentDirectoryW(pwszPath))
}
#else
int rc = VINF_SUCCESS;
/** @todo improove the slash stripping a bit? */
if ( cchPath >= 2
{
if (pszCopy)
{
if (!SetCurrentDirectory(pszCopy))
}
else
rc = VERR_NO_MEMORY;
}
else
{
if (!SetCurrentDirectory(pszPath))
}
#endif
return rc;
}