diff options
author | Rama krishnan Raghupathy <ramarag@microsoft.com> | 2016-03-07 16:58:24 -0800 |
---|---|---|
committer | Rama krishnan Raghupathy <ramarag@microsoft.com> | 2016-03-07 16:58:24 -0800 |
commit | be7b9df6252593c918aaf7efa7f57f266349d179 (patch) | |
tree | d02fc71bd0e7c41a18064b8f885d3d1c640afe29 | |
parent | 968361f43e7ced74d89c09535cac114889cdb39d (diff) | |
parent | a3348ba05a59f731b080bb6ddf23f95ad763db81 (diff) | |
download | coreclr-be7b9df6252593c918aaf7efa7f57f266349d179.tar.gz coreclr-be7b9df6252593c918aaf7efa7f57f266349d179.tar.bz2 coreclr-be7b9df6252593c918aaf7efa7f57f266349d179.zip |
Merge pull request #3441 from ramarag/pal_longfile
Enabling LongFile Support in PAL
27 files changed, 1001 insertions, 1049 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 4815662d32..c8eadf24cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -746,6 +746,7 @@ add_definitions(-DFEATURE_NORM_IDNA_ONLY) if(CLR_CMAKE_PLATFORM_UNIX) add_definitions(-DFEATURE_PAL) add_definitions(-DFEATURE_PAL_SXS) + add_definitions(-DFEATURE_PAL_ANSI) endif(CLR_CMAKE_PLATFORM_UNIX) if(CLR_CMAKE_PLATFORM_LINUX) add_definitions(-DFEATURE_PERFMAP) diff --git a/src/pal/inc/pal.h b/src/pal/inc/pal.h index a10f228b4d..648960cc65 100644 --- a/src/pal/inc/pal.h +++ b/src/pal/inc/pal.h @@ -604,15 +604,7 @@ BOOL PALAPI PAL_GetPALDirectoryW( OUT LPWSTR lpDirectoryName, - IN UINT cchDirectoryName); - -PALIMPORT -BOOL -PALAPI -PAL_GetPALDirectoryA( - OUT LPSTR lpDirectoryName, - IN UINT cchDirectoryName); - + IN OUT UINT* cchDirectoryName); #ifdef UNICODE #define PAL_GetPALDirectory PAL_GetPALDirectoryW #else @@ -735,6 +727,7 @@ wsprintfW( #define IDYES 6 #define IDNO 7 + PALIMPORT int PALAPI @@ -744,14 +737,6 @@ MessageBoxW( IN LPCWSTR lpCaption, IN UINT uType); -PALIMPORT -int -PALAPI -MessageBoxA( - IN LPVOID hWnd, // NOTE: diff from winuser.h - IN LPCSTR lpText, - IN LPCSTR lpCaption, - IN UINT uType); #ifdef UNICODE #define MessageBox MessageBoxW @@ -849,17 +834,6 @@ typedef struct _SECURITY_ATTRIBUTES { #define INVALID_SET_FILE_POINTER ((DWORD)-1) -PALIMPORT -HANDLE -PALAPI -CreateFileA( - IN LPCSTR lpFileName, - IN DWORD dwDesiredAccess, - IN DWORD dwShareMode, - IN LPSECURITY_ATTRIBUTES lpSecurityAttributes, - IN DWORD dwCreationDisposition, - IN DWORD dwFlagsAndAttributes, - IN HANDLE hTemplateFile); PALIMPORT HANDLE @@ -901,17 +875,6 @@ UnlockFile( IN DWORD nNumberOfBytesToUnlockHigh ); -PALIMPORT -DWORD -PALAPI -SearchPathA( - IN LPCSTR lpPath, - IN LPCSTR lpFileName, - IN LPCSTR lpExtension, - IN DWORD nBufferLength, - OUT LPSTR lpBuffer, - OUT LPSTR *lpFilePart - ); PALIMPORT DWORD @@ -931,13 +894,6 @@ SearchPathW( #endif // !UNICODE -PALIMPORT -BOOL -PALAPI -CopyFileA( - IN LPCSTR lpExistingFileName, - IN LPCSTR lpNewFileName, - IN BOOL bFailIfExists); PALIMPORT BOOL @@ -953,11 +909,6 @@ CopyFileW( #define CopyFile CopyFileA #endif -PALIMPORT -BOOL -PALAPI -DeleteFileA( - IN LPCSTR lpFileName); PALIMPORT BOOL @@ -972,12 +923,6 @@ DeleteFileW( #endif -PALIMPORT -BOOL -PALAPI -MoveFileA( - IN LPCSTR lpExistingFileName, - IN LPCSTR lpNewFileName); PALIMPORT BOOL @@ -995,13 +940,6 @@ MoveFileW( #define MOVEFILE_REPLACE_EXISTING 0x00000001 #define MOVEFILE_COPY_ALLOWED 0x00000002 -PALIMPORT -BOOL -PALAPI -MoveFileExA( - IN LPCSTR lpExistingFileName, - IN LPCSTR lpNewFileName, - IN DWORD dwFlags); PALIMPORT BOOL @@ -1020,13 +958,6 @@ MoveFileExW( PALIMPORT BOOL PALAPI -CreateDirectoryA( - IN LPCSTR lpPathName, - IN LPSECURITY_ATTRIBUTES lpSecurityAttributes); - -PALIMPORT -BOOL -PALAPI CreateDirectoryW( IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes); @@ -1043,12 +974,6 @@ PALAPI RemoveDirectoryW( IN LPCWSTR lpPathName); -PALIMPORT -BOOL -PALAPI -RemoveDirectoryA( - IN LPCSTR lpPathName); - #ifdef UNICODE #define RemoveDirectory RemoveDirectoryW #else @@ -1107,13 +1032,6 @@ typedef LPWIN32_FIND_DATAA LPWIN32_FIND_DATA; PALIMPORT HANDLE PALAPI -FindFirstFileA( - IN LPCSTR lpFileName, - OUT LPWIN32_FIND_DATAA lpFindFileData); - -PALIMPORT -HANDLE -PALAPI FindFirstFileW( IN LPCWSTR lpFileName, OUT LPWIN32_FIND_DATAW lpFindFileData); @@ -1127,13 +1045,6 @@ FindFirstFileW( PALIMPORT BOOL PALAPI -FindNextFileA( - IN HANDLE hFindFile, - OUT LPWIN32_FIND_DATAA lpFindFileData); - -PALIMPORT -BOOL -PALAPI FindNextFileW( IN HANDLE hFindFile, OUT LPWIN32_FIND_DATAW lpFindFileData); @@ -1153,12 +1064,6 @@ FindClose( PALIMPORT DWORD PALAPI -GetFileAttributesA( - IN LPCSTR lpFileName); - -PALIMPORT -DWORD -PALAPI GetFileAttributesW( IN LPCWSTR lpFileName); @@ -1209,13 +1114,6 @@ GetFileAttributesExW( PALIMPORT BOOL PALAPI -SetFileAttributesA( - IN LPCSTR lpFileName, - IN DWORD dwFileAttributes); - -PALIMPORT -BOOL -PALAPI SetFileAttributesW( IN LPCWSTR lpFileName, IN DWORD dwFileAttributes); @@ -1401,15 +1299,6 @@ GetConsoleOutputCP( PALIMPORT DWORD PALAPI -GetFullPathNameA( - IN LPCSTR lpFileName, - IN DWORD nBufferLength, - OUT LPSTR lpBuffer, - OUT LPSTR *lpFilePart); - -PALIMPORT -DWORD -PALAPI GetFullPathNameW( IN LPCWSTR lpFileName, IN DWORD nBufferLength, @@ -1450,15 +1339,6 @@ GetShortPathNameW( PALIMPORT UINT PALAPI -GetTempFileNameA( - IN LPCSTR lpPathName, - IN LPCSTR lpPrefixString, - IN UINT uUnique, - OUT LPSTR lpTempFileName); - -PALIMPORT -UINT -PALAPI GetTempFileNameW( IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, @@ -1474,13 +1354,6 @@ GetTempFileNameW( PALIMPORT DWORD PALAPI -GetTempPathA( - IN DWORD nBufferLength, - OUT LPSTR lpBuffer); - -PALIMPORT -DWORD -PALAPI GetTempPathW( IN DWORD nBufferLength, OUT LPWSTR lpBuffer); @@ -1494,13 +1367,6 @@ GetTempPathW( PALIMPORT DWORD PALAPI -GetCurrentDirectoryA( - IN DWORD nBufferLength, - OUT LPSTR lpBuffer); - -PALIMPORT -DWORD -PALAPI GetCurrentDirectoryW( IN DWORD nBufferLength, OUT LPWSTR lpBuffer); @@ -1514,12 +1380,6 @@ GetCurrentDirectoryW( PALIMPORT BOOL PALAPI -SetCurrentDirectoryA( - IN LPCSTR lpPathName); - -PALIMPORT -BOOL -PALAPI SetCurrentDirectoryW( IN LPCWSTR lpPathName); @@ -1558,26 +1418,6 @@ GetComputerNameW( PALIMPORT HANDLE PALAPI -CreateSemaphoreA( - IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, - IN LONG lInitialCount, - IN LONG lMaximumCount, - IN LPCSTR lpName); - -PALIMPORT -HANDLE -PALAPI -CreateSemaphoreExA( - IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, - IN LONG lInitialCount, - IN LONG lMaximumCount, - IN LPCSTR lpName, - IN /*_Reserved_*/ DWORD dwFlags, - IN DWORD dwDesiredAccess); - -PALIMPORT -HANDLE -PALAPI CreateSemaphoreW( IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, IN LONG lInitialCount, @@ -1622,15 +1462,6 @@ ReleaseSemaphore( PALIMPORT HANDLE PALAPI -CreateEventA( - IN LPSECURITY_ATTRIBUTES lpEventAttributes, - IN BOOL bManualReset, - IN BOOL bInitialState, - IN LPCSTR lpName); - -PALIMPORT -HANDLE -PALAPI CreateEventW( IN LPSECURITY_ATTRIBUTES lpEventAttributes, IN BOOL bManualReset, @@ -1675,14 +1506,6 @@ CreateMutexW( IN BOOL bInitialOwner, IN LPCWSTR lpName); -PALIMPORT -HANDLE -PALAPI -CreateMutexA( - IN LPSECURITY_ATTRIBUTES lpMutexAttributes, - IN BOOL bInitialOwner, - IN LPCSTR lpName); - #ifdef UNICODE #define CreateMutex CreateMutexW #else @@ -1697,13 +1520,6 @@ OpenMutexW( IN BOOL bInheritHandle, IN LPCWSTR lpName); -PALIMPORT -HANDLE -PALAPI -OpenMutexA( - IN DWORD dwDesiredAccess, - IN BOOL bInheritHandle, - IN LPCSTR lpName); #ifdef UNICODE #define OpenMutex OpenMutexW @@ -1810,21 +1626,6 @@ typedef struct _PROCESS_INFORMATION { PALIMPORT BOOL PALAPI -CreateProcessA( - IN LPCSTR lpApplicationName, - IN LPSTR lpCommandLine, - IN LPSECURITY_ATTRIBUTES lpProcessAttributes, - IN LPSECURITY_ATTRIBUTES lpThreadAttributes, - IN BOOL bInheritHandles, - IN DWORD dwCreationFlags, - IN LPVOID lpEnvironment, - IN LPCSTR lpCurrentDirectory, - IN LPSTARTUPINFOA lpStartupInfo, - OUT LPPROCESS_INFORMATION lpProcessInformation); - -PALIMPORT -BOOL -PALAPI CreateProcessW( IN LPCWSTR lpApplicationName, IN LPWSTR lpCommandLine, @@ -3576,17 +3377,6 @@ SetErrorMode( PALIMPORT HANDLE PALAPI -CreateFileMappingA( - IN HANDLE hFile, - IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes, - IN DWORD flProtect, - IN DWORD dwMaximumSizeHigh, - IN DWORD dwMaximumSizeLow, - IN LPCSTR lpName); - -PALIMPORT -HANDLE -PALAPI CreateFileMappingW( IN HANDLE hFile, IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes, @@ -3614,14 +3404,6 @@ CreateFileMappingW( PALIMPORT HANDLE PALAPI -OpenFileMappingA( - IN DWORD dwDesiredAccess, - IN BOOL bInheritHandle, - IN LPCSTR lpName); - -PALIMPORT -HANDLE -PALAPI OpenFileMappingW( IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, @@ -3667,11 +3449,6 @@ PALAPI UnmapViewOfFile( IN LPCVOID lpBaseAddress); -PALIMPORT -HMODULE -PALAPI -LoadLibraryA( - IN LPCSTR lpLibFileName); PALIMPORT HMODULE @@ -3682,14 +3459,6 @@ LoadLibraryW( PALIMPORT HMODULE PALAPI -LoadLibraryExA( - IN LPCSTR lpLibFileName, - IN /*Reserved*/ HANDLE hFile, - IN DWORD dwFlags); - -PALIMPORT -HMODULE -PALAPI LoadLibraryExW( IN LPCWSTR lpLibFileName, IN /*Reserved*/ HANDLE hFile, @@ -3783,14 +3552,6 @@ DisableThreadLibraryCalls( PALIMPORT DWORD PALAPI -GetModuleFileNameA( - IN HMODULE hModule, - OUT LPSTR lpFileName, - IN DWORD nSize); - -PALIMPORT -DWORD -PALAPI GetModuleFileNameW( IN HMODULE hModule, OUT LPWSTR lpFileName, @@ -4052,17 +3813,6 @@ typedef struct nlsversioninfo { PALIMPORT int PALAPI -CompareStringA( - IN LCID Locale, - IN DWORD dwCmpFlags, - IN LPCSTR lpString1, - IN int cchCount1, - IN LPCSTR lpString2, - IN int cchCount2); - -PALIMPORT -int -PALAPI CompareStringW( IN LCID Locale, IN DWORD dwCmpFlags, @@ -5095,14 +4845,6 @@ lstrcpynW( PALIMPORT DWORD PALAPI -GetEnvironmentVariableA( - IN LPCSTR lpName, - OUT LPSTR lpBuffer, - IN DWORD nSize); - -PALIMPORT -DWORD -PALAPI GetEnvironmentVariableW( IN LPCWSTR lpName, OUT LPWSTR lpBuffer, @@ -5117,13 +4859,6 @@ GetEnvironmentVariableW( PALIMPORT BOOL PALAPI -SetEnvironmentVariableA( - IN LPCSTR lpName, - IN LPCSTR lpValue); - -PALIMPORT -BOOL -PALAPI SetEnvironmentVariableW( IN LPCWSTR lpName, IN LPCWSTR lpValue); @@ -5135,12 +4870,6 @@ SetEnvironmentVariableW( #endif PALIMPORT -LPSTR -PALAPI -GetEnvironmentStringsA( - VOID); - -PALIMPORT LPWSTR PALAPI GetEnvironmentStringsW( @@ -5155,12 +4884,6 @@ GetEnvironmentStringsW( PALIMPORT BOOL PALAPI -FreeEnvironmentStringsA( - IN LPSTR); - -PALIMPORT -BOOL -PALAPI FreeEnvironmentStringsW( IN LPWSTR); @@ -5834,12 +5557,6 @@ typedef LPOSVERSIONINFOEXA LPOSVERSIONINFOEX; PALIMPORT BOOL PALAPI -GetVersionExA( - IN OUT LPOSVERSIONINFOA lpVersionInformation); - -PALIMPORT -BOOL -PALAPI GetVersionExW( IN OUT LPOSVERSIONINFOW lpVersionInformation); @@ -5972,6 +5689,9 @@ HRESULT PALAPI CoCreateGuid(OUT GUID * pguid); +#if defined FEATURE_PAL_ANSI +#include "palprivate.h" +#endif //FEATURE_PAL_ANSI /******************* C Runtime Entrypoints *******************************/ /* Some C runtime functions needs to be reimplemented by the PAL. diff --git a/src/pal/inc/palprivate.h b/src/pal/inc/palprivate.h new file mode 100644 index 0000000000..2677dd6bdd --- /dev/null +++ b/src/pal/inc/palprivate.h @@ -0,0 +1,316 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#ifndef __PAL_PRIVATE_H__ +#define __PAL_PRIVATE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +PALIMPORT +int +PALAPI +MessageBoxA( + IN LPVOID hWnd, // NOTE: diff from winuser.h + IN LPCSTR lpText, + IN LPCSTR lpCaption, + IN UINT uType); + + +PALIMPORT +HANDLE +PALAPI +CreateFileA( + IN LPCSTR lpFileName, + IN DWORD dwDesiredAccess, + IN DWORD dwShareMode, + IN LPSECURITY_ATTRIBUTES lpSecurityAttributes, + IN DWORD dwCreationDisposition, + IN DWORD dwFlagsAndAttributes, + IN HANDLE hTemplateFile); + +PALIMPORT +DWORD +PALAPI +SearchPathA( + IN LPCSTR lpPath, + IN LPCSTR lpFileName, + IN LPCSTR lpExtension, + IN DWORD nBufferLength, + OUT LPSTR lpBuffer, + OUT LPSTR *lpFilePart + ); + +PALIMPORT +BOOL +PALAPI +CopyFileA( + IN LPCSTR lpExistingFileName, + IN LPCSTR lpNewFileName, + IN BOOL bFailIfExists); + + +PALIMPORT +BOOL +PALAPI +DeleteFileA( + IN LPCSTR lpFileName); + +PALIMPORT +BOOL +PALAPI +MoveFileA( + IN LPCSTR lpExistingFileName, + IN LPCSTR lpNewFileName); + +PALIMPORT +BOOL +PALAPI +MoveFileExA( + IN LPCSTR lpExistingFileName, + IN LPCSTR lpNewFileName, + IN DWORD dwFlags); + +PALIMPORT +BOOL +PALAPI +CreateDirectoryA( + IN LPCSTR lpPathName, + IN LPSECURITY_ATTRIBUTES lpSecurityAttributes); + +PALIMPORT +HANDLE +PALAPI +FindFirstFileA( + IN LPCSTR lpFileName, + OUT LPWIN32_FIND_DATAA lpFindFileData); + +PALIMPORT +BOOL +PALAPI +FindNextFileA( + IN HANDLE hFindFile, + OUT LPWIN32_FIND_DATAA lpFindFileData); + +PALIMPORT +DWORD +PALAPI +GetFileAttributesA( + IN LPCSTR lpFileName); + +PALIMPORT +BOOL +PALAPI +SetFileAttributesA( + IN LPCSTR lpFileName, + IN DWORD dwFileAttributes); + +PALIMPORT +DWORD +PALAPI +GetFullPathNameA( + IN LPCSTR lpFileName, + IN DWORD nBufferLength, + OUT LPSTR lpBuffer, + OUT LPSTR *lpFilePart); + +PALIMPORT +UINT +PALAPI +GetTempFileNameA( + IN LPCSTR lpPathName, + IN LPCSTR lpPrefixString, + IN UINT uUnique, + OUT LPSTR lpTempFileName); + +PALIMPORT +DWORD +PALAPI +GetTempPathA( + IN DWORD nBufferLength, + OUT LPSTR lpBuffer); + +PALIMPORT +DWORD +PALAPI +GetCurrentDirectoryA( + IN DWORD nBufferLength, + OUT LPSTR lpBuffer); + +PALIMPORT +BOOL +PALAPI +SetCurrentDirectoryA( + IN LPCSTR lpPathName); + +PALIMPORT +HANDLE +PALAPI +CreateSemaphoreA( + IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, + IN LONG lInitialCount, + IN LONG lMaximumCount, + IN LPCSTR lpName); + +PALIMPORT +HANDLE +PALAPI +CreateSemaphoreExA( + IN LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, + IN LONG lInitialCount, + IN LONG lMaximumCount, + IN LPCSTR lpName, + IN /*_Reserved_*/ DWORD dwFlags, + IN DWORD dwDesiredAccess); + +PALIMPORT +HANDLE +PALAPI +CreateEventA( + IN LPSECURITY_ATTRIBUTES lpEventAttributes, + IN BOOL bManualReset, + IN BOOL bInitialState, + IN LPCSTR lpName); + +PALIMPORT +HANDLE +PALAPI +CreateMutexA( + IN LPSECURITY_ATTRIBUTES lpMutexAttributes, + IN BOOL bInitialOwner, + IN LPCSTR lpName); + +PALIMPORT +HANDLE +PALAPI +OpenMutexA( + IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCSTR lpName); + +PALIMPORT +BOOL +PALAPI +CreateProcessA( + IN LPCSTR lpApplicationName, + IN LPSTR lpCommandLine, + IN LPSECURITY_ATTRIBUTES lpProcessAttributes, + IN LPSECURITY_ATTRIBUTES lpThreadAttributes, + IN BOOL bInheritHandles, + IN DWORD dwCreationFlags, + IN LPVOID lpEnvironment, + IN LPCSTR lpCurrentDirectory, + IN LPSTARTUPINFOA lpStartupInfo, + OUT LPPROCESS_INFORMATION lpProcessInformation); + +PALIMPORT +HANDLE +PALAPI +CreateFileMappingA( + IN HANDLE hFile, + IN LPSECURITY_ATTRIBUTES lpFileMappingAttributes, + IN DWORD flProtect, + IN DWORD dwMaximumSizeHigh, + IN DWORD dwMaximumSizeLow, + IN LPCSTR lpName); + +PALIMPORT +HANDLE +PALAPI +OpenFileMappingA( + IN DWORD dwDesiredAccess, + IN BOOL bInheritHandle, + IN LPCSTR lpName); + +PALIMPORT +HMODULE +PALAPI +LoadLibraryA( + IN LPCSTR lpLibFileName); + +PALIMPORT +HMODULE +PALAPI +LoadLibraryExA( + IN LPCSTR lpLibFileName, + IN /*Reserved*/ HANDLE hFile, + IN DWORD dwFlags); + +PALIMPORT +DWORD +PALAPI +GetModuleFileNameA( + IN HMODULE hModule, + OUT LPSTR lpFileName, + IN DWORD nSize); + +#if ENABLE_DOWNLEVEL_FOR_NLS + + +PALIMPORT +int +PALAPI +CompareStringA( + IN LCID Locale, + IN DWORD dwCmpFlags, + IN LPCSTR lpString1, + IN int cchCount1, + IN LPCSTR lpString2, + IN int cchCount2); + +#endif // ENABLE_DOWNLEVEL_FOR_NLS + + +PALIMPORT +LPSTR +PALAPI +GetEnvironmentStringsA( + VOID); + +PALIMPORT +BOOL +PALAPI +SetEnvironmentVariableA( + IN LPCSTR lpName, + IN LPCSTR lpValue); + +PALIMPORT +DWORD +PALAPI +GetEnvironmentVariableA( + IN LPCSTR lpName, + OUT LPSTR lpBuffer, + IN DWORD nSize); + +PALIMPORT +BOOL +PALAPI +FreeEnvironmentStringsA( + IN LPSTR); + +PALIMPORT +BOOL +PALAPI +GetVersionExA( + IN OUT LPOSVERSIONINFOA lpVersionInformation); + +PALIMPORT +BOOL +PALAPI +RemoveDirectoryA( + IN LPCSTR lpPathName); + +PALIMPORT +BOOL +PALAPI +PAL_GetPALDirectoryA( + OUT LPSTR lpDirectoryName, + IN UINT* cchDirectoryName); + +#ifdef __cplusplus +} +#endif + +#endif// __PAL_PRIVATE_H__ diff --git a/src/pal/src/file/directory.cpp b/src/pal/src/file/directory.cpp index c6fb637d65..1d4dd430df 100644 --- a/src/pal/src/file/directory.cpp +++ b/src/pal/src/file/directory.cpp @@ -125,7 +125,7 @@ Return Value: static BOOL RemoveDirectoryHelper ( - LPSTR lpPathName, + PathCharString& lpPathName, LPDWORD dwLastError ) { @@ -133,6 +133,7 @@ RemoveDirectoryHelper ( *dwLastError = 0; FILEDosToUnixPathA( lpPathName ); + if ( !FILEGetFileNameFromSymLink(lpPathName)) { FILEGetProperNotFoundError( lpPathName, dwLastError ); @@ -142,7 +143,7 @@ RemoveDirectoryHelper ( if ( rmdir(lpPathName) != 0 ) { TRACE("Removal of directory [%s] was unsuccessful, errno = %d.\n", - lpPathName, errno); + lpPathName.GetString(), errno); switch( errno ) { @@ -172,7 +173,7 @@ RemoveDirectoryHelper ( } } else { - TRACE("Removal of directory [%s] was successful.\n", lpPathName); + TRACE("Removal of directory [%s] was successful.\n", lpPathName.GetString()); bRet = TRUE; } @@ -194,8 +195,6 @@ RemoveDirectoryA( DWORD dwLastError = 0; BOOL bRet = FALSE; PathCharString mb_dirPathString; - size_t length; - char * mb_dir; PERF_ENTRY(RemoveDirectoryA); ENTRY("RemoveDirectoryA(lpPathName=%p (%s))\n", @@ -208,24 +207,14 @@ RemoveDirectoryA( goto done; } - length = strlen(lpPathName); - mb_dir = mb_dirPathString.OpenStringBuffer(length); - if (NULL == mb_dir) + if (!mb_dirPathString.Set(lpPathName, strlen(lpPathName))) { + WARN("Set failed !\n"); dwLastError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - - if (strncpy_s (mb_dir, sizeof(char) * (length+1), lpPathName, MAX_LONGPATH) != SAFECRT_SUCCESS) - { - mb_dirPathString.CloseBuffer(length); - WARN("mb_dir is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - dwLastError = ERROR_FILENAME_EXCED_RANGE; - goto done; - } - mb_dirPathString.CloseBuffer(length); - bRet = RemoveDirectoryHelper (mb_dir, &dwLastError); + bRet = RemoveDirectoryHelper (mb_dirPathString, &dwLastError); done: if( dwLastError ) @@ -254,7 +243,7 @@ RemoveDirectoryW( DWORD dwLastError = 0; BOOL bRet = FALSE; size_t length; - char * mb_dir; + char * mb_dir = NULL; PERF_ENTRY(RemoveDirectoryW); ENTRY("RemoveDirectoryW(lpPathName=%p (%S))\n", @@ -277,25 +266,18 @@ RemoveDirectoryW( mb_size = WideCharToMultiByte( CP_ACP, 0, lpPathName, -1, mb_dir, length, NULL, NULL ); - mb_dirPathString.CloseBuffer(mb_size); if( mb_size == 0 ) { - dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpPathName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - dwLastError = ERROR_FILENAME_EXCED_RANGE; - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - dwLastError = ERROR_INTERNAL_ERROR; - } + mb_dirPathString.CloseBuffer(0); + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + dwLastError = ERROR_INTERNAL_ERROR; goto done; } + + mb_dirPathString.CloseBuffer(mb_size - 1); - if ((bRet = RemoveDirectoryHelper (mb_dir, &dwLastError))) + if ((bRet = RemoveDirectoryHelper (mb_dirPathString, &dwLastError))) { TRACE("Removal of directory [%s] was successful.\n", mb_dir); } @@ -316,13 +298,9 @@ done: Function: GetCurrentDirectoryA -See MSDN doc. --*/ DWORD -PALAPI -GetCurrentDirectoryA( - IN DWORD nBufferLength, - OUT LPSTR lpBuffer) +GetCurrentDirectoryA(PathCharString& lpBuffer) { DWORD dwDirLen = 0; DWORD dwLastError = 0; @@ -330,34 +308,36 @@ GetCurrentDirectoryA( char *current_dir; PERF_ENTRY(GetCurrentDirectoryA); - ENTRY("GetCurrentDirectoryA(nBufferLength=%u, lpBuffer=%p)\n", nBufferLength, lpBuffer); + ENTRY("GetCurrentDirectoryA(lpBuffer=%p)\n", lpBuffer.GetString()); + current_dir = lpBuffer.OpenStringBuffer(MAX_PATH); /* NULL first arg means getcwd will allocate the string */ - current_dir = PAL__getcwd( NULL, MAX_LONGPATH + 1 ); + current_dir = PAL__getcwd( current_dir, MAX_PATH); - if ( !current_dir ) + if (current_dir != NULL ) { - WARN( "PAL__getcwd returned NULL\n" ); - dwLastError = DIRGetLastErrorFromErrno(); + dwDirLen = strlen( current_dir ); + lpBuffer.CloseBuffer(dwDirLen); goto done; } - - dwDirLen = strlen( current_dir ); - - /* if the supplied buffer isn't long enough, return the required - length, including room for the NULL terminator */ - if ( nBufferLength <= dwDirLen ) + else if ( errno == ERANGE ) { - ++dwDirLen; /* include space for the NULL */ - goto done; + lpBuffer.CloseBuffer(0); + current_dir = PAL__getcwd( NULL, 0); } - else + + if ( !current_dir ) { - strcpy_s( lpBuffer, nBufferLength, current_dir ); + WARN("Getcwd failed with errno=%d [%s]\n", errno, strerror(errno)); + dwLastError = DIRGetLastErrorFromErrno(); + dwDirLen = 0; + goto done; } + dwDirLen = strlen( current_dir ); + lpBuffer.Set(current_dir, dwDirLen); + PAL_free(current_dir); done: - PAL_free( current_dir ); if ( dwLastError ) { @@ -369,6 +349,35 @@ done: return dwDirLen; } +/*++ +Function: + GetCurrentDirectoryA + +See MSDN doc. +--*/ +DWORD +PALAPI +GetCurrentDirectoryA( + IN DWORD nBufferLength, + OUT LPSTR lpBuffer) +{ + + PathCharString lpBufferString; + DWORD dwDirLen = GetCurrentDirectoryA(lpBufferString); + + /* if the supplied buffer isn't long enough, return the required + length, including room for the NULL terminator */ + if ( nBufferLength <= dwDirLen ) + { + ++dwDirLen; /* include space for the NULL */ + } + else + { + strcpy_s( lpBuffer, nBufferLength, lpBufferString ); + } + + return dwDirLen; +} /*++ Function: @@ -383,25 +392,23 @@ GetCurrentDirectoryW( OUT LPWSTR lpBuffer) { DWORD dwWideLen = 0; - DWORD dwLastError = 0; - - char *current_dir; + DWORD dwLastError = ERROR_BAD_PATHNAME; int dir_len; + PathCharString current_dir; PERF_ENTRY(GetCurrentDirectoryW); ENTRY("GetCurrentDirectoryW(nBufferLength=%u, lpBuffer=%p)\n", nBufferLength, lpBuffer); - current_dir = PAL__getcwd( NULL, MAX_LONGPATH + 1 ); - if ( !current_dir ) + dir_len = GetCurrentDirectoryA(current_dir); + + if( dir_len == 0) { - WARN( "PAL__getcwd returned NULL\n" ); dwLastError = DIRGetLastErrorFromErrno(); goto done; } - dir_len = strlen( current_dir ); dwWideLen = MultiByteToWideChar( CP_ACP, 0, current_dir, dir_len, NULL, 0 ); @@ -420,11 +427,10 @@ GetCurrentDirectoryW( } else { - ++dwWideLen; /* include space for the NULL */ + ++dwWideLen; /* include the space for the NULL */ } done: - PAL_free( current_dir ); if ( dwLastError ) { @@ -453,7 +459,7 @@ SetCurrentDirectoryW( PathCharString dirPathString; int size; size_t length; - char * dir; + char * dir = NULL; PERF_ENTRY(SetCurrentDirectoryW); ENTRY("SetCurrentDirectoryW(lpPathName=%p (%S))\n", @@ -481,25 +487,18 @@ SetCurrentDirectoryW( size = WideCharToMultiByte( CP_ACP, 0, lpPathName, -1, dir, length, NULL, NULL ); - dirPathString.CloseBuffer(size); if( size == 0 ) { + dirPathString.CloseBuffer(0); dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpPathName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - dwLastError = ERROR_FILENAME_EXCED_RANGE; - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - dwLastError = ERROR_INTERNAL_ERROR; - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + dwLastError = ERROR_INTERNAL_ERROR; bRet = FALSE; goto done; } + dirPathString.CloseBuffer(size - 1); bRet = SetCurrentDirectoryA(dir); done: if( dwLastError ) @@ -529,8 +528,9 @@ CreateDirectoryA( { BOOL bRet = FALSE; DWORD dwLastError = 0; - char *realPath; - LPSTR UnixPathName = NULL; + PathCharString realPath; + char* realPathBuf; + LPSTR unixPathName = NULL; int pathLength; int i; const int mode = S_IRWXU | S_IRWXG | S_IRWXO; @@ -556,23 +556,23 @@ CreateDirectoryA( goto done; } - UnixPathName = PAL__strdup(lpPathName); - if (UnixPathName == NULL ) + unixPathName = PAL__strdup(lpPathName); + if (unixPathName == NULL ) { ERROR("PAL__strdup() failed\n"); dwLastError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - FILEDosToUnixPathA( UnixPathName ); + FILEDosToUnixPathA( unixPathName ); // Remove any trailing slashes at the end because mkdir might not // handle them appropriately on all platforms. - pathLength = strlen(UnixPathName); + pathLength = strlen(unixPathName); i = pathLength; while(i > 1) { - if(UnixPathName[i - 1] =='/') + if(unixPathName[i - 1] =='/') { - UnixPathName[i - 1]='\0'; + unixPathName[i - 1]='\0'; i--; } else @@ -581,52 +581,42 @@ CreateDirectoryA( } } - // Check the constraint for the real path length (should be < MAX_LONGPATH). // Get an absolute path. - if (UnixPathName[0] == '/') + if (unixPathName[0] == '/') { - realPath = UnixPathName; + realPathBuf = unixPathName; } else { - const char *cwd = PAL__getcwd(NULL, MAX_LONGPATH); - if (NULL == cwd) + + DWORD len = GetCurrentDirectoryA(realPath); + if (len == 0 || !realPath.Reserve(realPath.GetCount() + pathLength + 1 )) { - WARN("Getcwd failed with errno=%d [%s]\n", errno, strerror(errno)); dwLastError = DIRGetLastErrorFromErrno(); + WARN("Getcwd failed with errno=%d \n", dwLastError); goto done; } - // Copy cwd, '/', path - int iLen = strlen(cwd) + 1 + pathLength + 1; - realPath = static_cast<char *>(alloca(iLen)); - sprintf_s(realPath, iLen, "%s/%s", cwd, UnixPathName); - - PAL_free((char *)cwd); + realPath.Append("/", 1); + realPath.Append(unixPathName, pathLength); + realPathBuf = realPath.OpenStringBuffer(realPath.GetCount()); } - + // Canonicalize the path so we can determine its length. - FILECanonicalizePath(realPath); - - if (strlen(realPath) >= MAX_LONGPATH) - { - WARN("UnixPathName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - dwLastError = ERROR_FILENAME_EXCED_RANGE; - goto done; - } + FILECanonicalizePath(realPathBuf); - if ( mkdir(realPath, mode) != 0 ) + if ( mkdir(realPathBuf, mode) != 0 ) { TRACE("Creation of directory [%s] was unsuccessful, errno = %d.\n", - UnixPathName, errno); + unixPathName, errno); switch( errno ) { case ENOTDIR: /* FALL THROUGH */ case ENOENT: - FILEGetProperNotFoundError( realPath, &dwLastError ); + FILEGetProperNotFoundError( realPathBuf, &dwLastError ); goto done; case EEXIST: dwLastError = ERROR_ALREADY_EXISTS; @@ -637,16 +627,17 @@ CreateDirectoryA( } else { - TRACE("Creation of directory [%s] was successful.\n", UnixPathName); + TRACE("Creation of directory [%s] was successful.\n", unixPathName); bRet = TRUE; } + realPath.CloseBuffer(0); //The PathCharString usage is done done: if( dwLastError ) { SetLastError( dwLastError ); } - PAL_free( UnixPathName ); + PAL_free( unixPathName ); LOGEXIT("CreateDirectoryA returns BOOL %d\n", bRet); PERF_EXIT(CreateDirectoryA); return bRet; @@ -666,7 +657,7 @@ SetCurrentDirectoryA( BOOL bRet = FALSE; DWORD dwLastError = 0; int result; - LPSTR UnixPathName = NULL; + LPSTR unixPathName = NULL; PERF_ENTRY(SetCurrentDirectoryA); ENTRY("SetCurrentDirectoryA(lpPathName=%p (%s))\n", @@ -681,24 +672,18 @@ SetCurrentDirectoryA( dwLastError = ERROR_INVALID_NAME; goto done; } - if (strlen(lpPathName) >= MAX_LONGPATH) - { - WARN("Path/directory name longer than MAX_LONGPATH characters\n"); - dwLastError = ERROR_FILENAME_EXCED_RANGE; - goto done; - } - UnixPathName = PAL__strdup(lpPathName); - if (UnixPathName == NULL ) + unixPathName = PAL__strdup(lpPathName); + if (unixPathName == NULL ) { ERROR("PAL__strdup() failed\n"); dwLastError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - FILEDosToUnixPathA( UnixPathName ); + FILEDosToUnixPathA( unixPathName ); - TRACE("Attempting to open Unix dir [%s]\n", UnixPathName); - result = chdir(UnixPathName); + TRACE("Attempting to open Unix dir [%s]\n", unixPathName); + result = chdir(unixPathName); if ( result == 0 ) { @@ -710,7 +695,7 @@ SetCurrentDirectoryA( { struct stat stat_data; - if ( stat( UnixPathName, &stat_data) == 0 && + if ( stat( unixPathName, &stat_data) == 0 && (stat_data.st_mode & S_IFMT) == S_IFREG ) { /* Not a directory, it is a file. */ @@ -718,7 +703,7 @@ SetCurrentDirectoryA( } else { - FILEGetProperNotFoundError( UnixPathName, &dwLastError ); + FILEGetProperNotFoundError( unixPathName, &dwLastError ); } TRACE("chdir() failed, path was invalid.\n"); } @@ -736,9 +721,9 @@ done: SetLastError(dwLastError); } - if(UnixPathName != NULL) + if(unixPathName != NULL) { - PAL_free( UnixPathName ); + PAL_free( unixPathName ); } LOGEXIT("SetCurrentDirectoryA returns BOOL %d\n", bRet); diff --git a/src/pal/src/file/file.cpp b/src/pal/src/file/file.cpp index d066524351..6cb4a9392d 100644 --- a/src/pal/src/file/file.cpp +++ b/src/pal/src/file/file.cpp @@ -154,7 +154,7 @@ Windows behavior. IN LPSTR lpPath - The path to check. LPDWORD lpErrorCode - The error to set. */ -void FILEGetProperNotFoundError( LPSTR lpPath, LPDWORD lpErrorCode ) +void FILEGetProperNotFoundError( LPCSTR lpPath, LPDWORD lpErrorCode ) { struct stat stat_data; LPSTR lpDupedPath = NULL; @@ -218,7 +218,7 @@ based on the Windows behavior for nonexistent filenames. IN LPSTR lpPath - The path to check. */ -PAL_ERROR FILEGetLastErrorFromErrnoAndFilename(LPSTR lpPath) +PAL_ERROR FILEGetLastErrorFromErrnoAndFilename(LPCSTR lpPath) { PAL_ERROR palError; if (ENOENT == errno) @@ -232,6 +232,19 @@ PAL_ERROR FILEGetLastErrorFromErrnoAndFilename(LPSTR lpPath) return palError; } +BOOL +CorUnix::RealPathHelper(LPCSTR lpUnixPath, PathCharString& lpBuffer) +{ + StringHolder lpRealPath; + lpRealPath = realpath(lpUnixPath, NULL); + if (lpRealPath.IsNull()) + { + return FALSE; + } + + lpBuffer.Set(lpRealPath, strlen(lpRealPath)); + return TRUE; +} /*++ InternalCanonicalizeRealPath Wraps realpath() to hide platform differences. See the man page for @@ -241,36 +254,33 @@ InternalCanonicalizeRealPath exist, this is a straight thunk through to realpath(). On other systems, we remove the last path component, then call realpath(). - cch is the size of lpBuffer and has to be atleast PATH_MAX (since - realpath() requires the buffer to be atleast PATH_MAX). --*/ PAL_ERROR -CorUnix::InternalCanonicalizeRealPath(LPCSTR lpUnixPath, LPSTR lpBuffer, DWORD cch) +CorUnix::InternalCanonicalizeRealPath(LPCSTR lpUnixPath, PathCharString& lpBuffer) { PAL_ERROR palError = NO_ERROR; - LPSTR lpRealPath = NULL; #if !REALPATH_SUPPORTS_NONEXISTENT_FILES - LPSTR lpExistingPath = NULL; + StringHolder lpExistingPath; LPSTR pchSeparator = NULL; LPSTR lpFilename = NULL; DWORD cchBuffer = 0; DWORD cchFilename = 0; #endif // !REALPATH_SUPPORTS_NONEXISTENT_FILES - if ( (lpUnixPath == NULL) || (lpBuffer == NULL) || (cch < PATH_MAX) ) + if (lpUnixPath == NULL) { ERROR ("Invalid argument to InternalCanonicalizeRealPath\n"); palError = ERROR_INVALID_PARAMETER; - goto LExitDontFree; + goto LExit; } #if REALPATH_SUPPORTS_NONEXISTENT_FILES - lpRealPath = realpath(lpUnixPath, lpBuffer); + RealPathHelper(lpUnixPath, lpBuffer); #else // !REALPATH_SUPPORTS_NONEXISTENT_FILES lpExistingPath = strdup(lpUnixPath); - if (lpExistingPath == NULL) + if (lpExistingPath.IsNull()) { ERROR ("strdup failed with error %d\n", errno); palError = ERROR_NOT_ENOUGH_MEMORY; @@ -280,18 +290,16 @@ CorUnix::InternalCanonicalizeRealPath(LPCSTR lpUnixPath, LPSTR lpBuffer, DWORD c pchSeparator = strrchr(lpExistingPath, '/'); if (pchSeparator == NULL) { - char pszCwdBuffer[MAXPATHLEN+1]; // MAXPATHLEN is for getcwd() - DWORD cchCwdBuffer = sizeof(pszCwdBuffer)/sizeof(pszCwdBuffer[0]); + PathCharString pszCwdBuffer; - if (getcwd(pszCwdBuffer, cchCwdBuffer) == NULL) + if (GetCurrentDirectoryA(pszCwdBuffer)== 0) { WARN("getcwd(NULL) failed with error %d\n", errno); palError = DIRGetLastErrorFromErrno(); goto LExit; } - lpRealPath = realpath(pszCwdBuffer, lpBuffer); - if (lpRealPath == NULL) + if (! RealPathHelper(pszCwdBuffer, lpBuffer)) { WARN("realpath() failed with error %d\n", errno); palError = FILEGetLastErrorFromErrno(); @@ -344,7 +352,7 @@ CorUnix::InternalCanonicalizeRealPath(LPCSTR lpUnixPath, LPSTR lpBuffer, DWORD c pchSeparator = lpExistingPath+strlen(lpExistingPath); // Set the lpBuffer to NULL - lpBuffer[0] = '\0'; + lpBuffer.Clear(); lpFilename = NULL; fSetFilename = false; } @@ -352,8 +360,7 @@ CorUnix::InternalCanonicalizeRealPath(LPCSTR lpUnixPath, LPSTR lpBuffer, DWORD c #endif // defined(_AMD64_) *pchSeparator = '\0'; - lpRealPath = realpath(lpExistingPath, lpBuffer); - if (lpRealPath == NULL) + if (!RealPathHelper(lpExistingPath, lpBuffer)) { WARN("realpath() failed with error %d\n", errno); palError = FILEGetLastErrorFromErrno(); @@ -392,54 +399,25 @@ CorUnix::InternalCanonicalizeRealPath(LPCSTR lpUnixPath, LPSTR lpBuffer, DWORD c lpFilename = pchSeparator + 1; } - cchBuffer = strlen(lpBuffer); - -#if defined(_AMD64_) - if (lpFilename == NULL) - cchFilename = 0; - else -#endif // _AMD64_ - cchFilename = strlen(lpFilename); - - // Append the file name. - // 2 is for "/" and NULL - if (cchBuffer + cchFilename + 2 > cch) - { - ERROR ("Provided buffer size (%d) is smaller than the required (%d)\n", - cch, cchBuffer + cchFilename + 2); - palError = ERROR_INSUFFICIENT_BUFFER; - lpRealPath = NULL; - goto LExit; - } - - lpBuffer[cchBuffer] = '/'; - lpBuffer[cchBuffer+1] = '\0'; - #if defined(_AMD64_) if (lpFilename == NULL) goto LExit; #endif // _AMD64_ - if (strcat_s(lpBuffer, cch, lpFilename) != SAFECRT_SUCCESS) + if (!lpBuffer.Append("/",1) || !lpBuffer.Append(lpFilename, strlen(lpFilename))) { - ERROR ("strcat_s failed!\n"); + ERROR ("Append failed!\n"); palError = ERROR_INSUFFICIENT_BUFFER; - lpRealPath = NULL; // Doing a goto here since we want to exit now. This will work // incase someone else adds another if clause below us. goto LExit; } -LExit: - if (lpExistingPath != NULL) - { - InternalFree(lpExistingPath); - } #endif // REALPATH_SUPPORTS_NONEXISTENT_FILES +LExit: -LExitDontFree: - if ((palError == NO_ERROR) && (lpRealPath == NULL)) + if ((palError == NO_ERROR) && lpBuffer.IsEmpty()) { // convert all these into ERROR_PATH_NOT_FOUND palError = ERROR_PATH_NOT_FOUND; @@ -471,7 +449,7 @@ CorUnix::InternalCreateFile( BOOL fFileExists = FALSE; BOOL inheritable = FALSE; - LPSTR lpUnixPath = NULL; + PathCharString lpUnixPath; int filed = -1; int create_flags = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); int open_flags = 0; @@ -482,8 +460,7 @@ CorUnix::InternalCreateFile( BOOL bFileCreated = FALSE; const char* szNonfilePrefix = "\\\\.\\"; - LPSTR lpFullUnixPath = NULL; - DWORD cchFullUnixPath = PATH_MAX+1; // InternalCanonicalizeRealPath requires this to be atleast PATH_MAX + PathCharString lpFullUnixPath; /* for dwShareMode only three flags are accepted */ if ( dwShareMode & ~(FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE) ) @@ -500,14 +477,6 @@ CorUnix::InternalCreateFile( goto done; } - if ( strlen(lpFileName) >= MAX_LONGPATH ) - { - WARN("InternalCreateFile called with a filename whose size is " - "%d >= MAX_LONGPATH (%d)\n", strlen(lpFileName), MAX_LONGPATH); - palError = ERROR_FILENAME_EXCED_RANGE; - goto done; - } - if ( strncmp(lpFileName, szNonfilePrefix, strlen(szNonfilePrefix)) == 0 ) { ERROR("InternalCreateFile does not support paths beginning with %s\n", szNonfilePrefix); @@ -515,38 +484,24 @@ CorUnix::InternalCreateFile( goto done; } - lpUnixPath = strdup(lpFileName); - if ( lpUnixPath == NULL ) + if( !lpUnixPath.Set(lpFileName,strlen(lpFileName))) { ERROR("strdup() failed\n"); palError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - lpFullUnixPath = reinterpret_cast<LPSTR>(InternalMalloc(cchFullUnixPath)); - if ( lpFullUnixPath == NULL ) - { - ERROR("InternalMalloc() failed\n"); - palError = ERROR_NOT_ENOUGH_MEMORY; - goto done; - } - - // Initialize the path to zeroes... - ZeroMemory(lpFullUnixPath, cchFullUnixPath); - FILEDosToUnixPathA( lpUnixPath ); // Compute the absolute pathname to the file. This pathname is used // to determine if two file names represent the same file. - palError = InternalCanonicalizeRealPath(lpUnixPath, lpFullUnixPath, cchFullUnixPath); + palError = InternalCanonicalizeRealPath(lpUnixPath, lpFullUnixPath); if (palError != NO_ERROR) { goto done; } - InternalFree(lpUnixPath); - lpUnixPath = lpFullUnixPath; - lpFullUnixPath = NULL; + lpUnixPath.Set(lpFullUnixPath); switch( dwDesiredAccess ) { @@ -869,16 +824,6 @@ done: pRegisteredFile->ReleaseReference(pThread); } - if (NULL != lpUnixPath) - { - InternalFree(lpUnixPath); - } - - if (NULL != lpFullUnixPath) - { - InternalFree(lpFullUnixPath); - } - if (NO_ERROR == palError && fFileExists) { palError = ERROR_ALREADY_EXISTS; @@ -1004,23 +949,17 @@ CreateFileW( size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, length, NULL, NULL ); - namePathString.CloseBuffer(size); if( size == 0 ) { + namePathString.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - palError = ERROR_FILENAME_EXCED_RANGE; - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - palError = ERROR_INTERNAL_ERROR; - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + palError = ERROR_INTERNAL_ERROR; goto done; } + + namePathString.CloseBuffer(size - 1); palError = InternalCreateFile( pThread, @@ -1097,25 +1036,19 @@ CopyFileW( src_size = WideCharToMultiByte( CP_ACP, 0, lpExistingFileName, -1, source, length, NULL, NULL ); - sourcePathString.CloseBuffer(src_size); if( src_size == 0 ) { + sourcePathString.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpExistingFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - pThread->SetLastError(ERROR_INTERNAL_ERROR); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + pThread->SetLastError(ERROR_INTERNAL_ERROR); goto done; } - + + sourcePathString.CloseBuffer(src_size - 1); length = 0; + if (lpNewFileName != NULL) { length = (PAL_wcslen(lpNewFileName)+1) * MaxWCharToAcpLengthFactor; @@ -1129,24 +1062,17 @@ CopyFileW( } dest_size = WideCharToMultiByte( CP_ACP, 0, lpNewFileName, -1, dest, length, NULL, NULL ); - destPathString.CloseBuffer(dest_size); if( dest_size == 0 ) { + destPathString.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpNewFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - pThread->SetLastError(ERROR_INTERNAL_ERROR); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + pThread->SetLastError(ERROR_INTERNAL_ERROR); goto done; } + destPathString.CloseBuffer(dest_size - 1); bRet = CopyFileA(source,dest,bFailIfExists); done: @@ -1173,61 +1099,41 @@ DeleteFileA( int result; BOOL bRet = FALSE; DWORD dwLastError = 0; - char * lpUnixFileName; - int length; - PathCharString lpUnixFileNamePS; - LPSTR lpFullUnixFileName = NULL; - DWORD cchFullUnixFileName = MAX_LONGPATH+1;// InternalCanonicalizeRealPath requires this to be atleast PATH_MAX + PathCharString lpunixFileName; + PathCharString lpFullunixFileName; PERF_ENTRY(DeleteFileA); ENTRY("DeleteFileA(lpFileName=%p (%s))\n", lpFileName?lpFileName:"NULL", lpFileName?lpFileName:"NULL"); pThread = InternalGetCurrentThread(); - length = strlen(lpFileName); - lpUnixFileName = lpUnixFileNamePS.OpenStringBuffer(length); - if (NULL == lpUnixFileName) + if( !lpunixFileName.Set(lpFileName, strlen(lpFileName))) { palError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - strcpy_s( lpUnixFileName, lpUnixFileNamePS.GetSizeOf(), lpFileName); - lpUnixFileNamePS.CloseBuffer(length); - FILEDosToUnixPathA( lpUnixFileName ); + FILEDosToUnixPathA( lpunixFileName ); - if ( !FILEGetFileNameFromSymLink(lpUnixFileName)) + if ( !FILEGetFileNameFromSymLink(lpunixFileName)) { - dwLastError = FILEGetLastErrorFromErrnoAndFilename(lpUnixFileName); + dwLastError = FILEGetLastErrorFromErrnoAndFilename(lpunixFileName); goto done; } - - lpFullUnixFileName = reinterpret_cast<LPSTR>(InternalMalloc(cchFullUnixFileName)); - if ( lpFullUnixFileName == NULL ) - { - ERROR("InternalMalloc() failed\n"); - palError = ERROR_NOT_ENOUGH_MEMORY; - goto done; - } - - // Initialize the path to zeroes... - ZeroMemory(lpFullUnixFileName, cchFullUnixFileName); // Compute the absolute pathname to the file. This pathname is used // to determine if two file names represent the same file. - palError = InternalCanonicalizeRealPath(lpUnixFileName, lpFullUnixFileName, cchFullUnixFileName); + palError = InternalCanonicalizeRealPath(lpunixFileName, lpFullunixFileName); if (palError != NO_ERROR) { - InternalFree(lpFullUnixFileName); - lpFullUnixFileName = strdup(lpUnixFileName); - if (!lpFullUnixFileName) + if (!lpFullunixFileName.Set(lpunixFileName, strlen(lpunixFileName))) { palError = ERROR_NOT_ENOUGH_MEMORY; goto done; } } - palError = g_pFileLockManager->GetFileShareModeForFile(lpFullUnixFileName, &dwShareMode); + palError = g_pFileLockManager->GetFileShareModeForFile(lpFullunixFileName, &dwShareMode); // Use unlink if we succesfully found the file to be opened with // a FILE_SHARE_DELETE mode. @@ -1244,17 +1150,17 @@ DeleteFileA( dwShareMode != SHARE_MODE_NOT_INITALIZED && (dwShareMode & FILE_SHARE_DELETE) != 0) { - result = unlink( lpFullUnixFileName ); + result = unlink( lpFullunixFileName ); } else { - result = InternalDeleteFile( lpFullUnixFileName ); + result = InternalDeleteFile( lpFullunixFileName ); } if ( result < 0 ) { TRACE("unlink returns %d\n", result); - dwLastError = FILEGetLastErrorFromErrnoAndFilename(lpFullUnixFileName); + dwLastError = FILEGetLastErrorFromErrnoAndFilename(lpFullunixFileName); } else { @@ -1266,10 +1172,6 @@ done: { pThread->SetLastError( dwLastError ); } - if (NULL != lpFullUnixFileName) - { - InternalFree(lpFullUnixFileName); - } LOGEXIT("DeleteFileA returns BOOL %d\n", bRet); PERF_EXIT(DeleteFileA); return bRet; @@ -1315,25 +1217,18 @@ DeleteFileW( size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, length, NULL, NULL ); - namePS.CloseBuffer(size); if( size == 0 ) { + namePS.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpFilePathName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - pThread->SetLastError(ERROR_INTERNAL_ERROR); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + pThread->SetLastError(ERROR_INTERNAL_ERROR); bRet = FALSE; goto done; } + namePS.CloseBuffer(size - 1); bRet = DeleteFileA( name ); done: @@ -1419,11 +1314,8 @@ MoveFileExA( { CPalThread *pThread; int result; - int length = 0; - PathCharString sourcePS; - PathCharString destPS; - char * source; - char * dest; + PathCharString source; + PathCharString dest; BOOL bRet = TRUE; DWORD dwLastError = 0; @@ -1444,29 +1336,20 @@ MoveFileExA( goto done; } - length = strlen(lpExistingFileName); - source = sourcePS.OpenStringBuffer(length); - if (NULL == source) + if( !source.Set(lpExistingFileName, strlen(lpExistingFileName))) { dwLastError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - strcpy_s( source, sourcePS.GetSizeOf(), lpExistingFileName); - sourcePS.CloseBuffer(length); FILEDosToUnixPathA( source ); - length = strlen(lpNewFileName); - - dest = destPS.OpenStringBuffer(length); - if (NULL == dest) + if( !dest.Set(lpNewFileName, strlen(lpNewFileName))) { dwLastError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - strcpy_s( dest, destPS.GetSizeOf(), lpNewFileName); - destPS.CloseBuffer(length); FILEDosToUnixPathA( dest ); @@ -1629,23 +1512,16 @@ MoveFileExW( } src_size = WideCharToMultiByte( CP_ACP, 0, lpExistingFileName, -1, source, length, NULL, NULL ); - sourcePS.CloseBuffer(src_size); if( src_size == 0 ) { + sourcePS.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpExistingFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - pThread->SetLastError(ERROR_INTERNAL_ERROR); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + pThread->SetLastError(ERROR_INTERNAL_ERROR); goto done; } + sourcePS.CloseBuffer(src_size - 1); length = 0; if (lpNewFileName != NULL) { @@ -1660,24 +1536,17 @@ MoveFileExW( } dest_size = WideCharToMultiByte( CP_ACP, 0, lpNewFileName, -1, dest, length, NULL, NULL ); - destPS.CloseBuffer(dest_size); if( dest_size == 0 ) { + destPS.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpNewFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - pThread->SetLastError(ERROR_INTERNAL_ERROR); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + pThread->SetLastError(ERROR_INTERNAL_ERROR); goto done; } + destPS.CloseBuffer(dest_size - 1); bRet = MoveFileExA(source,dest,dwFlags); done: @@ -1720,9 +1589,7 @@ GetFileAttributesA( struct stat stat_data; DWORD dwAttr = 0; DWORD dwLastError = 0; - CHAR * UnixFileName; - int length = 0; - PathCharString UnixFileNamePS; + PathCharString unixFileName; PERF_ENTRY(GetFileAttributesA); ENTRY("GetFileAttributesA(lpFileName=%p (%s))\n", lpFileName?lpFileName:"NULL", lpFileName?lpFileName:"NULL"); @@ -1734,22 +1601,18 @@ GetFileAttributesA( goto done; } - length = strlen(lpFileName); - UnixFileName = UnixFileNamePS.OpenStringBuffer(length); - if (NULL == UnixFileName) + if( !unixFileName.Set(lpFileName, strlen(lpFileName))) { dwLastError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - strcpy_s( UnixFileName, UnixFileNamePS.GetSizeOf(), lpFileName ); - UnixFileNamePS.CloseBuffer(length); - FILEDosToUnixPathA( UnixFileName ); + FILEDosToUnixPathA( unixFileName ); - if ( stat(UnixFileName, &stat_data) != 0 ) + if ( stat(unixFileName, &stat_data) != 0 ) { - dwLastError = FILEGetLastErrorFromErrnoAndFilename(UnixFileName); + dwLastError = FILEGetLastErrorFromErrnoAndFilename(unixFileName); goto done; } @@ -1833,24 +1696,20 @@ GetFileAttributesW( } size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, filename, length, NULL, NULL ); - filenamePS.CloseBuffer(size); if( size == 0 ) { + filenamePS.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - pThread->SetLastError(ERROR_INTERNAL_ERROR); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + pThread->SetLastError(ERROR_INTERNAL_ERROR); } else + { + filenamePS.CloseBuffer(size - 1); dwRet = GetFileAttributesA( filename ); + } + done: LOGEXIT("GetFileAttributesW returns DWORD %#x\n", dwRet); PERF_EXIT(GetFileAttributesW); @@ -1918,24 +1777,17 @@ GetFileAttributesExW( } size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, length, NULL, NULL ); - namePS.CloseBuffer(size); if( size == 0 ) { + namePS.CloseBuffer(0); dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - dwLastError = ERROR_FILENAME_EXCED_RANGE; - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - dwLastError = ERROR_INTERNAL_ERROR; - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + dwLastError = ERROR_INTERNAL_ERROR; goto done; } + namePS.CloseBuffer(size - 1); attr_data = (LPWIN32_FILE_ATTRIBUTE_DATA)lpFileInformation; attr_data->dwFileAttributes = GetFileAttributesW(lpFileName); @@ -2028,23 +1880,16 @@ SetFileAttributesW( } size = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, name, length, NULL, NULL ); - namePS.CloseBuffer(size); if( size == 0 ) { + namePS.CloseBuffer(0); dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - dwLastError = ERROR_FILENAME_EXCED_RANGE; - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - dwLastError = ERROR_INVALID_PARAMETER; - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + dwLastError = ERROR_INVALID_PARAMETER; goto done; } + namePS.CloseBuffer(size - 1); bRet = SetFileAttributesA(name,dwFileAttributes); done: @@ -3733,25 +3578,19 @@ GetTempFileNameW( } path_size = WideCharToMultiByte( CP_ACP, 0, lpPathName, -1, full_name, length, NULL, NULL ); - full_namePS.CloseBuffer(path_size); if( path_size == 0 ) { + full_namePS.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("lpPathName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - pThread->SetLastError(ERROR_INTERNAL_ERROR); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + pThread->SetLastError(ERROR_INTERNAL_ERROR); uRet = 0; goto done; } + full_namePS.CloseBuffer(path_size - 1); + if (lpPrefixString != NULL) { length = (PAL_wcslen(lpPrefixString)+1) * MaxWCharToAcpLengthFactor; @@ -3766,24 +3605,17 @@ GetTempFileNameW( prefix_string, MAX_LONGPATH - path_size - MAX_SEEDSIZE, NULL, NULL ); - prefix_stringPS.CloseBuffer(prefix_size); if( prefix_size == 0 ) { + prefix_stringPS.CloseBuffer(0); DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - WARN("Full name would be larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - pThread->SetLastError(ERROR_FILENAME_EXCED_RANGE); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - pThread->SetLastError(ERROR_INTERNAL_ERROR); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + pThread->SetLastError(ERROR_INTERNAL_ERROR); uRet = 0; goto done; } + prefix_stringPS.CloseBuffer(prefix_size - 1); } tempfile_name = (char*)InternalMalloc(MAX_LONGPATH); @@ -4110,7 +3942,7 @@ SetFileAttributesA( DWORD dwLastError = 0; BOOL bRet = FALSE; - LPSTR UnixFileName = NULL; + LPSTR unixFileName = NULL; PERF_ENTRY(SetFileAttributesA); ENTRY("SetFileAttributesA(lpFileName=%p (%s), dwFileAttributes=%#x)\n", @@ -4146,19 +3978,19 @@ SetFileAttributesA( goto done; } - if ((UnixFileName = strdup(lpFileName)) == NULL) + if ((unixFileName = strdup(lpFileName)) == NULL) { ERROR("strdup() failed\n"); dwLastError = ERROR_NOT_ENOUGH_MEMORY; goto done; } - FILEDosToUnixPathA( UnixFileName ); - if ( stat(UnixFileName, &stat_data) != 0 ) + FILEDosToUnixPathA( unixFileName ); + if ( stat(unixFileName, &stat_data) != 0 ) { TRACE("stat failed on %s; errno is %d (%s)\n", - UnixFileName, errno, strerror(errno)); - dwLastError = FILEGetLastErrorFromErrnoAndFilename(UnixFileName); + unixFileName, errno, strerror(errno)); + dwLastError = FILEGetLastErrorFromErrnoAndFilename(unixFileName); goto done; } @@ -4194,10 +4026,10 @@ SetFileAttributesA( bRet = TRUE; if ( new_mode != stat_data.st_mode ) { - if ( chmod(UnixFileName, new_mode) != 0 ) + if ( chmod(unixFileName, new_mode) != 0 ) { - ERROR("chmod(%s, %#x) failed\n", UnixFileName, new_mode); - dwLastError = FILEGetLastErrorFromErrnoAndFilename(UnixFileName); + ERROR("chmod(%s, %#x) failed\n", unixFileName, new_mode); + dwLastError = FILEGetLastErrorFromErrnoAndFilename(unixFileName); bRet = FALSE; } } @@ -4208,7 +4040,7 @@ done: pThread->SetLastError(dwLastError); } - InternalFree(UnixFileName); + InternalFree(unixFileName); LOGEXIT("SetFileAttributesA returns BOOL %d\n", bRet); PERF_EXIT(SetFileAttributesA); @@ -4971,27 +4803,31 @@ Input parameters: source = path to the file on input, path to the file with all symbolic links traversed on return -Note: Assumes the maximum size of the source is MAX_LONGPATH - Return value: TRUE on success, FALSE on failure --*/ -BOOL FILEGetFileNameFromSymLink(char *source) +BOOL FILEGetFileNameFromSymLink(PathCharString& source) { int ret; - char * sLinkData = (char*)InternalMalloc(MAX_LONGPATH); + PathCharString sLinkDataString; + struct stat sb; do { - ret = readlink(source, sLinkData, MAX_LONGPATH); - if (ret>0) + if (lstat(source, &sb) == -1) + { + break; + } + + char * sLinkData = sLinkDataString.OpenStringBuffer(sb.st_size); + ret = readlink(source, sLinkData, sb.st_size); + if (ret > 0) { - sLinkData[ret] = '\0'; - strcpy_s(source, sizeof(char)*(MAX_LONGPATH), sLinkData); + sLinkDataString.CloseBuffer(ret); + source.Set(sLinkDataString); } } while (ret > 0); - InternalFree(sLinkData); return (errno == EINVAL); } diff --git a/src/pal/src/file/find.cpp b/src/pal/src/file/find.cpp index 09faa9ac6a..dea860c7bf 100644 --- a/src/pal/src/file/find.cpp +++ b/src/pal/src/file/find.cpp @@ -138,13 +138,6 @@ FindFirstFileA( dwLastError = ERROR_INVALID_PARAMETER; goto done; } - if (strlen(lpFileName) >= MAX_LONGPATH) - { - WARN("FindFirstFileA called with a pattern whose size is " - "%d >= MAX_LONGPATH (%d)\n", strlen(lpFileName), MAX_LONGPATH); - dwLastError = ERROR_FILENAME_EXCED_RANGE; - goto done; - } find_data = (find_obj *)InternalMalloc(sizeof(find_obj)); if ( find_data == NULL ) diff --git a/src/pal/src/file/path.cpp b/src/pal/src/file/path.cpp index 5d34599dd1..c4ef31be32 100644 --- a/src/pal/src/file/path.cpp +++ b/src/pal/src/file/path.cpp @@ -56,7 +56,8 @@ GetFullPathNameA( OUT LPSTR *lpFilePart) { DWORD nReqPathLen, nRet = 0; - LPSTR lpUnixPath = NULL; + PathCharString unixPath; + LPSTR unixPathBuf; BOOL fullPath = FALSE; PERF_ENTRY(GetFullPathNameA); @@ -80,33 +81,18 @@ GetFullPathNameA( if(fullPath) { - lpUnixPath = PAL__strdup( lpFileName ); - if(NULL == lpUnixPath) + if( !unixPath.Set(lpFileName, strlen(lpFileName))) { - ERROR("strdup() failed; error is %d (%s)\n", - errno, strerror(errno)); + ERROR("Set() failed;\n"); SetLastError(ERROR_NOT_ENOUGH_MEMORY); goto done; } } else { - size_t max_len; - - /* allocate memory for full non-canonical path */ - max_len = strlen(lpFileName)+1; /* 1 for the slash to append */ - max_len += MAX_LONGPATH + 1; - lpUnixPath = (LPSTR)PAL_malloc(max_len); - if(NULL == lpUnixPath) - { - ERROR("PAL_malloc() failed; error is %d (%s)\n", - errno, strerror(errno)); - SetLastError(ERROR_NOT_ENOUGH_MEMORY); - goto done; - } /* build full path */ - if(!GetCurrentDirectoryA(MAX_LONGPATH + 1, lpUnixPath)) + if(!GetCurrentDirectoryA(unixPath)) { /* no reason for this to fail now... */ ASSERT("GetCurrentDirectoryA() failed! lasterror is %#xd\n", @@ -115,30 +101,29 @@ GetFullPathNameA( goto done; } - if (strcat_s(lpUnixPath, max_len, "/") != SAFECRT_SUCCESS) + if (!unixPath.Append("/", 1) || + !unixPath.Append(lpFileName,strlen(lpFileName)) + ) { - ERROR("strcat_s failed!\n"); - SetLastError(ERROR_FILENAME_EXCED_RANGE); + ERROR("Append failed!\n"); + SetLastError(ERROR_NOT_ENOUGH_MEMORY); goto done; } - if (strcat_s(lpUnixPath, max_len, lpFileName) != SAFECRT_SUCCESS) - { - ERROR("strcat_s failed!\n"); - SetLastError(ERROR_FILENAME_EXCED_RANGE); - goto done; - } } + unixPathBuf = unixPath.OpenStringBuffer(unixPath.GetCount()); /* do conversion to Unix path */ - FILEDosToUnixPathA( lpUnixPath ); + FILEDosToUnixPathA( unixPathBuf ); /* now we can canonicalize this */ - FILECanonicalizePath(lpUnixPath); + FILECanonicalizePath(unixPathBuf); /* at last, we can figure out how long this path is */ - nReqPathLen = strlen(lpUnixPath)+1; + nReqPathLen = strlen(unixPathBuf); + unixPath.CloseBuffer(nReqPathLen); + nReqPathLen++; if(nBufferLength < nReqPathLen) { TRACE("reporting insufficient buffer : minimum is %d, caller " @@ -148,7 +133,7 @@ GetFullPathNameA( } nRet = nReqPathLen-1; - strcpy_s(lpBuffer, nBufferLength, lpUnixPath); + strcpy_s(lpBuffer, nBufferLength, unixPath); /* locate the filename component if caller cares */ if(lpFilePart) @@ -169,7 +154,6 @@ GetFullPathNameA( } done: - PAL_free (lpUnixPath); LOGEXIT("GetFullPathNameA returns DWORD %u\n", nRet); PERF_EXIT(GetFullPathNameA); return nRet; @@ -191,8 +175,6 @@ GetFullPathNameW( OUT LPWSTR *lpFilePart) { LPSTR fileNameA; - /* bufferA needs to be able to hold a path that's potentially as - large as MAX_LONGPATH WCHARs. */ CHAR * bufferA; size_t bufferASize = 0; PathCharString bufferAPS; @@ -209,10 +191,6 @@ GetFullPathNameW( lpFileName?lpFileName:W16_NULLSTRING, nBufferLength, lpBuffer, lpFilePart); - /* Find the number of bytes required to convert lpFileName - to ANSI. This may be more than MAX_LONGPATH. We try to - handle that case, since it may be less than MAX_LONGPATH - WCHARs. */ fileNameLength = WideCharToMultiByte(CP_ACP, 0, lpFileName, -1, NULL, 0, NULL, NULL); @@ -234,19 +212,12 @@ GetFullPathNameW( if( srcSize == 0 ) { DWORD dwLastError = GetLastError(); - if( dwLastError == ERROR_INSUFFICIENT_BUFFER ) - { - ERROR("lpFileName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - } + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); SetLastError(ERROR_INVALID_PARAMETER); goto done; } - bufferASize = MAX_LONGPATH * MaxWCharToAcpLengthRatio; + bufferASize = nBufferLength * MaxWCharToAcpLengthRatio; bufferA = bufferAPS.OpenStringBuffer(bufferASize); if (NULL == bufferA) { @@ -724,6 +695,17 @@ FILEDosToUnixPathA( TRACE("Resulting Unix path = [%s]\n", lpPath); } +void +FILEDosToUnixPathA( + PathCharString& lpPath) +{ + + SIZE_T len = lpPath.GetCount(); + LPSTR lpPathBuf = lpPath.OpenStringBuffer(len); + FILEDosToUnixPathA(lpPathBuf); + lpPath.CloseBuffer(len); + +} /*++ Function: @@ -1143,14 +1125,8 @@ SearchPathA( provided path */ if('\\' == lpFileName[0] || '/' == lpFileName[0]) { - if(FileNameLength >= MAX_LONGPATH) - { - WARN("absolute file name <%s> is too long\n", lpFileName); - SetLastError(ERROR_INVALID_PARAMETER); - goto done; - } /* Canonicalize the path to deal with back-to-back '/', etc. */ - length = MAX_LONGPATH; + length = FileNameLength; CanonicalFullPath = CanonicalFullPathPS.OpenStringBuffer(length); if (NULL == CanonicalFullPath) { @@ -1215,14 +1191,7 @@ SearchPathA( PathLength = pPathEnd-pPathStart; - if (PathLength+FileNameLength+1 >= MAX_LONGPATH) - { - /* The path+'/'+file length is too long. Skip it. */ - WARN("path component %.*s is too long, skipping it\n", - (int)PathLength, pPathStart); - continue; - } - else if(0 == PathLength) + if(0 == PathLength) { /* empty component : there were 2 consecutive ':' */ continue; @@ -1249,7 +1218,7 @@ SearchPathA( FullPathPS.CloseBuffer(FullPathLength+1); /* Canonicalize the path to deal with back-to-back '/', etc. */ - length = MAX_LONGPATH; + length = MAX_LONGPATH; //Use it for first try CanonicalFullPath = CanonicalFullPathPS.OpenStringBuffer(length); if (NULL == CanonicalFullPath) { @@ -1415,7 +1384,7 @@ SearchPathW( if('\\' == lpFileName[0] || '/' == lpFileName[0]) { /* Canonicalize the path to deal with back-to-back '/', etc. */ - length = MAX_LONGPATH; + length = MAX_LONGPATH; //Use it for first try CanonicalPath = CanonicalPathPS.OpenStringBuffer(length); if (NULL == CanonicalPath) { @@ -1491,14 +1460,7 @@ SearchPathW( PathLength = pPathEnd-pPathStart; - if (PathLength+FileNameLength+1 >= MAX_LONGPATH) - { - /* The path+'/'+file length is too long. Skip it. */ - WARN("path component %.*S is too long, skipping it\n", - (int)PathLength, pPathStart); - continue; - } - else if(0 == PathLength) + if(0 == PathLength) { /* empty component : there were 2 consecutive ':' */ continue; @@ -1520,7 +1482,7 @@ SearchPathW( FullPathPS.CloseBuffer(FullPathLength+1); /* Canonicalize the path to deal with back-to-back '/', etc. */ - length = MAX_LONGPATH; + length = MAX_LONGPATH; //Use it for first try CanonicalPath = CanonicalPathPS.OpenStringBuffer(length); if (NULL == CanonicalPath) { diff --git a/src/pal/src/include/pal/file.h b/src/pal/src/include/pal/file.h index 99d743deb1..32174cbc41 100644 --- a/src/pal/src/include/pal/file.h +++ b/src/pal/src/include/pal/file.h @@ -23,6 +23,7 @@ Revision History: #define _PAL_FILE_H_ #include "pal/shmemory.h" +#include "pal/stackstring.hpp" #include <sys/types.h> #include <dirent.h> @@ -165,12 +166,11 @@ Input paramters: source = path to the file on input, path to the file with all symbolic links traversed on return -Note: Assumes the maximum size of the source is MAX_LONGPATH Return value: TRUE on success, FALSE on failure --*/ -BOOL FILEGetFileNameFromSymLink(char *source); +BOOL FILEGetFileNameFromSymLink(PathCharString& source); /*++ @@ -183,7 +183,7 @@ Windows behavoir. IN LPSTR lpPath - The path to check. LPDWORD lpErrorCode - The error to set. */ -void FILEGetProperNotFoundError( LPSTR lpPath, LPDWORD lpErrorCode ); +void FILEGetProperNotFoundError( LPCSTR lpPath, LPDWORD lpErrorCode ); /*++ PAL__getcwd diff --git a/src/pal/src/include/pal/file.hpp b/src/pal/src/include/pal/file.hpp index 6b4de974eb..cc9eb1ef3c 100644 --- a/src/pal/src/include/pal/file.hpp +++ b/src/pal/src/include/pal/file.hpp @@ -23,7 +23,7 @@ Revision History: #define _PAL_FILE_HPP_ #include "corunix.hpp" - +#include "pal/stackstring.hpp" #include <sys/types.h> #include <sys/param.h> #include <dirent.h> @@ -165,6 +165,8 @@ namespace CorUnix OUT LPFILETIME lpLastWriteTime ); + BOOL + RealPathHelper(LPCSTR lpUnixPath, PathCharString& lpBuffer); /*++ InternalCanonicalizeRealPath Wraps realpath() to hide platform differences. See the man page for @@ -173,15 +175,11 @@ namespace CorUnix On systems on which realpath() allows the last path component to not exist, this is a straight thunk through to realpath(). On other systems, we remove the last path component, then call realpath(). - - cch is the size of lpBuffer and has to be atleast PATH_MAX (since - realpath() requires the buffer to be atleast PATH_MAX). --*/ PAL_ERROR InternalCanonicalizeRealPath( LPCSTR lpUnixPath, - LPSTR lpBuffer, - DWORD cch + PathCharString& lpBuffer ); /*++ @@ -365,12 +363,11 @@ Input paramters: source = path to the file on input, path to the file with all symbolic links traversed on return -Note: Assumes the maximum size of the source is MAX_LONGPATH Return value: TRUE on success, FALSE on failure +BOOL FILEGetFileNameFromSymLink(PathCharString& source); --*/ -BOOL FILEGetFileNameFromSymLink(char *source); /*++ @@ -383,7 +380,7 @@ Windows behavoir. IN LPSTR lpPath - The path to check. LPDWORD lpErrorCode - The error to set. */ -void FILEGetProperNotFoundError( LPSTR lpPath, LPDWORD lpErrorCode ); +void FILEGetProperNotFoundError( LPCSTR lpPath, LPDWORD lpErrorCode ); } diff --git a/src/pal/src/include/pal/palinternal.h b/src/pal/src/include/pal/palinternal.h index 5b19ae6f1a..4c01be87ce 100644 --- a/src/pal/src/include/pal/palinternal.h +++ b/src/pal/src/include/pal/palinternal.h @@ -325,6 +325,7 @@ function_name() to call the system's implementation #define FILE PAL_FILE #include "pal.h" +#include "palprivate.h" #include "mbusafecrt.h" diff --git a/src/pal/src/include/pal/stackstring.hpp b/src/pal/src/include/pal/stackstring.hpp index 7446a67c5d..490e3f3a07 100644 --- a/src/pal/src/include/pal/stackstring.hpp +++ b/src/pal/src/include/pal/stackstring.hpp @@ -30,48 +30,69 @@ private: return; } - void ReallocateBuffer(SIZE_T count) + BOOL ReallocateBuffer(SIZE_T count) { // count is always > STACKCOUNT here. - T * newBuffer = (T *)PAL_malloc((count + 1) * sizeof(T)); + // We got so far, we will allocate a little extra + // to prevent frequent allocations +#if _DEBUG + SIZE_T count_allocated = count; +#else + SIZE_T count_allocated = count + 100; +#endif //_DEBUG + + BOOL dataOnStack = m_buffer == m_innerBuffer; + if( dataOnStack ) + { + m_buffer = NULL; + } + + T * newBuffer = (T *)PAL_realloc(m_buffer, (count_allocated + 1) * sizeof(T)); if (NULL == newBuffer) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); DeleteBuffer(); m_count = 0; + m_buffer = m_innerBuffer; + return FALSE; + } - return; + if( dataOnStack) + { + CopyMemory(newBuffer, m_innerBuffer, (m_count + 1) * sizeof(T)); } - DeleteBuffer(); m_buffer = newBuffer; m_count = count; - m_size = count+1; + m_size = count_allocated + 1; - return; + return TRUE; + } + + BOOL HasAvailableMemory(SIZE_T count) + { + return (count < m_size); } - void Resize(SIZE_T count) + //NOTE: Always call this before modifying the underlying buffer + BOOL Resize(SIZE_T count) { + if (NULL == m_buffer) { - if (count > STACKCOUNT) - { - ReallocateBuffer(count); - } - else - { - m_size = STACKCOUNT+1; - m_buffer = m_innerBuffer; - m_count = count; - } + m_buffer = m_innerBuffer; + } + + if (HasAvailableMemory(count)) + { + m_count = count; } - else if (m_innerBuffer == m_buffer) + else { if (count > STACKCOUNT) { - ReallocateBuffer(count); + return ReallocateBuffer(count); } else { @@ -79,12 +100,8 @@ private: m_size = STACKCOUNT+1; } } - else - { - ReallocateBuffer(count); - } - return; + return TRUE; } StackString(const StackString &s) @@ -94,14 +111,14 @@ private: public: StackString() - : m_buffer(m_innerBuffer), m_count(0) + : m_buffer(m_innerBuffer), m_size(0), m_count(0) { } + BOOL Set(const T * buffer, SIZE_T count) { - Resize(count); - if (NULL == m_buffer) + if (!Resize(count)) return FALSE; CopyMemory(m_buffer, buffer, (count + 1) * sizeof(T)); @@ -129,12 +146,16 @@ public: return (const T *)m_buffer; } + operator const T * () const { return GetString(); } + + //Always preserves the existing content T * OpenStringBuffer(SIZE_T count) { Resize(count); return (T *)m_buffer; } + //count should not include the terminating null void CloseBuffer(SIZE_T count) { if (m_count > count) @@ -144,10 +165,52 @@ public: return; } - ~StackString() + //Call this with the best estimate if you want to + //prevent possible reallocations on further operations + BOOL Reserve(SIZE_T count) { - DeleteBuffer(); + SIZE_T endpos = m_count; + + if (!Resize(count)) + return FALSE; + + m_count = endpos; + NullTerminate(); + + return TRUE; + } + + //count Should not include the terminating null + BOOL Append(const T * buffer, SIZE_T count) + { + SIZE_T endpos = m_count; + if (!Resize(m_count + count)) + return FALSE; + + CopyMemory(&m_buffer[endpos], buffer, (count + 1) * sizeof(T)); + NullTerminate(); + return TRUE; + } + + BOOL Append(const StackString &s) + { + return Append(s.GetString(), s.GetCount()); } + + BOOL IsEmpty() + { + return 0 == m_buffer[0]; + } + + void Clear() + { + m_count = 0; + NullTerminate(); + } + ~StackString() + { + DeleteBuffer(); + } }; #if _DEBUG @@ -158,3 +221,17 @@ typedef StackString<260, CHAR> PathCharString; typedef StackString<260, WCHAR> PathWCharString; #endif #endif + +// Some Helper Definitions +BOOL +PAL_GetPALDirectoryW( + PathWCharString& lpDirectoryName); +BOOL +PAL_GetPALDirectoryA( + PathCharString& lpDirectoryName); +DWORD +GetCurrentDirectoryA( + PathCharString& lpBuffer); +void +FILEDosToUnixPathA( + PathCharString& lpPath); diff --git a/src/pal/src/include/pal/utils.h b/src/pal/src/include/pal/utils.h index a20e077c37..3ddad4ae2f 100644 --- a/src/pal/src/include/pal/utils.h +++ b/src/pal/src/include/pal/utils.h @@ -129,5 +129,29 @@ void UTIL_SetLastErrorFromMach(kern_return_t MachReturn); #ifdef __cplusplus } #endif // __cplusplus - +class StringHolder + { + private: + LPSTR data; + public: + StringHolder() : data(NULL) { } + ~StringHolder() + { + PAL_free( data); + } + + operator LPSTR () { return data;} + + StringHolder& operator= (LPSTR value) + { + data = value; + return *this; + } + + BOOL IsNull() + { + return data == NULL; + } + + }; #endif /* _PAL_UTILS_H_ */ diff --git a/src/pal/src/init/pal.cpp b/src/pal/src/init/pal.cpp index 0855a00cee..0357ad9a55 100644 --- a/src/pal/src/init/pal.cpp +++ b/src/pal/src/init/pal.cpp @@ -39,6 +39,7 @@ Abstract: #include "pal/debug.h" #include "pal/locale.h" #include "pal/init.h" +#include "pal/stackstring.hpp" #if HAVE_MACH_EXCEPTIONS #include "../exception/machexception.h" @@ -1123,7 +1124,7 @@ Notes 2: static LPWSTR INIT_FindEXEPath(LPCSTR exe_name) { #ifndef __APPLE__ - CHAR real_path[PATH_MAX+1]; + PathCharString real_path; LPSTR env_path; LPSTR path_ptr; LPSTR cur_dir; @@ -1132,7 +1133,6 @@ static LPWSTR INIT_FindEXEPath(LPCSTR exe_name) LPWSTR return_value; INT return_size; struct stat theStats; - /* if a path is specified, only search there */ if (strchr(exe_name, '/')) { @@ -1144,7 +1144,7 @@ static LPWSTR INIT_FindEXEPath(LPCSTR exe_name) if ( UTIL_IsExecuteBitsSet( &theStats ) ) { - if(!realpath(exe_name, real_path)) + if (!CorUnix::RealPathHelper(exe_name, real_path)) { ERROR("realpath() failed!\n"); return NULL; @@ -1174,7 +1174,7 @@ static LPWSTR INIT_FindEXEPath(LPCSTR exe_name) } else { - TRACE("full path to executable is %s\n", real_path); + TRACE("full path to executable is %s\n", real_path.GetString()); } } return return_value; @@ -1274,7 +1274,7 @@ static LPWSTR INIT_FindEXEPath(LPCSTR exe_name) if( UTIL_IsExecuteBitsSet( &theStats ) ) { /* generate canonical path */ - if (!realpath(full_path, real_path)) + if (!CorUnix::RealPathHelper(full_path, real_path)) { ERROR("realpath() failed!\n"); InternalFree(full_path); @@ -1309,7 +1309,7 @@ static LPWSTR INIT_FindEXEPath(LPCSTR exe_name) else { TRACE("found %s in %s; real path is %s\n", exe_name, - cur_dir,real_path); + cur_dir,real_path.GetString()); } InternalFree(env_path); @@ -1334,7 +1334,7 @@ last_resort: { if ( UTIL_IsExecuteBitsSet( &theStats ) ) { - if (!realpath(exe_name, real_path)) + if (!CorUnix::RealPathHelper(exe_name, real_path)) { ERROR("realpath() failed!\n"); return NULL; @@ -1364,7 +1364,7 @@ last_resort: } else { - TRACE("full path to executable is %s\n", real_path); + TRACE("full path to executable is %s\n", real_path.GetString()); } } @@ -1387,17 +1387,27 @@ last_resort: #else // !__APPLE__ // On the Mac we can just directly ask the OS for the executable path. - CHAR exec_path[PATH_MAX+1]; LPWSTR return_value; INT return_size; - uint32_t bufsize = sizeof(exec_path); + PathCharString exec_pathPS; + LPSTR exec_path = exec_pathPS.OpenStringBuffer(MAX_PATH); + uint32_t bufsize = exec_pathPS.GetCount(); + + if (-1 == _NSGetExecutablePath(exec_path, &bufsize)) + { + exec_pathPS.CloseBuffer(exec_pathPS.GetCount()); + exec_path = exec_pathPS.OpenStringBuffer(bufsize); + } + if (_NSGetExecutablePath(exec_path, &bufsize)) { ASSERT("_NSGetExecutablePath failure\n"); return NULL; } + exec_pathPS.CloseBuffer(bufsize); + return_size = MultiByteToWideChar(CP_ACP,0,exec_path,-1,NULL,0); if (0 == return_size) { diff --git a/src/pal/src/loader/module.cpp b/src/pal/src/loader/module.cpp index f7febcfcc2..c7fe727c70 100644 --- a/src/pal/src/loader/module.cpp +++ b/src/pal/src/loader/module.cpp @@ -91,7 +91,6 @@ MODSTRUCT exe_module; MODSTRUCT *pal_module = nullptr; char * g_szCoreCLRPath = nullptr; -size_t g_cbszCoreCLRPath = MAX_LONGPATH * sizeof(char); int MaxWCharToAcpLength = 3; @@ -1270,14 +1269,9 @@ static bool LOADConvertLibraryPathWideStringToMultibyteString( if (*multibyteLibraryPathLengthRef == 0) { DWORD dwLastError = GetLastError(); - if (dwLastError == ERROR_INSUFFICIENT_BUFFER) - { - ERROR("wideLibraryPath converted to a multibyte string is longer than MAX_LONGPATH (%d)!\n", MAX_LONGPATH); - } - else - { - ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); - } + + ASSERT("WideCharToMultiByte failure! error is %d\n", dwLastError); + SetLastError(ERROR_INVALID_PARAMETER); return false; } @@ -1705,20 +1699,22 @@ MODSTRUCT *LOADGetPalLibrary() // Make sure it's terminated with a slash. if (g_szCoreCLRPath == nullptr) { - g_szCoreCLRPath = (char*) InternalMalloc(g_cbszCoreCLRPath); + size_t cbszCoreCLRPath = strlen(info.dli_fname) + 1; + g_szCoreCLRPath = (char*) InternalMalloc(cbszCoreCLRPath); if (g_szCoreCLRPath == nullptr) { ERROR("LOADGetPalLibrary: InternalMalloc failed!"); goto exit; } + + if (strcpy_s(g_szCoreCLRPath, cbszCoreCLRPath, info.dli_fname) != SAFECRT_SUCCESS) + { + ERROR("LOADGetPalLibrary: strcpy_s failed!"); + goto exit; + } } - if (strcpy_s(g_szCoreCLRPath, g_cbszCoreCLRPath, info.dli_fname) != SAFECRT_SUCCESS) - { - ERROR("LOADGetPalLibrary: strcpy_s failed!"); - goto exit; - } pal_module = (MODSTRUCT *)LOADLoadLibrary(info.dli_fname, FALSE); } diff --git a/src/pal/src/locale/unicode.cpp b/src/pal/src/locale/unicode.cpp index a434f3803f..c9cd15a9ea 100644 --- a/src/pal/src/locale/unicode.cpp +++ b/src/pal/src/locale/unicode.cpp @@ -978,7 +978,6 @@ PAL_BindResources(IN LPCSTR lpDomain) } DWORD size = FILEGetDirectoryFromFullPathA(g_szCoreCLRPath, len, coreCLRDirectoryPath); coreCLRDirectoryPathPS.CloseBuffer(size); - _ASSERTE(size <= MAX_LONGPATH); LPCSTR boundPath = bindtextdomain(lpDomain, coreCLRDirectoryPath); diff --git a/src/pal/src/misc/miscpalapi.cpp b/src/pal/src/misc/miscpalapi.cpp index 623fbc0c91..0e1234401e 100644 --- a/src/pal/src/misc/miscpalapi.cpp +++ b/src/pal/src/misc/miscpalapi.cpp @@ -26,6 +26,7 @@ Revision History: #include "pal/process.h" #include "pal/module.h" #include "pal/malloc.hpp" +#include "pal/stackstring.hpp" #include <errno.h> #include <unistd.h> @@ -61,20 +62,16 @@ Function : On failure it returns FALSE and sets the proper LastError code. - -See rotor_pal.doc for more details. --*/ BOOL -PALAPI -PAL_GetPALDirectoryW( OUT LPWSTR lpDirectoryName, IN UINT cchDirectoryName ) +PAL_GetPALDirectoryW(PathWCharString& lpDirectoryName) { - LPWSTR lpFullPathAndName = NULL; - LPWSTR lpEndPoint = NULL; + LPCWSTR lpFullPathAndName = NULL; + LPCWSTR lpEndPoint = NULL; BOOL bRet = FALSE; PERF_ENTRY(PAL_GetPALDirectoryW); - ENTRY( "PAL_GetPALDirectoryW( %p, %d )\n", lpDirectoryName, cchDirectoryName ); MODSTRUCT *module = LOADGetPalLibrary(); if (!module) @@ -94,41 +91,111 @@ PAL_GetPALDirectoryW( OUT LPWSTR lpDirectoryName, IN UINT cchDirectoryName ) /* The path that we return is required to have the trailing slash on the end.*/ lpEndPoint++; - } - if ( lpFullPathAndName && lpEndPoint && *lpEndPoint != '\0' ) - { - while ( cchDirectoryName - 1 && lpFullPathAndName != lpEndPoint ) - { - *lpDirectoryName = *lpFullPathAndName; - lpFullPathAndName++; - lpDirectoryName++; - cchDirectoryName--; - } - - if ( lpFullPathAndName == lpEndPoint ) - { - *lpDirectoryName = '\0'; - bRet = TRUE; - goto EXIT; - } - else + + + if(!lpDirectoryName.Set(lpFullPathAndName,lpEndPoint - lpFullPathAndName)) { ASSERT( "The buffer was not large enough.\n" ); SetLastError( ERROR_INSUFFICIENT_BUFFER ); goto EXIT; } + + bRet = TRUE; } else { ASSERT( "Unable to determine the path.\n" ); + /* Error path, should not be executed. */ + SetLastError( ERROR_INTERNAL_ERROR ); } - /* Error path, should not be executed. */ - SetLastError( ERROR_INTERNAL_ERROR ); EXIT: + PERF_EXIT(PAL_GetPALDirectoryW); + return bRet; +} + +BOOL +PAL_GetPALDirectoryA(PathCharString& lpDirectoryName) +{ + BOOL bRet; + PathWCharString directory; + + PERF_ENTRY(PAL_GetPALDirectoryA); + + bRet = PAL_GetPALDirectoryW(directory); + + if (bRet) + { + + int length = WideCharToMultiByte(CP_ACP, 0, directory.GetString(), -1, NULL, 0, NULL, 0); + LPSTR DirectoryName = lpDirectoryName.OpenStringBuffer(length); + if (NULL == DirectoryName) + { + SetLastError( ERROR_INSUFFICIENT_BUFFER ); + bRet = FALSE; + } + + length = WideCharToMultiByte(CP_ACP, 0, directory.GetString(), -1, DirectoryName, length, NULL, 0); + + if (0 == length) + { + bRet = FALSE; + length++; + } + + lpDirectoryName.CloseBuffer(length - 1); + } + + PERF_EXIT(PAL_GetPALDirectoryA); + return bRet; +} + +/*++ + +Function : + + PAL_GetPALDirectoryW + + Returns the fully qualified path name + where the PALL DLL was loaded from. + + On failure it returns FALSE and sets the + proper LastError code. + +See rotor_pal.doc for more details. + +--*/ +PALIMPORT +BOOL +PALAPI +PAL_GetPALDirectoryW( OUT LPWSTR lpDirectoryName, IN OUT UINT* cchDirectoryName ) +{ + PathWCharString directory; + BOOL bRet; + PERF_ENTRY(PAL_GetPALDirectoryW); + ENTRY( "PAL_GetPALDirectoryW( %p, %d )\n", lpDirectoryName, *cchDirectoryName ); + + bRet = PAL_GetPALDirectoryW(directory); + + if (bRet) { + + if (directory.GetCount() > *cchDirectoryName) + { + SetLastError( ERROR_INSUFFICIENT_BUFFER ); + bRet = FALSE; + } + else + { + PAL_wcscpy(lpDirectoryName, directory.GetString()); + } + + *cchDirectoryName = directory.GetCount(); + } + LOGEXIT( "PAL_GetPALDirectoryW returns BOOL %d.\n", bRet); PERF_EXIT(PAL_GetPALDirectoryW); return bRet; + } PALIMPORT @@ -136,25 +203,34 @@ BOOL PALAPI PAL_GetPALDirectoryA( OUT LPSTR lpDirectoryName, - IN UINT cchDirectoryName) + IN UINT* cchDirectoryName) { BOOL bRet; - WCHAR PALDirW[_MAX_PATH]; + PathCharString directory; PERF_ENTRY(PAL_GetPALDirectoryA); - ENTRY( "PAL_GetPALDirectoryA( %p, %d )\n", lpDirectoryName, cchDirectoryName ); + ENTRY( "PAL_GetPALDirectoryA( %p, %d )\n", lpDirectoryName, *cchDirectoryName ); - bRet = PAL_GetPALDirectoryW(PALDirW, _MAX_PATH); - if (bRet) { - if (WideCharToMultiByte(CP_ACP, 0, - PALDirW, -1, lpDirectoryName, cchDirectoryName, NULL, 0)) { - bRet = TRUE; - } else { + bRet = PAL_GetPALDirectoryA(directory); + + if (bRet) + { + if (directory.GetCount() > *cchDirectoryName) + { + SetLastError( ERROR_INSUFFICIENT_BUFFER ); + bRet = FALSE; + *cchDirectoryName = directory.GetCount(); + } + else if (strcpy_s(lpDirectoryName, directory.GetCount(), directory.GetString()) == SAFECRT_SUCCESS) + { + } + else + { bRet = FALSE; } } - LOGEXIT( "PAL_GetPALDirectoryW returns BOOL %d.\n", bRet); + LOGEXIT( "PAL_GetPALDirectoryA returns BOOL %d.\n", bRet); PERF_EXIT(PAL_GetPALDirectoryA); return bRet; } diff --git a/src/pal/src/misc/perftrace.cpp b/src/pal/src/misc/perftrace.cpp index 016a83d656..d4fba3367a 100644 --- a/src/pal/src/misc/perftrace.cpp +++ b/src/pal/src/misc/perftrace.cpp @@ -659,26 +659,30 @@ PERFlushAllLogs( ) static void -PERFLogFileName(PathCharString * destFileString, const char *fileName, const char *suffix, int max_length) +PERFLogFileName(PathCharString& destFileString, const char *fileName, const char *suffix) { const char *dir_path; CPalThread* pThread = InternalGetCurrentThread(); - char * destFileName = (char*)InternalMalloc(pThread, max_length); dir_path = (profile_log_path == NULL) ? "." : profile_log_path; - + + destFileString.Append(dir_path, strlen(dir_path)); + destFileString.Append(PATH_SEPARATOR, strlen(PATH_SEPARATOR)); if (fileName != NULL) { - snprintf(destFileName, max_length, "%s%s%s", dir_path, PATH_SEPARATOR, fileName); + destFileString.Append(fileName, strlen(fileName)); } else { - snprintf(destFileName, max_length, "%s%s%d_%d%s", dir_path, PATH_SEPARATOR, - program_info.process_id, THREADSilentGetCurrentThreadId(), suffix); + char buffer[33]; + char* process_id = itoa(program_info.process_id, buffer, 10); + destFileString.Append(process_id, strlen(process_id)); + destFileString.Append("_", 1); + + char* current_thread = itoa(THREADSilentGetCurrentThreadId(),buffer, 10); + destFileString.Append(current_thread, strlen( current_thread)); + destFileString.Append(suffix, strlen(suffix)); } - destFileString.Set(destFileName); - InternalFree(pThread, destFileName); - destFileName = NULL; } static @@ -692,7 +696,7 @@ PERFWriteCounters( pal_perf_api_info * table ) off = table; - PERFLogFileName(&fileName, profile_summary_log_name, "_perf_summary.log", MAX_LONGPATH); + PERFLogFileName(fileName, profile_summary_log_name, "_perf_summary.log"); hFile = PERF_FILEFN(fopen)(fileName, "a+"); if(hFile != NULL) { @@ -732,7 +736,7 @@ PERFWriteCounters( pal_perf_api_info * table ) if (pal_perf_histogram_size > 0) { off = table; - PERFLogFileName(&fileName, profile_summary_log_name, "_perf_summary.hist", MAX_LONGPATH); + PERFLogFileName(fileName, profile_summary_log_name, "_perf_summary.hist"); hFile = PERF_FILEFN(fopen)(fileName, "a+"); if (hFile != NULL) @@ -1085,7 +1089,7 @@ PERFFlushLog(pal_perf_thread_info * local_info, BOOL output_header) if (summary_only) return TRUE; - PERFLogFileName(&fileName, profile_time_log_name, "_perf_time.log", MAX_LONGPATH); + PERFLogFileName(fileName, profile_time_log_name, "_perf_time.log"); hFile = PERF_FILEFN(fopen)(fileName, "a+"); diff --git a/src/pal/src/misc/tracepointprovider.cpp b/src/pal/src/misc/tracepointprovider.cpp index 4df06e2b3e..8d20266688 100644 --- a/src/pal/src/misc/tracepointprovider.cpp +++ b/src/pal/src/misc/tracepointprovider.cpp @@ -22,6 +22,7 @@ Revision History: #include "pal/process.h" #include "pal/module.h" #include "pal/malloc.hpp" +#include "pal/stackstring.hpp" #include <errno.h> #include <unistd.h> @@ -67,9 +68,8 @@ PAL_InitializeTracing(void) } // Copy the path and modify the shared object name to be the tracepoint provider. - char tpProvPath[MAX_LONGPATH]; + PathCharString tpProvPath; int pathLen = strlen(info.dli_fname); - int tpLibNameLen = strlen(tpLibName); // Find the length of the full path without the shared object name, including the trailing slash. int lastTrailingSlashLen = -1; @@ -87,26 +87,23 @@ PAL_InitializeTracing(void) { return; } + + SIZE_T tpLibNameLen = strlen(tpLibName); - // Make sure that the final path is shorter than MAX_PATH. - // +1 ensures that the string can be NULL-terminated. - if((lastTrailingSlashLen + tpLibNameLen + 1) > MAX_LONGPATH) + if( !tpProvPath.Reserve(tpLibNameLen + lastTrailingSlashLen) || + // Copy the path without the shared object name. + !tpProvPath.Append(info.dli_fname, lastTrailingSlashLen) || + // Append the shared object name for the tracepoint provider. + !tpProvPath.Append(tpLibName, tpLibNameLen)) { return; } + - // Copy the path without the shared object name. - memcpy(&tpProvPath, info.dli_fname, lastTrailingSlashLen); - - // Append the shared object name for the tracepoint provider. - memcpy(&tpProvPath[lastTrailingSlashLen], &tpLibName, tpLibNameLen); - - // NULL-terminate the string. - tpProvPath[lastTrailingSlashLen + tpLibNameLen] = '\0'; // Load the tracepoint provider. // It's OK if this fails - that just means that tracing dependencies aren't available. dlopen(tpProvPath, RTLD_NOW | RTLD_GLOBAL); } -#endif
\ No newline at end of file +#endif diff --git a/src/pal/src/synchobj/event.cpp b/src/pal/src/synchobj/event.cpp index 58bc2f92a5..adb3c42fda 100644 --- a/src/pal/src/synchobj/event.cpp +++ b/src/pal/src/synchobj/event.cpp @@ -30,11 +30,6 @@ using namespace CorUnix; /* ------------------- Definitions ------------------------------*/ SET_DEFAULT_DEBUG_CHANNEL(SYNC); -enum -{ - c_cchMaxEvent = MAX_LONGPATH + 1 -}; - CObjectType CorUnix::otManualResetEvent( otiManualResetEvent, NULL, // No cleanup routine diff --git a/src/pal/src/synchobj/mutex.cpp b/src/pal/src/synchobj/mutex.cpp index 3a6c248676..ec4f1623cf 100644 --- a/src/pal/src/synchobj/mutex.cpp +++ b/src/pal/src/synchobj/mutex.cpp @@ -30,11 +30,6 @@ using namespace CorUnix; /* ------------------- Definitions ------------------------------*/ SET_DEFAULT_DEBUG_CHANNEL(SYNC); -enum -{ - c_cchMaxMutex = MAX_LONGPATH + 1 -}; - CObjectType CorUnix::otMutex( otiMutex, NULL, // No cleanup routine diff --git a/src/pal/src/synchobj/semaphore.cpp b/src/pal/src/synchobj/semaphore.cpp index d14fcdbf36..a20738b403 100644 --- a/src/pal/src/synchobj/semaphore.cpp +++ b/src/pal/src/synchobj/semaphore.cpp @@ -30,11 +30,6 @@ using namespace CorUnix; /* ------------------- Definitions ------------------------------*/ SET_DEFAULT_DEBUG_CHANNEL(SYNC); -enum -{ - c_cchMaxSemaphore = MAX_LONGPATH + 1 -}; - CObjectType CorUnix::otSemaphore( otiSemaphore, NULL, // No cleanup routine diff --git a/src/pal/src/thread/process.cpp b/src/pal/src/thread/process.cpp index 9295e8e8d1..4fd2e2d978 100644 --- a/src/pal/src/thread/process.cpp +++ b/src/pal/src/thread/process.cpp @@ -154,11 +154,11 @@ PROCGetProcessStatus( ); static BOOL getFileName(LPCWSTR lpApplicationName, LPWSTR lpCommandLine, - char *lpFileName); -static char ** buildArgv(LPCWSTR lpCommandLine, LPSTR lpAppPath, + PathCharString& lpFileName); +static char ** buildArgv(LPCWSTR lpCommandLine, PathCharString& lpAppPath, UINT *pnArg, BOOL prependLoader); -static BOOL getPath(LPCSTR lpFileName, UINT iLen, LPSTR lpPathFileName); -static int checkFileType(char *lpFileName); +static BOOL getPath(PathCharString& lpFileName, PathCharString& lpPathFileName); +static int checkFileType(LPCSTR lpFileName); static BOOL PROCEndProcess(HANDLE hProcess, UINT uExitCode, BOOL bTerminateUnconditionally); @@ -549,7 +549,6 @@ CorUnix::InternalCreateProcess( int iFdErr = -1; pid_t processId; - char * lpFileName; PathCharString lpFileNamePS; char **lppArgv = NULL; UINT nArg; @@ -664,41 +663,31 @@ CorUnix::InternalCreateProcess( } } - lpFileName = lpFileNamePS.OpenStringBuffer(MAX_LONGPATH-1); - if (NULL == lpFileName) - { - palError = ERROR_NOT_ENOUGH_MEMORY; - goto InternalCreateProcessExit; - } - if (!getFileName(lpApplicationName, lpCommandLine, lpFileName)) + if (!getFileName(lpApplicationName, lpCommandLine, lpFileNamePS)) { ERROR("Can't find executable!\n"); palError = ERROR_FILE_NOT_FOUND; goto InternalCreateProcessExit; } - lpFileNamePS.CloseBuffer(MAX_LONGPATH-1); /* check type of file */ - iRet = checkFileType(lpFileName); + iRet = checkFileType(lpFileNamePS); switch (iRet) { case FILE_ERROR: /* file not found, or not an executable */ - WARN ("File is not valid (%s)", lpFileName); + WARN ("File is not valid (%s)", lpFileNamePS.GetString()); palError = ERROR_FILE_NOT_FOUND; goto InternalCreateProcessExit; case FILE_PE: /* PE/COFF file */ - /*Get the path name where the PAL DLL was loaded from - * I am using MAX_LONGPATH - (strlen(PROCESS_PELOADER_FILENAME)+1) - * as the length as I have to append the file name at the end */ - if ( PAL_GetPALDirectoryA( lpFileName, - (MAX_LONGPATH - (strlen(PROCESS_PELOADER_FILENAME)+1)))) + //Get the path name where the PAL DLL was loaded from + if ( PAL_GetPALDirectoryA( lpFileNamePS )) { - if ((strcat_s(lpFileName, lpFileNamePS.GetSizeOf(), "/") != SAFECRT_SUCCESS) || - (strcat_s(lpFileName, lpFileNamePS.GetSizeOf(), PROCESS_PELOADER_FILENAME) != SAFECRT_SUCCESS)) + if (lpFileNamePS.Append("/", 1) == FALSE || + lpFileNamePS.Append( PROCESS_PELOADER_FILENAME, strlen(PROCESS_PELOADER_FILENAME)) == FALSE) { - ERROR("strcpy_s/strcat_s failed!\n"); + ERROR("Append failed!\n"); palError = ERROR_INTERNAL_ERROR; goto InternalCreateProcessExit; } @@ -717,7 +706,7 @@ CorUnix::InternalCreateProcess( break; /* nothing to do */ case FILE_DIR:/*Directory*/ - WARN ("File is a Directory (%s)", lpFileName); + WARN ("File is a Directory (%s)", lpFileNamePS.GetString()); palError = ERROR_ACCESS_DENIED; goto InternalCreateProcessExit; break; @@ -730,7 +719,7 @@ CorUnix::InternalCreateProcess( /* build Argument list, lppArgv is allocated in buildArgv function and requires to be freed */ - lppArgv = buildArgv(lpCommandLine, lpFileName, &nArg, iRet==1); + lppArgv = buildArgv(lpCommandLine, lpFileNamePS, &nArg, iRet==1); /* set the Environment variable */ if (lpEnvironment != NULL) @@ -972,11 +961,11 @@ CorUnix::InternalCreateProcess( if (EnvironmentArray) { - execve(lpFileName, lppArgv, EnvironmentArray); + execve(lpFileNamePS, lppArgv, EnvironmentArray); } else { - execve(lpFileName, lppArgv, palEnvironment); + execve(lpFileNamePS, lppArgv, palEnvironment); } /* if we get here, it means the execve function call failed so just exit */ @@ -3593,7 +3582,7 @@ BOOL getFileName( LPCWSTR lpApplicationName, LPWSTR lpCommandLine, - char *lpPathFileName) + PathCharString& lpPathFileName) { LPWSTR lpEnd; WCHAR wcEnd; @@ -3603,32 +3592,43 @@ getFileName( if (lpApplicationName) { - int path_size = MAX_LONGPATH; - lpTemp = lpPathFileName; + int length = WideCharToMultiByte(CP_ACP, 0, lpApplicationName, -1, + NULL, 0, NULL, NULL); + /* if only a file name is specified, prefix it with "./" */ if ((*lpApplicationName != '.') && (*lpApplicationName != '/') && (*lpApplicationName != '\\')) { - if (strcpy_s(lpPathFileName, MAX_LONGPATH, "./") != SAFECRT_SUCCESS) + length += 2; + lpTemp = lpPathFileName.OpenStringBuffer(length); + + if (strcpy_s(lpTemp, length, "./") != SAFECRT_SUCCESS) { ERROR("strcpy_s failed!\n"); return FALSE; } - lpTemp+=2; - path_size -= 2; + + } + else + { + lpTemp = lpPathFileName.OpenStringBuffer(length); } /* Convert to ASCII */ - if (!WideCharToMultiByte(CP_ACP, 0, lpApplicationName, -1, - lpTemp, path_size, NULL, NULL)) + length = WideCharToMultiByte(CP_ACP, 0, lpApplicationName, -1, + lpTemp, length, NULL, NULL); + if (length == 0) { + lpPathFileName.CloseBuffer(0); ASSERT("WideCharToMultiByte failure\n"); return FALSE; } + + lpPathFileName.CloseBuffer(length -1); /* Replace '\' by '/' */ - FILEDosToUnixPathA(lpPathFileName); + FILEDosToUnixPathA(lpTemp); return TRUE; } @@ -3696,14 +3696,14 @@ getFileName( return FALSE; } - lpFileNamePS.CloseBuffer(size); + lpFileNamePS.CloseBuffer(size - 1); /* restore last character */ *lpEnd = wcEnd; /* Replace '\' by '/' */ FILEDosToUnixPathA(lpFileName); - if (!getPath(lpFileName, MAX_LONGPATH, lpPathFileName)) + if (!getPath(lpFileNamePS, lpPathFileName)) { /* file is not in the path */ return FALSE; @@ -3837,7 +3837,7 @@ Determines if the passed in file is a managed executable --*/ static int -isManagedExecutable(LPSTR lpFileName) +isManagedExecutable(LPCSTR lpFileName) { HANDLE hFile = INVALID_HANDLE_VALUE; DWORD cbRead; @@ -3907,7 +3907,7 @@ Return: --*/ static int -checkFileType( char *lpFileName) +checkFileType( LPCSTR lpFileName) { struct stat stat_data; @@ -3981,7 +3981,7 @@ static char ** buildArgv( LPCWSTR lpCommandLine, - LPSTR lpAppPath, + PathCharString& lpAppPath, UINT *pnArg, BOOL prependLoader) { @@ -4005,7 +4005,7 @@ buildArgv( pThread = InternalGetCurrentThread(); /* make sure to allocate enough space, up for the worst case scenario */ - int iLength = (iWlen + strlen(PROCESS_PELOADER_FILENAME) + strlen(lpAppPath) + 2); + int iLength = (iWlen + strlen(PROCESS_PELOADER_FILENAME) + lpAppPath.GetCount() + 2); lpAsciiCmdLine = (char *) InternalMalloc(iLength); if (lpAsciiCmdLine == NULL) @@ -4263,7 +4263,6 @@ Abstract: Parameters: IN lpFileName: file name to search in the path - IN iLen: length of lpPathFileName buffer OUT lpPathFileName: returned string containing the path and the filename Return: @@ -4273,9 +4272,8 @@ Return: static BOOL getPath( - LPCSTR lpFileName, - UINT iLen, - LPSTR lpPathFileName) + PathCharString& lpFileNameString, + PathCharString& lpPathFileName) { LPSTR lpPath; LPSTR lpNext; @@ -4285,15 +4283,16 @@ getPath( INT nextLen; INT slashLen; CPalThread *pThread = NULL; + LPCSTR lpFileName = lpFileNameString.GetString(); /* if a path is specified, only look there */ if(strchr(lpFileName, '/')) { if (access (lpFileName, F_OK) == 0) { - if (strcpy_s(lpPathFileName, iLen, lpFileName) != SAFECRT_SUCCESS) + if (lpPathFileName.Set(lpFileNameString)) { - TRACE("strcpy_s failed!\n"); + TRACE("Set of StackString failed!\n"); return FALSE; } @@ -4313,43 +4312,49 @@ getPath( if (lpwstr) { /* convert path to multibyte, check buffer size */ - n = WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, lpPathFileName, iLen, + n = WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, NULL, 0, NULL, NULL); - if (n == 0) + + if (!lpPathFileName.Reserve(n + lpFileNameString.GetCount() + 1 )) { - ASSERT("WideCharToMultiByte failure!\n"); + ERROR("StackString Reserve failed!\n"); return FALSE; } - n += strlen(lpFileName) + 2; - if (n > (INT)iLen) - { - ERROR("Buffer too small for full path!\n"); - return FALSE; - } + lpPath = lpPathFileName.OpenStringBuffer(n); + + n = WideCharToMultiByte(CP_ACP, 0, lpwstr, -1, lpPath, n, + NULL, NULL); - if ((strcat_s(lpPathFileName, iLen, "/") != SAFECRT_SUCCESS) || - (strcat_s(lpPathFileName, iLen, lpFileName) != SAFECRT_SUCCESS)) + if (n == 0) { - ERROR("strcat_s failed!\n"); + lpPathFileName.CloseBuffer(0); + ASSERT("WideCharToMultiByte failure!\n"); return FALSE; } + + lpPathFileName.CloseBuffer(n - 1); + + lpPathFileName.Append("/", 1); + lpPathFileName.Append(lpFileNameString); if (access(lpPathFileName, F_OK) == 0) { - TRACE("found %s in application directory (%s)\n", lpFileName, lpPathFileName); + TRACE("found %s in application directory (%s)\n", lpFileName, lpPathFileName.GetString()); return TRUE; } } /* then try the current directory */ - if ((strcpy_s(lpPathFileName, iLen, "./") != SAFECRT_SUCCESS) || - (strcat_s(lpPathFileName, iLen, lpFileName) != SAFECRT_SUCCESS)) + if (!lpPathFileName.Reserve(lpFileNameString.GetCount() + 2)) { - ERROR("strcpy_s/strcat_s failed!\n"); + ERROR("StackString Reserve failed!\n"); return FALSE; } + lpPathFileName.Set("./", 2); + lpPathFileName.Append(lpFileNameString); + if (access (lpPathFileName, R_OK) == 0) { TRACE("found %s in current directory.\n", lpFileName); @@ -4389,25 +4394,24 @@ getPath( nextLen = strlen(lpNext); slashLen = (lpNext[nextLen-1] == '/') ? 0:1; - /* verify if the path fit in the OUT parameter */ - if (slashLen + nextLen + strlen (lpFileName) >= iLen) + if (!lpPathFileName.Reserve(nextLen + lpFileNameString.GetCount() + 1)) { InternalFree(lpPath); - ERROR("buffer too small for full path\n"); + ERROR("StackString ran out of memory for full path\n"); return FALSE; } - - strcpy_s (lpPathFileName, iLen, lpNext); - - /* append a '/' if there's no '/' at the end of the path */ - if ( slashLen == 1 ) + + lpPathFileName.Set(lpNext, nextLen); + + if( slashLen == 1) { - strcat_s (lpPathFileName, iLen, "/"); + /* append a '/' if there's no '/' at the end of the path */ + lpPathFileName.Append("/", 1); } - - strcat_s (lpPathFileName, iLen, lpFileName); - if (access (lpPathFileName, F_OK) == 0) + lpPathFileName.Append(lpFileNameString); + + if ( access (lpPathFileName, F_OK) == 0) { TRACE("Found %s in $PATH element %s\n", lpFileName, lpNext); InternalFree(lpPath); diff --git a/src/pal/tests/palsuite/common/palsuite.h b/src/pal/tests/palsuite/common/palsuite.h index f6712490ac..ef644ad8e5 100644 --- a/src/pal/tests/palsuite/common/palsuite.h +++ b/src/pal/tests/palsuite/common/palsuite.h @@ -19,6 +19,7 @@ #include <pal_assert.h> #include <pal.h> +#include <palprivate.h> enum { diff --git a/src/pal/tests/palsuite/file_io/FindFirstFileA/test1/FindFirstFileA.c b/src/pal/tests/palsuite/file_io/FindFirstFileA/test1/FindFirstFileA.c index 8da342d802..6ceb6a9747 100644 --- a/src/pal/tests/palsuite/file_io/FindFirstFileA/test1/FindFirstFileA.c +++ b/src/pal/tests/palsuite/file_io/FindFirstFileA/test1/FindFirstFileA.c @@ -100,22 +100,6 @@ int __cdecl main(int argc, char *argv[]) // - // find a file that is longer than MAX_LONGPATH characters - // - hFind = FindFirstFileA(szLongFindName, &findFileData); - if (hFind != INVALID_HANDLE_VALUE) - { - Fail ("FindFirstFileA: ERROR -> Found non-existent file longer than MAX_LONGPATH characters"); - } - else if (GetLastError() != ERROR_FILENAME_EXCED_RANGE) - { - // Confirm that the failure to find is due to long file name - Fail ("FindFirstFileA: ERROR -> Failed as expected on a non-existent " - "file longer than MAX_LONGPATH characters, but reporting some other " - "error: %d\n", GetLastError()); - } - - // // find a file that exists // pFile = fopen(szFindName, "w"); diff --git a/src/pal/tests/palsuite/file_io/SetCurrentDirectoryA/test1/SetCurrentDirectoryA.c b/src/pal/tests/palsuite/file_io/SetCurrentDirectoryA/test1/SetCurrentDirectoryA.c index 5513ce6131..c07a62412b 100644 --- a/src/pal/tests/palsuite/file_io/SetCurrentDirectoryA/test1/SetCurrentDirectoryA.c +++ b/src/pal/tests/palsuite/file_io/SetCurrentDirectoryA/test1/SetCurrentDirectoryA.c @@ -205,27 +205,7 @@ int __cdecl main(int argc, char *argv[]) Fail("SetCurrentDirectoryA: ERROR -> RemoveDirectoryW failed " "with error code: %ld.\n", GetLastError()); } - - /* Test that SetCurrentDirectoryA fails with paths longer than - MAX_PATH characters. Note that we cannot actually create this - directory and try to SetCurrentDirectory into it. */ - if (SetCurrentDirectoryA(szLongDirName) != TRUE) - { - /* Failure is expected because the directory doesn't exist and - it is longer than MAX_PATH characters. Check that the failure - is actually because of invalid argument. */ - if (GetLastError() != ERROR_FILENAME_EXCED_RANGE) - { - Fail("SetCurrentDirectoryA with a long path failed with some " - "other error: %d\n", GetLastError()); - } - } - else - { - Fail("SetCurrentDirectoryA succeeded with a path longer than " - "MAX_PATH characters\n"); - } - + free(szwPtr); PAL_Terminate(); diff --git a/src/pal/tests/palsuite/pal_specific/PAL_GetPALDirectoryW/test1/PAL_GetPALDirectoryW.c b/src/pal/tests/palsuite/pal_specific/PAL_GetPALDirectoryW/test1/PAL_GetPALDirectoryW.c index 1aed36fc76..2c67aa4954 100644 --- a/src/pal/tests/palsuite/pal_specific/PAL_GetPALDirectoryW/test1/PAL_GetPALDirectoryW.c +++ b/src/pal/tests/palsuite/pal_specific/PAL_GetPALDirectoryW/test1/PAL_GetPALDirectoryW.c @@ -37,8 +37,9 @@ int __cdecl main(int argc, char *argv[]) Fail("\nFailed to allocate memory for storing directory name!\n"); } + UINT size = MAX_PATH; /*retrieve the machine configuration directory*/ - bValue = PAL_GetPALDirectoryW(wpDirectoryName, MAX_PATH); + bValue = PAL_GetPALDirectoryW(wpDirectoryName, &size); if(FALSE == bValue) { free(wpDirectoryName); diff --git a/src/utilcode/longfilepathwrappers.cpp b/src/utilcode/longfilepathwrappers.cpp index 2cce3b5272..2b5a8b48a6 100644 --- a/src/utilcode/longfilepathwrappers.cpp +++ b/src/utilcode/longfilepathwrappers.cpp @@ -1230,12 +1230,15 @@ BOOL PAL_GetPALDirectoryWrapper(SString& pbuffer) BOOL PAL_GetPALDirectoryWrapper(SString& pbuffer) { - BOOL retval; - COUNT_T size = MAX_LONGPATH; //Retry once the PAL Api is fixed to return the correct size - WCHAR* buffer = pbuffer.OpenUnicodeBuffer(size - 1); + BOOL retval = FALSE; + COUNT_T size = MAX_LONGPATH; + + if(!(retval = PAL_GetPALDirectoryW(pbuffer.OpenUnicodeBuffer(size - 1), &size))) + { + pbuffer.CloseBuffer(0); + retval = PAL_GetPALDirectoryW(pbuffer.OpenUnicodeBuffer(size - 1), &size); + } - retval = PAL_GetPALDirectoryW(pbuffer.OpenUnicodeBuffer(size - 1), size); - size = (COUNT_T)wcslen(buffer); pbuffer.CloseBuffer(size); return retval; |