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/Support/Errc.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/YAMLParser.h"
23 #include "llvm/Config/llvm-config.h"
34 using namespace clang;
35 using namespace clang::vfs;
37 using llvm::sys::fs::file_status;
38 using llvm::sys::fs::file_type;
39 using llvm::sys::fs::perms;
40 using llvm::sys::fs::UniqueID;
42 Status::Status(const file_status &Status)
43 : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
44 User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
45 Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false) {}
47 Status::Status(StringRef Name, UniqueID UID, sys::TimeValue MTime,
48 uint32_t User, uint32_t Group, uint64_t Size, file_type Type,
50 : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
51 Type(Type), Perms(Perms), IsVFSMapped(false) {}
53 Status Status::copyWithNewName(const Status &In, StringRef NewName) {
54 return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
55 In.getUser(), In.getGroup(), In.getSize(), In.getType(),
59 Status Status::copyWithNewName(const file_status &In, StringRef NewName) {
60 return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
61 In.getUser(), In.getGroup(), In.getSize(), In.type(),
65 bool Status::equivalent(const Status &Other) const {
66 return getUniqueID() == Other.getUniqueID();
68 bool Status::isDirectory() const {
69 return Type == file_type::directory_file;
71 bool Status::isRegularFile() const {
72 return Type == file_type::regular_file;
74 bool Status::isOther() const {
75 return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
77 bool Status::isSymlink() const {
78 return Type == file_type::symlink_file;
80 bool Status::isStatusKnown() const {
81 return Type != file_type::status_error;
83 bool Status::exists() const {
84 return isStatusKnown() && Type != file_type::file_not_found;
89 FileSystem::~FileSystem() {}
91 ErrorOr<std::unique_ptr<MemoryBuffer>>
92 FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
93 bool RequiresNullTerminator, bool IsVolatile) {
94 auto F = openFileForRead(Name);
98 return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
101 std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
102 auto WorkingDir = getCurrentWorkingDirectory();
104 return WorkingDir.getError();
106 return llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
109 bool FileSystem::exists(const Twine &Path) {
110 auto Status = status(Path);
111 return Status && Status->exists();
114 //===-----------------------------------------------------------------------===/
115 // RealFileSystem implementation
116 //===-----------------------------------------------------------------------===/
119 /// \brief Wrapper around a raw file descriptor.
120 class RealFile : public File {
123 friend class RealFileSystem;
124 RealFile(int FD, StringRef NewName)
125 : FD(FD), S(NewName, {}, {}, {}, {}, {},
126 llvm::sys::fs::file_type::status_error, {}) {
127 assert(FD >= 0 && "Invalid or inactive file descriptor");
131 ~RealFile() override;
132 ErrorOr<Status> status() override;
133 ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name,
135 bool RequiresNullTerminator,
136 bool IsVolatile) override;
137 std::error_code close() override;
139 } // end anonymous namespace
140 RealFile::~RealFile() { close(); }
142 ErrorOr<Status> RealFile::status() {
143 assert(FD != -1 && "cannot stat closed file");
144 if (!S.isStatusKnown()) {
145 file_status RealStatus;
146 if (std::error_code EC = sys::fs::status(FD, RealStatus))
148 S = Status::copyWithNewName(RealStatus, S.getName());
153 ErrorOr<std::unique_ptr<MemoryBuffer>>
154 RealFile::getBuffer(const Twine &Name, int64_t FileSize,
155 bool RequiresNullTerminator, bool IsVolatile) {
156 assert(FD != -1 && "cannot get buffer for closed file");
157 return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
161 // FIXME: This is terrible, we need this for ::close.
162 #if !defined(_MSC_VER) && !defined(__MINGW32__)
168 #define S_ISFIFO(x) (0)
171 std::error_code RealFile::close() {
173 return std::error_code(errno, std::generic_category());
175 return std::error_code();
179 /// \brief The file system according to your operating system.
180 class RealFileSystem : public FileSystem {
182 ErrorOr<Status> status(const Twine &Path) override;
183 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
184 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
186 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
187 std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
189 } // end anonymous namespace
191 ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
192 sys::fs::file_status RealStatus;
193 if (std::error_code EC = sys::fs::status(Path, RealStatus))
195 return Status::copyWithNewName(RealStatus, Path.str());
198 ErrorOr<std::unique_ptr<File>>
199 RealFileSystem::openFileForRead(const Twine &Name) {
201 if (std::error_code EC = sys::fs::openFileForRead(Name, FD))
203 return std::unique_ptr<File>(new RealFile(FD, Name.str()));
206 llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
207 SmallString<256> Dir;
208 if (std::error_code EC = llvm::sys::fs::current_path(Dir))
210 return Dir.str().str();
213 std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
214 // FIXME: chdir is thread hostile; on the other hand, creating the same
215 // behavior as chdir is complex: chdir resolves the path once, thus
216 // guaranteeing that all subsequent relative path operations work
217 // on the same path the original chdir resulted in. This makes a
218 // difference for example on network filesystems, where symlinks might be
219 // switched during runtime of the tool. Fixing this depends on having a
220 // file system abstraction that allows openat() style interactions.
221 SmallString<256> Storage;
222 StringRef Dir = Path.toNullTerminatedStringRef(Storage);
223 if (int Err = ::chdir(Dir.data()))
224 return std::error_code(Err, std::generic_category());
225 return std::error_code();
228 IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
229 static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
234 class RealFSDirIter : public clang::vfs::detail::DirIterImpl {
236 llvm::sys::fs::directory_iterator Iter;
238 RealFSDirIter(const Twine &_Path, std::error_code &EC)
239 : Path(_Path.str()), Iter(Path, EC) {
240 if (!EC && Iter != llvm::sys::fs::directory_iterator()) {
241 llvm::sys::fs::file_status S;
242 EC = Iter->status(S);
244 CurrentEntry = Status::copyWithNewName(S, Iter->path());
248 std::error_code increment() override {
253 } else if (Iter == llvm::sys::fs::directory_iterator()) {
254 CurrentEntry = Status();
256 llvm::sys::fs::file_status S;
257 EC = Iter->status(S);
258 CurrentEntry = Status::copyWithNewName(S, Iter->path());
265 directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
266 std::error_code &EC) {
267 return directory_iterator(std::make_shared<RealFSDirIter>(Dir, EC));
270 //===-----------------------------------------------------------------------===/
271 // OverlayFileSystem implementation
272 //===-----------------------------------------------------------------------===/
273 OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
274 FSList.push_back(BaseFS);
277 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
278 FSList.push_back(FS);
279 // Synchronize added file systems by duplicating the working directory from
280 // the first one in the list.
281 FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
284 ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
285 // FIXME: handle symlinks that cross file systems
286 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
287 ErrorOr<Status> Status = (*I)->status(Path);
288 if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
291 return make_error_code(llvm::errc::no_such_file_or_directory);
294 ErrorOr<std::unique_ptr<File>>
295 OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
296 // FIXME: handle symlinks that cross file systems
297 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
298 auto Result = (*I)->openFileForRead(Path);
299 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
302 return make_error_code(llvm::errc::no_such_file_or_directory);
305 llvm::ErrorOr<std::string>
306 OverlayFileSystem::getCurrentWorkingDirectory() const {
307 // All file systems are synchronized, just take the first working directory.
308 return FSList.front()->getCurrentWorkingDirectory();
311 OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
312 for (auto &FS : FSList)
313 if (std::error_code EC = FS->setCurrentWorkingDirectory(Path))
315 return std::error_code();
318 clang::vfs::detail::DirIterImpl::~DirIterImpl() { }
321 class OverlayFSDirIterImpl : public clang::vfs::detail::DirIterImpl {
322 OverlayFileSystem &Overlays;
324 OverlayFileSystem::iterator CurrentFS;
325 directory_iterator CurrentDirIter;
326 llvm::StringSet<> SeenNames;
328 std::error_code incrementFS() {
329 assert(CurrentFS != Overlays.overlays_end() && "incrementing past end");
331 for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) {
333 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
334 if (EC && EC != errc::no_such_file_or_directory)
336 if (CurrentDirIter != directory_iterator())
339 return std::error_code();
342 std::error_code incrementDirIter(bool IsFirstTime) {
343 assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
344 "incrementing past end");
347 CurrentDirIter.increment(EC);
348 if (!EC && CurrentDirIter == directory_iterator())
353 std::error_code incrementImpl(bool IsFirstTime) {
355 std::error_code EC = incrementDirIter(IsFirstTime);
356 if (EC || CurrentDirIter == directory_iterator()) {
357 CurrentEntry = Status();
360 CurrentEntry = *CurrentDirIter;
361 StringRef Name = llvm::sys::path::filename(CurrentEntry.getName());
362 if (SeenNames.insert(Name).second)
363 return EC; // name not seen before
365 llvm_unreachable("returned above");
369 OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS,
371 : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) {
372 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
373 EC = incrementImpl(true);
376 std::error_code increment() override { return incrementImpl(false); }
378 } // end anonymous namespace
380 directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
381 std::error_code &EC) {
382 return directory_iterator(
383 std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC));
390 enum InMemoryNodeKind { IME_File, IME_Directory };
392 /// The in memory file system is a tree of Nodes. Every node can either be a
393 /// file or a directory.
396 InMemoryNodeKind Kind;
399 InMemoryNode(Status Stat, InMemoryNodeKind Kind)
400 : Stat(std::move(Stat)), Kind(Kind) {}
401 virtual ~InMemoryNode() {}
402 const Status &getStatus() const { return Stat; }
403 InMemoryNodeKind getKind() const { return Kind; }
404 virtual std::string toString(unsigned Indent) const = 0;
408 class InMemoryFile : public InMemoryNode {
409 std::unique_ptr<llvm::MemoryBuffer> Buffer;
412 InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer)
413 : InMemoryNode(std::move(Stat), IME_File), Buffer(std::move(Buffer)) {}
415 llvm::MemoryBuffer *getBuffer() { return Buffer.get(); }
416 std::string toString(unsigned Indent) const override {
417 return (std::string(Indent, ' ') + getStatus().getName() + "\n").str();
419 static bool classof(const InMemoryNode *N) {
420 return N->getKind() == IME_File;
424 /// Adapt a InMemoryFile for VFS' File interface.
425 class InMemoryFileAdaptor : public File {
429 explicit InMemoryFileAdaptor(InMemoryFile &Node) : Node(Node) {}
431 llvm::ErrorOr<Status> status() override { return Node.getStatus(); }
432 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
433 getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
434 bool IsVolatile) override {
435 llvm::MemoryBuffer *Buf = Node.getBuffer();
436 return llvm::MemoryBuffer::getMemBuffer(
437 Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator);
439 std::error_code close() override { return std::error_code(); }
441 } // end anonymous namespace
443 class InMemoryDirectory : public InMemoryNode {
444 std::map<std::string, std::unique_ptr<InMemoryNode>> Entries;
447 InMemoryDirectory(Status Stat)
448 : InMemoryNode(std::move(Stat), IME_Directory) {}
449 InMemoryNode *getChild(StringRef Name) {
450 auto I = Entries.find(Name);
451 if (I != Entries.end())
452 return I->second.get();
455 InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) {
456 return Entries.insert(make_pair(Name, std::move(Child)))
457 .first->second.get();
460 typedef decltype(Entries)::const_iterator const_iterator;
461 const_iterator begin() const { return Entries.begin(); }
462 const_iterator end() const { return Entries.end(); }
464 std::string toString(unsigned Indent) const override {
466 (std::string(Indent, ' ') + getStatus().getName() + "\n").str();
467 for (const auto &Entry : Entries) {
468 Result += Entry.second->toString(Indent + 2);
472 static bool classof(const InMemoryNode *N) {
473 return N->getKind() == IME_Directory;
478 InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths)
479 : Root(new detail::InMemoryDirectory(
480 Status("", getNextVirtualUniqueID(), llvm::sys::TimeValue::MinTime(),
481 0, 0, 0, llvm::sys::fs::file_type::directory_file,
482 llvm::sys::fs::perms::all_all))),
483 UseNormalizedPaths(UseNormalizedPaths) {}
485 InMemoryFileSystem::~InMemoryFileSystem() {}
487 std::string InMemoryFileSystem::toString() const {
488 return Root->toString(/*Indent=*/0);
491 bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
492 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
493 SmallString<128> Path;
496 // Fix up relative paths. This just prepends the current working directory.
497 std::error_code EC = makeAbsolute(Path);
501 if (useNormalizedPaths())
502 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
507 detail::InMemoryDirectory *Dir = Root.get();
508 auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
511 detail::InMemoryNode *Node = Dir->getChild(Name);
515 // End of the path, create a new file.
516 // FIXME: expose the status details in the interface.
517 Status Stat(P.str(), getNextVirtualUniqueID(),
518 llvm::sys::TimeValue(ModificationTime, 0), 0, 0,
519 Buffer->getBufferSize(),
520 llvm::sys::fs::file_type::regular_file,
521 llvm::sys::fs::all_all);
522 Dir->addChild(Name, llvm::make_unique<detail::InMemoryFile>(
523 std::move(Stat), std::move(Buffer)));
527 // Create a new directory. Use the path up to here.
528 // FIXME: expose the status details in the interface.
530 StringRef(Path.str().begin(), Name.end() - Path.str().begin()),
531 getNextVirtualUniqueID(), llvm::sys::TimeValue(ModificationTime, 0),
532 0, 0, Buffer->getBufferSize(),
533 llvm::sys::fs::file_type::directory_file, llvm::sys::fs::all_all);
534 Dir = cast<detail::InMemoryDirectory>(Dir->addChild(
535 Name, llvm::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
539 if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
542 assert(isa<detail::InMemoryFile>(Node) &&
543 "Must be either file or directory!");
545 // Trying to insert a directory in place of a file.
549 // Return false only if the new file is different from the existing one.
550 return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() ==
556 bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime,
557 llvm::MemoryBuffer *Buffer) {
558 return addFile(P, ModificationTime,
559 llvm::MemoryBuffer::getMemBuffer(
560 Buffer->getBuffer(), Buffer->getBufferIdentifier()));
563 static ErrorOr<detail::InMemoryNode *>
564 lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir,
566 SmallString<128> Path;
569 // Fix up relative paths. This just prepends the current working directory.
570 std::error_code EC = FS.makeAbsolute(Path);
574 if (FS.useNormalizedPaths())
575 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
580 auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
582 detail::InMemoryNode *Node = Dir->getChild(*I);
585 return errc::no_such_file_or_directory;
587 // Return the file if it's at the end of the path.
588 if (auto File = dyn_cast<detail::InMemoryFile>(Node)) {
591 return errc::no_such_file_or_directory;
594 // Traverse directories.
595 Dir = cast<detail::InMemoryDirectory>(Node);
601 llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) {
602 auto Node = lookupInMemoryNode(*this, Root.get(), Path);
604 return (*Node)->getStatus();
605 return Node.getError();
608 llvm::ErrorOr<std::unique_ptr<File>>
609 InMemoryFileSystem::openFileForRead(const Twine &Path) {
610 auto Node = lookupInMemoryNode(*this, Root.get(), Path);
612 return Node.getError();
614 // When we have a file provide a heap-allocated wrapper for the memory buffer
615 // to match the ownership semantics for File.
616 if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
617 return std::unique_ptr<File>(new detail::InMemoryFileAdaptor(*F));
619 // FIXME: errc::not_a_file?
620 return make_error_code(llvm::errc::invalid_argument);
624 /// Adaptor from InMemoryDir::iterator to directory_iterator.
625 class InMemoryDirIterator : public clang::vfs::detail::DirIterImpl {
626 detail::InMemoryDirectory::const_iterator I;
627 detail::InMemoryDirectory::const_iterator E;
630 InMemoryDirIterator() {}
631 explicit InMemoryDirIterator(detail::InMemoryDirectory &Dir)
632 : I(Dir.begin()), E(Dir.end()) {
634 CurrentEntry = I->second->getStatus();
637 std::error_code increment() override {
639 // When we're at the end, make CurrentEntry invalid and DirIterImpl will do
641 CurrentEntry = I != E ? I->second->getStatus() : Status();
642 return std::error_code();
645 } // end anonymous namespace
647 directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
648 std::error_code &EC) {
649 auto Node = lookupInMemoryNode(*this, Root.get(), Dir);
651 EC = Node.getError();
652 return directory_iterator(std::make_shared<InMemoryDirIterator>());
655 if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node))
656 return directory_iterator(std::make_shared<InMemoryDirIterator>(*DirNode));
658 EC = make_error_code(llvm::errc::not_a_directory);
659 return directory_iterator(std::make_shared<InMemoryDirIterator>());
662 std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
663 SmallString<128> Path;
666 // Fix up relative paths. This just prepends the current working directory.
667 std::error_code EC = makeAbsolute(Path);
671 if (useNormalizedPaths())
672 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
675 WorkingDirectory = Path.str();
676 return std::error_code();
681 //===-----------------------------------------------------------------------===/
682 // RedirectingFileSystem implementation
683 //===-----------------------------------------------------------------------===/
692 /// \brief A single file or directory in the VFS.
699 Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
700 StringRef getName() const { return Name; }
701 EntryKind getKind() const { return Kind; }
704 class RedirectingDirectoryEntry : public Entry {
705 std::vector<std::unique_ptr<Entry>> Contents;
709 RedirectingDirectoryEntry(StringRef Name,
710 std::vector<std::unique_ptr<Entry>> Contents,
712 : Entry(EK_Directory, Name), Contents(std::move(Contents)),
714 Status getStatus() { return S; }
715 typedef decltype(Contents)::iterator iterator;
716 iterator contents_begin() { return Contents.begin(); }
717 iterator contents_end() { return Contents.end(); }
718 static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
721 class RedirectingFileEntry : public Entry {
729 std::string ExternalContentsPath;
732 RedirectingFileEntry(StringRef Name, StringRef ExternalContentsPath,
734 : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
736 StringRef getExternalContentsPath() const { return ExternalContentsPath; }
737 /// \brief whether to use the external path as the name for this file.
738 bool useExternalName(bool GlobalUseExternalName) const {
739 return UseName == NK_NotSet ? GlobalUseExternalName
740 : (UseName == NK_External);
742 static bool classof(const Entry *E) { return E->getKind() == EK_File; }
745 class RedirectingFileSystem;
747 class VFSFromYamlDirIterImpl : public clang::vfs::detail::DirIterImpl {
749 RedirectingFileSystem &FS;
750 RedirectingDirectoryEntry::iterator Current, End;
753 VFSFromYamlDirIterImpl(const Twine &Path, RedirectingFileSystem &FS,
754 RedirectingDirectoryEntry::iterator Begin,
755 RedirectingDirectoryEntry::iterator End,
756 std::error_code &EC);
757 std::error_code increment() override;
760 /// \brief A virtual file system parsed from a YAML file.
762 /// Currently, this class allows creating virtual directories and mapping
763 /// virtual file paths to existing external files, available in \c ExternalFS.
765 /// The basic structure of the parsed file is:
768 /// 'version': <version number>,
769 /// <optional configuration>
771 /// <directory entries>
776 /// All configuration options are optional.
777 /// 'case-sensitive': <boolean, default=true>
778 /// 'use-external-names': <boolean, default=true>
780 /// Virtual directories are represented as
783 /// 'type': 'directory',
784 /// 'name': <string>,
785 /// 'contents': [ <file or directory entries> ]
789 /// The default attributes for virtual directories are:
791 /// MTime = now() when created
795 /// UniqueID = unspecified unique value
798 /// Re-mapped files are represented as
802 /// 'name': <string>,
803 /// 'use-external-name': <boolean> # Optional
804 /// 'external-contents': <path to external file>)
808 /// and inherit their attributes from the external contents.
810 /// In both cases, the 'name' field may contain multiple path components (e.g.
811 /// /path/to/file). However, any directory that contains more than one child
812 /// must be uniquely represented by a directory entry.
813 class RedirectingFileSystem : public vfs::FileSystem {
814 /// The root(s) of the virtual file system.
815 std::vector<std::unique_ptr<Entry>> Roots;
816 /// \brief The file system to use for external references.
817 IntrusiveRefCntPtr<FileSystem> ExternalFS;
819 /// @name Configuration
822 /// \brief Whether to perform case-sensitive comparisons.
824 /// Currently, case-insensitive matching only works correctly with ASCII.
827 /// \brief Whether to use to use the value of 'external-contents' for the
828 /// names of files. This global value is overridable on a per-file basis.
829 bool UseExternalNames;
832 friend class RedirectingFileSystemParser;
835 RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS)
836 : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {}
838 /// \brief Looks up \p Path in \c Roots.
839 ErrorOr<Entry *> lookupPath(const Twine &Path);
841 /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly
842 /// recursing into the contents of \p From if it is a directory.
843 ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
844 sys::path::const_iterator End, Entry *From);
846 /// \brief Get the status of a given an \c Entry.
847 ErrorOr<Status> status(const Twine &Path, Entry *E);
850 /// \brief Parses \p Buffer, which is expected to be in YAML format and
851 /// returns a virtual file system representing its contents.
852 static RedirectingFileSystem *
853 create(std::unique_ptr<MemoryBuffer> Buffer,
854 SourceMgr::DiagHandlerTy DiagHandler, void *DiagContext,
855 IntrusiveRefCntPtr<FileSystem> ExternalFS);
857 ErrorOr<Status> status(const Twine &Path) override;
858 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
860 llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
861 return ExternalFS->getCurrentWorkingDirectory();
863 std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
864 return ExternalFS->setCurrentWorkingDirectory(Path);
867 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{
868 ErrorOr<Entry *> E = lookupPath(Dir);
871 return directory_iterator();
873 ErrorOr<Status> S = status(Dir, *E);
876 return directory_iterator();
878 if (!S->isDirectory()) {
879 EC = std::error_code(static_cast<int>(errc::not_a_directory),
880 std::system_category());
881 return directory_iterator();
884 auto *D = cast<RedirectingDirectoryEntry>(*E);
885 return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(Dir,
886 *this, D->contents_begin(), D->contents_end(), EC));
890 /// \brief A helper class to hold the common YAML parsing state.
891 class RedirectingFileSystemParser {
892 yaml::Stream &Stream;
894 void error(yaml::Node *N, const Twine &Msg) {
895 Stream.printError(N, Msg);
899 bool parseScalarString(yaml::Node *N, StringRef &Result,
900 SmallVectorImpl<char> &Storage) {
901 yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
903 error(N, "expected string");
906 Result = S->getValue(Storage);
911 bool parseScalarBool(yaml::Node *N, bool &Result) {
912 SmallString<5> Storage;
914 if (!parseScalarString(N, Value, Storage))
917 if (Value.equals_lower("true") || Value.equals_lower("on") ||
918 Value.equals_lower("yes") || Value == "1") {
921 } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
922 Value.equals_lower("no") || Value == "0") {
927 error(N, "expected boolean value");
932 KeyStatus(bool Required=false) : Required(Required), Seen(false) {}
936 typedef std::pair<StringRef, KeyStatus> KeyStatusPair;
939 bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
940 DenseMap<StringRef, KeyStatus> &Keys) {
941 if (!Keys.count(Key)) {
942 error(KeyNode, "unknown key");
945 KeyStatus &S = Keys[Key];
947 error(KeyNode, Twine("duplicate key '") + Key + "'");
955 bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
956 for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(),
959 if (I->second.Required && !I->second.Seen) {
960 error(Obj, Twine("missing key '") + I->first + "'");
967 std::unique_ptr<Entry> parseEntry(yaml::Node *N) {
968 yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N);
970 error(N, "expected mapping node for file or directory entry");
974 KeyStatusPair Fields[] = {
975 KeyStatusPair("name", true),
976 KeyStatusPair("type", true),
977 KeyStatusPair("contents", false),
978 KeyStatusPair("external-contents", false),
979 KeyStatusPair("use-external-name", false),
982 DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
984 bool HasContents = false; // external or otherwise
985 std::vector<std::unique_ptr<Entry>> EntryArrayContents;
986 std::string ExternalContentsPath;
988 auto UseExternalName = RedirectingFileEntry::NK_NotSet;
991 for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E;
994 // Reuse the buffer for key and value, since we don't look at key after
996 SmallString<256> Buffer;
997 if (!parseScalarString(I->getKey(), Key, Buffer))
1000 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
1004 if (Key == "name") {
1005 if (!parseScalarString(I->getValue(), Value, Buffer))
1008 } else if (Key == "type") {
1009 if (!parseScalarString(I->getValue(), Value, Buffer))
1011 if (Value == "file")
1013 else if (Value == "directory")
1014 Kind = EK_Directory;
1016 error(I->getValue(), "unknown value for 'type'");
1019 } else if (Key == "contents") {
1022 "entry already has 'contents' or 'external-contents'");
1026 yaml::SequenceNode *Contents =
1027 dyn_cast<yaml::SequenceNode>(I->getValue());
1029 // FIXME: this is only for directories, what about files?
1030 error(I->getValue(), "expected array");
1034 for (yaml::SequenceNode::iterator I = Contents->begin(),
1035 E = Contents->end();
1037 if (std::unique_ptr<Entry> E = parseEntry(&*I))
1038 EntryArrayContents.push_back(std::move(E));
1042 } else if (Key == "external-contents") {
1045 "entry already has 'contents' or 'external-contents'");
1049 if (!parseScalarString(I->getValue(), Value, Buffer))
1051 ExternalContentsPath = Value;
1052 } else if (Key == "use-external-name") {
1054 if (!parseScalarBool(I->getValue(), Val))
1056 UseExternalName = Val ? RedirectingFileEntry::NK_External
1057 : RedirectingFileEntry::NK_Virtual;
1059 llvm_unreachable("key missing from Keys");
1063 if (Stream.failed())
1066 // check for missing keys
1068 error(N, "missing key 'contents' or 'external-contents'");
1071 if (!checkMissingKeys(N, Keys))
1074 // check invalid configuration
1075 if (Kind == EK_Directory &&
1076 UseExternalName != RedirectingFileEntry::NK_NotSet) {
1077 error(N, "'use-external-name' is not supported for directories");
1081 // Remove trailing slash(es), being careful not to remove the root path
1082 StringRef Trimmed(Name);
1083 size_t RootPathLen = sys::path::root_path(Trimmed).size();
1084 while (Trimmed.size() > RootPathLen &&
1085 sys::path::is_separator(Trimmed.back()))
1086 Trimmed = Trimmed.slice(0, Trimmed.size()-1);
1087 // Get the last component
1088 StringRef LastComponent = sys::path::filename(Trimmed);
1090 std::unique_ptr<Entry> Result;
1093 Result = llvm::make_unique<RedirectingFileEntry>(
1094 LastComponent, std::move(ExternalContentsPath), UseExternalName);
1097 Result = llvm::make_unique<RedirectingDirectoryEntry>(
1098 LastComponent, std::move(EntryArrayContents),
1099 Status("", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 0,
1100 file_type::directory_file, sys::fs::all_all));
1104 StringRef Parent = sys::path::parent_path(Trimmed);
1108 // if 'name' contains multiple components, create implicit directory entries
1109 for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
1110 E = sys::path::rend(Parent);
1112 std::vector<std::unique_ptr<Entry>> Entries;
1113 Entries.push_back(std::move(Result));
1114 Result = llvm::make_unique<RedirectingDirectoryEntry>(
1115 *I, std::move(Entries),
1116 Status("", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 0,
1117 file_type::directory_file, sys::fs::all_all));
1123 RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {}
1126 bool parse(yaml::Node *Root, RedirectingFileSystem *FS) {
1127 yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
1129 error(Root, "expected mapping node");
1133 KeyStatusPair Fields[] = {
1134 KeyStatusPair("version", true),
1135 KeyStatusPair("case-sensitive", false),
1136 KeyStatusPair("use-external-names", false),
1137 KeyStatusPair("roots", true),
1140 DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1142 // Parse configuration and 'roots'
1143 for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
1145 SmallString<10> KeyBuffer;
1147 if (!parseScalarString(I->getKey(), Key, KeyBuffer))
1150 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
1153 if (Key == "roots") {
1154 yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
1156 error(I->getValue(), "expected array");
1160 for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
1162 if (std::unique_ptr<Entry> E = parseEntry(&*I))
1163 FS->Roots.push_back(std::move(E));
1167 } else if (Key == "version") {
1168 StringRef VersionString;
1169 SmallString<4> Storage;
1170 if (!parseScalarString(I->getValue(), VersionString, Storage))
1173 if (VersionString.getAsInteger<int>(10, Version)) {
1174 error(I->getValue(), "expected integer");
1178 error(I->getValue(), "invalid version number");
1182 error(I->getValue(), "version mismatch, expected 0");
1185 } else if (Key == "case-sensitive") {
1186 if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
1188 } else if (Key == "use-external-names") {
1189 if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
1192 llvm_unreachable("key missing from Keys");
1196 if (Stream.failed())
1199 if (!checkMissingKeys(Top, Keys))
1204 } // end of anonymous namespace
1206 Entry::~Entry() = default;
1208 RedirectingFileSystem *RedirectingFileSystem::create(
1209 std::unique_ptr<MemoryBuffer> Buffer, SourceMgr::DiagHandlerTy DiagHandler,
1210 void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1213 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
1215 SM.setDiagHandler(DiagHandler, DiagContext);
1216 yaml::document_iterator DI = Stream.begin();
1217 yaml::Node *Root = DI->getRoot();
1218 if (DI == Stream.end() || !Root) {
1219 SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
1223 RedirectingFileSystemParser P(Stream);
1225 std::unique_ptr<RedirectingFileSystem> FS(
1226 new RedirectingFileSystem(ExternalFS));
1227 if (!P.parse(Root, FS.get()))
1230 return FS.release();
1233 ErrorOr<Entry *> RedirectingFileSystem::lookupPath(const Twine &Path_) {
1234 SmallString<256> Path;
1235 Path_.toVector(Path);
1237 // Handle relative paths
1238 if (std::error_code EC = makeAbsolute(Path))
1242 return make_error_code(llvm::errc::invalid_argument);
1244 sys::path::const_iterator Start = sys::path::begin(Path);
1245 sys::path::const_iterator End = sys::path::end(Path);
1246 for (const std::unique_ptr<Entry> &Root : Roots) {
1247 ErrorOr<Entry *> Result = lookupPath(Start, End, Root.get());
1248 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1251 return make_error_code(llvm::errc::no_such_file_or_directory);
1255 RedirectingFileSystem::lookupPath(sys::path::const_iterator Start,
1256 sys::path::const_iterator End, Entry *From) {
1257 if (Start->equals("."))
1261 if (CaseSensitive ? !Start->equals(From->getName())
1262 : !Start->equals_lower(From->getName()))
1264 return make_error_code(llvm::errc::no_such_file_or_directory);
1273 auto *DE = dyn_cast<RedirectingDirectoryEntry>(From);
1275 return make_error_code(llvm::errc::not_a_directory);
1277 for (const std::unique_ptr<Entry> &DirEntry :
1278 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1279 ErrorOr<Entry *> Result = lookupPath(Start, End, DirEntry.get());
1280 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1283 return make_error_code(llvm::errc::no_such_file_or_directory);
1286 static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames,
1287 Status ExternalStatus) {
1288 Status S = ExternalStatus;
1289 if (!UseExternalNames)
1290 S = Status::copyWithNewName(S, Path.str());
1291 S.IsVFSMapped = true;
1295 ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path, Entry *E) {
1296 assert(E != nullptr);
1297 if (auto *F = dyn_cast<RedirectingFileEntry>(E)) {
1298 ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
1299 assert(!S || S->getName() == F->getExternalContentsPath());
1301 return getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
1304 } else { // directory
1305 auto *DE = cast<RedirectingDirectoryEntry>(E);
1306 return Status::copyWithNewName(DE->getStatus(), Path.str());
1310 ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path) {
1311 ErrorOr<Entry *> Result = lookupPath(Path);
1313 return Result.getError();
1314 return status(Path, *Result);
1318 /// Provide a file wrapper with an overriden status.
1319 class FileWithFixedStatus : public File {
1320 std::unique_ptr<File> InnerFile;
1324 FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S)
1325 : InnerFile(std::move(InnerFile)), S(S) {}
1327 ErrorOr<Status> status() override { return S; }
1328 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
1329 getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
1330 bool IsVolatile) override {
1331 return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
1334 std::error_code close() override { return InnerFile->close(); }
1336 } // end anonymous namespace
1338 ErrorOr<std::unique_ptr<File>>
1339 RedirectingFileSystem::openFileForRead(const Twine &Path) {
1340 ErrorOr<Entry *> E = lookupPath(Path);
1342 return E.getError();
1344 auto *F = dyn_cast<RedirectingFileEntry>(*E);
1345 if (!F) // FIXME: errc::not_a_file?
1346 return make_error_code(llvm::errc::invalid_argument);
1348 auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath());
1352 auto ExternalStatus = (*Result)->status();
1353 if (!ExternalStatus)
1354 return ExternalStatus.getError();
1356 // FIXME: Update the status with the name and VFSMapped.
1357 Status S = getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
1359 return std::unique_ptr<File>(
1360 llvm::make_unique<FileWithFixedStatus>(std::move(*Result), S));
1363 IntrusiveRefCntPtr<FileSystem>
1364 vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
1365 SourceMgr::DiagHandlerTy DiagHandler, void *DiagContext,
1366 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1367 return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
1368 DiagContext, ExternalFS);
1371 UniqueID vfs::getNextVirtualUniqueID() {
1372 static std::atomic<unsigned> UID;
1373 unsigned ID = ++UID;
1374 // The following assumes that uint64_t max will never collide with a real
1375 // dev_t value from the OS.
1376 return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
1380 static bool pathHasTraversal(StringRef Path) {
1381 using namespace llvm::sys;
1382 for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
1383 if (Comp == "." || Comp == "..")
1389 void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
1390 assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
1391 assert(sys::path::is_absolute(RealPath) && "real path not absolute");
1392 assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
1393 Mappings.emplace_back(VirtualPath, RealPath);
1398 llvm::raw_ostream &OS;
1399 SmallVector<StringRef, 16> DirStack;
1400 inline unsigned getDirIndent() { return 4 * DirStack.size(); }
1401 inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
1402 bool containedIn(StringRef Parent, StringRef Path);
1403 StringRef containedPart(StringRef Parent, StringRef Path);
1404 void startDirectory(StringRef Path);
1405 void endDirectory();
1406 void writeEntry(StringRef VPath, StringRef RPath);
1409 JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
1410 void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> IsCaseSensitive);
1414 bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
1415 using namespace llvm::sys;
1416 // Compare each path component.
1417 auto IParent = path::begin(Parent), EParent = path::end(Parent);
1418 for (auto IChild = path::begin(Path), EChild = path::end(Path);
1419 IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
1420 if (*IParent != *IChild)
1423 // Have we exhausted the parent path?
1424 return IParent == EParent;
1427 StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
1428 assert(!Parent.empty());
1429 assert(containedIn(Parent, Path));
1430 return Path.slice(Parent.size() + 1, StringRef::npos);
1433 void JSONWriter::startDirectory(StringRef Path) {
1435 DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
1436 DirStack.push_back(Path);
1437 unsigned Indent = getDirIndent();
1438 OS.indent(Indent) << "{\n";
1439 OS.indent(Indent + 2) << "'type': 'directory',\n";
1440 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
1441 OS.indent(Indent + 2) << "'contents': [\n";
1444 void JSONWriter::endDirectory() {
1445 unsigned Indent = getDirIndent();
1446 OS.indent(Indent + 2) << "]\n";
1447 OS.indent(Indent) << "}";
1449 DirStack.pop_back();
1452 void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
1453 unsigned Indent = getFileIndent();
1454 OS.indent(Indent) << "{\n";
1455 OS.indent(Indent + 2) << "'type': 'file',\n";
1456 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
1457 OS.indent(Indent + 2) << "'external-contents': \""
1458 << llvm::yaml::escape(RPath) << "\"\n";
1459 OS.indent(Indent) << "}";
1462 void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
1463 Optional<bool> IsCaseSensitive) {
1464 using namespace llvm::sys;
1468 if (IsCaseSensitive.hasValue())
1469 OS << " 'case-sensitive': '"
1470 << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
1471 OS << " 'roots': [\n";
1473 if (!Entries.empty()) {
1474 const YAMLVFSEntry &Entry = Entries.front();
1475 startDirectory(path::parent_path(Entry.VPath));
1476 writeEntry(path::filename(Entry.VPath), Entry.RPath);
1478 for (const auto &Entry : Entries.slice(1)) {
1479 StringRef Dir = path::parent_path(Entry.VPath);
1480 if (Dir == DirStack.back())
1483 while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
1488 startDirectory(Dir);
1490 writeEntry(path::filename(Entry.VPath), Entry.RPath);
1493 while (!DirStack.empty()) {
1504 void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
1505 std::sort(Mappings.begin(), Mappings.end(),
1506 [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
1507 return LHS.VPath < RHS.VPath;
1510 JSONWriter(OS).write(Mappings, IsCaseSensitive);
1513 VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(
1514 const Twine &_Path, RedirectingFileSystem &FS,
1515 RedirectingDirectoryEntry::iterator Begin,
1516 RedirectingDirectoryEntry::iterator End, std::error_code &EC)
1517 : Dir(_Path.str()), FS(FS), Current(Begin), End(End) {
1518 if (Current != End) {
1519 SmallString<128> PathStr(Dir);
1520 llvm::sys::path::append(PathStr, (*Current)->getName());
1521 llvm::ErrorOr<vfs::Status> S = FS.status(PathStr);
1529 std::error_code VFSFromYamlDirIterImpl::increment() {
1530 assert(Current != End && "cannot iterate past end");
1531 if (++Current != End) {
1532 SmallString<128> PathStr(Dir);
1533 llvm::sys::path::append(PathStr, (*Current)->getName());
1534 llvm::ErrorOr<vfs::Status> S = FS.status(PathStr);
1536 return S.getError();
1539 CurrentEntry = Status();
1541 return std::error_code();
1544 vfs::recursive_directory_iterator::recursive_directory_iterator(FileSystem &FS_,
1546 std::error_code &EC)
1548 directory_iterator I = FS->dir_begin(Path, EC);
1549 if (!EC && I != directory_iterator()) {
1550 State = std::make_shared<IterState>();
1555 vfs::recursive_directory_iterator &
1556 recursive_directory_iterator::increment(std::error_code &EC) {
1557 assert(FS && State && !State->empty() && "incrementing past end");
1558 assert(State->top()->isStatusKnown() && "non-canonical end iterator");
1559 vfs::directory_iterator End;
1560 if (State->top()->isDirectory()) {
1561 vfs::directory_iterator I = FS->dir_begin(State->top()->getName(), EC);
1570 while (!State->empty() && State->top().increment(EC) == End)
1574 State.reset(); // end iterator