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