xref: /llvm-project/llvm/lib/Support/VirtualFileSystem.cpp (revision b55ac6e9cb539a52e6bdc5ce6bf40258dd41f63e)
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/STLExtras.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/Chrono.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Errc.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/ErrorOr.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/FileSystem/UniqueID.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/SMLoc.h"
37 #include "llvm/Support/SourceMgr.h"
38 #include "llvm/Support/YAMLParser.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include <algorithm>
41 #include <atomic>
42 #include <cassert>
43 #include <cstdint>
44 #include <iterator>
45 #include <limits>
46 #include <map>
47 #include <memory>
48 #include <optional>
49 #include <string>
50 #include <system_error>
51 #include <utility>
52 #include <vector>
53 
54 using namespace llvm;
55 using namespace llvm::vfs;
56 
57 using llvm::sys::fs::file_t;
58 using llvm::sys::fs::file_status;
59 using llvm::sys::fs::file_type;
60 using llvm::sys::fs::kInvalidFile;
61 using llvm::sys::fs::perms;
62 using llvm::sys::fs::UniqueID;
63 
64 Status::Status(const file_status &Status)
65     : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
66       User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
67       Type(Status.type()), Perms(Status.permissions()) {}
68 
69 Status::Status(const Twine &Name, UniqueID UID, sys::TimePoint<> MTime,
70                uint32_t User, uint32_t Group, uint64_t Size, file_type Type,
71                perms Perms)
72     : Name(Name.str()), UID(UID), MTime(MTime), User(User), Group(Group),
73       Size(Size), Type(Type), Perms(Perms) {}
74 
75 Status Status::copyWithNewSize(const Status &In, uint64_t NewSize) {
76   return Status(In.getName(), In.getUniqueID(), In.getLastModificationTime(),
77                 In.getUser(), In.getGroup(), NewSize, In.getType(),
78                 In.getPermissions());
79 }
80 
81 Status Status::copyWithNewName(const Status &In, const Twine &NewName) {
82   return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
83                 In.getUser(), In.getGroup(), In.getSize(), In.getType(),
84                 In.getPermissions());
85 }
86 
87 Status Status::copyWithNewName(const file_status &In, const Twine &NewName) {
88   return Status(NewName, In.getUniqueID(), In.getLastModificationTime(),
89                 In.getUser(), In.getGroup(), In.getSize(), In.type(),
90                 In.permissions());
91 }
92 
93 bool Status::equivalent(const Status &Other) const {
94   assert(isStatusKnown() && Other.isStatusKnown());
95   return getUniqueID() == Other.getUniqueID();
96 }
97 
98 bool Status::isDirectory() const { return Type == file_type::directory_file; }
99 
100 bool Status::isRegularFile() const { return Type == file_type::regular_file; }
101 
102 bool Status::isOther() const {
103   return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
104 }
105 
106 bool Status::isSymlink() const { return Type == file_type::symlink_file; }
107 
108 bool Status::isStatusKnown() const { return Type != file_type::status_error; }
109 
110 bool Status::exists() const {
111   return isStatusKnown() && Type != file_type::file_not_found;
112 }
113 
114 File::~File() = default;
115 
116 FileSystem::~FileSystem() = default;
117 
118 ErrorOr<std::unique_ptr<MemoryBuffer>>
119 FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
120                              bool RequiresNullTerminator, bool IsVolatile) {
121   auto F = openFileForRead(Name);
122   if (!F)
123     return F.getError();
124 
125   return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
126 }
127 
128 std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
129   if (llvm::sys::path::is_absolute(Path))
130     return {};
131 
132   auto WorkingDir = getCurrentWorkingDirectory();
133   if (!WorkingDir)
134     return WorkingDir.getError();
135 
136   llvm::sys::fs::make_absolute(WorkingDir.get(), Path);
137   return {};
138 }
139 
140 std::error_code FileSystem::getRealPath(const Twine &Path,
141                                         SmallVectorImpl<char> &Output) const {
142   return errc::operation_not_permitted;
143 }
144 
145 std::error_code FileSystem::isLocal(const Twine &Path, bool &Result) {
146   return errc::operation_not_permitted;
147 }
148 
149 bool FileSystem::exists(const Twine &Path) {
150   auto Status = status(Path);
151   return Status && Status->exists();
152 }
153 
154 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
155 void FileSystem::dump() const { print(dbgs(), PrintType::RecursiveContents); }
156 #endif
157 
158 #ifndef NDEBUG
159 static bool isTraversalComponent(StringRef Component) {
160   return Component.equals("..") || Component.equals(".");
161 }
162 
163 static bool pathHasTraversal(StringRef Path) {
164   using namespace llvm::sys;
165 
166   for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
167     if (isTraversalComponent(Comp))
168       return true;
169   return false;
170 }
171 #endif
172 
173 //===-----------------------------------------------------------------------===/
174 // RealFileSystem implementation
175 //===-----------------------------------------------------------------------===/
176 
177 namespace {
178 
179 /// Wrapper around a raw file descriptor.
180 class RealFile : public File {
181   friend class RealFileSystem;
182 
183   file_t FD;
184   Status S;
185   std::string RealName;
186 
187   RealFile(file_t RawFD, StringRef NewName, StringRef NewRealPathName)
188       : FD(RawFD), S(NewName, {}, {}, {}, {}, {},
189                      llvm::sys::fs::file_type::status_error, {}),
190         RealName(NewRealPathName.str()) {
191     assert(FD != kInvalidFile && "Invalid or inactive file descriptor");
192   }
193 
194 public:
195   ~RealFile() override;
196 
197   ErrorOr<Status> status() override;
198   ErrorOr<std::string> getName() override;
199   ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name,
200                                                    int64_t FileSize,
201                                                    bool RequiresNullTerminator,
202                                                    bool IsVolatile) override;
203   std::error_code close() override;
204   void setPath(const Twine &Path) override;
205 };
206 
207 } // namespace
208 
209 RealFile::~RealFile() { close(); }
210 
211 ErrorOr<Status> RealFile::status() {
212   assert(FD != kInvalidFile && "cannot stat closed file");
213   if (!S.isStatusKnown()) {
214     file_status RealStatus;
215     if (std::error_code EC = sys::fs::status(FD, RealStatus))
216       return EC;
217     S = Status::copyWithNewName(RealStatus, S.getName());
218   }
219   return S;
220 }
221 
222 ErrorOr<std::string> RealFile::getName() {
223   return RealName.empty() ? S.getName().str() : RealName;
224 }
225 
226 ErrorOr<std::unique_ptr<MemoryBuffer>>
227 RealFile::getBuffer(const Twine &Name, int64_t FileSize,
228                     bool RequiresNullTerminator, bool IsVolatile) {
229   assert(FD != kInvalidFile && "cannot get buffer for closed file");
230   return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
231                                    IsVolatile);
232 }
233 
234 std::error_code RealFile::close() {
235   std::error_code EC = sys::fs::closeFile(FD);
236   FD = kInvalidFile;
237   return EC;
238 }
239 
240 void RealFile::setPath(const Twine &Path) {
241   RealName = Path.str();
242   if (auto Status = status())
243     S = Status.get().copyWithNewName(Status.get(), Path);
244 }
245 
246 namespace {
247 
248 /// A file system according to your operating system.
249 /// This may be linked to the process's working directory, or maintain its own.
250 ///
251 /// Currently, its own working directory is emulated by storing the path and
252 /// sending absolute paths to llvm::sys::fs:: functions.
253 /// A more principled approach would be to push this down a level, modelling
254 /// the working dir as an llvm::sys::fs::WorkingDir or similar.
255 /// This would enable the use of openat()-style functions on some platforms.
256 class RealFileSystem : public FileSystem {
257 public:
258   explicit RealFileSystem(bool LinkCWDToProcess) {
259     if (!LinkCWDToProcess) {
260       SmallString<128> PWD, RealPWD;
261       if (std::error_code EC = llvm::sys::fs::current_path(PWD))
262         WD = EC;
263       else if (llvm::sys::fs::real_path(PWD, RealPWD))
264         WD = WorkingDirectory{PWD, PWD};
265       else
266         WD = WorkingDirectory{PWD, RealPWD};
267     }
268   }
269 
270   ErrorOr<Status> status(const Twine &Path) override;
271   ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
272   directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
273 
274   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override;
275   std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
276   std::error_code isLocal(const Twine &Path, bool &Result) override;
277   std::error_code getRealPath(const Twine &Path,
278                               SmallVectorImpl<char> &Output) const override;
279 
280 protected:
281   void printImpl(raw_ostream &OS, PrintType Type,
282                  unsigned IndentLevel) const override;
283 
284 private:
285   // If this FS has its own working dir, use it to make Path absolute.
286   // The returned twine is safe to use as long as both Storage and Path live.
287   Twine adjustPath(const Twine &Path, SmallVectorImpl<char> &Storage) const {
288     if (!WD || !*WD)
289       return Path;
290     Path.toVector(Storage);
291     sys::fs::make_absolute(WD->get().Resolved, Storage);
292     return Storage;
293   }
294 
295   struct WorkingDirectory {
296     // The current working directory, without symlinks resolved. (echo $PWD).
297     SmallString<128> Specified;
298     // The current working directory, with links resolved. (readlink .).
299     SmallString<128> Resolved;
300   };
301   std::optional<llvm::ErrorOr<WorkingDirectory>> WD;
302 };
303 
304 } // namespace
305 
306 ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
307   SmallString<256> Storage;
308   sys::fs::file_status RealStatus;
309   if (std::error_code EC =
310           sys::fs::status(adjustPath(Path, Storage), RealStatus))
311     return EC;
312   return Status::copyWithNewName(RealStatus, Path);
313 }
314 
315 ErrorOr<std::unique_ptr<File>>
316 RealFileSystem::openFileForRead(const Twine &Name) {
317   SmallString<256> RealName, Storage;
318   Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead(
319       adjustPath(Name, Storage), sys::fs::OF_None, &RealName);
320   if (!FDOrErr)
321     return errorToErrorCode(FDOrErr.takeError());
322   return std::unique_ptr<File>(
323       new RealFile(*FDOrErr, Name.str(), RealName.str()));
324 }
325 
326 llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const {
327   if (WD && *WD)
328     return std::string(WD->get().Specified.str());
329   if (WD)
330     return WD->getError();
331 
332   SmallString<128> Dir;
333   if (std::error_code EC = llvm::sys::fs::current_path(Dir))
334     return EC;
335   return std::string(Dir.str());
336 }
337 
338 std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
339   if (!WD)
340     return llvm::sys::fs::set_current_path(Path);
341 
342   SmallString<128> Absolute, Resolved, Storage;
343   adjustPath(Path, Storage).toVector(Absolute);
344   bool IsDir;
345   if (auto Err = llvm::sys::fs::is_directory(Absolute, IsDir))
346     return Err;
347   if (!IsDir)
348     return std::make_error_code(std::errc::not_a_directory);
349   if (auto Err = llvm::sys::fs::real_path(Absolute, Resolved))
350     return Err;
351   WD = WorkingDirectory{Absolute, Resolved};
352   return std::error_code();
353 }
354 
355 std::error_code RealFileSystem::isLocal(const Twine &Path, bool &Result) {
356   SmallString<256> Storage;
357   return llvm::sys::fs::is_local(adjustPath(Path, Storage), Result);
358 }
359 
360 std::error_code
361 RealFileSystem::getRealPath(const Twine &Path,
362                             SmallVectorImpl<char> &Output) const {
363   SmallString<256> Storage;
364   return llvm::sys::fs::real_path(adjustPath(Path, Storage), Output);
365 }
366 
367 void RealFileSystem::printImpl(raw_ostream &OS, PrintType Type,
368                                unsigned IndentLevel) const {
369   printIndent(OS, IndentLevel);
370   OS << "RealFileSystem using ";
371   if (WD)
372     OS << "own";
373   else
374     OS << "process";
375   OS << " CWD\n";
376 }
377 
378 IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
379   static IntrusiveRefCntPtr<FileSystem> FS(new RealFileSystem(true));
380   return FS;
381 }
382 
383 std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() {
384   return std::make_unique<RealFileSystem>(false);
385 }
386 
387 namespace {
388 
389 class RealFSDirIter : public llvm::vfs::detail::DirIterImpl {
390   llvm::sys::fs::directory_iterator Iter;
391 
392 public:
393   RealFSDirIter(const Twine &Path, std::error_code &EC) : Iter(Path, EC) {
394     if (Iter != llvm::sys::fs::directory_iterator())
395       CurrentEntry = directory_entry(Iter->path(), Iter->type());
396   }
397 
398   std::error_code increment() override {
399     std::error_code EC;
400     Iter.increment(EC);
401     CurrentEntry = (Iter == llvm::sys::fs::directory_iterator())
402                        ? directory_entry()
403                        : directory_entry(Iter->path(), Iter->type());
404     return EC;
405   }
406 };
407 
408 } // namespace
409 
410 directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
411                                              std::error_code &EC) {
412   SmallString<128> Storage;
413   return directory_iterator(
414       std::make_shared<RealFSDirIter>(adjustPath(Dir, Storage), EC));
415 }
416 
417 //===-----------------------------------------------------------------------===/
418 // OverlayFileSystem implementation
419 //===-----------------------------------------------------------------------===/
420 
421 OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
422   FSList.push_back(std::move(BaseFS));
423 }
424 
425 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
426   FSList.push_back(FS);
427   // Synchronize added file systems by duplicating the working directory from
428   // the first one in the list.
429   FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get());
430 }
431 
432 ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
433   // FIXME: handle symlinks that cross file systems
434   for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
435     ErrorOr<Status> Status = (*I)->status(Path);
436     if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
437       return Status;
438   }
439   return make_error_code(llvm::errc::no_such_file_or_directory);
440 }
441 
442 ErrorOr<std::unique_ptr<File>>
443 OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
444   // FIXME: handle symlinks that cross file systems
445   for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
446     auto Result = (*I)->openFileForRead(Path);
447     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
448       return Result;
449   }
450   return make_error_code(llvm::errc::no_such_file_or_directory);
451 }
452 
453 llvm::ErrorOr<std::string>
454 OverlayFileSystem::getCurrentWorkingDirectory() const {
455   // All file systems are synchronized, just take the first working directory.
456   return FSList.front()->getCurrentWorkingDirectory();
457 }
458 
459 std::error_code
460 OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
461   for (auto &FS : FSList)
462     if (std::error_code EC = FS->setCurrentWorkingDirectory(Path))
463       return EC;
464   return {};
465 }
466 
467 std::error_code OverlayFileSystem::isLocal(const Twine &Path, bool &Result) {
468   for (auto &FS : FSList)
469     if (FS->exists(Path))
470       return FS->isLocal(Path, Result);
471   return errc::no_such_file_or_directory;
472 }
473 
474 std::error_code
475 OverlayFileSystem::getRealPath(const Twine &Path,
476                                SmallVectorImpl<char> &Output) const {
477   for (const auto &FS : FSList)
478     if (FS->exists(Path))
479       return FS->getRealPath(Path, Output);
480   return errc::no_such_file_or_directory;
481 }
482 
483 void OverlayFileSystem::printImpl(raw_ostream &OS, PrintType Type,
484                                   unsigned IndentLevel) const {
485   printIndent(OS, IndentLevel);
486   OS << "OverlayFileSystem\n";
487   if (Type == PrintType::Summary)
488     return;
489 
490   if (Type == PrintType::Contents)
491     Type = PrintType::Summary;
492   for (auto FS : overlays_range())
493     FS->print(OS, Type, IndentLevel + 1);
494 }
495 
496 llvm::vfs::detail::DirIterImpl::~DirIterImpl() = default;
497 
498 namespace {
499 
500 /// Combines and deduplicates directory entries across multiple file systems.
501 class CombiningDirIterImpl : public llvm::vfs::detail::DirIterImpl {
502   using FileSystemPtr = llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>;
503 
504   /// Iterators to combine, processed in reverse order.
505   SmallVector<directory_iterator, 8> IterList;
506   /// The iterator currently being traversed.
507   directory_iterator CurrentDirIter;
508   /// The set of names already returned as entries.
509   llvm::StringSet<> SeenNames;
510 
511   /// Sets \c CurrentDirIter to the next iterator in the list, or leaves it as
512   /// is (at its end position) if we've already gone through them all.
513   std::error_code incrementIter(bool IsFirstTime) {
514     while (!IterList.empty()) {
515       CurrentDirIter = IterList.back();
516       IterList.pop_back();
517       if (CurrentDirIter != directory_iterator())
518         break; // found
519     }
520 
521     if (IsFirstTime && CurrentDirIter == directory_iterator())
522       return errc::no_such_file_or_directory;
523     return {};
524   }
525 
526   std::error_code incrementDirIter(bool IsFirstTime) {
527     assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
528            "incrementing past end");
529     std::error_code EC;
530     if (!IsFirstTime)
531       CurrentDirIter.increment(EC);
532     if (!EC && CurrentDirIter == directory_iterator())
533       EC = incrementIter(IsFirstTime);
534     return EC;
535   }
536 
537   std::error_code incrementImpl(bool IsFirstTime) {
538     while (true) {
539       std::error_code EC = incrementDirIter(IsFirstTime);
540       if (EC || CurrentDirIter == directory_iterator()) {
541         CurrentEntry = directory_entry();
542         return EC;
543       }
544       CurrentEntry = *CurrentDirIter;
545       StringRef Name = llvm::sys::path::filename(CurrentEntry.path());
546       if (SeenNames.insert(Name).second)
547         return EC; // name not seen before
548     }
549     llvm_unreachable("returned above");
550   }
551 
552 public:
553   CombiningDirIterImpl(ArrayRef<FileSystemPtr> FileSystems, std::string Dir,
554                        std::error_code &EC) {
555     for (auto FS : FileSystems) {
556       std::error_code FEC;
557       directory_iterator Iter = FS->dir_begin(Dir, FEC);
558       if (FEC && FEC != errc::no_such_file_or_directory) {
559         EC = FEC;
560         return;
561       }
562       if (!FEC)
563         IterList.push_back(Iter);
564     }
565     EC = incrementImpl(true);
566   }
567 
568   CombiningDirIterImpl(ArrayRef<directory_iterator> DirIters,
569                        std::error_code &EC)
570       : IterList(DirIters.begin(), DirIters.end()) {
571     EC = incrementImpl(true);
572   }
573 
574   std::error_code increment() override { return incrementImpl(false); }
575 };
576 
577 } // namespace
578 
579 directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
580                                                 std::error_code &EC) {
581   directory_iterator Combined = directory_iterator(
582       std::make_shared<CombiningDirIterImpl>(FSList, Dir.str(), EC));
583   if (EC)
584     return {};
585   return Combined;
586 }
587 
588 void ProxyFileSystem::anchor() {}
589 
590 namespace llvm {
591 namespace vfs {
592 
593 namespace detail {
594 
595 enum InMemoryNodeKind {
596   IME_File,
597   IME_Directory,
598   IME_HardLink,
599   IME_SymbolicLink,
600 };
601 
602 /// The in memory file system is a tree of Nodes. Every node can either be a
603 /// file, symlink, hardlink or a directory.
604 class InMemoryNode {
605   InMemoryNodeKind Kind;
606   std::string FileName;
607 
608 public:
609   InMemoryNode(llvm::StringRef FileName, InMemoryNodeKind Kind)
610       : Kind(Kind), FileName(std::string(llvm::sys::path::filename(FileName))) {
611   }
612   virtual ~InMemoryNode() = default;
613 
614   /// Return the \p Status for this node. \p RequestedName should be the name
615   /// through which the caller referred to this node. It will override
616   /// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
617   virtual Status getStatus(const Twine &RequestedName) const = 0;
618 
619   /// Get the filename of this node (the name without the directory part).
620   StringRef getFileName() const { return FileName; }
621   InMemoryNodeKind getKind() const { return Kind; }
622   virtual std::string toString(unsigned Indent) const = 0;
623 };
624 
625 class InMemoryFile : public InMemoryNode {
626   Status Stat;
627   std::unique_ptr<llvm::MemoryBuffer> Buffer;
628 
629 public:
630   InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer)
631       : InMemoryNode(Stat.getName(), IME_File), Stat(std::move(Stat)),
632         Buffer(std::move(Buffer)) {}
633 
634   Status getStatus(const Twine &RequestedName) const override {
635     return Status::copyWithNewName(Stat, RequestedName);
636   }
637   llvm::MemoryBuffer *getBuffer() const { return Buffer.get(); }
638 
639   std::string toString(unsigned Indent) const override {
640     return (std::string(Indent, ' ') + Stat.getName() + "\n").str();
641   }
642 
643   static bool classof(const InMemoryNode *N) {
644     return N->getKind() == IME_File;
645   }
646 };
647 
648 namespace {
649 
650 class InMemoryHardLink : public InMemoryNode {
651   const InMemoryFile &ResolvedFile;
652 
653 public:
654   InMemoryHardLink(StringRef Path, const InMemoryFile &ResolvedFile)
655       : InMemoryNode(Path, IME_HardLink), ResolvedFile(ResolvedFile) {}
656   const InMemoryFile &getResolvedFile() const { return ResolvedFile; }
657 
658   Status getStatus(const Twine &RequestedName) const override {
659     return ResolvedFile.getStatus(RequestedName);
660   }
661 
662   std::string toString(unsigned Indent) const override {
663     return std::string(Indent, ' ') + "HardLink to -> " +
664            ResolvedFile.toString(0);
665   }
666 
667   static bool classof(const InMemoryNode *N) {
668     return N->getKind() == IME_HardLink;
669   }
670 };
671 
672 class InMemorySymbolicLink : public InMemoryNode {
673   std::string TargetPath;
674   Status Stat;
675 
676 public:
677   InMemorySymbolicLink(StringRef Path, StringRef TargetPath, Status Stat)
678       : InMemoryNode(Path, IME_SymbolicLink), TargetPath(std::move(TargetPath)),
679         Stat(Stat) {}
680 
681   std::string toString(unsigned Indent) const override {
682     return std::string(Indent, ' ') + "SymbolicLink to -> " + TargetPath;
683   }
684 
685   Status getStatus(const Twine &RequestedName) const override {
686     return Status::copyWithNewName(Stat, RequestedName);
687   }
688 
689   StringRef getTargetPath() const { return TargetPath; }
690 
691   static bool classof(const InMemoryNode *N) {
692     return N->getKind() == IME_SymbolicLink;
693   }
694 };
695 
696 /// Adapt a InMemoryFile for VFS' File interface.  The goal is to make
697 /// \p InMemoryFileAdaptor mimic as much as possible the behavior of
698 /// \p RealFile.
699 class InMemoryFileAdaptor : public File {
700   const InMemoryFile &Node;
701   /// The name to use when returning a Status for this file.
702   std::string RequestedName;
703 
704 public:
705   explicit InMemoryFileAdaptor(const InMemoryFile &Node,
706                                std::string RequestedName)
707       : Node(Node), RequestedName(std::move(RequestedName)) {}
708 
709   llvm::ErrorOr<Status> status() override {
710     return Node.getStatus(RequestedName);
711   }
712 
713   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
714   getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
715             bool IsVolatile) override {
716     llvm::MemoryBuffer *Buf = Node.getBuffer();
717     return llvm::MemoryBuffer::getMemBuffer(
718         Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator);
719   }
720 
721   std::error_code close() override { return {}; }
722 
723   void setPath(const Twine &Path) override { RequestedName = Path.str(); }
724 };
725 } // namespace
726 
727 class InMemoryDirectory : public InMemoryNode {
728   Status Stat;
729   std::map<std::string, std::unique_ptr<InMemoryNode>> Entries;
730 
731 public:
732   InMemoryDirectory(Status Stat)
733       : InMemoryNode(Stat.getName(), IME_Directory), Stat(std::move(Stat)) {}
734 
735   /// Return the \p Status for this node. \p RequestedName should be the name
736   /// through which the caller referred to this node. It will override
737   /// \p Status::Name in the return value, to mimic the behavior of \p RealFile.
738   Status getStatus(const Twine &RequestedName) const override {
739     return Status::copyWithNewName(Stat, RequestedName);
740   }
741 
742   UniqueID getUniqueID() const { return Stat.getUniqueID(); }
743 
744   InMemoryNode *getChild(StringRef Name) const {
745     auto I = Entries.find(Name.str());
746     if (I != Entries.end())
747       return I->second.get();
748     return nullptr;
749   }
750 
751   InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) {
752     return Entries.insert(make_pair(Name.str(), std::move(Child)))
753         .first->second.get();
754   }
755 
756   using const_iterator = decltype(Entries)::const_iterator;
757 
758   const_iterator begin() const { return Entries.begin(); }
759   const_iterator end() const { return Entries.end(); }
760 
761   std::string toString(unsigned Indent) const override {
762     std::string Result =
763         (std::string(Indent, ' ') + Stat.getName() + "\n").str();
764     for (const auto &Entry : Entries)
765       Result += Entry.second->toString(Indent + 2);
766     return Result;
767   }
768 
769   static bool classof(const InMemoryNode *N) {
770     return N->getKind() == IME_Directory;
771   }
772 };
773 
774 } // namespace detail
775 
776 // The UniqueID of in-memory files is derived from path and content.
777 // This avoids difficulties in creating exactly equivalent in-memory FSes,
778 // as often needed in multithreaded programs.
779 static sys::fs::UniqueID getUniqueID(hash_code Hash) {
780   return sys::fs::UniqueID(std::numeric_limits<uint64_t>::max(),
781                            uint64_t(size_t(Hash)));
782 }
783 static sys::fs::UniqueID getFileID(sys::fs::UniqueID Parent,
784                                    llvm::StringRef Name,
785                                    llvm::StringRef Contents) {
786   return getUniqueID(llvm::hash_combine(Parent.getFile(), Name, Contents));
787 }
788 static sys::fs::UniqueID getDirectoryID(sys::fs::UniqueID Parent,
789                                         llvm::StringRef Name) {
790   return getUniqueID(llvm::hash_combine(Parent.getFile(), Name));
791 }
792 
793 Status detail::NewInMemoryNodeInfo::makeStatus() const {
794   UniqueID UID =
795       (Type == sys::fs::file_type::directory_file)
796           ? getDirectoryID(DirUID, Name)
797           : getFileID(DirUID, Name, Buffer ? Buffer->getBuffer() : "");
798 
799   return Status(Path, UID, llvm::sys::toTimePoint(ModificationTime), User,
800                 Group, Buffer ? Buffer->getBufferSize() : 0, Type, Perms);
801 }
802 
803 InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths)
804     : Root(new detail::InMemoryDirectory(
805           Status("", getDirectoryID(llvm::sys::fs::UniqueID(), ""),
806                  llvm::sys::TimePoint<>(), 0, 0, 0,
807                  llvm::sys::fs::file_type::directory_file,
808                  llvm::sys::fs::perms::all_all))),
809       UseNormalizedPaths(UseNormalizedPaths) {}
810 
811 InMemoryFileSystem::~InMemoryFileSystem() = default;
812 
813 std::string InMemoryFileSystem::toString() const {
814   return Root->toString(/*Indent=*/0);
815 }
816 
817 bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
818                                  std::unique_ptr<llvm::MemoryBuffer> Buffer,
819                                  std::optional<uint32_t> User,
820                                  std::optional<uint32_t> Group,
821                                  std::optional<llvm::sys::fs::file_type> Type,
822                                  std::optional<llvm::sys::fs::perms> Perms,
823                                  MakeNodeFn MakeNode) {
824   SmallString<128> Path;
825   P.toVector(Path);
826 
827   // Fix up relative paths. This just prepends the current working directory.
828   std::error_code EC = makeAbsolute(Path);
829   assert(!EC);
830   (void)EC;
831 
832   if (useNormalizedPaths())
833     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
834 
835   if (Path.empty())
836     return false;
837 
838   detail::InMemoryDirectory *Dir = Root.get();
839   auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
840   const auto ResolvedUser = User.value_or(0);
841   const auto ResolvedGroup = Group.value_or(0);
842   const auto ResolvedType = Type.value_or(sys::fs::file_type::regular_file);
843   const auto ResolvedPerms = Perms.value_or(sys::fs::all_all);
844   // Any intermediate directories we create should be accessible by
845   // the owner, even if Perms says otherwise for the final path.
846   const auto NewDirectoryPerms = ResolvedPerms | sys::fs::owner_all;
847   while (true) {
848     StringRef Name = *I;
849     detail::InMemoryNode *Node = Dir->getChild(Name);
850     ++I;
851     if (!Node) {
852       if (I == E) {
853         // End of the path.
854         Dir->addChild(
855             Name, MakeNode({Dir->getUniqueID(), Path, Name, ModificationTime,
856                             std::move(Buffer), ResolvedUser, ResolvedGroup,
857                             ResolvedType, ResolvedPerms}));
858         return true;
859       }
860 
861       // Create a new directory. Use the path up to here.
862       Status Stat(
863           StringRef(Path.str().begin(), Name.end() - Path.str().begin()),
864           getDirectoryID(Dir->getUniqueID(), Name),
865           llvm::sys::toTimePoint(ModificationTime), ResolvedUser, ResolvedGroup,
866           0, sys::fs::file_type::directory_file, NewDirectoryPerms);
867       Dir = cast<detail::InMemoryDirectory>(Dir->addChild(
868           Name, std::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
869       continue;
870     }
871 
872     if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) {
873       Dir = NewDir;
874     } else {
875       assert((isa<detail::InMemoryFile>(Node) ||
876               isa<detail::InMemoryHardLink>(Node)) &&
877              "Must be either file, hardlink or directory!");
878 
879       // Trying to insert a directory in place of a file.
880       if (I != E)
881         return false;
882 
883       // Return false only if the new file is different from the existing one.
884       if (auto Link = dyn_cast<detail::InMemoryHardLink>(Node)) {
885         return Link->getResolvedFile().getBuffer()->getBuffer() ==
886                Buffer->getBuffer();
887       }
888       return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() ==
889              Buffer->getBuffer();
890     }
891   }
892 }
893 
894 bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime,
895                                  std::unique_ptr<llvm::MemoryBuffer> Buffer,
896                                  std::optional<uint32_t> User,
897                                  std::optional<uint32_t> Group,
898                                  std::optional<llvm::sys::fs::file_type> Type,
899                                  std::optional<llvm::sys::fs::perms> Perms) {
900   return addFile(P, ModificationTime, std::move(Buffer), User, Group, Type,
901                  Perms,
902                  [](detail::NewInMemoryNodeInfo NNI)
903                      -> std::unique_ptr<detail::InMemoryNode> {
904                    Status Stat = NNI.makeStatus();
905                    if (Stat.getType() == sys::fs::file_type::directory_file)
906                      return std::make_unique<detail::InMemoryDirectory>(Stat);
907                    return std::make_unique<detail::InMemoryFile>(
908                        Stat, std::move(NNI.Buffer));
909                  });
910 }
911 
912 bool InMemoryFileSystem::addFileNoOwn(
913     const Twine &P, time_t ModificationTime,
914     const llvm::MemoryBufferRef &Buffer, std::optional<uint32_t> User,
915     std::optional<uint32_t> Group, std::optional<llvm::sys::fs::file_type> Type,
916     std::optional<llvm::sys::fs::perms> Perms) {
917   return addFile(P, ModificationTime, llvm::MemoryBuffer::getMemBuffer(Buffer),
918                  std::move(User), std::move(Group), std::move(Type),
919                  std::move(Perms),
920                  [](detail::NewInMemoryNodeInfo NNI)
921                      -> std::unique_ptr<detail::InMemoryNode> {
922                    Status Stat = NNI.makeStatus();
923                    if (Stat.getType() == sys::fs::file_type::directory_file)
924                      return std::make_unique<detail::InMemoryDirectory>(Stat);
925                    return std::make_unique<detail::InMemoryFile>(
926                        Stat, std::move(NNI.Buffer));
927                  });
928 }
929 
930 detail::NamedNodeOrError
931 InMemoryFileSystem::lookupNode(const Twine &P, bool FollowFinalSymlink,
932                                size_t SymlinkDepth) const {
933   SmallString<128> Path;
934   P.toVector(Path);
935 
936   // Fix up relative paths. This just prepends the current working directory.
937   std::error_code EC = makeAbsolute(Path);
938   assert(!EC);
939   (void)EC;
940 
941   if (useNormalizedPaths())
942     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
943 
944   const detail::InMemoryDirectory *Dir = Root.get();
945   if (Path.empty())
946     return detail::NamedNodeOrError(Path, Dir);
947 
948   auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path);
949   while (true) {
950     detail::InMemoryNode *Node = Dir->getChild(*I);
951     ++I;
952     if (!Node)
953       return errc::no_such_file_or_directory;
954 
955     if (auto Symlink = dyn_cast<detail::InMemorySymbolicLink>(Node)) {
956       // If we're at the end of the path, and we're not following through
957       // terminal symlinks, then we're done.
958       if (I == E && !FollowFinalSymlink)
959         return detail::NamedNodeOrError(Path, Symlink);
960 
961       if (SymlinkDepth > InMemoryFileSystem::MaxSymlinkDepth)
962         return errc::no_such_file_or_directory;
963 
964       SmallString<128> TargetPath = Symlink->getTargetPath();
965       if (std::error_code EC = makeAbsolute(TargetPath))
966         return EC;
967 
968       // Keep going with the target. We always want to follow symlinks here
969       // because we're either at the end of a path that we want to follow, or
970       // not at the end of a path, in which case we need to follow the symlink
971       // regardless.
972       auto Target =
973           lookupNode(TargetPath, /*FollowFinalSymlink=*/true, SymlinkDepth + 1);
974       if (!Target || I == E)
975         return Target;
976 
977       if (!isa<detail::InMemoryDirectory>(*Target))
978         return errc::no_such_file_or_directory;
979 
980       // Otherwise, continue on the search in the symlinked directory.
981       Dir = cast<detail::InMemoryDirectory>(*Target);
982       continue;
983     }
984 
985     // Return the file if it's at the end of the path.
986     if (auto File = dyn_cast<detail::InMemoryFile>(Node)) {
987       if (I == E)
988         return detail::NamedNodeOrError(Path, File);
989       return errc::no_such_file_or_directory;
990     }
991 
992     // If Node is HardLink then return the resolved file.
993     if (auto File = dyn_cast<detail::InMemoryHardLink>(Node)) {
994       if (I == E)
995         return detail::NamedNodeOrError(Path, &File->getResolvedFile());
996       return errc::no_such_file_or_directory;
997     }
998     // Traverse directories.
999     Dir = cast<detail::InMemoryDirectory>(Node);
1000     if (I == E)
1001       return detail::NamedNodeOrError(Path, Dir);
1002   }
1003 }
1004 
1005 bool InMemoryFileSystem::addHardLink(const Twine &NewLink,
1006                                      const Twine &Target) {
1007   auto NewLinkNode = lookupNode(NewLink, /*FollowFinalSymlink=*/false);
1008   // Whether symlinks in the hardlink target are followed is
1009   // implementation-defined in POSIX.
1010   // We're following symlinks here to be consistent with macOS.
1011   auto TargetNode = lookupNode(Target, /*FollowFinalSymlink=*/true);
1012   // FromPath must not have been added before. ToPath must have been added
1013   // before. Resolved ToPath must be a File.
1014   if (!TargetNode || NewLinkNode || !isa<detail::InMemoryFile>(*TargetNode))
1015     return false;
1016   return addFile(NewLink, 0, nullptr, std::nullopt, std::nullopt, std::nullopt,
1017                  std::nullopt, [&](detail::NewInMemoryNodeInfo NNI) {
1018                    return std::make_unique<detail::InMemoryHardLink>(
1019                        NNI.Path.str(),
1020                        *cast<detail::InMemoryFile>(*TargetNode));
1021                  });
1022 }
1023 
1024 bool InMemoryFileSystem::addSymbolicLink(
1025     const Twine &NewLink, const Twine &Target, time_t ModificationTime,
1026     std::optional<uint32_t> User, std::optional<uint32_t> Group,
1027     std::optional<llvm::sys::fs::perms> Perms) {
1028   auto NewLinkNode = lookupNode(NewLink, /*FollowFinalSymlink=*/false);
1029   if (NewLinkNode)
1030     return false;
1031 
1032   SmallString<128> NewLinkStr, TargetStr;
1033   NewLink.toVector(NewLinkStr);
1034   Target.toVector(TargetStr);
1035 
1036   return addFile(NewLinkStr, ModificationTime, nullptr, User, Group,
1037                  sys::fs::file_type::symlink_file, Perms,
1038                  [&](detail::NewInMemoryNodeInfo NNI) {
1039                    return std::make_unique<detail::InMemorySymbolicLink>(
1040                        NewLinkStr, TargetStr, NNI.makeStatus());
1041                  });
1042 }
1043 
1044 llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) {
1045   auto Node = lookupNode(Path, /*FollowFinalSymlink=*/true);
1046   if (Node)
1047     return (*Node)->getStatus(Path);
1048   return Node.getError();
1049 }
1050 
1051 llvm::ErrorOr<std::unique_ptr<File>>
1052 InMemoryFileSystem::openFileForRead(const Twine &Path) {
1053   auto Node = lookupNode(Path,/*FollowFinalSymlink=*/true);
1054   if (!Node)
1055     return Node.getError();
1056 
1057   // When we have a file provide a heap-allocated wrapper for the memory buffer
1058   // to match the ownership semantics for File.
1059   if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
1060     return std::unique_ptr<File>(
1061         new detail::InMemoryFileAdaptor(*F, Path.str()));
1062 
1063   // FIXME: errc::not_a_file?
1064   return make_error_code(llvm::errc::invalid_argument);
1065 }
1066 
1067 /// Adaptor from InMemoryDir::iterator to directory_iterator.
1068 class InMemoryFileSystem::DirIterator : public llvm::vfs::detail::DirIterImpl {
1069   const InMemoryFileSystem *FS;
1070   detail::InMemoryDirectory::const_iterator I;
1071   detail::InMemoryDirectory::const_iterator E;
1072   std::string RequestedDirName;
1073 
1074   void setCurrentEntry() {
1075     if (I != E) {
1076       SmallString<256> Path(RequestedDirName);
1077       llvm::sys::path::append(Path, I->second->getFileName());
1078       sys::fs::file_type Type = sys::fs::file_type::type_unknown;
1079       switch (I->second->getKind()) {
1080       case detail::IME_File:
1081       case detail::IME_HardLink:
1082         Type = sys::fs::file_type::regular_file;
1083         break;
1084       case detail::IME_Directory:
1085         Type = sys::fs::file_type::directory_file;
1086         break;
1087       case detail::IME_SymbolicLink:
1088         if (auto SymlinkTarget =
1089                 FS->lookupNode(Path, /*FollowFinalSymlink=*/true)) {
1090           Path = SymlinkTarget.getName();
1091           Type = (*SymlinkTarget)->getStatus(Path).getType();
1092         }
1093         break;
1094       }
1095       CurrentEntry = directory_entry(std::string(Path.str()), Type);
1096     } else {
1097       // When we're at the end, make CurrentEntry invalid and DirIterImpl will
1098       // do the rest.
1099       CurrentEntry = directory_entry();
1100     }
1101   }
1102 
1103 public:
1104   DirIterator() = default;
1105 
1106   DirIterator(const InMemoryFileSystem *FS,
1107               const detail::InMemoryDirectory &Dir,
1108               std::string RequestedDirName)
1109       : FS(FS), I(Dir.begin()), E(Dir.end()),
1110         RequestedDirName(std::move(RequestedDirName)) {
1111     setCurrentEntry();
1112   }
1113 
1114   std::error_code increment() override {
1115     ++I;
1116     setCurrentEntry();
1117     return {};
1118   }
1119 };
1120 
1121 directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
1122                                                  std::error_code &EC) {
1123   auto Node = lookupNode(Dir, /*FollowFinalSymlink=*/true);
1124   if (!Node) {
1125     EC = Node.getError();
1126     return directory_iterator(std::make_shared<DirIterator>());
1127   }
1128 
1129   if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node))
1130     return directory_iterator(
1131         std::make_shared<DirIterator>(this, *DirNode, Dir.str()));
1132 
1133   EC = make_error_code(llvm::errc::not_a_directory);
1134   return directory_iterator(std::make_shared<DirIterator>());
1135 }
1136 
1137 std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
1138   SmallString<128> Path;
1139   P.toVector(Path);
1140 
1141   // Fix up relative paths. This just prepends the current working directory.
1142   std::error_code EC = makeAbsolute(Path);
1143   assert(!EC);
1144   (void)EC;
1145 
1146   if (useNormalizedPaths())
1147     llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
1148 
1149   if (!Path.empty())
1150     WorkingDirectory = std::string(Path.str());
1151   return {};
1152 }
1153 
1154 std::error_code
1155 InMemoryFileSystem::getRealPath(const Twine &Path,
1156                                 SmallVectorImpl<char> &Output) const {
1157   auto CWD = getCurrentWorkingDirectory();
1158   if (!CWD || CWD->empty())
1159     return errc::operation_not_permitted;
1160   Path.toVector(Output);
1161   if (auto EC = makeAbsolute(Output))
1162     return EC;
1163   llvm::sys::path::remove_dots(Output, /*remove_dot_dot=*/true);
1164   return {};
1165 }
1166 
1167 std::error_code InMemoryFileSystem::isLocal(const Twine &Path, bool &Result) {
1168   Result = false;
1169   return {};
1170 }
1171 
1172 void InMemoryFileSystem::printImpl(raw_ostream &OS, PrintType PrintContents,
1173                                    unsigned IndentLevel) const {
1174   printIndent(OS, IndentLevel);
1175   OS << "InMemoryFileSystem\n";
1176 }
1177 
1178 } // namespace vfs
1179 } // namespace llvm
1180 
1181 //===-----------------------------------------------------------------------===/
1182 // RedirectingFileSystem implementation
1183 //===-----------------------------------------------------------------------===/
1184 
1185 namespace {
1186 
1187 static llvm::sys::path::Style getExistingStyle(llvm::StringRef Path) {
1188   // Detect the path style in use by checking the first separator.
1189   llvm::sys::path::Style style = llvm::sys::path::Style::native;
1190   const size_t n = Path.find_first_of("/\\");
1191   // Can't distinguish between posix and windows_slash here.
1192   if (n != static_cast<size_t>(-1))
1193     style = (Path[n] == '/') ? llvm::sys::path::Style::posix
1194                              : llvm::sys::path::Style::windows_backslash;
1195   return style;
1196 }
1197 
1198 /// Removes leading "./" as well as path components like ".." and ".".
1199 static llvm::SmallString<256> canonicalize(llvm::StringRef Path) {
1200   // First detect the path style in use by checking the first separator.
1201   llvm::sys::path::Style style = getExistingStyle(Path);
1202 
1203   // Now remove the dots.  Explicitly specifying the path style prevents the
1204   // direction of the slashes from changing.
1205   llvm::SmallString<256> result =
1206       llvm::sys::path::remove_leading_dotslash(Path, style);
1207   llvm::sys::path::remove_dots(result, /*remove_dot_dot=*/true, style);
1208   return result;
1209 }
1210 
1211 /// Whether the error and entry specify a file/directory that was not found.
1212 static bool isFileNotFound(std::error_code EC,
1213                            RedirectingFileSystem::Entry *E = nullptr) {
1214   if (E && !isa<RedirectingFileSystem::DirectoryRemapEntry>(E))
1215     return false;
1216   return EC == llvm::errc::no_such_file_or_directory;
1217 }
1218 
1219 } // anonymous namespace
1220 
1221 
1222 RedirectingFileSystem::RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> FS)
1223     : ExternalFS(std::move(FS)) {
1224   if (ExternalFS)
1225     if (auto ExternalWorkingDirectory =
1226             ExternalFS->getCurrentWorkingDirectory()) {
1227       WorkingDirectory = *ExternalWorkingDirectory;
1228     }
1229 }
1230 
1231 /// Directory iterator implementation for \c RedirectingFileSystem's
1232 /// directory entries.
1233 class llvm::vfs::RedirectingFSDirIterImpl
1234     : public llvm::vfs::detail::DirIterImpl {
1235   std::string Dir;
1236   RedirectingFileSystem::DirectoryEntry::iterator Current, End;
1237 
1238   std::error_code incrementImpl(bool IsFirstTime) {
1239     assert((IsFirstTime || Current != End) && "cannot iterate past end");
1240     if (!IsFirstTime)
1241       ++Current;
1242     if (Current != End) {
1243       SmallString<128> PathStr(Dir);
1244       llvm::sys::path::append(PathStr, (*Current)->getName());
1245       sys::fs::file_type Type = sys::fs::file_type::type_unknown;
1246       switch ((*Current)->getKind()) {
1247       case RedirectingFileSystem::EK_Directory:
1248         [[fallthrough]];
1249       case RedirectingFileSystem::EK_DirectoryRemap:
1250         Type = sys::fs::file_type::directory_file;
1251         break;
1252       case RedirectingFileSystem::EK_File:
1253         Type = sys::fs::file_type::regular_file;
1254         break;
1255       }
1256       CurrentEntry = directory_entry(std::string(PathStr.str()), Type);
1257     } else {
1258       CurrentEntry = directory_entry();
1259     }
1260     return {};
1261   };
1262 
1263 public:
1264   RedirectingFSDirIterImpl(
1265       const Twine &Path, RedirectingFileSystem::DirectoryEntry::iterator Begin,
1266       RedirectingFileSystem::DirectoryEntry::iterator End, std::error_code &EC)
1267       : Dir(Path.str()), Current(Begin), End(End) {
1268     EC = incrementImpl(/*IsFirstTime=*/true);
1269   }
1270 
1271   std::error_code increment() override {
1272     return incrementImpl(/*IsFirstTime=*/false);
1273   }
1274 };
1275 
1276 namespace {
1277 /// Directory iterator implementation for \c RedirectingFileSystem's
1278 /// directory remap entries that maps the paths reported by the external
1279 /// file system's directory iterator back to the virtual directory's path.
1280 class RedirectingFSDirRemapIterImpl : public llvm::vfs::detail::DirIterImpl {
1281   std::string Dir;
1282   llvm::sys::path::Style DirStyle;
1283   llvm::vfs::directory_iterator ExternalIter;
1284 
1285 public:
1286   RedirectingFSDirRemapIterImpl(std::string DirPath,
1287                                 llvm::vfs::directory_iterator ExtIter)
1288       : Dir(std::move(DirPath)), DirStyle(getExistingStyle(Dir)),
1289         ExternalIter(ExtIter) {
1290     if (ExternalIter != llvm::vfs::directory_iterator())
1291       setCurrentEntry();
1292   }
1293 
1294   void setCurrentEntry() {
1295     StringRef ExternalPath = ExternalIter->path();
1296     llvm::sys::path::Style ExternalStyle = getExistingStyle(ExternalPath);
1297     StringRef File = llvm::sys::path::filename(ExternalPath, ExternalStyle);
1298 
1299     SmallString<128> NewPath(Dir);
1300     llvm::sys::path::append(NewPath, DirStyle, File);
1301 
1302     CurrentEntry = directory_entry(std::string(NewPath), ExternalIter->type());
1303   }
1304 
1305   std::error_code increment() override {
1306     std::error_code EC;
1307     ExternalIter.increment(EC);
1308     if (!EC && ExternalIter != llvm::vfs::directory_iterator())
1309       setCurrentEntry();
1310     else
1311       CurrentEntry = directory_entry();
1312     return EC;
1313   }
1314 };
1315 } // namespace
1316 
1317 llvm::ErrorOr<std::string>
1318 RedirectingFileSystem::getCurrentWorkingDirectory() const {
1319   return WorkingDirectory;
1320 }
1321 
1322 std::error_code
1323 RedirectingFileSystem::setCurrentWorkingDirectory(const Twine &Path) {
1324   // Don't change the working directory if the path doesn't exist.
1325   if (!exists(Path))
1326     return errc::no_such_file_or_directory;
1327 
1328   SmallString<128> AbsolutePath;
1329   Path.toVector(AbsolutePath);
1330   if (std::error_code EC = makeAbsolute(AbsolutePath))
1331     return EC;
1332   WorkingDirectory = std::string(AbsolutePath.str());
1333   return {};
1334 }
1335 
1336 std::error_code RedirectingFileSystem::isLocal(const Twine &Path_,
1337                                                bool &Result) {
1338   SmallString<256> Path;
1339   Path_.toVector(Path);
1340 
1341   if (std::error_code EC = makeCanonical(Path))
1342     return {};
1343 
1344   return ExternalFS->isLocal(Path, Result);
1345 }
1346 
1347 std::error_code RedirectingFileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const {
1348   // is_absolute(..., Style::windows_*) accepts paths with both slash types.
1349   if (llvm::sys::path::is_absolute(Path, llvm::sys::path::Style::posix) ||
1350       llvm::sys::path::is_absolute(Path,
1351                                    llvm::sys::path::Style::windows_backslash))
1352     // This covers windows absolute path with forward slash as well, as the
1353     // forward slashes are treated as path seperation in llvm::path
1354     // regardless of what path::Style is used.
1355     return {};
1356 
1357   auto WorkingDir = getCurrentWorkingDirectory();
1358   if (!WorkingDir)
1359     return WorkingDir.getError();
1360 
1361   return makeAbsolute(WorkingDir.get(), Path);
1362 }
1363 
1364 std::error_code
1365 RedirectingFileSystem::makeAbsolute(StringRef WorkingDir,
1366                                     SmallVectorImpl<char> &Path) const {
1367   // We can't use sys::fs::make_absolute because that assumes the path style
1368   // is native and there is no way to override that.  Since we know WorkingDir
1369   // is absolute, we can use it to determine which style we actually have and
1370   // append Path ourselves.
1371   if (!WorkingDir.empty() &&
1372       !sys::path::is_absolute(WorkingDir, sys::path::Style::posix) &&
1373       !sys::path::is_absolute(WorkingDir,
1374                               sys::path::Style::windows_backslash)) {
1375     return std::error_code();
1376   }
1377   sys::path::Style style = sys::path::Style::windows_backslash;
1378   if (sys::path::is_absolute(WorkingDir, sys::path::Style::posix)) {
1379     style = sys::path::Style::posix;
1380   } else {
1381     // Distinguish between windows_backslash and windows_slash; getExistingStyle
1382     // returns posix for a path with windows_slash.
1383     if (getExistingStyle(WorkingDir) != sys::path::Style::windows_backslash)
1384       style = sys::path::Style::windows_slash;
1385   }
1386 
1387   std::string Result = std::string(WorkingDir);
1388   StringRef Dir(Result);
1389   if (!Dir.endswith(sys::path::get_separator(style))) {
1390     Result += sys::path::get_separator(style);
1391   }
1392   // backslashes '\' are legit path charactors under POSIX. Windows APIs
1393   // like CreateFile accepts forward slashes '/' as path
1394   // separator (even when mixed with backslashes). Therefore,
1395   // `Path` should be directly appended to `WorkingDir` without converting
1396   // path separator.
1397   Result.append(Path.data(), Path.size());
1398   Path.assign(Result.begin(), Result.end());
1399 
1400   return {};
1401 }
1402 
1403 directory_iterator RedirectingFileSystem::dir_begin(const Twine &Dir,
1404                                                     std::error_code &EC) {
1405   SmallString<256> Path;
1406   Dir.toVector(Path);
1407 
1408   EC = makeCanonical(Path);
1409   if (EC)
1410     return {};
1411 
1412   ErrorOr<RedirectingFileSystem::LookupResult> Result = lookupPath(Path);
1413   if (!Result) {
1414     if (Redirection != RedirectKind::RedirectOnly &&
1415         isFileNotFound(Result.getError()))
1416       return ExternalFS->dir_begin(Path, EC);
1417 
1418     EC = Result.getError();
1419     return {};
1420   }
1421 
1422   // Use status to make sure the path exists and refers to a directory.
1423   ErrorOr<Status> S = status(Path, Dir, *Result);
1424   if (!S) {
1425     if (Redirection != RedirectKind::RedirectOnly &&
1426         isFileNotFound(S.getError(), Result->E))
1427       return ExternalFS->dir_begin(Dir, EC);
1428 
1429     EC = S.getError();
1430     return {};
1431   }
1432 
1433   if (!S->isDirectory()) {
1434     EC = errc::not_a_directory;
1435     return {};
1436   }
1437 
1438   // Create the appropriate directory iterator based on whether we found a
1439   // DirectoryRemapEntry or DirectoryEntry.
1440   directory_iterator RedirectIter;
1441   std::error_code RedirectEC;
1442   if (auto ExtRedirect = Result->getExternalRedirect()) {
1443     auto RE = cast<RedirectingFileSystem::RemapEntry>(Result->E);
1444     RedirectIter = ExternalFS->dir_begin(*ExtRedirect, RedirectEC);
1445 
1446     if (!RE->useExternalName(UseExternalNames)) {
1447       // Update the paths in the results to use the virtual directory's path.
1448       RedirectIter =
1449           directory_iterator(std::make_shared<RedirectingFSDirRemapIterImpl>(
1450               std::string(Path), RedirectIter));
1451     }
1452   } else {
1453     auto DE = cast<DirectoryEntry>(Result->E);
1454     RedirectIter =
1455         directory_iterator(std::make_shared<RedirectingFSDirIterImpl>(
1456             Path, DE->contents_begin(), DE->contents_end(), RedirectEC));
1457   }
1458 
1459   if (RedirectEC) {
1460     if (RedirectEC != errc::no_such_file_or_directory) {
1461       EC = RedirectEC;
1462       return {};
1463     }
1464     RedirectIter = {};
1465   }
1466 
1467   if (Redirection == RedirectKind::RedirectOnly) {
1468     EC = RedirectEC;
1469     return RedirectIter;
1470   }
1471 
1472   std::error_code ExternalEC;
1473   directory_iterator ExternalIter = ExternalFS->dir_begin(Path, ExternalEC);
1474   if (ExternalEC) {
1475     if (ExternalEC != errc::no_such_file_or_directory) {
1476       EC = ExternalEC;
1477       return {};
1478     }
1479     ExternalIter = {};
1480   }
1481 
1482   SmallVector<directory_iterator, 2> Iters;
1483   switch (Redirection) {
1484   case RedirectKind::Fallthrough:
1485     Iters.push_back(ExternalIter);
1486     Iters.push_back(RedirectIter);
1487     break;
1488   case RedirectKind::Fallback:
1489     Iters.push_back(RedirectIter);
1490     Iters.push_back(ExternalIter);
1491     break;
1492   default:
1493     llvm_unreachable("unhandled RedirectKind");
1494   }
1495 
1496   directory_iterator Combined{
1497       std::make_shared<CombiningDirIterImpl>(Iters, EC)};
1498   if (EC)
1499     return {};
1500   return Combined;
1501 }
1502 
1503 void RedirectingFileSystem::setOverlayFileDir(StringRef Dir) {
1504   OverlayFileDir = Dir.str();
1505 }
1506 
1507 StringRef RedirectingFileSystem::getOverlayFileDir() const {
1508   return OverlayFileDir;
1509 }
1510 
1511 void RedirectingFileSystem::setFallthrough(bool Fallthrough) {
1512   if (Fallthrough) {
1513     Redirection = RedirectingFileSystem::RedirectKind::Fallthrough;
1514   } else {
1515     Redirection = RedirectingFileSystem::RedirectKind::RedirectOnly;
1516   }
1517 }
1518 
1519 void RedirectingFileSystem::setRedirection(
1520     RedirectingFileSystem::RedirectKind Kind) {
1521   Redirection = Kind;
1522 }
1523 
1524 std::vector<StringRef> RedirectingFileSystem::getRoots() const {
1525   std::vector<StringRef> R;
1526   R.reserve(Roots.size());
1527   for (const auto &Root : Roots)
1528     R.push_back(Root->getName());
1529   return R;
1530 }
1531 
1532 void RedirectingFileSystem::printImpl(raw_ostream &OS, PrintType Type,
1533                                       unsigned IndentLevel) const {
1534   printIndent(OS, IndentLevel);
1535   OS << "RedirectingFileSystem (UseExternalNames: "
1536      << (UseExternalNames ? "true" : "false") << ")\n";
1537   if (Type == PrintType::Summary)
1538     return;
1539 
1540   for (const auto &Root : Roots)
1541     printEntry(OS, Root.get(), IndentLevel);
1542 
1543   printIndent(OS, IndentLevel);
1544   OS << "ExternalFS:\n";
1545   ExternalFS->print(OS, Type == PrintType::Contents ? PrintType::Summary : Type,
1546                     IndentLevel + 1);
1547 }
1548 
1549 void RedirectingFileSystem::printEntry(raw_ostream &OS,
1550                                        RedirectingFileSystem::Entry *E,
1551                                        unsigned IndentLevel) const {
1552   printIndent(OS, IndentLevel);
1553   OS << "'" << E->getName() << "'";
1554 
1555   switch (E->getKind()) {
1556   case EK_Directory: {
1557     auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(E);
1558 
1559     OS << "\n";
1560     for (std::unique_ptr<Entry> &SubEntry :
1561          llvm::make_range(DE->contents_begin(), DE->contents_end()))
1562       printEntry(OS, SubEntry.get(), IndentLevel + 1);
1563     break;
1564   }
1565   case EK_DirectoryRemap:
1566   case EK_File: {
1567     auto *RE = cast<RedirectingFileSystem::RemapEntry>(E);
1568     OS << " -> '" << RE->getExternalContentsPath() << "'";
1569     switch (RE->getUseName()) {
1570     case NK_NotSet:
1571       break;
1572     case NK_External:
1573       OS << " (UseExternalName: true)";
1574       break;
1575     case NK_Virtual:
1576       OS << " (UseExternalName: false)";
1577       break;
1578     }
1579     OS << "\n";
1580     break;
1581   }
1582   }
1583 }
1584 
1585 /// A helper class to hold the common YAML parsing state.
1586 class llvm::vfs::RedirectingFileSystemParser {
1587   yaml::Stream &Stream;
1588 
1589   void error(yaml::Node *N, const Twine &Msg) { Stream.printError(N, Msg); }
1590 
1591   // false on error
1592   bool parseScalarString(yaml::Node *N, StringRef &Result,
1593                          SmallVectorImpl<char> &Storage) {
1594     const auto *S = dyn_cast<yaml::ScalarNode>(N);
1595 
1596     if (!S) {
1597       error(N, "expected string");
1598       return false;
1599     }
1600     Result = S->getValue(Storage);
1601     return true;
1602   }
1603 
1604   // false on error
1605   bool parseScalarBool(yaml::Node *N, bool &Result) {
1606     SmallString<5> Storage;
1607     StringRef Value;
1608     if (!parseScalarString(N, Value, Storage))
1609       return false;
1610 
1611     if (Value.equals_insensitive("true") || Value.equals_insensitive("on") ||
1612         Value.equals_insensitive("yes") || Value == "1") {
1613       Result = true;
1614       return true;
1615     } else if (Value.equals_insensitive("false") ||
1616                Value.equals_insensitive("off") ||
1617                Value.equals_insensitive("no") || Value == "0") {
1618       Result = false;
1619       return true;
1620     }
1621 
1622     error(N, "expected boolean value");
1623     return false;
1624   }
1625 
1626   std::optional<RedirectingFileSystem::RedirectKind>
1627   parseRedirectKind(yaml::Node *N) {
1628     SmallString<12> Storage;
1629     StringRef Value;
1630     if (!parseScalarString(N, Value, Storage))
1631       return std::nullopt;
1632 
1633     if (Value.equals_insensitive("fallthrough")) {
1634       return RedirectingFileSystem::RedirectKind::Fallthrough;
1635     } else if (Value.equals_insensitive("fallback")) {
1636       return RedirectingFileSystem::RedirectKind::Fallback;
1637     } else if (Value.equals_insensitive("redirect-only")) {
1638       return RedirectingFileSystem::RedirectKind::RedirectOnly;
1639     }
1640     return std::nullopt;
1641   }
1642 
1643   std::optional<RedirectingFileSystem::RootRelativeKind>
1644   parseRootRelativeKind(yaml::Node *N) {
1645     SmallString<12> Storage;
1646     StringRef Value;
1647     if (!parseScalarString(N, Value, Storage))
1648       return std::nullopt;
1649     if (Value.equals_insensitive("cwd")) {
1650       return RedirectingFileSystem::RootRelativeKind::CWD;
1651     } else if (Value.equals_insensitive("overlay-dir")) {
1652       return RedirectingFileSystem::RootRelativeKind::OverlayDir;
1653     }
1654     return std::nullopt;
1655   }
1656 
1657   struct KeyStatus {
1658     bool Required;
1659     bool Seen = false;
1660 
1661     KeyStatus(bool Required = false) : Required(Required) {}
1662   };
1663 
1664   using KeyStatusPair = std::pair<StringRef, KeyStatus>;
1665 
1666   // false on error
1667   bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
1668                                   DenseMap<StringRef, KeyStatus> &Keys) {
1669     if (!Keys.count(Key)) {
1670       error(KeyNode, "unknown key");
1671       return false;
1672     }
1673     KeyStatus &S = Keys[Key];
1674     if (S.Seen) {
1675       error(KeyNode, Twine("duplicate key '") + Key + "'");
1676       return false;
1677     }
1678     S.Seen = true;
1679     return true;
1680   }
1681 
1682   // false on error
1683   bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
1684     for (const auto &I : Keys) {
1685       if (I.second.Required && !I.second.Seen) {
1686         error(Obj, Twine("missing key '") + I.first + "'");
1687         return false;
1688       }
1689     }
1690     return true;
1691   }
1692 
1693 public:
1694   static RedirectingFileSystem::Entry *
1695   lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name,
1696                       RedirectingFileSystem::Entry *ParentEntry = nullptr) {
1697     if (!ParentEntry) { // Look for a existent root
1698       for (const auto &Root : FS->Roots) {
1699         if (Name.equals(Root->getName())) {
1700           ParentEntry = Root.get();
1701           return ParentEntry;
1702         }
1703       }
1704     } else { // Advance to the next component
1705       auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry);
1706       for (std::unique_ptr<RedirectingFileSystem::Entry> &Content :
1707            llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1708         auto *DirContent =
1709             dyn_cast<RedirectingFileSystem::DirectoryEntry>(Content.get());
1710         if (DirContent && Name.equals(Content->getName()))
1711           return DirContent;
1712       }
1713     }
1714 
1715     // ... or create a new one
1716     std::unique_ptr<RedirectingFileSystem::Entry> E =
1717         std::make_unique<RedirectingFileSystem::DirectoryEntry>(
1718             Name, Status("", getNextVirtualUniqueID(),
1719                          std::chrono::system_clock::now(), 0, 0, 0,
1720                          file_type::directory_file, sys::fs::all_all));
1721 
1722     if (!ParentEntry) { // Add a new root to the overlay
1723       FS->Roots.push_back(std::move(E));
1724       ParentEntry = FS->Roots.back().get();
1725       return ParentEntry;
1726     }
1727 
1728     auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry);
1729     DE->addContent(std::move(E));
1730     return DE->getLastContent();
1731   }
1732 
1733 private:
1734   void uniqueOverlayTree(RedirectingFileSystem *FS,
1735                          RedirectingFileSystem::Entry *SrcE,
1736                          RedirectingFileSystem::Entry *NewParentE = nullptr) {
1737     StringRef Name = SrcE->getName();
1738     switch (SrcE->getKind()) {
1739     case RedirectingFileSystem::EK_Directory: {
1740       auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(SrcE);
1741       // Empty directories could be present in the YAML as a way to
1742       // describe a file for a current directory after some of its subdir
1743       // is parsed. This only leads to redundant walks, ignore it.
1744       if (!Name.empty())
1745         NewParentE = lookupOrCreateEntry(FS, Name, NewParentE);
1746       for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
1747            llvm::make_range(DE->contents_begin(), DE->contents_end()))
1748         uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
1749       break;
1750     }
1751     case RedirectingFileSystem::EK_DirectoryRemap: {
1752       assert(NewParentE && "Parent entry must exist");
1753       auto *DR = cast<RedirectingFileSystem::DirectoryRemapEntry>(SrcE);
1754       auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE);
1755       DE->addContent(
1756           std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>(
1757               Name, DR->getExternalContentsPath(), DR->getUseName()));
1758       break;
1759     }
1760     case RedirectingFileSystem::EK_File: {
1761       assert(NewParentE && "Parent entry must exist");
1762       auto *FE = cast<RedirectingFileSystem::FileEntry>(SrcE);
1763       auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE);
1764       DE->addContent(std::make_unique<RedirectingFileSystem::FileEntry>(
1765           Name, FE->getExternalContentsPath(), FE->getUseName()));
1766       break;
1767     }
1768     }
1769   }
1770 
1771   std::unique_ptr<RedirectingFileSystem::Entry>
1772   parseEntry(yaml::Node *N, RedirectingFileSystem *FS, bool IsRootEntry) {
1773     auto *M = dyn_cast<yaml::MappingNode>(N);
1774     if (!M) {
1775       error(N, "expected mapping node for file or directory entry");
1776       return nullptr;
1777     }
1778 
1779     KeyStatusPair Fields[] = {
1780         KeyStatusPair("name", true),
1781         KeyStatusPair("type", true),
1782         KeyStatusPair("contents", false),
1783         KeyStatusPair("external-contents", false),
1784         KeyStatusPair("use-external-name", false),
1785     };
1786 
1787     DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
1788 
1789     enum { CF_NotSet, CF_List, CF_External } ContentsField = CF_NotSet;
1790     std::vector<std::unique_ptr<RedirectingFileSystem::Entry>>
1791         EntryArrayContents;
1792     SmallString<256> ExternalContentsPath;
1793     SmallString<256> Name;
1794     yaml::Node *NameValueNode = nullptr;
1795     auto UseExternalName = RedirectingFileSystem::NK_NotSet;
1796     RedirectingFileSystem::EntryKind Kind;
1797 
1798     for (auto &I : *M) {
1799       StringRef Key;
1800       // Reuse the buffer for key and value, since we don't look at key after
1801       // parsing value.
1802       SmallString<256> Buffer;
1803       if (!parseScalarString(I.getKey(), Key, Buffer))
1804         return nullptr;
1805 
1806       if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
1807         return nullptr;
1808 
1809       StringRef Value;
1810       if (Key == "name") {
1811         if (!parseScalarString(I.getValue(), Value, Buffer))
1812           return nullptr;
1813 
1814         NameValueNode = I.getValue();
1815         // Guarantee that old YAML files containing paths with ".." and "."
1816         // are properly canonicalized before read into the VFS.
1817         Name = canonicalize(Value).str();
1818       } else if (Key == "type") {
1819         if (!parseScalarString(I.getValue(), Value, Buffer))
1820           return nullptr;
1821         if (Value == "file")
1822           Kind = RedirectingFileSystem::EK_File;
1823         else if (Value == "directory")
1824           Kind = RedirectingFileSystem::EK_Directory;
1825         else if (Value == "directory-remap")
1826           Kind = RedirectingFileSystem::EK_DirectoryRemap;
1827         else {
1828           error(I.getValue(), "unknown value for 'type'");
1829           return nullptr;
1830         }
1831       } else if (Key == "contents") {
1832         if (ContentsField != CF_NotSet) {
1833           error(I.getKey(),
1834                 "entry already has 'contents' or 'external-contents'");
1835           return nullptr;
1836         }
1837         ContentsField = CF_List;
1838         auto *Contents = dyn_cast<yaml::SequenceNode>(I.getValue());
1839         if (!Contents) {
1840           // FIXME: this is only for directories, what about files?
1841           error(I.getValue(), "expected array");
1842           return nullptr;
1843         }
1844 
1845         for (auto &I : *Contents) {
1846           if (std::unique_ptr<RedirectingFileSystem::Entry> E =
1847                   parseEntry(&I, FS, /*IsRootEntry*/ false))
1848             EntryArrayContents.push_back(std::move(E));
1849           else
1850             return nullptr;
1851         }
1852       } else if (Key == "external-contents") {
1853         if (ContentsField != CF_NotSet) {
1854           error(I.getKey(),
1855                 "entry already has 'contents' or 'external-contents'");
1856           return nullptr;
1857         }
1858         ContentsField = CF_External;
1859         if (!parseScalarString(I.getValue(), Value, Buffer))
1860           return nullptr;
1861 
1862         SmallString<256> FullPath;
1863         if (FS->IsRelativeOverlay) {
1864           FullPath = FS->getOverlayFileDir();
1865           assert(!FullPath.empty() &&
1866                  "External contents prefix directory must exist");
1867           llvm::sys::path::append(FullPath, Value);
1868         } else {
1869           FullPath = Value;
1870         }
1871 
1872         // Guarantee that old YAML files containing paths with ".." and "."
1873         // are properly canonicalized before read into the VFS.
1874         FullPath = canonicalize(FullPath);
1875         ExternalContentsPath = FullPath.str();
1876       } else if (Key == "use-external-name") {
1877         bool Val;
1878         if (!parseScalarBool(I.getValue(), Val))
1879           return nullptr;
1880         UseExternalName = Val ? RedirectingFileSystem::NK_External
1881                               : RedirectingFileSystem::NK_Virtual;
1882       } else {
1883         llvm_unreachable("key missing from Keys");
1884       }
1885     }
1886 
1887     if (Stream.failed())
1888       return nullptr;
1889 
1890     // check for missing keys
1891     if (ContentsField == CF_NotSet) {
1892       error(N, "missing key 'contents' or 'external-contents'");
1893       return nullptr;
1894     }
1895     if (!checkMissingKeys(N, Keys))
1896       return nullptr;
1897 
1898     // check invalid configuration
1899     if (Kind == RedirectingFileSystem::EK_Directory &&
1900         UseExternalName != RedirectingFileSystem::NK_NotSet) {
1901       error(N, "'use-external-name' is not supported for 'directory' entries");
1902       return nullptr;
1903     }
1904 
1905     if (Kind == RedirectingFileSystem::EK_DirectoryRemap &&
1906         ContentsField == CF_List) {
1907       error(N, "'contents' is not supported for 'directory-remap' entries");
1908       return nullptr;
1909     }
1910 
1911     sys::path::Style path_style = sys::path::Style::native;
1912     if (IsRootEntry) {
1913       // VFS root entries may be in either Posix or Windows style.  Figure out
1914       // which style we have, and use it consistently.
1915       if (sys::path::is_absolute(Name, sys::path::Style::posix)) {
1916         path_style = sys::path::Style::posix;
1917       } else if (sys::path::is_absolute(Name,
1918                                         sys::path::Style::windows_backslash)) {
1919         path_style = sys::path::Style::windows_backslash;
1920       } else {
1921         // Relative VFS root entries are made absolute to either the overlay
1922         // directory, or the current working directory, then we can determine
1923         // the path style from that.
1924         std::error_code EC;
1925         if (FS->RootRelative ==
1926             RedirectingFileSystem::RootRelativeKind::OverlayDir) {
1927           StringRef FullPath = FS->getOverlayFileDir();
1928           assert(!FullPath.empty() && "Overlay file directory must exist");
1929           EC = FS->makeAbsolute(FullPath, Name);
1930           Name = canonicalize(Name);
1931         } else {
1932           EC = sys::fs::make_absolute(Name);
1933         }
1934         if (EC) {
1935           assert(NameValueNode && "Name presence should be checked earlier");
1936           error(
1937               NameValueNode,
1938               "entry with relative path at the root level is not discoverable");
1939           return nullptr;
1940         }
1941         path_style = sys::path::is_absolute(Name, sys::path::Style::posix)
1942                          ? sys::path::Style::posix
1943                          : sys::path::Style::windows_backslash;
1944       }
1945       // is::path::is_absolute(Name, sys::path::Style::windows_backslash) will
1946       // return true even if `Name` is using forward slashes. Distinguish
1947       // between windows_backslash and windows_slash.
1948       if (path_style == sys::path::Style::windows_backslash &&
1949           getExistingStyle(Name) != sys::path::Style::windows_backslash)
1950         path_style = sys::path::Style::windows_slash;
1951     }
1952 
1953     // Remove trailing slash(es), being careful not to remove the root path
1954     StringRef Trimmed = Name;
1955     size_t RootPathLen = sys::path::root_path(Trimmed, path_style).size();
1956     while (Trimmed.size() > RootPathLen &&
1957            sys::path::is_separator(Trimmed.back(), path_style))
1958       Trimmed = Trimmed.slice(0, Trimmed.size() - 1);
1959 
1960     // Get the last component
1961     StringRef LastComponent = sys::path::filename(Trimmed, path_style);
1962 
1963     std::unique_ptr<RedirectingFileSystem::Entry> Result;
1964     switch (Kind) {
1965     case RedirectingFileSystem::EK_File:
1966       Result = std::make_unique<RedirectingFileSystem::FileEntry>(
1967           LastComponent, std::move(ExternalContentsPath), UseExternalName);
1968       break;
1969     case RedirectingFileSystem::EK_DirectoryRemap:
1970       Result = std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>(
1971           LastComponent, std::move(ExternalContentsPath), UseExternalName);
1972       break;
1973     case RedirectingFileSystem::EK_Directory:
1974       Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>(
1975           LastComponent, std::move(EntryArrayContents),
1976           Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1977                  0, 0, 0, file_type::directory_file, sys::fs::all_all));
1978       break;
1979     }
1980 
1981     StringRef Parent = sys::path::parent_path(Trimmed, path_style);
1982     if (Parent.empty())
1983       return Result;
1984 
1985     // if 'name' contains multiple components, create implicit directory entries
1986     for (sys::path::reverse_iterator I = sys::path::rbegin(Parent, path_style),
1987                                      E = sys::path::rend(Parent);
1988          I != E; ++I) {
1989       std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> Entries;
1990       Entries.push_back(std::move(Result));
1991       Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>(
1992           *I, std::move(Entries),
1993           Status("", getNextVirtualUniqueID(), std::chrono::system_clock::now(),
1994                  0, 0, 0, file_type::directory_file, sys::fs::all_all));
1995     }
1996     return Result;
1997   }
1998 
1999 public:
2000   RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {}
2001 
2002   // false on error
2003   bool parse(yaml::Node *Root, RedirectingFileSystem *FS) {
2004     auto *Top = dyn_cast<yaml::MappingNode>(Root);
2005     if (!Top) {
2006       error(Root, "expected mapping node");
2007       return false;
2008     }
2009 
2010     KeyStatusPair Fields[] = {
2011         KeyStatusPair("version", true),
2012         KeyStatusPair("case-sensitive", false),
2013         KeyStatusPair("use-external-names", false),
2014         KeyStatusPair("root-relative", false),
2015         KeyStatusPair("overlay-relative", false),
2016         KeyStatusPair("fallthrough", false),
2017         KeyStatusPair("redirecting-with", false),
2018         KeyStatusPair("roots", true),
2019     };
2020 
2021     DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields));
2022     std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> RootEntries;
2023 
2024     // Parse configuration and 'roots'
2025     for (auto &I : *Top) {
2026       SmallString<10> KeyBuffer;
2027       StringRef Key;
2028       if (!parseScalarString(I.getKey(), Key, KeyBuffer))
2029         return false;
2030 
2031       if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
2032         return false;
2033 
2034       if (Key == "roots") {
2035         auto *Roots = dyn_cast<yaml::SequenceNode>(I.getValue());
2036         if (!Roots) {
2037           error(I.getValue(), "expected array");
2038           return false;
2039         }
2040 
2041         for (auto &I : *Roots) {
2042           if (std::unique_ptr<RedirectingFileSystem::Entry> E =
2043                   parseEntry(&I, FS, /*IsRootEntry*/ true))
2044             RootEntries.push_back(std::move(E));
2045           else
2046             return false;
2047         }
2048       } else if (Key == "version") {
2049         StringRef VersionString;
2050         SmallString<4> Storage;
2051         if (!parseScalarString(I.getValue(), VersionString, Storage))
2052           return false;
2053         int Version;
2054         if (VersionString.getAsInteger<int>(10, Version)) {
2055           error(I.getValue(), "expected integer");
2056           return false;
2057         }
2058         if (Version < 0) {
2059           error(I.getValue(), "invalid version number");
2060           return false;
2061         }
2062         if (Version != 0) {
2063           error(I.getValue(), "version mismatch, expected 0");
2064           return false;
2065         }
2066       } else if (Key == "case-sensitive") {
2067         if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
2068           return false;
2069       } else if (Key == "overlay-relative") {
2070         if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
2071           return false;
2072       } else if (Key == "use-external-names") {
2073         if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
2074           return false;
2075       } else if (Key == "fallthrough") {
2076         if (Keys["redirecting-with"].Seen) {
2077           error(I.getValue(),
2078                 "'fallthrough' and 'redirecting-with' are mutually exclusive");
2079           return false;
2080         }
2081 
2082         bool ShouldFallthrough = false;
2083         if (!parseScalarBool(I.getValue(), ShouldFallthrough))
2084           return false;
2085 
2086         if (ShouldFallthrough) {
2087           FS->Redirection = RedirectingFileSystem::RedirectKind::Fallthrough;
2088         } else {
2089           FS->Redirection = RedirectingFileSystem::RedirectKind::RedirectOnly;
2090         }
2091       } else if (Key == "redirecting-with") {
2092         if (Keys["fallthrough"].Seen) {
2093           error(I.getValue(),
2094                 "'fallthrough' and 'redirecting-with' are mutually exclusive");
2095           return false;
2096         }
2097 
2098         if (auto Kind = parseRedirectKind(I.getValue())) {
2099           FS->Redirection = *Kind;
2100         } else {
2101           error(I.getValue(), "expected valid redirect kind");
2102           return false;
2103         }
2104       } else if (Key == "root-relative") {
2105         if (auto Kind = parseRootRelativeKind(I.getValue())) {
2106           FS->RootRelative = *Kind;
2107         } else {
2108           error(I.getValue(), "expected valid root-relative kind");
2109           return false;
2110         }
2111       } else {
2112         llvm_unreachable("key missing from Keys");
2113       }
2114     }
2115 
2116     if (Stream.failed())
2117       return false;
2118 
2119     if (!checkMissingKeys(Top, Keys))
2120       return false;
2121 
2122     // Now that we sucessefully parsed the YAML file, canonicalize the internal
2123     // representation to a proper directory tree so that we can search faster
2124     // inside the VFS.
2125     for (auto &E : RootEntries)
2126       uniqueOverlayTree(FS, E.get());
2127 
2128     return true;
2129   }
2130 };
2131 
2132 std::unique_ptr<RedirectingFileSystem>
2133 RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
2134                               SourceMgr::DiagHandlerTy DiagHandler,
2135                               StringRef YAMLFilePath, void *DiagContext,
2136                               IntrusiveRefCntPtr<FileSystem> ExternalFS) {
2137   SourceMgr SM;
2138   yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
2139 
2140   SM.setDiagHandler(DiagHandler, DiagContext);
2141   yaml::document_iterator DI = Stream.begin();
2142   yaml::Node *Root = DI->getRoot();
2143   if (DI == Stream.end() || !Root) {
2144     SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
2145     return nullptr;
2146   }
2147 
2148   RedirectingFileSystemParser P(Stream);
2149 
2150   std::unique_ptr<RedirectingFileSystem> FS(
2151       new RedirectingFileSystem(ExternalFS));
2152 
2153   if (!YAMLFilePath.empty()) {
2154     // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed
2155     // to each 'external-contents' path.
2156     //
2157     // Example:
2158     //    -ivfsoverlay dummy.cache/vfs/vfs.yaml
2159     // yields:
2160     //  FS->OverlayFileDir => /<absolute_path_to>/dummy.cache/vfs
2161     //
2162     SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
2163     std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
2164     assert(!EC && "Overlay dir final path must be absolute");
2165     (void)EC;
2166     FS->setOverlayFileDir(OverlayAbsDir);
2167   }
2168 
2169   if (!P.parse(Root, FS.get()))
2170     return nullptr;
2171 
2172   return FS;
2173 }
2174 
2175 std::unique_ptr<RedirectingFileSystem> RedirectingFileSystem::create(
2176     ArrayRef<std::pair<std::string, std::string>> RemappedFiles,
2177     bool UseExternalNames, FileSystem &ExternalFS) {
2178   std::unique_ptr<RedirectingFileSystem> FS(
2179       new RedirectingFileSystem(&ExternalFS));
2180   FS->UseExternalNames = UseExternalNames;
2181 
2182   StringMap<RedirectingFileSystem::Entry *> Entries;
2183 
2184   for (auto &Mapping : llvm::reverse(RemappedFiles)) {
2185     SmallString<128> From = StringRef(Mapping.first);
2186     SmallString<128> To = StringRef(Mapping.second);
2187     {
2188       auto EC = ExternalFS.makeAbsolute(From);
2189       (void)EC;
2190       assert(!EC && "Could not make absolute path");
2191     }
2192 
2193     // Check if we've already mapped this file. The first one we see (in the
2194     // reverse iteration) wins.
2195     RedirectingFileSystem::Entry *&ToEntry = Entries[From];
2196     if (ToEntry)
2197       continue;
2198 
2199     // Add parent directories.
2200     RedirectingFileSystem::Entry *Parent = nullptr;
2201     StringRef FromDirectory = llvm::sys::path::parent_path(From);
2202     for (auto I = llvm::sys::path::begin(FromDirectory),
2203               E = llvm::sys::path::end(FromDirectory);
2204          I != E; ++I) {
2205       Parent = RedirectingFileSystemParser::lookupOrCreateEntry(FS.get(), *I,
2206                                                                 Parent);
2207     }
2208     assert(Parent && "File without a directory?");
2209     {
2210       auto EC = ExternalFS.makeAbsolute(To);
2211       (void)EC;
2212       assert(!EC && "Could not make absolute path");
2213     }
2214 
2215     // Add the file.
2216     auto NewFile = std::make_unique<RedirectingFileSystem::FileEntry>(
2217         llvm::sys::path::filename(From), To,
2218         UseExternalNames ? RedirectingFileSystem::NK_External
2219                          : RedirectingFileSystem::NK_Virtual);
2220     ToEntry = NewFile.get();
2221     cast<RedirectingFileSystem::DirectoryEntry>(Parent)->addContent(
2222         std::move(NewFile));
2223   }
2224 
2225   return FS;
2226 }
2227 
2228 RedirectingFileSystem::LookupResult::LookupResult(
2229     Entry *E, sys::path::const_iterator Start, sys::path::const_iterator End)
2230     : E(E) {
2231   assert(E != nullptr);
2232   // If the matched entry is a DirectoryRemapEntry, set ExternalRedirect to the
2233   // path of the directory it maps to in the external file system plus any
2234   // remaining path components in the provided iterator.
2235   if (auto *DRE = dyn_cast<RedirectingFileSystem::DirectoryRemapEntry>(E)) {
2236     SmallString<256> Redirect(DRE->getExternalContentsPath());
2237     sys::path::append(Redirect, Start, End,
2238                       getExistingStyle(DRE->getExternalContentsPath()));
2239     ExternalRedirect = std::string(Redirect);
2240   }
2241 }
2242 
2243 void RedirectingFileSystem::LookupResult::getPath(
2244     llvm::SmallVectorImpl<char> &Result) const {
2245   Result.clear();
2246   for (Entry *Parent : Parents)
2247     llvm::sys::path::append(Result, Parent->getName());
2248   llvm::sys::path::append(Result, E->getName());
2249 }
2250 
2251 std::error_code
2252 RedirectingFileSystem::makeCanonical(SmallVectorImpl<char> &Path) const {
2253   if (std::error_code EC = makeAbsolute(Path))
2254     return EC;
2255 
2256   llvm::SmallString<256> CanonicalPath =
2257       canonicalize(StringRef(Path.data(), Path.size()));
2258   if (CanonicalPath.empty())
2259     return make_error_code(llvm::errc::invalid_argument);
2260 
2261   Path.assign(CanonicalPath.begin(), CanonicalPath.end());
2262   return {};
2263 }
2264 
2265 ErrorOr<RedirectingFileSystem::LookupResult>
2266 RedirectingFileSystem::lookupPath(StringRef Path) const {
2267   sys::path::const_iterator Start = sys::path::begin(Path);
2268   sys::path::const_iterator End = sys::path::end(Path);
2269   llvm::SmallVector<Entry *, 32> Entries;
2270   for (const auto &Root : Roots) {
2271     ErrorOr<RedirectingFileSystem::LookupResult> Result =
2272         lookupPathImpl(Start, End, Root.get(), Entries);
2273     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) {
2274       Result->Parents = std::move(Entries);
2275       return Result;
2276     }
2277   }
2278   return make_error_code(llvm::errc::no_such_file_or_directory);
2279 }
2280 
2281 ErrorOr<RedirectingFileSystem::LookupResult>
2282 RedirectingFileSystem::lookupPathImpl(
2283     sys::path::const_iterator Start, sys::path::const_iterator End,
2284     RedirectingFileSystem::Entry *From,
2285     llvm::SmallVectorImpl<Entry *> &Entries) const {
2286   assert(!isTraversalComponent(*Start) &&
2287          !isTraversalComponent(From->getName()) &&
2288          "Paths should not contain traversal components");
2289 
2290   StringRef FromName = From->getName();
2291 
2292   // Forward the search to the next component in case this is an empty one.
2293   if (!FromName.empty()) {
2294     if (!pathComponentMatches(*Start, FromName))
2295       return make_error_code(llvm::errc::no_such_file_or_directory);
2296 
2297     ++Start;
2298 
2299     if (Start == End) {
2300       // Match!
2301       return LookupResult(From, Start, End);
2302     }
2303   }
2304 
2305   if (isa<RedirectingFileSystem::FileEntry>(From))
2306     return make_error_code(llvm::errc::not_a_directory);
2307 
2308   if (isa<RedirectingFileSystem::DirectoryRemapEntry>(From))
2309     return LookupResult(From, Start, End);
2310 
2311   auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(From);
2312   for (const std::unique_ptr<RedirectingFileSystem::Entry> &DirEntry :
2313        llvm::make_range(DE->contents_begin(), DE->contents_end())) {
2314     Entries.push_back(From);
2315     ErrorOr<RedirectingFileSystem::LookupResult> Result =
2316         lookupPathImpl(Start, End, DirEntry.get(), Entries);
2317     if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
2318       return Result;
2319     Entries.pop_back();
2320   }
2321 
2322   return make_error_code(llvm::errc::no_such_file_or_directory);
2323 }
2324 
2325 static Status getRedirectedFileStatus(const Twine &OriginalPath,
2326                                       bool UseExternalNames,
2327                                       Status ExternalStatus) {
2328   // The path has been mapped by some nested VFS and exposes an external path,
2329   // don't override it with the original path.
2330   if (ExternalStatus.ExposesExternalVFSPath)
2331     return ExternalStatus;
2332 
2333   Status S = ExternalStatus;
2334   if (!UseExternalNames)
2335     S = Status::copyWithNewName(S, OriginalPath);
2336   else
2337     S.ExposesExternalVFSPath = true;
2338   S.IsVFSMapped = true;
2339   return S;
2340 }
2341 
2342 ErrorOr<Status> RedirectingFileSystem::status(
2343     const Twine &CanonicalPath, const Twine &OriginalPath,
2344     const RedirectingFileSystem::LookupResult &Result) {
2345   if (std::optional<StringRef> ExtRedirect = Result.getExternalRedirect()) {
2346     SmallString<256> CanonicalRemappedPath((*ExtRedirect).str());
2347     if (std::error_code EC = makeCanonical(CanonicalRemappedPath))
2348       return EC;
2349 
2350     ErrorOr<Status> S = ExternalFS->status(CanonicalRemappedPath);
2351     if (!S)
2352       return S;
2353     S = Status::copyWithNewName(*S, *ExtRedirect);
2354     auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result.E);
2355     return getRedirectedFileStatus(OriginalPath,
2356                                    RE->useExternalName(UseExternalNames), *S);
2357   }
2358 
2359   auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(Result.E);
2360   return Status::copyWithNewName(DE->getStatus(), CanonicalPath);
2361 }
2362 
2363 ErrorOr<Status>
2364 RedirectingFileSystem::getExternalStatus(const Twine &CanonicalPath,
2365                                          const Twine &OriginalPath) const {
2366   auto Result = ExternalFS->status(CanonicalPath);
2367 
2368   // The path has been mapped by some nested VFS, don't override it with the
2369   // original path.
2370   if (!Result || Result->ExposesExternalVFSPath)
2371     return Result;
2372   return Status::copyWithNewName(Result.get(), OriginalPath);
2373 }
2374 
2375 ErrorOr<Status> RedirectingFileSystem::status(const Twine &OriginalPath) {
2376   SmallString<256> CanonicalPath;
2377   OriginalPath.toVector(CanonicalPath);
2378 
2379   if (std::error_code EC = makeCanonical(CanonicalPath))
2380     return EC;
2381 
2382   if (Redirection == RedirectKind::Fallback) {
2383     // Attempt to find the original file first, only falling back to the
2384     // mapped file if that fails.
2385     ErrorOr<Status> S = getExternalStatus(CanonicalPath, OriginalPath);
2386     if (S)
2387       return S;
2388   }
2389 
2390   ErrorOr<RedirectingFileSystem::LookupResult> Result =
2391       lookupPath(CanonicalPath);
2392   if (!Result) {
2393     // Was not able to map file, fallthrough to using the original path if
2394     // that was the specified redirection type.
2395     if (Redirection == RedirectKind::Fallthrough &&
2396         isFileNotFound(Result.getError()))
2397       return getExternalStatus(CanonicalPath, OriginalPath);
2398     return Result.getError();
2399   }
2400 
2401   ErrorOr<Status> S = status(CanonicalPath, OriginalPath, *Result);
2402   if (!S && Redirection == RedirectKind::Fallthrough &&
2403       isFileNotFound(S.getError(), Result->E)) {
2404     // Mapped the file but it wasn't found in the underlying filesystem,
2405     // fallthrough to using the original path if that was the specified
2406     // redirection type.
2407     return getExternalStatus(CanonicalPath, OriginalPath);
2408   }
2409 
2410   return S;
2411 }
2412 
2413 namespace {
2414 
2415 /// Provide a file wrapper with an overriden status.
2416 class FileWithFixedStatus : public File {
2417   std::unique_ptr<File> InnerFile;
2418   Status S;
2419 
2420 public:
2421   FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S)
2422       : InnerFile(std::move(InnerFile)), S(std::move(S)) {}
2423 
2424   ErrorOr<Status> status() override { return S; }
2425   ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
2426 
2427   getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
2428             bool IsVolatile) override {
2429     return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator,
2430                                 IsVolatile);
2431   }
2432 
2433   std::error_code close() override { return InnerFile->close(); }
2434 
2435   void setPath(const Twine &Path) override { S = S.copyWithNewName(S, Path); }
2436 };
2437 
2438 } // namespace
2439 
2440 ErrorOr<std::unique_ptr<File>>
2441 File::getWithPath(ErrorOr<std::unique_ptr<File>> Result, const Twine &P) {
2442   // See \c getRedirectedFileStatus - don't update path if it's exposing an
2443   // external path.
2444   if (!Result || (*Result)->status()->ExposesExternalVFSPath)
2445     return Result;
2446 
2447   ErrorOr<std::unique_ptr<File>> F = std::move(*Result);
2448   auto Name = F->get()->getName();
2449   if (Name && Name.get() != P.str())
2450     F->get()->setPath(P);
2451   return F;
2452 }
2453 
2454 ErrorOr<std::unique_ptr<File>>
2455 RedirectingFileSystem::openFileForRead(const Twine &OriginalPath) {
2456   SmallString<256> CanonicalPath;
2457   OriginalPath.toVector(CanonicalPath);
2458 
2459   if (std::error_code EC = makeCanonical(CanonicalPath))
2460     return EC;
2461 
2462   if (Redirection == RedirectKind::Fallback) {
2463     // Attempt to find the original file first, only falling back to the
2464     // mapped file if that fails.
2465     auto F = File::getWithPath(ExternalFS->openFileForRead(CanonicalPath),
2466                                OriginalPath);
2467     if (F)
2468       return F;
2469   }
2470 
2471   ErrorOr<RedirectingFileSystem::LookupResult> Result =
2472       lookupPath(CanonicalPath);
2473   if (!Result) {
2474     // Was not able to map file, fallthrough to using the original path if
2475     // that was the specified redirection type.
2476     if (Redirection == RedirectKind::Fallthrough &&
2477         isFileNotFound(Result.getError()))
2478       return File::getWithPath(ExternalFS->openFileForRead(CanonicalPath),
2479                                OriginalPath);
2480     return Result.getError();
2481   }
2482 
2483   if (!Result->getExternalRedirect()) // FIXME: errc::not_a_file?
2484     return make_error_code(llvm::errc::invalid_argument);
2485 
2486   StringRef ExtRedirect = *Result->getExternalRedirect();
2487   SmallString<256> CanonicalRemappedPath(ExtRedirect.str());
2488   if (std::error_code EC = makeCanonical(CanonicalRemappedPath))
2489     return EC;
2490 
2491   auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result->E);
2492 
2493   auto ExternalFile = File::getWithPath(
2494       ExternalFS->openFileForRead(CanonicalRemappedPath), ExtRedirect);
2495   if (!ExternalFile) {
2496     if (Redirection == RedirectKind::Fallthrough &&
2497         isFileNotFound(ExternalFile.getError(), Result->E)) {
2498       // Mapped the file but it wasn't found in the underlying filesystem,
2499       // fallthrough to using the original path if that was the specified
2500       // redirection type.
2501       return File::getWithPath(ExternalFS->openFileForRead(CanonicalPath),
2502                                OriginalPath);
2503     }
2504     return ExternalFile;
2505   }
2506 
2507   auto ExternalStatus = (*ExternalFile)->status();
2508   if (!ExternalStatus)
2509     return ExternalStatus.getError();
2510 
2511   // Otherwise, the file was successfully remapped. Mark it as such. Also
2512   // replace the underlying path if the external name is being used.
2513   Status S = getRedirectedFileStatus(
2514       OriginalPath, RE->useExternalName(UseExternalNames), *ExternalStatus);
2515   return std::unique_ptr<File>(
2516       std::make_unique<FileWithFixedStatus>(std::move(*ExternalFile), S));
2517 }
2518 
2519 std::error_code
2520 RedirectingFileSystem::getRealPath(const Twine &OriginalPath,
2521                                    SmallVectorImpl<char> &Output) const {
2522   SmallString<256> CanonicalPath;
2523   OriginalPath.toVector(CanonicalPath);
2524 
2525   if (std::error_code EC = makeCanonical(CanonicalPath))
2526     return EC;
2527 
2528   if (Redirection == RedirectKind::Fallback) {
2529     // Attempt to find the original file first, only falling back to the
2530     // mapped file if that fails.
2531     std::error_code EC = ExternalFS->getRealPath(CanonicalPath, Output);
2532     if (!EC)
2533       return EC;
2534   }
2535 
2536   ErrorOr<RedirectingFileSystem::LookupResult> Result =
2537       lookupPath(CanonicalPath);
2538   if (!Result) {
2539     // Was not able to map file, fallthrough to using the original path if
2540     // that was the specified redirection type.
2541     if (Redirection == RedirectKind::Fallthrough &&
2542         isFileNotFound(Result.getError()))
2543       return ExternalFS->getRealPath(CanonicalPath, Output);
2544     return Result.getError();
2545   }
2546 
2547   // If we found FileEntry or DirectoryRemapEntry, look up the mapped
2548   // path in the external file system.
2549   if (auto ExtRedirect = Result->getExternalRedirect()) {
2550     auto P = ExternalFS->getRealPath(*ExtRedirect, Output);
2551     if (P && Redirection == RedirectKind::Fallthrough &&
2552         isFileNotFound(P, Result->E)) {
2553       // Mapped the file but it wasn't found in the underlying filesystem,
2554       // fallthrough to using the original path if that was the specified
2555       // redirection type.
2556       return ExternalFS->getRealPath(CanonicalPath, Output);
2557     }
2558     return P;
2559   }
2560 
2561   // We found a DirectoryEntry, which does not have a single external contents
2562   // path. Use the canonical virtual path.
2563   if (Redirection == RedirectKind::Fallthrough) {
2564     Result->getPath(Output);
2565     return {};
2566   }
2567   return llvm::errc::invalid_argument;
2568 }
2569 
2570 std::unique_ptr<FileSystem>
2571 vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
2572                     SourceMgr::DiagHandlerTy DiagHandler,
2573                     StringRef YAMLFilePath, void *DiagContext,
2574                     IntrusiveRefCntPtr<FileSystem> ExternalFS) {
2575   return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
2576                                        YAMLFilePath, DiagContext,
2577                                        std::move(ExternalFS));
2578 }
2579 
2580 static void getVFSEntries(RedirectingFileSystem::Entry *SrcE,
2581                           SmallVectorImpl<StringRef> &Path,
2582                           SmallVectorImpl<YAMLVFSEntry> &Entries) {
2583   auto Kind = SrcE->getKind();
2584   if (Kind == RedirectingFileSystem::EK_Directory) {
2585     auto *DE = dyn_cast<RedirectingFileSystem::DirectoryEntry>(SrcE);
2586     assert(DE && "Must be a directory");
2587     for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
2588          llvm::make_range(DE->contents_begin(), DE->contents_end())) {
2589       Path.push_back(SubEntry->getName());
2590       getVFSEntries(SubEntry.get(), Path, Entries);
2591       Path.pop_back();
2592     }
2593     return;
2594   }
2595 
2596   if (Kind == RedirectingFileSystem::EK_DirectoryRemap) {
2597     auto *DR = dyn_cast<RedirectingFileSystem::DirectoryRemapEntry>(SrcE);
2598     assert(DR && "Must be a directory remap");
2599     SmallString<128> VPath;
2600     for (auto &Comp : Path)
2601       llvm::sys::path::append(VPath, Comp);
2602     Entries.push_back(
2603         YAMLVFSEntry(VPath.c_str(), DR->getExternalContentsPath()));
2604     return;
2605   }
2606 
2607   assert(Kind == RedirectingFileSystem::EK_File && "Must be a EK_File");
2608   auto *FE = dyn_cast<RedirectingFileSystem::FileEntry>(SrcE);
2609   assert(FE && "Must be a file");
2610   SmallString<128> VPath;
2611   for (auto &Comp : Path)
2612     llvm::sys::path::append(VPath, Comp);
2613   Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
2614 }
2615 
2616 void vfs::collectVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
2617                              SourceMgr::DiagHandlerTy DiagHandler,
2618                              StringRef YAMLFilePath,
2619                              SmallVectorImpl<YAMLVFSEntry> &CollectedEntries,
2620                              void *DiagContext,
2621                              IntrusiveRefCntPtr<FileSystem> ExternalFS) {
2622   std::unique_ptr<RedirectingFileSystem> VFS = RedirectingFileSystem::create(
2623       std::move(Buffer), DiagHandler, YAMLFilePath, DiagContext,
2624       std::move(ExternalFS));
2625   if (!VFS)
2626     return;
2627   ErrorOr<RedirectingFileSystem::LookupResult> RootResult =
2628       VFS->lookupPath("/");
2629   if (!RootResult)
2630     return;
2631   SmallVector<StringRef, 8> Components;
2632   Components.push_back("/");
2633   getVFSEntries(RootResult->E, Components, CollectedEntries);
2634 }
2635 
2636 UniqueID vfs::getNextVirtualUniqueID() {
2637   static std::atomic<unsigned> UID;
2638   unsigned ID = ++UID;
2639   // The following assumes that uint64_t max will never collide with a real
2640   // dev_t value from the OS.
2641   return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
2642 }
2643 
2644 void YAMLVFSWriter::addEntry(StringRef VirtualPath, StringRef RealPath,
2645                              bool IsDirectory) {
2646   assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
2647   assert(sys::path::is_absolute(RealPath) && "real path not absolute");
2648   assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
2649   Mappings.emplace_back(VirtualPath, RealPath, IsDirectory);
2650 }
2651 
2652 void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
2653   addEntry(VirtualPath, RealPath, /*IsDirectory=*/false);
2654 }
2655 
2656 void YAMLVFSWriter::addDirectoryMapping(StringRef VirtualPath,
2657                                         StringRef RealPath) {
2658   addEntry(VirtualPath, RealPath, /*IsDirectory=*/true);
2659 }
2660 
2661 namespace {
2662 
2663 class JSONWriter {
2664   llvm::raw_ostream &OS;
2665   SmallVector<StringRef, 16> DirStack;
2666 
2667   unsigned getDirIndent() { return 4 * DirStack.size(); }
2668   unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
2669   bool containedIn(StringRef Parent, StringRef Path);
2670   StringRef containedPart(StringRef Parent, StringRef Path);
2671   void startDirectory(StringRef Path);
2672   void endDirectory();
2673   void writeEntry(StringRef VPath, StringRef RPath);
2674 
2675 public:
2676   JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
2677 
2678   void write(ArrayRef<YAMLVFSEntry> Entries,
2679              std::optional<bool> UseExternalNames,
2680              std::optional<bool> IsCaseSensitive,
2681              std::optional<bool> IsOverlayRelative, StringRef OverlayDir);
2682 };
2683 
2684 } // namespace
2685 
2686 bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
2687   using namespace llvm::sys;
2688 
2689   // Compare each path component.
2690   auto IParent = path::begin(Parent), EParent = path::end(Parent);
2691   for (auto IChild = path::begin(Path), EChild = path::end(Path);
2692        IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
2693     if (*IParent != *IChild)
2694       return false;
2695   }
2696   // Have we exhausted the parent path?
2697   return IParent == EParent;
2698 }
2699 
2700 StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
2701   assert(!Parent.empty());
2702   assert(containedIn(Parent, Path));
2703   return Path.slice(Parent.size() + 1, StringRef::npos);
2704 }
2705 
2706 void JSONWriter::startDirectory(StringRef Path) {
2707   StringRef Name =
2708       DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
2709   DirStack.push_back(Path);
2710   unsigned Indent = getDirIndent();
2711   OS.indent(Indent) << "{\n";
2712   OS.indent(Indent + 2) << "'type': 'directory',\n";
2713   OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
2714   OS.indent(Indent + 2) << "'contents': [\n";
2715 }
2716 
2717 void JSONWriter::endDirectory() {
2718   unsigned Indent = getDirIndent();
2719   OS.indent(Indent + 2) << "]\n";
2720   OS.indent(Indent) << "}";
2721 
2722   DirStack.pop_back();
2723 }
2724 
2725 void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
2726   unsigned Indent = getFileIndent();
2727   OS.indent(Indent) << "{\n";
2728   OS.indent(Indent + 2) << "'type': 'file',\n";
2729   OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
2730   OS.indent(Indent + 2) << "'external-contents': \""
2731                         << llvm::yaml::escape(RPath) << "\"\n";
2732   OS.indent(Indent) << "}";
2733 }
2734 
2735 void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
2736                        std::optional<bool> UseExternalNames,
2737                        std::optional<bool> IsCaseSensitive,
2738                        std::optional<bool> IsOverlayRelative,
2739                        StringRef OverlayDir) {
2740   using namespace llvm::sys;
2741 
2742   OS << "{\n"
2743         "  'version': 0,\n";
2744   if (IsCaseSensitive)
2745     OS << "  'case-sensitive': '" << (*IsCaseSensitive ? "true" : "false")
2746        << "',\n";
2747   if (UseExternalNames)
2748     OS << "  'use-external-names': '" << (*UseExternalNames ? "true" : "false")
2749        << "',\n";
2750   bool UseOverlayRelative = false;
2751   if (IsOverlayRelative) {
2752     UseOverlayRelative = *IsOverlayRelative;
2753     OS << "  'overlay-relative': '" << (UseOverlayRelative ? "true" : "false")
2754        << "',\n";
2755   }
2756   OS << "  'roots': [\n";
2757 
2758   if (!Entries.empty()) {
2759     const YAMLVFSEntry &Entry = Entries.front();
2760 
2761     startDirectory(
2762       Entry.IsDirectory ? Entry.VPath : path::parent_path(Entry.VPath)
2763     );
2764 
2765     StringRef RPath = Entry.RPath;
2766     if (UseOverlayRelative) {
2767       unsigned OverlayDirLen = OverlayDir.size();
2768       assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
2769              "Overlay dir must be contained in RPath");
2770       RPath = RPath.slice(OverlayDirLen, RPath.size());
2771     }
2772 
2773     bool IsCurrentDirEmpty = true;
2774     if (!Entry.IsDirectory) {
2775       writeEntry(path::filename(Entry.VPath), RPath);
2776       IsCurrentDirEmpty = false;
2777     }
2778 
2779     for (const auto &Entry : Entries.slice(1)) {
2780       StringRef Dir =
2781           Entry.IsDirectory ? Entry.VPath : path::parent_path(Entry.VPath);
2782       if (Dir == DirStack.back()) {
2783         if (!IsCurrentDirEmpty) {
2784           OS << ",\n";
2785         }
2786       } else {
2787         bool IsDirPoppedFromStack = false;
2788         while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
2789           OS << "\n";
2790           endDirectory();
2791           IsDirPoppedFromStack = true;
2792         }
2793         if (IsDirPoppedFromStack || !IsCurrentDirEmpty) {
2794           OS << ",\n";
2795         }
2796         startDirectory(Dir);
2797         IsCurrentDirEmpty = true;
2798       }
2799       StringRef RPath = Entry.RPath;
2800       if (UseOverlayRelative) {
2801         unsigned OverlayDirLen = OverlayDir.size();
2802         assert(RPath.substr(0, OverlayDirLen) == OverlayDir &&
2803                "Overlay dir must be contained in RPath");
2804         RPath = RPath.slice(OverlayDirLen, RPath.size());
2805       }
2806       if (!Entry.IsDirectory) {
2807         writeEntry(path::filename(Entry.VPath), RPath);
2808         IsCurrentDirEmpty = false;
2809       }
2810     }
2811 
2812     while (!DirStack.empty()) {
2813       OS << "\n";
2814       endDirectory();
2815     }
2816     OS << "\n";
2817   }
2818 
2819   OS << "  ]\n"
2820      << "}\n";
2821 }
2822 
2823 void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
2824   llvm::sort(Mappings, [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
2825     return LHS.VPath < RHS.VPath;
2826   });
2827 
2828   JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
2829                        IsOverlayRelative, OverlayDir);
2830 }
2831 
2832 vfs::recursive_directory_iterator::recursive_directory_iterator(
2833     FileSystem &FS_, const Twine &Path, std::error_code &EC)
2834     : FS(&FS_) {
2835   directory_iterator I = FS->dir_begin(Path, EC);
2836   if (I != directory_iterator()) {
2837     State = std::make_shared<detail::RecDirIterState>();
2838     State->Stack.push(I);
2839   }
2840 }
2841 
2842 vfs::recursive_directory_iterator &
2843 recursive_directory_iterator::increment(std::error_code &EC) {
2844   assert(FS && State && !State->Stack.empty() && "incrementing past end");
2845   assert(!State->Stack.top()->path().empty() && "non-canonical end iterator");
2846   vfs::directory_iterator End;
2847 
2848   if (State->HasNoPushRequest)
2849     State->HasNoPushRequest = false;
2850   else {
2851     if (State->Stack.top()->type() == sys::fs::file_type::directory_file) {
2852       vfs::directory_iterator I = FS->dir_begin(State->Stack.top()->path(), EC);
2853       if (I != End) {
2854         State->Stack.push(I);
2855         return *this;
2856       }
2857     }
2858   }
2859 
2860   while (!State->Stack.empty() && State->Stack.top().increment(EC) == End)
2861     State->Stack.pop();
2862 
2863   if (State->Stack.empty())
2864     State.reset(); // end iterator
2865 
2866   return *this;
2867 }
2868