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" |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 13 | #include "clang/Basic/FileManager.h" |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/DenseMap.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 | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/iterator_range.h" |
Bruno Cardoso Lopes | b2e2e21 | 2016-05-06 23:21:57 +0000 | [diff] [blame^] | 19 | #include "llvm/Support/Debug.h" |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Errc.h" |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
David Majnemer | 6a6206d | 2016-03-04 05:26:14 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Process.h" |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 24 | #include "llvm/Support/YAMLParser.h" |
Benjamin Kramer | d6bbee7 | 2015-10-05 14:06:36 +0000 | [diff] [blame] | 25 | #include "llvm/Config/llvm-config.h" |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 26 | #include <atomic> |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 27 | #include <memory> |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 28 | |
Benjamin Kramer | d6bbee7 | 2015-10-05 14:06:36 +0000 | [diff] [blame] | 29 | // For chdir. |
| 30 | #ifdef LLVM_ON_WIN32 |
| 31 | # include <direct.h> |
| 32 | #else |
| 33 | # include <unistd.h> |
| 34 | #endif |
| 35 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | using namespace clang::vfs; |
| 38 | using namespace llvm; |
| 39 | using llvm::sys::fs::file_status; |
| 40 | using llvm::sys::fs::file_type; |
| 41 | using llvm::sys::fs::perms; |
| 42 | using llvm::sys::fs::UniqueID; |
| 43 | |
| 44 | Status::Status(const file_status &Status) |
| 45 | : UID(Status.getUniqueID()), MTime(Status.getLastModificationTime()), |
| 46 | User(Status.getUser()), Group(Status.getGroup()), Size(Status.getSize()), |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 47 | Type(Status.type()), Perms(Status.permissions()), IsVFSMapped(false) {} |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 48 | |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 49 | Status::Status(StringRef Name, UniqueID UID, sys::TimeValue MTime, |
| 50 | uint32_t User, uint32_t Group, uint64_t Size, file_type Type, |
| 51 | perms Perms) |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 52 | : Name(Name), UID(UID), MTime(MTime), User(User), Group(Group), Size(Size), |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 53 | Type(Type), Perms(Perms), IsVFSMapped(false) {} |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 54 | |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 55 | Status Status::copyWithNewName(const Status &In, StringRef NewName) { |
| 56 | return Status(NewName, In.getUniqueID(), In.getLastModificationTime(), |
| 57 | In.getUser(), In.getGroup(), In.getSize(), In.getType(), |
| 58 | In.getPermissions()); |
| 59 | } |
| 60 | |
| 61 | Status Status::copyWithNewName(const file_status &In, StringRef NewName) { |
| 62 | return Status(NewName, In.getUniqueID(), In.getLastModificationTime(), |
| 63 | In.getUser(), In.getGroup(), In.getSize(), In.type(), |
| 64 | In.permissions()); |
| 65 | } |
| 66 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 67 | bool Status::equivalent(const Status &Other) const { |
| 68 | return getUniqueID() == Other.getUniqueID(); |
| 69 | } |
| 70 | bool Status::isDirectory() const { |
| 71 | return Type == file_type::directory_file; |
| 72 | } |
| 73 | bool Status::isRegularFile() const { |
| 74 | return Type == file_type::regular_file; |
| 75 | } |
| 76 | bool Status::isOther() const { |
| 77 | return exists() && !isRegularFile() && !isDirectory() && !isSymlink(); |
| 78 | } |
| 79 | bool Status::isSymlink() const { |
| 80 | return Type == file_type::symlink_file; |
| 81 | } |
| 82 | bool Status::isStatusKnown() const { |
| 83 | return Type != file_type::status_error; |
| 84 | } |
| 85 | bool Status::exists() const { |
| 86 | return isStatusKnown() && Type != file_type::file_not_found; |
| 87 | } |
| 88 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 89 | File::~File() {} |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 90 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 91 | FileSystem::~FileSystem() {} |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 92 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 93 | ErrorOr<std::unique_ptr<MemoryBuffer>> |
| 94 | FileSystem::getBufferForFile(const llvm::Twine &Name, int64_t FileSize, |
| 95 | bool RequiresNullTerminator, bool IsVolatile) { |
| 96 | auto F = openFileForRead(Name); |
| 97 | if (!F) |
| 98 | return F.getError(); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 99 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 100 | return (*F)->getBuffer(Name, FileSize, RequiresNullTerminator, IsVolatile); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 103 | std::error_code FileSystem::makeAbsolute(SmallVectorImpl<char> &Path) const { |
Bob Wilson | f43354f | 2016-03-26 18:55:13 +0000 | [diff] [blame] | 104 | if (llvm::sys::path::is_absolute(Path)) |
| 105 | return std::error_code(); |
| 106 | |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 107 | auto WorkingDir = getCurrentWorkingDirectory(); |
| 108 | if (!WorkingDir) |
| 109 | return WorkingDir.getError(); |
| 110 | |
| 111 | return llvm::sys::fs::make_absolute(WorkingDir.get(), Path); |
| 112 | } |
| 113 | |
Benjamin Kramer | d45b205 | 2015-10-07 15:48:01 +0000 | [diff] [blame] | 114 | bool FileSystem::exists(const Twine &Path) { |
| 115 | auto Status = status(Path); |
| 116 | return Status && Status->exists(); |
| 117 | } |
| 118 | |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 119 | #ifndef NDEBUG |
| 120 | static bool isTraversalComponent(StringRef Component) { |
| 121 | return Component.equals("..") || Component.equals("."); |
| 122 | } |
| 123 | |
| 124 | static bool pathHasTraversal(StringRef Path) { |
| 125 | using namespace llvm::sys; |
| 126 | for (StringRef Comp : llvm::make_range(path::begin(Path), path::end(Path))) |
| 127 | if (isTraversalComponent(Comp)) |
| 128 | return true; |
| 129 | return false; |
| 130 | } |
| 131 | #endif |
| 132 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 133 | //===-----------------------------------------------------------------------===/ |
| 134 | // RealFileSystem implementation |
| 135 | //===-----------------------------------------------------------------------===/ |
| 136 | |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 137 | namespace { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 138 | /// \brief Wrapper around a raw file descriptor. |
| 139 | class RealFile : public File { |
| 140 | int FD; |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 141 | Status S; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 142 | friend class RealFileSystem; |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 143 | RealFile(int FD, StringRef NewName) |
| 144 | : FD(FD), S(NewName, {}, {}, {}, {}, {}, |
| 145 | llvm::sys::fs::file_type::status_error, {}) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 146 | assert(FD >= 0 && "Invalid or inactive file descriptor"); |
| 147 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 148 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 149 | public: |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 150 | ~RealFile() override; |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 151 | ErrorOr<Status> status() override; |
Benjamin Kramer | 737501c | 2015-10-05 21:20:19 +0000 | [diff] [blame] | 152 | ErrorOr<std::unique_ptr<MemoryBuffer>> getBuffer(const Twine &Name, |
| 153 | int64_t FileSize, |
| 154 | bool RequiresNullTerminator, |
| 155 | bool IsVolatile) override; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 156 | std::error_code close() override; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 157 | }; |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 158 | } // end anonymous namespace |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 159 | RealFile::~RealFile() { close(); } |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 160 | |
| 161 | ErrorOr<Status> RealFile::status() { |
| 162 | assert(FD != -1 && "cannot stat closed file"); |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 163 | if (!S.isStatusKnown()) { |
| 164 | file_status RealStatus; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 165 | if (std::error_code EC = sys::fs::status(FD, RealStatus)) |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 166 | return EC; |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 167 | S = Status::copyWithNewName(RealStatus, S.getName()); |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 168 | } |
| 169 | return S; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 172 | ErrorOr<std::unique_ptr<MemoryBuffer>> |
| 173 | RealFile::getBuffer(const Twine &Name, int64_t FileSize, |
| 174 | bool RequiresNullTerminator, bool IsVolatile) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 175 | assert(FD != -1 && "cannot get buffer for closed file"); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 176 | return MemoryBuffer::getOpenFile(FD, Name, FileSize, RequiresNullTerminator, |
| 177 | IsVolatile); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 180 | std::error_code RealFile::close() { |
David Majnemer | 6a6206d | 2016-03-04 05:26:14 +0000 | [diff] [blame] | 181 | std::error_code EC = sys::Process::SafelyCloseFileDescriptor(FD); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 182 | FD = -1; |
David Majnemer | 6a6206d | 2016-03-04 05:26:14 +0000 | [diff] [blame] | 183 | return EC; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 186 | namespace { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 187 | /// \brief The file system according to your operating system. |
| 188 | class RealFileSystem : public FileSystem { |
| 189 | public: |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 190 | ErrorOr<Status> status(const Twine &Path) override; |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 191 | ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override; |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 192 | directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override; |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 193 | |
| 194 | llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override; |
| 195 | std::error_code setCurrentWorkingDirectory(const Twine &Path) override; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 196 | }; |
Benjamin Kramer | 3d6220d | 2014-03-01 17:21:22 +0000 | [diff] [blame] | 197 | } // end anonymous namespace |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 198 | |
| 199 | ErrorOr<Status> RealFileSystem::status(const Twine &Path) { |
| 200 | sys::fs::file_status RealStatus; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 201 | if (std::error_code EC = sys::fs::status(Path, RealStatus)) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 202 | return EC; |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 203 | return Status::copyWithNewName(RealStatus, Path.str()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 206 | ErrorOr<std::unique_ptr<File>> |
| 207 | RealFileSystem::openFileForRead(const Twine &Name) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 208 | int FD; |
Rafael Espindola | 8e650d7 | 2014-06-12 20:37:59 +0000 | [diff] [blame] | 209 | if (std::error_code EC = sys::fs::openFileForRead(Name, FD)) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 210 | return EC; |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 211 | return std::unique_ptr<File>(new RealFile(FD, Name.str())); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 214 | llvm::ErrorOr<std::string> RealFileSystem::getCurrentWorkingDirectory() const { |
| 215 | SmallString<256> Dir; |
| 216 | if (std::error_code EC = llvm::sys::fs::current_path(Dir)) |
| 217 | return EC; |
| 218 | return Dir.str().str(); |
| 219 | } |
| 220 | |
| 221 | std::error_code RealFileSystem::setCurrentWorkingDirectory(const Twine &Path) { |
| 222 | // FIXME: chdir is thread hostile; on the other hand, creating the same |
| 223 | // behavior as chdir is complex: chdir resolves the path once, thus |
| 224 | // guaranteeing that all subsequent relative path operations work |
| 225 | // on the same path the original chdir resulted in. This makes a |
| 226 | // difference for example on network filesystems, where symlinks might be |
| 227 | // switched during runtime of the tool. Fixing this depends on having a |
| 228 | // file system abstraction that allows openat() style interactions. |
| 229 | SmallString<256> Storage; |
| 230 | StringRef Dir = Path.toNullTerminatedStringRef(Storage); |
| 231 | if (int Err = ::chdir(Dir.data())) |
| 232 | return std::error_code(Err, std::generic_category()); |
| 233 | return std::error_code(); |
| 234 | } |
| 235 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 236 | IntrusiveRefCntPtr<FileSystem> vfs::getRealFileSystem() { |
| 237 | static IntrusiveRefCntPtr<FileSystem> FS = new RealFileSystem(); |
| 238 | return FS; |
| 239 | } |
| 240 | |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 241 | namespace { |
| 242 | class RealFSDirIter : public clang::vfs::detail::DirIterImpl { |
| 243 | std::string Path; |
| 244 | llvm::sys::fs::directory_iterator Iter; |
| 245 | public: |
| 246 | RealFSDirIter(const Twine &_Path, std::error_code &EC) |
| 247 | : Path(_Path.str()), Iter(Path, EC) { |
| 248 | if (!EC && Iter != llvm::sys::fs::directory_iterator()) { |
| 249 | llvm::sys::fs::file_status S; |
| 250 | EC = Iter->status(S); |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 251 | if (!EC) |
| 252 | CurrentEntry = Status::copyWithNewName(S, Iter->path()); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | std::error_code increment() override { |
| 257 | std::error_code EC; |
| 258 | Iter.increment(EC); |
| 259 | if (EC) { |
| 260 | return EC; |
| 261 | } else if (Iter == llvm::sys::fs::directory_iterator()) { |
| 262 | CurrentEntry = Status(); |
| 263 | } else { |
| 264 | llvm::sys::fs::file_status S; |
| 265 | EC = Iter->status(S); |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 266 | CurrentEntry = Status::copyWithNewName(S, Iter->path()); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 267 | } |
| 268 | return EC; |
| 269 | } |
| 270 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 271 | } |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 272 | |
| 273 | directory_iterator RealFileSystem::dir_begin(const Twine &Dir, |
| 274 | std::error_code &EC) { |
| 275 | return directory_iterator(std::make_shared<RealFSDirIter>(Dir, EC)); |
| 276 | } |
| 277 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 278 | //===-----------------------------------------------------------------------===/ |
| 279 | // OverlayFileSystem implementation |
| 280 | //===-----------------------------------------------------------------------===/ |
| 281 | OverlayFileSystem::OverlayFileSystem(IntrusiveRefCntPtr<FileSystem> BaseFS) { |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 282 | FSList.push_back(BaseFS); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void OverlayFileSystem::pushOverlay(IntrusiveRefCntPtr<FileSystem> FS) { |
| 286 | FSList.push_back(FS); |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 287 | // Synchronize added file systems by duplicating the working directory from |
| 288 | // the first one in the list. |
| 289 | FS->setCurrentWorkingDirectory(getCurrentWorkingDirectory().get()); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | ErrorOr<Status> OverlayFileSystem::status(const Twine &Path) { |
| 293 | // FIXME: handle symlinks that cross file systems |
| 294 | for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { |
| 295 | ErrorOr<Status> Status = (*I)->status(Path); |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 296 | if (Status || Status.getError() != llvm::errc::no_such_file_or_directory) |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 297 | return Status; |
| 298 | } |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 299 | return make_error_code(llvm::errc::no_such_file_or_directory); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 302 | ErrorOr<std::unique_ptr<File>> |
| 303 | OverlayFileSystem::openFileForRead(const llvm::Twine &Path) { |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 304 | // FIXME: handle symlinks that cross file systems |
| 305 | for (iterator I = overlays_begin(), E = overlays_end(); I != E; ++I) { |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 306 | auto Result = (*I)->openFileForRead(Path); |
| 307 | if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) |
| 308 | return Result; |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 309 | } |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 310 | return make_error_code(llvm::errc::no_such_file_or_directory); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 311 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 312 | |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 313 | llvm::ErrorOr<std::string> |
| 314 | OverlayFileSystem::getCurrentWorkingDirectory() const { |
| 315 | // All file systems are synchronized, just take the first working directory. |
| 316 | return FSList.front()->getCurrentWorkingDirectory(); |
| 317 | } |
| 318 | std::error_code |
| 319 | OverlayFileSystem::setCurrentWorkingDirectory(const Twine &Path) { |
| 320 | for (auto &FS : FSList) |
| 321 | if (std::error_code EC = FS->setCurrentWorkingDirectory(Path)) |
| 322 | return EC; |
| 323 | return std::error_code(); |
| 324 | } |
| 325 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 326 | clang::vfs::detail::DirIterImpl::~DirIterImpl() { } |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 327 | |
| 328 | namespace { |
| 329 | class OverlayFSDirIterImpl : public clang::vfs::detail::DirIterImpl { |
| 330 | OverlayFileSystem &Overlays; |
| 331 | std::string Path; |
| 332 | OverlayFileSystem::iterator CurrentFS; |
| 333 | directory_iterator CurrentDirIter; |
| 334 | llvm::StringSet<> SeenNames; |
| 335 | |
| 336 | std::error_code incrementFS() { |
| 337 | assert(CurrentFS != Overlays.overlays_end() && "incrementing past end"); |
| 338 | ++CurrentFS; |
| 339 | for (auto E = Overlays.overlays_end(); CurrentFS != E; ++CurrentFS) { |
| 340 | std::error_code EC; |
| 341 | CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC); |
| 342 | if (EC && EC != errc::no_such_file_or_directory) |
| 343 | return EC; |
| 344 | if (CurrentDirIter != directory_iterator()) |
| 345 | break; // found |
| 346 | } |
| 347 | return std::error_code(); |
| 348 | } |
| 349 | |
| 350 | std::error_code incrementDirIter(bool IsFirstTime) { |
| 351 | assert((IsFirstTime || CurrentDirIter != directory_iterator()) && |
| 352 | "incrementing past end"); |
| 353 | std::error_code EC; |
| 354 | if (!IsFirstTime) |
| 355 | CurrentDirIter.increment(EC); |
| 356 | if (!EC && CurrentDirIter == directory_iterator()) |
| 357 | EC = incrementFS(); |
| 358 | return EC; |
| 359 | } |
| 360 | |
| 361 | std::error_code incrementImpl(bool IsFirstTime) { |
| 362 | while (true) { |
| 363 | std::error_code EC = incrementDirIter(IsFirstTime); |
| 364 | if (EC || CurrentDirIter == directory_iterator()) { |
| 365 | CurrentEntry = Status(); |
| 366 | return EC; |
| 367 | } |
| 368 | CurrentEntry = *CurrentDirIter; |
| 369 | StringRef Name = llvm::sys::path::filename(CurrentEntry.getName()); |
David Blaikie | 61b86d4 | 2014-11-19 02:56:13 +0000 | [diff] [blame] | 370 | if (SeenNames.insert(Name).second) |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 371 | return EC; // name not seen before |
| 372 | } |
| 373 | llvm_unreachable("returned above"); |
| 374 | } |
| 375 | |
| 376 | public: |
| 377 | OverlayFSDirIterImpl(const Twine &Path, OverlayFileSystem &FS, |
| 378 | std::error_code &EC) |
| 379 | : Overlays(FS), Path(Path.str()), CurrentFS(Overlays.overlays_begin()) { |
| 380 | CurrentDirIter = (*CurrentFS)->dir_begin(Path, EC); |
| 381 | EC = incrementImpl(true); |
| 382 | } |
| 383 | |
| 384 | std::error_code increment() override { return incrementImpl(false); } |
| 385 | }; |
| 386 | } // end anonymous namespace |
| 387 | |
| 388 | directory_iterator OverlayFileSystem::dir_begin(const Twine &Dir, |
| 389 | std::error_code &EC) { |
| 390 | return directory_iterator( |
| 391 | std::make_shared<OverlayFSDirIterImpl>(Dir, *this, EC)); |
| 392 | } |
| 393 | |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 394 | namespace clang { |
| 395 | namespace vfs { |
| 396 | namespace detail { |
| 397 | |
| 398 | enum InMemoryNodeKind { IME_File, IME_Directory }; |
| 399 | |
| 400 | /// The in memory file system is a tree of Nodes. Every node can either be a |
| 401 | /// file or a directory. |
| 402 | class InMemoryNode { |
| 403 | Status Stat; |
| 404 | InMemoryNodeKind Kind; |
| 405 | |
| 406 | public: |
| 407 | InMemoryNode(Status Stat, InMemoryNodeKind Kind) |
| 408 | : Stat(std::move(Stat)), Kind(Kind) {} |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 409 | virtual ~InMemoryNode() {} |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 410 | const Status &getStatus() const { return Stat; } |
| 411 | InMemoryNodeKind getKind() const { return Kind; } |
| 412 | virtual std::string toString(unsigned Indent) const = 0; |
| 413 | }; |
| 414 | |
| 415 | namespace { |
| 416 | class InMemoryFile : public InMemoryNode { |
| 417 | std::unique_ptr<llvm::MemoryBuffer> Buffer; |
| 418 | |
| 419 | public: |
| 420 | InMemoryFile(Status Stat, std::unique_ptr<llvm::MemoryBuffer> Buffer) |
| 421 | : InMemoryNode(std::move(Stat), IME_File), Buffer(std::move(Buffer)) {} |
| 422 | |
| 423 | llvm::MemoryBuffer *getBuffer() { return Buffer.get(); } |
| 424 | std::string toString(unsigned Indent) const override { |
| 425 | return (std::string(Indent, ' ') + getStatus().getName() + "\n").str(); |
| 426 | } |
| 427 | static bool classof(const InMemoryNode *N) { |
| 428 | return N->getKind() == IME_File; |
| 429 | } |
| 430 | }; |
| 431 | |
| 432 | /// Adapt a InMemoryFile for VFS' File interface. |
| 433 | class InMemoryFileAdaptor : public File { |
| 434 | InMemoryFile &Node; |
| 435 | |
| 436 | public: |
| 437 | explicit InMemoryFileAdaptor(InMemoryFile &Node) : Node(Node) {} |
| 438 | |
| 439 | llvm::ErrorOr<Status> status() override { return Node.getStatus(); } |
| 440 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
Benjamin Kramer | 737501c | 2015-10-05 21:20:19 +0000 | [diff] [blame] | 441 | getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator, |
| 442 | bool IsVolatile) override { |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 443 | llvm::MemoryBuffer *Buf = Node.getBuffer(); |
| 444 | return llvm::MemoryBuffer::getMemBuffer( |
| 445 | Buf->getBuffer(), Buf->getBufferIdentifier(), RequiresNullTerminator); |
| 446 | } |
| 447 | std::error_code close() override { return std::error_code(); } |
| 448 | }; |
| 449 | } // end anonymous namespace |
| 450 | |
| 451 | class InMemoryDirectory : public InMemoryNode { |
| 452 | std::map<std::string, std::unique_ptr<InMemoryNode>> Entries; |
| 453 | |
| 454 | public: |
| 455 | InMemoryDirectory(Status Stat) |
| 456 | : InMemoryNode(std::move(Stat), IME_Directory) {} |
| 457 | InMemoryNode *getChild(StringRef Name) { |
| 458 | auto I = Entries.find(Name); |
| 459 | if (I != Entries.end()) |
| 460 | return I->second.get(); |
| 461 | return nullptr; |
| 462 | } |
| 463 | InMemoryNode *addChild(StringRef Name, std::unique_ptr<InMemoryNode> Child) { |
| 464 | return Entries.insert(make_pair(Name, std::move(Child))) |
| 465 | .first->second.get(); |
| 466 | } |
| 467 | |
| 468 | typedef decltype(Entries)::const_iterator const_iterator; |
| 469 | const_iterator begin() const { return Entries.begin(); } |
| 470 | const_iterator end() const { return Entries.end(); } |
| 471 | |
| 472 | std::string toString(unsigned Indent) const override { |
| 473 | std::string Result = |
| 474 | (std::string(Indent, ' ') + getStatus().getName() + "\n").str(); |
| 475 | for (const auto &Entry : Entries) { |
| 476 | Result += Entry.second->toString(Indent + 2); |
| 477 | } |
| 478 | return Result; |
| 479 | } |
| 480 | static bool classof(const InMemoryNode *N) { |
| 481 | return N->getKind() == IME_Directory; |
| 482 | } |
| 483 | }; |
| 484 | } |
| 485 | |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 486 | InMemoryFileSystem::InMemoryFileSystem(bool UseNormalizedPaths) |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 487 | : Root(new detail::InMemoryDirectory( |
| 488 | Status("", getNextVirtualUniqueID(), llvm::sys::TimeValue::MinTime(), |
| 489 | 0, 0, 0, llvm::sys::fs::file_type::directory_file, |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 490 | llvm::sys::fs::perms::all_all))), |
| 491 | UseNormalizedPaths(UseNormalizedPaths) {} |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 492 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 493 | InMemoryFileSystem::~InMemoryFileSystem() {} |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 494 | |
Benjamin Kramer | decb2ae | 2015-10-09 13:03:22 +0000 | [diff] [blame] | 495 | std::string InMemoryFileSystem::toString() const { |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 496 | return Root->toString(/*Indent=*/0); |
| 497 | } |
| 498 | |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 499 | bool InMemoryFileSystem::addFile(const Twine &P, time_t ModificationTime, |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 500 | std::unique_ptr<llvm::MemoryBuffer> Buffer) { |
| 501 | SmallString<128> Path; |
| 502 | P.toVector(Path); |
| 503 | |
| 504 | // Fix up relative paths. This just prepends the current working directory. |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 505 | std::error_code EC = makeAbsolute(Path); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 506 | assert(!EC); |
| 507 | (void)EC; |
| 508 | |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 509 | if (useNormalizedPaths()) |
Mike Aizatsky | aeb9dd9 | 2015-11-09 19:12:18 +0000 | [diff] [blame] | 510 | llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true); |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 511 | |
| 512 | if (Path.empty()) |
| 513 | return false; |
| 514 | |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 515 | detail::InMemoryDirectory *Dir = Root.get(); |
| 516 | auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path); |
| 517 | while (true) { |
| 518 | StringRef Name = *I; |
| 519 | detail::InMemoryNode *Node = Dir->getChild(Name); |
| 520 | ++I; |
| 521 | if (!Node) { |
| 522 | if (I == E) { |
| 523 | // End of the path, create a new file. |
| 524 | // FIXME: expose the status details in the interface. |
Benjamin Kramer | 1b8dbe3 | 2015-10-06 14:45:16 +0000 | [diff] [blame] | 525 | Status Stat(P.str(), getNextVirtualUniqueID(), |
Benjamin Kramer | 6b61805 | 2015-10-05 14:02:15 +0000 | [diff] [blame] | 526 | llvm::sys::TimeValue(ModificationTime, 0), 0, 0, |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 527 | Buffer->getBufferSize(), |
| 528 | llvm::sys::fs::file_type::regular_file, |
| 529 | llvm::sys::fs::all_all); |
| 530 | Dir->addChild(Name, llvm::make_unique<detail::InMemoryFile>( |
| 531 | std::move(Stat), std::move(Buffer))); |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 532 | return true; |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // Create a new directory. Use the path up to here. |
| 536 | // FIXME: expose the status details in the interface. |
| 537 | Status Stat( |
| 538 | StringRef(Path.str().begin(), Name.end() - Path.str().begin()), |
Benjamin Kramer | 6b61805 | 2015-10-05 14:02:15 +0000 | [diff] [blame] | 539 | getNextVirtualUniqueID(), llvm::sys::TimeValue(ModificationTime, 0), |
| 540 | 0, 0, Buffer->getBufferSize(), |
| 541 | llvm::sys::fs::file_type::directory_file, llvm::sys::fs::all_all); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 542 | Dir = cast<detail::InMemoryDirectory>(Dir->addChild( |
| 543 | Name, llvm::make_unique<detail::InMemoryDirectory>(std::move(Stat)))); |
| 544 | continue; |
| 545 | } |
| 546 | |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 547 | if (auto *NewDir = dyn_cast<detail::InMemoryDirectory>(Node)) { |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 548 | Dir = NewDir; |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 549 | } else { |
| 550 | assert(isa<detail::InMemoryFile>(Node) && |
| 551 | "Must be either file or directory!"); |
| 552 | |
| 553 | // Trying to insert a directory in place of a file. |
| 554 | if (I != E) |
| 555 | return false; |
| 556 | |
| 557 | // Return false only if the new file is different from the existing one. |
| 558 | return cast<detail::InMemoryFile>(Node)->getBuffer()->getBuffer() == |
| 559 | Buffer->getBuffer(); |
| 560 | } |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 564 | bool InMemoryFileSystem::addFileNoOwn(const Twine &P, time_t ModificationTime, |
Benjamin Kramer | 2e2351a | 2015-10-06 10:04:08 +0000 | [diff] [blame] | 565 | llvm::MemoryBuffer *Buffer) { |
| 566 | return addFile(P, ModificationTime, |
| 567 | llvm::MemoryBuffer::getMemBuffer( |
| 568 | Buffer->getBuffer(), Buffer->getBufferIdentifier())); |
| 569 | } |
| 570 | |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 571 | static ErrorOr<detail::InMemoryNode *> |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 572 | lookupInMemoryNode(const InMemoryFileSystem &FS, detail::InMemoryDirectory *Dir, |
| 573 | const Twine &P) { |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 574 | SmallString<128> Path; |
| 575 | P.toVector(Path); |
| 576 | |
| 577 | // Fix up relative paths. This just prepends the current working directory. |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 578 | std::error_code EC = FS.makeAbsolute(Path); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 579 | assert(!EC); |
| 580 | (void)EC; |
| 581 | |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 582 | if (FS.useNormalizedPaths()) |
Mike Aizatsky | aeb9dd9 | 2015-11-09 19:12:18 +0000 | [diff] [blame] | 583 | llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true); |
Benjamin Kramer | 4ad1c43 | 2015-10-12 13:30:38 +0000 | [diff] [blame] | 584 | |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 585 | if (Path.empty()) |
Benjamin Kramer | decb2ae | 2015-10-09 13:03:22 +0000 | [diff] [blame] | 586 | return Dir; |
| 587 | |
Benjamin Kramer | 71ce376 | 2015-10-12 16:16:39 +0000 | [diff] [blame] | 588 | auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 589 | while (true) { |
| 590 | detail::InMemoryNode *Node = Dir->getChild(*I); |
| 591 | ++I; |
| 592 | if (!Node) |
| 593 | return errc::no_such_file_or_directory; |
| 594 | |
| 595 | // Return the file if it's at the end of the path. |
| 596 | if (auto File = dyn_cast<detail::InMemoryFile>(Node)) { |
| 597 | if (I == E) |
| 598 | return File; |
| 599 | return errc::no_such_file_or_directory; |
| 600 | } |
| 601 | |
| 602 | // Traverse directories. |
| 603 | Dir = cast<detail::InMemoryDirectory>(Node); |
| 604 | if (I == E) |
| 605 | return Dir; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | llvm::ErrorOr<Status> InMemoryFileSystem::status(const Twine &Path) { |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 610 | auto Node = lookupInMemoryNode(*this, Root.get(), Path); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 611 | if (Node) |
| 612 | return (*Node)->getStatus(); |
| 613 | return Node.getError(); |
| 614 | } |
| 615 | |
| 616 | llvm::ErrorOr<std::unique_ptr<File>> |
| 617 | InMemoryFileSystem::openFileForRead(const Twine &Path) { |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 618 | auto Node = lookupInMemoryNode(*this, Root.get(), Path); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 619 | if (!Node) |
| 620 | return Node.getError(); |
| 621 | |
| 622 | // When we have a file provide a heap-allocated wrapper for the memory buffer |
| 623 | // to match the ownership semantics for File. |
| 624 | if (auto *F = dyn_cast<detail::InMemoryFile>(*Node)) |
| 625 | return std::unique_ptr<File>(new detail::InMemoryFileAdaptor(*F)); |
| 626 | |
| 627 | // FIXME: errc::not_a_file? |
| 628 | return make_error_code(llvm::errc::invalid_argument); |
| 629 | } |
| 630 | |
| 631 | namespace { |
| 632 | /// Adaptor from InMemoryDir::iterator to directory_iterator. |
| 633 | class InMemoryDirIterator : public clang::vfs::detail::DirIterImpl { |
| 634 | detail::InMemoryDirectory::const_iterator I; |
| 635 | detail::InMemoryDirectory::const_iterator E; |
| 636 | |
| 637 | public: |
| 638 | InMemoryDirIterator() {} |
| 639 | explicit InMemoryDirIterator(detail::InMemoryDirectory &Dir) |
| 640 | : I(Dir.begin()), E(Dir.end()) { |
| 641 | if (I != E) |
| 642 | CurrentEntry = I->second->getStatus(); |
| 643 | } |
| 644 | |
| 645 | std::error_code increment() override { |
| 646 | ++I; |
| 647 | // When we're at the end, make CurrentEntry invalid and DirIterImpl will do |
| 648 | // the rest. |
| 649 | CurrentEntry = I != E ? I->second->getStatus() : Status(); |
| 650 | return std::error_code(); |
| 651 | } |
| 652 | }; |
| 653 | } // end anonymous namespace |
| 654 | |
| 655 | directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir, |
| 656 | std::error_code &EC) { |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 657 | auto Node = lookupInMemoryNode(*this, Root.get(), Dir); |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 658 | if (!Node) { |
| 659 | EC = Node.getError(); |
| 660 | return directory_iterator(std::make_shared<InMemoryDirIterator>()); |
| 661 | } |
| 662 | |
| 663 | if (auto *DirNode = dyn_cast<detail::InMemoryDirectory>(*Node)) |
| 664 | return directory_iterator(std::make_shared<InMemoryDirIterator>(*DirNode)); |
| 665 | |
| 666 | EC = make_error_code(llvm::errc::not_a_directory); |
| 667 | return directory_iterator(std::make_shared<InMemoryDirIterator>()); |
| 668 | } |
Benjamin Kramer | e9e7607 | 2016-01-09 16:33:16 +0000 | [diff] [blame] | 669 | |
| 670 | std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) { |
| 671 | SmallString<128> Path; |
| 672 | P.toVector(Path); |
| 673 | |
| 674 | // Fix up relative paths. This just prepends the current working directory. |
| 675 | std::error_code EC = makeAbsolute(Path); |
| 676 | assert(!EC); |
| 677 | (void)EC; |
| 678 | |
| 679 | if (useNormalizedPaths()) |
| 680 | llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true); |
| 681 | |
| 682 | if (!Path.empty()) |
| 683 | WorkingDirectory = Path.str(); |
| 684 | return std::error_code(); |
| 685 | } |
Benjamin Kramer | a25dcfd | 2015-10-05 13:55:14 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 689 | //===-----------------------------------------------------------------------===/ |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 690 | // RedirectingFileSystem implementation |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 691 | //===-----------------------------------------------------------------------===/ |
| 692 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 693 | namespace { |
| 694 | |
| 695 | enum EntryKind { |
| 696 | EK_Directory, |
| 697 | EK_File |
| 698 | }; |
| 699 | |
| 700 | /// \brief A single file or directory in the VFS. |
| 701 | class Entry { |
| 702 | EntryKind Kind; |
| 703 | std::string Name; |
| 704 | |
| 705 | public: |
| 706 | virtual ~Entry(); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 707 | Entry(EntryKind K, StringRef Name) : Kind(K), Name(Name) {} |
| 708 | StringRef getName() const { return Name; } |
| 709 | EntryKind getKind() const { return Kind; } |
| 710 | }; |
| 711 | |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 712 | class RedirectingDirectoryEntry : public Entry { |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 713 | std::vector<std::unique_ptr<Entry>> Contents; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 714 | Status S; |
| 715 | |
| 716 | public: |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 717 | RedirectingDirectoryEntry(StringRef Name, |
| 718 | std::vector<std::unique_ptr<Entry>> Contents, |
| 719 | Status S) |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 720 | : Entry(EK_Directory, Name), Contents(std::move(Contents)), |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 721 | S(std::move(S)) {} |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 722 | Status getStatus() { return S; } |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 723 | typedef decltype(Contents)::iterator iterator; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 724 | iterator contents_begin() { return Contents.begin(); } |
| 725 | iterator contents_end() { return Contents.end(); } |
| 726 | static bool classof(const Entry *E) { return E->getKind() == EK_Directory; } |
| 727 | }; |
| 728 | |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 729 | class RedirectingFileEntry : public Entry { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 730 | public: |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 731 | enum NameKind { |
| 732 | NK_NotSet, |
| 733 | NK_External, |
| 734 | NK_Virtual |
| 735 | }; |
| 736 | private: |
| 737 | std::string ExternalContentsPath; |
| 738 | NameKind UseName; |
| 739 | public: |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 740 | RedirectingFileEntry(StringRef Name, StringRef ExternalContentsPath, |
| 741 | NameKind UseName) |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 742 | : Entry(EK_File, Name), ExternalContentsPath(ExternalContentsPath), |
| 743 | UseName(UseName) {} |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 744 | StringRef getExternalContentsPath() const { return ExternalContentsPath; } |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 745 | /// \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] | 746 | bool useExternalName(bool GlobalUseExternalName) const { |
| 747 | return UseName == NK_NotSet ? GlobalUseExternalName |
| 748 | : (UseName == NK_External); |
| 749 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 750 | static bool classof(const Entry *E) { return E->getKind() == EK_File; } |
| 751 | }; |
| 752 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 753 | class RedirectingFileSystem; |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 754 | |
| 755 | class VFSFromYamlDirIterImpl : public clang::vfs::detail::DirIterImpl { |
| 756 | std::string Dir; |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 757 | RedirectingFileSystem &FS; |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 758 | RedirectingDirectoryEntry::iterator Current, End; |
| 759 | |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 760 | public: |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 761 | VFSFromYamlDirIterImpl(const Twine &Path, RedirectingFileSystem &FS, |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 762 | RedirectingDirectoryEntry::iterator Begin, |
| 763 | RedirectingDirectoryEntry::iterator End, |
| 764 | std::error_code &EC); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 765 | std::error_code increment() override; |
| 766 | }; |
| 767 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 768 | /// \brief A virtual file system parsed from a YAML file. |
| 769 | /// |
| 770 | /// Currently, this class allows creating virtual directories and mapping |
| 771 | /// virtual file paths to existing external files, available in \c ExternalFS. |
| 772 | /// |
| 773 | /// The basic structure of the parsed file is: |
| 774 | /// \verbatim |
| 775 | /// { |
| 776 | /// 'version': <version number>, |
| 777 | /// <optional configuration> |
| 778 | /// 'roots': [ |
| 779 | /// <directory entries> |
| 780 | /// ] |
| 781 | /// } |
| 782 | /// \endverbatim |
| 783 | /// |
| 784 | /// All configuration options are optional. |
| 785 | /// 'case-sensitive': <boolean, default=true> |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 786 | /// 'use-external-names': <boolean, default=true> |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 787 | /// 'overlay-relative': <boolean, default=false> |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 788 | /// |
| 789 | /// Virtual directories are represented as |
| 790 | /// \verbatim |
| 791 | /// { |
| 792 | /// 'type': 'directory', |
| 793 | /// 'name': <string>, |
| 794 | /// 'contents': [ <file or directory entries> ] |
| 795 | /// } |
| 796 | /// \endverbatim |
| 797 | /// |
| 798 | /// The default attributes for virtual directories are: |
| 799 | /// \verbatim |
| 800 | /// MTime = now() when created |
| 801 | /// Perms = 0777 |
| 802 | /// User = Group = 0 |
| 803 | /// Size = 0 |
| 804 | /// UniqueID = unspecified unique value |
| 805 | /// \endverbatim |
| 806 | /// |
| 807 | /// Re-mapped files are represented as |
| 808 | /// \verbatim |
| 809 | /// { |
| 810 | /// 'type': 'file', |
| 811 | /// 'name': <string>, |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 812 | /// 'use-external-name': <boolean> # Optional |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 813 | /// 'external-contents': <path to external file>) |
| 814 | /// } |
| 815 | /// \endverbatim |
| 816 | /// |
| 817 | /// and inherit their attributes from the external contents. |
| 818 | /// |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 819 | /// In both cases, the 'name' field may contain multiple path components (e.g. |
| 820 | /// /path/to/file). However, any directory that contains more than one child |
| 821 | /// must be uniquely represented by a directory entry. |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 822 | class RedirectingFileSystem : public vfs::FileSystem { |
| 823 | /// The root(s) of the virtual file system. |
| 824 | std::vector<std::unique_ptr<Entry>> Roots; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 825 | /// \brief The file system to use for external references. |
| 826 | IntrusiveRefCntPtr<FileSystem> ExternalFS; |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 827 | /// If IsRelativeOverlay is set, this represents the directory |
| 828 | /// path that should be prefixed to each 'external-contents' entry |
| 829 | /// when reading from YAML files. |
| 830 | std::string ExternalContentsPrefixDir; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 831 | |
| 832 | /// @name Configuration |
| 833 | /// @{ |
| 834 | |
| 835 | /// \brief Whether to perform case-sensitive comparisons. |
| 836 | /// |
| 837 | /// Currently, case-insensitive matching only works correctly with ASCII. |
Bruno Cardoso Lopes | f6f1def | 2016-04-13 19:28:16 +0000 | [diff] [blame] | 838 | bool CaseSensitive = true; |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 839 | |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 840 | /// IsRelativeOverlay marks whether a IsExternalContentsPrefixDir path must |
| 841 | /// be prefixed in every 'external-contents' when reading from YAML files. |
| 842 | bool IsRelativeOverlay = false; |
| 843 | |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 844 | /// \brief Whether to use to use the value of 'external-contents' for the |
| 845 | /// names of files. This global value is overridable on a per-file basis. |
Bruno Cardoso Lopes | f6f1def | 2016-04-13 19:28:16 +0000 | [diff] [blame] | 846 | bool UseExternalNames = true; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 847 | /// @} |
| 848 | |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 849 | /// Virtual file paths and external files could be canonicalized without "..", |
| 850 | /// "." and "./" in their paths. FIXME: some unittests currently fail on |
| 851 | /// win32 when using remove_dots and remove_leading_dotslash on paths. |
| 852 | bool UseCanonicalizedPaths = |
| 853 | #ifdef LLVM_ON_WIN32 |
| 854 | false; |
| 855 | #else |
| 856 | true; |
| 857 | #endif |
| 858 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 859 | friend class RedirectingFileSystemParser; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 860 | |
| 861 | private: |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 862 | RedirectingFileSystem(IntrusiveRefCntPtr<FileSystem> ExternalFS) |
Bruno Cardoso Lopes | f6f1def | 2016-04-13 19:28:16 +0000 | [diff] [blame] | 863 | : ExternalFS(ExternalFS) {} |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 864 | |
| 865 | /// \brief Looks up \p Path in \c Roots. |
| 866 | ErrorOr<Entry *> lookupPath(const Twine &Path); |
| 867 | |
| 868 | /// \brief Looks up the path <tt>[Start, End)</tt> in \p From, possibly |
| 869 | /// recursing into the contents of \p From if it is a directory. |
| 870 | ErrorOr<Entry *> lookupPath(sys::path::const_iterator Start, |
| 871 | sys::path::const_iterator End, Entry *From); |
| 872 | |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 873 | /// \brief Get the status of a given an \c Entry. |
| 874 | ErrorOr<Status> status(const Twine &Path, Entry *E); |
| 875 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 876 | public: |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 877 | /// \brief Parses \p Buffer, which is expected to be in YAML format and |
| 878 | /// returns a virtual file system representing its contents. |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 879 | static RedirectingFileSystem * |
| 880 | create(std::unique_ptr<MemoryBuffer> Buffer, |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 881 | SourceMgr::DiagHandlerTy DiagHandler, StringRef YAMLFilePath, |
| 882 | void *DiagContext, IntrusiveRefCntPtr<FileSystem> ExternalFS); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 883 | |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 884 | ErrorOr<Status> status(const Twine &Path) override; |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 885 | ErrorOr<std::unique_ptr<File>> openFileForRead(const Twine &Path) override; |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 886 | |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 887 | llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override { |
| 888 | return ExternalFS->getCurrentWorkingDirectory(); |
| 889 | } |
| 890 | std::error_code setCurrentWorkingDirectory(const Twine &Path) override { |
| 891 | return ExternalFS->setCurrentWorkingDirectory(Path); |
| 892 | } |
| 893 | |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 894 | directory_iterator dir_begin(const Twine &Dir, std::error_code &EC) override{ |
| 895 | ErrorOr<Entry *> E = lookupPath(Dir); |
| 896 | if (!E) { |
| 897 | EC = E.getError(); |
| 898 | return directory_iterator(); |
| 899 | } |
| 900 | ErrorOr<Status> S = status(Dir, *E); |
| 901 | if (!S) { |
| 902 | EC = S.getError(); |
| 903 | return directory_iterator(); |
| 904 | } |
| 905 | if (!S->isDirectory()) { |
| 906 | EC = std::error_code(static_cast<int>(errc::not_a_directory), |
| 907 | std::system_category()); |
| 908 | return directory_iterator(); |
| 909 | } |
| 910 | |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 911 | auto *D = cast<RedirectingDirectoryEntry>(*E); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 912 | return directory_iterator(std::make_shared<VFSFromYamlDirIterImpl>(Dir, |
| 913 | *this, D->contents_begin(), D->contents_end(), EC)); |
| 914 | } |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 915 | |
| 916 | void setExternalContentsPrefixDir(StringRef PrefixDir) { |
| 917 | ExternalContentsPrefixDir = PrefixDir.str(); |
| 918 | } |
| 919 | |
| 920 | StringRef getExternalContentsPrefixDir() const { |
| 921 | return ExternalContentsPrefixDir; |
| 922 | } |
| 923 | |
Bruno Cardoso Lopes | b2e2e21 | 2016-05-06 23:21:57 +0000 | [diff] [blame^] | 924 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 925 | LLVM_DUMP_METHOD void dump() const { |
| 926 | for (const std::unique_ptr<Entry> &Root : Roots) |
| 927 | dumpEntry(Root.get()); |
| 928 | } |
| 929 | |
| 930 | LLVM_DUMP_METHOD void dumpEntry(Entry *E, int NumSpaces = 0) const { |
| 931 | StringRef Name = E->getName(); |
| 932 | for (int i = 0, e = NumSpaces; i < e; ++i) |
| 933 | dbgs() << " "; |
| 934 | dbgs() << "'" << Name.str().c_str() << "'" << "\n"; |
| 935 | |
| 936 | if (E->getKind() == EK_Directory) { |
| 937 | auto *DE = dyn_cast<RedirectingDirectoryEntry>(E); |
| 938 | assert(DE && "Should be a directory"); |
| 939 | |
| 940 | for (std::unique_ptr<Entry> &SubEntry : |
| 941 | llvm::make_range(DE->contents_begin(), DE->contents_end())) |
| 942 | dumpEntry(SubEntry.get(), NumSpaces+2); |
| 943 | } |
| 944 | } |
| 945 | #endif |
| 946 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 947 | }; |
| 948 | |
| 949 | /// \brief A helper class to hold the common YAML parsing state. |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 950 | class RedirectingFileSystemParser { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 951 | yaml::Stream &Stream; |
| 952 | |
| 953 | void error(yaml::Node *N, const Twine &Msg) { |
| 954 | Stream.printError(N, Msg); |
| 955 | } |
| 956 | |
| 957 | // false on error |
| 958 | bool parseScalarString(yaml::Node *N, StringRef &Result, |
| 959 | SmallVectorImpl<char> &Storage) { |
| 960 | yaml::ScalarNode *S = dyn_cast<yaml::ScalarNode>(N); |
| 961 | if (!S) { |
| 962 | error(N, "expected string"); |
| 963 | return false; |
| 964 | } |
| 965 | Result = S->getValue(Storage); |
| 966 | return true; |
| 967 | } |
| 968 | |
| 969 | // false on error |
| 970 | bool parseScalarBool(yaml::Node *N, bool &Result) { |
| 971 | SmallString<5> Storage; |
| 972 | StringRef Value; |
| 973 | if (!parseScalarString(N, Value, Storage)) |
| 974 | return false; |
| 975 | |
| 976 | if (Value.equals_lower("true") || Value.equals_lower("on") || |
| 977 | Value.equals_lower("yes") || Value == "1") { |
| 978 | Result = true; |
| 979 | return true; |
| 980 | } else if (Value.equals_lower("false") || Value.equals_lower("off") || |
| 981 | Value.equals_lower("no") || Value == "0") { |
| 982 | Result = false; |
| 983 | return true; |
| 984 | } |
| 985 | |
| 986 | error(N, "expected boolean value"); |
| 987 | return false; |
| 988 | } |
| 989 | |
| 990 | struct KeyStatus { |
| 991 | KeyStatus(bool Required=false) : Required(Required), Seen(false) {} |
| 992 | bool Required; |
| 993 | bool Seen; |
| 994 | }; |
| 995 | typedef std::pair<StringRef, KeyStatus> KeyStatusPair; |
| 996 | |
| 997 | // false on error |
| 998 | bool checkDuplicateOrUnknownKey(yaml::Node *KeyNode, StringRef Key, |
| 999 | DenseMap<StringRef, KeyStatus> &Keys) { |
| 1000 | if (!Keys.count(Key)) { |
| 1001 | error(KeyNode, "unknown key"); |
| 1002 | return false; |
| 1003 | } |
| 1004 | KeyStatus &S = Keys[Key]; |
| 1005 | if (S.Seen) { |
| 1006 | error(KeyNode, Twine("duplicate key '") + Key + "'"); |
| 1007 | return false; |
| 1008 | } |
| 1009 | S.Seen = true; |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
| 1013 | // false on error |
| 1014 | bool checkMissingKeys(yaml::Node *Obj, DenseMap<StringRef, KeyStatus> &Keys) { |
| 1015 | for (DenseMap<StringRef, KeyStatus>::iterator I = Keys.begin(), |
| 1016 | E = Keys.end(); |
| 1017 | I != E; ++I) { |
| 1018 | if (I->second.Required && !I->second.Seen) { |
| 1019 | error(Obj, Twine("missing key '") + I->first + "'"); |
| 1020 | return false; |
| 1021 | } |
| 1022 | } |
| 1023 | return true; |
| 1024 | } |
| 1025 | |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1026 | std::unique_ptr<Entry> parseEntry(yaml::Node *N, RedirectingFileSystem *FS) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1027 | yaml::MappingNode *M = dyn_cast<yaml::MappingNode>(N); |
| 1028 | if (!M) { |
| 1029 | error(N, "expected mapping node for file or directory entry"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1030 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | KeyStatusPair Fields[] = { |
| 1034 | KeyStatusPair("name", true), |
| 1035 | KeyStatusPair("type", true), |
| 1036 | KeyStatusPair("contents", false), |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1037 | KeyStatusPair("external-contents", false), |
| 1038 | KeyStatusPair("use-external-name", false), |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1039 | }; |
| 1040 | |
Craig Topper | ac67c05 | 2015-11-30 03:11:10 +0000 | [diff] [blame] | 1041 | DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields)); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1042 | |
| 1043 | bool HasContents = false; // external or otherwise |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1044 | std::vector<std::unique_ptr<Entry>> EntryArrayContents; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1045 | std::string ExternalContentsPath; |
| 1046 | std::string Name; |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1047 | auto UseExternalName = RedirectingFileEntry::NK_NotSet; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1048 | EntryKind Kind; |
| 1049 | |
| 1050 | for (yaml::MappingNode::iterator I = M->begin(), E = M->end(); I != E; |
| 1051 | ++I) { |
| 1052 | StringRef Key; |
| 1053 | // Reuse the buffer for key and value, since we don't look at key after |
| 1054 | // parsing value. |
| 1055 | SmallString<256> Buffer; |
| 1056 | if (!parseScalarString(I->getKey(), Key, Buffer)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1057 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1058 | |
| 1059 | if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1060 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1061 | |
| 1062 | StringRef Value; |
| 1063 | if (Key == "name") { |
| 1064 | if (!parseScalarString(I->getValue(), Value, Buffer)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1065 | return nullptr; |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (FS->UseCanonicalizedPaths) { |
| 1068 | SmallString<256> Path(Value); |
| 1069 | // Guarantee that old YAML files containing paths with ".." and "." |
| 1070 | // are properly canonicalized before read into the VFS. |
| 1071 | Path = sys::path::remove_leading_dotslash(Path); |
| 1072 | sys::path::remove_dots(Path, /*remove_dot_dot=*/true); |
| 1073 | Name = Path.str(); |
| 1074 | } else { |
| 1075 | Name = Value; |
| 1076 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1077 | } else if (Key == "type") { |
| 1078 | if (!parseScalarString(I->getValue(), Value, Buffer)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1079 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1080 | if (Value == "file") |
| 1081 | Kind = EK_File; |
| 1082 | else if (Value == "directory") |
| 1083 | Kind = EK_Directory; |
| 1084 | else { |
| 1085 | error(I->getValue(), "unknown value for 'type'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1086 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } else if (Key == "contents") { |
| 1089 | if (HasContents) { |
| 1090 | error(I->getKey(), |
| 1091 | "entry already has 'contents' or 'external-contents'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1092 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1093 | } |
| 1094 | HasContents = true; |
| 1095 | yaml::SequenceNode *Contents = |
| 1096 | dyn_cast<yaml::SequenceNode>(I->getValue()); |
| 1097 | if (!Contents) { |
| 1098 | // FIXME: this is only for directories, what about files? |
| 1099 | error(I->getValue(), "expected array"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1100 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | for (yaml::SequenceNode::iterator I = Contents->begin(), |
| 1104 | E = Contents->end(); |
| 1105 | I != E; ++I) { |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1106 | if (std::unique_ptr<Entry> E = parseEntry(&*I, FS)) |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1107 | EntryArrayContents.push_back(std::move(E)); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1108 | else |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1109 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } else if (Key == "external-contents") { |
| 1112 | if (HasContents) { |
| 1113 | error(I->getKey(), |
| 1114 | "entry already has 'contents' or 'external-contents'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1115 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1116 | } |
| 1117 | HasContents = true; |
| 1118 | if (!parseScalarString(I->getValue(), Value, Buffer)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1119 | return nullptr; |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1120 | |
| 1121 | SmallString<256> FullPath; |
| 1122 | if (FS->IsRelativeOverlay) { |
| 1123 | FullPath = FS->getExternalContentsPrefixDir(); |
| 1124 | assert(!FullPath.empty() && |
| 1125 | "External contents prefix directory must exist"); |
| 1126 | llvm::sys::path::append(FullPath, Value); |
| 1127 | } else { |
| 1128 | FullPath = Value; |
| 1129 | } |
| 1130 | |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1131 | if (FS->UseCanonicalizedPaths) { |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1132 | // Guarantee that old YAML files containing paths with ".." and "." |
| 1133 | // are properly canonicalized before read into the VFS. |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1134 | FullPath = sys::path::remove_leading_dotslash(FullPath); |
| 1135 | sys::path::remove_dots(FullPath, /*remove_dot_dot=*/true); |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1136 | } |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1137 | ExternalContentsPath = FullPath.str(); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1138 | } else if (Key == "use-external-name") { |
| 1139 | bool Val; |
| 1140 | if (!parseScalarBool(I->getValue(), Val)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1141 | return nullptr; |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1142 | UseExternalName = Val ? RedirectingFileEntry::NK_External |
| 1143 | : RedirectingFileEntry::NK_Virtual; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1144 | } else { |
| 1145 | llvm_unreachable("key missing from Keys"); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | if (Stream.failed()) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1150 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1151 | |
| 1152 | // check for missing keys |
| 1153 | if (!HasContents) { |
| 1154 | error(N, "missing key 'contents' or 'external-contents'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1155 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1156 | } |
| 1157 | if (!checkMissingKeys(N, Keys)) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1158 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1159 | |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1160 | // check invalid configuration |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1161 | if (Kind == EK_Directory && |
| 1162 | UseExternalName != RedirectingFileEntry::NK_NotSet) { |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1163 | error(N, "'use-external-name' is not supported for directories"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1164 | return nullptr; |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1167 | // Remove trailing slash(es), being careful not to remove the root path |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1168 | StringRef Trimmed(Name); |
Ben Langmuir | 9385323 | 2014-03-05 21:32:20 +0000 | [diff] [blame] | 1169 | size_t RootPathLen = sys::path::root_path(Trimmed).size(); |
| 1170 | while (Trimmed.size() > RootPathLen && |
| 1171 | sys::path::is_separator(Trimmed.back())) |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1172 | Trimmed = Trimmed.slice(0, Trimmed.size()-1); |
| 1173 | // Get the last component |
| 1174 | StringRef LastComponent = sys::path::filename(Trimmed); |
| 1175 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1176 | std::unique_ptr<Entry> Result; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1177 | switch (Kind) { |
| 1178 | case EK_File: |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1179 | Result = llvm::make_unique<RedirectingFileEntry>( |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1180 | LastComponent, std::move(ExternalContentsPath), UseExternalName); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1181 | break; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1182 | case EK_Directory: |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1183 | Result = llvm::make_unique<RedirectingDirectoryEntry>( |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 1184 | LastComponent, std::move(EntryArrayContents), |
| 1185 | Status("", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 0, |
| 1186 | file_type::directory_file, sys::fs::all_all)); |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1187 | break; |
| 1188 | } |
| 1189 | |
| 1190 | StringRef Parent = sys::path::parent_path(Trimmed); |
| 1191 | if (Parent.empty()) |
| 1192 | return Result; |
| 1193 | |
| 1194 | // if 'name' contains multiple components, create implicit directory entries |
| 1195 | for (sys::path::reverse_iterator I = sys::path::rbegin(Parent), |
| 1196 | E = sys::path::rend(Parent); |
| 1197 | I != E; ++I) { |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1198 | std::vector<std::unique_ptr<Entry>> Entries; |
| 1199 | Entries.push_back(std::move(Result)); |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1200 | Result = llvm::make_unique<RedirectingDirectoryEntry>( |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1201 | *I, std::move(Entries), |
Benjamin Kramer | 268b51a | 2015-10-05 13:15:33 +0000 | [diff] [blame] | 1202 | Status("", getNextVirtualUniqueID(), sys::TimeValue::now(), 0, 0, 0, |
| 1203 | file_type::directory_file, sys::fs::all_all)); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1204 | } |
Ben Langmuir | 47ff9ab | 2014-02-25 04:34:14 +0000 | [diff] [blame] | 1205 | return Result; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | public: |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1209 | RedirectingFileSystemParser(yaml::Stream &S) : Stream(S) {} |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1210 | |
| 1211 | // false on error |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1212 | bool parse(yaml::Node *Root, RedirectingFileSystem *FS) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1213 | yaml::MappingNode *Top = dyn_cast<yaml::MappingNode>(Root); |
| 1214 | if (!Top) { |
| 1215 | error(Root, "expected mapping node"); |
| 1216 | return false; |
| 1217 | } |
| 1218 | |
| 1219 | KeyStatusPair Fields[] = { |
| 1220 | KeyStatusPair("version", true), |
| 1221 | KeyStatusPair("case-sensitive", false), |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1222 | KeyStatusPair("use-external-names", false), |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1223 | KeyStatusPair("overlay-relative", false), |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1224 | KeyStatusPair("roots", true), |
| 1225 | }; |
| 1226 | |
Craig Topper | ac67c05 | 2015-11-30 03:11:10 +0000 | [diff] [blame] | 1227 | DenseMap<StringRef, KeyStatus> Keys(std::begin(Fields), std::end(Fields)); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1228 | |
| 1229 | // Parse configuration and 'roots' |
| 1230 | for (yaml::MappingNode::iterator I = Top->begin(), E = Top->end(); I != E; |
| 1231 | ++I) { |
| 1232 | SmallString<10> KeyBuffer; |
| 1233 | StringRef Key; |
| 1234 | if (!parseScalarString(I->getKey(), Key, KeyBuffer)) |
| 1235 | return false; |
| 1236 | |
| 1237 | if (!checkDuplicateOrUnknownKey(I->getKey(), Key, Keys)) |
| 1238 | return false; |
| 1239 | |
| 1240 | if (Key == "roots") { |
| 1241 | yaml::SequenceNode *Roots = dyn_cast<yaml::SequenceNode>(I->getValue()); |
| 1242 | if (!Roots) { |
| 1243 | error(I->getValue(), "expected array"); |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | for (yaml::SequenceNode::iterator I = Roots->begin(), E = Roots->end(); |
| 1248 | I != E; ++I) { |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1249 | if (std::unique_ptr<Entry> E = parseEntry(&*I, FS)) |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1250 | FS->Roots.push_back(std::move(E)); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1251 | else |
| 1252 | return false; |
| 1253 | } |
| 1254 | } else if (Key == "version") { |
| 1255 | StringRef VersionString; |
| 1256 | SmallString<4> Storage; |
| 1257 | if (!parseScalarString(I->getValue(), VersionString, Storage)) |
| 1258 | return false; |
| 1259 | int Version; |
| 1260 | if (VersionString.getAsInteger<int>(10, Version)) { |
| 1261 | error(I->getValue(), "expected integer"); |
| 1262 | return false; |
| 1263 | } |
| 1264 | if (Version < 0) { |
| 1265 | error(I->getValue(), "invalid version number"); |
| 1266 | return false; |
| 1267 | } |
| 1268 | if (Version != 0) { |
| 1269 | error(I->getValue(), "version mismatch, expected 0"); |
| 1270 | return false; |
| 1271 | } |
| 1272 | } else if (Key == "case-sensitive") { |
| 1273 | if (!parseScalarBool(I->getValue(), FS->CaseSensitive)) |
| 1274 | return false; |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1275 | } else if (Key == "overlay-relative") { |
| 1276 | if (!parseScalarBool(I->getValue(), FS->IsRelativeOverlay)) |
| 1277 | return false; |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1278 | } else if (Key == "use-external-names") { |
| 1279 | if (!parseScalarBool(I->getValue(), FS->UseExternalNames)) |
| 1280 | return false; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1281 | } else { |
| 1282 | llvm_unreachable("key missing from Keys"); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | if (Stream.failed()) |
| 1287 | return false; |
| 1288 | |
| 1289 | if (!checkMissingKeys(Top, Keys)) |
| 1290 | return false; |
| 1291 | return true; |
| 1292 | } |
| 1293 | }; |
| 1294 | } // end of anonymous namespace |
| 1295 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1296 | Entry::~Entry() = default; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1297 | |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1298 | RedirectingFileSystem * |
| 1299 | RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer, |
| 1300 | SourceMgr::DiagHandlerTy DiagHandler, |
| 1301 | StringRef YAMLFilePath, void *DiagContext, |
| 1302 | IntrusiveRefCntPtr<FileSystem> ExternalFS) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1303 | |
| 1304 | SourceMgr SM; |
Rafael Espindola | 85d7892 | 2014-08-27 19:03:27 +0000 | [diff] [blame] | 1305 | yaml::Stream Stream(Buffer->getMemBufferRef(), SM); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1306 | |
Ben Langmuir | 97882e7 | 2014-02-24 20:56:37 +0000 | [diff] [blame] | 1307 | SM.setDiagHandler(DiagHandler, DiagContext); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1308 | yaml::document_iterator DI = Stream.begin(); |
| 1309 | yaml::Node *Root = DI->getRoot(); |
| 1310 | if (DI == Stream.end() || !Root) { |
| 1311 | SM.PrintMessage(SMLoc(), SourceMgr::DK_Error, "expected root node"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1312 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1315 | RedirectingFileSystemParser P(Stream); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1316 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1317 | std::unique_ptr<RedirectingFileSystem> FS( |
| 1318 | new RedirectingFileSystem(ExternalFS)); |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1319 | |
| 1320 | if (!YAMLFilePath.empty()) { |
| 1321 | // Use the YAML path from -ivfsoverlay to compute the dir to be prefixed |
| 1322 | // to each 'external-contents' path. |
| 1323 | // |
| 1324 | // Example: |
| 1325 | // -ivfsoverlay dummy.cache/vfs/vfs.yaml |
| 1326 | // yields: |
| 1327 | // FS->ExternalContentsPrefixDir => /<absolute_path_to>/dummy.cache/vfs |
| 1328 | // |
| 1329 | SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath); |
| 1330 | std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir); |
| 1331 | assert(!EC && "Overlay dir final path must be absolute"); |
| 1332 | (void)EC; |
| 1333 | FS->setExternalContentsPrefixDir(OverlayAbsDir); |
| 1334 | } |
| 1335 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1336 | if (!P.parse(Root, FS.get())) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 1337 | return nullptr; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1338 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 1339 | return FS.release(); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1342 | ErrorOr<Entry *> RedirectingFileSystem::lookupPath(const Twine &Path_) { |
Ben Langmuir | a6f8ca8 | 2014-03-04 22:34:50 +0000 | [diff] [blame] | 1343 | SmallString<256> Path; |
| 1344 | Path_.toVector(Path); |
| 1345 | |
| 1346 | // Handle relative paths |
Benjamin Kramer | 7708b2a | 2015-10-05 13:55:20 +0000 | [diff] [blame] | 1347 | if (std::error_code EC = makeAbsolute(Path)) |
Ben Langmuir | a6f8ca8 | 2014-03-04 22:34:50 +0000 | [diff] [blame] | 1348 | return EC; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1349 | |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1350 | // Canonicalize path by removing ".", "..", "./", etc components. This is |
| 1351 | // a VFS request, do bot bother about symlinks in the path components |
| 1352 | // but canonicalize in order to perform the correct entry search. |
| 1353 | if (UseCanonicalizedPaths) { |
| 1354 | Path = sys::path::remove_leading_dotslash(Path); |
| 1355 | sys::path::remove_dots(Path, /*remove_dot_dot=*/true); |
| 1356 | } |
| 1357 | |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1358 | if (Path.empty()) |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1359 | return make_error_code(llvm::errc::invalid_argument); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1360 | |
| 1361 | sys::path::const_iterator Start = sys::path::begin(Path); |
| 1362 | sys::path::const_iterator End = sys::path::end(Path); |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1363 | for (const std::unique_ptr<Entry> &Root : Roots) { |
| 1364 | ErrorOr<Entry *> Result = lookupPath(Start, End, Root.get()); |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1365 | if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1366 | return Result; |
| 1367 | } |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1368 | return make_error_code(llvm::errc::no_such_file_or_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1371 | ErrorOr<Entry *> |
| 1372 | RedirectingFileSystem::lookupPath(sys::path::const_iterator Start, |
| 1373 | sys::path::const_iterator End, Entry *From) { |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1374 | #ifndef LLVM_ON_WIN32 |
| 1375 | assert(!isTraversalComponent(*Start) && |
| 1376 | !isTraversalComponent(From->getName()) && |
| 1377 | "Paths should not contain traversal components"); |
| 1378 | #else |
| 1379 | // FIXME: this is here to support windows, remove it once canonicalized |
| 1380 | // paths become globally default. |
Bruno Cardoso Lopes | be056b1 | 2016-02-23 17:06:50 +0000 | [diff] [blame] | 1381 | if (Start->equals(".")) |
| 1382 | ++Start; |
Bruno Cardoso Lopes | b76c027 | 2016-03-17 02:20:43 +0000 | [diff] [blame] | 1383 | #endif |
Ben Langmuir | a6f8ca8 | 2014-03-04 22:34:50 +0000 | [diff] [blame] | 1384 | |
Bruno Cardoso Lopes | d712b34 | 2016-03-30 23:54:00 +0000 | [diff] [blame] | 1385 | StringRef FromName = From->getName(); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1386 | |
Bruno Cardoso Lopes | d712b34 | 2016-03-30 23:54:00 +0000 | [diff] [blame] | 1387 | // Forward the search to the next component in case this is an empty one. |
| 1388 | if (!FromName.empty()) { |
| 1389 | if (CaseSensitive ? !Start->equals(FromName) |
| 1390 | : !Start->equals_lower(FromName)) |
| 1391 | // failure to match |
| 1392 | return make_error_code(llvm::errc::no_such_file_or_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1393 | |
Bruno Cardoso Lopes | d712b34 | 2016-03-30 23:54:00 +0000 | [diff] [blame] | 1394 | ++Start; |
| 1395 | |
| 1396 | if (Start == End) { |
| 1397 | // Match! |
| 1398 | return From; |
| 1399 | } |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1402 | auto *DE = dyn_cast<RedirectingDirectoryEntry>(From); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1403 | if (!DE) |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1404 | return make_error_code(llvm::errc::not_a_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1405 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1406 | for (const std::unique_ptr<Entry> &DirEntry : |
| 1407 | llvm::make_range(DE->contents_begin(), DE->contents_end())) { |
| 1408 | ErrorOr<Entry *> Result = lookupPath(Start, End, DirEntry.get()); |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1409 | if (Result || Result.getError() != llvm::errc::no_such_file_or_directory) |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1410 | return Result; |
| 1411 | } |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1412 | return make_error_code(llvm::errc::no_such_file_or_directory); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1415 | static Status getRedirectedFileStatus(const Twine &Path, bool UseExternalNames, |
| 1416 | Status ExternalStatus) { |
| 1417 | Status S = ExternalStatus; |
| 1418 | if (!UseExternalNames) |
| 1419 | S = Status::copyWithNewName(S, Path.str()); |
| 1420 | S.IsVFSMapped = true; |
| 1421 | return S; |
| 1422 | } |
| 1423 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1424 | ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path, Entry *E) { |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1425 | assert(E != nullptr); |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1426 | if (auto *F = dyn_cast<RedirectingFileEntry>(E)) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1427 | ErrorOr<Status> S = ExternalFS->status(F->getExternalContentsPath()); |
Ben Langmuir | b59cf67 | 2014-02-27 00:25:12 +0000 | [diff] [blame] | 1428 | assert(!S || S->getName() == F->getExternalContentsPath()); |
Ben Langmuir | 5de00f3 | 2014-05-23 18:15:47 +0000 | [diff] [blame] | 1429 | if (S) |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1430 | return getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames), |
| 1431 | *S); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1432 | return S; |
| 1433 | } else { // directory |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1434 | auto *DE = cast<RedirectingDirectoryEntry>(E); |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1435 | return Status::copyWithNewName(DE->getStatus(), Path.str()); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1436 | } |
| 1437 | } |
| 1438 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1439 | ErrorOr<Status> RedirectingFileSystem::status(const Twine &Path) { |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1440 | ErrorOr<Entry *> Result = lookupPath(Path); |
| 1441 | if (!Result) |
| 1442 | return Result.getError(); |
| 1443 | return status(Path, *Result); |
| 1444 | } |
| 1445 | |
Benjamin Kramer | 5532ef1 | 2015-10-05 13:55:09 +0000 | [diff] [blame] | 1446 | namespace { |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1447 | /// Provide a file wrapper with an overriden status. |
| 1448 | class FileWithFixedStatus : public File { |
Benjamin Kramer | 5532ef1 | 2015-10-05 13:55:09 +0000 | [diff] [blame] | 1449 | std::unique_ptr<File> InnerFile; |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1450 | Status S; |
Benjamin Kramer | 5532ef1 | 2015-10-05 13:55:09 +0000 | [diff] [blame] | 1451 | |
| 1452 | public: |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1453 | FileWithFixedStatus(std::unique_ptr<File> InnerFile, Status S) |
| 1454 | : InnerFile(std::move(InnerFile)), S(S) {} |
Benjamin Kramer | 5532ef1 | 2015-10-05 13:55:09 +0000 | [diff] [blame] | 1455 | |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1456 | ErrorOr<Status> status() override { return S; } |
| 1457 | ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
Benjamin Kramer | 737501c | 2015-10-05 21:20:19 +0000 | [diff] [blame] | 1458 | getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator, |
| 1459 | bool IsVolatile) override { |
Benjamin Kramer | 5532ef1 | 2015-10-05 13:55:09 +0000 | [diff] [blame] | 1460 | return InnerFile->getBuffer(Name, FileSize, RequiresNullTerminator, |
| 1461 | IsVolatile); |
| 1462 | } |
| 1463 | std::error_code close() override { return InnerFile->close(); } |
| 1464 | }; |
| 1465 | } // end anonymous namespace |
| 1466 | |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1467 | ErrorOr<std::unique_ptr<File>> |
| 1468 | RedirectingFileSystem::openFileForRead(const Twine &Path) { |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1469 | ErrorOr<Entry *> E = lookupPath(Path); |
| 1470 | if (!E) |
| 1471 | return E.getError(); |
| 1472 | |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1473 | auto *F = dyn_cast<RedirectingFileEntry>(*E); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1474 | if (!F) // FIXME: errc::not_a_file? |
Rafael Espindola | 71de0b6 | 2014-06-13 17:20:50 +0000 | [diff] [blame] | 1475 | return make_error_code(llvm::errc::invalid_argument); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1476 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 1477 | auto Result = ExternalFS->openFileForRead(F->getExternalContentsPath()); |
| 1478 | if (!Result) |
| 1479 | return Result; |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 1480 | |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1481 | auto ExternalStatus = (*Result)->status(); |
| 1482 | if (!ExternalStatus) |
| 1483 | return ExternalStatus.getError(); |
Ben Langmuir | d066d4c | 2014-02-28 21:16:07 +0000 | [diff] [blame] | 1484 | |
Ben Langmuir | f13302e | 2015-12-10 23:41:39 +0000 | [diff] [blame] | 1485 | // FIXME: Update the status with the name and VFSMapped. |
| 1486 | Status S = getRedirectedFileStatus(Path, F->useExternalName(UseExternalNames), |
| 1487 | *ExternalStatus); |
| 1488 | return std::unique_ptr<File>( |
| 1489 | llvm::make_unique<FileWithFixedStatus>(std::move(*Result), S)); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | IntrusiveRefCntPtr<FileSystem> |
Rafael Espindola | 04ab21d7 | 2014-08-17 22:12:58 +0000 | [diff] [blame] | 1493 | vfs::getVFSFromYAML(std::unique_ptr<MemoryBuffer> Buffer, |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1494 | SourceMgr::DiagHandlerTy DiagHandler, |
| 1495 | StringRef YAMLFilePath, |
| 1496 | void *DiagContext, |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1497 | IntrusiveRefCntPtr<FileSystem> ExternalFS) { |
Benjamin Kramer | dadb58b | 2015-10-07 10:05:44 +0000 | [diff] [blame] | 1498 | return RedirectingFileSystem::create(std::move(Buffer), DiagHandler, |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1499 | YAMLFilePath, DiagContext, ExternalFS); |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | UniqueID vfs::getNextVirtualUniqueID() { |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 1503 | static std::atomic<unsigned> UID; |
| 1504 | unsigned ID = ++UID; |
Ben Langmuir | d51ba0b | 2014-02-21 23:39:37 +0000 | [diff] [blame] | 1505 | // The following assumes that uint64_t max will never collide with a real |
| 1506 | // dev_t value from the OS. |
| 1507 | return UniqueID(std::numeric_limits<uint64_t>::max(), ID); |
| 1508 | } |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1509 | |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1510 | void YAMLVFSWriter::addFileMapping(StringRef VirtualPath, StringRef RealPath) { |
| 1511 | assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute"); |
| 1512 | assert(sys::path::is_absolute(RealPath) && "real path not absolute"); |
| 1513 | assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported"); |
| 1514 | Mappings.emplace_back(VirtualPath, RealPath); |
| 1515 | } |
| 1516 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1517 | namespace { |
| 1518 | class JSONWriter { |
| 1519 | llvm::raw_ostream &OS; |
| 1520 | SmallVector<StringRef, 16> DirStack; |
| 1521 | inline unsigned getDirIndent() { return 4 * DirStack.size(); } |
| 1522 | inline unsigned getFileIndent() { return 4 * (DirStack.size() + 1); } |
| 1523 | bool containedIn(StringRef Parent, StringRef Path); |
| 1524 | StringRef containedPart(StringRef Parent, StringRef Path); |
| 1525 | void startDirectory(StringRef Path); |
| 1526 | void endDirectory(); |
| 1527 | void writeEntry(StringRef VPath, StringRef RPath); |
| 1528 | |
| 1529 | public: |
| 1530 | JSONWriter(llvm::raw_ostream &OS) : OS(OS) {} |
Bruno Cardoso Lopes | fc8644c | 2016-04-13 19:28:21 +0000 | [diff] [blame] | 1531 | void write(ArrayRef<YAMLVFSEntry> Entries, Optional<bool> UseExternalNames, |
| 1532 | Optional<bool> IsCaseSensitive, Optional<bool> IsOverlayRelative, |
| 1533 | StringRef OverlayDir); |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1534 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1535 | } |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1536 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1537 | bool JSONWriter::containedIn(StringRef Parent, StringRef Path) { |
Justin Bogner | 1c078f2 | 2014-05-20 22:12:58 +0000 | [diff] [blame] | 1538 | using namespace llvm::sys; |
| 1539 | // Compare each path component. |
| 1540 | auto IParent = path::begin(Parent), EParent = path::end(Parent); |
| 1541 | for (auto IChild = path::begin(Path), EChild = path::end(Path); |
| 1542 | IParent != EParent && IChild != EChild; ++IParent, ++IChild) { |
| 1543 | if (*IParent != *IChild) |
| 1544 | return false; |
| 1545 | } |
| 1546 | // Have we exhausted the parent path? |
| 1547 | return IParent == EParent; |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1550 | StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) { |
| 1551 | assert(!Parent.empty()); |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1552 | assert(containedIn(Parent, Path)); |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1553 | return Path.slice(Parent.size() + 1, StringRef::npos); |
| 1554 | } |
| 1555 | |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1556 | void JSONWriter::startDirectory(StringRef Path) { |
| 1557 | StringRef Name = |
| 1558 | DirStack.empty() ? Path : containedPart(DirStack.back(), Path); |
| 1559 | DirStack.push_back(Path); |
| 1560 | unsigned Indent = getDirIndent(); |
| 1561 | OS.indent(Indent) << "{\n"; |
| 1562 | OS.indent(Indent + 2) << "'type': 'directory',\n"; |
| 1563 | OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(Name) << "\",\n"; |
| 1564 | OS.indent(Indent + 2) << "'contents': [\n"; |
| 1565 | } |
| 1566 | |
| 1567 | void JSONWriter::endDirectory() { |
| 1568 | unsigned Indent = getDirIndent(); |
| 1569 | OS.indent(Indent + 2) << "]\n"; |
| 1570 | OS.indent(Indent) << "}"; |
| 1571 | |
| 1572 | DirStack.pop_back(); |
| 1573 | } |
| 1574 | |
| 1575 | void JSONWriter::writeEntry(StringRef VPath, StringRef RPath) { |
| 1576 | unsigned Indent = getFileIndent(); |
| 1577 | OS.indent(Indent) << "{\n"; |
| 1578 | OS.indent(Indent + 2) << "'type': 'file',\n"; |
| 1579 | OS.indent(Indent + 2) << "'name': \"" << llvm::yaml::escape(VPath) << "\",\n"; |
| 1580 | OS.indent(Indent + 2) << "'external-contents': \"" |
| 1581 | << llvm::yaml::escape(RPath) << "\"\n"; |
| 1582 | OS.indent(Indent) << "}"; |
| 1583 | } |
| 1584 | |
| 1585 | void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries, |
Bruno Cardoso Lopes | fc8644c | 2016-04-13 19:28:21 +0000 | [diff] [blame] | 1586 | Optional<bool> UseExternalNames, |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1587 | Optional<bool> IsCaseSensitive, |
| 1588 | Optional<bool> IsOverlayRelative, |
| 1589 | StringRef OverlayDir) { |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1590 | using namespace llvm::sys; |
| 1591 | |
| 1592 | OS << "{\n" |
| 1593 | " 'version': 0,\n"; |
| 1594 | if (IsCaseSensitive.hasValue()) |
| 1595 | OS << " 'case-sensitive': '" |
| 1596 | << (IsCaseSensitive.getValue() ? "true" : "false") << "',\n"; |
Bruno Cardoso Lopes | fc8644c | 2016-04-13 19:28:21 +0000 | [diff] [blame] | 1597 | if (UseExternalNames.hasValue()) |
| 1598 | OS << " 'use-external-names': '" |
| 1599 | << (UseExternalNames.getValue() ? "true" : "false") << "',\n"; |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1600 | bool UseOverlayRelative = false; |
| 1601 | if (IsOverlayRelative.hasValue()) { |
| 1602 | UseOverlayRelative = IsOverlayRelative.getValue(); |
| 1603 | OS << " 'overlay-relative': '" |
| 1604 | << (UseOverlayRelative ? "true" : "false") << "',\n"; |
| 1605 | } |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1606 | OS << " 'roots': [\n"; |
| 1607 | |
Justin Bogner | 7346640 | 2014-07-15 01:24:35 +0000 | [diff] [blame] | 1608 | if (!Entries.empty()) { |
| 1609 | const YAMLVFSEntry &Entry = Entries.front(); |
| 1610 | startDirectory(path::parent_path(Entry.VPath)); |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1611 | |
| 1612 | StringRef RPath = Entry.RPath; |
| 1613 | if (UseOverlayRelative) { |
| 1614 | unsigned OverlayDirLen = OverlayDir.size(); |
| 1615 | assert(RPath.substr(0, OverlayDirLen) == OverlayDir && |
| 1616 | "Overlay dir must be contained in RPath"); |
| 1617 | RPath = RPath.slice(OverlayDirLen, RPath.size()); |
| 1618 | } |
| 1619 | |
| 1620 | writeEntry(path::filename(Entry.VPath), RPath); |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1621 | |
Justin Bogner | 7346640 | 2014-07-15 01:24:35 +0000 | [diff] [blame] | 1622 | for (const auto &Entry : Entries.slice(1)) { |
| 1623 | StringRef Dir = path::parent_path(Entry.VPath); |
| 1624 | if (Dir == DirStack.back()) |
| 1625 | OS << ",\n"; |
| 1626 | else { |
| 1627 | while (!DirStack.empty() && !containedIn(DirStack.back(), Dir)) { |
| 1628 | OS << "\n"; |
| 1629 | endDirectory(); |
| 1630 | } |
| 1631 | OS << ",\n"; |
| 1632 | startDirectory(Dir); |
| 1633 | } |
Bruno Cardoso Lopes | d878e28 | 2016-03-20 02:08:48 +0000 | [diff] [blame] | 1634 | StringRef RPath = Entry.RPath; |
| 1635 | if (UseOverlayRelative) { |
| 1636 | unsigned OverlayDirLen = OverlayDir.size(); |
| 1637 | assert(RPath.substr(0, OverlayDirLen) == OverlayDir && |
| 1638 | "Overlay dir must be contained in RPath"); |
| 1639 | RPath = RPath.slice(OverlayDirLen, RPath.size()); |
| 1640 | } |
| 1641 | writeEntry(path::filename(Entry.VPath), RPath); |
Justin Bogner | 7346640 | 2014-07-15 01:24:35 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | while (!DirStack.empty()) { |
| 1645 | OS << "\n"; |
| 1646 | endDirectory(); |
| 1647 | } |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1648 | OS << "\n"; |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Justin Bogner | 7346640 | 2014-07-15 01:24:35 +0000 | [diff] [blame] | 1651 | OS << " ]\n" |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1652 | << "}\n"; |
| 1653 | } |
| 1654 | |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1655 | void YAMLVFSWriter::write(llvm::raw_ostream &OS) { |
| 1656 | std::sort(Mappings.begin(), Mappings.end(), |
Justin Bogner | 44fa45034 | 2014-05-21 22:46:51 +0000 | [diff] [blame] | 1657 | [](const YAMLVFSEntry &LHS, const YAMLVFSEntry &RHS) { |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1658 | return LHS.VPath < RHS.VPath; |
| 1659 | }); |
| 1660 | |
Bruno Cardoso Lopes | fc8644c | 2016-04-13 19:28:21 +0000 | [diff] [blame] | 1661 | JSONWriter(OS).write(Mappings, UseExternalNames, IsCaseSensitive, |
| 1662 | IsOverlayRelative, OverlayDir); |
Justin Bogner | 9c78529 | 2014-05-20 21:43:27 +0000 | [diff] [blame] | 1663 | } |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1664 | |
Benjamin Kramer | 49692ed | 2015-10-09 13:28:13 +0000 | [diff] [blame] | 1665 | VFSFromYamlDirIterImpl::VFSFromYamlDirIterImpl( |
| 1666 | const Twine &_Path, RedirectingFileSystem &FS, |
| 1667 | RedirectingDirectoryEntry::iterator Begin, |
| 1668 | RedirectingDirectoryEntry::iterator End, std::error_code &EC) |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1669 | : Dir(_Path.str()), FS(FS), Current(Begin), End(End) { |
| 1670 | if (Current != End) { |
| 1671 | SmallString<128> PathStr(Dir); |
| 1672 | llvm::sys::path::append(PathStr, (*Current)->getName()); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1673 | llvm::ErrorOr<vfs::Status> S = FS.status(PathStr); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1674 | if (S) |
| 1675 | CurrentEntry = *S; |
| 1676 | else |
| 1677 | EC = S.getError(); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | std::error_code VFSFromYamlDirIterImpl::increment() { |
| 1682 | assert(Current != End && "cannot iterate past end"); |
| 1683 | if (++Current != End) { |
| 1684 | SmallString<128> PathStr(Dir); |
| 1685 | llvm::sys::path::append(PathStr, (*Current)->getName()); |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1686 | llvm::ErrorOr<vfs::Status> S = FS.status(PathStr); |
Ben Langmuir | 740812b | 2014-06-24 19:37:16 +0000 | [diff] [blame] | 1687 | if (!S) |
| 1688 | return S.getError(); |
| 1689 | CurrentEntry = *S; |
| 1690 | } else { |
| 1691 | CurrentEntry = Status(); |
| 1692 | } |
| 1693 | return std::error_code(); |
| 1694 | } |
Ben Langmuir | 7c9f6c8 | 2014-06-25 20:25:40 +0000 | [diff] [blame] | 1695 | |
| 1696 | vfs::recursive_directory_iterator::recursive_directory_iterator(FileSystem &FS_, |
| 1697 | const Twine &Path, |
| 1698 | std::error_code &EC) |
| 1699 | : FS(&FS_) { |
| 1700 | directory_iterator I = FS->dir_begin(Path, EC); |
| 1701 | if (!EC && I != directory_iterator()) { |
| 1702 | State = std::make_shared<IterState>(); |
| 1703 | State->push(I); |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | vfs::recursive_directory_iterator & |
| 1708 | recursive_directory_iterator::increment(std::error_code &EC) { |
| 1709 | assert(FS && State && !State->empty() && "incrementing past end"); |
| 1710 | assert(State->top()->isStatusKnown() && "non-canonical end iterator"); |
| 1711 | vfs::directory_iterator End; |
| 1712 | if (State->top()->isDirectory()) { |
| 1713 | vfs::directory_iterator I = FS->dir_begin(State->top()->getName(), EC); |
| 1714 | if (EC) |
| 1715 | return *this; |
| 1716 | if (I != End) { |
| 1717 | State->push(I); |
| 1718 | return *this; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | while (!State->empty() && State->top().increment(EC) == End) |
| 1723 | State->pop(); |
| 1724 | |
| 1725 | if (State->empty()) |
| 1726 | State.reset(); // end iterator |
| 1727 | |
| 1728 | return *this; |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 1729 | } |