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> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include <libgen.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include <sys/stat.h> |
Rafael Espindola | 0907916 | 2013-06-13 20:10:23 +0000 | [diff] [blame] | 19 | #include <set> |
Greg Clayton | e0f3c02 | 2011-02-07 17:41:11 +0000 | [diff] [blame] | 20 | #include <string.h> |
Jim Ingham | 9035e7c | 2011-02-07 19:42:39 +0000 | [diff] [blame] | 21 | #include <fstream> |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 22 | |
Jim Ingham | 9035e7c | 2011-02-07 19:42:39 +0000 | [diff] [blame] | 23 | #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] | 24 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 25 | #include <pwd.h> |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 26 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringRef.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Path.h" |
| 30 | #include "llvm/Support/Program.h" |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 31 | |
Greg Clayton | 96c0968 | 2012-01-04 22:56:43 +0000 | [diff] [blame] | 32 | #include "lldb/Host/File.h" |
Greg Clayton | 53239f0 | 2011-02-08 05:05:52 +0000 | [diff] [blame] | 33 | #include "lldb/Host/FileSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include "lldb/Core/DataBufferHeap.h" |
| 35 | #include "lldb/Core/DataBufferMemoryMap.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 36 | #include "lldb/Core/RegularExpression.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | #include "lldb/Core/Stream.h" |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 38 | #include "lldb/Host/Host.h" |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 39 | #include "lldb/Utility/CleanUp.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace lldb; |
| 42 | using namespace lldb_private; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | |
| 44 | static bool |
| 45 | GetFileStats (const FileSpec *file_spec, struct stat *stats_ptr) |
| 46 | { |
| 47 | char resolved_path[PATH_MAX]; |
Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 48 | if (file_spec->GetPath (resolved_path, sizeof(resolved_path))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | return ::stat (resolved_path, stats_ptr) == 0; |
| 50 | return false; |
| 51 | } |
| 52 | |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 53 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | static const char* |
| 56 | GetCachedGlobTildeSlash() |
| 57 | { |
| 58 | static std::string g_tilde; |
| 59 | if (g_tilde.empty()) |
| 60 | { |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 61 | struct passwd *user_entry; |
| 62 | user_entry = getpwuid(geteuid()); |
| 63 | if (user_entry != NULL) |
| 64 | g_tilde = user_entry->pw_dir; |
| 65 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | if (g_tilde.empty()) |
| 67 | return NULL; |
| 68 | } |
| 69 | return g_tilde.c_str(); |
| 70 | } |
| 71 | |
Greg Clayton | 87e5ff0 | 2011-02-08 05:19:06 +0000 | [diff] [blame] | 72 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
| 73 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 74 | // Resolves the username part of a path of the form ~user/other/directories, and |
| 75 | // writes the result into dst_path. |
| 76 | // Returns 0 if there WAS a ~ in the path but the username couldn't be resolved. |
| 77 | // Otherwise returns the number of characters copied into dst_path. If the return |
| 78 | // is >= dst_len, then the resolved path is too long... |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 79 | size_t |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 80 | FileSpec::ResolveUsername (const char *src_path, char *dst_path, size_t dst_len) |
| 81 | { |
Greg Clayton | 87e5ff0 | 2011-02-08 05:19:06 +0000 | [diff] [blame] | 82 | if (src_path == NULL || src_path[0] == '\0') |
| 83 | return 0; |
| 84 | |
| 85 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
| 86 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 87 | char user_home[PATH_MAX]; |
| 88 | const char *user_name; |
| 89 | |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 90 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 91 | // If there's no ~, then just copy src_path straight to dst_path (they may be the same string...) |
| 92 | if (src_path[0] != '~') |
| 93 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 94 | size_t len = strlen (src_path); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 95 | if (len >= dst_len) |
| 96 | { |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 97 | ::bcopy (src_path, dst_path, dst_len - 1); |
| 98 | dst_path[dst_len] = '\0'; |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 99 | } |
| 100 | else |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 101 | ::bcopy (src_path, dst_path, len + 1); |
| 102 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 103 | return len; |
| 104 | } |
| 105 | |
Eli Friedman | feaeebf | 2010-07-02 19:15:50 +0000 | [diff] [blame] | 106 | const char *first_slash = ::strchr (src_path, '/'); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 107 | char remainder[PATH_MAX]; |
| 108 | |
| 109 | if (first_slash == NULL) |
| 110 | { |
| 111 | // The whole name is the username (minus the ~): |
| 112 | user_name = src_path + 1; |
| 113 | remainder[0] = '\0'; |
| 114 | } |
| 115 | else |
| 116 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 117 | size_t user_name_len = first_slash - src_path - 1; |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 118 | ::memcpy (user_home, src_path + 1, user_name_len); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 119 | user_home[user_name_len] = '\0'; |
| 120 | user_name = user_home; |
| 121 | |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 122 | ::strcpy (remainder, first_slash); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 123 | } |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 124 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 125 | if (user_name == NULL) |
| 126 | return 0; |
| 127 | // User name of "" means the current user... |
| 128 | |
| 129 | struct passwd *user_entry; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 130 | const char *home_dir = NULL; |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 131 | |
| 132 | if (user_name[0] == '\0') |
| 133 | { |
| 134 | home_dir = GetCachedGlobTildeSlash(); |
| 135 | } |
| 136 | else |
| 137 | { |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 138 | user_entry = ::getpwnam (user_name); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 139 | if (user_entry != NULL) |
| 140 | home_dir = user_entry->pw_dir; |
| 141 | } |
| 142 | |
| 143 | if (home_dir == NULL) |
| 144 | return 0; |
| 145 | else |
| 146 | return ::snprintf (dst_path, dst_len, "%s%s", home_dir, remainder); |
Greg Clayton | 87e5ff0 | 2011-02-08 05:19:06 +0000 | [diff] [blame] | 147 | #else |
| 148 | // Resolving home directories is not supported, just copy the path... |
| 149 | return ::snprintf (dst_path, dst_len, "%s", src_path); |
| 150 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 153 | size_t |
Jim Ingham | 8436307 | 2011-02-08 23:24:09 +0000 | [diff] [blame] | 154 | FileSpec::ResolvePartialUsername (const char *partial_name, StringList &matches) |
| 155 | { |
| 156 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
| 157 | size_t extant_entries = matches.GetSize(); |
| 158 | |
| 159 | setpwent(); |
| 160 | struct passwd *user_entry; |
| 161 | const char *name_start = partial_name + 1; |
| 162 | std::set<std::string> name_list; |
| 163 | |
| 164 | while ((user_entry = getpwent()) != NULL) |
| 165 | { |
| 166 | if (strstr(user_entry->pw_name, name_start) == user_entry->pw_name) |
| 167 | { |
| 168 | std::string tmp_buf("~"); |
| 169 | tmp_buf.append(user_entry->pw_name); |
| 170 | tmp_buf.push_back('/'); |
| 171 | name_list.insert(tmp_buf); |
| 172 | } |
| 173 | } |
| 174 | std::set<std::string>::iterator pos, end = name_list.end(); |
| 175 | for (pos = name_list.begin(); pos != end; pos++) |
| 176 | { |
| 177 | matches.AppendString((*pos).c_str()); |
| 178 | } |
| 179 | return matches.GetSize() - extant_entries; |
| 180 | #else |
| 181 | // Resolving home directories is not supported, just copy the path... |
| 182 | return 0; |
| 183 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len) |
| 190 | { |
| 191 | if (src_path == NULL || src_path[0] == '\0') |
| 192 | return 0; |
| 193 | |
| 194 | // Glob if needed for ~/, otherwise copy in case src_path is same as dst_path... |
| 195 | char unglobbed_path[PATH_MAX]; |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 196 | #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 197 | if (src_path[0] == '~') |
| 198 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 199 | size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path)); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 200 | |
| 201 | // If we couldn't find the user referred to, or the resultant path was too long, |
| 202 | // then just copy over the src_path. |
| 203 | if (return_count == 0 || return_count >= sizeof(unglobbed_path)) |
| 204 | ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", src_path); |
| 205 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | else |
Greg Clayton | 4531946 | 2011-02-08 00:35:34 +0000 | [diff] [blame] | 207 | #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER |
Greg Clayton | fd18426 | 2011-02-05 02:27:52 +0000 | [diff] [blame] | 208 | { |
| 209 | ::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path); |
| 210 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | |
| 212 | // Now resolve the path if needed |
| 213 | char resolved_path[PATH_MAX]; |
| 214 | if (::realpath (unglobbed_path, resolved_path)) |
| 215 | { |
| 216 | // Success, copy the resolved path |
| 217 | return ::snprintf(dst_path, dst_len, "%s", resolved_path); |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | // Failed, just copy the unglobbed path |
| 222 | return ::snprintf(dst_path, dst_len, "%s", unglobbed_path); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | FileSpec::FileSpec() : |
| 227 | m_directory(), |
| 228 | m_filename() |
| 229 | { |
| 230 | } |
| 231 | |
| 232 | //------------------------------------------------------------------ |
| 233 | // Default constructor that can take an optional full path to a |
| 234 | // file on disk. |
| 235 | //------------------------------------------------------------------ |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 236 | FileSpec::FileSpec(const char *pathname, bool resolve_path) : |
| 237 | m_directory(), |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 238 | m_filename(), |
| 239 | m_is_resolved(false) |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 240 | { |
| 241 | if (pathname && pathname[0]) |
| 242 | SetFile(pathname, resolve_path); |
| 243 | } |
| 244 | |
| 245 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | // Copy constructor |
| 247 | //------------------------------------------------------------------ |
| 248 | FileSpec::FileSpec(const FileSpec& rhs) : |
| 249 | m_directory (rhs.m_directory), |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 250 | m_filename (rhs.m_filename), |
| 251 | m_is_resolved (rhs.m_is_resolved) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | { |
| 253 | } |
| 254 | |
| 255 | //------------------------------------------------------------------ |
| 256 | // Copy constructor |
| 257 | //------------------------------------------------------------------ |
| 258 | FileSpec::FileSpec(const FileSpec* rhs) : |
| 259 | m_directory(), |
| 260 | m_filename() |
| 261 | { |
| 262 | if (rhs) |
| 263 | *this = *rhs; |
| 264 | } |
| 265 | |
| 266 | //------------------------------------------------------------------ |
| 267 | // Virtual destrcuctor in case anyone inherits from this class. |
| 268 | //------------------------------------------------------------------ |
| 269 | FileSpec::~FileSpec() |
| 270 | { |
| 271 | } |
| 272 | |
| 273 | //------------------------------------------------------------------ |
| 274 | // Assignment operator. |
| 275 | //------------------------------------------------------------------ |
| 276 | const FileSpec& |
| 277 | FileSpec::operator= (const FileSpec& rhs) |
| 278 | { |
| 279 | if (this != &rhs) |
| 280 | { |
| 281 | m_directory = rhs.m_directory; |
| 282 | m_filename = rhs.m_filename; |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 283 | m_is_resolved = rhs.m_is_resolved; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 284 | } |
| 285 | return *this; |
| 286 | } |
| 287 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | //------------------------------------------------------------------ |
| 289 | // Update the contents of this object with a new path. The path will |
| 290 | // be split up into a directory and filename and stored as uniqued |
| 291 | // string values for quick comparison and efficient memory usage. |
| 292 | //------------------------------------------------------------------ |
| 293 | void |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 294 | FileSpec::SetFile (const char *pathname, bool resolve) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | { |
| 296 | m_filename.Clear(); |
| 297 | m_directory.Clear(); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 298 | m_is_resolved = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 299 | if (pathname == NULL || pathname[0] == '\0') |
| 300 | return; |
| 301 | |
| 302 | char resolved_path[PATH_MAX]; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 303 | bool path_fit = true; |
| 304 | |
| 305 | if (resolve) |
| 306 | { |
| 307 | path_fit = (FileSpec::Resolve (pathname, resolved_path, sizeof(resolved_path)) < sizeof(resolved_path) - 1); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 308 | m_is_resolved = path_fit; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 309 | } |
| 310 | else |
| 311 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 312 | // Copy the path because "basename" and "dirname" want to muck with the |
| 313 | // path buffer |
| 314 | if (::strlen (pathname) > sizeof(resolved_path) - 1) |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 315 | path_fit = false; |
| 316 | else |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 317 | ::strcpy (resolved_path, pathname); |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 318 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 319 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 320 | |
| 321 | if (path_fit) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 322 | { |
| 323 | char *filename = ::basename (resolved_path); |
| 324 | if (filename) |
| 325 | { |
| 326 | m_filename.SetCString (filename); |
| 327 | // Truncate the basename off the end of the resolved path |
| 328 | |
| 329 | // Only attempt to get the dirname if it looks like we have a path |
| 330 | if (strchr(resolved_path, '/')) |
| 331 | { |
| 332 | char *directory = ::dirname (resolved_path); |
| 333 | |
| 334 | // Make sure we didn't get our directory resolved to "." without having |
| 335 | // specified |
| 336 | if (directory) |
| 337 | m_directory.SetCString(directory); |
| 338 | else |
| 339 | { |
| 340 | char *last_resolved_path_slash = strrchr(resolved_path, '/'); |
| 341 | if (last_resolved_path_slash) |
| 342 | { |
| 343 | *last_resolved_path_slash = '\0'; |
| 344 | m_directory.SetCString(resolved_path); |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | else |
| 350 | m_directory.SetCString(resolved_path); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | //---------------------------------------------------------------------- |
| 355 | // Convert to pointer operator. This allows code to check any FileSpec |
| 356 | // objects to see if they contain anything valid using code such as: |
| 357 | // |
| 358 | // if (file_spec) |
| 359 | // {} |
| 360 | //---------------------------------------------------------------------- |
Greg Clayton | 6372d1c | 2011-09-12 04:00:42 +0000 | [diff] [blame] | 361 | FileSpec::operator bool() const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 362 | { |
Greg Clayton | 6372d1c | 2011-09-12 04:00:42 +0000 | [diff] [blame] | 363 | return m_filename || m_directory; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | //---------------------------------------------------------------------- |
| 367 | // Logical NOT operator. This allows code to check any FileSpec |
| 368 | // objects to see if they are invalid using code such as: |
| 369 | // |
| 370 | // if (!file_spec) |
| 371 | // {} |
| 372 | //---------------------------------------------------------------------- |
| 373 | bool |
| 374 | FileSpec::operator!() const |
| 375 | { |
| 376 | return !m_directory && !m_filename; |
| 377 | } |
| 378 | |
| 379 | //------------------------------------------------------------------ |
| 380 | // Equal to operator |
| 381 | //------------------------------------------------------------------ |
| 382 | bool |
| 383 | FileSpec::operator== (const FileSpec& rhs) const |
| 384 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 385 | if (m_filename == rhs.m_filename) |
| 386 | { |
| 387 | if (m_directory == rhs.m_directory) |
| 388 | return true; |
| 389 | |
| 390 | // TODO: determine if we want to keep this code in here. |
| 391 | // The code below was added to handle a case where we were |
| 392 | // trying to set a file and line breakpoint and one path |
| 393 | // was resolved, and the other not and the directory was |
| 394 | // in a mount point that resolved to a more complete path: |
| 395 | // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling |
| 396 | // this out... |
| 397 | if (IsResolved() && rhs.IsResolved()) |
| 398 | { |
| 399 | // Both paths are resolved, no need to look further... |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | FileSpec resolved_lhs(*this); |
| 404 | |
| 405 | // If "this" isn't resolved, resolve it |
| 406 | if (!IsResolved()) |
| 407 | { |
| 408 | if (resolved_lhs.ResolvePath()) |
| 409 | { |
| 410 | // This path wasn't resolved but now it is. Check if the resolved |
| 411 | // directory is the same as our unresolved directory, and if so, |
| 412 | // we can mark this object as resolved to avoid more future resolves |
| 413 | m_is_resolved = (m_directory == resolved_lhs.m_directory); |
| 414 | } |
| 415 | else |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | FileSpec resolved_rhs(rhs); |
| 420 | if (!rhs.IsResolved()) |
| 421 | { |
| 422 | if (resolved_rhs.ResolvePath()) |
| 423 | { |
| 424 | // rhs's path wasn't resolved but now it is. Check if the resolved |
| 425 | // directory is the same as rhs's unresolved directory, and if so, |
| 426 | // we can mark this object as resolved to avoid more future resolves |
Jim Ingham | a537f6c | 2012-11-03 02:12:46 +0000 | [diff] [blame] | 427 | rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 428 | } |
| 429 | else |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | // If we reach this point in the code we were able to resolve both paths |
| 434 | // and since we only resolve the paths if the basenames are equal, then |
| 435 | // we can just check if both directories are equal... |
| 436 | return resolved_lhs.GetDirectory() == resolved_rhs.GetDirectory(); |
| 437 | } |
| 438 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | //------------------------------------------------------------------ |
| 442 | // Not equal to operator |
| 443 | //------------------------------------------------------------------ |
| 444 | bool |
| 445 | FileSpec::operator!= (const FileSpec& rhs) const |
| 446 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 447 | return !(*this == rhs); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | //------------------------------------------------------------------ |
| 451 | // Less than operator |
| 452 | //------------------------------------------------------------------ |
| 453 | bool |
| 454 | FileSpec::operator< (const FileSpec& rhs) const |
| 455 | { |
| 456 | return FileSpec::Compare(*this, rhs, true) < 0; |
| 457 | } |
| 458 | |
| 459 | //------------------------------------------------------------------ |
| 460 | // Dump a FileSpec object to a stream |
| 461 | //------------------------------------------------------------------ |
| 462 | Stream& |
| 463 | lldb_private::operator << (Stream &s, const FileSpec& f) |
| 464 | { |
| 465 | f.Dump(&s); |
| 466 | return s; |
| 467 | } |
| 468 | |
| 469 | //------------------------------------------------------------------ |
| 470 | // Clear this object by releasing both the directory and filename |
| 471 | // string values and making them both the empty string. |
| 472 | //------------------------------------------------------------------ |
| 473 | void |
| 474 | FileSpec::Clear() |
| 475 | { |
| 476 | m_directory.Clear(); |
| 477 | m_filename.Clear(); |
| 478 | } |
| 479 | |
| 480 | //------------------------------------------------------------------ |
| 481 | // Compare two FileSpec objects. If "full" is true, then both |
| 482 | // the directory and the filename must match. If "full" is false, |
| 483 | // then the directory names for "a" and "b" are only compared if |
| 484 | // they are both non-empty. This allows a FileSpec object to only |
| 485 | // contain a filename and it can match FileSpec objects that have |
| 486 | // matching filenames with different paths. |
| 487 | // |
| 488 | // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" |
| 489 | // and "1" if "a" is greater than "b". |
| 490 | //------------------------------------------------------------------ |
| 491 | int |
| 492 | FileSpec::Compare(const FileSpec& a, const FileSpec& b, bool full) |
| 493 | { |
| 494 | int result = 0; |
| 495 | |
| 496 | // If full is true, then we must compare both the directory and filename. |
| 497 | |
| 498 | // If full is false, then if either directory is empty, then we match on |
| 499 | // the basename only, and if both directories have valid values, we still |
| 500 | // do a full compare. This allows for matching when we just have a filename |
| 501 | // in one of the FileSpec objects. |
| 502 | |
| 503 | if (full || (a.m_directory && b.m_directory)) |
| 504 | { |
| 505 | result = ConstString::Compare(a.m_directory, b.m_directory); |
| 506 | if (result) |
| 507 | return result; |
| 508 | } |
| 509 | return ConstString::Compare (a.m_filename, b.m_filename); |
| 510 | } |
| 511 | |
| 512 | bool |
| 513 | FileSpec::Equal (const FileSpec& a, const FileSpec& b, bool full) |
| 514 | { |
Jim Ingham | 87df91b | 2011-09-23 00:54:11 +0000 | [diff] [blame] | 515 | if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty())) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 516 | return a.m_filename == b.m_filename; |
Jim Ingham | 87df91b | 2011-09-23 00:54:11 +0000 | [diff] [blame] | 517 | else |
| 518 | return a == b; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
| 522 | |
| 523 | //------------------------------------------------------------------ |
| 524 | // Dump the object to the supplied stream. If the object contains |
| 525 | // a valid directory name, it will be displayed followed by a |
| 526 | // directory delimiter, and the filename. |
| 527 | //------------------------------------------------------------------ |
| 528 | void |
| 529 | FileSpec::Dump(Stream *s) const |
| 530 | { |
Jason Molenda | db7d11c | 2013-05-06 10:21:11 +0000 | [diff] [blame] | 531 | static ConstString g_slash_only ("/"); |
Enrico Granata | 80fcdd4 | 2012-11-03 00:09:46 +0000 | [diff] [blame] | 532 | if (s) |
| 533 | { |
| 534 | m_directory.Dump(s); |
Jason Molenda | db7d11c | 2013-05-06 10:21:11 +0000 | [diff] [blame] | 535 | if (m_directory && m_directory != g_slash_only) |
Enrico Granata | 80fcdd4 | 2012-11-03 00:09:46 +0000 | [diff] [blame] | 536 | s->PutChar('/'); |
| 537 | m_filename.Dump(s); |
| 538 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | //------------------------------------------------------------------ |
| 542 | // Returns true if the file exists. |
| 543 | //------------------------------------------------------------------ |
| 544 | bool |
| 545 | FileSpec::Exists () const |
| 546 | { |
| 547 | struct stat file_stats; |
| 548 | return GetFileStats (this, &file_stats); |
| 549 | } |
| 550 | |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 551 | bool |
| 552 | FileSpec::ResolveExecutableLocation () |
| 553 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 554 | if (!m_directory) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 555 | { |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 556 | const char *file_cstr = m_filename.GetCString(); |
| 557 | if (file_cstr) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 558 | { |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 559 | const std::string file_str (file_cstr); |
Rafael Espindola | ff89ff2 | 2013-06-13 19:25:41 +0000 | [diff] [blame] | 560 | std::string path = llvm::sys::FindProgramByName (file_str); |
| 561 | llvm::StringRef dir_ref = llvm::sys::path::parent_path(path); |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 562 | //llvm::StringRef dir_ref = path.getDirname(); |
| 563 | if (! dir_ref.empty()) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 564 | { |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 565 | // FindProgramByName returns "." if it can't find the file. |
| 566 | if (strcmp (".", dir_ref.data()) == 0) |
| 567 | return false; |
| 568 | |
| 569 | m_directory.SetCString (dir_ref.data()); |
| 570 | if (Exists()) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 571 | return true; |
Greg Clayton | 58f4171 | 2011-01-25 21:32:01 +0000 | [diff] [blame] | 572 | else |
| 573 | { |
| 574 | // If FindProgramByName found the file, it returns the directory + filename in its return results. |
| 575 | // We need to separate them. |
| 576 | FileSpec tmp_file (dir_ref.data(), false); |
| 577 | if (tmp_file.Exists()) |
| 578 | { |
| 579 | m_directory = tmp_file.m_directory; |
| 580 | return true; |
| 581 | } |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | return false; |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 590 | bool |
| 591 | FileSpec::ResolvePath () |
| 592 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 593 | if (m_is_resolved) |
| 594 | return true; // We have already resolved this path |
| 595 | |
| 596 | char path_buf[PATH_MAX]; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 597 | if (!GetPath (path_buf, PATH_MAX)) |
| 598 | return false; |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 599 | // 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] | 600 | SetFile (path_buf, true); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 601 | return m_is_resolved; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 604 | uint64_t |
| 605 | FileSpec::GetByteSize() const |
| 606 | { |
| 607 | struct stat file_stats; |
| 608 | if (GetFileStats (this, &file_stats)) |
| 609 | return file_stats.st_size; |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | FileSpec::FileType |
| 614 | FileSpec::GetFileType () const |
| 615 | { |
| 616 | struct stat file_stats; |
| 617 | if (GetFileStats (this, &file_stats)) |
| 618 | { |
| 619 | mode_t file_type = file_stats.st_mode & S_IFMT; |
| 620 | switch (file_type) |
| 621 | { |
| 622 | case S_IFDIR: return eFileTypeDirectory; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | case S_IFREG: return eFileTypeRegular; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame^] | 624 | #ifndef _WIN32 |
| 625 | case S_IFIFO: return eFileTypePipe; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 626 | case S_IFSOCK: return eFileTypeSocket; |
| 627 | case S_IFLNK: return eFileTypeSymbolicLink; |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame^] | 628 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 629 | default: |
| 630 | break; |
| 631 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 632 | return eFileTypeUnknown; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 633 | } |
| 634 | return eFileTypeInvalid; |
| 635 | } |
| 636 | |
| 637 | TimeValue |
| 638 | FileSpec::GetModificationTime () const |
| 639 | { |
| 640 | TimeValue mod_time; |
| 641 | struct stat file_stats; |
| 642 | if (GetFileStats (this, &file_stats)) |
Eli Friedman | 6abb634 | 2010-06-11 04:52:22 +0000 | [diff] [blame] | 643 | mod_time.OffsetWithSeconds(file_stats.st_mtime); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 644 | return mod_time; |
| 645 | } |
| 646 | |
| 647 | //------------------------------------------------------------------ |
| 648 | // Directory string get accessor. |
| 649 | //------------------------------------------------------------------ |
| 650 | ConstString & |
| 651 | FileSpec::GetDirectory() |
| 652 | { |
| 653 | return m_directory; |
| 654 | } |
| 655 | |
| 656 | //------------------------------------------------------------------ |
| 657 | // Directory string const get accessor. |
| 658 | //------------------------------------------------------------------ |
| 659 | const ConstString & |
| 660 | FileSpec::GetDirectory() const |
| 661 | { |
| 662 | return m_directory; |
| 663 | } |
| 664 | |
| 665 | //------------------------------------------------------------------ |
| 666 | // Filename string get accessor. |
| 667 | //------------------------------------------------------------------ |
| 668 | ConstString & |
| 669 | FileSpec::GetFilename() |
| 670 | { |
| 671 | return m_filename; |
| 672 | } |
| 673 | |
| 674 | //------------------------------------------------------------------ |
| 675 | // Filename string const get accessor. |
| 676 | //------------------------------------------------------------------ |
| 677 | const ConstString & |
| 678 | FileSpec::GetFilename() const |
| 679 | { |
| 680 | return m_filename; |
| 681 | } |
| 682 | |
| 683 | //------------------------------------------------------------------ |
| 684 | // Extract the directory and path into a fixed buffer. This is |
| 685 | // needed as the directory and path are stored in separate string |
| 686 | // values. |
| 687 | //------------------------------------------------------------------ |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 688 | size_t |
| 689 | FileSpec::GetPath(char *path, size_t path_max_len) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 690 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 691 | if (path_max_len) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 692 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 693 | const char *dirname = m_directory.GetCString(); |
| 694 | const char *filename = m_filename.GetCString(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 695 | if (dirname) |
| 696 | { |
| 697 | if (filename) |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 698 | return ::snprintf (path, path_max_len, "%s/%s", dirname, filename); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 699 | else |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 700 | return ::snprintf (path, path_max_len, "%s", dirname); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 701 | } |
| 702 | else if (filename) |
| 703 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 704 | return ::snprintf (path, path_max_len, "%s", filename); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 707 | if (path) |
| 708 | path[0] = '\0'; |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 709 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Greg Clayton | a44c1e6 | 2013-04-29 16:36:27 +0000 | [diff] [blame] | 712 | std::string |
| 713 | FileSpec::GetPath (void) const |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 714 | { |
Jason Molenda | db7d11c | 2013-05-06 10:21:11 +0000 | [diff] [blame] | 715 | static ConstString g_slash_only ("/"); |
Greg Clayton | a44c1e6 | 2013-04-29 16:36:27 +0000 | [diff] [blame] | 716 | std::string path; |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 717 | const char *dirname = m_directory.GetCString(); |
| 718 | const char *filename = m_filename.GetCString(); |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 719 | if (dirname) |
| 720 | { |
| 721 | path.append (dirname); |
Jason Molenda | db7d11c | 2013-05-06 10:21:11 +0000 | [diff] [blame] | 722 | if (filename && m_directory != g_slash_only) |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 723 | path.append ("/"); |
| 724 | } |
| 725 | if (filename) |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 726 | path.append (filename); |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 727 | return path; |
| 728 | } |
| 729 | |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 730 | ConstString |
| 731 | FileSpec::GetFileNameExtension () const |
| 732 | { |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 733 | if (m_filename) |
| 734 | { |
| 735 | const char *filename = m_filename.GetCString(); |
| 736 | const char* dot_pos = strrchr(filename, '.'); |
| 737 | if (dot_pos && dot_pos[1] != '\0') |
| 738 | return ConstString(dot_pos+1); |
| 739 | } |
| 740 | return ConstString(); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | ConstString |
| 744 | FileSpec::GetFileNameStrippingExtension () const |
| 745 | { |
| 746 | const char *filename = m_filename.GetCString(); |
| 747 | if (filename == NULL) |
| 748 | return ConstString(); |
| 749 | |
Johnny Chen | f5df537 | 2011-10-18 19:28:30 +0000 | [diff] [blame] | 750 | const char* dot_pos = strrchr(filename, '.'); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 751 | if (dot_pos == NULL) |
| 752 | return m_filename; |
| 753 | |
| 754 | return ConstString(filename, dot_pos-filename); |
| 755 | } |
| 756 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 757 | //------------------------------------------------------------------ |
| 758 | // Returns a shared pointer to a data buffer that contains all or |
| 759 | // part of the contents of a file. The data is memory mapped and |
| 760 | // will lazily page in data from the file as memory is accessed. |
| 761 | // The data that is mappped will start "file_offset" bytes into the |
| 762 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 763 | // greater than the number of bytes available in the file starting |
| 764 | // at "file_offset", the number of bytes will be appropriately |
| 765 | // truncated. The final number of bytes that get mapped can be |
| 766 | // verified using the DataBuffer::GetByteSize() function. |
| 767 | //------------------------------------------------------------------ |
| 768 | DataBufferSP |
| 769 | FileSpec::MemoryMapFileContents(off_t file_offset, size_t file_size) const |
| 770 | { |
| 771 | DataBufferSP data_sp; |
Greg Clayton | 7b0992d | 2013-04-18 22:45:39 +0000 | [diff] [blame] | 772 | std::unique_ptr<DataBufferMemoryMap> mmap_data(new DataBufferMemoryMap()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 773 | if (mmap_data.get()) |
| 774 | { |
Greg Clayton | d398a1c | 2013-04-20 00:23:26 +0000 | [diff] [blame] | 775 | const size_t mapped_length = mmap_data->MemoryMapFromFileSpec (this, file_offset, file_size); |
| 776 | if (((file_size == SIZE_MAX) && (mapped_length > 0)) || (mapped_length >= file_size)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 777 | data_sp.reset(mmap_data.release()); |
| 778 | } |
| 779 | return data_sp; |
| 780 | } |
| 781 | |
| 782 | |
| 783 | //------------------------------------------------------------------ |
| 784 | // Return the size in bytes that this object takes in memory. This |
| 785 | // returns the size in bytes of this object, not any shared string |
| 786 | // values it may refer to. |
| 787 | //------------------------------------------------------------------ |
| 788 | size_t |
| 789 | FileSpec::MemorySize() const |
| 790 | { |
| 791 | return m_filename.MemorySize() + m_directory.MemorySize(); |
| 792 | } |
| 793 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 794 | |
| 795 | size_t |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 796 | 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] | 797 | { |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 798 | Error error; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 799 | size_t bytes_read = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 800 | char resolved_path[PATH_MAX]; |
| 801 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 802 | { |
Greg Clayton | 96c0968 | 2012-01-04 22:56:43 +0000 | [diff] [blame] | 803 | File file; |
| 804 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 805 | if (error.Success()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 806 | { |
Greg Clayton | 96c0968 | 2012-01-04 22:56:43 +0000 | [diff] [blame] | 807 | off_t file_offset_after_seek = file_offset; |
| 808 | bytes_read = dst_len; |
| 809 | error = file.Read(dst, bytes_read, file_offset_after_seek); |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 810 | } |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 811 | } |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 812 | else |
| 813 | { |
| 814 | error.SetErrorString("invalid file specification"); |
| 815 | } |
| 816 | if (error_ptr) |
| 817 | *error_ptr = error; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 818 | return bytes_read; |
| 819 | } |
| 820 | |
| 821 | //------------------------------------------------------------------ |
| 822 | // Returns a shared pointer to a data buffer that contains all or |
| 823 | // part of the contents of a file. The data copies into a heap based |
| 824 | // buffer that lives in the DataBuffer shared pointer object returned. |
| 825 | // The data that is cached will start "file_offset" bytes into the |
| 826 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 827 | // greater than the number of bytes available in the file starting |
| 828 | // at "file_offset", the number of bytes will be appropriately |
| 829 | // truncated. The final number of bytes that get mapped can be |
| 830 | // verified using the DataBuffer::GetByteSize() function. |
| 831 | //------------------------------------------------------------------ |
| 832 | DataBufferSP |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 833 | 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] | 834 | { |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 835 | Error error; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 836 | DataBufferSP data_sp; |
| 837 | char resolved_path[PATH_MAX]; |
| 838 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 839 | { |
Greg Clayton | 96c0968 | 2012-01-04 22:56:43 +0000 | [diff] [blame] | 840 | File file; |
| 841 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 842 | if (error.Success()) |
Greg Clayton | 0b0b512 | 2012-08-30 18:15:10 +0000 | [diff] [blame] | 843 | { |
| 844 | const bool null_terminate = false; |
| 845 | error = file.Read (file_size, file_offset, null_terminate, data_sp); |
| 846 | } |
| 847 | } |
| 848 | else |
| 849 | { |
| 850 | error.SetErrorString("invalid file specification"); |
| 851 | } |
| 852 | if (error_ptr) |
| 853 | *error_ptr = error; |
| 854 | return data_sp; |
| 855 | } |
| 856 | |
| 857 | DataBufferSP |
| 858 | FileSpec::ReadFileContentsAsCString(Error *error_ptr) |
| 859 | { |
| 860 | Error error; |
| 861 | DataBufferSP data_sp; |
| 862 | char resolved_path[PATH_MAX]; |
| 863 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 864 | { |
| 865 | File file; |
| 866 | error = file.Open(resolved_path, File::eOpenOptionRead); |
| 867 | if (error.Success()) |
| 868 | { |
| 869 | off_t offset = 0; |
| 870 | size_t length = SIZE_MAX; |
| 871 | const bool null_terminate = true; |
| 872 | error = file.Read (length, offset, null_terminate, data_sp); |
| 873 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 874 | } |
Greg Clayton | 4017fa3 | 2012-01-06 02:01:06 +0000 | [diff] [blame] | 875 | else |
| 876 | { |
| 877 | error.SetErrorString("invalid file specification"); |
| 878 | } |
| 879 | if (error_ptr) |
| 880 | *error_ptr = error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 881 | return data_sp; |
| 882 | } |
| 883 | |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 884 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 885 | FileSpec::ReadFileLines (STLStringArray &lines) |
| 886 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 887 | lines.clear(); |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 888 | char path[PATH_MAX]; |
| 889 | if (GetPath(path, sizeof(path))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 890 | { |
Greg Clayton | e01e07b | 2013-04-18 18:10:51 +0000 | [diff] [blame] | 891 | std::ifstream file_stream (path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 892 | |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 893 | if (file_stream) |
| 894 | { |
| 895 | std::string line; |
| 896 | while (getline (file_stream, line)) |
| 897 | lines.push_back (line); |
| 898 | } |
| 899 | } |
| 900 | return lines.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 901 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 902 | |
| 903 | FileSpec::EnumerateDirectoryResult |
| 904 | FileSpec::EnumerateDirectory |
| 905 | ( |
| 906 | const char *dir_path, |
| 907 | bool find_directories, |
| 908 | bool find_files, |
| 909 | bool find_other, |
| 910 | EnumerateDirectoryCallbackType callback, |
| 911 | void *callback_baton |
| 912 | ) |
| 913 | { |
| 914 | if (dir_path && dir_path[0]) |
| 915 | { |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame^] | 916 | #if _WIN32 |
| 917 | char szDir[MAX_PATH]; |
| 918 | strcpy_s(szDir, MAX_PATH, dir_path); |
| 919 | strcat_s(szDir, MAX_PATH, "\\*"); |
| 920 | |
| 921 | WIN32_FIND_DATA ffd; |
| 922 | HANDLE hFind = FindFirstFile(szDir, &ffd); |
| 923 | |
| 924 | if (hFind == INVALID_HANDLE_VALUE) |
| 925 | { |
| 926 | return eEnumerateDirectoryResultNext; |
| 927 | } |
| 928 | |
| 929 | do |
| 930 | { |
| 931 | bool call_callback = false; |
| 932 | FileSpec::FileType file_type = eFileTypeUnknown; |
| 933 | if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 934 | { |
| 935 | size_t len = strlen(ffd.cFileName); |
| 936 | |
| 937 | if (len == 1 && ffd.cFileName[0] == '.') |
| 938 | continue; |
| 939 | |
| 940 | if (len == 2 && ffd.cFileName[0] == '.' && ffd.cFileName[1] == '.') |
| 941 | continue; |
| 942 | |
| 943 | file_type = eFileTypeDirectory; |
| 944 | call_callback = find_directories; |
| 945 | } |
| 946 | else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) |
| 947 | { |
| 948 | file_type = eFileTypeOther; |
| 949 | call_callback = find_other; |
| 950 | } |
| 951 | else |
| 952 | { |
| 953 | file_type = eFileTypeRegular; |
| 954 | call_callback = find_files; |
| 955 | } |
| 956 | if (call_callback) |
| 957 | { |
| 958 | char child_path[MAX_PATH]; |
| 959 | const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s\\%s", dir_path, ffd.cFileName); |
| 960 | if (child_path_len < (int)(sizeof(child_path) - 1)) |
| 961 | { |
| 962 | // Don't resolve the file type or path |
| 963 | FileSpec child_path_spec (child_path, false); |
| 964 | |
| 965 | EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec); |
| 966 | |
| 967 | switch (result) |
| 968 | { |
| 969 | case eEnumerateDirectoryResultNext: |
| 970 | // Enumerate next entry in the current directory. We just |
| 971 | // exit this switch and will continue enumerating the |
| 972 | // current directory as we currently are... |
| 973 | break; |
| 974 | |
| 975 | case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not |
| 976 | if (FileSpec::EnumerateDirectory(child_path, |
| 977 | find_directories, |
| 978 | find_files, |
| 979 | find_other, |
| 980 | callback, |
| 981 | callback_baton) == eEnumerateDirectoryResultQuit) |
| 982 | { |
| 983 | // The subdirectory returned Quit, which means to |
| 984 | // stop all directory enumerations at all levels. |
| 985 | return eEnumerateDirectoryResultQuit; |
| 986 | } |
| 987 | break; |
| 988 | |
| 989 | case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level. |
| 990 | // Exit from this directory level and tell parent to |
| 991 | // keep enumerating. |
| 992 | return eEnumerateDirectoryResultNext; |
| 993 | |
| 994 | case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level |
| 995 | return eEnumerateDirectoryResultQuit; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | } while (FindNextFile(hFind, &ffd) != 0); |
| 1000 | |
| 1001 | FindClose(hFind); |
| 1002 | #else |
| 1003 | 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] | 1004 | if (dir_path_dir.is_valid()) |
| 1005 | { |
Jason Molenda | 14aef12 | 2013-04-04 03:19:27 +0000 | [diff] [blame] | 1006 | long path_max = fpathconf (dirfd (dir_path_dir.get()), _PC_NAME_MAX); |
| 1007 | #if defined (__APPLE_) && defined (__DARWIN_MAXPATHLEN) |
| 1008 | if (path_max < __DARWIN_MAXPATHLEN) |
| 1009 | path_max = __DARWIN_MAXPATHLEN; |
| 1010 | #endif |
| 1011 | struct dirent *buf, *dp; |
| 1012 | buf = (struct dirent *) malloc (offsetof (struct dirent, d_name) + path_max + 1); |
| 1013 | |
| 1014 | while (buf && readdir_r(dir_path_dir.get(), buf, &dp) == 0 && dp) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1015 | { |
| 1016 | // Only search directories |
| 1017 | if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) |
| 1018 | { |
Greg Clayton | e0f3c02 | 2011-02-07 17:41:11 +0000 | [diff] [blame] | 1019 | size_t len = strlen(dp->d_name); |
| 1020 | |
| 1021 | if (len == 1 && dp->d_name[0] == '.') |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1022 | continue; |
| 1023 | |
Greg Clayton | e0f3c02 | 2011-02-07 17:41:11 +0000 | [diff] [blame] | 1024 | if (len == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1025 | continue; |
| 1026 | } |
| 1027 | |
| 1028 | bool call_callback = false; |
| 1029 | FileSpec::FileType file_type = eFileTypeUnknown; |
| 1030 | |
| 1031 | switch (dp->d_type) |
| 1032 | { |
| 1033 | default: |
| 1034 | case DT_UNKNOWN: file_type = eFileTypeUnknown; call_callback = true; break; |
| 1035 | case DT_FIFO: file_type = eFileTypePipe; call_callback = find_other; break; |
| 1036 | case DT_CHR: file_type = eFileTypeOther; call_callback = find_other; break; |
| 1037 | case DT_DIR: file_type = eFileTypeDirectory; call_callback = find_directories; break; |
| 1038 | case DT_BLK: file_type = eFileTypeOther; call_callback = find_other; break; |
| 1039 | case DT_REG: file_type = eFileTypeRegular; call_callback = find_files; break; |
| 1040 | case DT_LNK: file_type = eFileTypeSymbolicLink; call_callback = find_other; break; |
| 1041 | case DT_SOCK: file_type = eFileTypeSocket; call_callback = find_other; break; |
Greg Clayton | 2b4d9b7 | 2011-04-01 18:18:34 +0000 | [diff] [blame] | 1042 | #if !defined(__OpenBSD__) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1043 | case DT_WHT: file_type = eFileTypeOther; call_callback = find_other; break; |
Greg Clayton | 2b4d9b7 | 2011-04-01 18:18:34 +0000 | [diff] [blame] | 1044 | #endif |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | if (call_callback) |
| 1048 | { |
| 1049 | char child_path[PATH_MAX]; |
| 1050 | const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s/%s", dir_path, dp->d_name); |
Johnny Chen | 4480530 | 2011-07-19 19:48:13 +0000 | [diff] [blame] | 1051 | if (child_path_len < (int)(sizeof(child_path) - 1)) |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1052 | { |
| 1053 | // Don't resolve the file type or path |
| 1054 | FileSpec child_path_spec (child_path, false); |
| 1055 | |
| 1056 | EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec); |
| 1057 | |
| 1058 | switch (result) |
| 1059 | { |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1060 | case eEnumerateDirectoryResultNext: |
| 1061 | // Enumerate next entry in the current directory. We just |
| 1062 | // exit this switch and will continue enumerating the |
| 1063 | // current directory as we currently are... |
| 1064 | break; |
| 1065 | |
| 1066 | case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not |
| 1067 | if (FileSpec::EnumerateDirectory (child_path, |
| 1068 | find_directories, |
| 1069 | find_files, |
| 1070 | find_other, |
| 1071 | callback, |
| 1072 | callback_baton) == eEnumerateDirectoryResultQuit) |
| 1073 | { |
| 1074 | // The subdirectory returned Quit, which means to |
| 1075 | // stop all directory enumerations at all levels. |
Jim Ingham | 5c42d8a | 2013-05-15 18:27:08 +0000 | [diff] [blame] | 1076 | if (buf) |
| 1077 | free (buf); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1078 | return eEnumerateDirectoryResultQuit; |
| 1079 | } |
| 1080 | break; |
| 1081 | |
| 1082 | case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level. |
| 1083 | // Exit from this directory level and tell parent to |
| 1084 | // keep enumerating. |
Jason Molenda | fe80690 | 2013-05-04 00:39:52 +0000 | [diff] [blame] | 1085 | if (buf) |
| 1086 | free (buf); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1087 | return eEnumerateDirectoryResultNext; |
| 1088 | |
| 1089 | case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level |
Jason Molenda | fe80690 | 2013-05-04 00:39:52 +0000 | [diff] [blame] | 1090 | if (buf) |
| 1091 | free (buf); |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1092 | return eEnumerateDirectoryResultQuit; |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | } |
Jason Molenda | 14aef12 | 2013-04-04 03:19:27 +0000 | [diff] [blame] | 1097 | if (buf) |
| 1098 | { |
| 1099 | free (buf); |
| 1100 | } |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1101 | } |
Virgile Bello | b2f1fb2 | 2013-08-23 12:44:05 +0000 | [diff] [blame^] | 1102 | #endif |
Greg Clayton | 4272cc7 | 2011-02-02 02:24:04 +0000 | [diff] [blame] | 1103 | } |
| 1104 | // By default when exiting a directory, we tell the parent enumeration |
| 1105 | // to continue enumerating. |
| 1106 | return eEnumerateDirectoryResultNext; |
| 1107 | } |
| 1108 | |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 1109 | //------------------------------------------------------------------ |
| 1110 | /// Returns true if the filespec represents an implementation source |
| 1111 | /// file (files with a ".c", ".cpp", ".m", ".mm" (many more) |
| 1112 | /// extension). |
| 1113 | /// |
| 1114 | /// @return |
| 1115 | /// \b true if the filespec represents an implementation source |
| 1116 | /// file, \b false otherwise. |
| 1117 | //------------------------------------------------------------------ |
| 1118 | bool |
| 1119 | FileSpec::IsSourceImplementationFile () const |
| 1120 | { |
| 1121 | ConstString extension (GetFileNameExtension()); |
| 1122 | if (extension) |
| 1123 | { |
| 1124 | 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)$", |
| 1125 | REG_EXTENDED | REG_ICASE); |
| 1126 | return g_source_file_regex.Execute (extension.GetCString()); |
| 1127 | } |
| 1128 | return false; |
| 1129 | } |
| 1130 | |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 1131 | bool |
| 1132 | FileSpec::IsRelativeToCurrentWorkingDirectory () const |
| 1133 | { |
| 1134 | const char *directory = m_directory.GetCString(); |
| 1135 | if (directory && directory[0]) |
| 1136 | { |
| 1137 | // If the path doesn't start with '/' or '~', return true |
| 1138 | switch (directory[0]) |
| 1139 | { |
| 1140 | case '/': |
| 1141 | case '~': |
| 1142 | return false; |
| 1143 | default: |
| 1144 | return true; |
| 1145 | } |
| 1146 | } |
| 1147 | else if (m_filename) |
| 1148 | { |
| 1149 | // No directory, just a basename, return true |
| 1150 | return true; |
| 1151 | } |
| 1152 | return false; |
| 1153 | } |