#include "lldb/Utility/RealpathPrefixes.h"
#include "lldb/Utility/FileSpec.h"
#include "lldb/Utility/FileSpecList.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/lldb-private-types.h"
using namespace lldb_private;
RealpathPrefixes::RealpathPrefixes(
const FileSpecList &file_spec_list,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)
: m_fs(fs) {
m_prefixes.reserve(file_spec_list.GetSize());
for (const FileSpec &file_spec : file_spec_list) {
m_prefixes.emplace_back(file_spec.GetPath());
}
}
std::optional<FileSpec>
RealpathPrefixes::ResolveSymlinks(const FileSpec &file_spec) {
if (m_prefixes.empty())
return std::nullopt;
auto is_path_prefix = [](llvm::StringRef a, llvm::StringRef b,
bool case_sensitive,
llvm::sys::path::Style style) -> bool {
if (case_sensitive ? a.consume_front(b) : a.consume_front_insensitive(b))
return b == "/" || a.empty() ||
llvm::sys::path::is_separator(a[0], style);
return false;
};
std::string file_spec_path = file_spec.GetPath();
for (const std::string &prefix : m_prefixes) {
if (is_path_prefix(file_spec_path, prefix, file_spec.IsCaseSensitive(),
file_spec.GetPathStyle())) {
IncreaseSourceRealpathAttemptCount();
Log *log = GetLog(LLDBLog::Source);
LLDB_LOGF(log, "Realpath'ing support file %s", file_spec_path.c_str());
PathSmallString buff;
std::error_code ec = m_fs->getRealPath(file_spec_path, buff);
if (ec)
return std::nullopt;
FileSpec realpath(buff, file_spec.GetPathStyle());
if (realpath != file_spec)
return realpath;
return std::nullopt;
}
}
return std::nullopt;
}