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