HPCToolkit
RealPathMgr.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 //************************* System Include Files ****************************
61 
62 #include <string>
63 using std::string;
64 
65 
66 //*************************** User Include Files ****************************
67 
68 #include <include/gcc-attr.h>
69 
70 #include "Logic.hpp"
71 #include "RealPathMgr.hpp"
72 #include "PathReplacementMgr.hpp"
73 #include "PathFindMgr.hpp"
74 #include "StrUtil.hpp"
75 
76 #include "diagnostics.h"
77 #include "realpath.h"
78 
79 //*************************** Forward Declarations **************************
80 
81 //***************************************************************************
82 
83 //***************************************************************************
84 // RealPathMgr
85 //***************************************************************************
86 
88 
89 
90 // Constructor with static singleton objects for PathFindMgr and
91 // PathReplacementMgr.
93 {
96 }
97 
98 
99 // Constructor with params for PathFindMgr and PathReplacementMgr to
100 // use instead of singletons.
102 {
103  m_pathFindMgr = findMgr;
104  m_pathReplaceMgr = replaceMgr;
105 }
106 
107 
108 // Delete path manager dependencies if non-null.
110 {
111  if (m_pathFindMgr != NULL) {
112  delete m_pathFindMgr;
113  }
114  if (m_pathReplaceMgr != NULL) {
115  delete m_pathReplaceMgr;
116  }
117 }
118 
119 
121 {
122  return s_singleton;
123 }
124 
125 
126 bool
127 RealPathMgr::realpath(string& pathNm) const
128 {
129  if (pathNm.empty()) {
130  return false;
131  }
132 
133  // INVARIANT: 'pathNm' is not empty
134 
135  // INVARIANT: all entries in the map are non-empty
136  MyMap::iterator it = m_cache.find(pathNm);
137 
138  // -------------------------------------------------------
139  // 1. Check cache for 'pathNm'
140  // -------------------------------------------------------
141  if (it != m_cache.end()) {
142  // use cached value
143  const string& pathNm_real = it->second;
144  if (pathNm_real[0] == '/') { // optimization: only copy if fully resolved
145  pathNm = pathNm_real;
146  }
147  }
148  else {
149  // -------------------------------------------------------
150  // 2. Consult cache with path-replaced 'pathNm'
151  // -------------------------------------------------------
152  string pathNm_orig = pathNm;
153 
154  if (m_pathReplaceMgr != NULL) {
155  pathNm = m_pathReplaceMgr->replace(pathNm);
156  }
157  else {
158  pathNm = PathReplacementMgr::singleton().replace(pathNm);
159  }
160 
161  it = m_cache.find(pathNm);
162 
163  if (it != m_cache.end()) {
164  // use cached value
165  const string& pathNm_real = it->second;
166  if (pathNm_real[0] == '/') { // optimization: only copy if fully resolved
167  pathNm = pathNm_real;
168  }
169 
170  // since 'pathNm_orig' was not in map, ensure it is
171  m_cache.insert(make_pair(pathNm_orig, pathNm_real));
172  }
173  else {
174  // -------------------------------------------------------
175  // 3. Resolve 'pathNm' using PathFindMgr or realpath
176  // -------------------------------------------------------
177  string pathNm_real = pathNm;
178 
179  if (m_searchPaths.empty()) {
180  pathNm_real = RealPath(pathNm.c_str());
181  }
182  else {
183  const char* pathNm_pf;
184 
185  if (m_pathFindMgr != NULL) {
186  pathNm_pf =
187  m_pathFindMgr->pathfind(m_searchPaths.c_str(), pathNm.c_str(), "r");
188  }
189  else {
190  pathNm_pf =
191  PathFindMgr::singleton().pathfind(m_searchPaths.c_str(), pathNm.c_str(), "r");
192  }
193  if (pathNm_pf) {
194  pathNm_real = pathNm_pf;
195  }
196  }
197 
198  pathNm = pathNm_real;
199  m_cache.insert(make_pair(pathNm_orig, pathNm_real));
200  }
201  }
202  return (pathNm[0] == '/'); // fully resolved
203 }
204 
205 
206 void
207 RealPathMgr::searchPaths(const string& pathsStr)
208 {
209  std::vector<std::string> searchPathVec;
210  StrUtil::tokenize_str(pathsStr, ":", searchPathVec);
211 
212  // INVARIANT: m_searchPaths is non-empty
213  m_searchPaths += "."; // current working directory
214 
215  for (uint i = 0; i < searchPathVec.size(); ++i) {
216  string path = searchPathVec[i];
217 
218  if (PathFindMgr::isRecursivePath(path.c_str())) {
219  path = path.substr(0, path.length() - PathFindMgr::RecursivePathSfxLn);
220  path = RealPath(path.c_str());
221  path += "/*";
222  }
223  else if (path != ".") {
224  path = RealPath(path.c_str());
225  }
226 
227  m_searchPaths += ":" + path;
228  }
229 }
230 
231 
232 //***************************************************************************
233 
234 string
236 {
237  std::ostringstream os;
238  dump(os, flags);
239  return os.str();
240 }
241 
242 
243 std::ostream&
244 RealPathMgr::dump(std::ostream& os, uint GCC_ATTR_UNUSED flags,
245  const char* pfx) const
246 {
247  os << pfx << "[ RealPathMgr:" << std::endl;
248  for (MyMap::const_iterator it = m_cache.begin(); it != m_cache.end(); ++it) {
249  const string& x = it->first;
250  const string& y = it->second;
251  os << pfx << " " << x << " => " << y << std::endl;
252  }
253  os << pfx << "]" << std::endl;
254 
255  os.flush();
256  return os;
257 }
258 
259 
260 void
262 {
263  dump(std::cerr, flags);
264 }
265 
std::string replace(const std::string &path) const
static int isRecursivePath(const char *path)
static PathFindMgr & singleton()
const char * pathfind(const char *pathList, const char *name, const char *mode)
void ddump(uint flags=0) const
static RealPathMgr & singleton()
void tokenize_str(const std::string &tokenstr, const char *delim, std::vector< std::string > &tokenvec)
Definition: StrUtil.cpp:122
PathReplacementMgr * m_pathReplaceMgr
bool realpath(std::string &pathNm) const
unsigned int uint
Definition: uint.h:124
static PathReplacementMgr & singleton()
std::string m_searchPaths
static RealPathMgr s_singleton
Definition: RealPathMgr.cpp:87
std::string toString(uint flags=0) const
PathFindMgr * m_pathFindMgr
static const int RecursivePathSfxLn
Definition: PathFindMgr.hpp:86
const std::string & searchPaths() const
std::ostream & dump(std::ostream &os, uint flags=0, const char *pfx="") const
const char * RealPath(const char *nm)
Definition: realpath.c:69
#define NULL
Definition: ElfHelper.cpp:85
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80