Fxing the paths that are resolved relative to a network share
authorRama Krishnan Raghupathy <ramarag@microsoft.com>
Thu, 28 Jul 2016 01:48:24 +0000 (18:48 -0700)
committerRama Krishnan Raghupathy <ramarag@microsoft.com>
Thu, 28 Jul 2016 21:12:23 +0000 (14:12 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/2eb7d0c11a2842243c34e2302997e739c18cf7d6

src/coreclr/src/utilcode/longfilepathwrappers.cpp

index 2b5a8b4..9ffbf27 100644 (file)
@@ -17,6 +17,7 @@ private:
         static const WCHAR* UNCPathPrefix;
         static const WCHAR* UNCExtendedPathPrefix;
         static const WCHAR VolumeSeparatorChar;
+               #define UNCPATHPREFIX W("\\\\")
 #endif //FEATURE_PAL
         static const WCHAR LongFile::DirectorySeparatorChar;
         static const WCHAR LongFile::AltDirectorySeparatorChar;
@@ -1256,7 +1257,7 @@ const WCHAR LongFile::VolumeSeparatorChar = W(':');
 const WCHAR* LongFile::ExtendedPrefix = W("\\\\?\\");
 const WCHAR* LongFile::DevicePathPrefix = W("\\\\.\\");
 const WCHAR* LongFile::UNCExtendedPathPrefix = W("\\\\?\\UNC\\");
-const WCHAR* LongFile::UNCPathPrefix = W("\\\\");
+const WCHAR* LongFile::UNCPathPrefix = UNCPATHPREFIX;
 
 BOOL LongFile::IsExtended(SString & path)
 {
@@ -1326,7 +1327,8 @@ HRESULT LongFile::NormalizePath(SString & path)
         //In this case if path is \\server the extended syntax should be like  \\?\UNC\server
         //The below logic populates the path from prefixLen offset from the start. This ensures that first 2 characters are overwritten
         //
-        prefixLen = prefix.GetCount() - 2;
+        prefixLen = prefix.GetCount() - (COUNT_T)wcslen(UNCPATHPREFIX);
+        _ASSERTE(prefixLen > 0 );
     }
 
    
@@ -1366,12 +1368,25 @@ HRESULT LongFile::NormalizePath(SString & path)
         }
     }
 
+       SString fullpath(SString::Literal,buffer + prefixLen);
 
-    //wcscpy_s always termintes with NULL, so we are saving the character that will be overwriiten
-    WCHAR temp = buffer[prefix.GetCount()];
-    wcscpy_s(buffer, prefix.GetCount() + 1, prefix.GetUnicode());
-    buffer[prefix.GetCount()] = temp;
-    path.CloseBuffer(ret + prefixLen);
+    //Check if the resolved path is a UNC. By default we assume relative path to resolve to disk 
+    if (fullpath.BeginsWith(UNCPathPrefix) && prefixLen != prefix.GetCount() - (COUNT_T)wcslen(UNCPATHPREFIX))
+    {
+
+        //Remove the leading '\\' from the UNC path to be replaced with UNCExtendedPathPrefix
+        fullpath.Replace(fullpath.Begin(), (COUNT_T)wcslen(UNCPATHPREFIX), UNCExtendedPathPrefix);
+        path.CloseBuffer();
+        path.Set(fullpath);
+    }
+    else
+    {
+        //wcscpy_s always termintes with NULL, so we are saving the character that will be overwriiten
+        WCHAR temp = buffer[prefix.GetCount()];
+        wcscpy_s(buffer, prefix.GetCount() + 1, prefix.GetUnicode());
+        buffer[prefix.GetCount()] = temp;
+        path.CloseBuffer(ret + prefixLen);
+    }
 
     return S_OK;
 }