LLVM 24.0.0git
VirtualFileSystem.cpp
Go to the documentation of this file.
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
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/StringSet.h"
22#include "llvm/ADT/Twine.h"
24#include "llvm/Config/llvm-config.h"
26#include "llvm/Support/Chrono.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/Errc.h"
36#include "llvm/Support/Path.h"
37#include "llvm/Support/SMLoc.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
62
65 User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
66 Type(Status.type()), Perms(Status.permissions()) {}
67
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
114 return (Perms & sys::fs::all_exe) != sys::fs::no_perms;
115}
116
117File::~File() = default;
118
119FileSystem::~FileSystem() = default;
120
122FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
123 bool RequiresNullTerminator, bool IsVolatile,
124 bool IsText) {
125 auto F = IsText ? openFileForRead(Name) : openFileForReadBinary(Name);
126 if (!F)
127 return F.getError();
128
129 return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
130}
131
134 return {};
135
136 auto WorkingDir = getCurrentWorkingDirectory();
137 if (!WorkingDir)
138 return WorkingDir.getError();
139
140 sys::path::make_absolute(WorkingDir.get(), Path);
141 return {};
142}
143
144std::error_code FileSystem::getRealPath(const Twine &Path,
145 SmallVectorImpl<char> &Output) {
147}
148
149std::error_code FileSystem::isLocal(const Twine &Path, bool &Result) {
151}
152
153bool FileSystem::exists(const Twine &Path) {
154 auto Status = status(Path);
155 return Status && Status->exists();
156}
157
159 auto StatusA = status(A);
160 if (!StatusA)
161 return StatusA.getError();
162 auto StatusB = status(B);
163 if (!StatusB)
164 return StatusB.getError();
165 return StatusA->equivalent(*StatusB);
166}
167
168#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
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, {}, {}, {}, {}, {},
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(FD, RealStatus))
235 return EC;
236 S = Status::copyWithNewName(RealStatus, 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())
248 if (!FD.isValid() || EC == errc::not_supported)
249 EC = sys::fs::real_path(OriginalName, 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, 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(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(Status.get(), 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(PWD))
297 WD = std::move(EC);
298 else if (llvm::sys::fs::real_path(PWD, RealPWD))
299 WD = WorkingDirectory{PWD, PWD};
300 else
301 WD = WorkingDirectory{PWD, 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(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(Storage);
333 sys::path::make_absolute(WD->get().Resolved, 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(Name, Storage).toVector(Path);
341 Expected<file_t> FDOrErr = sys::fs::openNativeFileForRead(Path, Flags);
342 if (!FDOrErr)
343 return errorToErrorCode(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(adjustPath(Path, Storage), RealStatus))
365 return EC;
366 return Status::copyWithNewName(RealStatus, Path);
367}
368
369ErrorOr<std::unique_ptr<File>>
370RealFileSystem::openFileForRead(const Twine &Name) {
371 auto BypassSandbox = sys::sandbox::scopedDisable();
372
373 return openFileForReadWithFlags(Name, 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, 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(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)
402
403 SmallString<128> Absolute, Resolved, Storage;
404 adjustPath(Path, Storage).toVector(Absolute);
405 bool IsDir;
406 if (auto Err = llvm::sys::fs::is_directory(Absolute, IsDir))
407 return Err;
408 if (!IsDir)
409 return std::make_error_code(std::errc::not_a_directory);
410 if (auto Err = llvm::sys::fs::real_path(Absolute, Resolved))
411 return Err;
412 WD = WorkingDirectory{Absolute, 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(adjustPath(Path, Storage), 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(adjustPath(Path, Storage), 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
449
450std::unique_ptr<FileSystem> vfs::createPhysicalFileSystem() {
452
453 return std::make_unique<RealFileSystem>(false);
454}
455
456std::optional<std::string>
458 ArrayRef<StringRef> FileNames) {
459 SmallString<256> Path;
460 for (; !Dir.empty(); Dir = sys::path::parent_path(Dir)) {
461 for (StringRef FileName : FileNames) {
462 Path = Dir;
463 sys::path::append(Path, 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 {
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);
491 CurrentEntry = (Iter == llvm::sys::fs::directory_iterator())
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>(adjustPath(Dir, Storage), EC));
507}
508
509//===-----------------------------------------------------------------------===/
510// OverlayFileSystem implementation
511//===-----------------------------------------------------------------------===/
512
514 FSList.push_back(std::move(BaseFS));
515}
516
518 FSList.push_back(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
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);
529 return Status;
530 }
532}
533
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
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 }
552}
553
556 // All file systems are synchronized, just take the first working directory.
557 return FSList.front()->getCurrentWorkingDirectory();
558}
559
560std::error_code
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);
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);
581}
582
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) {
592 Callback(*FS);
593 FS->visitChildFileSystems(Callback);
594 }
595}
596
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 + 1);
608}
609
611
612namespace {
613
614/// Combines and deduplicates directory entries across multiple file systems.
615class CombiningDirIterImpl : public llvm::vfs::detail::DirIterImpl {
617
618 /// Iterators to combine, processed in reverse order.
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())
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(CurrentEntry.path());
660 if (SeenNames.insert(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, FEC);
672 if (FEC && FEC != errc::no_such_file_or_directory) {
673 EC = FEC;
674 return;
675 }
676 if (!FEC)
677 IterList.push_back(Iter);
678 }
679 EC = incrementImpl(true);
680 }
681
682 CombiningDirIterImpl(ArrayRef<directory_iterator> DirIters,
683 std::error_code &EC)
684 : IterList(DirIters) {
685 EC = incrementImpl(true);
686 }
687
688 std::error_code increment() override { return incrementImpl(false); }
689};
690
691} // namespace
692
694 std::error_code &EC) {
696 std::make_shared<CombiningDirIterImpl>(FSList, Dir.str(), EC));
697 if (EC)
698 return {};
699 return Combined;
700}
701
702void ProxyFileSystem::anchor() {}
703
704namespace llvm {
705namespace vfs {
706
707namespace detail {
708
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.
719 InMemoryNodeKind Kind;
720 std::string FileName;
721
722public:
724 : Kind(Kind), FileName(std::string(llvm::sys::path::filename(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
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(Stat, 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(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(Stat, 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();
832 Buf->getBuffer(), 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
842 Status Stat;
843 std::map<std::string, std::unique_ptr<InMemoryNode>, std::less<>> Entries;
844
845public:
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(Stat, RequestedName);
854 }
855
856 UniqueID getUniqueID() const { return Stat.getUniqueID(); }
857
859 auto I = Entries.find(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(Name, 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 + 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.
893 return sys::fs::UniqueID(std::numeric_limits<uint64_t>::max(),
894 uint64_t(size_t(Hash)));
895}
897 llvm::StringRef Name,
898 llvm::StringRef Contents) {
899 return getUniqueID(llvm::hash_combine(Parent.getFile(), Name, Contents));
900}
905
915
917 : Root(new detail::InMemoryDirectory(
918 Status("", getDirectoryID(llvm::sys::fs::UniqueID(), ""),
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
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(Path);
939
940 // Fix up relative paths. This just prepends the current working directory.
941 std::error_code EC = makeAbsolute(Path);
942 assert(!EC);
943 (void)EC;
944
945 if (useNormalizedPaths())
946 llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
947
948 if (Path.empty())
949 return false;
950
951 detail::InMemoryDirectory *Dir = Root.get();
952 auto I = llvm::sys::path::begin(Path), E = sys::path::end(Path);
953 const auto ResolvedUser = User.value_or(0);
954 const auto ResolvedGroup = Group.value_or(0);
955 const auto ResolvedType = Type.value_or(sys::fs::file_type::regular_file);
956 const auto ResolvedPerms = Perms.value_or(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(Dir->getUniqueID(), Name),
973 llvm::sys::toTimePoint(ModificationTime), ResolvedUser, ResolvedGroup,
974 0, sys::fs::file_type::directory_file, NewDirectoryPerms);
976 Name, std::make_unique<detail::InMemoryDirectory>(std::move(Stat))));
977 continue;
978 }
979 // Creating file under another file.
981 return false;
983 }
984 detail::InMemoryNode *Node = Dir->getChild(Name);
985 if (!Node) {
986 Dir->addChild(Name,
987 MakeNode({Dir->getUniqueID(), Path, Name, ModificationTime,
988 std::move(Buffer), ResolvedUser, ResolvedGroup,
989 ResolvedType, ResolvedPerms}));
990 return true;
991 }
993 return ResolvedType == sys::fs::file_type::directory_file;
994
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>(Node)) {
1001 return Link->getResolvedFile().getBuffer()->getBuffer() ==
1002 Buffer->getBuffer();
1003 }
1004 return cast<detail::InMemoryFile>(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, std::move(Buffer), User, Group, Type,
1015 Perms,
1017 -> std::unique_ptr<detail::InMemoryNode> {
1018 Status Stat = NNI.makeStatus();
1020 return std::make_unique<detail::InMemoryDirectory>(Stat);
1021 return std::make_unique<detail::InMemoryFile>(
1022 Stat, std::move(NNI.Buffer));
1023 });
1024}
1025
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, llvm::MemoryBuffer::getMemBuffer(Buffer),
1032 std::move(User), std::move(Group), std::move(Type),
1033 std::move(Perms),
1035 -> std::unique_ptr<detail::InMemoryNode> {
1036 Status Stat = NNI.makeStatus();
1038 return std::make_unique<detail::InMemoryDirectory>(Stat);
1039 return std::make_unique<detail::InMemoryFile>(
1040 Stat, std::move(NNI.Buffer));
1041 });
1042}
1043
1045InMemoryFileSystem::lookupNode(const Twine &P, bool FollowFinalSymlink,
1046 size_t SymlinkDepth) const {
1047 SmallString<128> Path;
1048 P.toVector(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, /*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), E = llvm::sys::path::end(Path);
1063 while (true) {
1065 ++I;
1066 if (!Node)
1068
1069 if (auto Symlink = dyn_cast<detail::InMemorySymbolicLink>(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)
1077
1078 SmallString<128> TargetPath = Symlink->getTargetPath();
1079 if (std::error_code EC = makeAbsolute(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(TargetPath, /*FollowFinalSymlink=*/true, SymlinkDepth + 1);
1088 if (!Target || I == E)
1089 return Target;
1090
1093
1094 // Otherwise, continue on the search in the symlinked directory.
1096 continue;
1097 }
1098
1099 // Return the file if it's at the end of the path.
1101 if (I == E)
1102 return detail::NamedNodeOrError(Path, File);
1104 }
1105
1106 // If Node is HardLink then return the resolved file.
1108 if (I == E)
1109 return detail::NamedNodeOrError(Path, &File->getResolvedFile());
1111 }
1112 // Traverse directories.
1114 if (I == E)
1115 return detail::NamedNodeOrError(Path, Dir);
1116 }
1117}
1118
1120 const Twine &Target) {
1121 auto NewLinkNode = lookupNode(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(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>(*TargetNode))
1129 return false;
1130 return addFile(NewLink, 0, nullptr, std::nullopt, std::nullopt, std::nullopt,
1131 std::nullopt, [&](detail::NewInMemoryNodeInfo NNI) {
1132 return std::make_unique<detail::InMemoryHardLink>(
1133 NNI.Path.str(),
1134 *cast<detail::InMemoryFile>(*TargetNode));
1135 });
1136}
1137
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(NewLink, /*FollowFinalSymlink=*/false);
1143 if (NewLinkNode)
1144 return false;
1145
1146 SmallString<128> NewLinkStr, TargetStr;
1147 NewLink.toVector(NewLinkStr);
1148 Target.toVector(TargetStr);
1149
1150 return addFile(NewLinkStr, ModificationTime, nullptr, User, Group,
1153 return std::make_unique<detail::InMemorySymbolicLink>(
1154 NewLinkStr, TargetStr, NNI.makeStatus());
1155 });
1156}
1157
1159 auto Node = lookupNode(Path, /*FollowFinalSymlink=*/true);
1160 if (Node)
1161 return (*Node)->getStatus(Path);
1162 return Node.getError();
1163}
1164
1167 auto Node = lookupNode(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.
1174 return std::unique_ptr<File>(
1175 new detail::InMemoryFileAdaptor(*F, Path.str()));
1176
1177 // FIXME: errc::not_a_file?
1179}
1180
1181/// Adaptor from InMemoryDir::iterator to directory_iterator.
1183 const InMemoryFileSystem *FS;
1186 std::string RequestedDirName;
1187
1188 void setCurrentEntry() {
1189 if (I != E) {
1190 SmallString<256> Path(RequestedDirName);
1191 llvm::sys::path::append(Path, I->second->getFileName());
1193 switch (I->second->getKind()) {
1194 case detail::IME_File:
1197 break;
1200 break;
1202 if (auto SymlinkTarget =
1203 FS->lookupNode(Path, /*FollowFinalSymlink=*/true)) {
1204 Path = SymlinkTarget.getName();
1205 Type = (*SymlinkTarget)->getStatus(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.
1214 }
1215 }
1216
1217public:
1218 DirIterator() = default;
1219
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
1236 std::error_code &EC) {
1237 auto Node = lookupNode(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>(*Node))
1244 return directory_iterator(
1245 std::make_shared<DirIterator>(this, *DirNode, Dir.str()));
1246
1248 return directory_iterator(std::make_shared<DirIterator>());
1249}
1250
1252 SmallString<128> Path;
1253 P.toVector(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, /*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())
1273 Path.toVector(Output);
1274 if (auto EC = makeAbsolute(Output))
1275 return EC;
1276 llvm::sys::path::remove_dots(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.
1303 const size_t n = Path.find_first_of("/\\");
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 =
1320 llvm::sys::path::remove_dots(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) {
1328 return false;
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.
1346 std::string Dir;
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(PathStr, (*Current)->getName());
1357 switch ((*Current)->getKind()) {
1359 [[fallthrough]];
1362 break;
1365 break;
1366 }
1367 CurrentEntry = directory_entry(std::string(PathStr), Type);
1368 } else {
1370 }
1371 return {};
1372 };
1373
1374public:
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,
1399 : Dir(std::move(DirPath)), DirStyle(getExistingStyle(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(ExternalPath);
1408 StringRef File = llvm::sys::path::filename(ExternalPath, ExternalStyle);
1409
1410 SmallString<128> NewPath(Dir);
1411 llvm::sys::path::append(NewPath, DirStyle, 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>
1430 return WorkingDirectory;
1431}
1432
1433std::error_code
1435 // Don't change the working directory if the path doesn't exist.
1436 if (!exists(Path))
1438
1439 SmallString<128> AbsolutePath;
1440 Path.toVector(AbsolutePath);
1441 if (std::error_code EC = makeAbsolute(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(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.
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.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() &&
1484 !sys::path::is_absolute(WorkingDir,
1486 return std::error_code();
1487 }
1491 } else {
1492 // Distinguish between windows_backslash and windows_slash; getExistingStyle
1493 // returns posix for a path with windows_slash.
1494 if (getExistingStyle(WorkingDir) != sys::path::Style::windows_backslash)
1496 }
1497
1498 std::string Result = std::string(WorkingDir);
1499 StringRef Dir(Result);
1500 if (!Dir.ends_with(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(Path.data(), Path.size());
1509 Path.assign(Result.begin(), Result.end());
1510
1511 return {};
1512}
1513
1515 std::error_code &EC) {
1516 SmallString<256> Path;
1517 Dir.toVector(Path);
1518
1519 EC = makeAbsolute(Path);
1520 if (EC)
1521 return {};
1522
1524 if (!Result) {
1525 if (Redirection != RedirectKind::RedirectOnly &&
1526 isFileNotFound(Result.getError()))
1527 return ExternalFS->dir_begin(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(Path, Dir, *Result);
1535 if (!S) {
1536 if (Redirection != RedirectKind::RedirectOnly &&
1537 isFileNotFound(S.getError(), Result->E))
1538 return ExternalFS->dir_begin(Dir, EC);
1539
1540 EC = S.getError();
1541 return {};
1542 }
1543
1544 if (!S->isDirectory()) {
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>(Result->E);
1555 RedirectIter = ExternalFS->dir_begin(*ExtRedirect, RedirectEC);
1556
1557 if (!RE->useExternalName(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 std::string(Path), RedirectIter));
1562 }
1563 } else {
1564 auto DE = cast<DirectoryEntry>(Result->E);
1565 RedirectIter =
1566 directory_iterator(std::make_shared<RedirectingFSDirIterImpl>(
1567 Path, DE->contents_begin(), DE->contents_end(), 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(Path, ExternalEC);
1585 if (ExternalEC) {
1586 if (ExternalEC != errc::no_such_file_or_directory) {
1587 EC = ExternalEC;
1588 return {};
1589 }
1590 ExternalIter = {};
1591 }
1592
1594 switch (Redirection) {
1596 Iters.push_back(ExternalIter);
1597 Iters.push_back(RedirectIter);
1598 break;
1600 Iters.push_back(RedirectIter);
1601 Iters.push_back(ExternalIter);
1602 break;
1603 default:
1604 llvm_unreachable("unhandled RedirectKind");
1605 }
1606
1607 directory_iterator Combined{
1608 std::make_shared<CombiningDirIterImpl>(Iters, EC)};
1609 if (EC)
1610 return {};
1611 return Combined;
1612}
1613
1615 OverlayFileDir = Dir.str();
1616}
1617
1619 return OverlayFileDir;
1620}
1621
1629
1632 Redirection = Kind;
1633}
1634
1635std::vector<StringRef> RedirectingFileSystem::getRoots() const {
1636 std::vector<StringRef> R;
1637 R.reserve(Roots.size());
1638 for (const auto &Root : Roots)
1639 R.push_back(Root->getName());
1640 return R;
1641}
1642
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, Root.get(), IndentLevel);
1653
1654 printIndent(OS, IndentLevel);
1655 OS << "ExternalFS:\n";
1656 ExternalFS->print(OS, Type == PrintType::Contents ? PrintType::Summary : Type,
1657 IndentLevel + 1);
1658}
1659
1662 unsigned IndentLevel) const {
1663 printIndent(OS, IndentLevel);
1664 OS << "'" << E->getName() << "'";
1665
1666 switch (E->getKind()) {
1667 case EK_Directory: {
1669
1670 OS << "\n";
1671 for (std::unique_ptr<Entry> &SubEntry :
1672 llvm::make_range(DE->contents_begin(), DE->contents_end()))
1673 printEntry(OS, SubEntry.get(), IndentLevel + 1);
1674 break;
1675 }
1676 case EK_DirectoryRemap:
1677 case EK_File: {
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
1697 if (ExternalFS) {
1698 Callback(*ExternalFS);
1699 ExternalFS->visitChildFileSystems(Callback);
1700 }
1701}
1702
1703/// A helper class to hold the common YAML parsing state.
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) {
1713
1714 if (!S) {
1715 error(N, "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;
1726 if (!parseScalarString(N, Value, Storage))
1727 return false;
1728
1729 if (Value.equals_insensitive("true") || Value.equals_insensitive("on") ||
1730 Value.equals_insensitive("yes") || Value == "1") {
1731 Result = true;
1732 return true;
1733 } else if (Value.equals_insensitive("false") ||
1734 Value.equals_insensitive("off") ||
1735 Value.equals_insensitive("no") || Value == "0") {
1736 Result = false;
1737 return true;
1738 }
1739
1740 error(N, "expected boolean value");
1741 return false;
1742 }
1743
1744 std::optional<RedirectingFileSystem::RedirectKind>
1745 parseRedirectKind(yaml::Node *N) {
1746 SmallString<12> Storage;
1748 if (!parseScalarString(N, Value, Storage))
1749 return std::nullopt;
1750
1751 if (Value.equals_insensitive("fallthrough")) {
1753 } else if (Value.equals_insensitive("fallback")) {
1755 } else if (Value.equals_insensitive("redirect-only")) {
1757 }
1758 return std::nullopt;
1759 }
1760
1761 std::optional<RedirectingFileSystem::RootRelativeKind>
1762 parseRootRelativeKind(yaml::Node *N) {
1763 SmallString<12> Storage;
1765 if (!parseScalarString(N, Value, Storage))
1766 return std::nullopt;
1767 if (Value.equals_insensitive("cwd")) {
1769 } else if (Value.equals_insensitive("overlay-dir")) {
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,
1787 auto It = Keys.find(Key);
1788 if (It == Keys.end()) {
1789 error(KeyNode, "unknown key");
1790 return false;
1791 }
1792 KeyStatus &S = It->second;
1793 if (S.Seen) {
1794 error(KeyNode, 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(Obj, Twine("missing key '") + I.first + "'");
1806 return false;
1807 }
1808 }
1809 return true;
1810 }
1811
1812public:
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
1825 for (std::unique_ptr<RedirectingFileSystem::Entry> &Content :
1826 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
1827 auto *DirContent =
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 Name, 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(std::move(E));
1843 ParentEntry = FS->Roots.back().get();
1844 return ParentEntry;
1845 }
1846
1847 auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(ParentEntry);
1848 DE->addContent(std::move(E));
1849 return DE->getLastContent();
1850 }
1851
1852private:
1853 void uniqueOverlayTree(RedirectingFileSystem *FS,
1855 RedirectingFileSystem::Entry *NewParentE = nullptr) {
1856 StringRef Name = SrcE->getName();
1857 switch (SrcE->getKind()) {
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, NewParentE);
1865 for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
1866 llvm::make_range(DE->contents_begin(), DE->contents_end()))
1867 uniqueOverlayTree(FS, SubEntry.get(), NewParentE);
1868 break;
1869 }
1871 assert(NewParentE && "Parent entry must exist");
1873 auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE);
1874 DE->addContent(
1875 std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>(
1876 Name, DR->getExternalContentsPath(), DR->getUseName()));
1877 break;
1878 }
1880 assert(NewParentE && "Parent entry must exist");
1882 auto *DE = cast<RedirectingFileSystem::DirectoryEntry>(NewParentE);
1883 DE->addContent(std::make_unique<RedirectingFileSystem::FileEntry>(
1884 Name, FE->getExternalContentsPath(), FE->getUseName()));
1885 break;
1886 }
1887 }
1888 }
1889
1890 std::unique_ptr<RedirectingFileSystem::Entry>
1891 parseEntry(yaml::Node *N, RedirectingFileSystem *FS, bool IsRootEntry) {
1893 if (!M) {
1894 error(N, "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(Fields), std::end(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;
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(I.getKey(), Key, Buffer))
1923 return nullptr;
1924
1925 if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
1926 return nullptr;
1927
1928 StringRef Value;
1929 if (Key == "name") {
1930 if (!parseScalarString(I.getValue(), Value, 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(Value).str();
1937 } else if (Key == "type") {
1938 if (!parseScalarString(I.getValue(), Value, Buffer))
1939 return nullptr;
1940 if (Value == "file")
1942 else if (Value == "directory")
1944 else if (Value == "directory-remap")
1946 else {
1947 error(I.getValue(), "unknown value for 'type'");
1948 return nullptr;
1949 }
1950 } else if (Key == "contents") {
1951 if (ContentsField != CF_NotSet) {
1952 error(I.getKey(),
1953 "entry already has 'contents' or 'external-contents'");
1954 return nullptr;
1955 }
1956 ContentsField = CF_List;
1957 auto *Contents = dyn_cast_if_present<yaml::SequenceNode>(I.getValue());
1958 if (!Contents) {
1959 // FIXME: this is only for directories, what about files?
1960 error(I.getValue(), "expected array");
1961 return nullptr;
1962 }
1963
1964 for (auto &I : *Contents) {
1965 if (std::unique_ptr<RedirectingFileSystem::Entry> E =
1966 parseEntry(&I, FS, /*IsRootEntry*/ false))
1967 EntryArrayContents.push_back(std::move(E));
1968 else
1969 return nullptr;
1970 }
1971 } else if (Key == "external-contents") {
1972 if (ContentsField != CF_NotSet) {
1973 error(I.getKey(),
1974 "entry already has 'contents' or 'external-contents'");
1975 return nullptr;
1976 }
1977 ContentsField = CF_External;
1978 if (!parseScalarString(I.getValue(), Value, 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(FullPath, AbsFullPath)) {
1988 error(N, "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(FullPath);
1999 ExternalContentsPath = FullPath.str();
2000 } else if (Key == "use-external-name") {
2001 bool Val;
2002 if (!parseScalarBool(I.getValue(), Val))
2003 return nullptr;
2004 UseExternalName = Val ? RedirectingFileSystem::NK_External
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, "missing key 'contents' or 'external-contents'");
2017 return nullptr;
2018 }
2019 if (!checkMissingKeys(N, Keys))
2020 return nullptr;
2021
2022 // check invalid configuration
2024 UseExternalName != RedirectingFileSystem::NK_NotSet) {
2025 error(N, "'use-external-name' is not supported for 'directory' entries");
2026 return nullptr;
2027 }
2028
2030 ContentsField == CF_List) {
2031 error(N, "'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(Name, sys::path::Style::posix)) {
2040 path_style = sys::path::Style::posix;
2041 } else if (sys::path::is_absolute(Name,
2042 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(FullPath, Name);
2054 Name = canonicalize(Name);
2055 } else {
2056 EC = FS->makeAbsolute(Name);
2057 }
2058 if (EC) {
2059 assert(NameValueNode && "Name presence should be checked earlier");
2060 error(
2061 NameValueNode,
2062 "entry with relative path at the root level is not discoverable");
2063 return nullptr;
2064 }
2065 path_style = sys::path::is_absolute(Name, 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(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(Trimmed, path_style).size();
2080 while (Trimmed.size() > RootPathLen &&
2081 sys::path::is_separator(Trimmed.back(), path_style))
2082 Trimmed = Trimmed.slice(0, Trimmed.size() - 1);
2083
2084 // Get the last component
2085 StringRef LastComponent = sys::path::filename(Trimmed, path_style);
2086
2087 std::unique_ptr<RedirectingFileSystem::Entry> Result;
2088 switch (Kind) {
2090 Result = std::make_unique<RedirectingFileSystem::FileEntry>(
2091 LastComponent, std::move(ExternalContentsPath), UseExternalName);
2092 break;
2094 Result = std::make_unique<RedirectingFileSystem::DirectoryRemapEntry>(
2095 LastComponent, std::move(ExternalContentsPath), UseExternalName);
2096 break;
2098 Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>(
2099 LastComponent, std::move(EntryArrayContents),
2100 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(Trimmed, 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(Parent, path_style),
2111 E = sys::path::rend(Parent);
2112 I != E; ++I) {
2113 std::vector<std::unique_ptr<RedirectingFileSystem::Entry>> Entries;
2114 Entries.push_back(std::move(Result));
2115 Result = std::make_unique<RedirectingFileSystem::DirectoryEntry>(
2116 *I, std::move(Entries),
2117 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:
2125
2126 // false on error
2128 auto *Top = dyn_cast<yaml::MappingNode>(Root);
2129 if (!Top) {
2130 error(Root, "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(Fields), std::end(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(I.getKey(), Key, KeyBuffer))
2153 return false;
2154
2155 if (!checkDuplicateOrUnknownKey(I.getKey(), Key, Keys))
2156 return false;
2157
2158 if (Key == "roots") {
2159 auto *Roots = dyn_cast_if_present<yaml::SequenceNode>(I.getValue());
2160 if (!Roots) {
2161 error(I.getValue(), "expected array");
2162 return false;
2163 }
2164
2165 for (auto &I : *Roots) {
2166 if (std::unique_ptr<RedirectingFileSystem::Entry> E =
2167 parseEntry(&I, FS, /*IsRootEntry*/ true))
2168 RootEntries.push_back(std::move(E));
2169 else
2170 return false;
2171 }
2172 } else if (Key == "version") {
2173 StringRef VersionString;
2174 SmallString<4> Storage;
2175 if (!parseScalarString(I.getValue(), VersionString, Storage))
2176 return false;
2177 int Version;
2178 if (VersionString.getAsInteger<int>(10, Version)) {
2179 error(I.getValue(), "expected integer");
2180 return false;
2181 }
2182 if (Version < 0) {
2183 error(I.getValue(), "invalid version number");
2184 return false;
2185 }
2186 if (Version != 0) {
2187 error(I.getValue(), "version mismatch, expected 0");
2188 return false;
2189 }
2190 } else if (Key == "case-sensitive") {
2191 if (!parseScalarBool(I.getValue(), FS->CaseSensitive))
2192 return false;
2193 } else if (Key == "overlay-relative") {
2194 if (!parseScalarBool(I.getValue(), FS->IsRelativeOverlay))
2195 return false;
2196 } else if (Key == "use-external-names") {
2197 if (!parseScalarBool(I.getValue(), FS->UseExternalNames))
2198 return false;
2199 } else if (Key == "fallthrough") {
2200 if (Keys["redirecting-with"].Seen) {
2201 error(I.getValue(),
2202 "'fallthrough' and 'redirecting-with' are mutually exclusive");
2203 return false;
2204 }
2205
2206 bool ShouldFallthrough = false;
2207 if (!parseScalarBool(I.getValue(), ShouldFallthrough))
2208 return false;
2209
2210 if (ShouldFallthrough) {
2212 } else {
2214 }
2215 } else if (Key == "redirecting-with") {
2216 if (Keys["fallthrough"].Seen) {
2217 error(I.getValue(),
2218 "'fallthrough' and 'redirecting-with' are mutually exclusive");
2219 return false;
2220 }
2221
2222 if (auto Kind = parseRedirectKind(I.getValue())) {
2223 FS->Redirection = *Kind;
2224 } else {
2225 error(I.getValue(), "expected valid redirect kind");
2226 return false;
2227 }
2228 } else if (Key == "root-relative") {
2229 if (auto Kind = parseRootRelativeKind(I.getValue())) {
2230 FS->RootRelative = *Kind;
2231 } else {
2232 error(I.getValue(), "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(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, E.get());
2251
2252 return true;
2253 }
2254};
2255
2256std::unique_ptr<RedirectingFileSystem>
2257RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
2259 StringRef YAMLFilePath, void *DiagContext,
2260 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
2261 SourceMgr SM;
2262 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
2263
2264 SM.setDiagHandler(DiagHandler, DiagContext);
2265 yaml::document_iterator DI = Stream.begin();
2266 yaml::Node *Root = DI->getRoot();
2267 if (DI == Stream.end() || !Root) {
2268 SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
2269 return nullptr;
2270 }
2271
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(YAMLFilePath);
2287 std::error_code EC = FS->makeAbsolute(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.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
2307
2308 for (auto &Mapping : llvm::reverse(RemappedFiles)) {
2309 SmallString<128> From = StringRef(Mapping.first);
2310 SmallString<128> To = StringRef(Mapping.second);
2311 {
2312 auto EC = ExternalFS->makeAbsolute(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(From);
2326 for (auto I = llvm::sys::path::begin(FromDirectory),
2327 E = llvm::sys::path::end(FromDirectory);
2328 I != E; ++I) {
2330 Parent);
2331 }
2332 assert(Parent && "File without a directory?");
2333 {
2334 auto EC = ExternalFS->makeAbsolute(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 llvm::sys::path::filename(From), To,
2342 UseExternalNames ? RedirectingFileSystem::NK_External
2344 ToEntry = NewFile.get();
2346 std::move(NewFile));
2347 }
2348
2349 return FS;
2350}
2351
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.
2360 SmallString<256> Redirect(DRE->getExternalContentsPath());
2361 sys::path::append(Redirect, Start, End,
2362 getExistingStyle(DRE->getExternalContentsPath()));
2363 ExternalRedirect = std::string(Redirect);
2364 }
2365}
2366
2368 llvm::SmallVectorImpl<char> &Result) const {
2369 Result.clear();
2370 for (Entry *Parent : Parents)
2371 llvm::sys::path::append(Result, Parent->getName());
2372 llvm::sys::path::append(Result, 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(StringRef(Path.data(), Path.size()));
2382 if (CanonicalPath.empty())
2384
2385 Path.assign(CanonicalPath.begin(), CanonicalPath.end());
2386 return {};
2387}
2388
2391 llvm::SmallString<128> CanonicalPath(Path);
2392 if (std::error_code EC = makeCanonicalForLookup(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(CanonicalPath);
2402 for (const auto &Root : Roots) {
2404 lookupPathImpl(Start, End, Root.get(), Entries);
2405 if (UsageTrackingActive && Result && isa<RemapEntry>(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 }
2416}
2417
2419RedirectingFileSystem::lookupPathImpl(
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(*Start, FromName))
2433
2434 ++Start;
2435
2436 if (Start == End) {
2437 // Match!
2438 return LookupResult(From, Start, End);
2439 }
2440 }
2441
2444
2446 return LookupResult(From, Start, End);
2447
2449 for (const std::unique_ptr<RedirectingFileSystem::Entry> &DirEntry :
2450 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
2451 Entries.push_back(From);
2453 lookupPathImpl(Start, End, DirEntry.get(), Entries);
2454 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
2455 return Result;
2456 Entries.pop_back();
2457 }
2458
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(S, 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(RemappedPath))
2484 return EC;
2485
2486 ErrorOr<Status> S = ExternalFS->status(RemappedPath);
2487 if (!S)
2488 return S;
2489 S = Status::copyWithNewName(*S, *ExtRedirect);
2491 return getRedirectedFileStatus(OriginalPath,
2492 RE->useExternalName(UseExternalNames), *S);
2493 }
2494
2496 return Status::copyWithNewName(DE->getStatus(), LookupPath);
2497}
2498
2499ErrorOr<Status>
2500RedirectingFileSystem::getExternalStatus(const Twine &LookupPath,
2501 const Twine &OriginalPath) const {
2502 auto Result = ExternalFS->status(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(Result.get(), OriginalPath);
2509}
2510
2511ErrorOr<Status> RedirectingFileSystem::status(const Twine &OriginalPath) {
2512 SmallString<256> Path;
2513 OriginalPath.toVector(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(Path, OriginalPath);
2522 if (S)
2523 return S;
2524 }
2525
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(Result.getError()))
2532 return getExternalStatus(Path, OriginalPath);
2533 return Result.getError();
2534 }
2535
2536 ErrorOr<Status> S = status(Path, OriginalPath, *Result);
2537 if (!S && Redirection == RedirectKind::Fallthrough &&
2538 isFileNotFound(S.getError(), 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(Path, OriginalPath);
2543 }
2544
2545 return S;
2546}
2547
2548bool RedirectingFileSystem::exists(const Twine &OriginalPath) {
2549 SmallString<256> Path;
2550 OriginalPath.toVector(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
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(Result.getError()))
2568 return ExternalFS->exists(Path);
2569 return false;
2570 }
2571
2572 std::optional<StringRef> ExtRedirect = Result->getExternalRedirect();
2573 if (!ExtRedirect) {
2575 return true;
2576 }
2577
2578 SmallString<256> RemappedPath((*ExtRedirect).str());
2579 if (makeAbsolute(RemappedPath))
2580 return false;
2581
2582 if (ExternalFS->exists(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; }
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(S, 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
2638 SmallString<256> Path;
2639 OriginalPath.toVector(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(ExternalFS->openFileForRead(Path), OriginalPath);
2648 if (F)
2649 return F;
2650 }
2651
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(Result.getError()))
2658 return File::getWithPath(ExternalFS->openFileForRead(Path), OriginalPath);
2659 return Result.getError();
2660 }
2661
2662 if (!Result->getExternalRedirect()) // FIXME: errc::not_a_file?
2664
2665 StringRef ExtRedirect = *Result->getExternalRedirect();
2666 SmallString<256> RemappedPath(ExtRedirect.str());
2667 if (std::error_code EC = makeAbsolute(RemappedPath))
2668 return EC;
2669
2670 auto *RE = cast<RedirectingFileSystem::RemapEntry>(Result->E);
2671
2672 auto ExternalFile =
2673 File::getWithPath(ExternalFS->openFileForRead(RemappedPath), ExtRedirect);
2674 if (!ExternalFile) {
2675 if (Redirection == RedirectKind::Fallthrough &&
2676 isFileNotFound(ExternalFile.getError(), 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(ExternalFS->openFileForRead(Path), 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.
2692 OriginalPath, RE->useExternalName(UseExternalNames), *ExternalStatus);
2693 return std::unique_ptr<File>(
2694 std::make_unique<FileWithFixedStatus>(std::move(*ExternalFile), S));
2695}
2696
2697std::error_code
2699 SmallVectorImpl<char> &Output) {
2700 SmallString<256> Path;
2701 OriginalPath.toVector(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
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(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(*ExtRedirect, Output);
2728 if (P && Redirection == RedirectKind::Fallthrough &&
2729 isFileNotFound(P, 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(Output);
2742 return {};
2743 }
2745}
2746
2748 const Twine &Dir, SmallVectorImpl<std::string> &Out) {
2749 SmallString<256> Path;
2750 Dir.toVector(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
2759 if (!Result) {
2760 // dir_begin() delegates entirely to ExternalFS.
2761 if (ConsultsExternalFS)
2762 ExternalFS->getDirectoryContentRealSources(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(RemappedPath))
2777 ExternalFS->getDirectoryContentRealSources(RemappedPath, Out);
2778 break;
2779 }
2780 }
2781
2782 // dir_begin() also iterates the original path in ExternalFS.
2783 if (ConsultsExternalFS)
2784 ExternalFS->getDirectoryContentRealSources(Path, Out);
2785}
2786
2787std::unique_ptr<FileSystem>
2788vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
2790 StringRef YAMLFilePath, void *DiagContext,
2791 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
2792 return RedirectingFileSystem::create(std::move(Buffer), DiagHandler,
2793 YAMLFilePath, DiagContext,
2794 std::move(ExternalFS));
2795}
2796
2800 auto Kind = SrcE->getKind();
2803 assert(DE && "Must be a directory");
2804 for (std::unique_ptr<RedirectingFileSystem::Entry> &SubEntry :
2805 llvm::make_range(DE->contents_begin(), DE->contents_end())) {
2806 Path.push_back(SubEntry->getName());
2807 getVFSEntries(SubEntry.get(), Path, Entries);
2808 Path.pop_back();
2809 }
2810 return;
2811 }
2812
2815 assert(DR && "Must be a directory remap");
2816 SmallString<128> VPath;
2817 for (auto &Comp : Path)
2818 llvm::sys::path::append(VPath, Comp);
2819 Entries.push_back(
2820 YAMLVFSEntry(VPath.c_str(), DR->getExternalContentsPath()));
2821 return;
2822 }
2823
2824 assert(Kind == RedirectingFileSystem::EK_File && "Must be a EK_File");
2826 assert(FE && "Must be a file");
2827 SmallString<128> VPath;
2828 for (auto &Comp : Path)
2829 llvm::sys::path::append(VPath, Comp);
2830 Entries.push_back(YAMLVFSEntry(VPath.c_str(), FE->getExternalContentsPath()));
2831}
2832
2834 SmallVectorImpl<YAMLVFSEntry> &CollectedEntries) {
2836 if (!RootResult)
2837 return;
2838 SmallVector<StringRef, 8> Components;
2839 Components.push_back("/");
2840 getVFSEntries(RootResult->E, Components, CollectedEntries);
2841}
2842
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(VirtualPath, RealPath, IsDirectory);
2857}
2858
2860 addEntry(VirtualPath, RealPath, /*IsDirectory=*/false);
2861}
2862
2864 StringRef RealPath) {
2865 addEntry(VirtualPath, RealPath, /*IsDirectory=*/true);
2866}
2867
2868namespace {
2869
2870class JSONWriter {
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(Parent), EParent = path::end(Parent);
2898 for (auto IChild = path::begin(Path), EChild = path::end(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(Parent.size() + 1);
2911}
2912
2913void JSONWriter::startDirectory(StringRef Path) {
2914 StringRef Name =
2915 DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
2916 DirStack.push_back(Path);
2917 unsigned Indent = getDirIndent();
2918 OS.indent(Indent) << "{\n";
2919 OS.indent(Indent + 2) << "'type': 'directory',\n";
2920 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
2921 OS.indent(Indent + 2) << "'contents': [\n";
2922}
2923
2924void JSONWriter::endDirectory() {
2925 unsigned Indent = getDirIndent();
2926 OS.indent(Indent + 2) << "]\n";
2927 OS.indent(Indent) << "}";
2928
2929 DirStack.pop_back();
2930}
2931
2932void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
2933 unsigned Indent = getFileIndent();
2934 OS.indent(Indent) << "{\n";
2935 OS.indent(Indent + 2) << "'type': 'file',\n";
2936 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
2937 OS.indent(Indent + 2) << "'external-contents': \""
2938 << llvm::yaml::escape(RPath) << "\"\n";
2939 OS.indent(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 Entry.IsDirectory ? Entry.VPath : path::parent_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(OverlayDir.size());
2977 }
2978
2979 bool IsCurrentDirEmpty = true;
2980 if (!Entry.IsDirectory) {
2981 writeEntry(path::filename(Entry.VPath), RPath);
2982 IsCurrentDirEmpty = false;
2983 }
2984
2985 for (const auto &Entry : Entries.slice(1)) {
2986 StringRef Dir =
2987 Entry.IsDirectory ? Entry.VPath : path::parent_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(DirStack.back(), Dir)) {
2995 OS << "\n";
2996 endDirectory();
2997 IsDirPoppedFromStack = true;
2998 }
2999 if (IsDirPoppedFromStack || !IsCurrentDirEmpty) {
3000 OS << ",\n";
3001 }
3002 startDirectory(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(OverlayDir.size());
3010 }
3011 if (!Entry.IsDirectory) {
3012 writeEntry(path::filename(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
3029 llvm::sort(Mappings, [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
3030 return LHS.VPath < RHS.VPath;
3031 });
3032
3033 JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive,
3034 IsOverlayRelative, OverlayDir);
3035}
3036
3038 FileSystem &FS_, const Twine &Path, std::error_code &EC)
3039 : FS(&FS_) {
3040 directory_iterator I = FS->dir_begin(Path, EC);
3041 if (I != directory_iterator()) {
3042 State = std::make_shared<detail::RecDirIterState>();
3043 State->Stack.push_back(I);
3044 }
3045}
3046
3049 assert(FS && State && !State->Stack.empty() && "incrementing past end");
3050 assert(!State->Stack.back()->path().empty() && "non-canonical end iterator");
3052
3053 if (State->HasNoPushRequest)
3054 State->HasNoPushRequest = false;
3055 else {
3056 if (State->Stack.back()->type() == sys::fs::file_type::directory_file) {
3058 FS->dir_begin(State->Stack.back()->path(), EC);
3059 if (I != End) {
3060 State->Stack.push_back(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}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseMap class.
Provides ErrorOr<T> smart pointer.
static void makeAbsolute(vfs::FileSystem &VFS, SmallVectorImpl< char > &Path)
Make Path absolute.
This file defines the RefCountedBase, ThreadSafeRefCountedBase, and IntrusiveRefCntPtr classes.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static void printImpl(const MCAsmInfo &MAI, raw_ostream &OS, const MCSpecifierExpr &Expr)
#define P(N)
static StringRef getName(Value *V)
const char * Msg
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallString class.
This file defines the SmallVector class.
StringSet - A set-like wrapper for the StringMap.
#define error(X)
static void DiagHandler(const SMDiagnostic &Diag, void *Context)
static void getVFSEntries(RedirectingFileSystem::Entry *SrcE, SmallVectorImpl< StringRef > &Path, SmallVectorImpl< YAMLVFSEntry > &Entries)
static Status getRedirectedFileStatus(const Twine &OriginalPath, bool UseExternalNames, Status ExternalStatus)
static bool pathHasTraversal(StringRef Path)
static bool isTraversalComponent(StringRef Component)
Defines the virtual file system interface vfs::FileSystem.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const T & front() const
Get the first element.
Definition ArrayRef.h:144
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:185
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:766
iterator end()
Definition DenseMap.h:686
Represents either an error or a value T.
Definition ErrorOr.h:56
std::error_code getError() const
Definition ErrorOr.h:152
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
This interface provides simple read-only access to a block of memory, and provides simple methods for...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
StringRef getBuffer() const
Represents a location in source code.
Definition SMLoc.h:22
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:28
const char * c_str()
StringRef str() const
Explicit conversion to StringRef.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:34
LLVM_ABI void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
Definition SourceMgr.h:46
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Definition SourceMgr.h:128
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:490
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
char back() const
Get the last character in the string.
Definition StringRef.h:153
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition StringRef.h:720
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition StringSet.h:27
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition StringSet.h:41
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
LLVM_ABI void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition Twine.cpp:32
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
An opaque object representing a hash code.
Definition Hashing.h:77
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & write(unsigned char C)
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
uint64_t getFile() const
Definition UniqueID.h:48
file_type type() const
const std::string & path() const
directory_iterator - Iterates through the entries in path.
directory_iterator & increment(std::error_code &ec)
Represents the result of a call to sys::fs::status().
Definition FileSystem.h:214
The virtual file system interface.
llvm::function_ref< void(FileSystem &)> VisitCallbackTy
virtual llvm::ErrorOr< std::string > getCurrentWorkingDirectory() const =0
Get the working directory of this file system.
virtual bool exists(const Twine &Path)
Check whether Path exists.
virtual llvm::ErrorOr< std::unique_ptr< File > > openFileForReadBinary(const Twine &Path)
Get a File object for the binary file at Path, if one exists.
virtual std::error_code makeAbsolute(SmallVectorImpl< char > &Path) const
Make Path an absolute path.
virtual llvm::ErrorOr< std::unique_ptr< File > > openFileForRead(const Twine &Path)=0
Get a File object for the text file at Path, if one exists.
virtual std::error_code getRealPath(const Twine &Path, SmallVectorImpl< char > &Output)
Gets real path of Path e.g.
void printIndent(raw_ostream &OS, unsigned IndentLevel) const
LLVM_DUMP_METHOD void dump() const
void print(raw_ostream &OS, PrintType Type=PrintType::Contents, unsigned IndentLevel=0) const
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const Twine &Name, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false, bool IsText=true)
This is a convenience method that opens a file, gets its content and then closes the file.
llvm::ErrorOr< bool > equivalent(const Twine &A, const Twine &B)
virtual std::error_code isLocal(const Twine &Path, bool &Result)
Is the file mounted on a local filesystem?
virtual llvm::ErrorOr< Status > status(const Twine &Path)=0
Get the status of the entry at Path, if one exists.
Represents an open file.
static ErrorOr< std::unique_ptr< File > > getWithPath(ErrorOr< std::unique_ptr< File > > Result, const Twine &P)
virtual ~File()
Destroy the file after closing it (if open).
Adaptor from InMemoryDir::iterator to directory_iterator.
DirIterator(const InMemoryFileSystem *FS, const detail::InMemoryDirectory &Dir, std::string RequestedDirName)
std::error_code increment() override
Sets CurrentEntry to the next entry in the directory on success, to directory_entry() at end,...
std::error_code isLocal(const Twine &Path, bool &Result) override
directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override
std::error_code getRealPath(const Twine &Path, SmallVectorImpl< char > &Output) override
Canonicalizes Path by combining with the current working directory and normalizing the path (e....
static constexpr size_t MaxSymlinkDepth
Arbitrary max depth to search through symlinks.
InMemoryFileSystem(bool UseNormalizedPaths=true)
bool useNormalizedPaths() const
Return true if this file system normalizes . and .. in paths.
void printImpl(raw_ostream &OS, PrintType Type, unsigned IndentLevel) const override
llvm::ErrorOr< std::string > getCurrentWorkingDirectory() const override
bool addHardLink(const Twine &NewLink, const Twine &Target)
Add a hard link to a file.
bool addFileNoOwn(const Twine &Path, time_t ModificationTime, const llvm::MemoryBufferRef &Buffer, std::optional< uint32_t > User=std::nullopt, std::optional< uint32_t > Group=std::nullopt, std::optional< llvm::sys::fs::file_type > Type=std::nullopt, std::optional< llvm::sys::fs::perms > Perms=std::nullopt)
Add a buffer to the VFS with a path.
bool addSymbolicLink(const Twine &NewLink, const Twine &Target, time_t ModificationTime, std::optional< uint32_t > User=std::nullopt, std::optional< uint32_t > Group=std::nullopt, std::optional< llvm::sys::fs::perms > Perms=std::nullopt)
Add a symbolic link.
std::error_code setCurrentWorkingDirectory(const Twine &Path) override
llvm::ErrorOr< Status > status(const Twine &Path) override
llvm::ErrorOr< std::unique_ptr< File > > openFileForRead(const Twine &Path) override
directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override
void visitChildFileSystems(VisitCallbackTy Callback) override
llvm::ErrorOr< std::unique_ptr< File > > openFileForRead(const Twine &Path) override
std::error_code getRealPath(const Twine &Path, SmallVectorImpl< char > &Output) override
std::error_code setCurrentWorkingDirectory(const Twine &Path) override
void pushOverlay(IntrusiveRefCntPtr< FileSystem > FS)
Pushes a file system on top of the stack.
OverlayFileSystem(IntrusiveRefCntPtr< FileSystem > Base)
llvm::ErrorOr< std::string > getCurrentWorkingDirectory() const override
void getDirectoryContentRealSources(const Twine &Dir, SmallVectorImpl< std::string > &Out) override
iterator overlays_end()
Get an iterator pointing one-past the least recently added file system.
std::error_code isLocal(const Twine &Path, bool &Result) override
bool exists(const Twine &Path) override
llvm::ErrorOr< Status > status(const Twine &Path) override
iterator overlays_begin()
Get an iterator pointing to the most recently added file system.
FileSystemList::reverse_iterator iterator
void printImpl(raw_ostream &OS, PrintType Type, unsigned IndentLevel) const override
Directory iterator implementation for RedirectingFileSystem's directory entries.
std::error_code increment() override
Sets CurrentEntry to the next entry in the directory on success, to directory_entry() at end,...
RedirectingFSDirIterImpl(const Twine &Path, RedirectingFileSystem::DirectoryEntry::iterator Begin, RedirectingFileSystem::DirectoryEntry::iterator End, std::error_code &EC)
A helper class to hold the common YAML parsing state.
static RedirectingFileSystem::Entry * lookupOrCreateEntry(RedirectingFileSystem *FS, StringRef Name, RedirectingFileSystem::Entry *ParentEntry=nullptr)
bool parse(yaml::Node *Root, RedirectingFileSystem *FS)
A single file or directory in the VFS.
A virtual file system parsed from a YAML file.
@ OverlayDir
The roots are relative to the directory where the Overlay YAML file.
@ CWD
The roots are relative to the current working directory.
bool exists(const Twine &Path) override
Check whether Path exists.
void printImpl(raw_ostream &OS, PrintType Type, unsigned IndentLevel) const override
std::vector< llvm::StringRef > getRoots() const
directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override
Get a directory_iterator for Dir.
ErrorOr< LookupResult > lookupPath(StringRef Path) const
Looks up Path in Roots and returns a LookupResult giving the matched entry and, if the entry was a Fi...
RedirectKind
The type of redirection to perform.
@ Fallthrough
Lookup the redirected path first (ie.
@ Fallback
Lookup the provided path first and if that fails, "fallback" to a lookup of the redirected path.
@ RedirectOnly
Only lookup the redirected path, do not lookup the originally provided path.
void setFallthrough(bool Fallthrough)
Sets the redirection kind to Fallthrough if true or RedirectOnly otherwise.
void visitChildFileSystems(VisitCallbackTy Callback) override
std::error_code getRealPath(const Twine &Path, SmallVectorImpl< char > &Output) override
Gets real path of Path e.g.
ErrorOr< std::unique_ptr< File > > openFileForRead(const Twine &Path) override
Get a File object for the text file at Path, if one exists.
void setOverlayFileDir(StringRef PrefixDir)
llvm::ErrorOr< std::string > getCurrentWorkingDirectory() const override
Get the working directory of this file system.
void setRedirection(RedirectingFileSystem::RedirectKind Kind)
std::error_code isLocal(const Twine &Path, bool &Result) override
Is the file mounted on a local filesystem?
static std::unique_ptr< RedirectingFileSystem > create(std::unique_ptr< MemoryBuffer > Buffer, SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath, void *DiagContext, IntrusiveRefCntPtr< FileSystem > ExternalFS)
Parses Buffer, which is expected to be in YAML format and returns a virtual file system representing ...
std::error_code setCurrentWorkingDirectory(const Twine &Path) override
Set the working directory.
void printEntry(raw_ostream &OS, Entry *E, unsigned IndentLevel=0) const
void getDirectoryContentRealSources(const Twine &Dir, SmallVectorImpl< std::string > &Out) override
Collect the paths in the real file system that contribute to a potentially virtual directory when ite...
The result of a status operation.
llvm::sys::fs::UniqueID getUniqueID() const
uint32_t getUser() const
LLVM_ABI bool equivalent(const Status &Other) const
static LLVM_ABI Status copyWithNewName(const Status &In, const Twine &NewName)
Get a copy of a Status with a different name.
uint64_t getSize() const
LLVM_ABI bool isStatusKnown() const
LLVM_ABI bool exists() const
bool ExposesExternalVFSPath
Whether this entity has an external path different from the virtual path, and the external path is ex...
uint32_t getGroup() const
static LLVM_ABI Status copyWithNewSize(const Status &In, uint64_t NewSize)
Get a copy of a Status with a different size.
LLVM_ABI bool isOther() const
LLVM_ABI bool isSymlink() const
llvm::sys::TimePoint getLastModificationTime() const
llvm::sys::fs::file_type getType() const
LLVM_ABI bool isRegularFile() const
LLVM_ABI bool isExecutable() const
Returns true if any of the owner, group, or others execute permissions is set.
LLVM_ABI bool isDirectory() const
LLVM_ABI void addFileMapping(StringRef VirtualPath, StringRef RealPath)
LLVM_ABI void write(llvm::raw_ostream &OS)
LLVM_ABI void addDirectoryMapping(StringRef VirtualPath, StringRef RealPath)
InMemoryNode * addChild(StringRef Name, std::unique_ptr< InMemoryNode > Child)
Status getStatus(const Twine &RequestedName) const override
Return the Status for this node.
static bool classof(const InMemoryNode *N)
InMemoryNode * getChild(StringRef Name) const
decltype(Entries)::const_iterator const_iterator
std::string toString(unsigned Indent) const override
Status getStatus(const Twine &RequestedName) const override
Return the Status for this node.
std::string toString(unsigned Indent) const override
InMemoryFile(Status Stat, std::unique_ptr< llvm::MemoryBuffer > Buffer)
static bool classof(const InMemoryNode *N)
llvm::MemoryBuffer * getBuffer() const
The in memory file system is a tree of Nodes.
StringRef getFileName() const
Get the filename of this node (the name without the directory part).
virtual ~InMemoryNode()=default
InMemoryNode(llvm::StringRef FileName, InMemoryNodeKind Kind)
virtual std::string toString(unsigned Indent) const =0
virtual Status getStatus(const Twine &RequestedName) const =0
Return the Status for this node.
A member of a directory, yielded by a directory_iterator.
llvm::StringRef path() const
llvm::sys::fs::file_type type() const
An input iterator over the entries in a virtual path, similar to llvm::sys::fs::directory_iterator.
directory_iterator & increment(std::error_code &EC)
Equivalent to operator++, with an error code.
An input iterator over the recursive contents of a virtual path, similar to llvm::sys::fs::recursive_...
recursive_directory_iterator()=default
Construct an 'end' iterator.
LLVM_ABI recursive_directory_iterator & increment(std::error_code &EC)
Equivalent to operator++, with an error code.
Abstract base class for all Nodes.
Definition YAMLParser.h:121
This class represents a YAML stream potentially containing multiple documents.
Definition YAMLParser.h:88
LLVM_ABI document_iterator end()
LLVM_ABI document_iterator begin()
LLVM_ABI bool failed()
LLVM_ABI void printError(Node *N, const Twine &Msg, SourceMgr::DiagKind Kind=SourceMgr::DK_Error)
Iterator abstraction for Documents over a Stream.
Definition YAMLParser.h:595
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Resolved
Queried, materialization begun.
Definition Core.h:543
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
@ OF_Text
The file should be opened in text mode on platforms like z/OS that make this distinction.
Definition FileSystem.h:793
file_type
An enumeration for the file system's view of the type.
Definition FileSystem.h:54
LLVM_ABI std::error_code getRealPathFromHandle(file_t Handle, SmallVectorImpl< char > &RealPath)
Get the real path of an open file from its handle.
LLVM_ABI std::error_code set_current_path(const Twine &path)
Set the current path.
LLVM_ABI std::error_code real_path(const Twine &path, SmallVectorImpl< char > &output, bool expand_tilde=false)
Collapse all .
LLVM_ABI Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
LLVM_ABI std::error_code current_path(SmallVectorImpl< char > &result)
Get the current path.
LLVM_ABI std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
LLVM_ABI std::error_code is_local(const Twine &path, bool &result)
Is the file mounted on a local filesystem?
LLVM_ABI std::error_code openFileForRead(const Twine &Name, int &ResultFD, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
LLVM_ABI bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition Path.cpp:1122
LLVM_ABI StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition Path.cpp:626
LLVM_ABI StringRef root_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get root path.
Definition Path.cpp:359
LLVM_ABI const_iterator begin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get begin iterator over path.
Definition Path.cpp:237
LLVM_ABI bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
Remove '.
Definition Path.cpp:779
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:478
LLVM_ABI void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition Path.cpp:720
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:594
LLVM_ABI StringRef remove_leading_dotslash(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition Path.cpp:688
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:467
LLVM_ABI reverse_iterator rend(StringRef path LLVM_LIFETIME_BOUND)
Get reverse end iterator over path.
LLVM_ABI reverse_iterator rbegin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get reverse begin iterator over path.
LLVM_ABI const_iterator end(StringRef path LLVM_LIFETIME_BOUND)
Get end iterator over path.
Definition Path.cpp:246
LLVM_ABI bool is_separator(char value, Style style=Style::native)
Check whether the given char is a path separator on the host OS.
Definition Path.cpp:618
void violationIfEnabled()
Definition IOSandbox.h:37
ScopedSetting scopedDisable()
Definition IOSandbox.h:36
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition Chrono.h:34
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition Chrono.h:65
std::error_code make_error_code(OutputErrorCode EV)
LLVM_ABI void collectVFSEntries(RedirectingFileSystem &VFS, SmallVectorImpl< YAMLVFSEntry > &CollectedEntries)
Collect all pairs of <virtual path, real path> entries from the VFS.
LLVM_ABI std::unique_ptr< FileSystem > createPhysicalFileSystem()
Create an vfs::FileSystem for the 'real' file system, as seen by the operating system.
static sys::fs::UniqueID getFileID(sys::fs::UniqueID Parent, llvm::StringRef Name, llvm::StringRef Contents)
LLVM_ABI llvm::sys::fs::UniqueID getNextVirtualUniqueID()
Get a globally unique ID for a virtual file or directory.
static sys::fs::UniqueID getUniqueID(hash_code Hash)
LLVM_ABI std::optional< std::string > findFileInParentDirectories(FileSystem &FS, StringRef Dir, ArrayRef< StringRef > FileNames)
Searches Dir and then each of its parent directories for a regular file named one of FileNames,...
LLVM_ABI IntrusiveRefCntPtr< FileSystem > getRealFileSystem()
Gets an vfs::FileSystem for the 'real' file system, as seen by the operating system.
LLVM_ABI std::unique_ptr< FileSystem > getVFSFromYAML(std::unique_ptr< llvm::MemoryBuffer > Buffer, llvm::SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath, void *DiagContext=nullptr, IntrusiveRefCntPtr< FileSystem > ExternalFS=getRealFileSystem())
Gets a FileSystem for a virtual file system described in YAML format.
static sys::fs::UniqueID getDirectoryID(sys::fs::UniqueID Parent, llvm::StringRef Name)
LLVM_ABI std::string escape(StringRef Input, bool EscapePrintable=true)
Escape Input for a double quoted scalar; if EscapePrintable is true, all UTF8 sequences will be escap...
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
IntrusiveRefCntPtr< T > makeIntrusiveRefCnt(Args &&...A)
Factory function for creating intrusive ref counted pointers.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
@ not_a_directory
Definition Errc.h:67
@ no_such_file_or_directory
Definition Errc.h:65
@ operation_not_permitted
Definition Errc.h:70
@ not_supported
Definition Errc.h:69
@ invalid_argument
Definition Errc.h:56
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
@ Other
Any other memory.
Definition ModRef.h:68
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:307
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition Error.cpp:113
LLVM_ABI Error write(DWPWriter &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue, raw_pwrite_stream *OS=nullptr)
Definition DWP.cpp:746
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
#define N
Status()=default
static constexpr value_type Invalid
Value for an invalid file descriptor.
Definition File.h:31
This class wraps the platform specific file handle/descriptor type to provide an unified representati...
Definition File.h:21
bool isValid() const
Is a valid file.
Definition File.h:43
Entry * E
The entry the looked-up path corresponds to.
LLVM_ABI LookupResult(Entry *E, sys::path::const_iterator Start, sys::path::const_iterator End)
LLVM_ABI void getPath(llvm::SmallVectorImpl< char > &Path) const
Get the (canonical) path of the found entry.
llvm::SmallVector< Entry *, 32 > Parents
Chain of parent directory entries for E.
An interface for virtual file systems to provide an iterator over the (non-recursive) contents of a d...
std::unique_ptr< llvm::MemoryBuffer > Buffer