From adbfce14f2c809d49ee10635ea13f2d22b01bc80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Palomas?= Date: Tue, 22 Dec 2020 10:14:54 +0100 Subject: [PATCH] Fixed issue with long paths > 260 chars on Windows --- libretro-common/compat/fopen_utf8.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libretro-common/compat/fopen_utf8.c b/libretro-common/compat/fopen_utf8.c index ea16961535..f40944f416 100644 --- a/libretro-common/compat/fopen_utf8.c +++ b/libretro-common/compat/fopen_utf8.c @@ -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);