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