brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · f6e1d17 Raw
72 lines · cpp
1//===-- RealpathPrefixes.cpp ----------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Utility/RealpathPrefixes.h"10 11#include "lldb/Utility/FileSpec.h"12#include "lldb/Utility/FileSpecList.h"13#include "lldb/Utility/LLDBLog.h"14#include "lldb/Utility/Log.h"15#include "lldb/lldb-private-types.h"16 17using namespace lldb_private;18 19RealpathPrefixes::RealpathPrefixes(20    const FileSpecList &file_spec_list,21    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)22    : m_fs(fs) {23  m_prefixes.reserve(file_spec_list.GetSize());24  for (const FileSpec &file_spec : file_spec_list) {25    m_prefixes.emplace_back(file_spec.GetPath());26  }27}28 29std::optional<FileSpec>30RealpathPrefixes::ResolveSymlinks(const FileSpec &file_spec) {31  if (m_prefixes.empty())32    return std::nullopt;33 34  // Test if `b` is a *path* prefix of `a` (not just *string* prefix).35  // E.g. "/foo/bar" is a path prefix of "/foo/bar/baz" but not "/foo/barbaz".36  auto is_path_prefix = [](llvm::StringRef a, llvm::StringRef b,37                           bool case_sensitive,38                           llvm::sys::path::Style style) -> bool {39    if (case_sensitive ? a.consume_front(b) : a.consume_front_insensitive(b))40      // If `b` isn't "/", then it won't end with "/" because it comes from41      // `FileSpec`. After `a` consumes `b`, `a` should either be empty (i.e.42      // `a` == `b`) or end with "/" (the remainder of `a` is a subdirectory).43      return b == "/" || a.empty() ||44             llvm::sys::path::is_separator(a[0], style);45    return false;46  };47  std::string file_spec_path = file_spec.GetPath();48  for (const std::string &prefix : m_prefixes) {49    if (is_path_prefix(file_spec_path, prefix, file_spec.IsCaseSensitive(),50                       file_spec.GetPathStyle())) {51      // Stats and logging.52      IncreaseSourceRealpathAttemptCount();53      Log *log = GetLog(LLDBLog::Source);54      LLDB_LOGF(log, "Realpath'ing support file %s", file_spec_path.c_str());55 56      // One prefix matched. Try to realpath.57      PathSmallString buff;58      std::error_code ec = m_fs->getRealPath(file_spec_path, buff);59      if (ec)60        return std::nullopt;61      FileSpec realpath(buff, file_spec.GetPathStyle());62 63      // Only return realpath if it is different from the original file_spec.64      if (realpath != file_spec)65        return realpath;66      return std::nullopt;67    }68  }69  // No prefix matched70  return std::nullopt;71}72