| 1 | //===-- MockSymlinkFileSystem.h |
| 2 | //--------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Utility/FileSpec.h" |
| 11 | #include "llvm/Support/VirtualFileSystem.h" |
| 12 | |
| 13 | namespace lldb_private { |
| 14 | |
| 15 | // A mock file system that realpath's a given symlink to a given realpath. |
| 16 | class MockSymlinkFileSystem : public llvm::vfs::FileSystem { |
| 17 | public: |
| 18 | // Treat all files as non-symlinks. |
| 19 | MockSymlinkFileSystem() = default; |
| 20 | |
| 21 | /// Treat \a symlink as a symlink to \a realpath. Treat all other files as |
| 22 | /// non-symlinks. |
| 23 | MockSymlinkFileSystem(FileSpec &&symlink, FileSpec &&realpath, |
| 24 | FileSpec::Style style = FileSpec::Style::native) |
| 25 | : m_symlink(std::move(symlink)), m_realpath(std::move(realpath)), |
| 26 | m_style(style) {} |
| 27 | |
| 28 | /// If \a Path matches the symlink given in the ctor, put the realpath given |
| 29 | /// in the ctor into \a Output. |
| 30 | std::error_code getRealPath(const llvm::Twine &Path, |
| 31 | llvm::SmallVectorImpl<char> &Output) override { |
| 32 | if (FileSpec(Path.str(), m_style) == m_symlink) { |
| 33 | std::string path = m_realpath.GetPath(); |
| 34 | Output.assign(in_start: path.begin(), in_end: path.end()); |
| 35 | } else { |
| 36 | Path.toVector(Out&: Output); |
| 37 | } |
| 38 | return {}; |
| 39 | } |
| 40 | |
| 41 | // Implement the rest of the interface |
| 42 | llvm::ErrorOr<llvm::vfs::Status> status(const llvm::Twine &Path) override { |
| 43 | return llvm::errc::operation_not_permitted; |
| 44 | } |
| 45 | llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> |
| 46 | openFileForRead(const llvm::Twine &Path) override { |
| 47 | return llvm::errc::operation_not_permitted; |
| 48 | } |
| 49 | llvm::vfs::directory_iterator dir_begin(const llvm::Twine &Dir, |
| 50 | std::error_code &EC) override { |
| 51 | return llvm::vfs::directory_iterator(); |
| 52 | } |
| 53 | std::error_code setCurrentWorkingDirectory(const llvm::Twine &Path) override { |
| 54 | return llvm::errc::operation_not_permitted; |
| 55 | } |
| 56 | llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override { |
| 57 | return llvm::errc::operation_not_permitted; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | FileSpec m_symlink; |
| 62 | FileSpec m_realpath; |
| 63 | FileSpec::Style m_style; |
| 64 | }; |
| 65 | |
| 66 | } // namespace lldb_private |
| 67 | |