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