clang API Documentation
00001 //===- VirtualFileSystem.cpp - Virtual File System Layer --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // This file implements the VirtualFileSystem interface. 00010 //===----------------------------------------------------------------------===// 00011 00012 #include "clang/Basic/VirtualFileSystem.h" 00013 #include "llvm/ADT/DenseMap.h" 00014 #include "llvm/ADT/iterator_range.h" 00015 #include "llvm/ADT/STLExtras.h" 00016 #include "llvm/ADT/StringExtras.h" 00017 #include "llvm/ADT/StringSet.h" 00018 #include "llvm/Support/Errc.h" 00019 #include "llvm/Support/MemoryBuffer.h" 00020 #include "llvm/Support/Path.h" 00021 #include "llvm/Support/YAMLParser.h" 00022 #include <atomic> 00023 #include <memory> 00024 00025 using namespace clang; 00026 using namespace clang::vfs; 00027 using namespace llvm; 00028 using llvm::sys::fs::file_status; 00029 using llvm::sys::fs::file_type; 00030 using llvm::sys::fs::perms; 00031 using llvm::sys::fs::UniqueID; 00032 00033 Status::Status(const file_status &Status) 00034 : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()), 00035 User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()), 00036 Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false) {} 00037 00038 Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID, 00039 sys::TimeValue MTime, uint32_t User, uint32_t Group, 00040 uint64_t Size, file_type Type, perms Perms) 00041 : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size), 00042 Type(Type), Perms(Perms), IsVFSMapped(false) {} 00043 00044 bool Status::equivalent(const Status &Other) const { 00045 return getUniqueID() == Other.getUniqueID(); 00046 } 00047 bool Status::isDirectory() const { 00048 return Type == file_type::directory_file; 00049 } 00050 bool Status::isRegularFile() const { 00051 return Type == file_type::regular_file; 00052 } 00053 bool Status::isOther() const { 00054 return exists() && !isRegularFile() && !isDirectory() && !isSymlink(); 00055 } 00056 bool Status::isSymlink() const { 00057 return Type == file_type::symlink_file; 00058 } 00059 bool Status::isStatusKnown() const { 00060 return Type != file_type::status_error; 00061 } 00062 bool Status::exists() const { 00063 return isStatusKnown() && Type != file_type::file_not_found; 00064 } 00065 00066 File::~File() {} 00067 00068 FileSystem::~FileSystem() {} 00069 00070 ErrorOr<std::unique_ptr<MemoryBuffer>> 00071 FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize, 00072 bool RequiresNullTerminator, bool IsVolatile) { 00073 auto F = openFileForRead(Name); 00074 if (!F) 00075 return F.getError(); 00076 00077 return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile); 00078 } 00079 00080 //===-----------------------------------------------------------------------===/ 00081 // RealFileSystem implementation 00082 //===-----------------------------------------------------------------------===/ 00083 00084 namespace { 00085 /// \brief Wrapper around a raw file descriptor. 00086 class RealFile : public File { 00087 int FD; 00088 Status S; 00089 friend class RealFileSystem; 00090 RealFile(int FD) : FD(FD) { 00091 assert(FD >= 0 && "Invalid or inactive file descriptor"); 00092 } 00093 00094 public: 00095 ~RealFile(); 00096 ErrorOr<Status> status() override; 00097 ErrorOr<std::unique_ptr<MemoryBuffer>> 00098 getBuffer(const Twine &Name, int64_t FileSize = -1, 00099 bool RequiresNullTerminator = true, 00100 bool IsVolatile = false) override; 00101 std::error_code close() override; 00102 void setName(StringRef Name) override; 00103 }; 00104 } // end anonymous namespace 00105 RealFile::~RealFile() { close(); } 00106 00107 ErrorOr<Status> RealFile::status() { 00108 assert(FD != -1 && "cannot stat closed file"); 00109 if (!S.isStatusKnown()) { 00110 file_status RealStatus; 00111 if (std::error_code EC = sys::fs::status(FD, RealStatus)) 00112 return EC; 00113 Status NewS(RealStatus); 00114 NewS.setName(S.getName()); 00115 S = std::move(NewS); 00116 } 00117 return S; 00118 } 00119 00120 ErrorOr<std::unique_ptr<MemoryBuffer>> 00121 RealFile::getBuffer(const Twine &Name, int64_t FileSize, 00122 bool RequiresNullTerminator, bool IsVolatile) { 00123 assert(FD != -1 && "cannot get buffer for closed file"); 00124 return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator, 00125 IsVolatile); 00126 } 00127 00128 // FIXME: This is terrible, we need this for ::close. 00129 #if !defined(_MSC_VER) && !defined(__MINGW32__) 00130 #include <unistd.h> 00131 #include <sys/uio.h> 00132 #else 00133 #include <io.h> 00134 #ifndef S_ISFIFO 00135 #define S_ISFIFO(x) (0) 00136 #endif 00137 #endif 00138 std::error_code RealFile::close() { 00139 if (::close(FD)) 00140 return std::error_code(errno, std::generic_category()); 00141 FD = -1; 00142 return std::error_code(); 00143 } 00144 00145 void RealFile::setName(StringRef Name) { 00146 S.setName(Name); 00147 } 00148 00149 namespace { 00150 /// \brief The file system according to your operating system. 00151 class RealFileSystem : public FileSystem { 00152 public: 00153 ErrorOr<Status> status(const Twine &Path) override; 00154 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override; 00155 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; 00156 }; 00157 } // end anonymous namespace 00158 00159 ErrorOr<Status> RealFileSystem::status(const Twine &Path) { 00160 sys::fs::file_status RealStatus; 00161 if (std::error_code EC = sys::fs::status(Path, RealStatus)) 00162 return EC; 00163 Status Result(RealStatus); 00164 Result.setName(Path.str()); 00165 return Result; 00166 } 00167 00168 ErrorOr<std::unique_ptr<File>> 00169 RealFileSystem::openFileForRead(const Twine &Name) { 00170 int FD; 00171 if (std::error_code EC = sys::fs::openFileForRead(Name, FD)) 00172 return EC; 00173 std::unique_ptr<File> Result(new RealFile(FD)); 00174 Result->setName(Name.str()); 00175 return std::move(Result); 00176 } 00177 00178 IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() { 00179 static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem(); 00180 return FS; 00181 } 00182 00183 namespace { 00184 class RealFSDirIter : public clang::vfs::detail::DirIterImpl { 00185 std::string Path; 00186 llvm::sys::fs::directory_iterator Iter; 00187 public: 00188 RealFSDirIter(const Twine &_Path, std::error_code &EC) 00189 : Path(_Path.str()), Iter(Path, EC) { 00190 if (!EC && Iter != llvm::sys::fs::directory_iterator()) { 00191 llvm::sys::fs::file_status S; 00192 EC = Iter->status(S); 00193 if (!EC) { 00194 CurrentEntry = Status(S); 00195 CurrentEntry.setName(Iter->path()); 00196 } 00197 } 00198 } 00199 00200 std::error_code increment() override { 00201 std::error_code EC; 00202 Iter.increment(EC); 00203 if (EC) { 00204 return EC; 00205 } else if (Iter == llvm::sys::fs::directory_iterator()) { 00206 CurrentEntry = Status(); 00207 } else { 00208 llvm::sys::fs::file_status S; 00209 EC = Iter->status(S); 00210 CurrentEntry = Status(S); 00211 CurrentEntry.setName(Iter->path()); 00212 } 00213 return EC; 00214 } 00215 }; 00216 } 00217 00218 directory_iterator RealFileSystem::dir_begin(const Twine &Dir, 00219 std::error_code &EC) { 00220 return directory_iterator(std::make_shared<RealFSDirIter>(Dir, EC)); 00221 } 00222 00223 //===-----------------------------------------------------------------------===/ 00224 // OverlayFileSystem implementation 00225 //===-----------------------------------------------------------------------===/ 00226 OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) { 00227 pushOverlay(BaseFS); 00228 } 00229 00230 void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) { 00231 FSList.push_back(FS); 00232 } 00233 00234 ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) { 00235 // FIXME: handle symlinks that cross file systems 00236 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { 00237 ErrorOr<Status> Status = (*I)->status(Path); 00238 if (Status || Status.getError() != llvm::errc::no_such_file_or_directory) 00239 return Status; 00240 } 00241 return make_error_code(llvm::errc::no_such_file_or_directory); 00242 } 00243 00244 ErrorOr<std::unique_ptr<File>> 00245 OverlayFileSystem::openFileForRead(const llvm::Twine &Path) { 00246 // FIXME: handle symlinks that cross file systems 00247 for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { 00248 auto Result = (*I)->openFileForRead(Path); 00249 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) 00250 return Result; 00251 } 00252 return make_error_code(llvm::errc::no_such_file_or_directory); 00253 } 00254 00255 clang::vfs::detail::DirIterImpl::~DirIterImpl() { } 00256 00257 namespace { 00258 class OverlayFSDirIterImpl : public clang::vfs::detail::DirIterImpl { 00259 OverlayFileSystem &Overlays; 00260 std::string Path; 00261 OverlayFileSystem::iterator CurrentFS; 00262 directory_iterator CurrentDirIter; 00263 llvm::StringSet<> SeenNames; 00264 00265 std::error_code incrementFS() { 00266 assert(CurrentFS != Overlays.overlays_end() && "incrementing past end"); 00267 ++CurrentFS; 00268 for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) { 00269 std::error_code EC; 00270 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC); 00271 if (EC && EC != errc::no_such_file_or_directory) 00272 return EC; 00273 if (CurrentDirIter != directory_iterator()) 00274 break; // found 00275 } 00276 return std::error_code(); 00277 } 00278 00279 std::error_code incrementDirIter(bool IsFirstTime) { 00280 assert((IsFirstTime || CurrentDirIter != directory_iterator()) && 00281 "incrementing past end"); 00282 std::error_code EC; 00283 if (!IsFirstTime) 00284 CurrentDirIter.increment(EC); 00285 if (!EC && CurrentDirIter == directory_iterator()) 00286 EC = incrementFS(); 00287 return EC; 00288 } 00289 00290 std::error_code incrementImpl(bool IsFirstTime) { 00291 while (true) { 00292 std::error_code EC = incrementDirIter(IsFirstTime); 00293 if (EC || CurrentDirIter == directory_iterator()) { 00294 CurrentEntry = Status(); 00295 return EC; 00296 } 00297 CurrentEntry = *CurrentDirIter; 00298 StringRef Name = llvm::sys::path::filename(CurrentEntry.getName()); 00299 if (SeenNames.insert(Name)) 00300 return EC; // name not seen before 00301 } 00302 llvm_unreachable("returned above"); 00303 } 00304 00305 public: 00306 OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS, 00307 std::error_code &EC) 00308 : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) { 00309 CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC); 00310 EC = incrementImpl(true); 00311 } 00312 00313 std::error_code increment() override { return incrementImpl(false); } 00314 }; 00315 } // end anonymous namespace 00316 00317 directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir, 00318 std::error_code &EC) { 00319 return directory_iterator( 00320 std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC)); 00321 } 00322 00323 //===-----------------------------------------------------------------------===/ 00324 // VFSFromYAML implementation 00325 //===-----------------------------------------------------------------------===/ 00326 00327 // Allow DenseMap<StringRef, ...>. This is useful below because we know all the 00328 // strings are literals and will outlive the map, and there is no reason to 00329 // store them. 00330 namespace llvm { 00331 template<> 00332 struct DenseMapInfo<StringRef> { 00333 // This assumes that "" will never be a valid key. 00334 static inline StringRef getEmptyKey() { return StringRef(""); } 00335 static inline StringRef getTombstoneKey() { return StringRef(); } 00336 static unsigned getHashValue(StringRef Val) { return HashString(Val); } 00337 static bool isEqual(StringRef LHS, StringRef RHS) { return LHS == RHS; } 00338 }; 00339 } 00340 00341 namespace { 00342 00343 enum EntryKind { 00344 EK_Directory, 00345 EK_File 00346 }; 00347 00348 /// \brief A single file or directory in the VFS. 00349 class Entry { 00350 EntryKind Kind; 00351 std::string Name; 00352 00353 public: 00354 virtual ~Entry(); 00355 Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {} 00356 StringRef getName() const { return Name; } 00357 EntryKind getKind() const { return Kind; } 00358 }; 00359 00360 class DirectoryEntry : public Entry { 00361 std::vector<Entry *> Contents; 00362 Status S; 00363 00364 public: 00365 virtual ~DirectoryEntry(); 00366 DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S) 00367 : Entry(EK_Directory, Name), Contents(std::move(Contents)), 00368 S(std::move(S)) {} 00369 Status getStatus() { return S; } 00370 typedef std::vector<Entry *>::iterator iterator; 00371 iterator contents_begin() { return Contents.begin(); } 00372 iterator contents_end() { return Contents.end(); } 00373 static bool classof(const Entry *E) { return E->getKind() == EK_Directory; } 00374 }; 00375 00376 class FileEntry : public Entry { 00377 public: 00378 enum NameKind { 00379 NK_NotSet, 00380 NK_External, 00381 NK_Virtual 00382 }; 00383 private: 00384 std::string ExternalContentsPath; 00385 NameKind UseName; 00386 public: 00387 FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName) 00388 : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath), 00389 UseName(UseName) {} 00390 StringRef getExternalContentsPath() const { return ExternalContentsPath; } 00391 /// \brief whether to use the external path as the name for this file. 00392 bool useExternalName(bool GlobalUseExternalName) const { 00393 return UseName == NK_NotSet ? GlobalUseExternalName 00394 : (UseName == NK_External); 00395 } 00396 static bool classof(const Entry *E) { return E->getKind() == EK_File; } 00397 }; 00398 00399 class VFSFromYAML; 00400 00401 class VFSFromYamlDirIterImpl : public clang::vfs::detail::DirIterImpl { 00402 std::string Dir; 00403 VFSFromYAML &FS; 00404 DirectoryEntry::iterator Current, End; 00405 public: 00406 VFSFromYamlDirIterImpl(const Twine &Path, VFSFromYAML &FS, 00407 DirectoryEntry::iterator Begin, 00408 DirectoryEntry::iterator End, std::error_code &EC); 00409 std::error_code increment() override; 00410 }; 00411 00412 /// \brief A virtual file system parsed from a YAML file. 00413 /// 00414 /// Currently, this class allows creating virtual directories and mapping 00415 /// virtual file paths to existing external files, available in \c ExternalFS. 00416 /// 00417 /// The basic structure of the parsed file is: 00418 /// \verbatim 00419 /// { 00420 /// 'version': <version number>, 00421 /// <optional configuration> 00422 /// 'roots': [ 00423 /// <directory entries> 00424 /// ] 00425 /// } 00426 /// \endverbatim 00427 /// 00428 /// All configuration options are optional. 00429 /// 'case-sensitive': <boolean, default=true> 00430 /// 'use-external-names': <boolean, default=true> 00431 /// 00432 /// Virtual directories are represented as 00433 /// \verbatim 00434 /// { 00435 /// 'type': 'directory', 00436 /// 'name': <string>, 00437 /// 'contents': [ <file or directory entries> ] 00438 /// } 00439 /// \endverbatim 00440 /// 00441 /// The default attributes for virtual directories are: 00442 /// \verbatim 00443 /// MTime = now() when created 00444 /// Perms = 0777 00445 /// User = Group = 0 00446 /// Size = 0 00447 /// UniqueID = unspecified unique value 00448 /// \endverbatim 00449 /// 00450 /// Re-mapped files are represented as 00451 /// \verbatim 00452 /// { 00453 /// 'type': 'file', 00454 /// 'name': <string>, 00455 /// 'use-external-name': <boolean> # Optional 00456 /// 'external-contents': <path to external file>) 00457 /// } 00458 /// \endverbatim 00459 /// 00460 /// and inherit their attributes from the external contents. 00461 /// 00462 /// In both cases, the 'name' field may contain multiple path components (e.g. 00463 /// /path/to/file). However, any directory that contains more than one child 00464 /// must be uniquely represented by a directory entry. 00465 class VFSFromYAML : public vfs::FileSystem { 00466 std::vector<Entry *> Roots; ///< The root(s) of the virtual file system. 00467 /// \brief The file system to use for external references. 00468 IntrusiveRefCntPtr<FileSystem> ExternalFS; 00469 00470 /// @name Configuration 00471 /// @{ 00472 00473 /// \brief Whether to perform case-sensitive comparisons. 00474 /// 00475 /// Currently, case-insensitive matching only works correctly with ASCII. 00476 bool CaseSensitive; 00477 00478 /// \brief Whether to use to use the value of 'external-contents' for the 00479 /// names of files. This global value is overridable on a per-file basis. 00480 bool UseExternalNames; 00481 /// @} 00482 00483 friend class VFSFromYAMLParser; 00484 00485 private: 00486 VFSFromYAML(IntrusiveRefCntPtr<FileSystem> ExternalFS) 00487 : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {} 00488 00489 /// \brief Looks up \p Path in \c Roots. 00490 ErrorOr<Entry *> lookupPath(const Twine &Path); 00491 00492 /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly 00493 /// recursing into the contents of \p From if it is a directory. 00494 ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start, 00495 sys::path::const_iterator End, Entry *From); 00496 00497 /// \brief Get the status of a given an \c Entry. 00498 ErrorOr<Status> status(const Twine &Path, Entry *E); 00499 00500 public: 00501 ~VFSFromYAML(); 00502 00503 /// \brief Parses \p Buffer, which is expected to be in YAML format and 00504 /// returns a virtual file system representing its contents. 00505 static VFSFromYAML *create(std::unique_ptr<MemoryBuffer> Buffer, 00506 SourceMgr::DiagHandlerTy DiagHandler, 00507 void *DiagContext, 00508 IntrusiveRefCntPtr<FileSystem> ExternalFS); 00509 00510 ErrorOr<Status> status(const Twine &Path) override; 00511 ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override; 00512 00513 directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{ 00514 ErrorOr<Entry *> E = lookupPath(Dir); 00515 if (!E) { 00516 EC = E.getError(); 00517 return directory_iterator(); 00518 } 00519 ErrorOr<Status> S = status(Dir, *E); 00520 if (!S) { 00521 EC = S.getError(); 00522 return directory_iterator(); 00523 } 00524 if (!S->isDirectory()) { 00525 EC = std::error_code(static_cast<int>(errc::not_a_directory), 00526 std::system_category()); 00527 return directory_iterator(); 00528 } 00529 00530 DirectoryEntry *D = cast<DirectoryEntry>(*E); 00531 return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(Dir, 00532 *this, D->contents_begin(), D->contents_end(), EC)); 00533 } 00534 }; 00535 00536 /// \brief A helper class to hold the common YAML parsing state. 00537 class VFSFromYAMLParser { 00538 yaml::Stream &Stream; 00539 00540 void error(yaml::Node *N, const Twine &Msg) { 00541 Stream.printError(N, Msg); 00542 } 00543 00544 // false on error 00545 bool parseScalarString(yaml::Node *N, StringRef &Result, 00546 SmallVectorImpl<char> &Storage) { 00547 yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N); 00548 if (!S) { 00549 error(N, "expected string"); 00550 return false; 00551 } 00552 Result = S->getValue(Storage); 00553 return true; 00554 } 00555 00556 // false on error 00557 bool parseScalarBool(yaml::Node *N, bool &Result) { 00558 SmallString<5> Storage; 00559 StringRef Value; 00560 if (!parseScalarString(N, Value, Storage)) 00561 return false; 00562 00563 if (Value.equals_lower("true") || Value.equals_lower("on") || 00564 Value.equals_lower("yes") || Value == "1") { 00565 Result = true; 00566 return true; 00567 } else if (Value.equals_lower("false") || Value.equals_lower("off") || 00568 Value.equals_lower("no") || Value == "0") { 00569 Result = false; 00570 return true; 00571 } 00572 00573 error(N, "expected boolean value"); 00574 return false; 00575 } 00576 00577 struct KeyStatus { 00578 KeyStatus(bool Required=false) : Required(Required), Seen(false) {} 00579 bool Required; 00580 bool Seen; 00581 }; 00582 typedef std::pair<StringRef, KeyStatus> KeyStatusPair; 00583 00584 // false on error 00585 bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key, 00586 DenseMap<StringRef, KeyStatus> &Keys) { 00587 if (!Keys.count(Key)) { 00588 error(KeyNode, "unknown key"); 00589 return false; 00590 } 00591 KeyStatus &S = Keys[Key]; 00592 if (S.Seen) { 00593 error(KeyNode, Twine("duplicate key '") + Key + "'"); 00594 return false; 00595 } 00596 S.Seen = true; 00597 return true; 00598 } 00599 00600 // false on error 00601 bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) { 00602 for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(), 00603 E = Keys.end(); 00604 I != E; ++I) { 00605 if (I->second.Required && !I->second.Seen) { 00606 error(Obj, Twine("missing key '") + I->first + "'"); 00607 return false; 00608 } 00609 } 00610 return true; 00611 } 00612 00613 Entry *parseEntry(yaml::Node *N) { 00614 yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N); 00615 if (!M) { 00616 error(N, "expected mapping node for file or directory entry"); 00617 return nullptr; 00618 } 00619 00620 KeyStatusPair Fields[] = { 00621 KeyStatusPair("name", true), 00622 KeyStatusPair("type", true), 00623 KeyStatusPair("contents", false), 00624 KeyStatusPair("external-contents", false), 00625 KeyStatusPair("use-external-name", false), 00626 }; 00627 00628 DenseMap<StringRef, KeyStatus> Keys( 00629 &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0])); 00630 00631 bool HasContents = false; // external or otherwise 00632 std::vector<Entry *> EntryArrayContents; 00633 std::string ExternalContentsPath; 00634 std::string Name; 00635 FileEntry::NameKind UseExternalName = FileEntry::NK_NotSet; 00636 EntryKind Kind; 00637 00638 for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E; 00639 ++I) { 00640 StringRef Key; 00641 // Reuse the buffer for key and value, since we don't look at key after 00642 // parsing value. 00643 SmallString<256> Buffer; 00644 if (!parseScalarString(I->getKey(), Key, Buffer)) 00645 return nullptr; 00646 00647 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys)) 00648 return nullptr; 00649 00650 StringRef Value; 00651 if (Key == "name") { 00652 if (!parseScalarString(I->getValue(), Value, Buffer)) 00653 return nullptr; 00654 Name = Value; 00655 } else if (Key == "type") { 00656 if (!parseScalarString(I->getValue(), Value, Buffer)) 00657 return nullptr; 00658 if (Value == "file") 00659 Kind = EK_File; 00660 else if (Value == "directory") 00661 Kind = EK_Directory; 00662 else { 00663 error(I->getValue(), "unknown value for 'type'"); 00664 return nullptr; 00665 } 00666 } else if (Key == "contents") { 00667 if (HasContents) { 00668 error(I->getKey(), 00669 "entry already has 'contents' or 'external-contents'"); 00670 return nullptr; 00671 } 00672 HasContents = true; 00673 yaml::SequenceNode *Contents = 00674 dyn_cast<yaml::SequenceNode>(I->getValue()); 00675 if (!Contents) { 00676 // FIXME: this is only for directories, what about files? 00677 error(I->getValue(), "expected array"); 00678 return nullptr; 00679 } 00680 00681 for (yaml::SequenceNode::iterator I = Contents->begin(), 00682 E = Contents->end(); 00683 I != E; ++I) { 00684 if (Entry *E = parseEntry(&*I)) 00685 EntryArrayContents.push_back(E); 00686 else 00687 return nullptr; 00688 } 00689 } else if (Key == "external-contents") { 00690 if (HasContents) { 00691 error(I->getKey(), 00692 "entry already has 'contents' or 'external-contents'"); 00693 return nullptr; 00694 } 00695 HasContents = true; 00696 if (!parseScalarString(I->getValue(), Value, Buffer)) 00697 return nullptr; 00698 ExternalContentsPath = Value; 00699 } else if (Key == "use-external-name") { 00700 bool Val; 00701 if (!parseScalarBool(I->getValue(), Val)) 00702 return nullptr; 00703 UseExternalName = Val ? FileEntry::NK_External : FileEntry::NK_Virtual; 00704 } else { 00705 llvm_unreachable("key missing from Keys"); 00706 } 00707 } 00708 00709 if (Stream.failed()) 00710 return nullptr; 00711 00712 // check for missing keys 00713 if (!HasContents) { 00714 error(N, "missing key 'contents' or 'external-contents'"); 00715 return nullptr; 00716 } 00717 if (!checkMissingKeys(N, Keys)) 00718 return nullptr; 00719 00720 // check invalid configuration 00721 if (Kind == EK_Directory && UseExternalName != FileEntry::NK_NotSet) { 00722 error(N, "'use-external-name' is not supported for directories"); 00723 return nullptr; 00724 } 00725 00726 // Remove trailing slash(es), being careful not to remove the root path 00727 StringRef Trimmed(Name); 00728 size_t RootPathLen = sys::path::root_path(Trimmed).size(); 00729 while (Trimmed.size() > RootPathLen && 00730 sys::path::is_separator(Trimmed.back())) 00731 Trimmed = Trimmed.slice(0, Trimmed.size()-1); 00732 // Get the last component 00733 StringRef LastComponent = sys::path::filename(Trimmed); 00734 00735 Entry *Result = nullptr; 00736 switch (Kind) { 00737 case EK_File: 00738 Result = new FileEntry(LastComponent, std::move(ExternalContentsPath), 00739 UseExternalName); 00740 break; 00741 case EK_Directory: 00742 Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents), 00743 Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 00744 0, file_type::directory_file, sys::fs::all_all)); 00745 break; 00746 } 00747 00748 StringRef Parent = sys::path::parent_path(Trimmed); 00749 if (Parent.empty()) 00750 return Result; 00751 00752 // if 'name' contains multiple components, create implicit directory entries 00753 for (sys::path::reverse_iterator I = sys::path::rbegin(Parent), 00754 E = sys::path::rend(Parent); 00755 I != E; ++I) { 00756 Result = new DirectoryEntry(*I, llvm::makeArrayRef(Result), 00757 Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 00758 0, file_type::directory_file, sys::fs::all_all)); 00759 } 00760 return Result; 00761 } 00762 00763 public: 00764 VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {} 00765 00766 // false on error 00767 bool parse(yaml::Node *Root, VFSFromYAML *FS) { 00768 yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root); 00769 if (!Top) { 00770 error(Root, "expected mapping node"); 00771 return false; 00772 } 00773 00774 KeyStatusPair Fields[] = { 00775 KeyStatusPair("version", true), 00776 KeyStatusPair("case-sensitive", false), 00777 KeyStatusPair("use-external-names", false), 00778 KeyStatusPair("roots", true), 00779 }; 00780 00781 DenseMap<StringRef, KeyStatus> Keys( 00782 &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0])); 00783 00784 // Parse configuration and 'roots' 00785 for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E; 00786 ++I) { 00787 SmallString<10> KeyBuffer; 00788 StringRef Key; 00789 if (!parseScalarString(I->getKey(), Key, KeyBuffer)) 00790 return false; 00791 00792 if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys)) 00793 return false; 00794 00795 if (Key == "roots") { 00796 yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue()); 00797 if (!Roots) { 00798 error(I->getValue(), "expected array"); 00799 return false; 00800 } 00801 00802 for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end(); 00803 I != E; ++I) { 00804 if (Entry *E = parseEntry(&*I)) 00805 FS->Roots.push_back(E); 00806 else 00807 return false; 00808 } 00809 } else if (Key == "version") { 00810 StringRef VersionString; 00811 SmallString<4> Storage; 00812 if (!parseScalarString(I->getValue(), VersionString, Storage)) 00813 return false; 00814 int Version; 00815 if (VersionString.getAsInteger<int>(10, Version)) { 00816 error(I->getValue(), "expected integer"); 00817 return false; 00818 } 00819 if (Version < 0) { 00820 error(I->getValue(), "invalid version number"); 00821 return false; 00822 } 00823 if (Version != 0) { 00824 error(I->getValue(), "version mismatch, expected 0"); 00825 return false; 00826 } 00827 } else if (Key == "case-sensitive") { 00828 if (!parseScalarBool(I->getValue(), FS->CaseSensitive)) 00829 return false; 00830 } else if (Key == "use-external-names") { 00831 if (!parseScalarBool(I->getValue(), FS->UseExternalNames)) 00832 return false; 00833 } else { 00834 llvm_unreachable("key missing from Keys"); 00835 } 00836 } 00837 00838 if (Stream.failed()) 00839 return false; 00840 00841 if (!checkMissingKeys(Top, Keys)) 00842 return false; 00843 return true; 00844 } 00845 }; 00846 } // end of anonymous namespace 00847 00848 Entry::~Entry() {} 00849 DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); } 00850 00851 VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); } 00852 00853 VFSFromYAML *VFSFromYAML::create(std::unique_ptr<MemoryBuffer> Buffer, 00854 SourceMgr::DiagHandlerTy DiagHandler, 00855 void *DiagContext, 00856 IntrusiveRefCntPtr<FileSystem> ExternalFS) { 00857 00858 SourceMgr SM; 00859 yaml::Stream Stream(Buffer->getMemBufferRef(), SM); 00860 00861 SM.setDiagHandler(DiagHandler, DiagContext); 00862 yaml::document_iterator DI = Stream.begin(); 00863 yaml::Node *Root = DI->getRoot(); 00864 if (DI == Stream.end() || !Root) { 00865 SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node"); 00866 return nullptr; 00867 } 00868 00869 VFSFromYAMLParser P(Stream); 00870 00871 std::unique_ptr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS)); 00872 if (!P.parse(Root, FS.get())) 00873 return nullptr; 00874 00875 return FS.release(); 00876 } 00877 00878 ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) { 00879 SmallString<256> Path; 00880 Path_.toVector(Path); 00881 00882 // Handle relative paths 00883 if (std::error_code EC = sys::fs::make_absolute(Path)) 00884 return EC; 00885 00886 if (Path.empty()) 00887 return make_error_code(llvm::errc::invalid_argument); 00888 00889 sys::path::const_iterator Start = sys::path::begin(Path); 00890 sys::path::const_iterator End = sys::path::end(Path); 00891 for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end(); 00892 I != E; ++I) { 00893 ErrorOr<Entry *> Result = lookupPath(Start, End, *I); 00894 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) 00895 return Result; 00896 } 00897 return make_error_code(llvm::errc::no_such_file_or_directory); 00898 } 00899 00900 ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start, 00901 sys::path::const_iterator End, 00902 Entry *From) { 00903 if (Start->equals(".")) 00904 ++Start; 00905 00906 // FIXME: handle .. 00907 if (CaseSensitive ? !Start->equals(From->getName()) 00908 : !Start->equals_lower(From->getName())) 00909 // failure to match 00910 return make_error_code(llvm::errc::no_such_file_or_directory); 00911 00912 ++Start; 00913 00914 if (Start == End) { 00915 // Match! 00916 return From; 00917 } 00918 00919 DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From); 00920 if (!DE) 00921 return make_error_code(llvm::errc::not_a_directory); 00922 00923 for (DirectoryEntry::iterator I = DE->contents_begin(), 00924 E = DE->contents_end(); 00925 I != E; ++I) { 00926 ErrorOr<Entry *> Result = lookupPath(Start, End, *I); 00927 if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) 00928 return Result; 00929 } 00930 return make_error_code(llvm::errc::no_such_file_or_directory); 00931 } 00932 00933 ErrorOr<Status> VFSFromYAML::status(const Twine &Path, Entry *E) { 00934 assert(E != nullptr); 00935 std::string PathStr(Path.str()); 00936 if (FileEntry *F = dyn_cast<FileEntry>(E)) { 00937 ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath()); 00938 assert(!S || S->getName() == F->getExternalContentsPath()); 00939 if (S && !F->useExternalName(UseExternalNames)) 00940 S->setName(PathStr); 00941 if (S) 00942 S->IsVFSMapped = true; 00943 return S; 00944 } else { // directory 00945 DirectoryEntry *DE = cast<DirectoryEntry>(E); 00946 Status S = DE->getStatus(); 00947 S.setName(PathStr); 00948 return S; 00949 } 00950 } 00951 00952 ErrorOr<Status> VFSFromYAML::status(const Twine &Path) { 00953 ErrorOr<Entry *> Result = lookupPath(Path); 00954 if (!Result) 00955 return Result.getError(); 00956 return status(Path, *Result); 00957 } 00958 00959 ErrorOr<std::unique_ptr<File>> VFSFromYAML::openFileForRead(const Twine &Path) { 00960 ErrorOr<Entry *> E = lookupPath(Path); 00961 if (!E) 00962 return E.getError(); 00963 00964 FileEntry *F = dyn_cast<FileEntry>(*E); 00965 if (!F) // FIXME: errc::not_a_file? 00966 return make_error_code(llvm::errc::invalid_argument); 00967 00968 auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath()); 00969 if (!Result) 00970 return Result; 00971 00972 if (!F->useExternalName(UseExternalNames)) 00973 (*Result)->setName(Path.str()); 00974 00975 return Result; 00976 } 00977 00978 IntrusiveRefCntPtr<FileSystem> 00979 vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer, 00980 SourceMgr::DiagHandlerTy DiagHandler, void *DiagContext, 00981 IntrusiveRefCntPtr<FileSystem> ExternalFS) { 00982 return VFSFromYAML::create(std::move(Buffer), DiagHandler, DiagContext, 00983 ExternalFS); 00984 } 00985 00986 UniqueID vfs::getNextVirtualUniqueID() { 00987 static std::atomic<unsigned> UID; 00988 unsigned ID = ++UID; 00989 // The following assumes that uint64_t max will never collide with a real 00990 // dev_t value from the OS. 00991 return UniqueID(std::numeric_limits<uint64_t>::max(), ID); 00992 } 00993 00994 #ifndef NDEBUG 00995 static bool pathHasTraversal(StringRef Path) { 00996 using namespace llvm::sys; 00997 for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path))) 00998 if (Comp == "." || Comp == "..") 00999 return true; 01000 return false; 01001 } 01002 #endif 01003 01004 void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) { 01005 assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute"); 01006 assert(sys::path::is_absolute(RealPath) && "real path not absolute"); 01007 assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported"); 01008 Mappings.emplace_back(VirtualPath, RealPath); 01009 } 01010 01011 namespace { 01012 class JSONWriter { 01013 llvm::raw_ostream &OS; 01014 SmallVector<StringRef, 16> DirStack; 01015 inline unsigned getDirIndent() { return 4 * DirStack.size(); } 01016 inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); } 01017 bool containedIn(StringRef Parent, StringRef Path); 01018 StringRef containedPart(StringRef Parent, StringRef Path); 01019 void startDirectory(StringRef Path); 01020 void endDirectory(); 01021 void writeEntry(StringRef VPath, StringRef RPath); 01022 01023 public: 01024 JSONWriter(llvm::raw_ostream &OS) : OS(OS) {} 01025 void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> IsCaseSensitive); 01026 }; 01027 } 01028 01029 bool JSONWriter::containedIn(StringRef Parent, StringRef Path) { 01030 using namespace llvm::sys; 01031 // Compare each path component. 01032 auto IParent = path::begin(Parent), EParent = path::end(Parent); 01033 for (auto IChild = path::begin(Path), EChild = path::end(Path); 01034 IParent != EParent && IChild != EChild; ++IParent, ++IChild) { 01035 if (*IParent != *IChild) 01036 return false; 01037 } 01038 // Have we exhausted the parent path? 01039 return IParent == EParent; 01040 } 01041 01042 StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) { 01043 assert(!Parent.empty()); 01044 assert(containedIn(Parent, Path)); 01045 return Path.slice(Parent.size() + 1, StringRef::npos); 01046 } 01047 01048 void JSONWriter::startDirectory(StringRef Path) { 01049 StringRef Name = 01050 DirStack.empty() ? Path : containedPart(DirStack.back(), Path); 01051 DirStack.push_back(Path); 01052 unsigned Indent = getDirIndent(); 01053 OS.indent(Indent) << "{\n"; 01054 OS.indent(Indent + 2) << "'type': 'directory',\n"; 01055 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n"; 01056 OS.indent(Indent + 2) << "'contents': [\n"; 01057 } 01058 01059 void JSONWriter::endDirectory() { 01060 unsigned Indent = getDirIndent(); 01061 OS.indent(Indent + 2) << "]\n"; 01062 OS.indent(Indent) << "}"; 01063 01064 DirStack.pop_back(); 01065 } 01066 01067 void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) { 01068 unsigned Indent = getFileIndent(); 01069 OS.indent(Indent) << "{\n"; 01070 OS.indent(Indent + 2) << "'type': 'file',\n"; 01071 OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n"; 01072 OS.indent(Indent + 2) << "'external-contents': \"" 01073 << llvm::yaml::escape(RPath) << "\"\n"; 01074 OS.indent(Indent) << "}"; 01075 } 01076 01077 void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries, 01078 Optional<bool> IsCaseSensitive) { 01079 using namespace llvm::sys; 01080 01081 OS << "{\n" 01082 " 'version': 0,\n"; 01083 if (IsCaseSensitive.hasValue()) 01084 OS << " 'case-sensitive': '" 01085 << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n"; 01086 OS << " 'roots': [\n"; 01087 01088 if (!Entries.empty()) { 01089 const YAMLVFSEntry &Entry = Entries.front(); 01090 startDirectory(path::parent_path(Entry.VPath)); 01091 writeEntry(path::filename(Entry.VPath), Entry.RPath); 01092 01093 for (const auto &Entry : Entries.slice(1)) { 01094 StringRef Dir = path::parent_path(Entry.VPath); 01095 if (Dir == DirStack.back()) 01096 OS << ",\n"; 01097 else { 01098 while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) { 01099 OS << "\n"; 01100 endDirectory(); 01101 } 01102 OS << ",\n"; 01103 startDirectory(Dir); 01104 } 01105 writeEntry(path::filename(Entry.VPath), Entry.RPath); 01106 } 01107 01108 while (!DirStack.empty()) { 01109 OS << "\n"; 01110 endDirectory(); 01111 } 01112 OS << "\n"; 01113 } 01114 01115 OS << " ]\n" 01116 << "}\n"; 01117 } 01118 01119 void YAMLVFSWriter::write(llvm::raw_ostream &OS) { 01120 std::sort(Mappings.begin(), Mappings.end(), 01121 [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) { 01122 return LHS.VPath < RHS.VPath; 01123 }); 01124 01125 JSONWriter(OS).write(Mappings, IsCaseSensitive); 01126 } 01127 01128 VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl(const Twine &_Path, 01129 VFSFromYAML &FS, 01130 DirectoryEntry::iterator Begin, 01131 DirectoryEntry::iterator End, 01132 std::error_code &EC) 01133 : Dir(_Path.str()), FS(FS), Current(Begin), End(End) { 01134 if (Current != End) { 01135 SmallString<128> PathStr(Dir); 01136 llvm::sys::path::append(PathStr, (*Current)->getName()); 01137 llvm::ErrorOr<vfs::Status> S = FS.status(PathStr.str()); 01138 if (S) 01139 CurrentEntry = *S; 01140 else 01141 EC = S.getError(); 01142 } 01143 } 01144 01145 std::error_code VFSFromYamlDirIterImpl::increment() { 01146 assert(Current != End && "cannot iterate past end"); 01147 if (++Current != End) { 01148 SmallString<128> PathStr(Dir); 01149 llvm::sys::path::append(PathStr, (*Current)->getName()); 01150 llvm::ErrorOr<vfs::Status> S = FS.status(PathStr.str()); 01151 if (!S) 01152 return S.getError(); 01153 CurrentEntry = *S; 01154 } else { 01155 CurrentEntry = Status(); 01156 } 01157 return std::error_code(); 01158 } 01159 01160 vfs::recursive_directory_iterator::recursive_directory_iterator(FileSystem &FS_, 01161 const Twine &Path, 01162 std::error_code &EC) 01163 : FS(&FS_) { 01164 directory_iterator I = FS->dir_begin(Path, EC); 01165 if (!EC && I != directory_iterator()) { 01166 State = std::make_shared<IterState>(); 01167 State->push(I); 01168 } 01169 } 01170 01171 vfs::recursive_directory_iterator & 01172 recursive_directory_iterator::increment(std::error_code &EC) { 01173 assert(FS && State && !State->empty() && "incrementing past end"); 01174 assert(State->top()->isStatusKnown() && "non-canonical end iterator"); 01175 vfs::directory_iterator End; 01176 if (State->top()->isDirectory()) { 01177 vfs::directory_iterator I = FS->dir_begin(State->top()->getName(), EC); 01178 if (EC) 01179 return *this; 01180 if (I != End) { 01181 State->push(I); 01182 return *this; 01183 } 01184 } 01185 01186 while (!State->empty() && State->top().increment(EC) == End) 01187 State->pop(); 01188 01189 if (State->empty()) 01190 State.reset(); // end iterator 01191 01192 return *this; 01193 }