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