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