CMS 3D CMS Logo

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