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