1 //===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/FileSpec.h"
10 #include "lldb/Utility/RegularExpression.h"
11 #include "lldb/Utility/Stream.h"
12 
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Program.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 #include <algorithm>
24 #include <system_error>
25 #include <vector>
26 
27 #include <assert.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <string.h>
31 
32 using namespace lldb;
33 using namespace lldb_private;
34 
35 namespace {
36 
37 static constexpr FileSpec::Style GetNativeStyle() {
38 #if defined(_WIN32)
39   return FileSpec::Style::windows;
40 #else
41   return FileSpec::Style::posix;
42 #endif
43 }
44 
45 bool PathStyleIsPosix(FileSpec::Style style) {
46   return (style == FileSpec::Style::posix ||
47           (style == FileSpec::Style::native &&
48            GetNativeStyle() == FileSpec::Style::posix));
49 }
50 
51 const char *GetPathSeparators(FileSpec::Style style) {
52   return llvm::sys::path::get_separator(style).data();
53 }
54 
55 char GetPreferredPathSeparator(FileSpec::Style style) {
56   return GetPathSeparators(style)[0];
57 }
58 
59 void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
60   if (PathStyleIsPosix(style))
61     return;
62 
63   std::replace(path.begin(), path.end(), '/', '\\');
64 }
65 
66 } // end anonymous namespace
67 
68 FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
69 
70 // Default constructor that can take an optional full path to a file on disk.
71 FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
72   SetFile(path, style);
73 }
74 
75 FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple)
76     : FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {}
77 
78 // Copy constructor
79 FileSpec::FileSpec(const FileSpec &rhs)
80     : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
81       m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
82 
83 // Copy constructor
84 FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
85   if (rhs)
86     *this = *rhs;
87 }
88 
89 // Virtual destructor in case anyone inherits from this class.
90 FileSpec::~FileSpec() {}
91 
92 namespace {
93 /// Safely get a character at the specified index.
94 ///
95 /// \param[in] path
96 ///     A full, partial, or relative path to a file.
97 ///
98 /// \param[in] i
99 ///     An index into path which may or may not be valid.
100 ///
101 /// \return
102 ///   The character at index \a i if the index is valid, or 0 if
103 ///   the index is not valid.
104 inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
105   if (i < path.size())
106     return path[i];
107   return 0;
108 }
109 
110 /// Check if a path needs to be normalized.
111 ///
112 /// Check if a path needs to be normalized. We currently consider a
113 /// path to need normalization if any of the following are true
114 ///  - path contains "/./"
115 ///  - path contains "/../"
116 ///  - path contains "//"
117 ///  - path ends with "/"
118 /// Paths that start with "./" or with "../" are not considered to
119 /// need normalization since we aren't trying to resolve the path,
120 /// we are just trying to remove redundant things from the path.
121 ///
122 /// \param[in] path
123 ///     A full, partial, or relative path to a file.
124 ///
125 /// \return
126 ///   Returns \b true if the path needs to be normalized.
127 bool needsNormalization(const llvm::StringRef &path) {
128   if (path.empty())
129     return false;
130   // We strip off leading "." values so these paths need to be normalized
131   if (path[0] == '.')
132     return true;
133   for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
134        i = path.find_first_of("\\/", i + 1)) {
135     const auto next = safeCharAtIndex(path, i+1);
136     switch (next) {
137       case 0:
138         // path separator char at the end of the string which should be
139         // stripped unless it is the one and only character
140         return i > 0;
141       case '/':
142       case '\\':
143         // two path separator chars in the middle of a path needs to be
144         // normalized
145         if (i > 0)
146           return true;
147         ++i;
148         break;
149 
150       case '.': {
151           const auto next_next = safeCharAtIndex(path, i+2);
152           switch (next_next) {
153             default: break;
154             case 0: return true; // ends with "/."
155             case '/':
156             case '\\':
157               return true; // contains "/./"
158             case '.': {
159               const auto next_next_next = safeCharAtIndex(path, i+3);
160               switch (next_next_next) {
161                 default: break;
162                 case 0: return true; // ends with "/.."
163                 case '/':
164                 case '\\':
165                   return true; // contains "/../"
166               }
167               break;
168             }
169           }
170         }
171         break;
172 
173       default:
174         break;
175     }
176   }
177   return false;
178 }
179 
180 
181 }
182 // Assignment operator.
183 const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
184   if (this != &rhs) {
185     m_directory = rhs.m_directory;
186     m_filename = rhs.m_filename;
187     m_is_resolved = rhs.m_is_resolved;
188     m_style = rhs.m_style;
189   }
190   return *this;
191 }
192 
193 void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
194 
195 // Update the contents of this object with a new path. The path will be split
196 // up into a directory and filename and stored as uniqued string values for
197 // quick comparison and efficient memory usage.
198 void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
199   m_filename.Clear();
200   m_directory.Clear();
201   m_is_resolved = false;
202   m_style = (style == Style::native) ? GetNativeStyle() : style;
203 
204   if (pathname.empty())
205     return;
206 
207   llvm::SmallString<128> resolved(pathname);
208 
209   // Normalize the path by removing ".", ".." and other redundant components.
210   if (needsNormalization(resolved))
211     llvm::sys::path::remove_dots(resolved, true, m_style);
212 
213   // Normalize back slashes to forward slashes
214   if (m_style == Style::windows)
215     std::replace(resolved.begin(), resolved.end(), '\\', '/');
216 
217   if (resolved.empty()) {
218     // If we have no path after normalization set the path to the current
219     // directory. This matches what python does and also a few other path
220     // utilities.
221     m_filename.SetString(".");
222     return;
223   }
224 
225   // Split path into filename and directory. We rely on the underlying char
226   // pointer to be nullptr when the components are empty.
227   llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
228   if(!filename.empty())
229     m_filename.SetString(filename);
230 
231   llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
232   if(!directory.empty())
233     m_directory.SetString(directory);
234 }
235 
236 void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) {
237   return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix);
238 }
239 
240 // Convert to pointer operator. This allows code to check any FileSpec objects
241 // to see if they contain anything valid using code such as:
242 //
243 //  if (file_spec)
244 //  {}
245 FileSpec::operator bool() const { return m_filename || m_directory; }
246 
247 // Logical NOT operator. This allows code to check any FileSpec objects to see
248 // if they are invalid using code such as:
249 //
250 //  if (!file_spec)
251 //  {}
252 bool FileSpec::operator!() const { return !m_directory && !m_filename; }
253 
254 bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
255   const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
256   return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
257 }
258 
259 bool FileSpec::FileEquals(const FileSpec &rhs) const {
260   const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
261   return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
262 }
263 
264 // Equal to operator
265 bool FileSpec::operator==(const FileSpec &rhs) const {
266   return FileEquals(rhs) && DirectoryEquals(rhs);
267 }
268 
269 // Not equal to operator
270 bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
271 
272 // Less than operator
273 bool FileSpec::operator<(const FileSpec &rhs) const {
274   return FileSpec::Compare(*this, rhs, true) < 0;
275 }
276 
277 // Dump a FileSpec object to a stream
278 Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
279   f.Dump(&s);
280   return s;
281 }
282 
283 // Clear this object by releasing both the directory and filename string values
284 // and making them both the empty string.
285 void FileSpec::Clear() {
286   m_directory.Clear();
287   m_filename.Clear();
288 }
289 
290 // Compare two FileSpec objects. If "full" is true, then both the directory and
291 // the filename must match. If "full" is false, then the directory names for
292 // "a" and "b" are only compared if they are both non-empty. This allows a
293 // FileSpec object to only contain a filename and it can match FileSpec objects
294 // that have matching filenames with different paths.
295 //
296 // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
297 // "a" is greater than "b".
298 int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
299   int result = 0;
300 
301   // case sensitivity of compare
302   const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
303 
304   // If full is true, then we must compare both the directory and filename.
305 
306   // If full is false, then if either directory is empty, then we match on the
307   // basename only, and if both directories have valid values, we still do a
308   // full compare. This allows for matching when we just have a filename in one
309   // of the FileSpec objects.
310 
311   if (full || (a.m_directory && b.m_directory)) {
312     result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
313     if (result)
314       return result;
315   }
316   return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
317 }
318 
319 bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
320   // case sensitivity of equality test
321   const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
322 
323   const bool filenames_equal = ConstString::Equals(a.m_filename,
324                                                    b.m_filename,
325                                                    case_sensitive);
326 
327   if (!filenames_equal)
328     return false;
329 
330   if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
331     return filenames_equal;
332 
333   return a == b;
334 }
335 
336 llvm::Optional<FileSpec::Style> FileSpec::GuessPathStyle(llvm::StringRef absolute_path) {
337   if (absolute_path.startswith("/"))
338     return Style::posix;
339   if (absolute_path.startswith(R"(\\)"))
340     return Style::windows;
341   if (absolute_path.size() > 3 && llvm::isAlpha(absolute_path[0]) &&
342       absolute_path.substr(1, 2) == R"(:\)")
343     return Style::windows;
344   return llvm::None;
345 }
346 
347 // Dump the object to the supplied stream. If the object contains a valid
348 // directory name, it will be displayed followed by a directory delimiter, and
349 // the filename.
350 void FileSpec::Dump(Stream *s) const {
351   if (s) {
352     std::string path{GetPath(true)};
353     s->PutCString(path);
354     char path_separator = GetPreferredPathSeparator(m_style);
355     if (!m_filename && !path.empty() && path.back() != path_separator)
356       s->PutChar(path_separator);
357   }
358 }
359 
360 FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
361 
362 // Directory string get accessor.
363 ConstString &FileSpec::GetDirectory() { return m_directory; }
364 
365 // Directory string const get accessor.
366 ConstString FileSpec::GetDirectory() const { return m_directory; }
367 
368 // Filename string get accessor.
369 ConstString &FileSpec::GetFilename() { return m_filename; }
370 
371 // Filename string const get accessor.
372 ConstString FileSpec::GetFilename() const { return m_filename; }
373 
374 // Extract the directory and path into a fixed buffer. This is needed as the
375 // directory and path are stored in separate string values.
376 size_t FileSpec::GetPath(char *path, size_t path_max_len,
377                          bool denormalize) const {
378   if (!path)
379     return 0;
380 
381   std::string result = GetPath(denormalize);
382   ::snprintf(path, path_max_len, "%s", result.c_str());
383   return std::min(path_max_len - 1, result.length());
384 }
385 
386 std::string FileSpec::GetPath(bool denormalize) const {
387   llvm::SmallString<64> result;
388   GetPath(result, denormalize);
389   return std::string(result.begin(), result.end());
390 }
391 
392 const char *FileSpec::GetCString(bool denormalize) const {
393   return ConstString{GetPath(denormalize)}.AsCString(nullptr);
394 }
395 
396 void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
397                        bool denormalize) const {
398   path.append(m_directory.GetStringRef().begin(),
399               m_directory.GetStringRef().end());
400   // Since the path was normalized and all paths use '/' when stored in these
401   // objects, we don't need to look for the actual syntax specific path
402   // separator, we just look for and insert '/'.
403   if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
404       m_filename.GetStringRef().back() != '/')
405     path.insert(path.end(), '/');
406   path.append(m_filename.GetStringRef().begin(),
407               m_filename.GetStringRef().end());
408   if (denormalize && !path.empty())
409     Denormalize(path, m_style);
410 }
411 
412 ConstString FileSpec::GetFileNameExtension() const {
413   return ConstString(
414       llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
415 }
416 
417 ConstString FileSpec::GetFileNameStrippingExtension() const {
418   return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
419 }
420 
421 // Return the size in bytes that this object takes in memory. This returns the
422 // size in bytes of this object, not any shared string values it may refer to.
423 size_t FileSpec::MemorySize() const {
424   return m_filename.MemorySize() + m_directory.MemorySize();
425 }
426 
427 FileSpec
428 FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
429   FileSpec ret = *this;
430   ret.AppendPathComponent(component);
431   return ret;
432 }
433 
434 FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
435   llvm::SmallString<64> current_path;
436   GetPath(current_path, false);
437   if (llvm::sys::path::has_parent_path(current_path, m_style))
438     return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
439                     m_style);
440   return *this;
441 }
442 
443 ConstString FileSpec::GetLastPathComponent() const {
444   llvm::SmallString<64> current_path;
445   GetPath(current_path, false);
446   return ConstString(llvm::sys::path::filename(current_path, m_style));
447 }
448 
449 void FileSpec::PrependPathComponent(llvm::StringRef component) {
450   llvm::SmallString<64> new_path(component);
451   llvm::SmallString<64> current_path;
452   GetPath(current_path, false);
453   llvm::sys::path::append(new_path,
454                           llvm::sys::path::begin(current_path, m_style),
455                           llvm::sys::path::end(current_path), m_style);
456   SetFile(new_path, m_style);
457 }
458 
459 void FileSpec::PrependPathComponent(const FileSpec &new_path) {
460   return PrependPathComponent(new_path.GetPath(false));
461 }
462 
463 void FileSpec::AppendPathComponent(llvm::StringRef component) {
464   llvm::SmallString<64> current_path;
465   GetPath(current_path, false);
466   llvm::sys::path::append(current_path, m_style, component);
467   SetFile(current_path, m_style);
468 }
469 
470 void FileSpec::AppendPathComponent(const FileSpec &new_path) {
471   return AppendPathComponent(new_path.GetPath(false));
472 }
473 
474 bool FileSpec::RemoveLastPathComponent() {
475   llvm::SmallString<64> current_path;
476   GetPath(current_path, false);
477   if (llvm::sys::path::has_parent_path(current_path, m_style)) {
478     SetFile(llvm::sys::path::parent_path(current_path, m_style));
479     return true;
480   }
481   return false;
482 }
483 /// Returns true if the filespec represents an implementation source
484 /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
485 /// extension).
486 ///
487 /// \return
488 ///     \b true if the filespec represents an implementation source
489 ///     file, \b false otherwise.
490 bool FileSpec::IsSourceImplementationFile() const {
491   ConstString extension(GetFileNameExtension());
492   if (!extension)
493     return false;
494 
495   static RegularExpression g_source_file_regex(llvm::StringRef(
496       "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
497       "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
498       "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
499       "$"));
500   return g_source_file_regex.Execute(extension.GetStringRef());
501 }
502 
503 bool FileSpec::IsRelative() const {
504   return !IsAbsolute();
505 }
506 
507 bool FileSpec::IsAbsolute() const {
508   llvm::SmallString<64> current_path;
509   GetPath(current_path, false);
510 
511   // Early return if the path is empty.
512   if (current_path.empty())
513     return false;
514 
515   // We consider paths starting with ~ to be absolute.
516   if (current_path[0] == '~')
517     return true;
518 
519   return llvm::sys::path::is_absolute(current_path, m_style);
520 }
521 
522 void FileSpec::MakeAbsolute(const FileSpec &dir) {
523   if (IsRelative())
524     PrependPathComponent(dir);
525 }
526 
527 void llvm::format_provider<FileSpec>::format(const FileSpec &F,
528                                              raw_ostream &Stream,
529                                              StringRef Style) {
530   assert(
531       (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
532       "Invalid FileSpec style!");
533 
534   StringRef dir = F.GetDirectory().GetStringRef();
535   StringRef file = F.GetFilename().GetStringRef();
536 
537   if (dir.empty() && file.empty()) {
538     Stream << "(empty)";
539     return;
540   }
541 
542   if (Style.equals_lower("F")) {
543     Stream << (file.empty() ? "(empty)" : file);
544     return;
545   }
546 
547   // Style is either D or empty, either way we need to print the directory.
548   if (!dir.empty()) {
549     // Directory is stored in normalized form, which might be different than
550     // preferred form.  In order to handle this, we need to cut off the
551     // filename, then denormalize, then write the entire denorm'ed directory.
552     llvm::SmallString<64> denormalized_dir = dir;
553     Denormalize(denormalized_dir, F.GetPathStyle());
554     Stream << denormalized_dir;
555     Stream << GetPreferredPathSeparator(F.GetPathStyle());
556   }
557 
558   if (Style.equals_lower("D")) {
559     // We only want to print the directory, so now just exit.
560     if (dir.empty())
561       Stream << "(empty)";
562     return;
563   }
564 
565   if (!file.empty())
566     Stream << file;
567 }
568