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