1 //===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/FileSpec.h"
11 #include "lldb/Utility/RegularExpression.h"
12 #include "lldb/Utility/Stream.h"
13 #include "lldb/Utility/TildeExpressionResolver.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Program.h"
23 #include "llvm/Support/raw_ostream.h"
25 #include <algorithm> // for replace, min, unique
26 #include <system_error> // for error_code
27 #include <vector> // for vector
29 #include <assert.h> // for assert
30 #include <stdio.h> // for size_t, NULL, snpr...
31 #include <string.h> // for strcmp
34 using namespace lldb_private;
38 static constexpr FileSpec::Style GetNativeStyle() {
40 return FileSpec::Style::windows;
42 return FileSpec::Style::posix;
46 bool PathStyleIsPosix(FileSpec::Style style) {
47 return (style == FileSpec::Style::posix ||
48 (style == FileSpec::Style::native &&
49 GetNativeStyle() == FileSpec::Style::posix));
52 const char *GetPathSeparators(FileSpec::Style style) {
53 return llvm::sys::path::get_separator(style).data();
56 char GetPreferredPathSeparator(FileSpec::Style style) {
57 return GetPathSeparators(style)[0];
60 void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
61 if (PathStyleIsPosix(style))
64 std::replace(path.begin(), path.end(), '/', '\\');
67 } // end anonymous namespace
69 void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) {
73 llvm::SmallString<32> Source(path.begin(), path.end());
74 StandardTildeExpressionResolver Resolver;
75 Resolver.ResolveFullPath(Source, path);
77 // Save a copy of the original path that's passed in
78 llvm::SmallString<128> original_path(path.begin(), path.end());
80 llvm::sys::fs::make_absolute(path);
81 if (!llvm::sys::fs::exists(path)) {
83 path.append(original_path.begin(), original_path.end());
87 FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
89 //------------------------------------------------------------------
90 // Default constructor that can take an optional full path to a file on disk.
91 //------------------------------------------------------------------
92 FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, Style style)
94 SetFile(path, resolve_path, style);
97 FileSpec::FileSpec(llvm::StringRef path, bool resolve_path,
98 const llvm::Triple &Triple)
99 : FileSpec{path, resolve_path,
100 Triple.isOSWindows() ? Style::windows : Style::posix} {}
102 //------------------------------------------------------------------
104 //------------------------------------------------------------------
105 FileSpec::FileSpec(const FileSpec &rhs)
106 : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
107 m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
109 //------------------------------------------------------------------
111 //------------------------------------------------------------------
112 FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
117 //------------------------------------------------------------------
118 // Virtual destructor in case anyone inherits from this class.
119 //------------------------------------------------------------------
120 FileSpec::~FileSpec() {}
123 //------------------------------------------------------------------
124 /// Safely get a character at the specified index.
127 /// A full, partial, or relative path to a file.
130 /// An index into path which may or may not be valid.
133 /// The character at index \a i if the index is valid, or 0 if
134 /// the index is not valid.
135 //------------------------------------------------------------------
136 inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) {
142 //------------------------------------------------------------------
143 /// Check if a path needs to be normalized.
145 /// Check if a path needs to be normalized. We currently consider a
146 /// path to need normalization if any of the following are true
147 /// - path contains "/./"
148 /// - path contains "/../"
149 /// - path contains "//"
150 /// - path ends with "/"
151 /// Paths that start with "./" or with "../" are not considered to
152 /// need normalization since we aren't trying to resolve the path,
153 /// we are just trying to remove redundant things from the path.
156 /// A full, partial, or relative path to a file.
159 /// Returns \b true if the path needs to be normalized.
160 //------------------------------------------------------------------
161 bool needsNormalization(const llvm::StringRef &path) {
164 // We strip off leading "." values so these paths need to be normalized
167 for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos;
168 i = path.find_first_of("\\/", i + 1)) {
169 const auto next = safeCharAtIndex(path, i+1);
172 // path separator char at the end of the string which should be
173 // stripped unless it is the one and only character
177 // two path separator chars in the middle of a path needs to be
185 const auto next_next = safeCharAtIndex(path, i+2);
188 case 0: return true; // ends with "/."
191 return true; // contains "/./"
193 const auto next_next_next = safeCharAtIndex(path, i+3);
194 switch (next_next_next) {
196 case 0: return true; // ends with "/.."
199 return true; // contains "/../"
216 //------------------------------------------------------------------
217 // Assignment operator.
218 //------------------------------------------------------------------
219 const FileSpec &FileSpec::operator=(const FileSpec &rhs) {
221 m_directory = rhs.m_directory;
222 m_filename = rhs.m_filename;
223 m_is_resolved = rhs.m_is_resolved;
224 m_style = rhs.m_style;
229 void FileSpec::SetFile(llvm::StringRef pathname, bool resolve) {
230 SetFile(pathname, resolve, m_style);
233 //------------------------------------------------------------------
234 // Update the contents of this object with a new path. The path will be split
235 // up into a directory and filename and stored as uniqued string values for
236 // quick comparison and efficient memory usage.
237 //------------------------------------------------------------------
238 void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, Style style) {
241 m_is_resolved = false;
242 m_style = (style == Style::native) ? GetNativeStyle() : style;
244 if (pathname.empty())
247 llvm::SmallString<64> resolved(pathname);
250 FileSpec::Resolve(resolved);
251 m_is_resolved = true;
254 // Normalize the path by removing ".", ".." and other redundant components.
255 if (needsNormalization(resolved))
256 llvm::sys::path::remove_dots(resolved, true, m_style);
258 // Normalize back slashes to forward slashes
259 if (m_style == Style::windows)
260 std::replace(resolved.begin(), resolved.end(), '\\', '/');
262 if (resolved.empty()) {
263 // If we have no path after normalization set the path to the current
264 // directory. This matches what python does and also a few other path
266 m_filename.SetString(".");
270 // Split path into filename and directory. We rely on the underlying char
271 // pointer to be nullptr when the components are empty.
272 llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style);
273 if(!filename.empty())
274 m_filename.SetString(filename);
275 llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
276 if(!directory.empty())
277 m_directory.SetString(directory);
280 void FileSpec::SetFile(llvm::StringRef path, bool resolve,
281 const llvm::Triple &Triple) {
282 return SetFile(path, resolve,
283 Triple.isOSWindows() ? Style::windows : Style::posix);
286 //----------------------------------------------------------------------
287 // Convert to pointer operator. This allows code to check any FileSpec objects
288 // to see if they contain anything valid using code such as:
292 //----------------------------------------------------------------------
293 FileSpec::operator bool() const { return m_filename || m_directory; }
295 //----------------------------------------------------------------------
296 // Logical NOT operator. This allows code to check any FileSpec objects to see
297 // if they are invalid using code such as:
301 //----------------------------------------------------------------------
302 bool FileSpec::operator!() const { return !m_directory && !m_filename; }
304 bool FileSpec::DirectoryEquals(const FileSpec &rhs) const {
305 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
306 return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive);
309 bool FileSpec::FileEquals(const FileSpec &rhs) const {
310 const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive();
311 return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive);
314 //------------------------------------------------------------------
316 //------------------------------------------------------------------
317 bool FileSpec::operator==(const FileSpec &rhs) const {
318 if (!FileEquals(rhs))
320 if (DirectoryEquals(rhs))
323 // TODO: determine if we want to keep this code in here.
324 // The code below was added to handle a case where we were trying to set a
325 // file and line breakpoint and one path was resolved, and the other not and
326 // the directory was in a mount point that resolved to a more complete path:
327 // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling this out...
328 if (IsResolved() && rhs.IsResolved()) {
329 // Both paths are resolved, no need to look further...
333 FileSpec resolved_lhs(*this);
335 // If "this" isn't resolved, resolve it
337 if (resolved_lhs.ResolvePath()) {
338 // This path wasn't resolved but now it is. Check if the resolved
339 // directory is the same as our unresolved directory, and if so, we can
340 // mark this object as resolved to avoid more future resolves
341 m_is_resolved = (m_directory == resolved_lhs.m_directory);
346 FileSpec resolved_rhs(rhs);
347 if (!rhs.IsResolved()) {
348 if (resolved_rhs.ResolvePath()) {
349 // rhs's path wasn't resolved but now it is. Check if the resolved
350 // directory is the same as rhs's unresolved directory, and if so, we can
351 // mark this object as resolved to avoid more future resolves
352 rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory);
357 // If we reach this point in the code we were able to resolve both paths and
358 // since we only resolve the paths if the basenames are equal, then we can
359 // just check if both directories are equal...
360 return DirectoryEquals(rhs);
363 //------------------------------------------------------------------
364 // Not equal to operator
365 //------------------------------------------------------------------
366 bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
368 //------------------------------------------------------------------
369 // Less than operator
370 //------------------------------------------------------------------
371 bool FileSpec::operator<(const FileSpec &rhs) const {
372 return FileSpec::Compare(*this, rhs, true) < 0;
375 //------------------------------------------------------------------
376 // Dump a FileSpec object to a stream
377 //------------------------------------------------------------------
378 Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
383 //------------------------------------------------------------------
384 // Clear this object by releasing both the directory and filename string values
385 // and making them both the empty string.
386 //------------------------------------------------------------------
387 void FileSpec::Clear() {
392 //------------------------------------------------------------------
393 // Compare two FileSpec objects. If "full" is true, then both the directory and
394 // the filename must match. If "full" is false, then the directory names for
395 // "a" and "b" are only compared if they are both non-empty. This allows a
396 // FileSpec object to only contain a filename and it can match FileSpec objects
397 // that have matching filenames with different paths.
399 // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if
400 // "a" is greater than "b".
401 //------------------------------------------------------------------
402 int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
405 // case sensitivity of compare
406 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
408 // If full is true, then we must compare both the directory and filename.
410 // If full is false, then if either directory is empty, then we match on the
411 // basename only, and if both directories have valid values, we still do a
412 // full compare. This allows for matching when we just have a filename in one
413 // of the FileSpec objects.
415 if (full || (a.m_directory && b.m_directory)) {
416 result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive);
420 return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive);
423 bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
424 // case sensitivity of equality test
425 const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
427 const bool filenames_equal = ConstString::Equals(a.m_filename,
431 if (!filenames_equal)
434 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
435 return filenames_equal;
440 //------------------------------------------------------------------
441 // Dump the object to the supplied stream. If the object contains a valid
442 // directory name, it will be displayed followed by a directory delimiter, and
444 //------------------------------------------------------------------
445 void FileSpec::Dump(Stream *s) const {
447 std::string path{GetPath(true)};
449 char path_separator = GetPreferredPathSeparator(m_style);
450 if (!m_filename && !path.empty() && path.back() != path_separator)
451 s->PutChar(path_separator);
455 //------------------------------------------------------------------
456 // Returns true if the file exists.
457 //------------------------------------------------------------------
458 bool FileSpec::Exists() const { return llvm::sys::fs::exists(GetPath()); }
460 bool FileSpec::Readable() const {
461 return GetPermissions() & llvm::sys::fs::perms::all_read;
464 bool FileSpec::ResolveExecutableLocation() {
465 // CLEANUP: Use StringRef for string handling.
467 const char *file_cstr = m_filename.GetCString();
469 const std::string file_str(file_cstr);
470 llvm::ErrorOr<std::string> error_or_path =
471 llvm::sys::findProgramByName(file_str);
474 std::string path = error_or_path.get();
475 llvm::StringRef dir_ref = llvm::sys::path::parent_path(path);
476 if (!dir_ref.empty()) {
477 // FindProgramByName returns "." if it can't find the file.
478 if (strcmp(".", dir_ref.data()) == 0)
481 m_directory.SetCString(dir_ref.data());
485 // If FindProgramByName found the file, it returns the directory +
486 // filename in its return results. We need to separate them.
487 FileSpec tmp_file(dir_ref.data(), false);
488 if (tmp_file.Exists()) {
489 m_directory = tmp_file.m_directory;
500 bool FileSpec::ResolvePath() {
502 return true; // We have already resolved this path
504 // SetFile(...) will set m_is_resolved correctly if it can resolve the path
505 SetFile(GetPath(false), true);
506 return m_is_resolved;
509 uint64_t FileSpec::GetByteSize() const {
511 if (llvm::sys::fs::file_size(GetPath(), Size))
516 FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
518 uint32_t FileSpec::GetPermissions() const {
519 namespace fs = llvm::sys::fs;
521 if (fs::status(GetPath(), st, false))
522 return fs::perms::perms_not_known;
524 return st.permissions();
527 //------------------------------------------------------------------
528 // Directory string get accessor.
529 //------------------------------------------------------------------
530 ConstString &FileSpec::GetDirectory() { return m_directory; }
532 //------------------------------------------------------------------
533 // Directory string const get accessor.
534 //------------------------------------------------------------------
535 const ConstString &FileSpec::GetDirectory() const { return m_directory; }
537 //------------------------------------------------------------------
538 // Filename string get accessor.
539 //------------------------------------------------------------------
540 ConstString &FileSpec::GetFilename() { return m_filename; }
542 //------------------------------------------------------------------
543 // Filename string const get accessor.
544 //------------------------------------------------------------------
545 const ConstString &FileSpec::GetFilename() const { return m_filename; }
547 //------------------------------------------------------------------
548 // Extract the directory and path into a fixed buffer. This is needed as the
549 // directory and path are stored in separate string values.
550 //------------------------------------------------------------------
551 size_t FileSpec::GetPath(char *path, size_t path_max_len,
552 bool denormalize) const {
556 std::string result = GetPath(denormalize);
557 ::snprintf(path, path_max_len, "%s", result.c_str());
558 return std::min(path_max_len - 1, result.length());
561 std::string FileSpec::GetPath(bool denormalize) const {
562 llvm::SmallString<64> result;
563 GetPath(result, denormalize);
564 return std::string(result.begin(), result.end());
567 const char *FileSpec::GetCString(bool denormalize) const {
568 return ConstString{GetPath(denormalize)}.AsCString(NULL);
571 void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
572 bool denormalize) const {
573 path.append(m_directory.GetStringRef().begin(),
574 m_directory.GetStringRef().end());
575 // Since the path was normalized and all paths use '/' when stored in these
576 // objects, we don't need to look for the actual syntax specific path
577 // separator, we just look for and insert '/'.
578 if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' &&
579 m_filename.GetStringRef().back() != '/')
580 path.insert(path.end(), '/');
581 path.append(m_filename.GetStringRef().begin(),
582 m_filename.GetStringRef().end());
583 if (denormalize && !path.empty())
584 Denormalize(path, m_style);
587 ConstString FileSpec::GetFileNameExtension() const {
589 llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
592 ConstString FileSpec::GetFileNameStrippingExtension() const {
593 return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
596 //------------------------------------------------------------------
597 // Return the size in bytes that this object takes in memory. This returns the
598 // size in bytes of this object, not any shared string values it may refer to.
599 //------------------------------------------------------------------
600 size_t FileSpec::MemorySize() const {
601 return m_filename.MemorySize() + m_directory.MemorySize();
604 void FileSpec::EnumerateDirectory(llvm::StringRef dir_path,
605 bool find_directories, bool find_files,
607 EnumerateDirectoryCallbackType callback,
608 void *callback_baton) {
609 namespace fs = llvm::sys::fs;
611 fs::recursive_directory_iterator Iter(dir_path, EC);
612 fs::recursive_directory_iterator End;
613 for (; Iter != End && !EC; Iter.increment(EC)) {
614 const auto &Item = *Iter;
615 llvm::ErrorOr<fs::basic_file_status> Status = Item.status();
618 if (!find_files && fs::is_regular_file(*Status))
620 if (!find_directories && fs::is_directory(*Status))
622 if (!find_other && fs::is_other(*Status))
625 FileSpec Spec(Item.path(), false);
626 auto Result = callback(callback_baton, Status->type(), Spec);
627 if (Result == eEnumerateDirectoryResultQuit)
629 if (Result == eEnumerateDirectoryResultNext) {
630 // Default behavior is to recurse. Opt out if the callback doesn't want
638 FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
639 FileSpec ret = *this;
640 ret.AppendPathComponent(component);
644 FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
645 llvm::SmallString<64> current_path;
646 GetPath(current_path, false);
647 if (llvm::sys::path::has_parent_path(current_path, m_style))
648 return FileSpec(llvm::sys::path::parent_path(current_path, m_style), false,
653 ConstString FileSpec::GetLastPathComponent() const {
654 llvm::SmallString<64> current_path;
655 GetPath(current_path, false);
656 return ConstString(llvm::sys::path::filename(current_path, m_style));
659 void FileSpec::PrependPathComponent(llvm::StringRef component) {
660 llvm::SmallString<64> new_path(component);
661 llvm::SmallString<64> current_path;
662 GetPath(current_path, false);
663 llvm::sys::path::append(new_path,
664 llvm::sys::path::begin(current_path, m_style),
665 llvm::sys::path::end(current_path), m_style);
666 SetFile(new_path, false, m_style);
669 void FileSpec::PrependPathComponent(const FileSpec &new_path) {
670 return PrependPathComponent(new_path.GetPath(false));
673 void FileSpec::AppendPathComponent(llvm::StringRef component) {
674 llvm::SmallString<64> current_path;
675 GetPath(current_path, false);
676 llvm::sys::path::append(current_path, m_style, component);
677 SetFile(current_path, false, m_style);
680 void FileSpec::AppendPathComponent(const FileSpec &new_path) {
681 return AppendPathComponent(new_path.GetPath(false));
684 bool FileSpec::RemoveLastPathComponent() {
685 llvm::SmallString<64> current_path;
686 GetPath(current_path, false);
687 if (llvm::sys::path::has_parent_path(current_path, m_style)) {
688 SetFile(llvm::sys::path::parent_path(current_path, m_style), false);
693 //------------------------------------------------------------------
694 /// Returns true if the filespec represents an implementation source
695 /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
699 /// \b true if the filespec represents an implementation source
700 /// file, \b false otherwise.
701 //------------------------------------------------------------------
702 bool FileSpec::IsSourceImplementationFile() const {
703 ConstString extension(GetFileNameExtension());
707 static RegularExpression g_source_file_regex(llvm::StringRef(
708 "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
709 "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO]["
710 "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])"
712 return g_source_file_regex.Execute(extension.GetStringRef());
715 bool FileSpec::IsRelative() const {
716 return !IsAbsolute();
719 bool FileSpec::IsAbsolute() const {
720 llvm::SmallString<64> current_path;
721 GetPath(current_path, false);
723 // Early return if the path is empty.
724 if (current_path.empty())
727 // We consider paths starting with ~ to be absolute.
728 if (current_path[0] == '~')
731 return llvm::sys::path::is_absolute(current_path, m_style);
734 void llvm::format_provider<FileSpec>::format(const FileSpec &F,
738 (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
739 "Invalid FileSpec style!");
741 StringRef dir = F.GetDirectory().GetStringRef();
742 StringRef file = F.GetFilename().GetStringRef();
744 if (dir.empty() && file.empty()) {
749 if (Style.equals_lower("F")) {
750 Stream << (file.empty() ? "(empty)" : file);
754 // Style is either D or empty, either way we need to print the directory.
756 // Directory is stored in normalized form, which might be different than
757 // preferred form. In order to handle this, we need to cut off the
758 // filename, then denormalize, then write the entire denorm'ed directory.
759 llvm::SmallString<64> denormalized_dir = dir;
760 Denormalize(denormalized_dir, F.GetPathStyle());
761 Stream << denormalized_dir;
762 Stream << GetPreferredPathSeparator(F.GetPathStyle());
765 if (Style.equals_lower("D")) {
766 // We only want to print the directory, so now just exit.