Bug Summary

File:llvm/lib/Support/VirtualFileSystem.cpp
Warning:line 1466, column 14
The left operand of '==' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

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