Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 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 | |
| 12 | #include "clang/Basic/VirtualFileSystem.h" |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/iterator_range.h" |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 19 | #include "llvm/Support/YAMLParser.h" |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 20 | #include <atomic> |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 21 | #include <memory> |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | using namespace clang::vfs; |
| 25 | using namespace llvm; |
| 26 | using llvm::sys::fs::file_status; |
| 27 | using llvm::sys::fs::file_type; |
| 28 | using llvm::sys::fs::perms; |
| 29 | using llvm::sys::fs::UniqueID; |
| 30 | |
| 31 | Status::Status(const file_status &Status) |
| 32 | : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()), |
| 33 | User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()), |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 34 | Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false) {} |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 35 | |
| 36 | Status::Status(StringRef Name, StringRef ExternalName, UniqueID UID, |
| 37 | sys::TimeValue MTime, uint32_t User, uint32_t Group, |
| 38 | uint64_t Size, file_type Type, perms Perms) |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 39 | : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size), |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 40 | Type(Type), Perms(Perms), IsVFSMapped(false) {} |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 41 | |
| 42 | bool Status::equivalent(const Status &Other) const { |
| 43 | return getUniqueID() == Other.getUniqueID(); |
| 44 | } |
| 45 | bool Status::isDirectory() const { |
| 46 | return Type == file_type::directory_file; |
| 47 | } |
| 48 | bool Status::isRegularFile() const { |
| 49 | return Type == file_type::regular_file; |
| 50 | } |
| 51 | bool Status::isOther() const { |
| 52 | return exists() && !isRegularFile() && !isDirectory() && !isSymlink(); |
| 53 | } |
| 54 | bool Status::isSymlink() const { |
| 55 | return Type == file_type::symlink_file; |
| 56 | } |
| 57 | bool Status::isStatusKnown() const { |
| 58 | return Type != file_type::status_error; |
| 59 | } |
| 60 | bool Status::exists() const { |
| 61 | return isStatusKnown() && Type != file_type::file_not_found; |
| 62 | } |
| 63 | |
| 64 | File::~File() {} |
| 65 | |
| 66 | FileSystem::~FileSystem() {} |
| 67 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 68 | std::error_code FileSystem::getBufferForFile( |
| 69 | const llvm::Twine &Name, std::unique_ptr<MemoryBuffer> &Result, |
| 70 | int64_t FileSize, bool RequiresNullTerminator, bool IsVolatile) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 71 | std::unique_ptr<File> F; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 72 | if (std::error_code EC = openFileForRead(Name, F)) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 73 | return EC; |
| 74 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 75 | std::error_code EC = |
| 76 | F->getBuffer(Name, Result, FileSize, RequiresNullTerminator, IsVolatile); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 77 | return EC; |
| 78 | } |
| 79 | |
| 80 | //===-----------------------------------------------------------------------===/ |
| 81 | // RealFileSystem implementation |
| 82 | //===-----------------------------------------------------------------------===/ |
| 83 | |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 84 | namespace { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 85 | /// \brief Wrapper around a raw file descriptor. |
| 86 | class RealFile : public File { |
| 87 | int FD; |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 88 | Status S; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 89 | friend class RealFileSystem; |
| 90 | RealFile(int FD) : FD(FD) { |
| 91 | assert(FD >= 0 && "Invalid or inactive file descriptor"); |
| 92 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 93 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 94 | public: |
| 95 | ~RealFile(); |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 96 | ErrorOr<Status> status() override; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 97 | std::error_code getBuffer(const Twine &Name, |
| 98 | std::unique_ptr<MemoryBuffer> &Result, |
| 99 | int64_t FileSize = -1, |
| 100 | bool RequiresNullTerminator = true, |
| 101 | bool IsVolatile = false) override; |
| 102 | std::error_code close() override; |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 103 | void setName(StringRef Name) override; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 104 | }; |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 105 | } // end anonymous namespace |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 106 | RealFile::~RealFile() { close(); } |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 107 | |
| 108 | ErrorOr<Status> RealFile::status() { |
| 109 | assert(FD != -1 && "cannot stat closed file"); |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 110 | if (!S.isStatusKnown()) { |
| 111 | file_status RealStatus; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 112 | if (std::error_code EC = sys::fs::status(FD, RealStatus)) |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 113 | return EC; |
| 114 | Status NewS(RealStatus); |
| 115 | NewS.setName(S.getName()); |
Chandler Carruth | c72d9b3 | 2014-03-02 04:02:40 +0000 | [diff] [blame] | 116 | S = std::move(NewS); |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 117 | } |
| 118 | return S; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 121 | std::error_code RealFile::getBuffer(const Twine &Name, |
| 122 | std::unique_ptr<MemoryBuffer> &Result, |
| 123 | int64_t FileSize, |
| 124 | bool RequiresNullTerminator, |
| 125 | bool IsVolatile) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 126 | assert(FD != -1 && "cannot get buffer for closed file"); |
| 127 | return MemoryBuffer::getOpenFile(FD, Name.str().c_str(), Result, FileSize, |
Argyrios Kyrtzidis | 26d5639 | 2014-05-05 21:57:46 +0000 | [diff] [blame] | 128 | RequiresNullTerminator, IsVolatile); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // FIXME: This is terrible, we need this for ::close. |
| 132 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 133 | #include <unistd.h> |
| 134 | #include <sys/uio.h> |
| 135 | #else |
| 136 | #include <io.h> |
| 137 | #ifndef S_ISFIFO |
| 138 | #define S_ISFIFO(x) (0) |
| 139 | #endif |
| 140 | #endif |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 141 | std::error_code RealFile::close() { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 142 | if (::close(FD)) |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 143 | return std::error_code(errno, std::generic_category()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 144 | FD = -1; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 145 | return std::error_code(); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 148 | void RealFile::setName(StringRef Name) { |
| 149 | S.setName(Name); |
| 150 | } |
| 151 | |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 152 | namespace { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 153 | /// \brief The file system according to your operating system. |
| 154 | class RealFileSystem : public FileSystem { |
| 155 | public: |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 156 | ErrorOr<Status> status(const Twine &Path) override; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 157 | std::error_code openFileForRead(const Twine &Path, |
| 158 | std::unique_ptr<File> &Result) override; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 159 | }; |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 160 | } // end anonymous namespace |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 161 | |
| 162 | ErrorOr<Status> RealFileSystem::status(const Twine &Path) { |
| 163 | sys::fs::file_status RealStatus; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 164 | if (std::error_code EC = sys::fs::status(Path, RealStatus)) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 165 | return EC; |
| 166 | Status Result(RealStatus); |
| 167 | Result.setName(Path.str()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 168 | return Result; |
| 169 | } |
| 170 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 171 | std::error_code RealFileSystem::openFileForRead(const Twine &Name, |
| 172 | std::unique_ptr<File> &Result) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 173 | int FD; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 174 | if (std::error_code EC = sys::fs::openFileForRead(Name, FD)) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 175 | return EC; |
| 176 | Result.reset(new RealFile(FD)); |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 177 | Result->setName(Name.str()); |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 178 | return std::error_code(); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() { |
| 182 | static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem(); |
| 183 | return FS; |
| 184 | } |
| 185 | |
| 186 | //===-----------------------------------------------------------------------===/ |
| 187 | // OverlayFileSystem implementation |
| 188 | //===-----------------------------------------------------------------------===/ |
| 189 | OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) { |
| 190 | pushOverlay(BaseFS); |
| 191 | } |
| 192 | |
| 193 | void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) { |
| 194 | FSList.push_back(FS); |
| 195 | } |
| 196 | |
| 197 | ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) { |
| 198 | // FIXME: handle symlinks that cross file systems |
| 199 | for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { |
| 200 | ErrorOr<Status> Status = (*I)->status(Path); |
Rafael Espindola | 96b0330 | 2014-06-11 19:05:55 +0000 | [diff] [blame] | 201 | if (Status || Status.getError() != std::errc::no_such_file_or_directory) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 202 | return Status; |
| 203 | } |
Rafael Espindola | 790589c | 2014-06-12 03:53:36 +0000 | [diff] [blame] | 204 | return std::make_error_code(std::errc::no_such_file_or_directory); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 207 | std::error_code |
| 208 | OverlayFileSystem::openFileForRead(const llvm::Twine &Path, |
| 209 | std::unique_ptr<File> &Result) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 210 | // FIXME: handle symlinks that cross file systems |
| 211 | for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 212 | std::error_code EC = (*I)->openFileForRead(Path, Result); |
Rafael Espindola | 96b0330 | 2014-06-11 19:05:55 +0000 | [diff] [blame] | 213 | if (!EC || EC != std::errc::no_such_file_or_directory) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 214 | return EC; |
| 215 | } |
Rafael Espindola | 790589c | 2014-06-12 03:53:36 +0000 | [diff] [blame] | 216 | return std::make_error_code(std::errc::no_such_file_or_directory); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 217 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 218 | |
| 219 | //===-----------------------------------------------------------------------===/ |
| 220 | // VFSFromYAML implementation |
| 221 | //===-----------------------------------------------------------------------===/ |
| 222 | |
| 223 | // Allow DenseMap<StringRef, ...>. This is useful below because we know all the |
| 224 | // strings are literals and will outlive the map, and there is no reason to |
| 225 | // store them. |
| 226 | namespace llvm { |
| 227 | template<> |
| 228 | struct DenseMapInfo<StringRef> { |
| 229 | // This assumes that "" will never be a valid key. |
| 230 | static inline StringRef getEmptyKey() { return StringRef(""); } |
| 231 | static inline StringRef getTombstoneKey() { return StringRef(); } |
| 232 | static unsigned getHashValue(StringRef Val) { return HashString(Val); } |
| 233 | static bool isEqual(StringRef LHS, StringRef RHS) { return LHS == RHS; } |
| 234 | }; |
| 235 | } |
| 236 | |
| 237 | namespace { |
| 238 | |
| 239 | enum EntryKind { |
| 240 | EK_Directory, |
| 241 | EK_File |
| 242 | }; |
| 243 | |
| 244 | /// \brief A single file or directory in the VFS. |
| 245 | class Entry { |
| 246 | EntryKind Kind; |
| 247 | std::string Name; |
| 248 | |
| 249 | public: |
| 250 | virtual ~Entry(); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 251 | Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {} |
| 252 | StringRef getName() const { return Name; } |
| 253 | EntryKind getKind() const { return Kind; } |
| 254 | }; |
| 255 | |
| 256 | class DirectoryEntry : public Entry { |
| 257 | std::vector<Entry *> Contents; |
| 258 | Status S; |
| 259 | |
| 260 | public: |
| 261 | virtual ~DirectoryEntry(); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 262 | DirectoryEntry(StringRef Name, std::vector<Entry *> Contents, Status S) |
| 263 | : Entry(EK_Directory, Name), Contents(std::move(Contents)), |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 264 | S(std::move(S)) {} |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 265 | Status getStatus() { return S; } |
| 266 | typedef std::vector<Entry *>::iterator iterator; |
| 267 | iterator contents_begin() { return Contents.begin(); } |
| 268 | iterator contents_end() { return Contents.end(); } |
| 269 | static bool classof(const Entry *E) { return E->getKind() == EK_Directory; } |
| 270 | }; |
| 271 | |
| 272 | class FileEntry : public Entry { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 273 | public: |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 274 | enum NameKind { |
| 275 | NK_NotSet, |
| 276 | NK_External, |
| 277 | NK_Virtual |
| 278 | }; |
| 279 | private: |
| 280 | std::string ExternalContentsPath; |
| 281 | NameKind UseName; |
| 282 | public: |
| 283 | FileEntry(StringRef Name, StringRef ExternalContentsPath, NameKind UseName) |
| 284 | : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath), |
| 285 | UseName(UseName) {} |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 286 | StringRef getExternalContentsPath() const { return ExternalContentsPath; } |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 287 | /// \brief whether to use the external path as the name for this file. |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 288 | bool useExternalName(bool GlobalUseExternalName) const { |
| 289 | return UseName == NK_NotSet ? GlobalUseExternalName |
| 290 | : (UseName == NK_External); |
| 291 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 292 | static bool classof(const Entry *E) { return E->getKind() == EK_File; } |
| 293 | }; |
| 294 | |
| 295 | /// \brief A virtual file system parsed from a YAML file. |
| 296 | /// |
| 297 | /// Currently, this class allows creating virtual directories and mapping |
| 298 | /// virtual file paths to existing external files, available in \c ExternalFS. |
| 299 | /// |
| 300 | /// The basic structure of the parsed file is: |
| 301 | /// \verbatim |
| 302 | /// { |
| 303 | /// 'version': <version number>, |
| 304 | /// <optional configuration> |
| 305 | /// 'roots': [ |
| 306 | /// <directory entries> |
| 307 | /// ] |
| 308 | /// } |
| 309 | /// \endverbatim |
| 310 | /// |
| 311 | /// All configuration options are optional. |
| 312 | /// 'case-sensitive': <boolean, default=true> |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 313 | /// 'use-external-names': <boolean, default=true> |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 314 | /// |
| 315 | /// Virtual directories are represented as |
| 316 | /// \verbatim |
| 317 | /// { |
| 318 | /// 'type': 'directory', |
| 319 | /// 'name': <string>, |
| 320 | /// 'contents': [ <file or directory entries> ] |
| 321 | /// } |
| 322 | /// \endverbatim |
| 323 | /// |
| 324 | /// The default attributes for virtual directories are: |
| 325 | /// \verbatim |
| 326 | /// MTime = now() when created |
| 327 | /// Perms = 0777 |
| 328 | /// User = Group = 0 |
| 329 | /// Size = 0 |
| 330 | /// UniqueID = unspecified unique value |
| 331 | /// \endverbatim |
| 332 | /// |
| 333 | /// Re-mapped files are represented as |
| 334 | /// \verbatim |
| 335 | /// { |
| 336 | /// 'type': 'file', |
| 337 | /// 'name': <string>, |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 338 | /// 'use-external-name': <boolean> # Optional |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 339 | /// 'external-contents': <path to external file>) |
| 340 | /// } |
| 341 | /// \endverbatim |
| 342 | /// |
| 343 | /// and inherit their attributes from the external contents. |
| 344 | /// |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 345 | /// In both cases, the 'name' field may contain multiple path components (e.g. |
| 346 | /// /path/to/file). However, any directory that contains more than one child |
| 347 | /// must be uniquely represented by a directory entry. |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 348 | class VFSFromYAML : public vfs::FileSystem { |
| 349 | std::vector<Entry *> Roots; ///< The root(s) of the virtual file system. |
| 350 | /// \brief The file system to use for external references. |
| 351 | IntrusiveRefCntPtr<FileSystem> ExternalFS; |
| 352 | |
| 353 | /// @name Configuration |
| 354 | /// @{ |
| 355 | |
| 356 | /// \brief Whether to perform case-sensitive comparisons. |
| 357 | /// |
| 358 | /// Currently, case-insensitive matching only works correctly with ASCII. |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 359 | bool CaseSensitive; |
| 360 | |
| 361 | /// \brief Whether to use to use the value of 'external-contents' for the |
| 362 | /// names of files. This global value is overridable on a per-file basis. |
| 363 | bool UseExternalNames; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 364 | /// @} |
| 365 | |
| 366 | friend class VFSFromYAMLParser; |
| 367 | |
| 368 | private: |
| 369 | VFSFromYAML(IntrusiveRefCntPtr<FileSystem> ExternalFS) |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 370 | : ExternalFS(ExternalFS), CaseSensitive(true), UseExternalNames(true) {} |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 371 | |
| 372 | /// \brief Looks up \p Path in \c Roots. |
| 373 | ErrorOr<Entry *> lookupPath(const Twine &Path); |
| 374 | |
| 375 | /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly |
| 376 | /// recursing into the contents of \p From if it is a directory. |
| 377 | ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start, |
| 378 | sys::path::const_iterator End, Entry *From); |
| 379 | |
| 380 | public: |
| 381 | ~VFSFromYAML(); |
| 382 | |
| 383 | /// \brief Parses \p Buffer, which is expected to be in YAML format and |
| 384 | /// returns a virtual file system representing its contents. |
| 385 | /// |
| 386 | /// Takes ownership of \p Buffer. |
| 387 | static VFSFromYAML *create(MemoryBuffer *Buffer, |
| 388 | SourceMgr::DiagHandlerTy DiagHandler, |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 389 | void *DiagContext, |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 390 | IntrusiveRefCntPtr<FileSystem> ExternalFS); |
| 391 | |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 392 | ErrorOr<Status> status(const Twine &Path) override; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 393 | std::error_code openFileForRead(const Twine &Path, |
| 394 | std::unique_ptr<File> &Result) override; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | /// \brief A helper class to hold the common YAML parsing state. |
| 398 | class VFSFromYAMLParser { |
| 399 | yaml::Stream &Stream; |
| 400 | |
| 401 | void error(yaml::Node *N, const Twine &Msg) { |
| 402 | Stream.printError(N, Msg); |
| 403 | } |
| 404 | |
| 405 | // false on error |
| 406 | bool parseScalarString(yaml::Node *N, StringRef &Result, |
| 407 | SmallVectorImpl<char> &Storage) { |
| 408 | yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N); |
| 409 | if (!S) { |
| 410 | error(N, "expected string"); |
| 411 | return false; |
| 412 | } |
| 413 | Result = S->getValue(Storage); |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | // false on error |
| 418 | bool parseScalarBool(yaml::Node *N, bool &Result) { |
| 419 | SmallString<5> Storage; |
| 420 | StringRef Value; |
| 421 | if (!parseScalarString(N, Value, Storage)) |
| 422 | return false; |
| 423 | |
| 424 | if (Value.equals_lower("true") || Value.equals_lower("on") || |
| 425 | Value.equals_lower("yes") || Value == "1") { |
| 426 | Result = true; |
| 427 | return true; |
| 428 | } else if (Value.equals_lower("false") || Value.equals_lower("off") || |
| 429 | Value.equals_lower("no") || Value == "0") { |
| 430 | Result = false; |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | error(N, "expected boolean value"); |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | struct KeyStatus { |
| 439 | KeyStatus(bool Required=false) : Required(Required), Seen(false) {} |
| 440 | bool Required; |
| 441 | bool Seen; |
| 442 | }; |
| 443 | typedef std::pair<StringRef, KeyStatus> KeyStatusPair; |
| 444 | |
| 445 | // false on error |
| 446 | bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key, |
| 447 | DenseMap<StringRef, KeyStatus> &Keys) { |
| 448 | if (!Keys.count(Key)) { |
| 449 | error(KeyNode, "unknown key"); |
| 450 | return false; |
| 451 | } |
| 452 | KeyStatus &S = Keys[Key]; |
| 453 | if (S.Seen) { |
| 454 | error(KeyNode, Twine("duplicate key '") + Key + "'"); |
| 455 | return false; |
| 456 | } |
| 457 | S.Seen = true; |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | // false on error |
| 462 | bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) { |
| 463 | for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(), |
| 464 | E = Keys.end(); |
| 465 | I != E; ++I) { |
| 466 | if (I->second.Required && !I->second.Seen) { |
| 467 | error(Obj, Twine("missing key '") + I->first + "'"); |
| 468 | return false; |
| 469 | } |
| 470 | } |
| 471 | return true; |
| 472 | } |
| 473 | |
| 474 | Entry *parseEntry(yaml::Node *N) { |
| 475 | yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N); |
| 476 | if (!M) { |
| 477 | error(N, "expected mapping node for file or directory entry"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 478 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | KeyStatusPair Fields[] = { |
| 482 | KeyStatusPair("name", true), |
| 483 | KeyStatusPair("type", true), |
| 484 | KeyStatusPair("contents", false), |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 485 | KeyStatusPair("external-contents", false), |
| 486 | KeyStatusPair("use-external-name", false), |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 487 | }; |
| 488 | |
| 489 | DenseMap<StringRef, KeyStatus> Keys( |
| 490 | &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0])); |
| 491 | |
| 492 | bool HasContents = false; // external or otherwise |
| 493 | std::vector<Entry *> EntryArrayContents; |
| 494 | std::string ExternalContentsPath; |
| 495 | std::string Name; |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 496 | FileEntry::NameKind UseExternalName = FileEntry::NK_NotSet; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 497 | EntryKind Kind; |
| 498 | |
| 499 | for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E; |
| 500 | ++I) { |
| 501 | StringRef Key; |
| 502 | // Reuse the buffer for key and value, since we don't look at key after |
| 503 | // parsing value. |
| 504 | SmallString<256> Buffer; |
| 505 | if (!parseScalarString(I->getKey(), Key, Buffer)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 506 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 507 | |
| 508 | if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 509 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 510 | |
| 511 | StringRef Value; |
| 512 | if (Key == "name") { |
| 513 | if (!parseScalarString(I->getValue(), Value, Buffer)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 514 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 515 | Name = Value; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 516 | } else if (Key == "type") { |
| 517 | if (!parseScalarString(I->getValue(), Value, Buffer)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 518 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 519 | if (Value == "file") |
| 520 | Kind = EK_File; |
| 521 | else if (Value == "directory") |
| 522 | Kind = EK_Directory; |
| 523 | else { |
| 524 | error(I->getValue(), "unknown value for 'type'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 525 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 526 | } |
| 527 | } else if (Key == "contents") { |
| 528 | if (HasContents) { |
| 529 | error(I->getKey(), |
| 530 | "entry already has 'contents' or 'external-contents'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 531 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 532 | } |
| 533 | HasContents = true; |
| 534 | yaml::SequenceNode *Contents = |
| 535 | dyn_cast<yaml::SequenceNode>(I->getValue()); |
| 536 | if (!Contents) { |
| 537 | // FIXME: this is only for directories, what about files? |
| 538 | error(I->getValue(), "expected array"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 539 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | for (yaml::SequenceNode::iterator I = Contents->begin(), |
| 543 | E = Contents->end(); |
| 544 | I != E; ++I) { |
| 545 | if (Entry *E = parseEntry(&*I)) |
| 546 | EntryArrayContents.push_back(E); |
| 547 | else |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 548 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 549 | } |
| 550 | } else if (Key == "external-contents") { |
| 551 | if (HasContents) { |
| 552 | error(I->getKey(), |
| 553 | "entry already has 'contents' or 'external-contents'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 554 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 555 | } |
| 556 | HasContents = true; |
| 557 | if (!parseScalarString(I->getValue(), Value, Buffer)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 558 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 559 | ExternalContentsPath = Value; |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 560 | } else if (Key == "use-external-name") { |
| 561 | bool Val; |
| 562 | if (!parseScalarBool(I->getValue(), Val)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 563 | return nullptr; |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 564 | UseExternalName = Val ? FileEntry::NK_External : FileEntry::NK_Virtual; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 565 | } else { |
| 566 | llvm_unreachable("key missing from Keys"); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (Stream.failed()) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 571 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 572 | |
| 573 | // check for missing keys |
| 574 | if (!HasContents) { |
| 575 | error(N, "missing key 'contents' or 'external-contents'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 576 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 577 | } |
| 578 | if (!checkMissingKeys(N, Keys)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 579 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 580 | |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 581 | // check invalid configuration |
| 582 | if (Kind == EK_Directory && UseExternalName != FileEntry::NK_NotSet) { |
| 583 | error(N, "'use-external-name' is not supported for directories"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 584 | return nullptr; |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 587 | // Remove trailing slash(es), being careful not to remove the root path |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 588 | StringRef Trimmed(Name); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 589 | size_t RootPathLen = sys::path::root_path(Trimmed).size(); |
| 590 | while (Trimmed.size() > RootPathLen && |
| 591 | sys::path::is_separator(Trimmed.back())) |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 592 | Trimmed = Trimmed.slice(0, Trimmed.size()-1); |
| 593 | // Get the last component |
| 594 | StringRef LastComponent = sys::path::filename(Trimmed); |
| 595 | |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 596 | Entry *Result = nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 597 | switch (Kind) { |
| 598 | case EK_File: |
Chandler Carruth | c72d9b3 | 2014-03-02 04:02:40 +0000 | [diff] [blame] | 599 | Result = new FileEntry(LastComponent, std::move(ExternalContentsPath), |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 600 | UseExternalName); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 601 | break; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 602 | case EK_Directory: |
Chandler Carruth | c72d9b3 | 2014-03-02 04:02:40 +0000 | [diff] [blame] | 603 | Result = new DirectoryEntry(LastComponent, std::move(EntryArrayContents), |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 604 | Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, |
| 605 | 0, file_type::directory_file, sys::fs::all_all)); |
| 606 | break; |
| 607 | } |
| 608 | |
| 609 | StringRef Parent = sys::path::parent_path(Trimmed); |
| 610 | if (Parent.empty()) |
| 611 | return Result; |
| 612 | |
| 613 | // if 'name' contains multiple components, create implicit directory entries |
| 614 | for (sys::path::reverse_iterator I = sys::path::rbegin(Parent), |
| 615 | E = sys::path::rend(Parent); |
| 616 | I != E; ++I) { |
Benjamin Kramer | 3f755aa | 2014-03-10 17:55:02 +0000 | [diff] [blame] | 617 | Result = new DirectoryEntry(*I, llvm::makeArrayRef(Result), |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 618 | Status("", "", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, |
| 619 | 0, file_type::directory_file, sys::fs::all_all)); |
| 620 | } |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 621 | return Result; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | public: |
| 625 | VFSFromYAMLParser(yaml::Stream &S) : Stream(S) {} |
| 626 | |
| 627 | // false on error |
| 628 | bool parse(yaml::Node *Root, VFSFromYAML *FS) { |
| 629 | yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root); |
| 630 | if (!Top) { |
| 631 | error(Root, "expected mapping node"); |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | KeyStatusPair Fields[] = { |
| 636 | KeyStatusPair("version", true), |
| 637 | KeyStatusPair("case-sensitive", false), |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 638 | KeyStatusPair("use-external-names", false), |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 639 | KeyStatusPair("roots", true), |
| 640 | }; |
| 641 | |
| 642 | DenseMap<StringRef, KeyStatus> Keys( |
| 643 | &Fields[0], Fields + sizeof(Fields)/sizeof(Fields[0])); |
| 644 | |
| 645 | // Parse configuration and 'roots' |
| 646 | for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E; |
| 647 | ++I) { |
| 648 | SmallString<10> KeyBuffer; |
| 649 | StringRef Key; |
| 650 | if (!parseScalarString(I->getKey(), Key, KeyBuffer)) |
| 651 | return false; |
| 652 | |
| 653 | if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys)) |
| 654 | return false; |
| 655 | |
| 656 | if (Key == "roots") { |
| 657 | yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue()); |
| 658 | if (!Roots) { |
| 659 | error(I->getValue(), "expected array"); |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end(); |
| 664 | I != E; ++I) { |
| 665 | if (Entry *E = parseEntry(&*I)) |
| 666 | FS->Roots.push_back(E); |
| 667 | else |
| 668 | return false; |
| 669 | } |
| 670 | } else if (Key == "version") { |
| 671 | StringRef VersionString; |
| 672 | SmallString<4> Storage; |
| 673 | if (!parseScalarString(I->getValue(), VersionString, Storage)) |
| 674 | return false; |
| 675 | int Version; |
| 676 | if (VersionString.getAsInteger<int>(10, Version)) { |
| 677 | error(I->getValue(), "expected integer"); |
| 678 | return false; |
| 679 | } |
| 680 | if (Version < 0) { |
| 681 | error(I->getValue(), "invalid version number"); |
| 682 | return false; |
| 683 | } |
| 684 | if (Version != 0) { |
| 685 | error(I->getValue(), "version mismatch, expected 0"); |
| 686 | return false; |
| 687 | } |
| 688 | } else if (Key == "case-sensitive") { |
| 689 | if (!parseScalarBool(I->getValue(), FS->CaseSensitive)) |
| 690 | return false; |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 691 | } else if (Key == "use-external-names") { |
| 692 | if (!parseScalarBool(I->getValue(), FS->UseExternalNames)) |
| 693 | return false; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 694 | } else { |
| 695 | llvm_unreachable("key missing from Keys"); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if (Stream.failed()) |
| 700 | return false; |
| 701 | |
| 702 | if (!checkMissingKeys(Top, Keys)) |
| 703 | return false; |
| 704 | return true; |
| 705 | } |
| 706 | }; |
| 707 | } // end of anonymous namespace |
| 708 | |
| 709 | Entry::~Entry() {} |
| 710 | DirectoryEntry::~DirectoryEntry() { llvm::DeleteContainerPointers(Contents); } |
| 711 | |
| 712 | VFSFromYAML::~VFSFromYAML() { llvm::DeleteContainerPointers(Roots); } |
| 713 | |
| 714 | VFSFromYAML *VFSFromYAML::create(MemoryBuffer *Buffer, |
| 715 | SourceMgr::DiagHandlerTy DiagHandler, |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 716 | void *DiagContext, |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 717 | IntrusiveRefCntPtr<FileSystem> ExternalFS) { |
| 718 | |
| 719 | SourceMgr SM; |
| 720 | yaml::Stream Stream(Buffer, SM); |
| 721 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 722 | SM.setDiagHandler(DiagHandler, DiagContext); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 723 | yaml::document_iterator DI = Stream.begin(); |
| 724 | yaml::Node *Root = DI->getRoot(); |
| 725 | if (DI == Stream.end() || !Root) { |
| 726 | SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 727 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | VFSFromYAMLParser P(Stream); |
| 731 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 732 | std::unique_ptr<VFSFromYAML> FS(new VFSFromYAML(ExternalFS)); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 733 | if (!P.parse(Root, FS.get())) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 734 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 735 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 736 | return FS.release(); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | ErrorOr<Entry *> VFSFromYAML::lookupPath(const Twine &Path_) { |
Ben Langmuir | a6f8ca8 | 2014-03-04 22:34:50 +0000 | [diff] [blame] | 740 | SmallString<256> Path; |
| 741 | Path_.toVector(Path); |
| 742 | |
| 743 | // Handle relative paths |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 744 | if (std::error_code EC = sys::fs::make_absolute(Path)) |
Ben Langmuir | a6f8ca8 | 2014-03-04 22:34:50 +0000 | [diff] [blame] | 745 | return EC; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 746 | |
| 747 | if (Path.empty()) |
Rafael Espindola | 790589c | 2014-06-12 03:53:36 +0000 | [diff] [blame] | 748 | return std::make_error_code(std::errc::invalid_argument); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 749 | |
| 750 | sys::path::const_iterator Start = sys::path::begin(Path); |
| 751 | sys::path::const_iterator End = sys::path::end(Path); |
| 752 | for (std::vector<Entry *>::iterator I = Roots.begin(), E = Roots.end(); |
| 753 | I != E; ++I) { |
| 754 | ErrorOr<Entry *> Result = lookupPath(Start, End, *I); |
Rafael Espindola | 96b0330 | 2014-06-11 19:05:55 +0000 | [diff] [blame] | 755 | if (Result || Result.getError() != std::errc::no_such_file_or_directory) |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 756 | return Result; |
| 757 | } |
Rafael Espindola | 790589c | 2014-06-12 03:53:36 +0000 | [diff] [blame] | 758 | return std::make_error_code(std::errc::no_such_file_or_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | ErrorOr<Entry *> VFSFromYAML::lookupPath(sys::path::const_iterator Start, |
| 762 | sys::path::const_iterator End, |
| 763 | Entry *From) { |
Ben Langmuir | a6f8ca8 | 2014-03-04 22:34:50 +0000 | [diff] [blame] | 764 | if (Start->equals(".")) |
| 765 | ++Start; |
| 766 | |
| 767 | // FIXME: handle .. |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 768 | if (CaseSensitive ? !Start->equals(From->getName()) |
| 769 | : !Start->equals_lower(From->getName())) |
| 770 | // failure to match |
Rafael Espindola | 790589c | 2014-06-12 03:53:36 +0000 | [diff] [blame] | 771 | return std::make_error_code(std::errc::no_such_file_or_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 772 | |
| 773 | ++Start; |
| 774 | |
| 775 | if (Start == End) { |
| 776 | // Match! |
| 777 | return From; |
| 778 | } |
| 779 | |
| 780 | DirectoryEntry *DE = dyn_cast<DirectoryEntry>(From); |
| 781 | if (!DE) |
Rafael Espindola | 790589c | 2014-06-12 03:53:36 +0000 | [diff] [blame] | 782 | return std::make_error_code(std::errc::not_a_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 783 | |
| 784 | for (DirectoryEntry::iterator I = DE->contents_begin(), |
| 785 | E = DE->contents_end(); |
| 786 | I != E; ++I) { |
| 787 | ErrorOr<Entry *> Result = lookupPath(Start, End, *I); |
Rafael Espindola | 96b0330 | 2014-06-11 19:05:55 +0000 | [diff] [blame] | 788 | if (Result || Result.getError() != std::errc::no_such_file_or_directory) |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 789 | return Result; |
| 790 | } |
Rafael Espindola | 790589c | 2014-06-12 03:53:36 +0000 | [diff] [blame] | 791 | return std::make_error_code(std::errc::no_such_file_or_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | ErrorOr<Status> VFSFromYAML::status(const Twine &Path) { |
| 795 | ErrorOr<Entry *> Result = lookupPath(Path); |
| 796 | if (!Result) |
| 797 | return Result.getError(); |
| 798 | |
| 799 | std::string PathStr(Path.str()); |
| 800 | if (FileEntry *F = dyn_cast<FileEntry>(*Result)) { |
| 801 | ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath()); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 802 | assert(!S || S->getName() == F->getExternalContentsPath()); |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 803 | if (S && !F->useExternalName(UseExternalNames)) |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 804 | S->setName(PathStr); |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 805 | if (S) |
| 806 | S->IsVFSMapped = true; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 807 | return S; |
| 808 | } else { // directory |
| 809 | DirectoryEntry *DE = cast<DirectoryEntry>(*Result); |
| 810 | Status S = DE->getStatus(); |
| 811 | S.setName(PathStr); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 812 | return S; |
| 813 | } |
| 814 | } |
| 815 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 816 | std::error_code |
| 817 | VFSFromYAML::openFileForRead(const Twine &Path, |
| 818 | std::unique_ptr<vfs::File> &Result) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 819 | ErrorOr<Entry *> E = lookupPath(Path); |
| 820 | if (!E) |
| 821 | return E.getError(); |
| 822 | |
| 823 | FileEntry *F = dyn_cast<FileEntry>(*E); |
| 824 | if (!F) // FIXME: errc::not_a_file? |
Rafael Espindola | 790589c | 2014-06-12 03:53:36 +0000 | [diff] [blame] | 825 | return std::make_error_code(std::errc::invalid_argument); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 826 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 827 | if (std::error_code EC = |
| 828 | ExternalFS->openFileForRead(F->getExternalContentsPath(), Result)) |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 829 | return EC; |
| 830 | |
| 831 | if (!F->useExternalName(UseExternalNames)) |
| 832 | Result->setName(Path.str()); |
| 833 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame^] | 834 | return std::error_code(); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | IntrusiveRefCntPtr<FileSystem> |
| 838 | vfs::getVFSFromYAML(MemoryBuffer *Buffer, SourceMgr::DiagHandlerTy DiagHandler, |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 839 | void *DiagContext, |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 840 | IntrusiveRefCntPtr<FileSystem> ExternalFS) { |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 841 | return VFSFromYAML::create(Buffer, DiagHandler, DiagContext, ExternalFS); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | UniqueID vfs::getNextVirtualUniqueID() { |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 845 | static std::atomic<unsigned> UID; |
| 846 | unsigned ID = ++UID; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 847 | // The following assumes that uint64_t max will never collide with a real |
| 848 | // dev_t value from the OS. |
| 849 | return UniqueID(std::numeric_limits<uint64_t>::max(), ID); |
| 850 | } |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 851 | |
| 852 | #ifndef NDEBUG |
| 853 | static bool pathHasTraversal(StringRef Path) { |
| 854 | using namespace llvm::sys; |
| 855 | for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path))) |
| 856 | if (Comp == "." || Comp == "..") |
| 857 | return true; |
| 858 | return false; |
| 859 | } |
| 860 | #endif |
| 861 | |
| 862 | void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) { |
| 863 | assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute"); |
| 864 | assert(sys::path::is_absolute(RealPath) && "real path not absolute"); |
| 865 | assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported"); |
| 866 | Mappings.emplace_back(VirtualPath, RealPath); |
| 867 | } |
| 868 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 869 | namespace { |
| 870 | class JSONWriter { |
| 871 | llvm::raw_ostream &OS; |
| 872 | SmallVector<StringRef, 16> DirStack; |
| 873 | inline unsigned getDirIndent() { return 4 * DirStack.size(); } |
| 874 | inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); } |
| 875 | bool containedIn(StringRef Parent, StringRef Path); |
| 876 | StringRef containedPart(StringRef Parent, StringRef Path); |
| 877 | void startDirectory(StringRef Path); |
| 878 | void endDirectory(); |
| 879 | void writeEntry(StringRef VPath, StringRef RPath); |
| 880 | |
| 881 | public: |
| 882 | JSONWriter(llvm::raw_ostream &OS) : OS(OS) {} |
| 883 | void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> IsCaseSensitive); |
| 884 | }; |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 887 | bool JSONWriter::containedIn(StringRef Parent, StringRef Path) { |
Justin Bogner | 1c078f2 | 2014-05-20 22:12:58 +0000 | [diff] [blame] | 888 | using namespace llvm::sys; |
| 889 | // Compare each path component. |
| 890 | auto IParent = path::begin(Parent), EParent = path::end(Parent); |
| 891 | for (auto IChild = path::begin(Path), EChild = path::end(Path); |
| 892 | IParent != EParent && IChild != EChild; ++IParent, ++IChild) { |
| 893 | if (*IParent != *IChild) |
| 894 | return false; |
| 895 | } |
| 896 | // Have we exhausted the parent path? |
| 897 | return IParent == EParent; |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 900 | StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) { |
| 901 | assert(!Parent.empty()); |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 902 | assert(containedIn(Parent, Path)); |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 903 | return Path.slice(Parent.size() + 1, StringRef::npos); |
| 904 | } |
| 905 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 906 | void JSONWriter::startDirectory(StringRef Path) { |
| 907 | StringRef Name = |
| 908 | DirStack.empty() ? Path : containedPart(DirStack.back(), Path); |
| 909 | DirStack.push_back(Path); |
| 910 | unsigned Indent = getDirIndent(); |
| 911 | OS.indent(Indent) << "{\n"; |
| 912 | OS.indent(Indent + 2) << "'type': 'directory',\n"; |
| 913 | OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n"; |
| 914 | OS.indent(Indent + 2) << "'contents': [\n"; |
| 915 | } |
| 916 | |
| 917 | void JSONWriter::endDirectory() { |
| 918 | unsigned Indent = getDirIndent(); |
| 919 | OS.indent(Indent + 2) << "]\n"; |
| 920 | OS.indent(Indent) << "}"; |
| 921 | |
| 922 | DirStack.pop_back(); |
| 923 | } |
| 924 | |
| 925 | void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) { |
| 926 | unsigned Indent = getFileIndent(); |
| 927 | OS.indent(Indent) << "{\n"; |
| 928 | OS.indent(Indent + 2) << "'type': 'file',\n"; |
| 929 | OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n"; |
| 930 | OS.indent(Indent + 2) << "'external-contents': \"" |
| 931 | << llvm::yaml::escape(RPath) << "\"\n"; |
| 932 | OS.indent(Indent) << "}"; |
| 933 | } |
| 934 | |
| 935 | void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries, |
| 936 | Optional<bool> IsCaseSensitive) { |
| 937 | using namespace llvm::sys; |
| 938 | |
| 939 | OS << "{\n" |
| 940 | " 'version': 0,\n"; |
| 941 | if (IsCaseSensitive.hasValue()) |
| 942 | OS << " 'case-sensitive': '" |
| 943 | << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n"; |
| 944 | OS << " 'roots': [\n"; |
| 945 | |
| 946 | if (Entries.empty()) |
| 947 | return; |
| 948 | |
| 949 | const YAMLVFSEntry &Entry = Entries.front(); |
| 950 | startDirectory(path::parent_path(Entry.VPath)); |
| 951 | writeEntry(path::filename(Entry.VPath), Entry.RPath); |
| 952 | |
| 953 | for (const auto &Entry : Entries.slice(1)) { |
| 954 | StringRef Dir = path::parent_path(Entry.VPath); |
| 955 | if (Dir == DirStack.back()) |
| 956 | OS << ",\n"; |
| 957 | else { |
| 958 | while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) { |
| 959 | OS << "\n"; |
| 960 | endDirectory(); |
| 961 | } |
| 962 | OS << ",\n"; |
| 963 | startDirectory(Dir); |
| 964 | } |
| 965 | writeEntry(path::filename(Entry.VPath), Entry.RPath); |
| 966 | } |
| 967 | |
| 968 | while (!DirStack.empty()) { |
| 969 | OS << "\n"; |
| 970 | endDirectory(); |
| 971 | } |
| 972 | |
| 973 | OS << "\n" |
| 974 | << " ]\n" |
| 975 | << "}\n"; |
| 976 | } |
| 977 | |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 978 | void YAMLVFSWriter::write(llvm::raw_ostream &OS) { |
| 979 | std::sort(Mappings.begin(), Mappings.end(), |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 980 | [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) { |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 981 | return LHS.VPath < RHS.VPath; |
| 982 | }); |
| 983 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 984 | JSONWriter(OS).write(Mappings, IsCaseSensitive); |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 985 | } |