From: Benjamin Kramer Date: Mon, 5 Oct 2015 13:55:20 +0000 (+0000) Subject: [VFS] Add working directories to every virtual file system. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5b1864da9ca2054e158efa4042f448be057438ec;p=clang [VFS] Add working directories to every virtual file system. For RealFileSystem this is getcwd()/chdir(), the synthetic file systems can make up one for themselves. OverlayFileSystem now synchronizes the working directories when a new FS is added to the overlay or the overlay working directory is set. This allows purely artificial file systems that have zero ties to the underlying disks. Differential Revision: http://reviews.llvm.org/D13430 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@249316 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/VirtualFileSystem.h b/include/clang/Basic/VirtualFileSystem.h index d263e15c91..52bcce2cd1 100644 --- a/include/clang/Basic/VirtualFileSystem.h +++ b/include/clang/Basic/VirtualFileSystem.h @@ -199,6 +199,25 @@ public: /// \note The 'end' iterator is directory_iterator(). virtual directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) = 0; + + /// Set the working directory. This will affect all following operations on + /// this file system and may propagate down for nested file systems. + virtual std::error_code setCurrentWorkingDirectory(const Twine &Path) = 0; + /// Get the working directory of this file system. + virtual llvm::ErrorOr getCurrentWorkingDirectory() const = 0; + + /// Make \a Path an absolute path. + /// + /// Makes \a Path absolute using the current directory if it is not already. + /// An empty \a Path will result in the current directory. + /// + /// /absolute/path => /absolute/path + /// relative/../path => /relative/../path + /// + /// \param Path A path that is modified to be an absolute path. + /// \returns success if \a path has been made absolute, otherwise a + /// platform-specific error_code. + std::error_code makeAbsolute(SmallVectorImpl &Path) const; }; /// \brief Gets an \p vfs::FileSystem for the 'real' file system, as seen by @@ -230,6 +249,8 @@ public: llvm::ErrorOr> openFileForRead(const Twine &Path) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; + llvm::ErrorOr getCurrentWorkingDirectory() const override; + std::error_code setCurrentWorkingDirectory(const Twine &Path) override; typedef FileSystemList::reverse_iterator iterator; @@ -248,6 +269,7 @@ class InMemoryDirectory; /// An in-memory file system. class InMemoryFileSystem : public FileSystem { std::unique_ptr Root; + std::string WorkingDirectory; public: InMemoryFileSystem(); @@ -260,6 +282,13 @@ public: llvm::ErrorOr> openFileForRead(const Twine &Path) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; + llvm::ErrorOr getCurrentWorkingDirectory() const override { + return WorkingDirectory; + } + std::error_code setCurrentWorkingDirectory(const Twine &Path) override { + WorkingDirectory = Path.str(); + return std::error_code(); + } }; /// \brief Get a globally unique ID for a virtual file or directory. diff --git a/lib/Basic/VirtualFileSystem.cpp b/lib/Basic/VirtualFileSystem.cpp index b258bc4252..1ba4a29c1c 100644 --- a/lib/Basic/VirtualFileSystem.cpp +++ b/lib/Basic/VirtualFileSystem.cpp @@ -89,6 +89,14 @@ FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize, return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile); } +std::error_code FileSystem::makeAbsolute(SmallVectorImpl &Path) const { + auto WorkingDir = getCurrentWorkingDirectory(); + if (!WorkingDir) + return WorkingDir.getError(); + + return llvm::sys::fs::make_absolute(WorkingDir.get(), Path); +} + //===-----------------------------------------------------------------------===/ // RealFileSystem implementation //===-----------------------------------------------------------------------===/ @@ -160,6 +168,9 @@ public: ErrorOr status(const Twine &Path) override; ErrorOr> openFileForRead(const Twine &Path) override; directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; + + llvm::ErrorOr getCurrentWorkingDirectory() const override; + std::error_code setCurrentWorkingDirectory(const Twine &Path) override; }; } // end anonymous namespace @@ -178,6 +189,28 @@ RealFileSystem::openFileForRead(const Twine &Name) { return std::unique_ptr(new RealFile(FD, Name.str())); } +llvm::ErrorOr RealFileSystem::getCurrentWorkingDirectory() const { + SmallString<256> Dir; + if (std::error_code EC = llvm::sys::fs::current_path(Dir)) + return EC; + return Dir.str().str(); +} + +std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) { + // FIXME: chdir is thread hostile; on the other hand, creating the same + // behavior as chdir is complex: chdir resolves the path once, thus + // guaranteeing that all subsequent relative path operations work + // on the same path the original chdir resulted in. This makes a + // difference for example on network filesystems, where symlinks might be + // switched during runtime of the tool. Fixing this depends on having a + // file system abstraction that allows openat() style interactions. + SmallString<256> Storage; + StringRef Dir = Path.toNullTerminatedStringRef(Storage); + if (int Err = ::chdir(Dir.data())) + return std::error_code(Err, std::generic_category()); + return std::error_code(); +} + IntrusiveRefCntPtr vfs::getRealFileSystem() { static IntrusiveRefCntPtr FS = new RealFileSystem(); return FS; @@ -224,11 +257,14 @@ directory_iterator RealFileSystem::dir_begin(const Twine &Dir, // OverlayFileSystem implementation //===-----------------------------------------------------------------------===/ OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr BaseFS) { - pushOverlay(BaseFS); + FSList.push_back(BaseFS); } void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr FS) { FSList.push_back(FS); + // Synchronize added file systems by duplicating the working directory from + // the first one in the list. + FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get()); } ErrorOr OverlayFileSystem::status(const Twine &Path) { @@ -252,6 +288,19 @@ OverlayFileSystem::openFileForRead(const llvm::Twine &Path) { return make_error_code(llvm::errc::no_such_file_or_directory); } +llvm::ErrorOr +OverlayFileSystem::getCurrentWorkingDirectory() const { + // All file systems are synchronized, just take the first working directory. + return FSList.front()->getCurrentWorkingDirectory(); +} +std::error_code +OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) { + for (auto &FS : FSList) + if (std::error_code EC = FS->setCurrentWorkingDirectory(Path)) + return EC; + return std::error_code(); +} + clang::vfs::detail::DirIterImpl::~DirIterImpl() { } namespace { @@ -431,7 +480,7 @@ void InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime, P.toVector(Path); // Fix up relative paths. This just prepends the current working directory. - std::error_code EC = sys::fs::make_absolute(Path); + std::error_code EC = makeAbsolute(Path); assert(!EC); (void)EC; @@ -473,12 +522,13 @@ void InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime, } static ErrorOr -lookupInMemoryNode(detail::InMemoryDirectory *Dir, const Twine &P) { +lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir, + const Twine &P) { SmallString<128> Path; P.toVector(Path); // Fix up relative paths. This just prepends the current working directory. - std::error_code EC = sys::fs::make_absolute(Path); + std::error_code EC = FS.makeAbsolute(Path); assert(!EC); (void)EC; @@ -504,7 +554,7 @@ lookupInMemoryNode(detail::InMemoryDirectory *Dir, const Twine &P) { } llvm::ErrorOr InMemoryFileSystem::status(const Twine &Path) { - auto Node = lookupInMemoryNode(Root.get(), Path); + auto Node = lookupInMemoryNode(*this, Root.get(), Path); if (Node) return (*Node)->getStatus(); return Node.getError(); @@ -512,7 +562,7 @@ llvm::ErrorOr InMemoryFileSystem::status(const Twine &Path) { llvm::ErrorOr> InMemoryFileSystem::openFileForRead(const Twine &Path) { - auto Node = lookupInMemoryNode(Root.get(), Path); + auto Node = lookupInMemoryNode(*this, Root.get(), Path); if (!Node) return Node.getError(); @@ -551,7 +601,7 @@ public: directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir, std::error_code &EC) { - auto Node = lookupInMemoryNode(Root.get(), Dir); + auto Node = lookupInMemoryNode(*this, Root.get(), Dir); if (!Node) { EC = Node.getError(); return directory_iterator(std::make_shared()); @@ -742,6 +792,13 @@ public: ErrorOr status(const Twine &Path) override; ErrorOr> openFileForRead(const Twine &Path) override; + llvm::ErrorOr getCurrentWorkingDirectory() const override { + return ExternalFS->getCurrentWorkingDirectory(); + } + std::error_code setCurrentWorkingDirectory(const Twine &Path) override { + return ExternalFS->setCurrentWorkingDirectory(Path); + } + directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{ ErrorOr E = lookupPath(Dir); if (!E) { @@ -1114,7 +1171,7 @@ ErrorOr VFSFromYAML::lookupPath(const Twine &Path_) { Path_.toVector(Path); // Handle relative paths - if (std::error_code EC = sys::fs::make_absolute(Path)) + if (std::error_code EC = makeAbsolute(Path)) return EC; if (Path.empty()) diff --git a/unittests/Basic/VirtualFileSystemTest.cpp b/unittests/Basic/VirtualFileSystemTest.cpp index 4e5ec874ba..5362e59943 100644 --- a/unittests/Basic/VirtualFileSystemTest.cpp +++ b/unittests/Basic/VirtualFileSystemTest.cpp @@ -43,6 +43,12 @@ public: openFileForRead(const Twine &Path) override { llvm_unreachable("unimplemented"); } + llvm::ErrorOr getCurrentWorkingDirectory() const override { + return std::string(); + } + std::error_code setCurrentWorkingDirectory(const Twine &Path) override { + return std::error_code(); + } struct DirIterImpl : public clang::vfs::detail::DirIterImpl { std::map &FilesAndDirs;