1 //===- VirtualFileSystem.cpp - Virtual File System Layer --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This file implements the VirtualFileSystem interface.
10 //===----------------------------------------------------------------------===//
12 #include "clang/Basic/VirtualFileSystem.h"
13 #include "clang/Basic/FileManager.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringSet.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Errc.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/Process.h"
25 #include "llvm/Support/YAMLParser.h"
30 using namespace clang;
31 using namespace clang::vfs;
33 using llvm::sys::fs::file_status;
34 using llvm::sys::fs::file_type;
35 using llvm::sys::fs::perms;
36 using llvm::sys::fs::UniqueID;
38 Status::Status(const file_status &Status)
39 : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
40 User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
41 Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false) {}
43 Status::Status(StringRef Name, UniqueID UID, sys::TimePoint<> MTime,
44 uint32_t User, uint32_t Group, uint64_t Size, file_type Type,
46 : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
47 Type(Type), Perms(Perms), IsVFSMapped(false) {}
49 Status Status::copyWithNewName(const Status &In, StringRef NewName) {
50 return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
51 In.getUser(), In.getGroup(), In.getSize(), In.getType(),
55 Status Status::copyWithNewName(const file_status &In, StringRef NewName) {
56 return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
57 In.getUser(), In.getGroup(), In.getSize(), In.type(),
61 bool Status::equivalent(const Status &Other) const {
62 return getUniqueID() == Other.getUniqueID();
64 bool Status::isDirectory() const {
65 return Type == file_type::directory_file;
67 bool Status::isRegularFile() const {
68 return Type == file_type::regular_file;
70 bool Status::isOther() const {
71 return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
73 bool Status::isSymlink() const {
74 return Type == file_type::symlink_file;
76 bool Status::isStatusKnown() const {
77 return Type != file_type::status_error;
79 bool Status::exists() const {
80 return isStatusKnown() && Type != file_type::file_not_found;
85 FileSystem::~FileSystem() {}
87 ErrorOr<std::unique_ptr<MemoryBuffer>>
88 FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
89 bool RequiresNullTerminator, bool IsVolatile) {
90 auto F = openFileForRead(Name);
94 return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
97 std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
98 if (llvm::sys::path::is_absolute(Path))
99 return std::error_code();
101 auto WorkingDir = getCurrentWorkingDirectory();
103 return WorkingDir.getError();
105 return llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
108 bool FileSystem::exists(const Twine &Path) {
109 auto Status = status(Path);
110 return Status && Status->exists();
114 static bool isTraversalComponent(StringRef Component) {
115 return Component.equals("..") || Component.equals(".");
118 static bool pathHasTraversal(StringRef Path) {
119 using namespace llvm::sys;
120 for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
121 if (isTraversalComponent(Comp))
127 //===-----------------------------------------------------------------------===/
128 // RealFileSystem implementation
129 //===-----------------------------------------------------------------------===/
132 /// \brief Wrapper around a raw file descriptor.
133 class RealFile : public File {
136 std::string RealName;
137 friend class RealFileSystem;
138 RealFile(int FD, StringRef NewName, StringRef NewRealPathName)
139 : FD(FD), S(NewName, {}, {}, {}, {}, {},
140 llvm::sys::fs::file_type::status_error, {}),
141 RealName(NewRealPathName.str()) {
142 assert(FD >= 0 && "Invalid or inactive file descriptor");
146 ~RealFile() override;
147 ErrorOr<Status> status() override;
148 ErrorOr<std::string> getName() override;
149 ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name,
151 bool RequiresNullTerminator,
152 bool IsVolatile) override;
153 std::error_code close() override;
155 } // end anonymous namespace
156 RealFile::~RealFile() { close(); }
158 ErrorOr<Status> RealFile::status() {
159 assert(FD != -1 && "cannot stat closed file");
160 if (!S.isStatusKnown()) {
161 file_status RealStatus;
162 if (std::error_code EC = sys::fs::status(FD, RealStatus))
164 S = Status::copyWithNewName(RealStatus, S.getName());
169 ErrorOr<std::string> RealFile::getName() {
170 return RealName.empty() ? S.getName().str() : RealName;
173 ErrorOr<std::unique_ptr<MemoryBuffer>>
174 RealFile::getBuffer(const Twine &Name, int64_t FileSize,
175 bool RequiresNullTerminator, bool IsVolatile) {
176 assert(FD != -1 && "cannot get buffer for closed file");
177 return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
181 std::error_code RealFile::close() {
182 std::error_code EC = sys::Process::SafelyCloseFileDescriptor(FD);
188 /// \brief The file system according to your operating system.
189 class RealFileSystem : public FileSystem {
191 ErrorOr<Status> status(const Twine &Path) override;
192 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
193 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
195 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
196 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
198 } // end anonymous namespace
200 ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
201 sys::fs::file_status RealStatus;
202 if (std::error_code EC = sys::fs::status(Path, RealStatus))
204 return Status::copyWithNewName(RealStatus, Path.str());
207 ErrorOr<std::unique_ptr<File>>
208 RealFileSystem::openFileForRead(const Twine &Name) {
210 SmallString<256> RealName;
211 if (std::error_code EC = sys::fs::openFileForRead(Name, FD, &RealName))
213 return std::unique_ptr<File>(new RealFile(FD, Name.str(), RealName.str()));
216 llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
217 SmallString<256> Dir;
218 if (std::error_code EC = llvm::sys::fs::current_path(Dir))
220 return Dir.str().str();
223 std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
224 // FIXME: chdir is thread hostile; on the other hand, creating the same
225 // behavior as chdir is complex: chdir resolves the path once, thus
226 // guaranteeing that all subsequent relative path operations work
227 // on the same path the original chdir resulted in. This makes a
228 // difference for example on network filesystems, where symlinks might be
229 // switched during runtime of the tool. Fixing this depends on having a
230 // file system abstraction that allows openat() style interactions.
231 return llvm::sys::fs::set_current_path(Path);
234 IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
235 static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
240 class RealFSDirIter : public clang::vfs::detail::DirIterImpl {
241 llvm::sys::fs::directory_iterator Iter;
243 RealFSDirIter(const Twine &Path, std::error_code &EC) : Iter(Path, EC) {
244 if (!EC && Iter != llvm::sys::fs::directory_iterator()) {
245 llvm::sys::fs::file_status S;
246 EC = Iter->status(S);
247 CurrentEntry = Status::copyWithNewName(S, Iter->path());
251 std::error_code increment() override {
256 } else if (Iter == llvm::sys::fs::directory_iterator()) {
257 CurrentEntry = Status();
259 llvm::sys::fs::file_status S;
260 EC = Iter->status(S);
261 CurrentEntry = Status::copyWithNewName(S, Iter->path());
268 directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
269 std::error_code &EC) {
270 return directory_iterator(std::make_shared<RealFSDirIter>(Dir, EC));
273 //===-----------------------------------------------------------------------===/
274 // OverlayFileSystem implementation
275 //===-----------------------------------------------------------------------===/
276 OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
277 FSList.push_back(std::move(BaseFS));
280 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
281 FSList.push_back(FS);
282 // Synchronize added file systems by duplicating the working directory from
283 // the first one in the list.
284 FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
287 ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
288 // FIXME: handle symlinks that cross file systems
289 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
290 ErrorOr<Status> Status = (*I)->status(Path);
291 if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
294 return make_error_code(llvm::errc::no_such_file_or_directory);
297 ErrorOr<std::unique_ptr<File>>
298 OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
299 // FIXME: handle symlinks that cross file systems
300 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
301 auto Result = (*I)->openFileForRead(Path);
302 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
305 return make_error_code(llvm::errc::no_such_file_or_directory);
308 llvm::ErrorOr<std::string>
309 OverlayFileSystem::getCurrentWorkingDirectory() const {
310 // All file systems are synchronized, just take the first working directory.
311 return FSList.front()->getCurrentWorkingDirectory();
314 OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
315 for (auto &FS : FSList)
316 if (std::error_code EC = FS->setCurrentWorkingDirectory(Path))
318 return std::error_code();
321 clang::vfs::detail::DirIterImpl::~DirIterImpl() { }
324 class OverlayFSDirIterImpl : public clang::vfs::detail::DirIterImpl {
325 OverlayFileSystem &Overlays;
327 OverlayFileSystem::iterator CurrentFS;
328 directory_iterator CurrentDirIter;
329 llvm::StringSet<> SeenNames;
331 std::error_code incrementFS() {
332 assert(CurrentFS != Overlays.overlays_end() && "incrementing past end");
334 for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) {
336 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
337 if (EC && EC != errc::no_such_file_or_directory)
339 if (CurrentDirIter != directory_iterator())
342 return std::error_code();
345 std::error_code incrementDirIter(bool IsFirstTime) {
346 assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
347 "incrementing past end");
350 CurrentDirIter.increment(EC);
351 if (!EC && CurrentDirIter == directory_iterator())
356 std::error_code incrementImpl(bool IsFirstTime) {
358 std::error_code EC = incrementDirIter(IsFirstTime);
359 if (EC || CurrentDirIter == directory_iterator()) {
360 CurrentEntry = Status();
363 CurrentEntry = *CurrentDirIter;
364 StringRef Name = llvm::sys::path::filename(CurrentEntry.getName());
365 if (SeenNames.insert(Name).second)
366 return EC; // name not seen before
368 llvm_unreachable("returned above");
372 OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS,
374 : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) {
375 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
376 EC = incrementImpl(true);
379 std::error_code increment() override { return incrementImpl(false); }
381 } // end anonymous namespace
383 directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
384 std::error_code &EC) {
385 return directory_iterator(
386 std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC));
393 enum InMemoryNodeKind { IME_File, IME_Directory };
395 /// The in memory file system is a tree of Nodes. Every node can either be a
396 /// file or a directory.
399 InMemoryNodeKind Kind;
402 InMemoryNode(Status Stat, InMemoryNodeKind Kind)
403 : Stat(std::move(Stat)), Kind(Kind) {}
404 virtual ~InMemoryNode() {}
405 const Status &getStatus() const { return Stat; }
406 InMemoryNodeKind getKind() const { return Kind; }
407 virtual std::string toString(unsigned Indent) const = 0;
411 class InMemoryFile : public InMemoryNode {
412 std::unique_ptr<llvm::MemoryBuffer> Buffer;
415 InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer)
416 : InMemoryNode(std::move(Stat), IME_File), Buffer(std::move(Buffer)) {}
418 llvm::MemoryBuffer *getBuffer() { return Buffer.get(); }
419 std::string toString(unsigned Indent) const override {
420 return (std::string(Indent, ' ') + getStatus().getName() + "\n").str();
422 static bool classof(const InMemoryNode *N) {
423 return N->getKind() == IME_File;
427 /// Adapt a InMemoryFile for VFS' File interface.
428 class InMemoryFileAdaptor : public File {
432 explicit InMemoryFileAdaptor(InMemoryFile &Node) : Node(Node) {}
434 llvm::ErrorOr<Status> status() override { return Node.getStatus(); }
435 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
436 getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
437 bool IsVolatile) override {
438 llvm::MemoryBuffer *Buf = Node.getBuffer();
439 return llvm::MemoryBuffer::getMemBuffer(
440 Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator);
442 std::error_code close() override { return std::error_code(); }
444 } // end anonymous namespace
446 class InMemoryDirectory : public InMemoryNode {
447 std::map<std::string, std::unique_ptr<InMemoryNode>> Entries;
450 InMemoryDirectory(Status Stat)
451 : InMemoryNode(std::move(Stat), IME_Directory) {}
452 InMemoryNode *getChild(StringRef Name) {
453 auto I = Entries.find(Name);
454 if (I != Entries.end())
455 return I->second.get();
458 InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) {
459 return Entries.insert(make_pair(Name, std::move(Child)))
460 .first->second.get();
463 typedef decltype(Entries)::const_iterator const_iterator;
464 const_iterator begin() const { return Entries.begin(); }
465 const_iterator end() const { return Entries.end(); }
467 std::string toString(unsigned Indent) const override {
469 (std::string(Indent, ' ') + getStatus().getName() + "\n").str();
470 for (const auto &Entry : Entries) {
471 Result += Entry.second->toString(Indent + 2);
475 static bool classof(const InMemoryNode *N) {
476 return N->getKind() == IME_Directory;
481 InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths)
482 : Root(new detail::InMemoryDirectory(
483 Status("", getNextVirtualUniqueID(), llvm::sys::TimePoint<>(), 0, 0,
484 0, llvm::sys::fs::file_type::directory_file,
485 llvm::sys::fs::perms::all_all))),
486 UseNormalizedPaths(UseNormalizedPaths) {}
488 InMemoryFileSystem::~InMemoryFileSystem() {}
490 std::string InMemoryFileSystem::toString() const {
491 return Root->toString(/*Indent=*/0);
494 bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
495 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
496 SmallString<128> Path;
499 // Fix up relative paths. This just prepends the current working directory.
500 std::error_code EC = makeAbsolute(Path);
504 if (useNormalizedPaths())
505 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
510 detail::InMemoryDirectory *Dir = Root.get();
511 auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
514 detail::InMemoryNode *Node = Dir->getChild(Name);
518 // End of the path, create a new file.
519 // FIXME: expose the status details in the interface.
520 Status Stat(P.str(), getNextVirtualUniqueID(),
521 llvm::sys::toTimePoint(ModificationTime), 0, 0,
522 Buffer->getBufferSize(),
523 llvm::sys::fs::file_type::regular_file,
524 llvm::sys::fs::all_all);
525 Dir->addChild(Name, llvm::make_unique<detail::InMemoryFile>(
526 std::move(Stat), std::move(Buffer)));
530 // Create a new directory. Use the path up to here.
531 // FIXME: expose the status details in the interface.
533 StringRef(Path.str().begin(), Name.end() - Path.str().begin()),
534 getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime), 0,
535 0, Buffer->getBufferSize(), llvm::sys::fs::file_type::directory_file,
536 llvm::sys::fs::all_all);
537 Dir = cast<detail::InMemoryDirectory>(Dir->addChild(
538 Name, llvm::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
542 if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
545 assert(isa<detail::InMemoryFile>(Node) &&
546 "Must be either file or directory!");
548 // Trying to insert a directory in place of a file.
552 // Return false only if the new file is different from the existing one.
553 return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() ==
559 bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime,
560 llvm::MemoryBuffer *Buffer) {
561 return addFile(P, ModificationTime,
562 llvm::MemoryBuffer::getMemBuffer(
563 Buffer->getBuffer(), Buffer->getBufferIdentifier()));
566 static ErrorOr<detail::InMemoryNode *>
567 lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir,
569 SmallString<128> Path;
572 // Fix up relative paths. This just prepends the current working directory.
573 std::error_code EC = FS.makeAbsolute(Path);
577 if (FS.useNormalizedPaths())
578 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
583 auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
585 detail::InMemoryNode *Node = Dir->getChild(*I);
588 return errc::no_such_file_or_directory;
590 // Return the file if it's at the end of the path.
591 if (auto File = dyn_cast<detail::InMemoryFile>(Node)) {
594 return errc::no_such_file_or_directory;
597 // Traverse directories.
598 Dir = cast<detail::InMemoryDirectory>(Node);
604 llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) {
605 auto Node = lookupInMemoryNode(*this, Root.get(), Path);
607 return (*Node)->getStatus();
608 return Node.getError();
611 llvm::ErrorOr<std::unique_ptr<File>>
612 InMemoryFileSystem::openFileForRead(const Twine &Path) {
613 auto Node = lookupInMemoryNode(*this, Root.get(), Path);
615 return Node.getError();
617 // When we have a file provide a heap-allocated wrapper for the memory buffer
618 // to match the ownership semantics for File.
619 if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
620 return std::unique_ptr<File>(new detail::InMemoryFileAdaptor(*F));
622 // FIXME: errc::not_a_file?
623 return make_error_code(llvm::errc::invalid_argument);
627 /// Adaptor from InMemoryDir::iterator to directory_iterator.
628 class InMemoryDirIterator : public clang::vfs::detail::DirIterImpl {
629 detail::InMemoryDirectory::const_iterator I;
630 detail::InMemoryDirectory::const_iterator E;
633 InMemoryDirIterator() {}
634 explicit InMemoryDirIterator(detail::InMemoryDirectory &Dir)
635 : I(Dir.begin()), E(Dir.end()) {
637 CurrentEntry = I->second->getStatus();
640 std::error_code increment() override {
642 // When we're at the end, make CurrentEntry invalid and DirIterImpl will do
644 CurrentEntry = I != E ? I->second->getStatus() : Status();
645 return std::error_code();
648 } // end anonymous namespace
650 directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
651 std::error_code &EC) {
652 auto Node = lookupInMemoryNode(*this, Root.get(), Dir);
654 EC = Node.getError();
655 return directory_iterator(std::make_shared<InMemoryDirIterator>());
658 if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node))
659 return directory_iterator(std::make_shared<InMemoryDirIterator>(*DirNode));
661 EC = make_error_code(llvm::errc::not_a_directory);
662 return directory_iterator(std::make_shared<InMemoryDirIterator>());
665 std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
666 SmallString<128> Path;
669 // Fix up relative paths. This just prepends the current working directory.
670 std::error_code EC = makeAbsolute(Path);
674 if (useNormalizedPaths())
675 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
678 WorkingDirectory = Path.str();
679 return std::error_code();
684 //===-----------------------------------------------------------------------===/
685 // RedirectingFileSystem implementation
686 //===-----------------------------------------------------------------------===/
695 /// \brief A single file or directory in the VFS.
702 Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
703 StringRef getName() const { return Name; }
704 EntryKind getKind() const { return Kind; }
707 class RedirectingDirectoryEntry : public Entry {
708 std::vector<std::unique_ptr<Entry>> Contents;
712 RedirectingDirectoryEntry(StringRef Name,
713 std::vector<std::unique_ptr<Entry>> Contents,
715 : Entry(EK_Directory, Name), Contents(std::move(Contents)),
717 RedirectingDirectoryEntry(StringRef Name, Status S)
718 : Entry(EK_Directory, Name), S(std::move(S)) {}
719 Status getStatus() { return S; }
720 void addContent(std::unique_ptr<Entry> Content) {
721 Contents.push_back(std::move(Content));
723 Entry *getLastContent() const { return Contents.back().get(); }
724 typedef decltype(Contents)::iterator iterator;
725 iterator contents_begin() { return Contents.begin(); }
726 iterator contents_end() { return Contents.end(); }
727 static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
730 class RedirectingFileEntry : public Entry {
738 std::string ExternalContentsPath;
741 RedirectingFileEntry(StringRef Name, StringRef ExternalContentsPath,
743 : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
745 StringRef getExternalContentsPath() const { return ExternalContentsPath; }
746 /// \brief whether to use the external path as the name for this file.
747 bool useExternalName(bool GlobalUseExternalName) const {
748 return UseName == NK_NotSet ? GlobalUseExternalName
749 : (UseName == NK_External);
751 NameKind getUseName() const { return UseName; }
752 static bool classof(const Entry *E) { return E->getKind() == EK_File; }
755 class RedirectingFileSystem;
757 class VFSFromYamlDirIterImpl : public clang::vfs::detail::DirIterImpl {
759 RedirectingFileSystem &FS;
760 RedirectingDirectoryEntry::iterator Current, End;
763 VFSFromYamlDirIterImpl(const Twine &Path, RedirectingFileSystem &FS,
764 RedirectingDirectoryEntry::iterator Begin,
765 RedirectingDirectoryEntry::iterator End,
766 std::error_code &EC);
767 std::error_code increment() override;
770 /// \brief A virtual file system parsed from a YAML file.
772 /// Currently, this class allows creating virtual directories and mapping
773 /// virtual file paths to existing external files, available in \c ExternalFS.
775 /// The basic structure of the parsed file is:
778 /// 'version': <version number>,
779 /// <optional configuration>
781 /// <directory entries>
786 /// All configuration options are optional.
787 /// 'case-sensitive': <boolean, default=true>
788 /// 'use-external-names': <boolean, default=true>
789 /// 'overlay-relative': <boolean, default=false>
790 /// 'ignore-non-existent-contents': <boolean, default=true>
792 /// Virtual directories are represented as
795 /// 'type': 'directory',
796 /// 'name': <string>,
797 /// 'contents': [ <file or directory entries> ]
801 /// The default attributes for virtual directories are:
803 /// MTime = now() when created
807 /// UniqueID = unspecified unique value
810 /// Re-mapped files are represented as
814 /// 'name': <string>,
815 /// 'use-external-name': <boolean> # Optional
816 /// 'external-contents': <path to external file>)
820 /// and inherit their attributes from the external contents.
822 /// In both cases, the 'name' field may contain multiple path components (e.g.
823 /// /path/to/file). However, any directory that contains more than one child
824 /// must be uniquely represented by a directory entry.
825 class RedirectingFileSystem : public vfs::FileSystem {
826 /// The root(s) of the virtual file system.
827 std::vector<std::unique_ptr<Entry>> Roots;
828 /// \brief The file system to use for external references.
829 IntrusiveRefCntPtr<FileSystem> ExternalFS;
830 /// If IsRelativeOverlay is set, this represents the directory
831 /// path that should be prefixed to each 'external-contents' entry
832 /// when reading from YAML files.
833 std::string ExternalContentsPrefixDir;
835 /// @name Configuration
838 /// \brief Whether to perform case-sensitive comparisons.
840 /// Currently, case-insensitive matching only works correctly with ASCII.
841 bool CaseSensitive = true;
843 /// IsRelativeOverlay marks whether a IsExternalContentsPrefixDir path must
844 /// be prefixed in every 'external-contents' when reading from YAML files.
845 bool IsRelativeOverlay = false;
847 /// \brief Whether to use to use the value of 'external-contents' for the
848 /// names of files. This global value is overridable on a per-file basis.
849 bool UseExternalNames = true;
851 /// \brief Whether an invalid path obtained via 'external-contents' should
852 /// cause iteration on the VFS to stop. If 'true', the VFS should ignore
853 /// the entry and continue with the next. Allows YAML files to be shared
854 /// across multiple compiler invocations regardless of prior existent
855 /// paths in 'external-contents'. This global value is overridable on a
857 bool IgnoreNonExistentContents = true;
860 /// Virtual file paths and external files could be canonicalized without "..",
861 /// "." and "./" in their paths. FIXME: some unittests currently fail on
862 /// win32 when using remove_dots and remove_leading_dotslash on paths.
863 bool UseCanonicalizedPaths =
870 friend class RedirectingFileSystemParser;
873 RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS)
874 : ExternalFS(std::move(ExternalFS)) {}
876 /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly
877 /// recursing into the contents of \p From if it is a directory.
878 ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
879 sys::path::const_iterator End, Entry *From);
881 /// \brief Get the status of a given an \c Entry.
882 ErrorOr<Status> status(const Twine &Path, Entry *E);
885 /// \brief Looks up \p Path in \c Roots.
886 ErrorOr<Entry *> lookupPath(const Twine &Path);
888 /// \brief Parses \p Buffer, which is expected to be in YAML format and
889 /// returns a virtual file system representing its contents.
890 static RedirectingFileSystem *
891 create(std::unique_ptr<MemoryBuffer> Buffer,
892 SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
893 void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
895 ErrorOr<Status> status(const Twine &Path) override;
896 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
898 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
899 return ExternalFS->getCurrentWorkingDirectory();
901 std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
902 return ExternalFS->setCurrentWorkingDirectory(Path);
905 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{
906 ErrorOr<Entry *> E = lookupPath(Dir);
909 return directory_iterator();
911 ErrorOr<Status> S = status(Dir, *E);
914 return directory_iterator();
916 if (!S->isDirectory()) {
917 EC = std::error_code(static_cast<int>(errc::not_a_directory),
918 std::system_category());
919 return directory_iterator();
922 auto *D = cast<RedirectingDirectoryEntry>(*E);
923 return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(Dir,
924 *this, D->contents_begin(), D->contents_end(), EC));
927 void setExternalContentsPrefixDir(StringRef PrefixDir) {
928 ExternalContentsPrefixDir = PrefixDir.str();
931 StringRef getExternalContentsPrefixDir() const {
932 return ExternalContentsPrefixDir;
935 bool ignoreNonExistentContents() const {
936 return IgnoreNonExistentContents;
939 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
940 LLVM_DUMP_METHOD void dump() const {
941 for (const std::unique_ptr<Entry> &Root : Roots)
942 dumpEntry(Root.get());
945 LLVM_DUMP_METHOD void dumpEntry(Entry *E, int NumSpaces = 0) const {
946 StringRef Name = E->getName();
947 for (int i = 0, e = NumSpaces; i < e; ++i)
949 dbgs() << "'" << Name.str().c_str() << "'" << "\n";
951 if (E->getKind() == EK_Directory) {
952 auto *DE = dyn_cast<RedirectingDirectoryEntry>(E);
953 assert(DE && "Should be a directory");
955 for (std::unique_ptr<Entry> &SubEntry :
956 llvm::make_range(DE->contents_begin(), DE->contents_end()))
957 dumpEntry(SubEntry.get(), NumSpaces+2);
964 /// \brief A helper class to hold the common YAML parsing state.
965 class RedirectingFileSystemParser {
966 yaml::Stream &Stream;
968 void error(yaml::Node *N, const Twine &Msg) {
969 Stream.printError(N, Msg);
973 bool parseScalarString(yaml::Node *N, StringRef &Result,
974 SmallVectorImpl<char> &Storage) {
975 yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
977 error(N, "expected string");
980 Result = S->getValue(Storage);
985 bool parseScalarBool(yaml::Node *N, bool &Result) {
986 SmallString<5> Storage;
988 if (!parseScalarString(N, Value, Storage))
991 if (Value.equals_lower("true") || Value.equals_lower("on") ||
992 Value.equals_lower("yes") || Value == "1") {
995 } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
996 Value.equals_lower("no") || Value == "0") {
1001 error(N, "expected boolean value");
1006 KeyStatus(bool Required=false) : Required(Required), Seen(false) {}
1010 typedef std::pair<StringRef, KeyStatus> KeyStatusPair;
1013 bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
1014 DenseMap<StringRef, KeyStatus> &Keys) {
1015 if (!Keys.count(Key)) {
1016 error(KeyNode, "unknown key");
1019 KeyStatus &S = Keys[Key];
1021 error(KeyNode, Twine("duplicate key '") + Key + "'");
1029 bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
1030 for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(),
1033 if (I->second.Required && !I->second.Seen) {
1034 error(Obj, Twine("missing key '") + I->first + "'");
1041 Entry *lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name,
1042 Entry *ParentEntry = nullptr) {
1043 if (!ParentEntry) { // Look for a existent root
1044 for (const std::unique_ptr<Entry> &Root : FS->Roots) {
1045 if (Name.equals(Root->getName())) {
1046 ParentEntry = Root.get();
1050 } else { // Advance to the next component
1051 auto *DE = dyn_cast<RedirectingDirectoryEntry>(ParentEntry);
1052 for (std::unique_ptr<Entry> &Content :
1053 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1054 auto *DirContent = dyn_cast<RedirectingDirectoryEntry>(Content.get());
1055 if (DirContent && Name.equals(Content->getName()))
1060 // ... or create a new one
1061 std::unique_ptr<Entry> E = llvm::make_unique<RedirectingDirectoryEntry>(
1063 Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1064 0, 0, 0, file_type::directory_file, sys::fs::all_all));
1066 if (!ParentEntry) { // Add a new root to the overlay
1067 FS->Roots.push_back(std::move(E));
1068 ParentEntry = FS->Roots.back().get();
1072 auto *DE = dyn_cast<RedirectingDirectoryEntry>(ParentEntry);
1073 DE->addContent(std::move(E));
1074 return DE->getLastContent();
1077 void uniqueOverlayTree(RedirectingFileSystem *FS, Entry *SrcE,
1078 Entry *NewParentE = nullptr) {
1079 StringRef Name = SrcE->getName();
1080 switch (SrcE->getKind()) {
1081 case EK_Directory: {
1082 auto *DE = dyn_cast<RedirectingDirectoryEntry>(SrcE);
1083 assert(DE && "Must be a directory");
1084 // Empty directories could be present in the YAML as a way to
1085 // describe a file for a current directory after some of its subdir
1086 // is parsed. This only leads to redundant walks, ignore it.
1088 NewParentE = lookupOrCreateEntry(FS, Name, NewParentE);
1089 for (std::unique_ptr<Entry> &SubEntry :
1090 llvm::make_range(DE->contents_begin(), DE->contents_end()))
1091 uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
1095 auto *FE = dyn_cast<RedirectingFileEntry>(SrcE);
1096 assert(FE && "Must be a file");
1097 assert(NewParentE && "Parent entry must exist");
1098 auto *DE = dyn_cast<RedirectingDirectoryEntry>(NewParentE);
1099 DE->addContent(llvm::make_unique<RedirectingFileEntry>(
1100 Name, FE->getExternalContentsPath(), FE->getUseName()));
1106 std::unique_ptr<Entry> parseEntry(yaml::Node *N, RedirectingFileSystem *FS) {
1107 yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N);
1109 error(N, "expected mapping node for file or directory entry");
1113 KeyStatusPair Fields[] = {
1114 KeyStatusPair("name", true),
1115 KeyStatusPair("type", true),
1116 KeyStatusPair("contents", false),
1117 KeyStatusPair("external-contents", false),
1118 KeyStatusPair("use-external-name", false),
1121 DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1123 bool HasContents = false; // external or otherwise
1124 std::vector<std::unique_ptr<Entry>> EntryArrayContents;
1125 std::string ExternalContentsPath;
1127 auto UseExternalName = RedirectingFileEntry::NK_NotSet;
1130 for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E;
1133 // Reuse the buffer for key and value, since we don't look at key after
1135 SmallString<256> Buffer;
1136 if (!parseScalarString(I->getKey(), Key, Buffer))
1139 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
1143 if (Key == "name") {
1144 if (!parseScalarString(I->getValue(), Value, Buffer))
1147 if (FS->UseCanonicalizedPaths) {
1148 SmallString<256> Path(Value);
1149 // Guarantee that old YAML files containing paths with ".." and "."
1150 // are properly canonicalized before read into the VFS.
1151 Path = sys::path::remove_leading_dotslash(Path);
1152 sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1157 } else if (Key == "type") {
1158 if (!parseScalarString(I->getValue(), Value, Buffer))
1160 if (Value == "file")
1162 else if (Value == "directory")
1163 Kind = EK_Directory;
1165 error(I->getValue(), "unknown value for 'type'");
1168 } else if (Key == "contents") {
1171 "entry already has 'contents' or 'external-contents'");
1175 yaml::SequenceNode *Contents =
1176 dyn_cast<yaml::SequenceNode>(I->getValue());
1178 // FIXME: this is only for directories, what about files?
1179 error(I->getValue(), "expected array");
1183 for (yaml::SequenceNode::iterator I = Contents->begin(),
1184 E = Contents->end();
1186 if (std::unique_ptr<Entry> E = parseEntry(&*I, FS))
1187 EntryArrayContents.push_back(std::move(E));
1191 } else if (Key == "external-contents") {
1194 "entry already has 'contents' or 'external-contents'");
1198 if (!parseScalarString(I->getValue(), Value, Buffer))
1201 SmallString<256> FullPath;
1202 if (FS->IsRelativeOverlay) {
1203 FullPath = FS->getExternalContentsPrefixDir();
1204 assert(!FullPath.empty() &&
1205 "External contents prefix directory must exist");
1206 llvm::sys::path::append(FullPath, Value);
1211 if (FS->UseCanonicalizedPaths) {
1212 // Guarantee that old YAML files containing paths with ".." and "."
1213 // are properly canonicalized before read into the VFS.
1214 FullPath = sys::path::remove_leading_dotslash(FullPath);
1215 sys::path::remove_dots(FullPath, /*remove_dot_dot=*/true);
1217 ExternalContentsPath = FullPath.str();
1218 } else if (Key == "use-external-name") {
1220 if (!parseScalarBool(I->getValue(), Val))
1222 UseExternalName = Val ? RedirectingFileEntry::NK_External
1223 : RedirectingFileEntry::NK_Virtual;
1225 llvm_unreachable("key missing from Keys");
1229 if (Stream.failed())
1232 // check for missing keys
1234 error(N, "missing key 'contents' or 'external-contents'");
1237 if (!checkMissingKeys(N, Keys))
1240 // check invalid configuration
1241 if (Kind == EK_Directory &&
1242 UseExternalName != RedirectingFileEntry::NK_NotSet) {
1243 error(N, "'use-external-name' is not supported for directories");
1247 // Remove trailing slash(es), being careful not to remove the root path
1248 StringRef Trimmed(Name);
1249 size_t RootPathLen = sys::path::root_path(Trimmed).size();
1250 while (Trimmed.size() > RootPathLen &&
1251 sys::path::is_separator(Trimmed.back()))
1252 Trimmed = Trimmed.slice(0, Trimmed.size()-1);
1253 // Get the last component
1254 StringRef LastComponent = sys::path::filename(Trimmed);
1256 std::unique_ptr<Entry> Result;
1259 Result = llvm::make_unique<RedirectingFileEntry>(
1260 LastComponent, std::move(ExternalContentsPath), UseExternalName);
1263 Result = llvm::make_unique<RedirectingDirectoryEntry>(
1264 LastComponent, std::move(EntryArrayContents),
1265 Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1266 0, 0, 0, file_type::directory_file, sys::fs::all_all));
1270 StringRef Parent = sys::path::parent_path(Trimmed);
1274 // if 'name' contains multiple components, create implicit directory entries
1275 for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
1276 E = sys::path::rend(Parent);
1278 std::vector<std::unique_ptr<Entry>> Entries;
1279 Entries.push_back(std::move(Result));
1280 Result = llvm::make_unique<RedirectingDirectoryEntry>(
1281 *I, std::move(Entries),
1282 Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1283 0, 0, 0, file_type::directory_file, sys::fs::all_all));
1289 RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {}
1292 bool parse(yaml::Node *Root, RedirectingFileSystem *FS) {
1293 yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
1295 error(Root, "expected mapping node");
1299 KeyStatusPair Fields[] = {
1300 KeyStatusPair("version", true),
1301 KeyStatusPair("case-sensitive", false),
1302 KeyStatusPair("use-external-names", false),
1303 KeyStatusPair("overlay-relative", false),
1304 KeyStatusPair("ignore-non-existent-contents", false),
1305 KeyStatusPair("roots", true),
1308 DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1309 std::vector<std::unique_ptr<Entry>> RootEntries;
1311 // Parse configuration and 'roots'
1312 for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
1314 SmallString<10> KeyBuffer;
1316 if (!parseScalarString(I->getKey(), Key, KeyBuffer))
1319 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
1322 if (Key == "roots") {
1323 yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
1325 error(I->getValue(), "expected array");
1329 for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
1331 if (std::unique_ptr<Entry> E = parseEntry(&*I, FS))
1332 RootEntries.push_back(std::move(E));
1336 } else if (Key == "version") {
1337 StringRef VersionString;
1338 SmallString<4> Storage;
1339 if (!parseScalarString(I->getValue(), VersionString, Storage))
1342 if (VersionString.getAsInteger<int>(10, Version)) {
1343 error(I->getValue(), "expected integer");
1347 error(I->getValue(), "invalid version number");
1351 error(I->getValue(), "version mismatch, expected 0");
1354 } else if (Key == "case-sensitive") {
1355 if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
1357 } else if (Key == "overlay-relative") {
1358 if (!parseScalarBool(I->getValue(), FS->IsRelativeOverlay))
1360 } else if (Key == "use-external-names") {
1361 if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
1363 } else if (Key == "ignore-non-existent-contents") {
1364 if (!parseScalarBool(I->getValue(), FS->IgnoreNonExistentContents))
1367 llvm_unreachable("key missing from Keys");
1371 if (Stream.failed())
1374 if (!checkMissingKeys(Top, Keys))
1377 // Now that we sucessefully parsed the YAML file, canonicalize the internal
1378 // representation to a proper directory tree so that we can search faster
1380 for (std::unique_ptr<Entry> &E : RootEntries)
1381 uniqueOverlayTree(FS, E.get());
1386 } // end of anonymous namespace
1388 Entry::~Entry() = default;
1390 RedirectingFileSystem *
1391 RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
1392 SourceMgr::DiagHandlerTy DiagHandler,
1393 StringRef YAMLFilePath, void *DiagContext,
1394 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1397 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
1399 SM.setDiagHandler(DiagHandler, DiagContext);
1400 yaml::document_iterator DI = Stream.begin();
1401 yaml::Node *Root = DI->getRoot();
1402 if (DI == Stream.end() || !Root) {
1403 SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
1407 RedirectingFileSystemParser P(Stream);
1409 std::unique_ptr<RedirectingFileSystem> FS(
1410 new RedirectingFileSystem(std::move(ExternalFS)));
1412 if (!YAMLFilePath.empty()) {
1413 // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed
1414 // to each 'external-contents' path.
1417 // -ivfsoverlay dummy.cache/vfs/vfs.yaml
1419 // FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs
1421 SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
1422 std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
1423 assert(!EC && "Overlay dir final path must be absolute");
1425 FS->setExternalContentsPrefixDir(OverlayAbsDir);
1428 if (!P.parse(Root, FS.get()))
1431 return FS.release();
1434 ErrorOr<Entry *> RedirectingFileSystem::lookupPath(const Twine &Path_) {
1435 SmallString<256> Path;
1436 Path_.toVector(Path);
1438 // Handle relative paths
1439 if (std::error_code EC = makeAbsolute(Path))
1442 // Canonicalize path by removing ".", "..", "./", etc components. This is
1443 // a VFS request, do bot bother about symlinks in the path components
1444 // but canonicalize in order to perform the correct entry search.
1445 if (UseCanonicalizedPaths) {
1446 Path = sys::path::remove_leading_dotslash(Path);
1447 sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1451 return make_error_code(llvm::errc::invalid_argument);
1453 sys::path::const_iterator Start = sys::path::begin(Path);
1454 sys::path::const_iterator End = sys::path::end(Path);
1455 for (const std::unique_ptr<Entry> &Root : Roots) {
1456 ErrorOr<Entry *> Result = lookupPath(Start, End, Root.get());
1457 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1460 return make_error_code(llvm::errc::no_such_file_or_directory);
1464 RedirectingFileSystem::lookupPath(sys::path::const_iterator Start,
1465 sys::path::const_iterator End, Entry *From) {
1466 #ifndef LLVM_ON_WIN32
1467 assert(!isTraversalComponent(*Start) &&
1468 !isTraversalComponent(From->getName()) &&
1469 "Paths should not contain traversal components");
1471 // FIXME: this is here to support windows, remove it once canonicalized
1472 // paths become globally default.
1473 if (Start->equals("."))
1477 StringRef FromName = From->getName();
1479 // Forward the search to the next component in case this is an empty one.
1480 if (!FromName.empty()) {
1481 if (CaseSensitive ? !Start->equals(FromName)
1482 : !Start->equals_lower(FromName))
1484 return make_error_code(llvm::errc::no_such_file_or_directory);
1494 auto *DE = dyn_cast<RedirectingDirectoryEntry>(From);
1496 return make_error_code(llvm::errc::not_a_directory);
1498 for (const std::unique_ptr<Entry> &DirEntry :
1499 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1500 ErrorOr<Entry *> Result = lookupPath(Start, End, DirEntry.get());
1501 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1504 return make_error_code(llvm::errc::no_such_file_or_directory);
1507 static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames,
1508 Status ExternalStatus) {
1509 Status S = ExternalStatus;
1510 if (!UseExternalNames)
1511 S = Status::copyWithNewName(S, Path.str());
1512 S.IsVFSMapped = true;
1516 ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path, Entry *E) {
1517 assert(E != nullptr);
1518 if (auto *F = dyn_cast<RedirectingFileEntry>(E)) {
1519 ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
1520 assert(!S || S->getName() == F->getExternalContentsPath());
1522 return getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
1525 } else { // directory
1526 auto *DE = cast<RedirectingDirectoryEntry>(E);
1527 return Status::copyWithNewName(DE->getStatus(), Path.str());
1531 ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path) {
1532 ErrorOr<Entry *> Result = lookupPath(Path);
1534 return Result.getError();
1535 return status(Path, *Result);
1539 /// Provide a file wrapper with an overriden status.
1540 class FileWithFixedStatus : public File {
1541 std::unique_ptr<File> InnerFile;
1545 FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S)
1546 : InnerFile(std::move(InnerFile)), S(std::move(S)) {}
1548 ErrorOr<Status> status() override { return S; }
1549 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
1550 getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
1551 bool IsVolatile) override {
1552 return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
1555 std::error_code close() override { return InnerFile->close(); }
1557 } // end anonymous namespace
1559 ErrorOr<std::unique_ptr<File>>
1560 RedirectingFileSystem::openFileForRead(const Twine &Path) {
1561 ErrorOr<Entry *> E = lookupPath(Path);
1563 return E.getError();
1565 auto *F = dyn_cast<RedirectingFileEntry>(*E);
1566 if (!F) // FIXME: errc::not_a_file?
1567 return make_error_code(llvm::errc::invalid_argument);
1569 auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath());
1573 auto ExternalStatus = (*Result)->status();
1574 if (!ExternalStatus)
1575 return ExternalStatus.getError();
1577 // FIXME: Update the status with the name and VFSMapped.
1578 Status S = getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
1580 return std::unique_ptr<File>(
1581 llvm::make_unique<FileWithFixedStatus>(std::move(*Result), S));
1584 IntrusiveRefCntPtr<FileSystem>
1585 vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
1586 SourceMgr::DiagHandlerTy DiagHandler,
1587 StringRef YAMLFilePath,
1589 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1590 return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
1591 YAMLFilePath, DiagContext,
1592 std::move(ExternalFS));
1595 static void getVFSEntries(Entry *SrcE, SmallVectorImpl<StringRef> &Path,
1596 SmallVectorImpl<YAMLVFSEntry> &Entries) {
1597 auto Kind = SrcE->getKind();
1598 if (Kind == EK_Directory) {
1599 auto *DE = dyn_cast<RedirectingDirectoryEntry>(SrcE);
1600 assert(DE && "Must be a directory");
1601 for (std::unique_ptr<Entry> &SubEntry :
1602 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1603 Path.push_back(SubEntry->getName());
1604 getVFSEntries(SubEntry.get(), Path, Entries);
1610 assert(Kind == EK_File && "Must be a EK_File");
1611 auto *FE = dyn_cast<RedirectingFileEntry>(SrcE);
1612 assert(FE && "Must be a file");
1613 SmallString<128> VPath;
1614 for (auto &Comp : Path)
1615 llvm::sys::path::append(VPath, Comp);
1616 Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
1619 void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
1620 SourceMgr::DiagHandlerTy DiagHandler,
1621 StringRef YAMLFilePath,
1622 SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
1624 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1625 RedirectingFileSystem *VFS = RedirectingFileSystem::create(
1626 std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
1627 std::move(ExternalFS));
1628 ErrorOr<Entry *> RootE = VFS->lookupPath("/");
1631 SmallVector<StringRef, 8> Components;
1632 Components.push_back("/");
1633 getVFSEntries(*RootE, Components, CollectedEntries);
1636 UniqueID vfs::getNextVirtualUniqueID() {
1637 static std::atomic<unsigned> UID;
1638 unsigned ID = ++UID;
1639 // The following assumes that uint64_t max will never collide with a real
1640 // dev_t value from the OS.
1641 return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
1644 void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
1645 assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
1646 assert(sys::path::is_absolute(RealPath) && "real path not absolute");
1647 assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
1648 Mappings.emplace_back(VirtualPath, RealPath);
1653 llvm::raw_ostream &OS;
1654 SmallVector<StringRef, 16> DirStack;
1655 inline unsigned getDirIndent() { return 4 * DirStack.size(); }
1656 inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
1657 bool containedIn(StringRef Parent, StringRef Path);
1658 StringRef containedPart(StringRef Parent, StringRef Path);
1659 void startDirectory(StringRef Path);
1660 void endDirectory();
1661 void writeEntry(StringRef VPath, StringRef RPath);
1664 JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
1665 void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames,
1666 Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative,
1667 Optional<bool> IgnoreNonExistentContents, StringRef OverlayDir);
1671 bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
1672 using namespace llvm::sys;
1673 // Compare each path component.
1674 auto IParent = path::begin(Parent), EParent = path::end(Parent);
1675 for (auto IChild = path::begin(Path), EChild = path::end(Path);
1676 IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
1677 if (*IParent != *IChild)
1680 // Have we exhausted the parent path?
1681 return IParent == EParent;
1684 StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
1685 assert(!Parent.empty());
1686 assert(containedIn(Parent, Path));
1687 return Path.slice(Parent.size() + 1, StringRef::npos);
1690 void JSONWriter::startDirectory(StringRef Path) {
1692 DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
1693 DirStack.push_back(Path);
1694 unsigned Indent = getDirIndent();
1695 OS.indent(Indent) << "{\n";
1696 OS.indent(Indent + 2) << "'type': 'directory',\n";
1697 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
1698 OS.indent(Indent + 2) << "'contents': [\n";
1701 void JSONWriter::endDirectory() {
1702 unsigned Indent = getDirIndent();
1703 OS.indent(Indent + 2) << "]\n";
1704 OS.indent(Indent) << "}";
1706 DirStack.pop_back();
1709 void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
1710 unsigned Indent = getFileIndent();
1711 OS.indent(Indent) << "{\n";
1712 OS.indent(Indent + 2) << "'type': 'file',\n";
1713 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
1714 OS.indent(Indent + 2) << "'external-contents': \""
1715 << llvm::yaml::escape(RPath) << "\"\n";
1716 OS.indent(Indent) << "}";
1719 void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
1720 Optional<bool> UseExternalNames,
1721 Optional<bool> IsCaseSensitive,
1722 Optional<bool> IsOverlayRelative,
1723 Optional<bool> IgnoreNonExistentContents,
1724 StringRef OverlayDir) {
1725 using namespace llvm::sys;
1729 if (IsCaseSensitive.hasValue())
1730 OS << " 'case-sensitive': '"
1731 << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
1732 if (UseExternalNames.hasValue())
1733 OS << " 'use-external-names': '"
1734 << (UseExternalNames.getValue() ? "true" : "false") << "',\n";
1735 bool UseOverlayRelative = false;
1736 if (IsOverlayRelative.hasValue()) {
1737 UseOverlayRelative = IsOverlayRelative.getValue();
1738 OS << " 'overlay-relative': '"
1739 << (UseOverlayRelative ? "true" : "false") << "',\n";
1741 if (IgnoreNonExistentContents.hasValue())
1742 OS << " 'ignore-non-existent-contents': '"
1743 << (IgnoreNonExistentContents.getValue() ? "true" : "false") << "',\n";
1744 OS << " 'roots': [\n";
1746 if (!Entries.empty()) {
1747 const YAMLVFSEntry &Entry = Entries.front();
1748 startDirectory(path::parent_path(Entry.VPath));
1750 StringRef RPath = Entry.RPath;
1751 if (UseOverlayRelative) {
1752 unsigned OverlayDirLen = OverlayDir.size();
1753 assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
1754 "Overlay dir must be contained in RPath");
1755 RPath = RPath.slice(OverlayDirLen, RPath.size());
1758 writeEntry(path::filename(Entry.VPath), RPath);
1760 for (const auto &Entry : Entries.slice(1)) {
1761 StringRef Dir = path::parent_path(Entry.VPath);
1762 if (Dir == DirStack.back())
1765 while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
1770 startDirectory(Dir);
1772 StringRef RPath = Entry.RPath;
1773 if (UseOverlayRelative) {
1774 unsigned OverlayDirLen = OverlayDir.size();
1775 assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
1776 "Overlay dir must be contained in RPath");
1777 RPath = RPath.slice(OverlayDirLen, RPath.size());
1779 writeEntry(path::filename(Entry.VPath), RPath);
1782 while (!DirStack.empty()) {
1793 void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
1794 std::sort(Mappings.begin(), Mappings.end(),
1795 [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
1796 return LHS.VPath < RHS.VPath;
1799 JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
1800 IsOverlayRelative, IgnoreNonExistentContents,
1804 VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(
1805 const Twine &_Path, RedirectingFileSystem &FS,
1806 RedirectingDirectoryEntry::iterator Begin,
1807 RedirectingDirectoryEntry::iterator End, std::error_code &EC)
1808 : Dir(_Path.str()), FS(FS), Current(Begin), End(End) {
1809 while (Current != End) {
1810 SmallString<128> PathStr(Dir);
1811 llvm::sys::path::append(PathStr, (*Current)->getName());
1812 llvm::ErrorOr<vfs::Status> S = FS.status(PathStr);
1817 // Skip entries which do not map to a reliable external content.
1818 if (FS.ignoreNonExistentContents() &&
1819 S.getError() == llvm::errc::no_such_file_or_directory) {
1829 std::error_code VFSFromYamlDirIterImpl::increment() {
1830 assert(Current != End && "cannot iterate past end");
1831 while (++Current != End) {
1832 SmallString<128> PathStr(Dir);
1833 llvm::sys::path::append(PathStr, (*Current)->getName());
1834 llvm::ErrorOr<vfs::Status> S = FS.status(PathStr);
1836 // Skip entries which do not map to a reliable external content.
1837 if (FS.ignoreNonExistentContents() &&
1838 S.getError() == llvm::errc::no_such_file_or_directory) {
1841 return S.getError();
1849 CurrentEntry = Status();
1850 return std::error_code();
1853 vfs::recursive_directory_iterator::recursive_directory_iterator(FileSystem &FS_,
1855 std::error_code &EC)
1857 directory_iterator I = FS->dir_begin(Path, EC);
1858 if (I != directory_iterator()) {
1859 State = std::make_shared<IterState>();
1864 vfs::recursive_directory_iterator &
1865 recursive_directory_iterator::increment(std::error_code &EC) {
1866 assert(FS && State && !State->empty() && "incrementing past end");
1867 assert(State->top()->isStatusKnown() && "non-canonical end iterator");
1868 vfs::directory_iterator End;
1869 if (State->top()->isDirectory()) {
1870 vfs::directory_iterator I = FS->dir_begin(State->top()->getName(), EC);
1877 while (!State->empty() && State->top().increment(EC) == End)
1881 State.reset(); // end iterator