From c1620fef4a1ad5c77b5ef5040ea87854aae05387 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 21 Sep 2020 00:52:26 +0200 Subject: [PATCH] canonicalize: Add support for UNC file names on native Windows. Reported and initial patch by Vaclav Slavik in . * lib/canonicalize.c (canonicalize_filename_mode): For UNC file names, extend the prefix to include the server. --- ChangeLog | 8 ++++++++ lib/canonicalize.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 167c7e7893..4fdd1a778c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2020-09-20 Bruno Haible + + canonicalize: Add support for UNC file names on native Windows. + Reported and initial patch by Vaclav Slavik in + . + * lib/canonicalize.c (canonicalize_filename_mode): For UNC file names, + extend the prefix to include the server. + 2020-09-20 Bruno Haible supersede: Fix test failures on native Windows. diff --git a/lib/canonicalize.c b/lib/canonicalize.c index aa0c3bd280..1fb3878ef2 100644 --- a/lib/canonicalize.c +++ b/lib/canonicalize.c @@ -163,8 +163,34 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode) *dest++ = '/'; if (DOUBLE_SLASH_IS_DISTINCT_ROOT) { - if (ISSLASH (name[1]) && !ISSLASH (name[2]) && !prefix_len) - *dest++ = '/'; + if (prefix_len == 0 /* implies ISSLASH (name[0]) */ + && ISSLASH (name[1]) && !ISSLASH (name[2])) + { + *dest++ = '/'; +#if defined _WIN32 && !defined __CYGWIN__ + /* For UNC file names '\\server\path\to\file', extend the prefix + to include the server: '\\server\'. */ + { + size_t i; + for (i = 2; name[i] != '\0' && !ISSLASH (name[i]); ) + i++; + if (name[i] != '\0' /* implies ISSLASH (name[i]) */ + && i + 1 < rname_limit - rname) + { + prefix_len = i; + memcpy (dest, name + 2, i - 2 + 1); + dest += i - 2 + 1; + } + else + { + /* Either name = '\\server'; this is an invalid file name. + Or name = '\\server\...' and server is more than + PATH_MAX - 4 bytes long. In either case, stop the UNC + processing. */ + } + } +#endif + } *dest = '\0'; } start = name + prefix_len; -- 2.39.5