Skip to content

Commit

Permalink
[Support] Use realpath(3) instead of trying to open a file.
Browse files Browse the repository at this point in the history
If we don't have read permissions on the directory the call would
fail.

<rdar://problem/35871293>

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322095 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dcci committed Jan 9, 2018
1 parent 861f6eb commit 74e1b78
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/Support/Unix/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -860,12 +860,12 @@ std::error_code real_path(const Twine &path, SmallVectorImpl<char> &dest,
return real_path(Storage, dest, false);
}

int fd;
std::error_code EC = openFileForRead(path, fd, &dest);

if (EC)
return EC;
::close(fd);
SmallString<128> Storage;
StringRef P = path.toNullTerminatedStringRef(Storage);
char Buffer[PATH_MAX];
if (::realpath(P.begin(), Buffer) == nullptr)
return std::error_code(errno, std::generic_category());
dest.append(Buffer, Buffer + strlen(Buffer));
return std::error_code();
}

Expand Down
19 changes: 19 additions & 0 deletions unittests/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,25 @@ TEST_F(FileSystemTest, RealPath) {
ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/test1"));
}

#ifdef LLVM_ON_UNIX
TEST_F(FileSystemTest, RealPathNoReadPerm) {
SmallString<64> Expanded;

ASSERT_NO_ERROR(
fs::create_directories(Twine(TestDirectory) + "/noreadperm"));
ASSERT_TRUE(fs::exists(Twine(TestDirectory) + "/noreadperm"));

fs::setPermissions(Twine(TestDirectory) + "/noreadperm", fs::no_perms);
fs::setPermissions(Twine(TestDirectory) + "/noreadperm", fs::all_exe);

ASSERT_NO_ERROR(fs::real_path(Twine(TestDirectory) + "/noreadperm", Expanded,
false));

ASSERT_NO_ERROR(fs::remove_directories(Twine(TestDirectory) + "/noreadperm"));
}
#endif


TEST_F(FileSystemTest, TempFileKeepDiscard) {
// We can keep then discard.
auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
Expand Down

0 comments on commit 74e1b78

Please sign in to comment.