git/compat/win32
Karsten Blees 678ab6e1e6 Win32: support long paths
Windows paths are typically limited to MAX_PATH = 260 characters, even
though the underlying NTFS file system supports paths up to 32,767 chars.
This limitation is also evident in Windows Explorer, cmd.exe and many
other applications (including IDEs).

Particularly annoying is that most Windows APIs return bogus error codes
if a relative path only barely exceeds MAX_PATH in conjunction with the
current directory, e.g. ERROR_PATH_NOT_FOUND / ENOENT instead of the
infinitely more helpful ERROR_FILENAME_EXCED_RANGE / ENAMETOOLONG.

Many Windows wide char APIs support longer than MAX_PATH paths through the
file namespace prefix ('\\?\' or '\\?\UNC\') followed by an absolute path.
Notable exceptions include functions dealing with executables and the
current directory (CreateProcess, LoadLibrary, Get/SetCurrentDirectory) as
well as the entire shell API (ShellExecute, SHGetSpecialFolderPath...).

Introduce a handle_long_path function to check the length of a specified
path properly (and fail with ENAMETOOLONG), and to optionally expand long
paths using the '\\?\' file namespace prefix. Short paths will not be
modified, so we don't need to worry about device names (NUL, CON, AUX).

Contrary to MSDN docs, the GetFullPathNameW function doesn't seem to be
limited to MAX_PATH (at least not on Win7), so we can use it to do the
heavy lifting of the conversion (translate '/' to '\', eliminate '.' and
'..', and make an absolute path).

Add long path error checking to xutftowcs_path for APIs with hard MAX_PATH
limit.

Add a new MAX_LONG_PATH constant and xutftowcs_long_path function for APIs
that support long paths.

While improved error checking is always active, long paths support must be
explicitly enabled via 'core.longpaths' option. This is to prevent end
users to shoot themselves in the foot by checking out files that Windows
Explorer, cmd/bash or their favorite IDE cannot handle.

Test suite:
Test the case is when the full pathname length of a dir is close
to 260 (MAX_PATH).
Bug report and an original reproducer by Andrey Rogozhnikov:
https://github.com/msysgit/git/pull/122#issuecomment-43604199

[jes: adjusted test number to avoid conflicts, added support for
chdir(), etc]

Thanks-to: Martin W. Kirst <maki@bitkings.de>
Thanks-to: Doug Kelly <dougk.ff7@gmail.com>
Signed-off-by: Karsten Blees <blees@dcon.de>
Original-test-by: Andrey Rogozhnikov <rogozhnikov.andrey@gmail.com>
Signed-off-by: Stepan Kasal <kasal@ucw.cz>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2019-06-03 23:04:43 +02:00
..
alloca.h mingw: activate alloca 2014-04-09 10:08:35 -07:00
dirent.c Win32: support long paths 2019-06-03 23:04:43 +02:00
dirent.h Win32: Make the dirent implementation pluggable 2019-06-03 23:04:39 +02:00
fscache.c Win32: support long paths 2019-06-03 23:04:43 +02:00
fscache.h fscache: make fscache_enable() thread safe 2019-06-03 23:04:42 +02:00
git.manifest mingw: Embed a manifest to trick UAC into Doing The Right Thing 2019-06-03 22:59:29 +02:00
lazyload.h Win32: simplify loading of DLL functions 2017-09-26 11:02:49 +09:00
ntifs.h fscache: teach fscache to use NtQueryDirectoryFile 2019-06-03 23:04:42 +02:00
path-utils.c git clone <url> C:\cygwin\home\USER\repo' is working (again) 2018-12-26 15:26:17 -08:00
path-utils.h compat/win32/path-utils.h: add #include guards 2019-06-03 22:59:28 +02:00
pthread.c win32: replace pthread_cond_*() with much simpler code 2018-11-14 15:14:22 +09:00
pthread.h win32: replace pthread_cond_*() with much simpler code 2018-11-14 15:14:22 +09:00
syslog.c win32: plug memory leak on realloc() failure in syslog() 2017-08-10 13:57:52 -07:00
syslog.h mingw: implement syslog 2010-11-04 16:53:49 -07:00
trace2_win32_process_info.c trace2: report peak memory usage of the process 2019-04-16 13:37:07 +09:00