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