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