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