From: yaoyongjie yaoyongjie@uniontech.com
In CopyFileEx, by default, the file name and path are limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, need prepend "\\?\" to the path. --- dlls/kernel32/tests/file.c | 8 ++--- dlls/kernelbase/file.c | 67 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-)
diff --git a/dlls/kernel32/tests/file.c b/dlls/kernel32/tests/file.c index 2d00ddf6994..690c9f27ede 100644 --- a/dlls/kernel32/tests/file.c +++ b/dlls/kernel32/tests/file.c @@ -1047,12 +1047,12 @@ static void test_CopyFileW(void) wcscat(long_path_1, long_name_1); SetLastError(0xdeadbeef); ret = CopyFileExW(source, long_path_1, NULL, NULL, NULL, 0); - todo_wine ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND, "Expected CopyFileExW failed with ERROR_PATH_NOT_FOUND, but got %ld, copy %s -> %s\n", GetLastError(), wine_dbgstr_w(source), wine_dbgstr_w(long_path_1)); + ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND, "Expected CopyFileExW failed with ERROR_PATH_NOT_FOUND, but got %ld, copy %s -> %s\n", GetLastError(), wine_dbgstr_w(source), wine_dbgstr_w(long_path_1)); wcscpy(long_path_2, temp_path); wcscat(long_path_2, long_name_2); SetLastError(0xdeadbeef); ret = CopyFileExW(long_path_1, long_path_2, NULL, NULL, NULL, 0); - todo_wine ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND, "Expected CopyFileExW failed with ERROR_PATH_NOT_FOUND, but got %ld, copy %s -> %s\n", GetLastError(), wine_dbgstr_w(long_path_1), wine_dbgstr_w(long_path_2)); + ok(!ret && GetLastError() == ERROR_PATH_NOT_FOUND, "Expected CopyFileExW failed with ERROR_PATH_NOT_FOUND, but got %ld, copy %s -> %s\n", GetLastError(), wine_dbgstr_w(long_path_1), wine_dbgstr_w(long_path_2)); ret = DeleteFileW(long_path_1); todo_wine ok(!ret, "Unexpected DeleteFileW successed\n"); ret = DeleteFileW(long_path_2); @@ -1432,12 +1432,12 @@ static void test_CopyFileEx(void) strcat(long_path_1, long_name_1); SetLastError(0xdeadbeef); retok = CopyFileExA(source, long_path_1, NULL, NULL, NULL, 0); - todo_wine ok(!retok && GetLastError() == ERROR_PATH_NOT_FOUND, "Expected CopyFileExA failed with ERROR_PATH_NOT_FOUND, but got %ld, copy %s -> %s\n", GetLastError(), source, long_path_1); + ok(!retok && GetLastError() == ERROR_PATH_NOT_FOUND, "Expected CopyFileExA failed with ERROR_PATH_NOT_FOUND, but got %ld, copy %s -> %s\n", GetLastError(), source, long_path_1); strcpy(long_path_2, temp_path); strcat(long_path_2, long_name_2); SetLastError(0xdeadbeef); retok = CopyFileExA(long_path_1, long_path_2, NULL, NULL, NULL, 0); - todo_wine ok(!retok && GetLastError() == ERROR_PATH_NOT_FOUND, "Expected CopyFileExA failed with ERROR_PATH_NOT_FOUND, but got %ld, copy %s -> %s\n", GetLastError(), long_path_1, long_path_2); + ok(!retok && GetLastError() == ERROR_PATH_NOT_FOUND, "Expected CopyFileExA failed with ERROR_PATH_NOT_FOUND, but got %ld, copy %s -> %s\n", GetLastError(), long_path_1, long_path_2); retok = DeleteFileA(long_path_1); todo_wine ok(!retok, "Unexpected DeleteFileA successed\n"); retok = DeleteFileA(long_path_2); diff --git a/dlls/kernelbase/file.c b/dlls/kernelbase/file.c index a2c34a55b40..7788056aa72 100644 --- a/dlls/kernelbase/file.c +++ b/dlls/kernelbase/file.c @@ -69,6 +69,61 @@ const WCHAR system_dir[] = L"C:\windows\system32";
static BOOL oem_file_apis;
+/* Windows 10, version 1607, building number 14393, Redstone, August 2, 2016 */ +static BOOL is_win10_1607_or_later(void) +{ + static RTL_OSVERSIONINFOEXW rovi = { 0 }; + if (rovi.dwOSVersionInfoSize == 0) + { + rovi.dwOSVersionInfoSize = sizeof(rovi); + if (RtlGetVersion(&rovi) == ERROR_SUCCESS) + { + TRACE("windows version: win %ld, build %ld\n", rovi.dwMajorVersion, rovi.dwBuildNumber); + } + else + { + ERR("RtlGetVersion failed\n"); + rovi.dwMajorVersion = 0; + rovi.dwBuildNumber = 0; + } + } + + return rovi.dwMajorVersion >= 10 && rovi.dwBuildNumber >= 14393; +} + +static BOOL is_longpath_enabled_in_reg(void) +{ + static DWORD LongPathEnabled = -1; + if (LongPathEnabled == -1) + { + DWORD ret; + HKEY hkey = (HKEY)0xdeadbeef; + + ret = RegOpenKeyExA( HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\FileSystem", 0, KEY_READ, &hkey); + if (ret == ERROR_SUCCESS) + { + DWORD type, size = sizeof(LongPathEnabled); + ret = RegQueryValueExA(hkey, "LongPathsEnabled", NULL, &type, (LPBYTE)&LongPathEnabled, &size); + RegCloseKey(hkey); + } + else + { + LongPathEnabled = 0; + } + TRACE("in registry LongPathsEnabled: %ld\n", LongPathEnabled); + } + return LongPathEnabled == 1; +} + +static BOOL is_longpath_enabled_in_manifest(void) +{ + return RtlGetCurrentPeb()->IsLongPathAwareProcess != 0; +} + +static BOOL is_longpath_enabled(void) +{ + return is_win10_1607_or_later() && is_longpath_enabled_in_reg() && is_longpath_enabled_in_manifest(); +}
static void WINAPI read_write_apc( void *apc_user, PIO_STATUS_BLOCK io, ULONG reserved ) { @@ -512,6 +567,18 @@ static BOOL copy_file( const WCHAR *source, const WCHAR *dest, COPYFILE2_EXTENDE SetLastError( ERROR_INVALID_PARAMETER ); return FALSE; } + + if (!is_longpath_enabled() && wcsncmp(source, L"\\?\", 4) && wcslen(source) >= MAX_PATH ) + { + SetLastError( ERROR_PATH_NOT_FOUND ); + return FALSE; + } + if (!is_longpath_enabled() && wcsncmp(dest, L"\\?\", 4) && wcslen(dest) >= MAX_PATH ) + { + SetLastError( ERROR_PATH_NOT_FOUND ); + return FALSE; + } + if (!(buffer = HeapAlloc( GetProcessHeap(), 0, buffer_size ))) { SetLastError( ERROR_NOT_ENOUGH_MEMORY );