Fixed issue with long paths > 260 chars on Windows

This commit is contained in:
Michaël Palomas 2020-12-22 10:14:54 +01:00
parent 7dcfd7a038
commit adbfce14f2

View File

@ -36,9 +36,11 @@
void *fopen_utf8(const char * filename, const char * mode)
{
const char * filename_local = NULL;
#if defined(LEGACY_WIN32)
FILE *ret = NULL;
char * filename_local = utf8_to_local_string_alloc(filename);
filename_local = utf8_to_local_string_alloc(filename);
if (!filename_local)
return NULL;
@ -47,7 +49,17 @@ void *fopen_utf8(const char * filename, const char * mode)
free(filename_local);
return ret;
#else
wchar_t * filename_w = utf8_to_utf16_string_alloc(filename);
#ifdef _WIN32
// prefix to tell Windows to bypass the ~260 characters limit in many I/O APIs
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
const char * windows_long_prefix = "\\\\?\\";
char long_filename[PATH_MAX_LENGTH];
snprintf(long_filename, PATH_MAX_LENGTH, "%s%s", windows_long_prefix, filename);
filename_local = long_filename;
#else
filename_local = filename;
#endif
wchar_t * filename_w = utf8_to_utf16_string_alloc(filename_local);
wchar_t * mode_w = utf8_to_utf16_string_alloc(mode);
FILE* ret = _wfopen(filename_w, mode_w);
free(filename_w);