1 //===-- PathMappingList.cpp -------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // C Includes 11 #include <limits.h> 12 #include <string.h> 13 14 // C++ Includes 15 // Other libraries and framework includes 16 // Project includes 17 #include "lldb/Core/Error.h" 18 #include "lldb/Core/Stream.h" 19 #include "lldb/Host/FileSpec.h" 20 #include "lldb/Target/PathMappingList.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 //---------------------------------------------------------------------- 26 // PathMappingList constructor 27 //---------------------------------------------------------------------- 28 PathMappingList::PathMappingList () : 29 m_pairs (), 30 m_callback (NULL), 31 m_callback_baton (NULL) 32 { 33 } 34 35 PathMappingList::PathMappingList 36 ( 37 ChangedCallback callback, 38 void *callback_baton 39 ) : 40 m_pairs (), 41 m_callback (callback), 42 m_callback_baton (callback_baton) 43 { 44 } 45 46 47 PathMappingList::PathMappingList (const PathMappingList &rhs) : 48 m_pairs (rhs.m_pairs), 49 m_callback (NULL), 50 m_callback_baton (NULL) 51 { 52 53 } 54 55 const PathMappingList & 56 PathMappingList::operator =(const PathMappingList &rhs) 57 { 58 if (this != &rhs) 59 { 60 m_pairs = rhs.m_pairs; 61 m_callback = NULL; 62 m_callback_baton = NULL; 63 } 64 return *this; 65 } 66 67 68 //---------------------------------------------------------------------- 69 // Destructor 70 //---------------------------------------------------------------------- 71 PathMappingList::~PathMappingList () 72 { 73 } 74 75 void 76 PathMappingList::Append (const ConstString &path, 77 const ConstString &replacement, 78 bool notify) 79 { 80 m_pairs.push_back(pair(path, replacement)); 81 if (notify && m_callback) 82 m_callback (*this, m_callback_baton); 83 } 84 85 void 86 PathMappingList::Append (const PathMappingList &rhs, bool notify) 87 { 88 if (!rhs.m_pairs.empty()) 89 { 90 const_iterator pos, end = rhs.m_pairs.end(); 91 for (pos = rhs.m_pairs.begin(); pos != end; ++pos) 92 m_pairs.push_back(*pos); 93 if (notify && m_callback) 94 m_callback (*this, m_callback_baton); 95 } 96 } 97 98 void 99 PathMappingList::Insert (const ConstString &path, 100 const ConstString &replacement, 101 uint32_t index, 102 bool notify) 103 { 104 iterator insert_iter; 105 if (index >= m_pairs.size()) 106 insert_iter = m_pairs.end(); 107 else 108 insert_iter = m_pairs.begin() + index; 109 m_pairs.insert(insert_iter, pair(path, replacement)); 110 if (notify && m_callback) 111 m_callback (*this, m_callback_baton); 112 } 113 114 bool 115 PathMappingList::Replace (const ConstString &path, 116 const ConstString &replacement, 117 uint32_t index, 118 bool notify) 119 { 120 iterator insert_iter; 121 if (index >= m_pairs.size()) 122 return false; 123 m_pairs[index] = pair(path, replacement); 124 if (notify && m_callback) 125 m_callback (*this, m_callback_baton); 126 return true; 127 } 128 129 bool 130 PathMappingList::Remove (off_t index, bool notify) 131 { 132 if (index >= m_pairs.size()) 133 return false; 134 135 iterator iter = m_pairs.begin() + index; 136 m_pairs.erase(iter); 137 if (notify && m_callback) 138 m_callback (*this, m_callback_baton); 139 return true; 140 } 141 142 // For clients which do not need the pair index dumped, pass a pair_index >= 0 143 // to only dump the indicated pair. 144 void 145 PathMappingList::Dump (Stream *s, int pair_index) 146 { 147 unsigned int numPairs = m_pairs.size(); 148 149 if (pair_index < 0) 150 { 151 unsigned int index; 152 for (index = 0; index < numPairs; ++index) 153 s->Printf("[%d] \"%s\" -> \"%s\"\n", 154 index, m_pairs[index].first.GetCString(), m_pairs[index].second.GetCString()); 155 } 156 else 157 { 158 if (pair_index < numPairs) 159 s->Printf("%s -> %s", 160 m_pairs[pair_index].first.GetCString(), m_pairs[pair_index].second.GetCString()); 161 } 162 } 163 164 void 165 PathMappingList::Clear (bool notify) 166 { 167 m_pairs.clear(); 168 if (notify && m_callback) 169 m_callback (*this, m_callback_baton); 170 } 171 172 bool 173 PathMappingList::RemapPath (const ConstString &path, ConstString &new_path) const 174 { 175 const_iterator pos, end = m_pairs.end(); 176 for (pos = m_pairs.begin(); pos != end; ++pos) 177 { 178 const size_t prefixLen = pos->first.GetLength(); 179 180 if (::strncmp (pos->first.GetCString(), path.GetCString(), prefixLen) == 0) 181 { 182 std::string new_path_str (pos->second.GetCString()); 183 new_path_str.append(path.GetCString() + prefixLen); 184 new_path.SetCString(new_path_str.c_str()); 185 return true; 186 } 187 } 188 return false; 189 } 190 191 bool 192 PathMappingList::RemapPath (const char *path, std::string &new_path) const 193 { 194 if (m_pairs.empty() || path == NULL || path[0] == '\0') 195 return false; 196 197 const_iterator pos, end = m_pairs.end(); 198 for (pos = m_pairs.begin(); pos != end; ++pos) 199 { 200 const size_t prefix_len = pos->first.GetLength(); 201 202 if (::strncmp (pos->first.GetCString(), path, prefix_len) == 0) 203 { 204 new_path = pos->second.GetCString(); 205 new_path.append(path + prefix_len); 206 return true; 207 } 208 } 209 return false; 210 } 211 212 bool 213 PathMappingList::FindFile (const FileSpec &orig_spec, FileSpec &new_spec) const 214 { 215 if (!m_pairs.empty()) 216 { 217 char orig_path[PATH_MAX]; 218 char new_path[PATH_MAX]; 219 const size_t orig_path_len = orig_spec.GetPath (orig_path, sizeof(orig_path)); 220 if (orig_path_len > 0) 221 { 222 const_iterator pos, end = m_pairs.end(); 223 for (pos = m_pairs.begin(); pos != end; ++pos) 224 { 225 const size_t prefix_len = pos->first.GetLength(); 226 227 if (orig_path_len >= prefix_len) 228 { 229 if (::strncmp (pos->first.GetCString(), orig_path, prefix_len) == 0) 230 { 231 const size_t new_path_len = snprintf(new_path, sizeof(new_path), "%s/%s", pos->second.GetCString(), orig_path + prefix_len); 232 if (new_path_len < sizeof(new_path)) 233 { 234 new_spec.SetFile (new_path, true); 235 if (new_spec.Exists()) 236 return true; 237 } 238 } 239 } 240 } 241 } 242 } 243 new_spec.Clear(); 244 return false; 245 } 246 247 bool 248 PathMappingList::Replace (const ConstString &path, const ConstString &new_path, bool notify) 249 { 250 uint32_t idx = FindIndexForPath (path); 251 if (idx < m_pairs.size()) 252 { 253 m_pairs[idx].second = new_path; 254 if (notify && m_callback) 255 m_callback (*this, m_callback_baton); 256 return true; 257 } 258 return false; 259 } 260 261 bool 262 PathMappingList::Remove (const ConstString &path, bool notify) 263 { 264 iterator pos = FindIteratorForPath (path); 265 if (pos != m_pairs.end()) 266 { 267 m_pairs.erase (pos); 268 if (notify && m_callback) 269 m_callback (*this, m_callback_baton); 270 return true; 271 } 272 return false; 273 } 274 275 PathMappingList::const_iterator 276 PathMappingList::FindIteratorForPath (const ConstString &path) const 277 { 278 const_iterator pos; 279 const_iterator begin = m_pairs.begin(); 280 const_iterator end = m_pairs.end(); 281 282 for (pos = begin; pos != end; ++pos) 283 { 284 if (pos->first == path) 285 break; 286 } 287 return pos; 288 } 289 290 PathMappingList::iterator 291 PathMappingList::FindIteratorForPath (const ConstString &path) 292 { 293 iterator pos; 294 iterator begin = m_pairs.begin(); 295 iterator end = m_pairs.end(); 296 297 for (pos = begin; pos != end; ++pos) 298 { 299 if (pos->first == path) 300 break; 301 } 302 return pos; 303 } 304 305 bool 306 PathMappingList::GetPathsAtIndex (uint32_t idx, ConstString &path, ConstString &new_path) const 307 { 308 if (idx < m_pairs.size()) 309 { 310 path = m_pairs[idx].first; 311 new_path = m_pairs[idx].second; 312 return true; 313 } 314 return false; 315 } 316 317 318 319 uint32_t 320 PathMappingList::FindIndexForPath (const ConstString &path) const 321 { 322 const_iterator pos; 323 const_iterator begin = m_pairs.begin(); 324 const_iterator end = m_pairs.end(); 325 326 for (pos = begin; pos != end; ++pos) 327 { 328 if (pos->first == path) 329 return std::distance (begin, pos); 330 } 331 return UINT32_MAX; 332 } 333 334