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