Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- FileSpec.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 | |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 10 | #ifndef _WIN32 |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 11 | #include <dirent.h> |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 12 | #else |
| 13 | #include "lldb/Host/windows/windows.h" |
| 14 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include <fcntl.h> |
Virgile Bello | 6957195 | 2013-09-20 22:35:22 +0000 | [diff] [blame] | 16 | #ifndef _MSC_VER |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include <libgen.h> |
Virgile Bello | 6957195 | 2013-09-20 22:35:22 +0000 | [diff] [blame] | 18 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 19 | #include <fstream> |
Rafael Espindola | 0907916 | 2013-06-13 20:10:23 +0000 | [diff] [blame] | 20 | #include <set> |
Greg Clayton | e0f3c02 | 2011-02-07 17:41:11 +0000 | [diff] [blame] | 21 | #include <string.h> |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 22 | |
Jim Ingham | 9035e7c | 2011-02-07 19:42:39 +0000 | [diff] [blame] | 23 | #include "lldb/Host/Config.h" // Have to include this before we test the define... |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 24 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 25 | #include <pwd.h> |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 26 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
Chaoren Lin | f34f410 | 2015-05-09 01:21:32 +0000 | [diff] [blame] | 28 | #include "lldb/Core/ArchSpec.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 29 | #include "lldb/Core/DataBufferHeap.h" |
| 30 | #include "lldb/Core/DataBufferMemoryMap.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 31 | #include "lldb/Host/File.h" |
| 32 | #include "lldb/Host/FileSpec.h" |
| 33 | #include "lldb/Host/FileSystem.h" |
| 34 | #include "lldb/Host/Host.h" |
| 35 | #include "lldb/Utility/CleanUp.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame^] | 36 | #include "lldb/Utility/RegularExpression.h" |
| 37 | #include "lldb/Utility/Stream.h" |
| 38 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 39 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/StringExtras.h" |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 42 | #include "llvm/Support/ConvertUTF.h" |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 43 | #include "llvm/Support/FileSystem.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Path.h" |
| 45 | #include "llvm/Support/Program.h" |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | using namespace lldb; |
| 48 | using namespace lldb_private; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | bool PathSyntaxIsPosix(FileSpec::PathSyntax syntax) { |
| 53 | return (syntax == FileSpec::ePathSyntaxPosix || |
| 54 | (syntax == FileSpec::ePathSyntaxHostNative && |
| 55 | FileSystem::GetNativePathSyntax() == FileSpec::ePathSyntaxPosix)); |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | const char *GetPathSeparators(FileSpec::PathSyntax syntax) { |
| 59 | return PathSyntaxIsPosix(syntax) ? "/" : "\\/"; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 62 | char GetPreferredPathSeparator(FileSpec::PathSyntax syntax) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | return GetPathSeparators(syntax)[0]; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | bool IsPathSeparator(char value, FileSpec::PathSyntax syntax) { |
| 67 | return value == '/' || (!PathSyntaxIsPosix(syntax) && value == '\\'); |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | void Normalize(llvm::SmallVectorImpl<char> &path, FileSpec::PathSyntax syntax) { |
| 71 | if (PathSyntaxIsPosix(syntax)) |
| 72 | return; |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | std::replace(path.begin(), path.end(), '\\', '/'); |
| 75 | // Windows path can have \\ slashes which can be changed by replace |
| 76 | // call above to //. Here we remove the duplicate. |
| 77 | auto iter = std::unique(path.begin(), path.end(), [](char &c1, char &c2) { |
| 78 | return (c1 == '/' && c2 == '/'); |
| 79 | }); |
| 80 | path.erase(iter, path.end()); |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | void Denormalize(llvm::SmallVectorImpl<char> &path, |
| 84 | FileSpec::PathSyntax syntax) { |
| 85 | if (PathSyntaxIsPosix(syntax)) |
| 86 | return; |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | std::replace(path.begin(), path.end(), '/', '\\'); |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | bool GetFileStats(const FileSpec *file_spec, struct stat *stats_ptr) { |
| 92 | char resolved_path[PATH_MAX]; |
| 93 | if (file_spec->GetPath(resolved_path, sizeof(resolved_path))) |
| 94 | return FileSystem::Stat(resolved_path, stats_ptr) == 0; |
| 95 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | size_t FilenamePos(llvm::StringRef str, FileSpec::PathSyntax syntax) { |
| 99 | if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1]) |
| 100 | return 0; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | if (str.size() > 0 && IsPathSeparator(str.back(), syntax)) |
| 103 | return str.size() - 1; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 104 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | size_t pos = str.find_last_of(GetPathSeparators(syntax), str.size() - 1); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | if (!PathSyntaxIsPosix(syntax) && pos == llvm::StringRef::npos) |
| 108 | pos = str.find_last_of(':', str.size() - 2); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | if (pos == llvm::StringRef::npos || |
| 111 | (pos == 1 && IsPathSeparator(str[0], syntax))) |
| 112 | return 0; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | return pos + 1; |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | size_t RootDirStart(llvm::StringRef str, FileSpec::PathSyntax syntax) { |
| 118 | // case "c:/" |
| 119 | if (!PathSyntaxIsPosix(syntax) && |
| 120 | (str.size() > 2 && str[1] == ':' && IsPathSeparator(str[2], syntax))) |
| 121 | return 2; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 122 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | // case "//" |
| 124 | if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1]) |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 125 | return llvm::StringRef::npos; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | |
| 127 | // case "//net" |
| 128 | if (str.size() > 3 && IsPathSeparator(str[0], syntax) && str[0] == str[1] && |
| 129 | !IsPathSeparator(str[2], syntax)) |
| 130 | return str.find_first_of(GetPathSeparators(syntax), 2); |
| 131 | |
| 132 | // case "/" |
| 133 | if (str.size() > 0 && IsPathSeparator(str[0], syntax)) |
| 134 | return 0; |
| 135 | |
| 136 | return llvm::StringRef::npos; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | size_t ParentPathEnd(llvm::StringRef path, FileSpec::PathSyntax syntax) { |
| 140 | size_t end_pos = FilenamePos(path, syntax); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | bool filename_was_sep = |
| 143 | path.size() > 0 && IsPathSeparator(path[end_pos], syntax); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 144 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | // Skip separators except for root dir. |
| 146 | size_t root_dir_pos = RootDirStart(path.substr(0, end_pos), syntax); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | while (end_pos > 0 && (end_pos - 1) != root_dir_pos && |
| 149 | IsPathSeparator(path[end_pos - 1], syntax)) |
| 150 | --end_pos; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep) |
| 153 | return llvm::StringRef::npos; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | return end_pos; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // end anonymous namespace |
| 159 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 160 | // Resolves the username part of a path of the form ~user/other/directories, and |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | // writes the result into dst_path. This will also resolve "~" to the current |
| 162 | // user. |
| 163 | // If you want to complete "~" to the list of users, pass it to |
| 164 | // ResolvePartialUsername. |
| 165 | void FileSpec::ResolveUsername(llvm::SmallVectorImpl<char> &path) { |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 166 | #if LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | if (path.empty() || path[0] != '~') |
| 168 | return; |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | llvm::StringRef path_str(path.data(), path.size()); |
| 171 | size_t slash_pos = path_str.find('/', 1); |
| 172 | if (slash_pos == 1 || path.size() == 1) { |
| 173 | // A path of ~/ resolves to the current user's home dir |
| 174 | llvm::SmallString<64> home_dir; |
| 175 | // llvm::sys::path::home_directory() only checks if "HOME" is set in the |
| 176 | // environment and does nothing else to locate the user home directory |
| 177 | if (!llvm::sys::path::home_directory(home_dir)) { |
| 178 | struct passwd *pw = getpwuid(getuid()); |
| 179 | if (pw && pw->pw_dir && pw->pw_dir[0]) { |
| 180 | // Update our environemnt so llvm::sys::path::home_directory() works |
| 181 | // next time |
| 182 | setenv("HOME", pw->pw_dir, 0); |
| 183 | home_dir.assign(llvm::StringRef(pw->pw_dir)); |
| 184 | } else { |
| 185 | return; |
| 186 | } |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 187 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | |
| 189 | // Overwrite the ~ with the first character of the homedir, and insert |
| 190 | // the rest. This way we only trigger one move, whereas an insert |
| 191 | // followed by a delete (or vice versa) would trigger two. |
| 192 | path[0] = home_dir[0]; |
| 193 | path.insert(path.begin() + 1, home_dir.begin() + 1, home_dir.end()); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | auto username_begin = path.begin() + 1; |
| 198 | auto username_end = (slash_pos == llvm::StringRef::npos) |
| 199 | ? path.end() |
| 200 | : (path.begin() + slash_pos); |
| 201 | size_t replacement_length = std::distance(path.begin(), username_end); |
| 202 | |
| 203 | llvm::SmallString<20> username(username_begin, username_end); |
| 204 | struct passwd *user_entry = ::getpwnam(username.c_str()); |
| 205 | if (user_entry != nullptr) { |
| 206 | // Copy over the first n characters of the path, where n is the smaller of |
| 207 | // the length |
| 208 | // of the home directory and the slash pos. |
| 209 | llvm::StringRef homedir(user_entry->pw_dir); |
| 210 | size_t initial_copy_length = std::min(homedir.size(), replacement_length); |
| 211 | auto src_begin = homedir.begin(); |
| 212 | auto src_end = src_begin + initial_copy_length; |
| 213 | std::copy(src_begin, src_end, path.begin()); |
| 214 | if (replacement_length > homedir.size()) { |
| 215 | // We copied the entire home directory, but the ~username portion of the |
| 216 | // path was |
| 217 | // longer, so there's characters that need to be removed. |
| 218 | path.erase(path.begin() + initial_copy_length, username_end); |
| 219 | } else if (replacement_length < homedir.size()) { |
| 220 | // We copied all the way up to the slash in the destination, but there's |
| 221 | // still more |
| 222 | // characters that need to be inserted. |
| 223 | path.insert(username_end, src_end, homedir.end()); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 224 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | } else { |
| 226 | // Unable to resolve username (user doesn't exist?) |
| 227 | path.clear(); |
| 228 | } |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 229 | #endif |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 232 | size_t FileSpec::ResolvePartialUsername(llvm::StringRef partial_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | StringList &matches) { |
Jim Ingham | 8436307 | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 234 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | size_t extant_entries = matches.GetSize(); |
| 236 | |
| 237 | setpwent(); |
| 238 | struct passwd *user_entry; |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 239 | partial_name = partial_name.drop_front(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 240 | std::set<std::string> name_list; |
| 241 | |
| 242 | while ((user_entry = getpwent()) != NULL) { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 243 | if (llvm::StringRef(user_entry->pw_name).startswith(partial_name)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 244 | std::string tmp_buf("~"); |
| 245 | tmp_buf.append(user_entry->pw_name); |
| 246 | tmp_buf.push_back('/'); |
| 247 | name_list.insert(tmp_buf); |
Jim Ingham | 8436307 | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 248 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | } |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 250 | |
| 251 | for (auto &name : name_list) { |
| 252 | matches.AppendString(name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | } |
| 254 | return matches.GetSize() - extant_entries; |
Jim Ingham | 8436307 | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 255 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | // Resolving home directories is not supported, just copy the path... |
| 257 | return 0; |
| 258 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Jim Ingham | 8436307 | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 261 | void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) { |
| 262 | if (path.empty()) |
| 263 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 265 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 266 | if (path[0] == '~') |
| 267 | ResolveUsername(path); |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 268 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | // Save a copy of the original path that's passed in |
| 271 | llvm::SmallString<128> original_path(path.begin(), path.end()); |
Jason Molenda | 671a29d | 2015-02-25 02:35:25 +0000 | [diff] [blame] | 272 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | llvm::sys::fs::make_absolute(path); |
| 274 | if (!llvm::sys::fs::exists(path)) { |
| 275 | path.clear(); |
| 276 | path.append(original_path.begin(), original_path.end()); |
| 277 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Sam McCall | 6f43d9d | 2016-11-15 10:58:16 +0000 | [diff] [blame] | 280 | FileSpec::FileSpec() : m_syntax(FileSystem::GetNativePathSyntax()) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | |
| 282 | //------------------------------------------------------------------ |
| 283 | // Default constructor that can take an optional full path to a |
| 284 | // file on disk. |
| 285 | //------------------------------------------------------------------ |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 286 | FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, PathSyntax syntax) |
Sam McCall | 6f43d9d | 2016-11-15 10:58:16 +0000 | [diff] [blame] | 287 | : m_syntax(syntax) { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 288 | SetFile(path, resolve_path, syntax); |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 291 | FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, ArchSpec arch) |
| 292 | : FileSpec{path, resolve_path, arch.GetTriple().isOSWindows() |
| 293 | ? ePathSyntaxWindows |
| 294 | : ePathSyntaxPosix} {} |
Chaoren Lin | f34f410 | 2015-05-09 01:21:32 +0000 | [diff] [blame] | 295 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 296 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 297 | // Copy constructor |
| 298 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 299 | FileSpec::FileSpec(const FileSpec &rhs) |
| 300 | : m_directory(rhs.m_directory), m_filename(rhs.m_filename), |
| 301 | m_is_resolved(rhs.m_is_resolved), m_syntax(rhs.m_syntax) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | |
| 303 | //------------------------------------------------------------------ |
| 304 | // Copy constructor |
| 305 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 306 | FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() { |
| 307 | if (rhs) |
| 308 | *this = *rhs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | //------------------------------------------------------------------ |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 312 | // Virtual destructor in case anyone inherits from this class. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 314 | FileSpec::~FileSpec() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | |
| 316 | //------------------------------------------------------------------ |
| 317 | // Assignment operator. |
| 318 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 319 | const FileSpec &FileSpec::operator=(const FileSpec &rhs) { |
| 320 | if (this != &rhs) { |
| 321 | m_directory = rhs.m_directory; |
| 322 | m_filename = rhs.m_filename; |
| 323 | m_is_resolved = rhs.m_is_resolved; |
| 324 | m_syntax = rhs.m_syntax; |
| 325 | } |
| 326 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 329 | //------------------------------------------------------------------ |
| 330 | // Update the contents of this object with a new path. The path will |
| 331 | // be split up into a directory and filename and stored as uniqued |
| 332 | // string values for quick comparison and efficient memory usage. |
| 333 | //------------------------------------------------------------------ |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 334 | void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, |
| 335 | PathSyntax syntax) { |
| 336 | // CLEANUP: Use StringRef for string handling. This function is kind of a |
| 337 | // mess and the unclear semantics of RootDirStart and ParentPathEnd make |
| 338 | // it very difficult to understand this function. There's no reason this |
| 339 | // function should be particularly complicated or difficult to understand. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 340 | m_filename.Clear(); |
| 341 | m_directory.Clear(); |
| 342 | m_is_resolved = false; |
| 343 | m_syntax = (syntax == ePathSyntaxHostNative) |
| 344 | ? FileSystem::GetNativePathSyntax() |
| 345 | : syntax; |
Zachary Turner | df62f20 | 2014-08-07 17:33:07 +0000 | [diff] [blame] | 346 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 347 | if (pathname.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 349 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | llvm::SmallString<64> resolved(pathname); |
Zachary Turner | df62f20 | 2014-08-07 17:33:07 +0000 | [diff] [blame] | 351 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 352 | if (resolve) { |
| 353 | FileSpec::Resolve(resolved); |
| 354 | m_is_resolved = true; |
| 355 | } |
Zachary Turner | c7a17ed | 2014-12-01 23:13:32 +0000 | [diff] [blame] | 356 | |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 357 | Normalize(resolved, m_syntax); |
Pavel Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 358 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 359 | llvm::StringRef resolve_path_ref(resolved.c_str()); |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 360 | size_t dir_end = ParentPathEnd(resolve_path_ref, m_syntax); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | if (dir_end == 0) { |
| 362 | m_filename.SetString(resolve_path_ref); |
| 363 | return; |
| 364 | } |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | m_directory.SetString(resolve_path_ref.substr(0, dir_end)); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 367 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 368 | size_t filename_begin = dir_end; |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 369 | size_t root_dir_start = RootDirStart(resolve_path_ref, m_syntax); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 370 | while (filename_begin != llvm::StringRef::npos && |
| 371 | filename_begin < resolve_path_ref.size() && |
| 372 | filename_begin != root_dir_start && |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 373 | IsPathSeparator(resolve_path_ref[filename_begin], m_syntax)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 374 | ++filename_begin; |
| 375 | m_filename.SetString((filename_begin == llvm::StringRef::npos || |
| 376 | filename_begin >= resolve_path_ref.size()) |
| 377 | ? "." |
| 378 | : resolve_path_ref.substr(filename_begin)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 381 | void FileSpec::SetFile(llvm::StringRef path, bool resolve, ArchSpec arch) { |
| 382 | return SetFile(path, resolve, arch.GetTriple().isOSWindows() |
| 383 | ? ePathSyntaxWindows |
| 384 | : ePathSyntaxPosix); |
Chaoren Lin | 44145d7 | 2015-05-29 19:52:37 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 387 | //---------------------------------------------------------------------- |
| 388 | // Convert to pointer operator. This allows code to check any FileSpec |
| 389 | // objects to see if they contain anything valid using code such as: |
| 390 | // |
| 391 | // if (file_spec) |
| 392 | // {} |
| 393 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | FileSpec::operator bool() const { return m_filename || m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | |
| 396 | //---------------------------------------------------------------------- |
| 397 | // Logical NOT operator. This allows code to check any FileSpec |
| 398 | // objects to see if they are invalid using code such as: |
| 399 | // |
| 400 | // if (!file_spec) |
| 401 | // {} |
| 402 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 403 | bool FileSpec::operator!() const { return !m_directory && !m_filename; } |
| 404 | |
| 405 | bool FileSpec::DirectoryEquals(const FileSpec &rhs) const { |
| 406 | const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); |
| 407 | return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | bool FileSpec::FileEquals(const FileSpec &rhs) const { |
| 411 | const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); |
| 412 | return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 415 | //------------------------------------------------------------------ |
| 416 | // Equal to operator |
| 417 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | bool FileSpec::operator==(const FileSpec &rhs) const { |
| 419 | if (!FileEquals(rhs)) |
| 420 | return false; |
| 421 | if (DirectoryEquals(rhs)) |
| 422 | return true; |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 423 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 424 | // TODO: determine if we want to keep this code in here. |
| 425 | // The code below was added to handle a case where we were |
| 426 | // trying to set a file and line breakpoint and one path |
| 427 | // was resolved, and the other not and the directory was |
| 428 | // in a mount point that resolved to a more complete path: |
| 429 | // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling |
| 430 | // this out... |
| 431 | if (IsResolved() && rhs.IsResolved()) { |
| 432 | // Both paths are resolved, no need to look further... |
| 433 | return false; |
| 434 | } |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 435 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 436 | FileSpec resolved_lhs(*this); |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 437 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | // If "this" isn't resolved, resolve it |
| 439 | if (!IsResolved()) { |
| 440 | if (resolved_lhs.ResolvePath()) { |
| 441 | // This path wasn't resolved but now it is. Check if the resolved |
| 442 | // directory is the same as our unresolved directory, and if so, |
| 443 | // we can mark this object as resolved to avoid more future resolves |
| 444 | m_is_resolved = (m_directory == resolved_lhs.m_directory); |
| 445 | } else |
| 446 | return false; |
| 447 | } |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 448 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | FileSpec resolved_rhs(rhs); |
| 450 | if (!rhs.IsResolved()) { |
| 451 | if (resolved_rhs.ResolvePath()) { |
| 452 | // rhs's path wasn't resolved but now it is. Check if the resolved |
| 453 | // directory is the same as rhs's unresolved directory, and if so, |
| 454 | // we can mark this object as resolved to avoid more future resolves |
| 455 | rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory); |
| 456 | } else |
| 457 | return false; |
| 458 | } |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 459 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 460 | // If we reach this point in the code we were able to resolve both paths |
| 461 | // and since we only resolve the paths if the basenames are equal, then |
| 462 | // we can just check if both directories are equal... |
| 463 | return DirectoryEquals(rhs); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | //------------------------------------------------------------------ |
| 467 | // Not equal to operator |
| 468 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 469 | bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 470 | |
| 471 | //------------------------------------------------------------------ |
| 472 | // Less than operator |
| 473 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 474 | bool FileSpec::operator<(const FileSpec &rhs) const { |
| 475 | return FileSpec::Compare(*this, rhs, true) < 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | //------------------------------------------------------------------ |
| 479 | // Dump a FileSpec object to a stream |
| 480 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) { |
| 482 | f.Dump(&s); |
| 483 | return s; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | //------------------------------------------------------------------ |
| 487 | // Clear this object by releasing both the directory and filename |
| 488 | // string values and making them both the empty string. |
| 489 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 490 | void FileSpec::Clear() { |
| 491 | m_directory.Clear(); |
| 492 | m_filename.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | //------------------------------------------------------------------ |
| 496 | // Compare two FileSpec objects. If "full" is true, then both |
| 497 | // the directory and the filename must match. If "full" is false, |
| 498 | // then the directory names for "a" and "b" are only compared if |
| 499 | // they are both non-empty. This allows a FileSpec object to only |
| 500 | // contain a filename and it can match FileSpec objects that have |
| 501 | // matching filenames with different paths. |
| 502 | // |
| 503 | // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" |
| 504 | // and "1" if "a" is greater than "b". |
| 505 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 506 | int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) { |
| 507 | int result = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 508 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 509 | // case sensitivity of compare |
| 510 | const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 511 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 512 | // If full is true, then we must compare both the directory and filename. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 513 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 514 | // If full is false, then if either directory is empty, then we match on |
| 515 | // the basename only, and if both directories have valid values, we still |
| 516 | // do a full compare. This allows for matching when we just have a filename |
| 517 | // in one of the FileSpec objects. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 518 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | if (full || (a.m_directory && b.m_directory)) { |
| 520 | result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive); |
| 521 | if (result) |
| 522 | return result; |
| 523 | } |
| 524 | return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 527 | bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full, |
| 528 | bool remove_backups) { |
| 529 | // case sensitivity of equality test |
| 530 | const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 531 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 532 | if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty())) |
| 533 | return ConstString::Equals(a.m_filename, b.m_filename, case_sensitive); |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 534 | |
| 535 | if (remove_backups == false) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 536 | return a == b; |
Jim Ingham | 96a1596 | 2014-11-15 01:54:26 +0000 | [diff] [blame] | 537 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 538 | if (a == b) |
| 539 | return true; |
| 540 | |
| 541 | return Equal(a.GetNormalizedPath(), b.GetNormalizedPath(), full, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 544 | FileSpec FileSpec::GetNormalizedPath() const { |
| 545 | // Fast path. Do nothing if the path is not interesting. |
| 546 | if (!m_directory.GetStringRef().contains(".") && |
Pavel Labath | e6e7e6c | 2016-11-30 16:08:45 +0000 | [diff] [blame] | 547 | !m_directory.GetStringRef().contains("//") && |
| 548 | m_filename.GetStringRef() != ".." && m_filename.GetStringRef() != ".") |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 549 | return *this; |
Greg Clayton | 5a27195 | 2015-06-02 22:43:29 +0000 | [diff] [blame] | 550 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 551 | llvm::SmallString<64> path, result; |
| 552 | const bool normalize = false; |
| 553 | GetPath(path, normalize); |
| 554 | llvm::StringRef rest(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 555 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 556 | // We will not go below root dir. |
| 557 | size_t root_dir_start = RootDirStart(path, m_syntax); |
| 558 | const bool absolute = root_dir_start != llvm::StringRef::npos; |
| 559 | if (absolute) { |
| 560 | result += rest.take_front(root_dir_start + 1); |
| 561 | rest = rest.drop_front(root_dir_start + 1); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 562 | } else { |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 563 | if (m_syntax == ePathSyntaxWindows && path.size() > 2 && path[1] == ':') { |
| 564 | result += rest.take_front(2); |
| 565 | rest = rest.drop_front(2); |
| 566 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 569 | bool anything_added = false; |
| 570 | llvm::SmallVector<llvm::StringRef, 0> components, processed; |
| 571 | rest.split(components, '/', -1, false); |
| 572 | processed.reserve(components.size()); |
| 573 | for (auto component : components) { |
| 574 | if (component == ".") |
| 575 | continue; // Skip these. |
| 576 | if (component != "..") { |
| 577 | processed.push_back(component); |
| 578 | continue; // Regular file name. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 579 | } |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 580 | if (!processed.empty()) { |
| 581 | processed.pop_back(); |
| 582 | continue; // Dots. Go one level up if we can. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 583 | } |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 584 | if (absolute) |
| 585 | continue; // We're at the top level. Cannot go higher than that. Skip. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 586 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 587 | result += component; // We're a relative path. We need to keep these. |
| 588 | result += '/'; |
| 589 | anything_added = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | } |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 591 | for (auto component : processed) { |
| 592 | result += component; |
| 593 | result += '/'; |
| 594 | anything_added = true; |
| 595 | } |
| 596 | if (anything_added) |
| 597 | result.pop_back(); // Pop last '/'. |
| 598 | else if (result.empty()) |
| 599 | result = "."; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 600 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 601 | return FileSpec(result, false, m_syntax); |
Jim Ingham | 96a1596 | 2014-11-15 01:54:26 +0000 | [diff] [blame] | 602 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | |
| 604 | //------------------------------------------------------------------ |
| 605 | // Dump the object to the supplied stream. If the object contains |
| 606 | // a valid directory name, it will be displayed followed by a |
| 607 | // directory delimiter, and the filename. |
| 608 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 609 | void FileSpec::Dump(Stream *s) const { |
| 610 | if (s) { |
| 611 | std::string path{GetPath(true)}; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 612 | s->PutCString(path); |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 613 | char path_separator = GetPreferredPathSeparator(m_syntax); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 614 | if (!m_filename && !path.empty() && path.back() != path_separator) |
| 615 | s->PutChar(path_separator); |
| 616 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | //------------------------------------------------------------------ |
| 620 | // Returns true if the file exists. |
| 621 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 622 | bool FileSpec::Exists() const { |
| 623 | struct stat file_stats; |
| 624 | return GetFileStats(this, &file_stats); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 627 | bool FileSpec::Readable() const { |
| 628 | const uint32_t permissions = GetPermissions(); |
| 629 | if (permissions & eFilePermissionsEveryoneR) |
| 630 | return true; |
| 631 | return false; |
Greg Clayton | 5acc125 | 2014-08-15 18:00:45 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 634 | bool FileSpec::ResolveExecutableLocation() { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 635 | // CLEANUP: Use StringRef for string handling. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 636 | if (!m_directory) { |
| 637 | const char *file_cstr = m_filename.GetCString(); |
| 638 | if (file_cstr) { |
| 639 | const std::string file_str(file_cstr); |
| 640 | llvm::ErrorOr<std::string> error_or_path = |
| 641 | llvm::sys::findProgramByName(file_str); |
| 642 | if (!error_or_path) |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 643 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 644 | std::string path = error_or_path.get(); |
| 645 | llvm::StringRef dir_ref = llvm::sys::path::parent_path(path); |
| 646 | if (!dir_ref.empty()) { |
| 647 | // FindProgramByName returns "." if it can't find the file. |
| 648 | if (strcmp(".", dir_ref.data()) == 0) |
| 649 | return false; |
| 650 | |
| 651 | m_directory.SetCString(dir_ref.data()); |
| 652 | if (Exists()) |
| 653 | return true; |
| 654 | else { |
| 655 | // If FindProgramByName found the file, it returns the directory + |
| 656 | // filename in its return results. |
| 657 | // We need to separate them. |
| 658 | FileSpec tmp_file(dir_ref.data(), false); |
| 659 | if (tmp_file.Exists()) { |
| 660 | m_directory = tmp_file.m_directory; |
| 661 | return true; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return false; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 671 | bool FileSpec::ResolvePath() { |
| 672 | if (m_is_resolved) |
| 673 | return true; // We have already resolved this path |
| 674 | |
| 675 | char path_buf[PATH_MAX]; |
| 676 | if (!GetPath(path_buf, PATH_MAX, false)) |
| 677 | return false; |
| 678 | // SetFile(...) will set m_is_resolved correctly if it can resolve the path |
| 679 | SetFile(path_buf, true); |
| 680 | return m_is_resolved; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 683 | uint64_t FileSpec::GetByteSize() const { |
| 684 | struct stat file_stats; |
| 685 | if (GetFileStats(this, &file_stats)) |
| 686 | return file_stats.st_size; |
| 687 | return 0; |
Zachary Turner | df62f20 | 2014-08-07 17:33:07 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 690 | FileSpec::PathSyntax FileSpec::GetPathSyntax() const { return m_syntax; } |
| 691 | |
| 692 | FileSpec::FileType FileSpec::GetFileType() const { |
| 693 | struct stat file_stats; |
| 694 | if (GetFileStats(this, &file_stats)) { |
| 695 | mode_t file_type = file_stats.st_mode & S_IFMT; |
| 696 | switch (file_type) { |
| 697 | case S_IFDIR: |
| 698 | return eFileTypeDirectory; |
| 699 | case S_IFREG: |
| 700 | return eFileTypeRegular; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 701 | #ifndef _WIN32 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 702 | case S_IFIFO: |
| 703 | return eFileTypePipe; |
| 704 | case S_IFSOCK: |
| 705 | return eFileTypeSocket; |
| 706 | case S_IFLNK: |
| 707 | return eFileTypeSymbolicLink; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame] | 708 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 709 | default: |
| 710 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 711 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 712 | return eFileTypeUnknown; |
| 713 | } |
| 714 | return eFileTypeInvalid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 717 | bool FileSpec::IsSymbolicLink() const { |
| 718 | char resolved_path[PATH_MAX]; |
| 719 | if (!GetPath(resolved_path, sizeof(resolved_path))) |
| 720 | return false; |
Oleksiy Vyalov | 8a578bf | 2015-07-21 01:28:22 +0000 | [diff] [blame] | 721 | |
| 722 | #ifdef _WIN32 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | std::wstring wpath; |
| 724 | if (!llvm::ConvertUTF8toWide(resolved_path, wpath)) |
| 725 | return false; |
| 726 | auto attrs = ::GetFileAttributesW(wpath.c_str()); |
| 727 | if (attrs == INVALID_FILE_ATTRIBUTES) |
| 728 | return false; |
Oleksiy Vyalov | 8a578bf | 2015-07-21 01:28:22 +0000 | [diff] [blame] | 729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 730 | return (attrs & FILE_ATTRIBUTE_REPARSE_POINT); |
Oleksiy Vyalov | 8a578bf | 2015-07-21 01:28:22 +0000 | [diff] [blame] | 731 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 732 | struct stat file_stats; |
| 733 | if (::lstat(resolved_path, &file_stats) != 0) |
| 734 | return false; |
Oleksiy Vyalov | 8a578bf | 2015-07-21 01:28:22 +0000 | [diff] [blame] | 735 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 736 | return (file_stats.st_mode & S_IFMT) == S_IFLNK; |
Oleksiy Vyalov | 8a578bf | 2015-07-21 01:28:22 +0000 | [diff] [blame] | 737 | #endif |
| 738 | } |
| 739 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 740 | uint32_t FileSpec::GetPermissions() const { |
| 741 | uint32_t file_permissions = 0; |
| 742 | if (*this) |
| 743 | FileSystem::GetFilePermissions(*this, file_permissions); |
| 744 | return file_permissions; |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 747 | //------------------------------------------------------------------ |
| 748 | // Directory string get accessor. |
| 749 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 750 | ConstString &FileSpec::GetDirectory() { return m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 751 | |
| 752 | //------------------------------------------------------------------ |
| 753 | // Directory string const get accessor. |
| 754 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 755 | const ConstString &FileSpec::GetDirectory() const { return m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 756 | |
| 757 | //------------------------------------------------------------------ |
| 758 | // Filename string get accessor. |
| 759 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 760 | ConstString &FileSpec::GetFilename() { return m_filename; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 761 | |
| 762 | //------------------------------------------------------------------ |
| 763 | // Filename string const get accessor. |
| 764 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 765 | const ConstString &FileSpec::GetFilename() const { return m_filename; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 766 | |
| 767 | //------------------------------------------------------------------ |
| 768 | // Extract the directory and path into a fixed buffer. This is |
| 769 | // needed as the directory and path are stored in separate string |
| 770 | // values. |
| 771 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 772 | size_t FileSpec::GetPath(char *path, size_t path_max_len, |
| 773 | bool denormalize) const { |
| 774 | if (!path) |
| 775 | return 0; |
Zachary Turner | b6d9924 | 2014-08-08 23:54:35 +0000 | [diff] [blame] | 776 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 777 | std::string result = GetPath(denormalize); |
| 778 | ::snprintf(path, path_max_len, "%s", result.c_str()); |
| 779 | return std::min(path_max_len - 1, result.length()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 782 | std::string FileSpec::GetPath(bool denormalize) const { |
| 783 | llvm::SmallString<64> result; |
| 784 | GetPath(result, denormalize); |
| 785 | return std::string(result.begin(), result.end()); |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 788 | const char *FileSpec::GetCString(bool denormalize) const { |
| 789 | return ConstString{GetPath(denormalize)}.AsCString(NULL); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 792 | void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, |
| 793 | bool denormalize) const { |
| 794 | path.append(m_directory.GetStringRef().begin(), |
| 795 | m_directory.GetStringRef().end()); |
| 796 | if (m_directory && m_filename && |
| 797 | !IsPathSeparator(m_directory.GetStringRef().back(), m_syntax)) |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 798 | path.insert(path.end(), GetPreferredPathSeparator(m_syntax)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 799 | path.append(m_filename.GetStringRef().begin(), |
| 800 | m_filename.GetStringRef().end()); |
| 801 | Normalize(path, m_syntax); |
| 802 | if (denormalize && !path.empty()) |
| 803 | Denormalize(path, m_syntax); |
Zachary Turner | 4e8ddf5 | 2015-04-09 18:08:50 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 806 | ConstString FileSpec::GetFileNameExtension() const { |
| 807 | if (m_filename) { |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 808 | const char *filename = m_filename.GetCString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 809 | const char *dot_pos = strrchr(filename, '.'); |
| 810 | if (dot_pos && dot_pos[1] != '\0') |
| 811 | return ConstString(dot_pos + 1); |
| 812 | } |
| 813 | return ConstString(); |
| 814 | } |
| 815 | |
| 816 | ConstString FileSpec::GetFileNameStrippingExtension() const { |
| 817 | const char *filename = m_filename.GetCString(); |
| 818 | if (filename == NULL) |
| 819 | return ConstString(); |
| 820 | |
| 821 | const char *dot_pos = strrchr(filename, '.'); |
| 822 | if (dot_pos == NULL) |
| 823 | return m_filename; |
| 824 | |
| 825 | return ConstString(filename, dot_pos - filename); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 828 | //------------------------------------------------------------------ |
| 829 | // Returns a shared pointer to a data buffer that contains all or |
| 830 | // part of the contents of a file. The data is memory mapped and |
| 831 | // will lazily page in data from the file as memory is accessed. |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 832 | // The data that is mapped will start "file_offset" bytes into the |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 833 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 834 | // greater than the number of bytes available in the file starting |
| 835 | // at "file_offset", the number of bytes will be appropriately |
| 836 | // truncated. The final number of bytes that get mapped can be |
| 837 | // verified using the DataBuffer::GetByteSize() function. |
| 838 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 839 | DataBufferSP FileSpec::MemoryMapFileContents(off_t file_offset, |
| 840 | size_t file_size) const { |
| 841 | DataBufferSP data_sp; |
| 842 | std::unique_ptr<DataBufferMemoryMap> mmap_data(new DataBufferMemoryMap()); |
| 843 | if (mmap_data.get()) { |
| 844 | const size_t mapped_length = |
| 845 | mmap_data->MemoryMapFromFileSpec(this, file_offset, file_size); |
| 846 | if (((file_size == SIZE_MAX) && (mapped_length > 0)) || |
| 847 | (mapped_length >= file_size)) |
| 848 | data_sp.reset(mmap_data.release()); |
| 849 | } |
| 850 | return data_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 853 | DataBufferSP FileSpec::MemoryMapFileContentsIfLocal(off_t file_offset, |
| 854 | size_t file_size) const { |
| 855 | if (FileSystem::IsLocal(*this)) |
| 856 | return MemoryMapFileContents(file_offset, file_size); |
| 857 | else |
| 858 | return ReadFileContents(file_offset, file_size, NULL); |
Greg Clayton | 736888c | 2015-02-23 23:47:09 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 861 | //------------------------------------------------------------------ |
| 862 | // Return the size in bytes that this object takes in memory. This |
| 863 | // returns the size in bytes of this object, not any shared string |
| 864 | // values it may refer to. |
| 865 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 866 | size_t FileSpec::MemorySize() const { |
| 867 | return m_filename.MemorySize() + m_directory.MemorySize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 870 | size_t FileSpec::ReadFileContents(off_t file_offset, void *dst, size_t dst_len, |
| 871 | Error *error_ptr) const { |
| 872 | Error error; |
| 873 | size_t bytes_read = 0; |
| 874 | char resolved_path[PATH_MAX]; |
| 875 | if (GetPath(resolved_path, sizeof(resolved_path))) { |
| 876 | File file; |
| 877 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 878 | if (error.Success()) { |
| 879 | off_t file_offset_after_seek = file_offset; |
| 880 | bytes_read = dst_len; |
| 881 | error = file.Read(dst, bytes_read, file_offset_after_seek); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 882 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 883 | } else { |
| 884 | error.SetErrorString("invalid file specification"); |
| 885 | } |
| 886 | if (error_ptr) |
| 887 | *error_ptr = error; |
| 888 | return bytes_read; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | //------------------------------------------------------------------ |
| 892 | // Returns a shared pointer to a data buffer that contains all or |
| 893 | // part of the contents of a file. The data copies into a heap based |
| 894 | // buffer that lives in the DataBuffer shared pointer object returned. |
| 895 | // The data that is cached will start "file_offset" bytes into the |
| 896 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 897 | // greater than the number of bytes available in the file starting |
| 898 | // at "file_offset", the number of bytes will be appropriately |
| 899 | // truncated. The final number of bytes that get mapped can be |
| 900 | // verified using the DataBuffer::GetByteSize() function. |
| 901 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 902 | DataBufferSP FileSpec::ReadFileContents(off_t file_offset, size_t file_size, |
| 903 | Error *error_ptr) const { |
| 904 | Error error; |
| 905 | DataBufferSP data_sp; |
| 906 | char resolved_path[PATH_MAX]; |
| 907 | if (GetPath(resolved_path, sizeof(resolved_path))) { |
| 908 | File file; |
| 909 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 910 | if (error.Success()) { |
| 911 | const bool null_terminate = false; |
| 912 | error = file.Read(file_size, file_offset, null_terminate, data_sp); |
Greg Clayton | 0b0b512 | 2012-08-30 18:15:10 +0000 | [diff] [blame] | 913 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 914 | } else { |
| 915 | error.SetErrorString("invalid file specification"); |
| 916 | } |
| 917 | if (error_ptr) |
| 918 | *error_ptr = error; |
| 919 | return data_sp; |
Greg Clayton | 0b0b512 | 2012-08-30 18:15:10 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 922 | DataBufferSP FileSpec::ReadFileContentsAsCString(Error *error_ptr) { |
| 923 | Error error; |
| 924 | DataBufferSP data_sp; |
| 925 | char resolved_path[PATH_MAX]; |
| 926 | if (GetPath(resolved_path, sizeof(resolved_path))) { |
| 927 | File file; |
| 928 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 929 | if (error.Success()) { |
| 930 | off_t offset = 0; |
| 931 | size_t length = SIZE_MAX; |
| 932 | const bool null_terminate = true; |
| 933 | error = file.Read(length, offset, null_terminate, data_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 934 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 935 | } else { |
| 936 | error.SetErrorString("invalid file specification"); |
| 937 | } |
| 938 | if (error_ptr) |
| 939 | *error_ptr = error; |
| 940 | return data_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 943 | size_t FileSpec::ReadFileLines(STLStringArray &lines) { |
| 944 | lines.clear(); |
| 945 | char path[PATH_MAX]; |
| 946 | if (GetPath(path, sizeof(path))) { |
| 947 | std::ifstream file_stream(path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 948 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 949 | if (file_stream) { |
| 950 | std::string line; |
| 951 | while (getline(file_stream, line)) |
| 952 | lines.push_back(line); |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 953 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 954 | } |
| 955 | return lines.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 956 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 957 | |
| 958 | FileSpec::EnumerateDirectoryResult |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 959 | FileSpec::ForEachItemInDirectory(llvm::StringRef dir_path, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 960 | DirectoryCallback const &callback) { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 961 | if (dir_path.empty()) |
| 962 | return eEnumerateDirectoryResultNext; |
| 963 | |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 964 | #ifdef _WIN32 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 965 | std::string szDir(dir_path); |
| 966 | szDir += "\\*"; |
Greg Clayton | 58c65f0 | 2015-06-29 18:29:00 +0000 | [diff] [blame] | 967 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 968 | std::wstring wszDir; |
| 969 | if (!llvm::ConvertUTF8toWide(szDir, wszDir)) { |
| 970 | return eEnumerateDirectoryResultNext; |
Greg Clayton | 58c65f0 | 2015-06-29 18:29:00 +0000 | [diff] [blame] | 971 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 972 | |
| 973 | WIN32_FIND_DATAW ffd; |
| 974 | HANDLE hFind = FindFirstFileW(wszDir.c_str(), &ffd); |
| 975 | |
| 976 | if (hFind == INVALID_HANDLE_VALUE) { |
| 977 | return eEnumerateDirectoryResultNext; |
| 978 | } |
| 979 | |
| 980 | do { |
| 981 | FileSpec::FileType file_type = eFileTypeUnknown; |
| 982 | if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 983 | size_t len = wcslen(ffd.cFileName); |
| 984 | |
| 985 | if (len == 1 && ffd.cFileName[0] == L'.') |
| 986 | continue; |
| 987 | |
| 988 | if (len == 2 && ffd.cFileName[0] == L'.' && ffd.cFileName[1] == L'.') |
| 989 | continue; |
| 990 | |
| 991 | file_type = eFileTypeDirectory; |
| 992 | } else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) { |
| 993 | file_type = eFileTypeOther; |
| 994 | } else { |
| 995 | file_type = eFileTypeRegular; |
| 996 | } |
| 997 | |
| 998 | std::string fileName; |
| 999 | if (!llvm::convertWideToUTF8(ffd.cFileName, fileName)) { |
| 1000 | continue; |
| 1001 | } |
| 1002 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1003 | std::string child_path = llvm::join_items("\\", dir_path, fileName); |
| 1004 | // Don't resolve the file type or path |
| 1005 | FileSpec child_path_spec(child_path.data(), false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1006 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1007 | EnumerateDirectoryResult result = callback(file_type, child_path_spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1008 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1009 | switch (result) { |
| 1010 | case eEnumerateDirectoryResultNext: |
| 1011 | // Enumerate next entry in the current directory. We just |
| 1012 | // exit this switch and will continue enumerating the |
| 1013 | // current directory as we currently are... |
| 1014 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1015 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1016 | case eEnumerateDirectoryResultEnter: // Recurse into the current entry |
| 1017 | // if it is a directory or symlink, |
| 1018 | // or next if not |
| 1019 | if (FileSpec::ForEachItemInDirectory(child_path.data(), callback) == |
| 1020 | eEnumerateDirectoryResultQuit) { |
| 1021 | // The subdirectory returned Quit, which means to |
| 1022 | // stop all directory enumerations at all levels. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1023 | return eEnumerateDirectoryResultQuit; |
| 1024 | } |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1025 | break; |
| 1026 | |
| 1027 | case eEnumerateDirectoryResultExit: // Exit from the current directory |
| 1028 | // at the current level. |
| 1029 | // Exit from this directory level and tell parent to |
| 1030 | // keep enumerating. |
| 1031 | return eEnumerateDirectoryResultNext; |
| 1032 | |
| 1033 | case eEnumerateDirectoryResultQuit: // Stop directory enumerations at |
| 1034 | // any level |
| 1035 | return eEnumerateDirectoryResultQuit; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } while (FindNextFileW(hFind, &ffd) != 0); |
| 1038 | |
| 1039 | FindClose(hFind); |
| 1040 | #else |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1041 | std::string dir_string(dir_path); |
| 1042 | lldb_utility::CleanUp<DIR *, int> dir_path_dir(opendir(dir_string.c_str()), |
| 1043 | NULL, closedir); |
| 1044 | if (dir_path_dir.is_valid()) { |
| 1045 | char dir_path_last_char = dir_path.back(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1046 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1047 | long path_max = fpathconf(dirfd(dir_path_dir.get()), _PC_NAME_MAX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1048 | #if defined(__APPLE_) && defined(__DARWIN_MAXPATHLEN) |
| 1049 | if (path_max < __DARWIN_MAXPATHLEN) |
| 1050 | path_max = __DARWIN_MAXPATHLEN; |
| 1051 | #endif |
| 1052 | struct dirent *buf, *dp; |
| 1053 | buf = (struct dirent *)malloc(offsetof(struct dirent, d_name) + path_max + |
| 1054 | 1); |
| 1055 | |
| 1056 | while (buf && readdir_r(dir_path_dir.get(), buf, &dp) == 0 && dp) { |
| 1057 | // Only search directories |
| 1058 | if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) { |
| 1059 | size_t len = strlen(dp->d_name); |
| 1060 | |
| 1061 | if (len == 1 && dp->d_name[0] == '.') |
| 1062 | continue; |
| 1063 | |
| 1064 | if (len == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | FileSpec::FileType file_type = eFileTypeUnknown; |
| 1069 | |
| 1070 | switch (dp->d_type) { |
| 1071 | default: |
| 1072 | case DT_UNKNOWN: |
| 1073 | file_type = eFileTypeUnknown; |
| 1074 | break; |
| 1075 | case DT_FIFO: |
| 1076 | file_type = eFileTypePipe; |
| 1077 | break; |
| 1078 | case DT_CHR: |
| 1079 | file_type = eFileTypeOther; |
| 1080 | break; |
| 1081 | case DT_DIR: |
| 1082 | file_type = eFileTypeDirectory; |
| 1083 | break; |
| 1084 | case DT_BLK: |
| 1085 | file_type = eFileTypeOther; |
| 1086 | break; |
| 1087 | case DT_REG: |
| 1088 | file_type = eFileTypeRegular; |
| 1089 | break; |
| 1090 | case DT_LNK: |
| 1091 | file_type = eFileTypeSymbolicLink; |
| 1092 | break; |
| 1093 | case DT_SOCK: |
| 1094 | file_type = eFileTypeSocket; |
| 1095 | break; |
| 1096 | #if !defined(__OpenBSD__) |
| 1097 | case DT_WHT: |
| 1098 | file_type = eFileTypeOther; |
| 1099 | break; |
| 1100 | #endif |
| 1101 | } |
| 1102 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1103 | std::string child_path; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1104 | // Don't make paths with "/foo//bar", that just confuses everybody. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1105 | if (dir_path_last_char == '/') |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1106 | child_path = llvm::join_items("", dir_path, dp->d_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1107 | else |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1108 | child_path = llvm::join_items('/', dir_path, dp->d_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1110 | // Don't resolve the file type or path |
| 1111 | FileSpec child_path_spec(child_path, false); |
| 1112 | |
| 1113 | EnumerateDirectoryResult result = |
| 1114 | callback(file_type, child_path_spec); |
| 1115 | |
| 1116 | switch (result) { |
| 1117 | case eEnumerateDirectoryResultNext: |
| 1118 | // Enumerate next entry in the current directory. We just |
| 1119 | // exit this switch and will continue enumerating the |
| 1120 | // current directory as we currently are... |
| 1121 | break; |
| 1122 | |
| 1123 | case eEnumerateDirectoryResultEnter: // Recurse into the current entry |
| 1124 | // if it is a directory or |
| 1125 | // symlink, or next if not |
| 1126 | if (FileSpec::ForEachItemInDirectory(child_path, callback) == |
| 1127 | eEnumerateDirectoryResultQuit) { |
| 1128 | // The subdirectory returned Quit, which means to |
| 1129 | // stop all directory enumerations at all levels. |
| 1130 | if (buf) |
| 1131 | free(buf); |
| 1132 | return eEnumerateDirectoryResultQuit; |
| 1133 | } |
| 1134 | break; |
| 1135 | |
| 1136 | case eEnumerateDirectoryResultExit: // Exit from the current directory |
| 1137 | // at the current level. |
| 1138 | // Exit from this directory level and tell parent to |
| 1139 | // keep enumerating. |
| 1140 | if (buf) |
| 1141 | free(buf); |
| 1142 | return eEnumerateDirectoryResultNext; |
| 1143 | |
| 1144 | case eEnumerateDirectoryResultQuit: // Stop directory enumerations at |
| 1145 | // any level |
| 1146 | if (buf) |
| 1147 | free(buf); |
| 1148 | return eEnumerateDirectoryResultQuit; |
| 1149 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | } |
| 1151 | if (buf) { |
| 1152 | free(buf); |
| 1153 | } |
| 1154 | } |
| 1155 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1156 | // By default when exiting a directory, we tell the parent enumeration |
| 1157 | // to continue enumerating. |
| 1158 | return eEnumerateDirectoryResultNext; |
Greg Clayton | 58c65f0 | 2015-06-29 18:29:00 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | FileSpec::EnumerateDirectoryResult |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1162 | FileSpec::EnumerateDirectory(llvm::StringRef dir_path, bool find_directories, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1163 | bool find_files, bool find_other, |
| 1164 | EnumerateDirectoryCallbackType callback, |
| 1165 | void *callback_baton) { |
| 1166 | return ForEachItemInDirectory( |
| 1167 | dir_path, |
| 1168 | [&find_directories, &find_files, &find_other, &callback, |
| 1169 | &callback_baton](FileType file_type, const FileSpec &file_spec) { |
| 1170 | switch (file_type) { |
| 1171 | case FileType::eFileTypeDirectory: |
| 1172 | if (find_directories) |
| 1173 | return callback(callback_baton, file_type, file_spec); |
| 1174 | break; |
| 1175 | case FileType::eFileTypeRegular: |
| 1176 | if (find_files) |
| 1177 | return callback(callback_baton, file_type, file_spec); |
| 1178 | break; |
| 1179 | default: |
| 1180 | if (find_other) |
| 1181 | return callback(callback_baton, file_type, file_spec); |
| 1182 | break; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1183 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1184 | return eEnumerateDirectoryResultNext; |
| 1185 | }); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1188 | FileSpec |
| 1189 | FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1190 | FileSpec ret = *this; |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1191 | ret.AppendPathComponent(component); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1192 | return ret; |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1195 | FileSpec FileSpec::CopyByRemovingLastPathComponent() const { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1196 | // CLEANUP: Use StringRef for string handling. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1197 | const bool resolve = false; |
| 1198 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) |
| 1199 | return FileSpec("", resolve); |
| 1200 | if (m_directory.IsEmpty()) |
| 1201 | return FileSpec("", resolve); |
| 1202 | if (m_filename.IsEmpty()) { |
| 1203 | const char *dir_cstr = m_directory.GetCString(); |
| 1204 | const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 1205 | |
| 1206 | // check for obvious cases before doing the full thing |
| 1207 | if (!last_slash_ptr) |
| 1208 | return FileSpec("", resolve); |
| 1209 | if (last_slash_ptr == dir_cstr) |
| 1210 | return FileSpec("/", resolve); |
| 1211 | |
| 1212 | size_t last_slash_pos = last_slash_ptr - dir_cstr + 1; |
| 1213 | ConstString new_path(dir_cstr, last_slash_pos); |
| 1214 | return FileSpec(new_path.GetCString(), resolve); |
| 1215 | } else |
| 1216 | return FileSpec(m_directory.GetCString(), resolve); |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1219 | ConstString FileSpec::GetLastPathComponent() const { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1220 | // CLEANUP: Use StringRef for string handling. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1221 | if (m_filename) |
| 1222 | return m_filename; |
| 1223 | if (m_directory) { |
| 1224 | const char *dir_cstr = m_directory.GetCString(); |
| 1225 | const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 1226 | if (last_slash_ptr == NULL) |
| 1227 | return m_directory; |
| 1228 | if (last_slash_ptr == dir_cstr) { |
| 1229 | if (last_slash_ptr[1] == 0) |
| 1230 | return ConstString(last_slash_ptr); |
| 1231 | else |
| 1232 | return ConstString(last_slash_ptr + 1); |
| 1233 | } |
| 1234 | if (last_slash_ptr[1] != 0) |
| 1235 | return ConstString(last_slash_ptr + 1); |
| 1236 | const char *penultimate_slash_ptr = last_slash_ptr; |
| 1237 | while (*penultimate_slash_ptr) { |
| 1238 | --penultimate_slash_ptr; |
| 1239 | if (penultimate_slash_ptr == dir_cstr) |
| 1240 | break; |
| 1241 | if (*penultimate_slash_ptr == '/') |
| 1242 | break; |
| 1243 | } |
| 1244 | ConstString result(penultimate_slash_ptr + 1, |
| 1245 | last_slash_ptr - penultimate_slash_ptr); |
| 1246 | return result; |
| 1247 | } |
| 1248 | return ConstString(); |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Pavel Labath | 59d725c | 2017-01-16 10:07:02 +0000 | [diff] [blame] | 1251 | static std::string |
| 1252 | join_path_components(FileSpec::PathSyntax syntax, |
| 1253 | const std::vector<llvm::StringRef> components) { |
| 1254 | std::string result; |
| 1255 | for (size_t i = 0; i < components.size(); ++i) { |
| 1256 | if (components[i].empty()) |
| 1257 | continue; |
| 1258 | result += components[i]; |
| 1259 | if (i != components.size() - 1 && |
| 1260 | !IsPathSeparator(components[i].back(), syntax)) |
| 1261 | result += GetPreferredPathSeparator(syntax); |
| 1262 | } |
| 1263 | |
| 1264 | return result; |
| 1265 | } |
| 1266 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1267 | void FileSpec::PrependPathComponent(llvm::StringRef component) { |
| 1268 | if (component.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1269 | return; |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1271 | const bool resolve = false; |
| 1272 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1273 | SetFile(component, resolve); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1274 | return; |
| 1275 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1276 | |
Pavel Labath | 59d725c | 2017-01-16 10:07:02 +0000 | [diff] [blame] | 1277 | std::string result = |
| 1278 | join_path_components(m_syntax, {component, m_directory.GetStringRef(), |
| 1279 | m_filename.GetStringRef()}); |
Pavel Labath | 238169d | 2017-01-16 12:15:42 +0000 | [diff] [blame] | 1280 | SetFile(result, resolve, m_syntax); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1283 | void FileSpec::PrependPathComponent(const FileSpec &new_path) { |
| 1284 | return PrependPathComponent(new_path.GetPath(false)); |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1287 | void FileSpec::AppendPathComponent(llvm::StringRef component) { |
| 1288 | if (component.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1289 | return; |
| 1290 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1291 | component = component.drop_while( |
| 1292 | [this](char c) { return IsPathSeparator(c, m_syntax); }); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1293 | |
Pavel Labath | 59d725c | 2017-01-16 10:07:02 +0000 | [diff] [blame] | 1294 | std::string result = |
| 1295 | join_path_components(m_syntax, {m_directory.GetStringRef(), |
| 1296 | m_filename.GetStringRef(), component}); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1297 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1298 | SetFile(result, false, m_syntax); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | void FileSpec::AppendPathComponent(const FileSpec &new_path) { |
| 1302 | return AppendPathComponent(new_path.GetPath(false)); |
| 1303 | } |
| 1304 | |
| 1305 | void FileSpec::RemoveLastPathComponent() { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 1306 | // CLEANUP: Use StringRef for string handling. |
| 1307 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1308 | const bool resolve = false; |
| 1309 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) { |
| 1310 | SetFile("", resolve); |
| 1311 | return; |
| 1312 | } |
| 1313 | if (m_directory.IsEmpty()) { |
| 1314 | SetFile("", resolve); |
| 1315 | return; |
| 1316 | } |
| 1317 | if (m_filename.IsEmpty()) { |
| 1318 | const char *dir_cstr = m_directory.GetCString(); |
| 1319 | const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 1320 | |
| 1321 | // check for obvious cases before doing the full thing |
| 1322 | if (!last_slash_ptr) { |
| 1323 | SetFile("", resolve); |
| 1324 | return; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1325 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1326 | if (last_slash_ptr == dir_cstr) { |
| 1327 | SetFile("/", resolve); |
| 1328 | return; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1329 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1330 | size_t last_slash_pos = last_slash_ptr - dir_cstr + 1; |
| 1331 | ConstString new_path(dir_cstr, last_slash_pos); |
| 1332 | SetFile(new_path.GetCString(), resolve); |
| 1333 | } else |
| 1334 | SetFile(m_directory.GetCString(), resolve); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 1335 | } |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 1336 | //------------------------------------------------------------------ |
| 1337 | /// Returns true if the filespec represents an implementation source |
| 1338 | /// file (files with a ".c", ".cpp", ".m", ".mm" (many more) |
| 1339 | /// extension). |
| 1340 | /// |
| 1341 | /// @return |
| 1342 | /// \b true if the filespec represents an implementation source |
| 1343 | /// file, \b false otherwise. |
| 1344 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1345 | bool FileSpec::IsSourceImplementationFile() const { |
| 1346 | ConstString extension(GetFileNameExtension()); |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 1347 | if (!extension) |
| 1348 | return false; |
| 1349 | |
| 1350 | static RegularExpression g_source_file_regex(llvm::StringRef( |
| 1351 | "^([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|[" |
| 1352 | "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO][" |
| 1353 | "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])" |
| 1354 | "$")); |
| 1355 | return g_source_file_regex.Execute(extension.GetStringRef()); |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1358 | bool FileSpec::IsRelative() const { |
| 1359 | const char *dir = m_directory.GetCString(); |
| 1360 | llvm::StringRef directory(dir ? dir : ""); |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 1361 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1362 | if (directory.size() > 0) { |
| 1363 | if (PathSyntaxIsPosix(m_syntax)) { |
| 1364 | // If the path doesn't start with '/' or '~', return true |
| 1365 | switch (directory[0]) { |
| 1366 | case '/': |
| 1367 | case '~': |
| 1368 | return false; |
| 1369 | default: |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 1370 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1371 | } |
| 1372 | } else { |
| 1373 | if (directory.size() >= 2 && directory[1] == ':') |
| 1374 | return false; |
| 1375 | if (directory[0] == '/') |
| 1376 | return false; |
| 1377 | return true; |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 1378 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1379 | } else if (m_filename) { |
| 1380 | // No directory, just a basename, return true |
| 1381 | return true; |
| 1382 | } |
| 1383 | return false; |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 1384 | } |
Chaoren Lin | 372e906 | 2015-06-09 17:54:27 +0000 | [diff] [blame] | 1385 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1386 | bool FileSpec::IsAbsolute() const { return !FileSpec::IsRelative(); } |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 1387 | |
| 1388 | void llvm::format_provider<FileSpec>::format(const FileSpec &F, |
| 1389 | raw_ostream &Stream, |
| 1390 | StringRef Style) { |
| 1391 | assert( |
| 1392 | (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) && |
| 1393 | "Invalid FileSpec style!"); |
| 1394 | |
| 1395 | StringRef dir = F.GetDirectory().GetStringRef(); |
| 1396 | StringRef file = F.GetFilename().GetStringRef(); |
| 1397 | |
| 1398 | if (dir.empty() && file.empty()) { |
| 1399 | Stream << "(empty)"; |
| 1400 | return; |
| 1401 | } |
| 1402 | |
| 1403 | if (Style.equals_lower("F")) { |
| 1404 | Stream << (file.empty() ? "(empty)" : file); |
| 1405 | return; |
| 1406 | } |
| 1407 | |
| 1408 | // Style is either D or empty, either way we need to print the directory. |
| 1409 | if (!dir.empty()) { |
| 1410 | // Directory is stored in normalized form, which might be different |
| 1411 | // than preferred form. In order to handle this, we need to cut off |
| 1412 | // the filename, then denormalize, then write the entire denorm'ed |
| 1413 | // directory. |
| 1414 | llvm::SmallString<64> denormalized_dir = dir; |
| 1415 | Denormalize(denormalized_dir, F.GetPathSyntax()); |
| 1416 | Stream << denormalized_dir; |
| 1417 | Stream << GetPreferredPathSeparator(F.GetPathSyntax()); |
| 1418 | } |
| 1419 | |
| 1420 | if (Style.equals_lower("D")) { |
| 1421 | // We only want to print the directory, so now just exit. |
| 1422 | if (dir.empty()) |
| 1423 | Stream << "(empty)"; |
| 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | if (!file.empty()) |
| 1428 | Stream << file; |
| 1429 | } |