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