Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8a19f53

Browse files
authored
Merge pull request #6499 from ramarag/unc_cwd
Fxing the paths that are resolved relative to a network share
2 parents 5081848 + 2eb7d0c commit 8a19f53

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/utilcode/longfilepathwrappers.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class LongFile
1717
static const WCHAR* UNCPathPrefix;
1818
static const WCHAR* UNCExtendedPathPrefix;
1919
static const WCHAR VolumeSeparatorChar;
20+
#define UNCPATHPREFIX W("\\\\")
2021
#endif //FEATURE_PAL
2122
static const WCHAR LongFile::DirectorySeparatorChar;
2223
static const WCHAR LongFile::AltDirectorySeparatorChar;
@@ -1256,7 +1257,7 @@ const WCHAR LongFile::VolumeSeparatorChar = W(':');
12561257
const WCHAR* LongFile::ExtendedPrefix = W("\\\\?\\");
12571258
const WCHAR* LongFile::DevicePathPrefix = W("\\\\.\\");
12581259
const WCHAR* LongFile::UNCExtendedPathPrefix = W("\\\\?\\UNC\\");
1259-
const WCHAR* LongFile::UNCPathPrefix = W("\\\\");
1260+
const WCHAR* LongFile::UNCPathPrefix = UNCPATHPREFIX;
12601261

12611262
BOOL LongFile::IsExtended(SString & path)
12621263
{
@@ -1326,7 +1327,8 @@ HRESULT LongFile::NormalizePath(SString & path)
13261327
//In this case if path is \\server the extended syntax should be like \\?\UNC\server
13271328
//The below logic populates the path from prefixLen offset from the start. This ensures that first 2 characters are overwritten
13281329
//
1329-
prefixLen = prefix.GetCount() - 2;
1330+
prefixLen = prefix.GetCount() - (COUNT_T)wcslen(UNCPATHPREFIX);
1331+
_ASSERTE(prefixLen > 0 );
13301332
}
13311333

13321334

@@ -1366,12 +1368,25 @@ HRESULT LongFile::NormalizePath(SString & path)
13661368
}
13671369
}
13681370

1371+
SString fullpath(SString::Literal,buffer + prefixLen);
13691372

1370-
//wcscpy_s always termintes with NULL, so we are saving the character that will be overwriiten
1371-
WCHAR temp = buffer[prefix.GetCount()];
1372-
wcscpy_s(buffer, prefix.GetCount() + 1, prefix.GetUnicode());
1373-
buffer[prefix.GetCount()] = temp;
1374-
path.CloseBuffer(ret + prefixLen);
1373+
//Check if the resolved path is a UNC. By default we assume relative path to resolve to disk
1374+
if (fullpath.BeginsWith(UNCPathPrefix) && prefixLen != prefix.GetCount() - (COUNT_T)wcslen(UNCPATHPREFIX))
1375+
{
1376+
1377+
//Remove the leading '\\' from the UNC path to be replaced with UNCExtendedPathPrefix
1378+
fullpath.Replace(fullpath.Begin(), (COUNT_T)wcslen(UNCPATHPREFIX), UNCExtendedPathPrefix);
1379+
path.CloseBuffer();
1380+
path.Set(fullpath);
1381+
}
1382+
else
1383+
{
1384+
//wcscpy_s always termintes with NULL, so we are saving the character that will be overwriiten
1385+
WCHAR temp = buffer[prefix.GetCount()];
1386+
wcscpy_s(buffer, prefix.GetCount() + 1, prefix.GetUnicode());
1387+
buffer[prefix.GetCount()] = temp;
1388+
path.CloseBuffer(ret + prefixLen);
1389+
}
13751390

13761391
return S_OK;
13771392
}

0 commit comments

Comments
 (0)