Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to handling of Windows symbolic links and mount points #1378

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pxr/base/arch/fileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,24 @@ std::string ArchReadLink(const char* path)
std::wstring ws(reparsePath.get());
string str(ws.begin(), ws.end());

return str;
}
else if (reparse->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
const size_t length =
reparse->MountPointReparseBuffer.PrintNameLength /
sizeof(WCHAR);
std::unique_ptr<WCHAR[]> reparsePath(new WCHAR[length + 1]);
wcsncpy(reparsePath.get(),
&reparse->MountPointReparseBuffer.PathBuffer[
reparse->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR)
], length);

reparsePath.get()[length] = 0;

// Convert wide-char to narrow char
std::wstring ws(reparsePath.get());
string str(ws.begin(), ws.end());

return str;
}
}
Expand Down
7 changes: 7 additions & 0 deletions pxr/base/tf/fileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ Tf_HasAttribute(
SetLastError(ERROR_SUCCESS);
return false;
}

// On Linux, a trailing slash forces the resolution of symlinks
// (https://man7.org/linux/man-pages/man7/path_resolution.7.html).
// We need to provide the same behavior on Windows.
if (path.back() == '/' || path.back() == '\\')
resolveSymlinks = true;

const DWORD attribs = GetFileAttributes(path.c_str());
if (attribs == INVALID_FILE_ATTRIBUTES) {
if (attribute == 0 && GetLastError() == ERROR_FILE_NOT_FOUND) {
Expand Down
34 changes: 33 additions & 1 deletion pxr/base/tf/testenv/fileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,37 @@ TestTfTouchFile()
return true;
}

static bool
TestSymlinkBehavior()
{
if (testSymlinks)
{
cout << "Testing symlink behavior" << endl;

(void) ArchUnlinkFile("junction");

TF_AXIOM(TfMakeDir("junction-target"));
#if defined(ARCH_OS_WINDOWS)
TF_AXIOM(system("mklink /j junction junction-target") == 0);
#else
TF_AXIOM(TfSymlink("junction-target", "junction"));
#endif
TF_AXIOM(TfIsLink("junction"));
TF_AXIOM(!TfIsDir("junction", false));
TF_AXIOM(TfIsDir("junction", true));
TF_AXIOM(TfIsDir("junction/", false));
TF_AXIOM(TfIsDir("junction/", true));
TF_AXIOM(TfTouchFile("junction/test-file"));
TF_AXIOM(TfIsFile("junction/test-file", false));
TF_AXIOM(TfIsFile("junction/test-file", true));
TF_AXIOM(TfDeleteFile("junction/test-file"));

(void) ArchUnlinkFile("junction");
}

return true;
}

static bool
Test_TfFileUtils()
{
Expand All @@ -684,7 +715,8 @@ Test_TfFileUtils()
TestTfWalkDirs() &&
TestTfListDir() &&
TestTfRmTree() &&
TestTfTouchFile()
TestTfTouchFile() &&
TestSymlinkBehavior()
;
}

Expand Down