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