VirtualFileSystem.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //===- VirtualFileSystem.cpp - Virtual File System Layer --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // This file implements the VirtualFileSystem interface.
  10. //===----------------------------------------------------------------------===//
  11. #include "clang/Basic/VirtualFileSystem.h"
  12. #include "llvm/ADT/OwningPtr.h"
  13. #include "llvm/Support/MemoryBuffer.h"
  14. #include "llvm/Support/SourceMgr.h"
  15. #include "llvm/Support/Path.h"
  16. using namespace clang;
  17. using namespace clang::vfs;
  18. using namespace llvm;
  19. using llvm::sys::fs::file_status;
  20. using llvm::sys::fs::file_type;
  21. using llvm::sys::fs::perms;
  22. using llvm::sys::fs::UniqueID;
  23. Status::Status(const file_status &Status)
  24. : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()),
  25. User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()),
  26. Type(Status.type()), Perms(Status.permissions()) {}
  27. Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID,
  28. sys::TimeValue MTime, uint32_t User, uint32_t Group,
  29. uint64_t Size, file_type Type, perms Perms)
  30. : Name(Name), ExternalName(ExternalName), UID(UID), MTime(MTime),
  31. User(User), Group(Group), Size(Size), Type(Type), Perms(Perms) {}
  32. bool Status::equivalent(const Status &Other) const {
  33. return getUniqueID() == Other.getUniqueID();
  34. }
  35. bool Status::isDirectory() const {
  36. return Type == file_type::directory_file;
  37. }
  38. bool Status::isRegularFile() const {
  39. return Type == file_type::regular_file;
  40. }
  41. bool Status::isOther() const {
  42. return exists() && !isRegularFile() && !isDirectory() && !isSymlink();
  43. }
  44. bool Status::isSymlink() const {
  45. return Type == file_type::symlink_file;
  46. }
  47. bool Status::isStatusKnown() const {
  48. return Type != file_type::status_error;
  49. }
  50. bool Status::exists() const {
  51. return isStatusKnown() && Type != file_type::file_not_found;
  52. }
  53. File::~File() {}
  54. FileSystem::~FileSystem() {}
  55. error_code FileSystem::getBufferForFile(const llvm::Twine &Name,
  56. OwningPtr<MemoryBuffer> &Result,
  57. int64_t FileSize,
  58. bool RequiresNullTerminator) {
  59. llvm::OwningPtr<File> F;
  60. if (error_code EC = openFileForRead(Name, F))
  61. return EC;
  62. error_code EC = F->getBuffer(Name, Result, FileSize, RequiresNullTerminator);
  63. return EC;
  64. }
  65. //===-----------------------------------------------------------------------===/
  66. // RealFileSystem implementation
  67. //===-----------------------------------------------------------------------===/
  68. /// \brief Wrapper around a raw file descriptor.
  69. class RealFile : public File {
  70. int FD;
  71. friend class RealFileSystem;
  72. RealFile(int FD) : FD(FD) {
  73. assert(FD >= 0 && "Invalid or inactive file descriptor");
  74. }
  75. public:
  76. ~RealFile();
  77. ErrorOr<Status> status() LLVM_OVERRIDE;
  78. error_code getBuffer(const Twine &Name, OwningPtr<MemoryBuffer> &Result,
  79. int64_t FileSize = -1,
  80. bool RequiresNullTerminator = true) LLVM_OVERRIDE;
  81. error_code close() LLVM_OVERRIDE;
  82. };
  83. RealFile::~RealFile() {
  84. close();
  85. }
  86. ErrorOr<Status> RealFile::status() {
  87. assert(FD != -1 && "cannot stat closed file");
  88. file_status RealStatus;
  89. if (error_code EC = sys::fs::status(FD, RealStatus))
  90. return EC;
  91. return Status(RealStatus);
  92. }
  93. error_code RealFile::getBuffer(const Twine &Name,
  94. OwningPtr<MemoryBuffer> &Result,
  95. int64_t FileSize, bool RequiresNullTerminator) {
  96. assert(FD != -1 && "cannot get buffer for closed file");
  97. return MemoryBuffer::getOpenFile(FD, Name.str().c_str(), Result, FileSize,
  98. RequiresNullTerminator);
  99. }
  100. // FIXME: This is terrible, we need this for ::close.
  101. #if !defined(_MSC_VER) && !defined(__MINGW32__)
  102. #include <unistd.h>
  103. #include <sys/uio.h>
  104. #else
  105. #include <io.h>
  106. #ifndef S_ISFIFO
  107. #define S_ISFIFO(x) (0)
  108. #endif
  109. #endif
  110. error_code RealFile::close() {
  111. if (::close(FD))
  112. return error_code(errno, system_category());
  113. FD = -1;
  114. return error_code::success();
  115. }
  116. /// \brief The file system according to your operating system.
  117. class RealFileSystem : public FileSystem {
  118. public:
  119. ErrorOr<Status> status(const Twine &Path) LLVM_OVERRIDE;
  120. error_code openFileForRead(const Twine &Path,
  121. OwningPtr<File> &Result) LLVM_OVERRIDE;
  122. };
  123. ErrorOr<Status> RealFileSystem::status(const Twine &Path) {
  124. sys::fs::file_status RealStatus;
  125. if (error_code EC = sys::fs::status(Path, RealStatus))
  126. return EC;
  127. Status Result(RealStatus);
  128. Result.setName(Path.str());
  129. Result.setExternalName(Path.str());
  130. return Result;
  131. }
  132. error_code RealFileSystem::openFileForRead(const Twine &Name,
  133. OwningPtr<File> &Result) {
  134. int FD;
  135. if (error_code EC = sys::fs::openFileForRead(Name, FD))
  136. return EC;
  137. Result.reset(new RealFile(FD));
  138. return error_code::success();
  139. }
  140. IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() {
  141. static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem();
  142. return FS;
  143. }
  144. //===-----------------------------------------------------------------------===/
  145. // OverlayFileSystem implementation
  146. //===-----------------------------------------------------------------------===/
  147. OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) {
  148. pushOverlay(BaseFS);
  149. }
  150. void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) {
  151. FSList.push_back(FS);
  152. }
  153. ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) {
  154. // FIXME: handle symlinks that cross file systems
  155. for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
  156. ErrorOr<Status> Status = (*I)->status(Path);
  157. if (Status || Status.getError() != errc::no_such_file_or_directory)
  158. return Status;
  159. }
  160. return error_code(errc::no_such_file_or_directory, system_category());
  161. }
  162. error_code OverlayFileSystem::openFileForRead(const llvm::Twine &Path,
  163. OwningPtr<File> &Result) {
  164. // FIXME: handle symlinks that cross file systems
  165. for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) {
  166. error_code EC = (*I)->openFileForRead(Path, Result);
  167. if (!EC || EC != errc::no_such_file_or_directory)
  168. return EC;
  169. }
  170. return error_code(errc::no_such_file_or_directory, system_category());
  171. }