summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Support
diff options
context:
space:
mode:
authorSam McCall <sam.mccall@gmail.com>2019-01-14 10:56:35 +0000
committerSam McCall <sam.mccall@gmail.com>2019-01-14 10:56:35 +0000
commitc2b310aedf69f83c0b7252a969a6bda023c9de34 (patch)
tree3df71e6e5db716d4f3e2919d344e65fcd5a43070 /llvm/unittests/Support
parentb7cef81fd36c85e52b115b9ed6d1fb92d63781d6 (diff)
downloadbcm5719-llvm-c2b310aedf69f83c0b7252a969a6bda023c9de34.tar.gz
bcm5719-llvm-c2b310aedf69f83c0b7252a969a6bda023c9de34.zip
[VFS] Allow multiple RealFileSystem instances with independent CWDs.
Summary: Previously only one RealFileSystem instance was available, and its working directory is shared with the process. This doesn't work well for multithreaded programs that want to work with relative paths - the vfs::FileSystem is assumed to provide the working directory, but a thread cannot control this exclusively. The new vfs::createPhysicalFileSystem() factory copies the process's working directory initially, and then allows it to be independently modified. This implementation records the working directory path, and glues it to relative paths to provide the correct absolute path to the sys::fs:: functions. This will give different results in unusual situations (e.g. the CWD is moved). The main alternative is the use of openat(), fstatat(), etc to ask the OS to resolve paths relative to a directory handle which can be kept open. This is more robust. There are two reasons not to do this initially: 1. these functions are not available on all supported Unixes, and are somewhere between difficult and unavailable on Windows. So we need a path-based fallback anyway. 2. this would mean also adding support at the llvm::sys::fs level, which is a larger project. My clearest idea is an OS-specific `BaseDirectory` object that can be optionally passed to functions there. Eventually this could be backed by either paths or a fd where openat() is supported. This is a large project, and demonstrating here that a path-based fallback works is a useful prerequisite. There is some subtlety to the path-manipulation mechanism: - when setting the working directory, both Specified=makeAbsolute(path) and Resolved=realpath(path) are recorded. These may differ in the presence of symlinks. - getCurrentWorkingDirectory() and makeAbsolute() use Specified - this is similar to the behavior of $PWD and sys::path::current_path - IO operations like openFileForRead use Resolved. This is similar to the behavior of an openat() based implementation, that doesn't see changes in symlinks. There may still be combinations of operations and FS states that yield unhelpful behavior. This is hard to avoid with symlinks and FS abstractions :( The caching behavior of the current working directory is removed in this patch. getRealFileSystem() is now specified to link to the process CWD, so the caching is incorrect. The user who needed this so far is clangd, which will immediately switch to createPhysicalFileSystem(). Reviewers: ilya-biryukov, bkramer, labath Subscribers: ioeric, kadircet, kristina, llvm-commits Differential Revision: https://reviews.llvm.org/D56545 llvm-svn: 351050
Diffstat (limited to 'llvm/unittests/Support')
-rw-r--r--llvm/unittests/Support/VirtualFileSystemTest.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/llvm/unittests/Support/VirtualFileSystemTest.cpp b/llvm/unittests/Support/VirtualFileSystemTest.cpp
index 7b42943f0fd..508725881bf 100644
--- a/llvm/unittests/Support/VirtualFileSystemTest.cpp
+++ b/llvm/unittests/Support/VirtualFileSystemTest.cpp
@@ -382,6 +382,25 @@ struct ScopedLink {
}
operator StringRef() { return Path.str(); }
};
+
+struct ScopedFile {
+ SmallString<128> Path;
+ ScopedFile(const Twine &Path, StringRef Contents) {
+ Path.toVector(this->Path);
+ std::error_code EC;
+ raw_fd_ostream OS(this->Path, EC);
+ EXPECT_FALSE(EC);
+ OS << Contents;
+ OS.flush();
+ EXPECT_FALSE(OS.error());
+ if (EC || OS.error())
+ this->Path = "";
+ }
+ ~ScopedFile() {
+ if (Path != "")
+ EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
+ }
+};
} // end anonymous namespace
TEST(VirtualFileSystemTest, BasicRealFSIteration) {
@@ -411,6 +430,67 @@ TEST(VirtualFileSystemTest, BasicRealFSIteration) {
EXPECT_EQ(vfs::directory_iterator(), I);
}
+TEST(VirtualFileSystemTest, MultipleWorkingDirs) {
+ // Our root contains a/aa, b/bb, c, where c is a link to a/.
+ // Run tests both in root/b/ and root/c/ (to test "normal" and symlink dirs).
+ // Interleave operations to show the working directories are independent.
+ ScopedDir Root("r", true), ADir(Root.Path + "/a"), BDir(Root.Path + "/b");
+ ScopedLink C(ADir.Path, Root.Path + "/c");
+ ScopedFile AA(ADir.Path + "/aa", "aaaa"), BB(BDir.Path + "/bb", "bbbb");
+ std::unique_ptr<vfs::FileSystem> BFS = vfs::createPhysicalFileSystem(),
+ CFS = vfs::createPhysicalFileSystem();
+
+ ASSERT_FALSE(BFS->setCurrentWorkingDirectory(BDir.Path));
+ ASSERT_FALSE(CFS->setCurrentWorkingDirectory(C.Path));
+ EXPECT_EQ(BDir.Path, *BFS->getCurrentWorkingDirectory());
+ EXPECT_EQ(C.Path, *CFS->getCurrentWorkingDirectory());
+
+ // openFileForRead(), indirectly.
+ auto BBuf = BFS->getBufferForFile("bb");
+ ASSERT_TRUE(BBuf);
+ EXPECT_EQ("bbbb", (*BBuf)->getBuffer());
+
+ auto ABuf = CFS->getBufferForFile("aa");
+ ASSERT_TRUE(ABuf);
+ EXPECT_EQ("aaaa", (*ABuf)->getBuffer());
+
+ // status()
+ auto BStat = BFS->status("bb");
+ ASSERT_TRUE(BStat);
+ EXPECT_EQ("bb", BStat->getName());
+
+ auto AStat = CFS->status("aa");
+ ASSERT_TRUE(AStat);
+ EXPECT_EQ("aa", AStat->getName()); // unresolved name
+
+ // getRealPath()
+ SmallString<128> BPath;
+ ASSERT_FALSE(BFS->getRealPath("bb", BPath));
+ EXPECT_EQ(BB.Path, BPath);
+
+ SmallString<128> APath;
+ ASSERT_FALSE(CFS->getRealPath("aa", APath));
+ EXPECT_EQ(AA.Path, APath); // Reports resolved name.
+
+ // dir_begin
+ std::error_code EC;
+ auto BIt = BFS->dir_begin(".", EC);
+ ASSERT_FALSE(EC);
+ ASSERT_NE(BIt, vfs::directory_iterator());
+ EXPECT_EQ((BDir.Path + "/./bb").str(), BIt->path());
+ BIt.increment(EC);
+ ASSERT_FALSE(EC);
+ ASSERT_EQ(BIt, vfs::directory_iterator());
+
+ auto CIt = CFS->dir_begin(".", EC);
+ ASSERT_FALSE(EC);
+ ASSERT_NE(CIt, vfs::directory_iterator());
+ EXPECT_EQ((ADir.Path + "/./aa").str(), CIt->path()); // Partly resolved name!
+ CIt.increment(EC); // Because likely to read through this path.
+ ASSERT_FALSE(EC);
+ ASSERT_EQ(CIt, vfs::directory_iterator());
+}
+
#ifdef LLVM_ON_UNIX
TEST(VirtualFileSystemTest, BrokenSymlinkRealFSIteration) {
ScopedDir TestDirectory("virtual-file-system-test", /*Unique*/ true);
OpenPOWER on IntegriCloud