1 //===-- MockSymlinkFileSystem.h
2 //--------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/FileSpec.h"
11 #include "llvm/Support/VirtualFileSystem.h"
13 namespace lldb_private
{
15 // A mock file system that realpath's a given symlink to a given realpath.
16 class MockSymlinkFileSystem
: public llvm::vfs::FileSystem
{
18 // Treat all files as non-symlinks.
19 MockSymlinkFileSystem() = default;
21 /// Treat \a symlink as a symlink to \a realpath. Treat all other files as
23 MockSymlinkFileSystem(FileSpec
&&symlink
, FileSpec
&&realpath
,
24 FileSpec::Style style
= FileSpec::Style::native
)
25 : m_symlink(std::move(symlink
)), m_realpath(std::move(realpath
)),
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(path
.begin(), path
.end());
36 Path
.toVector(Output
);
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
;
45 llvm::ErrorOr
<std::unique_ptr
<llvm::vfs::File
>>
46 openFileForRead(const llvm::Twine
&Path
) override
{
47 return llvm::errc::operation_not_permitted
;
49 llvm::vfs::directory_iterator
dir_begin(const llvm::Twine
&Dir
,
50 std::error_code
&EC
) override
{
51 return llvm::vfs::directory_iterator();
53 std::error_code
setCurrentWorkingDirectory(const llvm::Twine
&Path
) override
{
54 return llvm::errc::operation_not_permitted
;
56 llvm::ErrorOr
<std::string
> getCurrentWorkingDirectory() const override
{
57 return llvm::errc::operation_not_permitted
;
63 FileSpec::Style m_style
;
66 } // namespace lldb_private