CMS 3D CMS Logo

resolveSymbolicLinks.cc
Go to the documentation of this file.
3 
4 #include <filesystem>
5 #include <vector>
6 
7 namespace edm {
8 
9  namespace {
10  namespace bf = std::filesystem;
11  bool resolveOneSymbolicLink(std::string& fullPath) {
12  if (fullPath.empty())
13  return false;
14  if (fullPath[0] != '/')
15  return false;
16  std::string pathToResolve;
17  std::vector<std::string> pathElements = edm::tokenize(fullPath, "/");
18  for (auto const& path : pathElements) {
19  if (!path.empty()) {
20  pathToResolve += "/";
21  pathToResolve += path;
22  bf::path symLinkPath(pathToResolve);
23  if (bf::is_symlink(bf::symlink_status(symLinkPath))) {
24  bf::path resolved = bf::read_symlink(symLinkPath);
25  // This check is needed because in weird filesystems
26  // (e.g. AFS), the resolved link may not be accessible.
27  if (!bf::exists(resolved)) {
28  continue;
29  }
30  std::string resolvedPath = resolved.string();
31  auto begin = fullPath.begin();
32  auto end = begin + pathToResolve.size();
33  // resolvedPath may or may not contain the leading "/".
34  if (resolvedPath[0] == '/') {
35  fullPath.replace(begin, end, resolvedPath);
36  } else {
37  fullPath.replace(begin + 1, end, resolvedPath);
38  }
39  return true;
40  }
41  }
42  }
43  return false;
44  }
45  } // namespace
46 
47  // Resolves symlinks recursively from anywhere in fullPath.
49  bool found = resolveOneSymbolicLink(fullPath);
50  if (found) {
52  }
53  }
54 } // namespace edm
std::vector< std::string > tokenize(std::string const &input, std::string const &separator)
breaks the input string into tokens, delimited by the separator
Definition: Parse.cc:52
HLT enums.
void resolveSymbolicLinks(std::string &fullPath)