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