xref: /llvm-project/llvm/lib/Support/VirtualFileSystem.cpp (revision 7a99727c62ac457ad122aa6ede3500b75184426c)
1 //===- VirtualFileSystem.cpp - Virtual File System Layer ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the VirtualFileSystem interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/VirtualFileSystem.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/IntrusiveRefCntPtr.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSet.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/ADT/iterator_range.h"
27 #include "llvm/Config/llvm-config.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Chrono.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/Errc.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/ErrorOr.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Path.h"
38 #include "llvm/Support/Process.h"
39 #include "llvm/Support/SMLoc.h"
40 #include "llvm/Support/SourceMgr.h"
41 #include "llvm/Support/YAMLParser.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include <algorithm>
44 #include <atomic>
45 #include <cassert>
46 #include <cstdint>
47 #include <iterator>
48 #include <limits>
49 #include <map>
50 #include <memory>
51 #include <mutex>
52 #include <string>
53 #include <system_error>
54 #include <utility>
55 #include <vector>
56 
57 using namespace llvm;
58 using namespace llvm::vfs;
59 
60 using llvm::sys::fs::file_status;
61 using llvm::sys::fs::file_type;
62 using llvm::sys::fs::perms;
63 using llvm::sys::fs::UniqueID;
64 
65 Status::Status(const file_status &Status)
66     : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
67       User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
68       Type(Status.type()), Perms(Status.permissions()) {}
69 
70 Status::Status(StringRef Name, UniqueID UID, sys::TimePoint<> MTime,
71                uint32_t User, uint32_t Group, uint64_t Size, file_type Type,
72                perms Perms)
73     : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
74       Type(Type), Perms(Perms) {}
75 
76 Status Status::copyWithNewName(const Status &In, StringRef NewName) {
77   return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
78                 In.getUser(), In.getGroup(), In.getSize(), In.getType(),
79                 In.getPermissions());
80 }
81 
82 Status Status::copyWithNewName(const file_status &In, StringRef NewName) {
83   return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
84                 In.getUser(), In.getGroup(), In.getSize(), In.type(),
85                 In.permissions());
86 }
87 
88 bool Status::equivalent(const Status &Other) const {
89   assert(isStatusKnown() && Other.isStatusKnown());
90   return getUniqueID() == Other.getUniqueID();
91 }
92 
93 bool Status::isDirectory() const { return Type == file_type::directory_file; }
94 
95 bool Status::isRegularFile() const { return Type == file_type::regular_file; }
96 
97 bool Status::isOther() const {
98   return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
99 }
100 
101 bool Status::isSymlink() const { return Type == file_type::symlink_file; }
102 
103 bool Status::isStatusKnown() const { return Type != file_type::status_error; }
104 
105 bool Status::exists() const {
106   return isStatusKnown() && Type != file_type::file_not_found;
107 }
108 
109 File::~File() = default;
110 
111 FileSystem::~FileSystem() = default;
112 
113 ErrorOr<std::unique_ptr<MemoryBuffer>>
114 FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
115                              bool RequiresNullTerminator, bool IsVolatile) {
116   auto F = openFileForRead(Name);
117   if (!F)
118     return F.getError();
119 
120   return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
121 }
122 
123 std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
124   if (llvm::sys::path::is_absolute(Path))
125     return {};
126 
127   auto WorkingDir = getCurrentWorkingDirectory();
128   if (!WorkingDir)
129     return WorkingDir.getError();
130 
131   return llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
132 }
133 
134 std::error_code FileSystem::getRealPath(const Twine &Path,
135                                         SmallVectorImpl<char> &Output) const {
136   return errc::operation_not_permitted;
137 }
138 
139 std::error_code FileSystem::isLocal(const Twine &Path, bool &Result) {
140   return errc::operation_not_permitted;
141 }
142 
143 bool FileSystem::exists(const Twine &Path) {
144   auto Status = status(Path);
145   return Status && Status->exists();
146 }
147 
148 #ifndef NDEBUG
149 static bool isTraversalComponent(StringRef Component) {
150   return Component.equals("..") || Component.equals(".");
151 }
152 
153 static bool pathHasTraversal(StringRef Path) {
154   using namespace llvm::sys;
155 
156   for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
157     if (isTraversalComponent(Comp))
158       return true;
159   return false;
160 }
161 #endif
162 
163 //===-----------------------------------------------------------------------===/
164 // RealFileSystem implementation
165 //===-----------------------------------------------------------------------===/
166 
167 namespace {
168 
169 /// Wrapper around a raw file descriptor.
170 class RealFile : public File {
171   friend class RealFileSystem;
172 
173   int FD;
174   Status S;
175   std::string RealName;
176 
177   RealFile(int FD, StringRef NewName, StringRef NewRealPathName)
178       : FD(FD), S(NewName, {}, {}, {}, {}, {},
179                   llvm::sys::fs::file_type::status_error, {}),
180         RealName(NewRealPathName.str()) {
181     assert(FD >= 0 && "Invalid or inactive file descriptor");
182   }
183 
184 public:
185   ~RealFile() override;
186 
187   ErrorOr<Status> status() override;
188   ErrorOr<std::string> getName() override;
189   ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name,
190                                                    int64_t FileSize,
191                                                    bool RequiresNullTerminator,
192                                                    bool IsVolatile) override;
193   std::error_code close() override;
194 };
195 
196 } // namespace
197 
198 RealFile::~RealFile() { close(); }
199 
200 ErrorOr<Status> RealFile::status() {
201   assert(FD != -1 && "cannot stat closed file");
202   if (!S.isStatusKnown()) {
203     file_status RealStatus;
204     if (std::error_code EC = sys::fs::status(FD, RealStatus))
205       return EC;
206     S = Status::copyWithNewName(RealStatus, S.getName());
207   }
208   return S;
209 }
210 
211 ErrorOr<std::string> RealFile::getName() {
212   return RealName.empty() ? S.getName().str() : RealName;
213 }
214 
215 ErrorOr<std::unique_ptr<MemoryBuffer>>
216 RealFile::getBuffer(const Twine &Name, int64_t FileSize,
217                     bool RequiresNullTerminator, bool IsVolatile) {
218   assert(FD != -1 && "cannot get buffer for closed file");
219   return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
220                                    IsVolatile);
221 }
222 
223 std::error_code RealFile::close() {
224   std::error_code EC = sys::Process::SafelyCloseFileDescriptor(FD);
225   FD = -1;
226   return EC;
227 }
228 
229 namespace {
230 
231 /// A file system according to your operating system.
232 /// This may be linked to the process's working directory, or maintain its own.
233 ///
234 /// Currently, its own working directory is emulated by storing the path and
235 /// sending absolute paths to llvm::sys::fs:: functions.
236 /// A more principled approach would be to push this down a level, modelling
237 /// the working dir as an llvm::sys::fs::WorkingDir or similar.
238 /// This would enable the use of openat()-style functions on some platforms.
239 class RealFileSystem : public FileSystem {
240 public:
241   explicit RealFileSystem(bool LinkCWDToProcess) {
242     if (!LinkCWDToProcess) {
243       SmallString<128> PWD, RealPWD;
244       if (llvm::sys::fs::current_path(PWD))
245         return; // Awful, but nothing to do here.
246       if (llvm::sys::fs::real_path(PWD, RealPWD))
247         WD = {PWD, PWD};
248       else
249         WD = {PWD, RealPWD};
250     }
251   }
252 
253   ErrorOr<Status> status(const Twine &Path) override;
254   ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
255   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
256 
257   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
258   std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
259   std::error_code isLocal(const Twine &Path, bool &Result) override;
260   std::error_code getRealPath(const Twine &Path,
261                               SmallVectorImpl<char> &Output) const override;
262 
263 private:
264   // If this FS has its own working dir, use it to make Path absolute.
265   // The returned twine is safe to use as long as both Storage and Path live.
266   Twine adjustPath(const Twine &Path, SmallVectorImpl<char> &Storage) const {
267     if (!WD)
268       return Path;
269     Path.toVector(Storage);
270     sys::fs::make_absolute(WD->Resolved, Storage);
271     return Storage;
272   }
273 
274   struct WorkingDirectory {
275     // The current working directory, without symlinks resolved. (echo $PWD).
276     SmallString<128> Specified;
277     // The current working directory, with links resolved. (readlink .).
278     SmallString<128> Resolved;
279   };
280   Optional<WorkingDirectory> WD;
281 };
282 
283 } // namespace
284 
285 ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
286   SmallString<256> Storage;
287   sys::fs::file_status RealStatus;
288   if (std::error_code EC =
289           sys::fs::status(adjustPath(Path, Storage), RealStatus))
290     return EC;
291   return Status::copyWithNewName(RealStatus, Path.str());
292 }
293 
294 ErrorOr<std::unique_ptr<File>>
295 RealFileSystem::openFileForRead(const Twine &Name) {
296   int FD;
297   SmallString<256> RealName, Storage;
298   if (std::error_code EC = sys::fs::openFileForRead(
299           adjustPath(Name, Storage), FD, sys::fs::OF_None, &RealName))
300     return EC;
301   return std::unique_ptr<File>(new RealFile(FD, Name.str(), RealName.str()));
302 }
303 
304 llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
305   if (WD)
306     return WD->Specified.str();
307 
308   SmallString<128> Dir;
309   if (std::error_code EC = llvm::sys::fs::current_path(Dir))
310     return EC;
311   return Dir.str();
312 }
313 
314 std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
315   if (!WD)
316     return llvm::sys::fs::set_current_path(Path);
317 
318   SmallString<128> Absolute, Resolved, Storage;
319   adjustPath(Path, Storage).toVector(Absolute);
320   bool IsDir;
321   if (auto Err = llvm::sys::fs::is_directory(Absolute, IsDir))
322     return Err;
323   if (!IsDir)
324     return std::make_error_code(std::errc::not_a_directory);
325   if (auto Err = llvm::sys::fs::real_path(Absolute, Resolved))
326     return Err;
327   WD = {Absolute, Resolved};
328   return std::error_code();
329 }
330 
331 std::error_code RealFileSystem::isLocal(const Twine &Path, bool &Result) {
332   SmallString<256> Storage;
333   return llvm::sys::fs::is_local(adjustPath(Path, Storage), Result);
334 }
335 
336 std::error_code
337 RealFileSystem::getRealPath(const Twine &Path,
338                             SmallVectorImpl<char> &Output) const {
339   SmallString<256> Storage;
340   return llvm::sys::fs::real_path(adjustPath(Path, Storage), Output);
341 }
342 
343 IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
344   static IntrusiveRefCntPtr<FileSystem> FS(new RealFileSystem(true));
345   return FS;
346 }
347 
348 std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() {
349   return llvm::make_unique<RealFileSystem>(false);
350 }
351 
352 namespace {
353 
354 class RealFSDirIter : public llvm::vfs::detail::DirIterImpl {
355   llvm::sys::fs::directory_iterator Iter;
356 
357 public:
358   RealFSDirIter(const Twine &Path, std::error_code &EC) : Iter(Path, EC) {
359     if (Iter != llvm::sys::fs::directory_iterator())
360       CurrentEntry = directory_entry(Iter->path(), Iter->type());
361   }
362 
363   std::error_code increment() override {
364     std::error_code EC;
365     Iter.increment(EC);
366     CurrentEntry = (Iter == llvm::sys::fs::directory_iterator())
367                        ? directory_entry()
368                        : directory_entry(Iter->path(), Iter->type());
369     return EC;
370   }
371 };
372 
373 } // namespace
374 
375 directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
376                                              std::error_code &EC) {
377   SmallString<128> Storage;
378   return directory_iterator(
379       std::make_shared<RealFSDirIter>(adjustPath(Dir, Storage), EC));
380 }
381 
382 //===-----------------------------------------------------------------------===/
383 // OverlayFileSystem implementation
384 //===-----------------------------------------------------------------------===/
385 
386 OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
387   FSList.push_back(std::move(BaseFS));
388 }
389 
390 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
391   FSList.push_back(FS);
392   // Synchronize added file systems by duplicating the working directory from
393   // the first one in the list.
394   FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
395 }
396 
397 ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
398   // FIXME: handle symlinks that cross file systems
399   for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
400     ErrorOr<Status> Status = (*I)->status(Path);
401     if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
402       return Status;
403   }
404   return make_error_code(llvm::errc::no_such_file_or_directory);
405 }
406 
407 ErrorOr<std::unique_ptr<File>>
408 OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
409   // FIXME: handle symlinks that cross file systems
410   for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
411     auto Result = (*I)->openFileForRead(Path);
412     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
413       return Result;
414   }
415   return make_error_code(llvm::errc::no_such_file_or_directory);
416 }
417 
418 llvm::ErrorOr<std::string>
419 OverlayFileSystem::getCurrentWorkingDirectory() const {
420   // All file systems are synchronized, just take the first working directory.
421   return FSList.front()->getCurrentWorkingDirectory();
422 }
423 
424 std::error_code
425 OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
426   for (auto &FS : FSList)
427     if (std::error_code EC = FS->setCurrentWorkingDirectory(Path))
428       return EC;
429   return {};
430 }
431 
432 std::error_code OverlayFileSystem::isLocal(const Twine &Path, bool &Result) {
433   for (auto &FS : FSList)
434     if (FS->exists(Path))
435       return FS->isLocal(Path, Result);
436   return errc::no_such_file_or_directory;
437 }
438 
439 std::error_code
440 OverlayFileSystem::getRealPath(const Twine &Path,
441                                SmallVectorImpl<char> &Output) const {
442   for (auto &FS : FSList)
443     if (FS->exists(Path))
444       return FS->getRealPath(Path, Output);
445   return errc::no_such_file_or_directory;
446 }
447 
448 llvm::vfs::detail::DirIterImpl::~DirIterImpl() = default;
449 
450 namespace {
451 
452 class OverlayFSDirIterImpl : public llvm::vfs::detail::DirIterImpl {
453   OverlayFileSystem &Overlays;
454   std::string Path;
455   OverlayFileSystem::iterator CurrentFS;
456   directory_iterator CurrentDirIter;
457   llvm::StringSet<> SeenNames;
458 
459   std::error_code incrementFS() {
460     assert(CurrentFS != Overlays.overlays_end() && "incrementing past end");
461     ++CurrentFS;
462     for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) {
463       std::error_code EC;
464       CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
465       if (EC && EC != errc::no_such_file_or_directory)
466         return EC;
467       if (CurrentDirIter != directory_iterator())
468         break; // found
469     }
470     return {};
471   }
472 
473   std::error_code incrementDirIter(bool IsFirstTime) {
474     assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
475            "incrementing past end");
476     std::error_code EC;
477     if (!IsFirstTime)
478       CurrentDirIter.increment(EC);
479     if (!EC && CurrentDirIter == directory_iterator())
480       EC = incrementFS();
481     return EC;
482   }
483 
484   std::error_code incrementImpl(bool IsFirstTime) {
485     while (true) {
486       std::error_code EC = incrementDirIter(IsFirstTime);
487       if (EC || CurrentDirIter == directory_iterator()) {
488         CurrentEntry = directory_entry();
489         return EC;
490       }
491       CurrentEntry = *CurrentDirIter;
492       StringRef Name = llvm::sys::path::filename(CurrentEntry.path());
493       if (SeenNames.insert(Name).second)
494         return EC; // name not seen before
495     }
496     llvm_unreachable("returned above");
497   }
498 
499 public:
500   OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS,
501                        std::error_code &EC)
502       : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) {
503     CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
504     EC = incrementImpl(true);
505   }
506 
507   std::error_code increment() override { return incrementImpl(false); }
508 };
509 
510 } // namespace
511 
512 directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
513                                                 std::error_code &EC) {
514   return directory_iterator(
515       std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC));
516 }
517 
518 void ProxyFileSystem::anchor() {}
519 
520 namespace llvm {
521 namespace vfs {
522 
523 namespace detail {
524 
525 enum InMemoryNodeKind { IME_File, IME_Directory, IME_HardLink };
526 
527 /// The in memory file system is a tree of Nodes. Every node can either be a
528 /// file , hardlink or a directory.
529 class InMemoryNode {
530   InMemoryNodeKind Kind;
531   std::string FileName;
532 
533 public:
534   InMemoryNode(llvm::StringRef FileName, InMemoryNodeKind Kind)
535       : Kind(Kind), FileName(llvm::sys::path::filename(FileName)) {}
536   virtual ~InMemoryNode() = default;
537 
538   /// Get the filename of this node (the name without the directory part).
539   StringRef getFileName() const { return FileName; }
540   InMemoryNodeKind getKind() const { return Kind; }
541   virtual std::string toString(unsigned Indent) const = 0;
542 };
543 
544 class InMemoryFile : public InMemoryNode {
545   Status Stat;
546   std::unique_ptr<llvm::MemoryBuffer> Buffer;
547 
548 public:
549   InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer)
550       : InMemoryNode(Stat.getName(), IME_File), Stat(std::move(Stat)),
551         Buffer(std::move(Buffer)) {}
552 
553   /// Return the \p Status for this node. \p RequestedName should be the name
554   /// through which the caller referred to this node. It will override
555   /// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
556   Status getStatus(StringRef RequestedName) const {
557     return Status::copyWithNewName(Stat, RequestedName);
558   }
559   llvm::MemoryBuffer *getBuffer() const { return Buffer.get(); }
560 
561   std::string toString(unsigned Indent) const override {
562     return (std::string(Indent, ' ') + Stat.getName() + "\n").str();
563   }
564 
565   static bool classof(const InMemoryNode *N) {
566     return N->getKind() == IME_File;
567   }
568 };
569 
570 namespace {
571 
572 class InMemoryHardLink : public InMemoryNode {
573   const InMemoryFile &ResolvedFile;
574 
575 public:
576   InMemoryHardLink(StringRef Path, const InMemoryFile &ResolvedFile)
577       : InMemoryNode(Path, IME_HardLink), ResolvedFile(ResolvedFile) {}
578   const InMemoryFile &getResolvedFile() const { return ResolvedFile; }
579 
580   std::string toString(unsigned Indent) const override {
581     return std::string(Indent, ' ') + "HardLink to -> " +
582            ResolvedFile.toString(0);
583   }
584 
585   static bool classof(const InMemoryNode *N) {
586     return N->getKind() == IME_HardLink;
587   }
588 };
589 
590 /// Adapt a InMemoryFile for VFS' File interface.  The goal is to make
591 /// \p InMemoryFileAdaptor mimic as much as possible the behavior of
592 /// \p RealFile.
593 class InMemoryFileAdaptor : public File {
594   const InMemoryFile &Node;
595   /// The name to use when returning a Status for this file.
596   std::string RequestedName;
597 
598 public:
599   explicit InMemoryFileAdaptor(const InMemoryFile &Node,
600                                std::string RequestedName)
601       : Node(Node), RequestedName(std::move(RequestedName)) {}
602 
603   llvm::ErrorOr<Status> status() override {
604     return Node.getStatus(RequestedName);
605   }
606 
607   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
608   getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
609             bool IsVolatile) override {
610     llvm::MemoryBuffer *Buf = Node.getBuffer();
611     return llvm::MemoryBuffer::getMemBuffer(
612         Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator);
613   }
614 
615   std::error_code close() override { return {}; }
616 };
617 } // namespace
618 
619 class InMemoryDirectory : public InMemoryNode {
620   Status Stat;
621   llvm::StringMap<std::unique_ptr<InMemoryNode>> Entries;
622 
623 public:
624   InMemoryDirectory(Status Stat)
625       : InMemoryNode(Stat.getName(), IME_Directory), Stat(std::move(Stat)) {}
626 
627   /// Return the \p Status for this node. \p RequestedName should be the name
628   /// through which the caller referred to this node. It will override
629   /// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
630   Status getStatus(StringRef RequestedName) const {
631     return Status::copyWithNewName(Stat, RequestedName);
632   }
633   InMemoryNode *getChild(StringRef Name) {
634     auto I = Entries.find(Name);
635     if (I != Entries.end())
636       return I->second.get();
637     return nullptr;
638   }
639 
640   InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) {
641     return Entries.insert(make_pair(Name, std::move(Child)))
642         .first->second.get();
643   }
644 
645   using const_iterator = decltype(Entries)::const_iterator;
646 
647   const_iterator begin() const { return Entries.begin(); }
648   const_iterator end() const { return Entries.end(); }
649 
650   std::string toString(unsigned Indent) const override {
651     std::string Result =
652         (std::string(Indent, ' ') + Stat.getName() + "\n").str();
653     for (const auto &Entry : Entries)
654       Result += Entry.second->toString(Indent + 2);
655     return Result;
656   }
657 
658   static bool classof(const InMemoryNode *N) {
659     return N->getKind() == IME_Directory;
660   }
661 };
662 
663 namespace {
664 Status getNodeStatus(const InMemoryNode *Node, StringRef RequestedName) {
665   if (auto Dir = dyn_cast<detail::InMemoryDirectory>(Node))
666     return Dir->getStatus(RequestedName);
667   if (auto File = dyn_cast<detail::InMemoryFile>(Node))
668     return File->getStatus(RequestedName);
669   if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node))
670     return Link->getResolvedFile().getStatus(RequestedName);
671   llvm_unreachable("Unknown node type");
672 }
673 } // namespace
674 } // namespace detail
675 
676 InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths)
677     : Root(new detail::InMemoryDirectory(
678           Status("", getNextVirtualUniqueID(), llvm::sys::TimePoint<>(), 0, 0,
679                  0, llvm::sys::fs::file_type::directory_file,
680                  llvm::sys::fs::perms::all_all))),
681       UseNormalizedPaths(UseNormalizedPaths) {}
682 
683 InMemoryFileSystem::~InMemoryFileSystem() = default;
684 
685 std::string InMemoryFileSystem::toString() const {
686   return Root->toString(/*Indent=*/0);
687 }
688 
689 bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
690                                  std::unique_ptr<llvm::MemoryBuffer> Buffer,
691                                  Optional<uint32_t> User,
692                                  Optional<uint32_t> Group,
693                                  Optional<llvm::sys::fs::file_type> Type,
694                                  Optional<llvm::sys::fs::perms> Perms,
695                                  const detail::InMemoryFile *HardLinkTarget) {
696   SmallString<128> Path;
697   P.toVector(Path);
698 
699   // Fix up relative paths. This just prepends the current working directory.
700   std::error_code EC = makeAbsolute(Path);
701   assert(!EC);
702   (void)EC;
703 
704   if (useNormalizedPaths())
705     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
706 
707   if (Path.empty())
708     return false;
709 
710   detail::InMemoryDirectory *Dir = Root.get();
711   auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
712   const auto ResolvedUser = User.getValueOr(0);
713   const auto ResolvedGroup = Group.getValueOr(0);
714   const auto ResolvedType = Type.getValueOr(sys::fs::file_type::regular_file);
715   const auto ResolvedPerms = Perms.getValueOr(sys::fs::all_all);
716   assert(!(HardLinkTarget && Buffer) && "HardLink cannot have a buffer");
717   // Any intermediate directories we create should be accessible by
718   // the owner, even if Perms says otherwise for the final path.
719   const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all;
720   while (true) {
721     StringRef Name = *I;
722     detail::InMemoryNode *Node = Dir->getChild(Name);
723     ++I;
724     if (!Node) {
725       if (I == E) {
726         // End of the path.
727         std::unique_ptr<detail::InMemoryNode> Child;
728         if (HardLinkTarget)
729           Child.reset(new detail::InMemoryHardLink(P.str(), *HardLinkTarget));
730         else {
731           // Create a new file or directory.
732           Status Stat(P.str(), getNextVirtualUniqueID(),
733                       llvm::sys::toTimePoint(ModificationTime), ResolvedUser,
734                       ResolvedGroup, Buffer->getBufferSize(), ResolvedType,
735                       ResolvedPerms);
736           if (ResolvedType == sys::fs::file_type::directory_file) {
737             Child.reset(new detail::InMemoryDirectory(std::move(Stat)));
738           } else {
739             Child.reset(
740                 new detail::InMemoryFile(std::move(Stat), std::move(Buffer)));
741           }
742         }
743         Dir->addChild(Name, std::move(Child));
744         return true;
745       }
746 
747       // Create a new directory. Use the path up to here.
748       Status Stat(
749           StringRef(Path.str().begin(), Name.end() - Path.str().begin()),
750           getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime),
751           ResolvedUser, ResolvedGroup, 0, sys::fs::file_type::directory_file,
752           NewDirectoryPerms);
753       Dir = cast<detail::InMemoryDirectory>(Dir->addChild(
754           Name, llvm::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
755       continue;
756     }
757 
758     if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
759       Dir = NewDir;
760     } else {
761       assert((isa<detail::InMemoryFile>(Node) ||
762               isa<detail::InMemoryHardLink>(Node)) &&
763              "Must be either file, hardlink or directory!");
764 
765       // Trying to insert a directory in place of a file.
766       if (I != E)
767         return false;
768 
769       // Return false only if the new file is different from the existing one.
770       if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node)) {
771         return Link->getResolvedFile().getBuffer()->getBuffer() ==
772                Buffer->getBuffer();
773       }
774       return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() ==
775              Buffer->getBuffer();
776     }
777   }
778 }
779 
780 bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
781                                  std::unique_ptr<llvm::MemoryBuffer> Buffer,
782                                  Optional<uint32_t> User,
783                                  Optional<uint32_t> Group,
784                                  Optional<llvm::sys::fs::file_type> Type,
785                                  Optional<llvm::sys::fs::perms> Perms) {
786   return addFile(P, ModificationTime, std::move(Buffer), User, Group, Type,
787                  Perms, /*HardLinkTarget=*/nullptr);
788 }
789 
790 bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime,
791                                       llvm::MemoryBuffer *Buffer,
792                                       Optional<uint32_t> User,
793                                       Optional<uint32_t> Group,
794                                       Optional<llvm::sys::fs::file_type> Type,
795                                       Optional<llvm::sys::fs::perms> Perms) {
796   return addFile(P, ModificationTime,
797                  llvm::MemoryBuffer::getMemBuffer(
798                      Buffer->getBuffer(), Buffer->getBufferIdentifier()),
799                  std::move(User), std::move(Group), std::move(Type),
800                  std::move(Perms));
801 }
802 
803 static ErrorOr<const detail::InMemoryNode *>
804 lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir,
805                    const Twine &P) {
806   SmallString<128> Path;
807   P.toVector(Path);
808 
809   // Fix up relative paths. This just prepends the current working directory.
810   std::error_code EC = FS.makeAbsolute(Path);
811   assert(!EC);
812   (void)EC;
813 
814   if (FS.useNormalizedPaths())
815     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
816 
817   if (Path.empty())
818     return Dir;
819 
820   auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
821   while (true) {
822     detail::InMemoryNode *Node = Dir->getChild(*I);
823     ++I;
824     if (!Node)
825       return errc::no_such_file_or_directory;
826 
827     // Return the file if it's at the end of the path.
828     if (auto File = dyn_cast<detail::InMemoryFile>(Node)) {
829       if (I == E)
830         return File;
831       return errc::no_such_file_or_directory;
832     }
833 
834     // If Node is HardLink then return the resolved file.
835     if (auto File = dyn_cast<detail::InMemoryHardLink>(Node)) {
836       if (I == E)
837         return &File->getResolvedFile();
838       return errc::no_such_file_or_directory;
839     }
840     // Traverse directories.
841     Dir = cast<detail::InMemoryDirectory>(Node);
842     if (I == E)
843       return Dir;
844   }
845 }
846 
847 bool InMemoryFileSystem::addHardLink(const Twine &FromPath,
848                                      const Twine &ToPath) {
849   auto FromNode = lookupInMemoryNode(*this, Root.get(), FromPath);
850   auto ToNode = lookupInMemoryNode(*this, Root.get(), ToPath);
851   // FromPath must not have been added before. ToPath must have been added
852   // before. Resolved ToPath must be a File.
853   if (!ToNode || FromNode || !isa<detail::InMemoryFile>(*ToNode))
854     return false;
855   return this->addFile(FromPath, 0, nullptr, None, None, None, None,
856                        cast<detail::InMemoryFile>(*ToNode));
857 }
858 
859 llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) {
860   auto Node = lookupInMemoryNode(*this, Root.get(), Path);
861   if (Node)
862     return detail::getNodeStatus(*Node, Path.str());
863   return Node.getError();
864 }
865 
866 llvm::ErrorOr<std::unique_ptr<File>>
867 InMemoryFileSystem::openFileForRead(const Twine &Path) {
868   auto Node = lookupInMemoryNode(*this, Root.get(), Path);
869   if (!Node)
870     return Node.getError();
871 
872   // When we have a file provide a heap-allocated wrapper for the memory buffer
873   // to match the ownership semantics for File.
874   if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
875     return std::unique_ptr<File>(
876         new detail::InMemoryFileAdaptor(*F, Path.str()));
877 
878   // FIXME: errc::not_a_file?
879   return make_error_code(llvm::errc::invalid_argument);
880 }
881 
882 namespace {
883 
884 /// Adaptor from InMemoryDir::iterator to directory_iterator.
885 class InMemoryDirIterator : public llvm::vfs::detail::DirIterImpl {
886   detail::InMemoryDirectory::const_iterator I;
887   detail::InMemoryDirectory::const_iterator E;
888   std::string RequestedDirName;
889 
890   void setCurrentEntry() {
891     if (I != E) {
892       SmallString<256> Path(RequestedDirName);
893       llvm::sys::path::append(Path, I->second->getFileName());
894       sys::fs::file_type Type;
895       switch (I->second->getKind()) {
896       case detail::IME_File:
897       case detail::IME_HardLink:
898         Type = sys::fs::file_type::regular_file;
899         break;
900       case detail::IME_Directory:
901         Type = sys::fs::file_type::directory_file;
902         break;
903       }
904       CurrentEntry = directory_entry(Path.str(), Type);
905     } else {
906       // When we're at the end, make CurrentEntry invalid and DirIterImpl will
907       // do the rest.
908       CurrentEntry = directory_entry();
909     }
910   }
911 
912 public:
913   InMemoryDirIterator() = default;
914 
915   explicit InMemoryDirIterator(const detail::InMemoryDirectory &Dir,
916                                std::string RequestedDirName)
917       : I(Dir.begin()), E(Dir.end()),
918         RequestedDirName(std::move(RequestedDirName)) {
919     setCurrentEntry();
920   }
921 
922   std::error_code increment() override {
923     ++I;
924     setCurrentEntry();
925     return {};
926   }
927 };
928 
929 } // namespace
930 
931 directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
932                                                  std::error_code &EC) {
933   auto Node = lookupInMemoryNode(*this, Root.get(), Dir);
934   if (!Node) {
935     EC = Node.getError();
936     return directory_iterator(std::make_shared<InMemoryDirIterator>());
937   }
938 
939   if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node))
940     return directory_iterator(
941         std::make_shared<InMemoryDirIterator>(*DirNode, Dir.str()));
942 
943   EC = make_error_code(llvm::errc::not_a_directory);
944   return directory_iterator(std::make_shared<InMemoryDirIterator>());
945 }
946 
947 std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
948   SmallString<128> Path;
949   P.toVector(Path);
950 
951   // Fix up relative paths. This just prepends the current working directory.
952   std::error_code EC = makeAbsolute(Path);
953   assert(!EC);
954   (void)EC;
955 
956   if (useNormalizedPaths())
957     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
958 
959   if (!Path.empty())
960     WorkingDirectory = Path.str();
961   return {};
962 }
963 
964 std::error_code
965 InMemoryFileSystem::getRealPath(const Twine &Path,
966                                 SmallVectorImpl<char> &Output) const {
967   auto CWD = getCurrentWorkingDirectory();
968   if (!CWD || CWD->empty())
969     return errc::operation_not_permitted;
970   Path.toVector(Output);
971   if (auto EC = makeAbsolute(Output))
972     return EC;
973   llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
974   return {};
975 }
976 
977 std::error_code InMemoryFileSystem::isLocal(const Twine &Path, bool &Result) {
978   Result = false;
979   return {};
980 }
981 
982 } // namespace vfs
983 } // namespace llvm
984 
985 //===-----------------------------------------------------------------------===/
986 // RedirectingFileSystem implementation
987 //===-----------------------------------------------------------------------===/
988 
989 namespace {
990 
991 enum EntryKind { EK_Directory, EK_File };
992 
993 /// A single file or directory in the VFS.
994 class Entry {
995   EntryKind Kind;
996   std::string Name;
997 
998 public:
999   Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
1000   virtual ~Entry() = default;
1001 
1002   StringRef getName() const { return Name; }
1003   EntryKind getKind() const { return Kind; }
1004 };
1005 
1006 class RedirectingDirectoryEntry : public Entry {
1007   std::vector<std::unique_ptr<Entry>> Contents;
1008   Status S;
1009 
1010 public:
1011   RedirectingDirectoryEntry(StringRef Name,
1012                             std::vector<std::unique_ptr<Entry>> Contents,
1013                             Status S)
1014       : Entry(EK_Directory, Name), Contents(std::move(Contents)),
1015         S(std::move(S)) {}
1016   RedirectingDirectoryEntry(StringRef Name, Status S)
1017       : Entry(EK_Directory, Name), S(std::move(S)) {}
1018 
1019   Status getStatus() { return S; }
1020 
1021   void addContent(std::unique_ptr<Entry> Content) {
1022     Contents.push_back(std::move(Content));
1023   }
1024 
1025   Entry *getLastContent() const { return Contents.back().get(); }
1026 
1027   using iterator = decltype(Contents)::iterator;
1028 
1029   iterator contents_begin() { return Contents.begin(); }
1030   iterator contents_end() { return Contents.end(); }
1031 
1032   static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
1033 };
1034 
1035 class RedirectingFileEntry : public Entry {
1036 public:
1037   enum NameKind { NK_NotSet, NK_External, NK_Virtual };
1038 
1039 private:
1040   std::string ExternalContentsPath;
1041   NameKind UseName;
1042 
1043 public:
1044   RedirectingFileEntry(StringRef Name, StringRef ExternalContentsPath,
1045                        NameKind UseName)
1046       : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
1047         UseName(UseName) {}
1048 
1049   StringRef getExternalContentsPath() const { return ExternalContentsPath; }
1050 
1051   /// whether to use the external path as the name for this file.
1052   bool useExternalName(bool GlobalUseExternalName) const {
1053     return UseName == NK_NotSet ? GlobalUseExternalName
1054                                 : (UseName == NK_External);
1055   }
1056 
1057   NameKind getUseName() const { return UseName; }
1058 
1059   static bool classof(const Entry *E) { return E->getKind() == EK_File; }
1060 };
1061 
1062 // FIXME: reuse implementation common with OverlayFSDirIterImpl as these
1063 // iterators are conceptually similar.
1064 class VFSFromYamlDirIterImpl : public llvm::vfs::detail::DirIterImpl {
1065   std::string Dir;
1066   RedirectingDirectoryEntry::iterator Current, End;
1067 
1068   // To handle 'fallthrough' mode we need to iterate at first through
1069   // RedirectingDirectoryEntry and then through ExternalFS. These operations are
1070   // done sequentially, we just need to keep a track of what kind of iteration
1071   // we are currently performing.
1072 
1073   /// Flag telling if we should iterate through ExternalFS or stop at the last
1074   /// RedirectingDirectoryEntry::iterator.
1075   bool IterateExternalFS;
1076   /// Flag telling if we have switched to iterating through ExternalFS.
1077   bool IsExternalFSCurrent = false;
1078   FileSystem &ExternalFS;
1079   directory_iterator ExternalDirIter;
1080   llvm::StringSet<> SeenNames;
1081 
1082   /// To combine multiple iterations, different methods are responsible for
1083   /// different iteration steps.
1084   /// @{
1085 
1086   /// Responsible for dispatching between RedirectingDirectoryEntry iteration
1087   /// and ExternalFS iteration.
1088   std::error_code incrementImpl(bool IsFirstTime);
1089   /// Responsible for RedirectingDirectoryEntry iteration.
1090   std::error_code incrementContent(bool IsFirstTime);
1091   /// Responsible for ExternalFS iteration.
1092   std::error_code incrementExternal();
1093   /// @}
1094 
1095 public:
1096   VFSFromYamlDirIterImpl(const Twine &Path,
1097                          RedirectingDirectoryEntry::iterator Begin,
1098                          RedirectingDirectoryEntry::iterator End,
1099                          bool IterateExternalFS, FileSystem &ExternalFS,
1100                          std::error_code &EC);
1101 
1102   std::error_code increment() override;
1103 };
1104 
1105 /// A virtual file system parsed from a YAML file.
1106 ///
1107 /// Currently, this class allows creating virtual directories and mapping
1108 /// virtual file paths to existing external files, available in \c ExternalFS.
1109 ///
1110 /// The basic structure of the parsed file is:
1111 /// \verbatim
1112 /// {
1113 ///   'version': <version number>,
1114 ///   <optional configuration>
1115 ///   'roots': [
1116 ///              <directory entries>
1117 ///            ]
1118 /// }
1119 /// \endverbatim
1120 ///
1121 /// All configuration options are optional.
1122 ///   'case-sensitive': <boolean, default=true>
1123 ///   'use-external-names': <boolean, default=true>
1124 ///   'overlay-relative': <boolean, default=false>
1125 ///   'fallthrough': <boolean, default=true>
1126 ///
1127 /// Virtual directories are represented as
1128 /// \verbatim
1129 /// {
1130 ///   'type': 'directory',
1131 ///   'name': <string>,
1132 ///   'contents': [ <file or directory entries> ]
1133 /// }
1134 /// \endverbatim
1135 ///
1136 /// The default attributes for virtual directories are:
1137 /// \verbatim
1138 /// MTime = now() when created
1139 /// Perms = 0777
1140 /// User = Group = 0
1141 /// Size = 0
1142 /// UniqueID = unspecified unique value
1143 /// \endverbatim
1144 ///
1145 /// Re-mapped files are represented as
1146 /// \verbatim
1147 /// {
1148 ///   'type': 'file',
1149 ///   'name': <string>,
1150 ///   'use-external-name': <boolean> # Optional
1151 ///   'external-contents': <path to external file>
1152 /// }
1153 /// \endverbatim
1154 ///
1155 /// and inherit their attributes from the external contents.
1156 ///
1157 /// In both cases, the 'name' field may contain multiple path components (e.g.
1158 /// /path/to/file). However, any directory that contains more than one child
1159 /// must be uniquely represented by a directory entry.
1160 class RedirectingFileSystem : public vfs::FileSystem {
1161   friend class RedirectingFileSystemParser;
1162 
1163   /// The root(s) of the virtual file system.
1164   std::vector<std::unique_ptr<Entry>> Roots;
1165 
1166   /// The file system to use for external references.
1167   IntrusiveRefCntPtr<FileSystem> ExternalFS;
1168 
1169   /// If IsRelativeOverlay is set, this represents the directory
1170   /// path that should be prefixed to each 'external-contents' entry
1171   /// when reading from YAML files.
1172   std::string ExternalContentsPrefixDir;
1173 
1174   /// @name Configuration
1175   /// @{
1176 
1177   /// Whether to perform case-sensitive comparisons.
1178   ///
1179   /// Currently, case-insensitive matching only works correctly with ASCII.
1180   bool CaseSensitive = true;
1181 
1182   /// IsRelativeOverlay marks whether a ExternalContentsPrefixDir path must
1183   /// be prefixed in every 'external-contents' when reading from YAML files.
1184   bool IsRelativeOverlay = false;
1185 
1186   /// Whether to use to use the value of 'external-contents' for the
1187   /// names of files.  This global value is overridable on a per-file basis.
1188   bool UseExternalNames = true;
1189 
1190   /// Whether to attempt a file lookup in external file system after it wasn't
1191   /// found in VFS.
1192   bool IsFallthrough = true;
1193   /// @}
1194 
1195   /// Virtual file paths and external files could be canonicalized without "..",
1196   /// "." and "./" in their paths. FIXME: some unittests currently fail on
1197   /// win32 when using remove_dots and remove_leading_dotslash on paths.
1198   bool UseCanonicalizedPaths =
1199 #ifdef _WIN32
1200       false;
1201 #else
1202       true;
1203 #endif
1204 
1205 private:
1206   RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS)
1207       : ExternalFS(std::move(ExternalFS)) {}
1208 
1209   /// Looks up the path <tt>[Start, End)</tt> in \p From, possibly
1210   /// recursing into the contents of \p From if it is a directory.
1211   ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
1212                               sys::path::const_iterator End, Entry *From) const;
1213 
1214   /// Get the status of a given an \c Entry.
1215   ErrorOr<Status> status(const Twine &Path, Entry *E);
1216 
1217 public:
1218   /// Looks up \p Path in \c Roots.
1219   ErrorOr<Entry *> lookupPath(const Twine &Path) const;
1220 
1221   /// Parses \p Buffer, which is expected to be in YAML format and
1222   /// returns a virtual file system representing its contents.
1223   static RedirectingFileSystem *
1224   create(std::unique_ptr<MemoryBuffer> Buffer,
1225          SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath,
1226          void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS);
1227 
1228   ErrorOr<Status> status(const Twine &Path) override;
1229   ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
1230 
1231   std::error_code getRealPath(const Twine &Path,
1232                               SmallVectorImpl<char> &Output) const override;
1233 
1234   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
1235     return ExternalFS->getCurrentWorkingDirectory();
1236   }
1237 
1238   std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
1239     return ExternalFS->setCurrentWorkingDirectory(Path);
1240   }
1241 
1242   std::error_code isLocal(const Twine &Path, bool &Result) override {
1243     return ExternalFS->isLocal(Path, Result);
1244   }
1245 
1246   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override {
1247     ErrorOr<Entry *> E = lookupPath(Dir);
1248     if (!E) {
1249       EC = E.getError();
1250       if (IsFallthrough && EC == errc::no_such_file_or_directory)
1251         return ExternalFS->dir_begin(Dir, EC);
1252       return {};
1253     }
1254     ErrorOr<Status> S = status(Dir, *E);
1255     if (!S) {
1256       EC = S.getError();
1257       return {};
1258     }
1259     if (!S->isDirectory()) {
1260       EC = std::error_code(static_cast<int>(errc::not_a_directory),
1261                            std::system_category());
1262       return {};
1263     }
1264 
1265     auto *D = cast<RedirectingDirectoryEntry>(*E);
1266     return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(
1267         Dir, D->contents_begin(), D->contents_end(),
1268         /*IterateExternalFS=*/IsFallthrough, *ExternalFS, EC));
1269   }
1270 
1271   void setExternalContentsPrefixDir(StringRef PrefixDir) {
1272     ExternalContentsPrefixDir = PrefixDir.str();
1273   }
1274 
1275   StringRef getExternalContentsPrefixDir() const {
1276     return ExternalContentsPrefixDir;
1277   }
1278 
1279 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1280   LLVM_DUMP_METHOD void dump() const {
1281     for (const auto &Root : Roots)
1282       dumpEntry(Root.get());
1283   }
1284 
1285   LLVM_DUMP_METHOD void dumpEntry(Entry *E, int NumSpaces = 0) const {
1286     StringRef Name = E->getName();
1287     for (int i = 0, e = NumSpaces; i < e; ++i)
1288       dbgs() << " ";
1289     dbgs() << "'" << Name.str().c_str() << "'"
1290            << "\n";
1291 
1292     if (E->getKind() == EK_Directory) {
1293       auto *DE = dyn_cast<RedirectingDirectoryEntry>(E);
1294       assert(DE && "Should be a directory");
1295 
1296       for (std::unique_ptr<Entry> &SubEntry :
1297            llvm::make_range(DE->contents_begin(), DE->contents_end()))
1298         dumpEntry(SubEntry.get(), NumSpaces + 2);
1299     }
1300   }
1301 #endif
1302 };
1303 
1304 /// A helper class to hold the common YAML parsing state.
1305 class RedirectingFileSystemParser {
1306   yaml::Stream &Stream;
1307 
1308   void error(yaml::Node *N, const Twine &Msg) { Stream.printError(N, Msg); }
1309 
1310   // false on error
1311   bool parseScalarString(yaml::Node *N, StringRef &Result,
1312                          SmallVectorImpl<char> &Storage) {
1313     const auto *S = dyn_cast<yaml::ScalarNode>(N);
1314 
1315     if (!S) {
1316       error(N, "expected string");
1317       return false;
1318     }
1319     Result = S->getValue(Storage);
1320     return true;
1321   }
1322 
1323   // false on error
1324   bool parseScalarBool(yaml::Node *N, bool &Result) {
1325     SmallString<5> Storage;
1326     StringRef Value;
1327     if (!parseScalarString(N, Value, Storage))
1328       return false;
1329 
1330     if (Value.equals_lower("true") || Value.equals_lower("on") ||
1331         Value.equals_lower("yes") || Value == "1") {
1332       Result = true;
1333       return true;
1334     } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
1335                Value.equals_lower("no") || Value == "0") {
1336       Result = false;
1337       return true;
1338     }
1339 
1340     error(N, "expected boolean value");
1341     return false;
1342   }
1343 
1344   struct KeyStatus {
1345     bool Required;
1346     bool Seen = false;
1347 
1348     KeyStatus(bool Required = false) : Required(Required) {}
1349   };
1350 
1351   using KeyStatusPair = std::pair<StringRef, KeyStatus>;
1352 
1353   // false on error
1354   bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
1355                                   DenseMap<StringRef, KeyStatus> &Keys) {
1356     if (!Keys.count(Key)) {
1357       error(KeyNode, "unknown key");
1358       return false;
1359     }
1360     KeyStatus &S = Keys[Key];
1361     if (S.Seen) {
1362       error(KeyNode, Twine("duplicate key '") + Key + "'");
1363       return false;
1364     }
1365     S.Seen = true;
1366     return true;
1367   }
1368 
1369   // false on error
1370   bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
1371     for (const auto &I : Keys) {
1372       if (I.second.Required && !I.second.Seen) {
1373         error(Obj, Twine("missing key '") + I.first + "'");
1374         return false;
1375       }
1376     }
1377     return true;
1378   }
1379 
1380   Entry *lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name,
1381                              Entry *ParentEntry = nullptr) {
1382     if (!ParentEntry) { // Look for a existent root
1383       for (const auto &Root : FS->Roots) {
1384         if (Name.equals(Root->getName())) {
1385           ParentEntry = Root.get();
1386           return ParentEntry;
1387         }
1388       }
1389     } else { // Advance to the next component
1390       auto *DE = dyn_cast<RedirectingDirectoryEntry>(ParentEntry);
1391       for (std::unique_ptr<Entry> &Content :
1392            llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1393         auto *DirContent = dyn_cast<RedirectingDirectoryEntry>(Content.get());
1394         if (DirContent && Name.equals(Content->getName()))
1395           return DirContent;
1396       }
1397     }
1398 
1399     // ... or create a new one
1400     std::unique_ptr<Entry> E = llvm::make_unique<RedirectingDirectoryEntry>(
1401         Name,
1402         Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1403                0, 0, 0, file_type::directory_file, sys::fs::all_all));
1404 
1405     if (!ParentEntry) { // Add a new root to the overlay
1406       FS->Roots.push_back(std::move(E));
1407       ParentEntry = FS->Roots.back().get();
1408       return ParentEntry;
1409     }
1410 
1411     auto *DE = dyn_cast<RedirectingDirectoryEntry>(ParentEntry);
1412     DE->addContent(std::move(E));
1413     return DE->getLastContent();
1414   }
1415 
1416   void uniqueOverlayTree(RedirectingFileSystem *FS, Entry *SrcE,
1417                          Entry *NewParentE = nullptr) {
1418     StringRef Name = SrcE->getName();
1419     switch (SrcE->getKind()) {
1420     case EK_Directory: {
1421       auto *DE = dyn_cast<RedirectingDirectoryEntry>(SrcE);
1422       assert(DE && "Must be a directory");
1423       // Empty directories could be present in the YAML as a way to
1424       // describe a file for a current directory after some of its subdir
1425       // is parsed. This only leads to redundant walks, ignore it.
1426       if (!Name.empty())
1427         NewParentE = lookupOrCreateEntry(FS, Name, NewParentE);
1428       for (std::unique_ptr<Entry> &SubEntry :
1429            llvm::make_range(DE->contents_begin(), DE->contents_end()))
1430         uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
1431       break;
1432     }
1433     case EK_File: {
1434       auto *FE = dyn_cast<RedirectingFileEntry>(SrcE);
1435       assert(FE && "Must be a file");
1436       assert(NewParentE && "Parent entry must exist");
1437       auto *DE = dyn_cast<RedirectingDirectoryEntry>(NewParentE);
1438       DE->addContent(llvm::make_unique<RedirectingFileEntry>(
1439           Name, FE->getExternalContentsPath(), FE->getUseName()));
1440       break;
1441     }
1442     }
1443   }
1444 
1445   std::unique_ptr<Entry> parseEntry(yaml::Node *N, RedirectingFileSystem *FS,
1446                                     bool IsRootEntry) {
1447     auto *M = dyn_cast<yaml::MappingNode>(N);
1448     if (!M) {
1449       error(N, "expected mapping node for file or directory entry");
1450       return nullptr;
1451     }
1452 
1453     KeyStatusPair Fields[] = {
1454         KeyStatusPair("name", true),
1455         KeyStatusPair("type", true),
1456         KeyStatusPair("contents", false),
1457         KeyStatusPair("external-contents", false),
1458         KeyStatusPair("use-external-name", false),
1459     };
1460 
1461     DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1462 
1463     bool HasContents = false; // external or otherwise
1464     std::vector<std::unique_ptr<Entry>> EntryArrayContents;
1465     std::string ExternalContentsPath;
1466     std::string Name;
1467     yaml::Node *NameValueNode;
1468     auto UseExternalName = RedirectingFileEntry::NK_NotSet;
1469     EntryKind Kind;
1470 
1471     for (auto &I : *M) {
1472       StringRef Key;
1473       // Reuse the buffer for key and value, since we don't look at key after
1474       // parsing value.
1475       SmallString<256> Buffer;
1476       if (!parseScalarString(I.getKey(), Key, Buffer))
1477         return nullptr;
1478 
1479       if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
1480         return nullptr;
1481 
1482       StringRef Value;
1483       if (Key == "name") {
1484         if (!parseScalarString(I.getValue(), Value, Buffer))
1485           return nullptr;
1486 
1487         NameValueNode = I.getValue();
1488         if (FS->UseCanonicalizedPaths) {
1489           SmallString<256> Path(Value);
1490           // Guarantee that old YAML files containing paths with ".." and "."
1491           // are properly canonicalized before read into the VFS.
1492           Path = sys::path::remove_leading_dotslash(Path);
1493           sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1494           Name = Path.str();
1495         } else {
1496           Name = Value;
1497         }
1498       } else if (Key == "type") {
1499         if (!parseScalarString(I.getValue(), Value, Buffer))
1500           return nullptr;
1501         if (Value == "file")
1502           Kind = EK_File;
1503         else if (Value == "directory")
1504           Kind = EK_Directory;
1505         else {
1506           error(I.getValue(), "unknown value for 'type'");
1507           return nullptr;
1508         }
1509       } else if (Key == "contents") {
1510         if (HasContents) {
1511           error(I.getKey(),
1512                 "entry already has 'contents' or 'external-contents'");
1513           return nullptr;
1514         }
1515         HasContents = true;
1516         auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue());
1517         if (!Contents) {
1518           // FIXME: this is only for directories, what about files?
1519           error(I.getValue(), "expected array");
1520           return nullptr;
1521         }
1522 
1523         for (auto &I : *Contents) {
1524           if (std::unique_ptr<Entry> E =
1525                   parseEntry(&I, FS, /*IsRootEntry*/ false))
1526             EntryArrayContents.push_back(std::move(E));
1527           else
1528             return nullptr;
1529         }
1530       } else if (Key == "external-contents") {
1531         if (HasContents) {
1532           error(I.getKey(),
1533                 "entry already has 'contents' or 'external-contents'");
1534           return nullptr;
1535         }
1536         HasContents = true;
1537         if (!parseScalarString(I.getValue(), Value, Buffer))
1538           return nullptr;
1539 
1540         SmallString<256> FullPath;
1541         if (FS->IsRelativeOverlay) {
1542           FullPath = FS->getExternalContentsPrefixDir();
1543           assert(!FullPath.empty() &&
1544                  "External contents prefix directory must exist");
1545           llvm::sys::path::append(FullPath, Value);
1546         } else {
1547           FullPath = Value;
1548         }
1549 
1550         if (FS->UseCanonicalizedPaths) {
1551           // Guarantee that old YAML files containing paths with ".." and "."
1552           // are properly canonicalized before read into the VFS.
1553           FullPath = sys::path::remove_leading_dotslash(FullPath);
1554           sys::path::remove_dots(FullPath, /*remove_dot_dot=*/true);
1555         }
1556         ExternalContentsPath = FullPath.str();
1557       } else if (Key == "use-external-name") {
1558         bool Val;
1559         if (!parseScalarBool(I.getValue(), Val))
1560           return nullptr;
1561         UseExternalName = Val ? RedirectingFileEntry::NK_External
1562                               : RedirectingFileEntry::NK_Virtual;
1563       } else {
1564         llvm_unreachable("key missing from Keys");
1565       }
1566     }
1567 
1568     if (Stream.failed())
1569       return nullptr;
1570 
1571     // check for missing keys
1572     if (!HasContents) {
1573       error(N, "missing key 'contents' or 'external-contents'");
1574       return nullptr;
1575     }
1576     if (!checkMissingKeys(N, Keys))
1577       return nullptr;
1578 
1579     // check invalid configuration
1580     if (Kind == EK_Directory &&
1581         UseExternalName != RedirectingFileEntry::NK_NotSet) {
1582       error(N, "'use-external-name' is not supported for directories");
1583       return nullptr;
1584     }
1585 
1586     if (IsRootEntry && !sys::path::is_absolute(Name)) {
1587       assert(NameValueNode && "Name presence should be checked earlier");
1588       error(NameValueNode,
1589             "entry with relative path at the root level is not discoverable");
1590       return nullptr;
1591     }
1592 
1593     // Remove trailing slash(es), being careful not to remove the root path
1594     StringRef Trimmed(Name);
1595     size_t RootPathLen = sys::path::root_path(Trimmed).size();
1596     while (Trimmed.size() > RootPathLen &&
1597            sys::path::is_separator(Trimmed.back()))
1598       Trimmed = Trimmed.slice(0, Trimmed.size() - 1);
1599     // Get the last component
1600     StringRef LastComponent = sys::path::filename(Trimmed);
1601 
1602     std::unique_ptr<Entry> Result;
1603     switch (Kind) {
1604     case EK_File:
1605       Result = llvm::make_unique<RedirectingFileEntry>(
1606           LastComponent, std::move(ExternalContentsPath), UseExternalName);
1607       break;
1608     case EK_Directory:
1609       Result = llvm::make_unique<RedirectingDirectoryEntry>(
1610           LastComponent, std::move(EntryArrayContents),
1611           Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1612                  0, 0, 0, file_type::directory_file, sys::fs::all_all));
1613       break;
1614     }
1615 
1616     StringRef Parent = sys::path::parent_path(Trimmed);
1617     if (Parent.empty())
1618       return Result;
1619 
1620     // if 'name' contains multiple components, create implicit directory entries
1621     for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
1622                                      E = sys::path::rend(Parent);
1623          I != E; ++I) {
1624       std::vector<std::unique_ptr<Entry>> Entries;
1625       Entries.push_back(std::move(Result));
1626       Result = llvm::make_unique<RedirectingDirectoryEntry>(
1627           *I, std::move(Entries),
1628           Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1629                  0, 0, 0, file_type::directory_file, sys::fs::all_all));
1630     }
1631     return Result;
1632   }
1633 
1634 public:
1635   RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {}
1636 
1637   // false on error
1638   bool parse(yaml::Node *Root, RedirectingFileSystem *FS) {
1639     auto *Top = dyn_cast<yaml::MappingNode>(Root);
1640     if (!Top) {
1641       error(Root, "expected mapping node");
1642       return false;
1643     }
1644 
1645     KeyStatusPair Fields[] = {
1646         KeyStatusPair("version", true),
1647         KeyStatusPair("case-sensitive", false),
1648         KeyStatusPair("use-external-names", false),
1649         KeyStatusPair("overlay-relative", false),
1650         KeyStatusPair("fallthrough", false),
1651         KeyStatusPair("roots", true),
1652     };
1653 
1654     DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1655     std::vector<std::unique_ptr<Entry>> RootEntries;
1656 
1657     // Parse configuration and 'roots'
1658     for (auto &I : *Top) {
1659       SmallString<10> KeyBuffer;
1660       StringRef Key;
1661       if (!parseScalarString(I.getKey(), Key, KeyBuffer))
1662         return false;
1663 
1664       if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
1665         return false;
1666 
1667       if (Key == "roots") {
1668         auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue());
1669         if (!Roots) {
1670           error(I.getValue(), "expected array");
1671           return false;
1672         }
1673 
1674         for (auto &I : *Roots) {
1675           if (std::unique_ptr<Entry> E =
1676                   parseEntry(&I, FS, /*IsRootEntry*/ true))
1677             RootEntries.push_back(std::move(E));
1678           else
1679             return false;
1680         }
1681       } else if (Key == "version") {
1682         StringRef VersionString;
1683         SmallString<4> Storage;
1684         if (!parseScalarString(I.getValue(), VersionString, Storage))
1685           return false;
1686         int Version;
1687         if (VersionString.getAsInteger<int>(10, Version)) {
1688           error(I.getValue(), "expected integer");
1689           return false;
1690         }
1691         if (Version < 0) {
1692           error(I.getValue(), "invalid version number");
1693           return false;
1694         }
1695         if (Version != 0) {
1696           error(I.getValue(), "version mismatch, expected 0");
1697           return false;
1698         }
1699       } else if (Key == "case-sensitive") {
1700         if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
1701           return false;
1702       } else if (Key == "overlay-relative") {
1703         if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
1704           return false;
1705       } else if (Key == "use-external-names") {
1706         if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
1707           return false;
1708       } else if (Key == "fallthrough") {
1709         if (!parseScalarBool(I.getValue(), FS->IsFallthrough))
1710           return false;
1711       } else {
1712         llvm_unreachable("key missing from Keys");
1713       }
1714     }
1715 
1716     if (Stream.failed())
1717       return false;
1718 
1719     if (!checkMissingKeys(Top, Keys))
1720       return false;
1721 
1722     // Now that we sucessefully parsed the YAML file, canonicalize the internal
1723     // representation to a proper directory tree so that we can search faster
1724     // inside the VFS.
1725     for (auto &E : RootEntries)
1726       uniqueOverlayTree(FS, E.get());
1727 
1728     return true;
1729   }
1730 };
1731 
1732 } // namespace
1733 
1734 RedirectingFileSystem *
1735 RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
1736                               SourceMgr::DiagHandlerTy DiagHandler,
1737                               StringRef YAMLFilePath, void *DiagContext,
1738                               IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1739   SourceMgr SM;
1740   yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
1741 
1742   SM.setDiagHandler(DiagHandler, DiagContext);
1743   yaml::document_iterator DI = Stream.begin();
1744   yaml::Node *Root = DI->getRoot();
1745   if (DI == Stream.end() || !Root) {
1746     SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
1747     return nullptr;
1748   }
1749 
1750   RedirectingFileSystemParser P(Stream);
1751 
1752   std::unique_ptr<RedirectingFileSystem> FS(
1753       new RedirectingFileSystem(std::move(ExternalFS)));
1754 
1755   if (!YAMLFilePath.empty()) {
1756     // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed
1757     // to each 'external-contents' path.
1758     //
1759     // Example:
1760     //    -ivfsoverlay dummy.cache/vfs/vfs.yaml
1761     // yields:
1762     //  FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs
1763     //
1764     SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
1765     std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
1766     assert(!EC && "Overlay dir final path must be absolute");
1767     (void)EC;
1768     FS->setExternalContentsPrefixDir(OverlayAbsDir);
1769   }
1770 
1771   if (!P.parse(Root, FS.get()))
1772     return nullptr;
1773 
1774   return FS.release();
1775 }
1776 
1777 ErrorOr<Entry *> RedirectingFileSystem::lookupPath(const Twine &Path_) const {
1778   SmallString<256> Path;
1779   Path_.toVector(Path);
1780 
1781   // Handle relative paths
1782   if (std::error_code EC = makeAbsolute(Path))
1783     return EC;
1784 
1785   // Canonicalize path by removing ".", "..", "./", etc components. This is
1786   // a VFS request, do bot bother about symlinks in the path components
1787   // but canonicalize in order to perform the correct entry search.
1788   if (UseCanonicalizedPaths) {
1789     Path = sys::path::remove_leading_dotslash(Path);
1790     sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1791   }
1792 
1793   if (Path.empty())
1794     return make_error_code(llvm::errc::invalid_argument);
1795 
1796   sys::path::const_iterator Start = sys::path::begin(Path);
1797   sys::path::const_iterator End = sys::path::end(Path);
1798   for (const auto &Root : Roots) {
1799     ErrorOr<Entry *> Result = lookupPath(Start, End, Root.get());
1800     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1801       return Result;
1802   }
1803   return make_error_code(llvm::errc::no_such_file_or_directory);
1804 }
1805 
1806 ErrorOr<Entry *>
1807 RedirectingFileSystem::lookupPath(sys::path::const_iterator Start,
1808                                   sys::path::const_iterator End,
1809                                   Entry *From) const {
1810 #ifndef _WIN32
1811   assert(!isTraversalComponent(*Start) &&
1812          !isTraversalComponent(From->getName()) &&
1813          "Paths should not contain traversal components");
1814 #else
1815   // FIXME: this is here to support windows, remove it once canonicalized
1816   // paths become globally default.
1817   if (Start->equals("."))
1818     ++Start;
1819 #endif
1820 
1821   StringRef FromName = From->getName();
1822 
1823   // Forward the search to the next component in case this is an empty one.
1824   if (!FromName.empty()) {
1825     if (CaseSensitive ? !Start->equals(FromName)
1826                       : !Start->equals_lower(FromName))
1827       // failure to match
1828       return make_error_code(llvm::errc::no_such_file_or_directory);
1829 
1830     ++Start;
1831 
1832     if (Start == End) {
1833       // Match!
1834       return From;
1835     }
1836   }
1837 
1838   auto *DE = dyn_cast<RedirectingDirectoryEntry>(From);
1839   if (!DE)
1840     return make_error_code(llvm::errc::not_a_directory);
1841 
1842   for (const std::unique_ptr<Entry> &DirEntry :
1843        llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1844     ErrorOr<Entry *> Result = lookupPath(Start, End, DirEntry.get());
1845     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
1846       return Result;
1847   }
1848   return make_error_code(llvm::errc::no_such_file_or_directory);
1849 }
1850 
1851 static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames,
1852                                       Status ExternalStatus) {
1853   Status S = ExternalStatus;
1854   if (!UseExternalNames)
1855     S = Status::copyWithNewName(S, Path.str());
1856   S.IsVFSMapped = true;
1857   return S;
1858 }
1859 
1860 ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path, Entry *E) {
1861   assert(E != nullptr);
1862   if (auto *F = dyn_cast<RedirectingFileEntry>(E)) {
1863     ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
1864     assert(!S || S->getName() == F->getExternalContentsPath());
1865     if (S)
1866       return getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
1867                                      *S);
1868     return S;
1869   } else { // directory
1870     auto *DE = cast<RedirectingDirectoryEntry>(E);
1871     return Status::copyWithNewName(DE->getStatus(), Path.str());
1872   }
1873 }
1874 
1875 ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path) {
1876   ErrorOr<Entry *> Result = lookupPath(Path);
1877   if (!Result) {
1878     if (IsFallthrough &&
1879         Result.getError() == llvm::errc::no_such_file_or_directory) {
1880       return ExternalFS->status(Path);
1881     }
1882     return Result.getError();
1883   }
1884   return status(Path, *Result);
1885 }
1886 
1887 namespace {
1888 
1889 /// Provide a file wrapper with an overriden status.
1890 class FileWithFixedStatus : public File {
1891   std::unique_ptr<File> InnerFile;
1892   Status S;
1893 
1894 public:
1895   FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S)
1896       : InnerFile(std::move(InnerFile)), S(std::move(S)) {}
1897 
1898   ErrorOr<Status> status() override { return S; }
1899   ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
1900 
1901   getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
1902             bool IsVolatile) override {
1903     return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
1904                                 IsVolatile);
1905   }
1906 
1907   std::error_code close() override { return InnerFile->close(); }
1908 };
1909 
1910 } // namespace
1911 
1912 ErrorOr<std::unique_ptr<File>>
1913 RedirectingFileSystem::openFileForRead(const Twine &Path) {
1914   ErrorOr<Entry *> E = lookupPath(Path);
1915   if (!E) {
1916     if (IsFallthrough &&
1917         E.getError() == llvm::errc::no_such_file_or_directory) {
1918       return ExternalFS->openFileForRead(Path);
1919     }
1920     return E.getError();
1921   }
1922 
1923   auto *F = dyn_cast<RedirectingFileEntry>(*E);
1924   if (!F) // FIXME: errc::not_a_file?
1925     return make_error_code(llvm::errc::invalid_argument);
1926 
1927   auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath());
1928   if (!Result)
1929     return Result;
1930 
1931   auto ExternalStatus = (*Result)->status();
1932   if (!ExternalStatus)
1933     return ExternalStatus.getError();
1934 
1935   // FIXME: Update the status with the name and VFSMapped.
1936   Status S = getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames),
1937                                      *ExternalStatus);
1938   return std::unique_ptr<File>(
1939       llvm::make_unique<FileWithFixedStatus>(std::move(*Result), S));
1940 }
1941 
1942 std::error_code
1943 RedirectingFileSystem::getRealPath(const Twine &Path,
1944                                    SmallVectorImpl<char> &Output) const {
1945   ErrorOr<Entry *> Result = lookupPath(Path);
1946   if (!Result) {
1947     if (IsFallthrough &&
1948         Result.getError() == llvm::errc::no_such_file_or_directory) {
1949       return ExternalFS->getRealPath(Path, Output);
1950     }
1951     return Result.getError();
1952   }
1953 
1954   if (auto *F = dyn_cast<RedirectingFileEntry>(*Result)) {
1955     return ExternalFS->getRealPath(F->getExternalContentsPath(), Output);
1956   }
1957   // Even if there is a directory entry, fall back to ExternalFS if allowed,
1958   // because directories don't have a single external contents path.
1959   return IsFallthrough ? ExternalFS->getRealPath(Path, Output)
1960                        : llvm::errc::invalid_argument;
1961 }
1962 
1963 IntrusiveRefCntPtr<FileSystem>
1964 vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
1965                     SourceMgr::DiagHandlerTy DiagHandler,
1966                     StringRef YAMLFilePath, void *DiagContext,
1967                     IntrusiveRefCntPtr<FileSystem> ExternalFS) {
1968   return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
1969                                        YAMLFilePath, DiagContext,
1970                                        std::move(ExternalFS));
1971 }
1972 
1973 static void getVFSEntries(Entry *SrcE, SmallVectorImpl<StringRef> &Path,
1974                           SmallVectorImpl<YAMLVFSEntry> &Entries) {
1975   auto Kind = SrcE->getKind();
1976   if (Kind == EK_Directory) {
1977     auto *DE = dyn_cast<RedirectingDirectoryEntry>(SrcE);
1978     assert(DE && "Must be a directory");
1979     for (std::unique_ptr<Entry> &SubEntry :
1980          llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1981       Path.push_back(SubEntry->getName());
1982       getVFSEntries(SubEntry.get(), Path, Entries);
1983       Path.pop_back();
1984     }
1985     return;
1986   }
1987 
1988   assert(Kind == EK_File && "Must be a EK_File");
1989   auto *FE = dyn_cast<RedirectingFileEntry>(SrcE);
1990   assert(FE && "Must be a file");
1991   SmallString<128> VPath;
1992   for (auto &Comp : Path)
1993     llvm::sys::path::append(VPath, Comp);
1994   Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
1995 }
1996 
1997 void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
1998                              SourceMgr::DiagHandlerTy DiagHandler,
1999                              StringRef YAMLFilePath,
2000                              SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
2001                              void *DiagContext,
2002                              IntrusiveRefCntPtr<FileSystem> ExternalFS) {
2003   RedirectingFileSystem *VFS = RedirectingFileSystem::create(
2004       std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
2005       std::move(ExternalFS));
2006   ErrorOr<Entry *> RootE = VFS->lookupPath("/");
2007   if (!RootE)
2008     return;
2009   SmallVector<StringRef, 8> Components;
2010   Components.push_back("/");
2011   getVFSEntries(*RootE, Components, CollectedEntries);
2012 }
2013 
2014 UniqueID vfs::getNextVirtualUniqueID() {
2015   static std::atomic<unsigned> UID;
2016   unsigned ID = ++UID;
2017   // The following assumes that uint64_t max will never collide with a real
2018   // dev_t value from the OS.
2019   return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
2020 }
2021 
2022 void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
2023   assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
2024   assert(sys::path::is_absolute(RealPath) && "real path not absolute");
2025   assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
2026   Mappings.emplace_back(VirtualPath, RealPath);
2027 }
2028 
2029 namespace {
2030 
2031 class JSONWriter {
2032   llvm::raw_ostream &OS;
2033   SmallVector<StringRef, 16> DirStack;
2034 
2035   unsigned getDirIndent() { return 4 * DirStack.size(); }
2036   unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
2037   bool containedIn(StringRef Parent, StringRef Path);
2038   StringRef containedPart(StringRef Parent, StringRef Path);
2039   void startDirectory(StringRef Path);
2040   void endDirectory();
2041   void writeEntry(StringRef VPath, StringRef RPath);
2042 
2043 public:
2044   JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
2045 
2046   void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames,
2047              Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative,
2048              StringRef OverlayDir);
2049 };
2050 
2051 } // namespace
2052 
2053 bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
2054   using namespace llvm::sys;
2055 
2056   // Compare each path component.
2057   auto IParent = path::begin(Parent), EParent = path::end(Parent);
2058   for (auto IChild = path::begin(Path), EChild = path::end(Path);
2059        IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
2060     if (*IParent != *IChild)
2061       return false;
2062   }
2063   // Have we exhausted the parent path?
2064   return IParent == EParent;
2065 }
2066 
2067 StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
2068   assert(!Parent.empty());
2069   assert(containedIn(Parent, Path));
2070   return Path.slice(Parent.size() + 1, StringRef::npos);
2071 }
2072 
2073 void JSONWriter::startDirectory(StringRef Path) {
2074   StringRef Name =
2075       DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
2076   DirStack.push_back(Path);
2077   unsigned Indent = getDirIndent();
2078   OS.indent(Indent) << "{\n";
2079   OS.indent(Indent + 2) << "'type': 'directory',\n";
2080   OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
2081   OS.indent(Indent + 2) << "'contents': [\n";
2082 }
2083 
2084 void JSONWriter::endDirectory() {
2085   unsigned Indent = getDirIndent();
2086   OS.indent(Indent + 2) << "]\n";
2087   OS.indent(Indent) << "}";
2088 
2089   DirStack.pop_back();
2090 }
2091 
2092 void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
2093   unsigned Indent = getFileIndent();
2094   OS.indent(Indent) << "{\n";
2095   OS.indent(Indent + 2) << "'type': 'file',\n";
2096   OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
2097   OS.indent(Indent + 2) << "'external-contents': \""
2098                         << llvm::yaml::escape(RPath) << "\"\n";
2099   OS.indent(Indent) << "}";
2100 }
2101 
2102 void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
2103                        Optional<bool> UseExternalNames,
2104                        Optional<bool> IsCaseSensitive,
2105                        Optional<bool> IsOverlayRelative,
2106                        StringRef OverlayDir) {
2107   using namespace llvm::sys;
2108 
2109   OS << "{\n"
2110         "  'version': 0,\n";
2111   if (IsCaseSensitive.hasValue())
2112     OS << "  'case-sensitive': '"
2113        << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
2114   if (UseExternalNames.hasValue())
2115     OS << "  'use-external-names': '"
2116        << (UseExternalNames.getValue() ? "true" : "false") << "',\n";
2117   bool UseOverlayRelative = false;
2118   if (IsOverlayRelative.hasValue()) {
2119     UseOverlayRelative = IsOverlayRelative.getValue();
2120     OS << "  'overlay-relative': '" << (UseOverlayRelative ? "true" : "false")
2121        << "',\n";
2122   }
2123   OS << "  'roots': [\n";
2124 
2125   if (!Entries.empty()) {
2126     const YAMLVFSEntry &Entry = Entries.front();
2127     startDirectory(path::parent_path(Entry.VPath));
2128 
2129     StringRef RPath = Entry.RPath;
2130     if (UseOverlayRelative) {
2131       unsigned OverlayDirLen = OverlayDir.size();
2132       assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
2133              "Overlay dir must be contained in RPath");
2134       RPath = RPath.slice(OverlayDirLen, RPath.size());
2135     }
2136 
2137     writeEntry(path::filename(Entry.VPath), RPath);
2138 
2139     for (const auto &Entry : Entries.slice(1)) {
2140       StringRef Dir = path::parent_path(Entry.VPath);
2141       if (Dir == DirStack.back())
2142         OS << ",\n";
2143       else {
2144         while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
2145           OS << "\n";
2146           endDirectory();
2147         }
2148         OS << ",\n";
2149         startDirectory(Dir);
2150       }
2151       StringRef RPath = Entry.RPath;
2152       if (UseOverlayRelative) {
2153         unsigned OverlayDirLen = OverlayDir.size();
2154         assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
2155                "Overlay dir must be contained in RPath");
2156         RPath = RPath.slice(OverlayDirLen, RPath.size());
2157       }
2158       writeEntry(path::filename(Entry.VPath), RPath);
2159     }
2160 
2161     while (!DirStack.empty()) {
2162       OS << "\n";
2163       endDirectory();
2164     }
2165     OS << "\n";
2166   }
2167 
2168   OS << "  ]\n"
2169      << "}\n";
2170 }
2171 
2172 void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
2173   llvm::sort(Mappings, [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
2174     return LHS.VPath < RHS.VPath;
2175   });
2176 
2177   JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
2178                        IsOverlayRelative, OverlayDir);
2179 }
2180 
2181 VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(
2182     const Twine &_Path, RedirectingDirectoryEntry::iterator Begin,
2183     RedirectingDirectoryEntry::iterator End, bool IterateExternalFS,
2184     FileSystem &ExternalFS, std::error_code &EC)
2185     : Dir(_Path.str()), Current(Begin), End(End),
2186       IterateExternalFS(IterateExternalFS), ExternalFS(ExternalFS) {
2187   EC = incrementImpl(/*IsFirstTime=*/true);
2188 }
2189 
2190 std::error_code VFSFromYamlDirIterImpl::increment() {
2191   return incrementImpl(/*IsFirstTime=*/false);
2192 }
2193 
2194 std::error_code VFSFromYamlDirIterImpl::incrementExternal() {
2195   assert(!(IsExternalFSCurrent && ExternalDirIter == directory_iterator()) &&
2196          "incrementing past end");
2197   std::error_code EC;
2198   if (IsExternalFSCurrent) {
2199     ExternalDirIter.increment(EC);
2200   } else if (IterateExternalFS) {
2201     ExternalDirIter = ExternalFS.dir_begin(Dir, EC);
2202     IsExternalFSCurrent = true;
2203     if (EC && EC != errc::no_such_file_or_directory)
2204       return EC;
2205     EC = {};
2206   }
2207   if (EC || ExternalDirIter == directory_iterator()) {
2208     CurrentEntry = directory_entry();
2209   } else {
2210     CurrentEntry = *ExternalDirIter;
2211   }
2212   return EC;
2213 }
2214 
2215 std::error_code VFSFromYamlDirIterImpl::incrementContent(bool IsFirstTime) {
2216   assert((IsFirstTime || Current != End) && "cannot iterate past end");
2217   if (!IsFirstTime)
2218     ++Current;
2219   while (Current != End) {
2220     SmallString<128> PathStr(Dir);
2221     llvm::sys::path::append(PathStr, (*Current)->getName());
2222     sys::fs::file_type Type;
2223     switch ((*Current)->getKind()) {
2224     case EK_Directory:
2225       Type = sys::fs::file_type::directory_file;
2226       break;
2227     case EK_File:
2228       Type = sys::fs::file_type::regular_file;
2229       break;
2230     }
2231     CurrentEntry = directory_entry(PathStr.str(), Type);
2232     return {};
2233   }
2234   return incrementExternal();
2235 }
2236 
2237 std::error_code VFSFromYamlDirIterImpl::incrementImpl(bool IsFirstTime) {
2238   while (true) {
2239     std::error_code EC = IsExternalFSCurrent ? incrementExternal()
2240                                              : incrementContent(IsFirstTime);
2241     if (EC || CurrentEntry.path().empty())
2242       return EC;
2243     StringRef Name = llvm::sys::path::filename(CurrentEntry.path());
2244     if (SeenNames.insert(Name).second)
2245       return EC; // name not seen before
2246   }
2247   llvm_unreachable("returned above");
2248 }
2249 
2250 vfs::recursive_directory_iterator::recursive_directory_iterator(
2251     FileSystem &FS_, const Twine &Path, std::error_code &EC)
2252     : FS(&FS_) {
2253   directory_iterator I = FS->dir_begin(Path, EC);
2254   if (I != directory_iterator()) {
2255     State = std::make_shared<detail::RecDirIterState>();
2256     State->Stack.push(I);
2257   }
2258 }
2259 
2260 vfs::recursive_directory_iterator &
2261 recursive_directory_iterator::increment(std::error_code &EC) {
2262   assert(FS && State && !State->Stack.empty() && "incrementing past end");
2263   assert(!State->Stack.top()->path().empty() && "non-canonical end iterator");
2264   vfs::directory_iterator End;
2265 
2266   if (State->HasNoPushRequest)
2267     State->HasNoPushRequest = false;
2268   else {
2269     if (State->Stack.top()->type() == sys::fs::file_type::directory_file) {
2270       vfs::directory_iterator I = FS->dir_begin(State->Stack.top()->path(), EC);
2271       if (I != End) {
2272         State->Stack.push(I);
2273         return *this;
2274       }
2275     }
2276   }
2277 
2278   while (!State->Stack.empty() && State->Stack.top().increment(EC) == End)
2279     State->Stack.pop();
2280 
2281   if (State->Stack.empty())
2282     State.reset(); // end iterator
2283 
2284   return *this;
2285 }
2286