pathint-nt.cpp revision 46b9d3cd08a855c5d0e968e4fff0e89dea3fc2df
/* $Id$ */
/** @file
* IPRT - Native NT, Internal Path stuff.
*/
/*
* Copyright (C) 2006-2013 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_FS
#include "internal-r3-nt.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static char const g_szPrefixUnc[] = "\\??\\UNC\\";
static char const g_szPrefix[] = "\\??\\";
/**
* Handles the pass thru case for UTF-8 input.
*
* @returns IPRT status code.
* @param pNtName Where to return the NT name.
* @param phRootDir Where to return the root handle, if applicable.
* @param pszPath The UTF-8 path.
*/
static int rtNtPathFromWinUtf8PassThru(struct _UNICODE_STRING *pNtName, PHANDLE phRootDir, const char *pszPath)
{
if (RT_SUCCESS(rc))
{
{
pwszPath[0] = '\\';
return VINF_SUCCESS;
}
}
return rc;
}
/**
* Handles the pass thru case for UTF-16 input.
*
* @returns IPRT status code.
* @param pNtName Where to return the NT name.
* @param phRootDir Stores NULL here, as we don't use it.
* @param pwszWinPath The UTF-16 windows-style path.
* @param cwcWinPath The length of the windows-style input path.
*/
{
/* Drop a character because: \\?\ -> \.\ */
pwszWinPath++;
cwcWinPath--;
/* Check length and allocate memory for it. */
int rc;
{
if (pwszNtPath)
{
/* Intialize the path. */
pwszNtPath[0] = '\\';
/* Initialize the return values. */
rc = VINF_SUCCESS;
}
else
}
else
return rc;
}
/**
* Converts the path to UTF-16 and sets all the return values.
*
* @returns IPRT status code.
* @param pNtName Where to return the NT name.
* @param phRootDir Where to return the root handle, if applicable.
* @param pszPath The UTF-8 path.
*/
static int rtNtPathUtf8ToUniStr(struct _UNICODE_STRING *pNtName, PHANDLE phRootDir, const char *pszPath)
{
if (RT_SUCCESS(rc))
{
{
return VINF_SUCCESS;
}
}
return rc;
}
/**
* Converts a path to NT format and encoding.
*
* @returns IPRT status code.
* @param pNtName Where to return the NT name.
* @param phRootDir Where to return the root handle, if applicable.
* @param pszPath The UTF-8 path.
*/
static int rtNtPathToNative(struct _UNICODE_STRING *pNtName, PHANDLE phRootDir, const char *pszPath)
{
/*
* Very simple conversion of a win32-like path into an NT path.
*/
const char *pszPrefix = g_szPrefix;
if ( RTPATH_IS_SLASH(pszPath[0])
&& pszPath[2])
{
#ifdef IPRT_WITH_NT_PATH_PASSTHRU
/* Special hack: The path starts with "\\\\!\\", we will skip past the bang and pass it thru. */
#endif
{
/*
* Device path.
* Note! I suspect \\.\stuff\..\otherstuff may be handled differently by windows.
*/
cchSkip = 4;
}
else
{
/* UNC */
cchSkip = 2;
}
}
/*
* Straighten out all .. and uncessary . references and convert slashes.
*/
char szPath[RTPATH_MAX];
if (RT_FAILURE(rc))
return rc;
/*
* Add prefix and convert it to UTF16.
*/
}
/**
* Converts a UTF-16 windows-style path to NT format.
*
* @returns IPRT status code.
* @param pNtName Where to return the NT name. Free using
* RTNtPathFree.
* @param phRootDir Where to return the root handle, if applicable.
* @param pwszWinPath The UTF-16 windows-style path.
* @param cwcWinPath The max length of the windows-style path in
* RTUTF16 units. Use RTSTR_MAX if unknown and @a
* pwszWinPath is correctly terminated.
*/
RTDECL(int) RTNtPathFromWinUtf16Ex(struct _UNICODE_STRING *pNtName, HANDLE *phRootDir, PCRTUTF16 pwszWinPath, size_t cwcWinPath)
{
/*
* Validate the input, calculating the correct length.
*/
return VERR_INVALID_NAME;
if (RT_FAILURE(rc))
return rc;
/*
* Very simple conversion of a win32-like path into an NT path.
*/
const char *pszPrefix = g_szPrefix;
if ( RTPATH_IS_SLASH(pwszWinPath[0])
&& cwcWinPath >= 3
{
&& cwcWinPath >= 4
#ifdef IPRT_WITH_NT_PATH_PASSTHRU
/* Special hack: The path starts with "\\\\!\\", we will skip past the bang and pass it thru. */
&& cwcWinPath >= 4
{
pwszWinPath += 3;
cwcWinPath -= 3;
{
if (pwszNtPath)
{
return VINF_SUCCESS;
}
}
else
return rc;
}
#endif
&& cwcWinPath >= 4
{
/*
* Device path.
* Note! I suspect \\.\stuff\..\otherstuff may be handled differently by windows.
*/
cchSkip = 4;
}
else
{
/* UNC */
cchSkip = 2;
}
}
/*
* Straighten out all .. and unnecessary . references and convert slashes.
*/
char szAbsPath[RTPATH_MAX];
char szRelPath[RTPATH_MAX];
char *pszRelPath = szRelPath;
if (RT_SUCCESS(rc))
rc = RTPathAbs(szRelPath, &szAbsPath[cchPrefix - cchSkip], sizeof(szAbsPath) - (cchPrefix - cchSkip));
if (RT_SUCCESS(rc))
{
/*
* Add prefix and convert it to UTF16.
*/
}
return rc;
}
/**
* Frees the native path and root handle.
*
* @param pNtName The NT path after a successful rtNtPathToNative
* call.
* @param phRootDir The root handle variable after a
* rtNtPathToNative.
*/
{
}
/**
* Frees the native path and root handle.
*
* @param pNtName The NT path after a successful
* RTNtPathFromWinUtf16Ex call.
* @param phRootDir The root handle variable after a successfull
* RTNtPathFromWinUtf16Ex call.
*/
{
}
/**
* Wrapper around NtCreateFile.
*
* @returns IPRT status code.
* @param pszPath The UTF-8 path.
* @param fDesiredAccess See NtCreateFile.
* @param fFileAttribs See NtCreateFile.
* @param fShareAccess See NtCreateFile.
* @param fCreateDisposition See NtCreateFile.
* @param fCreateOptions See NtCreateFile.
* @param fObjAttribs The OBJECT_ATTRIBUTES::Attributes value, see
* NtCreateFile and InitializeObjectAttributes.
* @param phHandle Where to return the handle.
* @param puAction Where to return the action taken. Optional.
*/
RTDECL(int) RTNtPathOpen(const char *pszPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs, ULONG fShareAccess,
{
if (RT_SUCCESS(rc))
{
&ObjAttr,
&Ios,
NULL /* AllocationSize*/,
NULL /*EaBuffer*/,
0 /*EaLength*/);
if (NT_SUCCESS(rcNt))
{
if (puAction)
rc = VINF_SUCCESS;
}
else
}
return rc;
}
/**
* Wrapper around NtCreateFile.
*
* @returns IPRT status code.
* @param pszPath The UTF-8 path.
* @param fDesiredAccess See NtCreateFile.
* @param fFileAttribs See NtCreateFile.
* @param fShareAccess See NtCreateFile.
* @param fCreateDisposition See NtCreateFile.
* @param fCreateOptions See NtCreateFile.
* @param fObjAttribs The OBJECT_ATTRIBUTES::Attributes value, see
* NtCreateFile and InitializeObjectAttributes.
* @param phHandle Where to return the handle.
* @param pfObjDir If not NULL, the variable pointed to will be set
* to @c true if we opened an object directory and
* @c false if we opened an directory file (normal
* directory).
*/
RTDECL(int) RTNtPathOpenDir(const char *pszPath, ACCESS_MASK fDesiredAccess, ULONG fShareAccess, ULONG fCreateOptions,
{
if (RT_SUCCESS(rc))
{
&ObjAttr,
&Ios,
NULL /* AllocationSize*/,
NULL /*EaBuffer*/,
0 /*EaLength*/);
if (NT_SUCCESS(rcNt))
{
if (pfObjDir)
*pfObjDir = false;
rc = VINF_SUCCESS;
}
#ifdef IPRT_WITH_NT_PATH_PASSTHRU
else if ( pfObjDir
&& RTPATH_IS_SLASH(pszPath[0])
{
/* Strip trailing slash. */
/* Rought conversion of the access flags. */
ULONG fObjDesiredAccess = 0;
else
{
if ( (fDesiredAccess & (FILE_LIST_DIRECTORY | FILE_GENERIC_READ | GENERIC_READ | STANDARD_RIGHTS_READ))
|| !fObjDesiredAccess)
}
if (NT_SUCCESS(rcNt))
{
*pfObjDir = true;
rc = VINF_SUCCESS;
}
else
}
#endif
else
}
return rc;
}
/**
* Closes an handled open by rtNtPathOpen.
*
* @returns IPRT status code
* @param hHandle The handle value.
*/
{
if (NT_SUCCESS(rcNt))
return VINF_SUCCESS;
return RTErrConvertFromNtStatus(rcNt);
}