1 //===- VirtualFileSystem.cpp - Virtual File System Layer --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // This file implements the VirtualFileSystem interface.
10 //===----------------------------------------------------------------------===//
12 #include "clang/Basic/VirtualFileSystem.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/ADT/iterator_range.h"
18 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/YAMLParser.h"
25 using namespace clang;
26 using namespace clang::vfs;
28 using llvm::sys::fs::file_status;
29 using llvm::sys::fs::file_type;
30 using llvm::sys::fs::perms;
31 using llvm::sys::fs::UniqueID;
33 Status::Status(const file_status &Status)
34 : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
35 User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
36 Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false) {}
38 Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID,
39 sys::TimeValue MTime, uint32_t User, uint32_t Group,
40 uint64_t Size, file_type Type, perms Perms)
41 : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size),
42 Type(Type), Perms(Perms), IsVFSMapped(false) {}
44 bool Status::equivalent(const Status &Other) const {
45 return getUniqueID() == Other.getUniqueID();
47 bool Status::isDirectory() const {
48 return Type == file_type::directory_file;
50 bool Status::isRegularFile() const {
51 return Type == file_type::regular_file;
53 bool Status::isOther() const {
54 return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
56 bool Status::isSymlink() const {
57 return Type == file_type::symlink_file;
59 bool Status::isStatusKnown() const {
60 return Type != file_type::status_error;
62 bool Status::exists() const {
63 return isStatusKnown() && Type != file_type::file_not_found;
68 FileSystem::~FileSystem() {}
70 ErrorOr<std::unique_ptr<MemoryBuffer>>
71 FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize,
72 bool RequiresNullTerminator, bool IsVolatile) {
73 auto F = openFileForRead(Name);
77 return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile);
80 //===-----------------------------------------------------------------------===/
81 // RealFileSystem implementation
82 //===-----------------------------------------------------------------------===/
85 /// \brief Wrapper around a raw file descriptor.
86 class RealFile : public File {
89 friend class RealFileSystem;
90 RealFile(int FD) : FD(FD) {
91 assert(FD >= 0 && "Invalid or inactive file descriptor");
96 ErrorOr<Status> status() override;
97 ErrorOr<std::unique_ptr<MemoryBuffer>>
98 getBuffer(const Twine &Name, int64_t FileSize = -1,
99 bool RequiresNullTerminator = true,
100 bool IsVolatile = false) override;
101 std::error_code close() override;
102 void setName(StringRef Name) override;
104 } // end anonymous namespace
105 RealFile::~RealFile() { close(); }
107 ErrorOr<Status> RealFile::status() {
108 assert(FD != -1 && "cannot stat closed file");
109 if (!S.isStatusKnown()) {
110 file_status RealStatus;
111 if (std::error_code EC = sys::fs::status(FD, RealStatus))
113 Status NewS(RealStatus);
114 NewS.setName(S.getName());
120 ErrorOr<std::unique_ptr<MemoryBuffer>>
121 RealFile::getBuffer(const Twine &Name, int64_t FileSize,
122 bool RequiresNullTerminator, bool IsVolatile) {
123 assert(FD != -1 && "cannot get buffer for closed file");
124 return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator,
128 // FIXME: This is terrible, we need this for ::close.
129 #if !defined(_MSC_VER) && !defined(__MINGW32__)
135 #define S_ISFIFO(x) (0)
138 std::error_code RealFile::close() {
140 return std::error_code(errno, std::generic_category());
142 return std::error_code();
145 void RealFile::setName(StringRef Name) {
150 /// \brief The file system according to your operating system.
151 class RealFileSystem : public FileSystem {
153 ErrorOr<Status> status(const Twine &Path) override;
154 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
155 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override;
157 } // end anonymous namespace
159 ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
160 sys::fs::file_status RealStatus;
161 if (std::error_code EC = sys::fs::status(Path, RealStatus))
163 Status Result(RealStatus);
164 Result.setName(Path.str());
168 ErrorOr<std::unique_ptr<File>>
169 RealFileSystem::openFileForRead(const Twine &Name) {
171 if (std::error_code EC = sys::fs::openFileForRead(Name, FD))
173 std::unique_ptr<File> Result(new RealFile(FD));
174 Result->setName(Name.str());
175 return std::move(Result);
178 IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
179 static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
184 class RealFSDirIter : public clang::vfs::detail::DirIterImpl {
186 llvm::sys::fs::directory_iterator Iter;
188 RealFSDirIter(const Twine &_Path, std::error_code &EC)
189 : Path(_Path.str()), Iter(Path, EC) {
190 if (!EC && Iter != llvm::sys::fs::directory_iterator()) {
191 llvm::sys::fs::file_status S;
192 EC = Iter->status(S);
194 CurrentEntry = Status(S);
195 CurrentEntry.setName(Iter->path());
200 std::error_code increment() override {
205 } else if (Iter == llvm::sys::fs::directory_iterator()) {
206 CurrentEntry = Status();
208 llvm::sys::fs::file_status S;
209 EC = Iter->status(S);
210 CurrentEntry = Status(S);
211 CurrentEntry.setName(Iter->path());
218 directory_iterator RealFileSystem::dir_begin(const Twine &Dir,
219 std::error_code &EC) {
220 return directory_iterator(std::make_shared<RealFSDirIter>(Dir, EC));
223 //===-----------------------------------------------------------------------===/
224 // OverlayFileSystem implementation
225 //===-----------------------------------------------------------------------===/
226 OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
230 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
231 FSList.push_back(FS);
234 ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
235 // FIXME: handle symlinks that cross file systems
236 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
237 ErrorOr<Status> Status = (*I)->status(Path);
238 if (Status || Status.getError() != llvm::errc::no_such_file_or_directory)
241 return make_error_code(llvm::errc::no_such_file_or_directory);
244 ErrorOr<std::unique_ptr<File>>
245 OverlayFileSystem::openFileForRead(const llvm::Twine &Path) {
246 // FIXME: handle symlinks that cross file systems
247 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
248 auto Result = (*I)->openFileForRead(Path);
249 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
252 return make_error_code(llvm::errc::no_such_file_or_directory);
255 clang::vfs::detail::DirIterImpl::~DirIterImpl() { }
258 class OverlayFSDirIterImpl : public clang::vfs::detail::DirIterImpl {
259 OverlayFileSystem &Overlays;
261 OverlayFileSystem::iterator CurrentFS;
262 directory_iterator CurrentDirIter;
263 llvm::StringSet<> SeenNames;
265 std::error_code incrementFS() {
266 assert(CurrentFS != Overlays.overlays_end() && "incrementing past end");
268 for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) {
270 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
271 if (EC && EC != errc::no_such_file_or_directory)
273 if (CurrentDirIter != directory_iterator())
276 return std::error_code();
279 std::error_code incrementDirIter(bool IsFirstTime) {
280 assert((IsFirstTime || CurrentDirIter != directory_iterator()) &&
281 "incrementing past end");
284 CurrentDirIter.increment(EC);
285 if (!EC && CurrentDirIter == directory_iterator())
290 std::error_code incrementImpl(bool IsFirstTime) {
292 std::error_code EC = incrementDirIter(IsFirstTime);
293 if (EC || CurrentDirIter == directory_iterator()) {
294 CurrentEntry = Status();
297 CurrentEntry = *CurrentDirIter;
298 StringRef Name = llvm::sys::path::filename(CurrentEntry.getName());
299 if (SeenNames.insert(Name).second)
300 return EC; // name not seen before
302 llvm_unreachable("returned above");
306 OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS,
308 : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) {
309 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC);
310 EC = incrementImpl(true);
313 std::error_code increment() override { return incrementImpl(false); }
315 } // end anonymous namespace
317 directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir,
318 std::error_code &EC) {
319 return directory_iterator(
320 std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC));
323 //===-----------------------------------------------------------------------===/
324 // VFSFromYAML implementation
325 //===-----------------------------------------------------------------------===/
334 /// \brief A single file or directory in the VFS.
341 Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {}
342 StringRef getName() const { return Name; }
343 EntryKind getKind() const { return Kind; }
346 class DirectoryEntry : public Entry {
347 std::vector<Entry *> Contents;
351 ~DirectoryEntry() override;
352 DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S)
353 : Entry(EK_Directory, Name), Contents(std::move(Contents)),
355 Status getStatus() { return S; }
356 typedef std::vector<Entry *>::iterator iterator;
357 iterator contents_begin() { return Contents.begin(); }
358 iterator contents_end() { return Contents.end(); }
359 static bool classof(const Entry *E) { return E->getKind() == EK_Directory; }
362 class FileEntry : public Entry {
370 std::string ExternalContentsPath;
373 FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName)
374 : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath),
376 StringRef getExternalContentsPath() const { return ExternalContentsPath; }
377 /// \brief whether to use the external path as the name for this file.
378 bool useExternalName(bool GlobalUseExternalName) const {
379 return UseName == NK_NotSet ? GlobalUseExternalName
380 : (UseName == NK_External);
382 static bool classof(const Entry *E) { return E->getKind() == EK_File; }
387 class VFSFromYamlDirIterImpl : public clang::vfs::detail::DirIterImpl {
390 DirectoryEntry::iterator Current, End;
392 VFSFromYamlDirIterImpl(const Twine &Path, VFSFromYAML &FS,
393 DirectoryEntry::iterator Begin,
394 DirectoryEntry::iterator End, std::error_code &EC);
395 std::error_code increment() override;
398 /// \brief A virtual file system parsed from a YAML file.
400 /// Currently, this class allows creating virtual directories and mapping
401 /// virtual file paths to existing external files, available in \c ExternalFS.
403 /// The basic structure of the parsed file is:
406 /// 'version': <version number>,
407 /// <optional configuration>
409 /// <directory entries>
414 /// All configuration options are optional.
415 /// 'case-sensitive': <boolean, default=true>
416 /// 'use-external-names': <boolean, default=true>
418 /// Virtual directories are represented as
421 /// 'type': 'directory',
422 /// 'name': <string>,
423 /// 'contents': [ <file or directory entries> ]
427 /// The default attributes for virtual directories are:
429 /// MTime = now() when created
433 /// UniqueID = unspecified unique value
436 /// Re-mapped files are represented as
440 /// 'name': <string>,
441 /// 'use-external-name': <boolean> # Optional
442 /// 'external-contents': <path to external file>)
446 /// and inherit their attributes from the external contents.
448 /// In both cases, the 'name' field may contain multiple path components (e.g.
449 /// /path/to/file). However, any directory that contains more than one child
450 /// must be uniquely represented by a directory entry.
451 class VFSFromYAML : public vfs::FileSystem {
452 std::vector<Entry *> Roots; ///< The root(s) of the virtual file system.
453 /// \brief The file system to use for external references.
454 IntrusiveRefCntPtr<FileSystem> ExternalFS;
456 /// @name Configuration
459 /// \brief Whether to perform case-sensitive comparisons.
461 /// Currently, case-insensitive matching only works correctly with ASCII.
464 /// \brief Whether to use to use the value of 'external-contents' for the
465 /// names of files. This global value is overridable on a per-file basis.
466 bool UseExternalNames;
469 friend class VFSFromYAMLParser;
472 VFSFromYAML(IntrusiveRefCntPtr<FileSystem> ExternalFS)
473 : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {}
475 /// \brief Looks up \p Path in \c Roots.
476 ErrorOr<Entry *> lookupPath(const Twine &Path);
478 /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly
479 /// recursing into the contents of \p From if it is a directory.
480 ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start,
481 sys::path::const_iterator End, Entry *From);
483 /// \brief Get the status of a given an \c Entry.
484 ErrorOr<Status> status(const Twine &Path, Entry *E);
487 ~VFSFromYAML() override;
489 /// \brief Parses \p Buffer, which is expected to be in YAML format and
490 /// returns a virtual file system representing its contents.
491 static VFSFromYAML *create(std::unique_ptr<MemoryBuffer> Buffer,
492 SourceMgr::DiagHandlerTy DiagHandler,
494 IntrusiveRefCntPtr<FileSystem> ExternalFS);
496 ErrorOr<Status> status(const Twine &Path) override;
497 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override;
499 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{
500 ErrorOr<Entry *> E = lookupPath(Dir);
503 return directory_iterator();
505 ErrorOr<Status> S = status(Dir, *E);
508 return directory_iterator();
510 if (!S->isDirectory()) {
511 EC = std::error_code(static_cast<int>(errc::not_a_directory),
512 std::system_category());
513 return directory_iterator();
516 DirectoryEntry *D = cast<DirectoryEntry>(*E);
517 return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(Dir,
518 *this, D->contents_begin(), D->contents_end(), EC));
522 /// \brief A helper class to hold the common YAML parsing state.
523 class VFSFromYAMLParser {
524 yaml::Stream &Stream;
526 void error(yaml::Node *N, const Twine &Msg) {
527 Stream.printError(N, Msg);
531 bool parseScalarString(yaml::Node *N, StringRef &Result,
532 SmallVectorImpl<char> &Storage) {
533 yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N);
535 error(N, "expected string");
538 Result = S->getValue(Storage);
543 bool parseScalarBool(yaml::Node *N, bool &Result) {
544 SmallString<5> Storage;
546 if (!parseScalarString(N, Value, Storage))
549 if (Value.equals_lower("true") || Value.equals_lower("on") ||
550 Value.equals_lower("yes") || Value == "1") {
553 } else if (Value.equals_lower("false") || Value.equals_lower("off") ||
554 Value.equals_lower("no") || Value == "0") {
559 error(N, "expected boolean value");
564 KeyStatus(bool Required=false) : Required(Required), Seen(false) {}
568 typedef std::pair<StringRef, KeyStatus> KeyStatusPair;
571 bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key,
572 DenseMap<StringRef, KeyStatus> &Keys) {
573 if (!Keys.count(Key)) {
574 error(KeyNode, "unknown key");
577 KeyStatus &S = Keys[Key];
579 error(KeyNode, Twine("duplicate key '") + Key + "'");
587 bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) {
588 for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(),
591 if (I->second.Required && !I->second.Seen) {
592 error(Obj, Twine("missing key '") + I->first + "'");
599 Entry *parseEntry(yaml::Node *N) {
600 yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N);
602 error(N, "expected mapping node for file or directory entry");
606 KeyStatusPair Fields[] = {
607 KeyStatusPair("name", true),
608 KeyStatusPair("type", true),
609 KeyStatusPair("contents", false),
610 KeyStatusPair("external-contents", false),
611 KeyStatusPair("use-external-name", false),
614 DenseMap<StringRef, KeyStatus> Keys(
615 &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));
617 bool HasContents = false; // external or otherwise
618 std::vector<Entry *> EntryArrayContents;
619 std::string ExternalContentsPath;
621 FileEntry::NameKind UseExternalName = FileEntry::NK_NotSet;
624 for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E;
627 // Reuse the buffer for key and value, since we don't look at key after
629 SmallString<256> Buffer;
630 if (!parseScalarString(I->getKey(), Key, Buffer))
633 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
638 if (!parseScalarString(I->getValue(), Value, Buffer))
641 } else if (Key == "type") {
642 if (!parseScalarString(I->getValue(), Value, Buffer))
646 else if (Value == "directory")
649 error(I->getValue(), "unknown value for 'type'");
652 } else if (Key == "contents") {
655 "entry already has 'contents' or 'external-contents'");
659 yaml::SequenceNode *Contents =
660 dyn_cast<yaml::SequenceNode>(I->getValue());
662 // FIXME: this is only for directories, what about files?
663 error(I->getValue(), "expected array");
667 for (yaml::SequenceNode::iterator I = Contents->begin(),
670 if (Entry *E = parseEntry(&*I))
671 EntryArrayContents.push_back(E);
675 } else if (Key == "external-contents") {
678 "entry already has 'contents' or 'external-contents'");
682 if (!parseScalarString(I->getValue(), Value, Buffer))
684 ExternalContentsPath = Value;
685 } else if (Key == "use-external-name") {
687 if (!parseScalarBool(I->getValue(), Val))
689 UseExternalName = Val ? FileEntry::NK_External : FileEntry::NK_Virtual;
691 llvm_unreachable("key missing from Keys");
698 // check for missing keys
700 error(N, "missing key 'contents' or 'external-contents'");
703 if (!checkMissingKeys(N, Keys))
706 // check invalid configuration
707 if (Kind == EK_Directory && UseExternalName != FileEntry::NK_NotSet) {
708 error(N, "'use-external-name' is not supported for directories");
712 // Remove trailing slash(es), being careful not to remove the root path
713 StringRef Trimmed(Name);
714 size_t RootPathLen = sys::path::root_path(Trimmed).size();
715 while (Trimmed.size() > RootPathLen &&
716 sys::path::is_separator(Trimmed.back()))
717 Trimmed = Trimmed.slice(0, Trimmed.size()-1);
718 // Get the last component
719 StringRef LastComponent = sys::path::filename(Trimmed);
721 Entry *Result = nullptr;
724 Result = new FileEntry(LastComponent, std::move(ExternalContentsPath),
728 Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents),
729 Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
730 0, file_type::directory_file, sys::fs::all_all));
734 StringRef Parent = sys::path::parent_path(Trimmed);
738 // if 'name' contains multiple components, create implicit directory entries
739 for (sys::path::reverse_iterator I = sys::path::rbegin(Parent),
740 E = sys::path::rend(Parent);
742 Result = new DirectoryEntry(*I, llvm::makeArrayRef(Result),
743 Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0,
744 0, file_type::directory_file, sys::fs::all_all));
750 VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {}
753 bool parse(yaml::Node *Root, VFSFromYAML *FS) {
754 yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root);
756 error(Root, "expected mapping node");
760 KeyStatusPair Fields[] = {
761 KeyStatusPair("version", true),
762 KeyStatusPair("case-sensitive", false),
763 KeyStatusPair("use-external-names", false),
764 KeyStatusPair("roots", true),
767 DenseMap<StringRef, KeyStatus> Keys(
768 &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0]));
770 // Parse configuration and 'roots'
771 for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E;
773 SmallString<10> KeyBuffer;
775 if (!parseScalarString(I->getKey(), Key, KeyBuffer))
778 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys))
781 if (Key == "roots") {
782 yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue());
784 error(I->getValue(), "expected array");
788 for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end();
790 if (Entry *E = parseEntry(&*I))
791 FS->Roots.push_back(E);
795 } else if (Key == "version") {
796 StringRef VersionString;
797 SmallString<4> Storage;
798 if (!parseScalarString(I->getValue(), VersionString, Storage))
801 if (VersionString.getAsInteger<int>(10, Version)) {
802 error(I->getValue(), "expected integer");
806 error(I->getValue(), "invalid version number");
810 error(I->getValue(), "version mismatch, expected 0");
813 } else if (Key == "case-sensitive") {
814 if (!parseScalarBool(I->getValue(), FS->CaseSensitive))
816 } else if (Key == "use-external-names") {
817 if (!parseScalarBool(I->getValue(), FS->UseExternalNames))
820 llvm_unreachable("key missing from Keys");
827 if (!checkMissingKeys(Top, Keys))
832 } // end of anonymous namespace
835 DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); }
837 VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); }
839 VFSFromYAML *VFSFromYAML::create(std::unique_ptr<MemoryBuffer> Buffer,
840 SourceMgr::DiagHandlerTy DiagHandler,
842 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
845 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
847 SM.setDiagHandler(DiagHandler, DiagContext);
848 yaml::document_iterator DI = Stream.begin();
849 yaml::Node *Root = DI->getRoot();
850 if (DI == Stream.end() || !Root) {
851 SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node");
855 VFSFromYAMLParser P(Stream);
857 std::unique_ptr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS));
858 if (!P.parse(Root, FS.get()))
864 ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) {
865 SmallString<256> Path;
866 Path_.toVector(Path);
868 // Handle relative paths
869 if (std::error_code EC = sys::fs::make_absolute(Path))
873 return make_error_code(llvm::errc::invalid_argument);
875 sys::path::const_iterator Start = sys::path::begin(Path);
876 sys::path::const_iterator End = sys::path::end(Path);
877 for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end();
879 ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
880 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
883 return make_error_code(llvm::errc::no_such_file_or_directory);
886 ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start,
887 sys::path::const_iterator End,
889 if (Start->equals("."))
893 if (CaseSensitive ? !Start->equals(From->getName())
894 : !Start->equals_lower(From->getName()))
896 return make_error_code(llvm::errc::no_such_file_or_directory);
905 DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From);
907 return make_error_code(llvm::errc::not_a_directory);
909 for (DirectoryEntry::iterator I = DE->contents_begin(),
910 E = DE->contents_end();
912 ErrorOr<Entry *> Result = lookupPath(Start, End, *I);
913 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory)
916 return make_error_code(llvm::errc::no_such_file_or_directory);
919 ErrorOr<Status> VFSFromYAML::status(const Twine &Path, Entry *E) {
920 assert(E != nullptr);
921 std::string PathStr(Path.str());
922 if (FileEntry *F = dyn_cast<FileEntry>(E)) {
923 ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath());
924 assert(!S || S->getName() == F->getExternalContentsPath());
925 if (S && !F->useExternalName(UseExternalNames))
928 S->IsVFSMapped = true;
930 } else { // directory
931 DirectoryEntry *DE = cast<DirectoryEntry>(E);
932 Status S = DE->getStatus();
938 ErrorOr<Status> VFSFromYAML::status(const Twine &Path) {
939 ErrorOr<Entry *> Result = lookupPath(Path);
941 return Result.getError();
942 return status(Path, *Result);
945 ErrorOr<std::unique_ptr<File>> VFSFromYAML::openFileForRead(const Twine &Path) {
946 ErrorOr<Entry *> E = lookupPath(Path);
950 FileEntry *F = dyn_cast<FileEntry>(*E);
951 if (!F) // FIXME: errc::not_a_file?
952 return make_error_code(llvm::errc::invalid_argument);
954 auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath());
958 if (!F->useExternalName(UseExternalNames))
959 (*Result)->setName(Path.str());
964 IntrusiveRefCntPtr<FileSystem>
965 vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer,
966 SourceMgr::DiagHandlerTy DiagHandler, void *DiagContext,
967 IntrusiveRefCntPtr<FileSystem> ExternalFS) {
968 return VFSFromYAML::create(std::move(Buffer), DiagHandler, DiagContext,
972 UniqueID vfs::getNextVirtualUniqueID() {
973 static std::atomic<unsigned> UID;
975 // The following assumes that uint64_t max will never collide with a real
976 // dev_t value from the OS.
977 return UniqueID(std::numeric_limits<uint64_t>::max(), ID);
981 static bool pathHasTraversal(StringRef Path) {
982 using namespace llvm::sys;
983 for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path)))
984 if (Comp == "." || Comp == "..")
990 void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) {
991 assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
992 assert(sys::path::is_absolute(RealPath) && "real path not absolute");
993 assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
994 Mappings.emplace_back(VirtualPath, RealPath);
999 llvm::raw_ostream &OS;
1000 SmallVector<StringRef, 16> DirStack;
1001 inline unsigned getDirIndent() { return 4 * DirStack.size(); }
1002 inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); }
1003 bool containedIn(StringRef Parent, StringRef Path);
1004 StringRef containedPart(StringRef Parent, StringRef Path);
1005 void startDirectory(StringRef Path);
1006 void endDirectory();
1007 void writeEntry(StringRef VPath, StringRef RPath);
1010 JSONWriter(llvm::raw_ostream &OS) : OS(OS) {}
1011 void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> IsCaseSensitive);
1015 bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
1016 using namespace llvm::sys;
1017 // Compare each path component.
1018 auto IParent = path::begin(Parent), EParent = path::end(Parent);
1019 for (auto IChild = path::begin(Path), EChild = path::end(Path);
1020 IParent != EParent && IChild != EChild; ++IParent, ++IChild) {
1021 if (*IParent != *IChild)
1024 // Have we exhausted the parent path?
1025 return IParent == EParent;
1028 StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
1029 assert(!Parent.empty());
1030 assert(containedIn(Parent, Path));
1031 return Path.slice(Parent.size() + 1, StringRef::npos);
1034 void JSONWriter::startDirectory(StringRef Path) {
1036 DirStack.empty() ? Path : containedPart(DirStack.back(), Path);
1037 DirStack.push_back(Path);
1038 unsigned Indent = getDirIndent();
1039 OS.indent(Indent) << "{\n";
1040 OS.indent(Indent + 2) << "'type': 'directory',\n";
1041 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n";
1042 OS.indent(Indent + 2) << "'contents': [\n";
1045 void JSONWriter::endDirectory() {
1046 unsigned Indent = getDirIndent();
1047 OS.indent(Indent + 2) << "]\n";
1048 OS.indent(Indent) << "}";
1050 DirStack.pop_back();
1053 void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) {
1054 unsigned Indent = getFileIndent();
1055 OS.indent(Indent) << "{\n";
1056 OS.indent(Indent + 2) << "'type': 'file',\n";
1057 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n";
1058 OS.indent(Indent + 2) << "'external-contents': \""
1059 << llvm::yaml::escape(RPath) << "\"\n";
1060 OS.indent(Indent) << "}";
1063 void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
1064 Optional<bool> IsCaseSensitive) {
1065 using namespace llvm::sys;
1069 if (IsCaseSensitive.hasValue())
1070 OS << " 'case-sensitive': '"
1071 << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n";
1072 OS << " 'roots': [\n";
1074 if (!Entries.empty()) {
1075 const YAMLVFSEntry &Entry = Entries.front();
1076 startDirectory(path::parent_path(Entry.VPath));
1077 writeEntry(path::filename(Entry.VPath), Entry.RPath);
1079 for (const auto &Entry : Entries.slice(1)) {
1080 StringRef Dir = path::parent_path(Entry.VPath);
1081 if (Dir == DirStack.back())
1084 while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) {
1089 startDirectory(Dir);
1091 writeEntry(path::filename(Entry.VPath), Entry.RPath);
1094 while (!DirStack.empty()) {
1105 void YAMLVFSWriter::write(llvm::raw_ostream &OS) {
1106 std::sort(Mappings.begin(), Mappings.end(),
1107 [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) {
1108 return LHS.VPath < RHS.VPath;
1111 JSONWriter(OS).write(Mappings, IsCaseSensitive);
1114 VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(const Twine &_Path,
1116 DirectoryEntry::iterator Begin,
1117 DirectoryEntry::iterator End,
1118 std::error_code &EC)
1119 : Dir(_Path.str()), FS(FS), Current(Begin), End(End) {
1120 if (Current != End) {
1121 SmallString<128> PathStr(Dir);
1122 llvm::sys::path::append(PathStr, (*Current)->getName());
1123 llvm::ErrorOr<vfs::Status> S = FS.status(PathStr);
1131 std::error_code VFSFromYamlDirIterImpl::increment() {
1132 assert(Current != End && "cannot iterate past end");
1133 if (++Current != End) {
1134 SmallString<128> PathStr(Dir);
1135 llvm::sys::path::append(PathStr, (*Current)->getName());
1136 llvm::ErrorOr<vfs::Status> S = FS.status(PathStr);
1138 return S.getError();
1141 CurrentEntry = Status();
1143 return std::error_code();
1146 vfs::recursive_directory_iterator::recursive_directory_iterator(FileSystem &FS_,
1148 std::error_code &EC)
1150 directory_iterator I = FS->dir_begin(Path, EC);
1151 if (!EC && I != directory_iterator()) {
1152 State = std::make_shared<IterState>();
1157 vfs::recursive_directory_iterator &
1158 recursive_directory_iterator::increment(std::error_code &EC) {
1159 assert(FS && State && !State->empty() && "incrementing past end");
1160 assert(State->top()->isStatusKnown() && "non-canonical end iterator");
1161 vfs::directory_iterator End;
1162 if (State->top()->isDirectory()) {
1163 vfs::directory_iterator I = FS->dir_begin(State->top()->getName(), EC);
1172 while (!State->empty() && State->top().increment(EC) == End)
1176 State.reset(); // end iterator