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