CodeCompass

Форк
0
/
filesystem.cpp 
79 строк · 1.9 Кб
1
#include <cstdlib>
2
#include <climits>
3
#include <unistd.h>
4

5
#include <boost/filesystem.hpp>
6

7
#include <util/filesystem.h>
8

9
namespace fs = boost::filesystem;
10

11
namespace cc
12
{
13
namespace util
14
{
15

16
std::string binaryPathToInstallDir(const char* path)
17
{
18
  // Expand the path to a full path as given by the shell. This is then stripped
19
  // of any symbolic links and relative references.
20
  // The path is then parented twice (from binary to "bin" folder, and then
21
  // the root).
22
  // If the path does not exist, the given binary is searched for in the PATH
23
  // environment variable.
24
  // TODO: replace else branch with fs::search_path if Boost 1.64 or above is used.
25

26
  if (fs::exists(fs::system_complete(fs::path(path))))
27
  {
28
    return fs::canonical(fs::system_complete(fs::path(path)))
29
      .parent_path().parent_path().string();
30
  }
31
  else
32
  {
33
    fs::path pPath;
34
    std::string fullPath = std::getenv("PATH");
35
    std::string delimiter = ":";
36

37
    std::size_t pos = 0;
38
    while ((pos = fullPath.find(delimiter)) != std::string::npos)
39
    {
40
      pPath = fs::path(fullPath.substr(0, pos)) / fs::path(path);
41

42
      if (fs::exists(fs::path(pPath)))
43
      {
44
        return fs::canonical(pPath)
45
          .parent_path().parent_path().string();
46
      }
47

48
      fullPath.erase(0, pos + delimiter.length());
49
    }
50
  }
51

52
  throw std::runtime_error(std::string("Could not find ") + path + std::string("."));
53
}
54

55
std::string findCurrentExecutableDir()
56
{
57
  char exePath[PATH_MAX];
58
  ssize_t len = ::readlink("/proc/self/exe", exePath, sizeof(exePath));
59

60
  if (len == -1 || len == sizeof(exePath))
61
    len = 0;
62

63
  exePath[len] = '\0';
64
  return fs::path(exePath).parent_path().string();
65
}
66

67
bool isRootedUnderAnyOf(
68
  const std::vector<std::string>& paths_,
69
  const std::string& path_)
70
{
71
  auto it = paths_.begin();
72
  const auto end = paths_.end();
73
  while (it != end && path_.rfind(*it, 0) != 0)
74
    ++it;
75
  return it != end;
76
}
77

78
} // namespace util
79
} // namespace cc
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.