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