Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:21

0001 #include "FWCore/Utilities/interface/resolveSymbolicLinks.h"
0002 #include "FWCore/Utilities/interface/Parse.h"
0003 
0004 #include <filesystem>
0005 #include <vector>
0006 
0007 namespace edm {
0008 
0009   namespace {
0010     namespace bf = std::filesystem;
0011     bool resolveOneSymbolicLink(std::string& fullPath) {
0012       if (fullPath.empty())
0013         return false;
0014       if (fullPath[0] != '/')
0015         return false;
0016       std::string pathToResolve;
0017       std::vector<std::string> pathElements = edm::tokenize(fullPath, "/");
0018       for (auto const& path : pathElements) {
0019         if (!path.empty()) {
0020           pathToResolve += "/";
0021           pathToResolve += path;
0022           bf::path symLinkPath(pathToResolve);
0023           if (bf::is_symlink(bf::symlink_status(symLinkPath))) {
0024             bf::path resolved = bf::read_symlink(symLinkPath);
0025             // This check is needed because in weird filesystems
0026             // (e.g. AFS), the resolved link may not be accessible.
0027             if (!bf::exists(resolved)) {
0028               continue;
0029             }
0030             std::string resolvedPath = resolved.string();
0031             auto begin = fullPath.begin();
0032             auto end = begin + pathToResolve.size();
0033             // resolvedPath may or may not contain the leading "/".
0034             if (resolvedPath[0] == '/') {
0035               fullPath.replace(begin, end, resolvedPath);
0036             } else {
0037               fullPath.replace(begin + 1, end, resolvedPath);
0038             }
0039             return true;
0040           }
0041         }
0042       }
0043       return false;
0044     }
0045   }  // namespace
0046 
0047   // Resolves symlinks recursively from anywhere in fullPath.
0048   void resolveSymbolicLinks(std::string& fullPath) {
0049     bool found = resolveOneSymbolicLink(fullPath);
0050     if (found) {
0051       resolveSymbolicLinks(fullPath);
0052     }
0053   }
0054 }  // namespace edm