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 | |
| 11 | #include <fcntl.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include <libgen.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <sys/param.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <sys/types.h> |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 17 | #include <pwd.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
| 19 | #include <fstream> |
| 20 | |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/System/Path.h" |
| 23 | #include "llvm/System/Program.h" |
| 24 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | #include "lldb/Core/FileSpec.h" |
| 26 | #include "lldb/Core/DataBufferHeap.h" |
| 27 | #include "lldb/Core/DataBufferMemoryMap.h" |
| 28 | #include "lldb/Core/Stream.h" |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Host.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | using namespace std; |
| 34 | |
| 35 | static bool |
| 36 | GetFileStats (const FileSpec *file_spec, struct stat *stats_ptr) |
| 37 | { |
| 38 | char resolved_path[PATH_MAX]; |
| 39 | if (file_spec->GetPath(&resolved_path[0], sizeof(resolved_path))) |
| 40 | return ::stat (resolved_path, stats_ptr) == 0; |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | static const char* |
| 45 | GetCachedGlobTildeSlash() |
| 46 | { |
| 47 | static std::string g_tilde; |
| 48 | if (g_tilde.empty()) |
| 49 | { |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 50 | struct passwd *user_entry; |
| 51 | user_entry = getpwuid(geteuid()); |
| 52 | if (user_entry != NULL) |
| 53 | g_tilde = user_entry->pw_dir; |
| 54 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | if (g_tilde.empty()) |
| 56 | return NULL; |
| 57 | } |
| 58 | return g_tilde.c_str(); |
| 59 | } |
| 60 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 61 | // Resolves the username part of a path of the form ~user/other/directories, and |
| 62 | // writes the result into dst_path. |
| 63 | // Returns 0 if there WAS a ~ in the path but the username couldn't be resolved. |
| 64 | // Otherwise returns the number of characters copied into dst_path. If the return |
| 65 | // is >= dst_len, then the resolved path is too long... |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 66 | size_t |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 67 | FileSpec::ResolveUsername (const char *src_path, char *dst_path, size_t dst_len) |
| 68 | { |
| 69 | char user_home[PATH_MAX]; |
| 70 | const char *user_name; |
| 71 | |
| 72 | if (src_path == NULL || src_path[0] == '\0') |
| 73 | return 0; |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 74 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 75 | // If there's no ~, then just copy src_path straight to dst_path (they may be the same string...) |
| 76 | if (src_path[0] != '~') |
| 77 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 78 | size_t len = strlen (src_path); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 79 | if (len >= dst_len) |
| 80 | { |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 81 | ::bcopy (src_path, dst_path, dst_len - 1); |
| 82 | dst_path[dst_len] = '\0'; |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 83 | } |
| 84 | else |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 85 | ::bcopy (src_path, dst_path, len + 1); |
| 86 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 87 | return len; |
| 88 | } |
| 89 | |
Eli Friedman | feaeebf | 2010-07-02 19:15:50 +0000 | [diff] [blame] | 90 | const char *first_slash = ::strchr (src_path, '/'); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 91 | char remainder[PATH_MAX]; |
| 92 | |
| 93 | if (first_slash == NULL) |
| 94 | { |
| 95 | // The whole name is the username (minus the ~): |
| 96 | user_name = src_path + 1; |
| 97 | remainder[0] = '\0'; |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | int user_name_len = first_slash - src_path - 1; |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 102 | ::memcpy (user_home, src_path + 1, user_name_len); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 103 | user_home[user_name_len] = '\0'; |
| 104 | user_name = user_home; |
| 105 | |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 106 | ::strcpy (remainder, first_slash); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 107 | } |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 108 | |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 109 | if (user_name == NULL) |
| 110 | return 0; |
| 111 | // User name of "" means the current user... |
| 112 | |
| 113 | struct passwd *user_entry; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 114 | const char *home_dir = NULL; |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 115 | |
| 116 | if (user_name[0] == '\0') |
| 117 | { |
| 118 | home_dir = GetCachedGlobTildeSlash(); |
| 119 | } |
| 120 | else |
| 121 | { |
Greg Clayton | a5d24f6 | 2010-07-01 17:07:48 +0000 | [diff] [blame] | 122 | user_entry = ::getpwnam (user_name); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 123 | if (user_entry != NULL) |
| 124 | home_dir = user_entry->pw_dir; |
| 125 | } |
| 126 | |
| 127 | if (home_dir == NULL) |
| 128 | return 0; |
| 129 | else |
| 130 | return ::snprintf (dst_path, dst_len, "%s%s", home_dir, remainder); |
| 131 | } |
| 132 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 133 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len) |
| 135 | { |
| 136 | if (src_path == NULL || src_path[0] == '\0') |
| 137 | return 0; |
| 138 | |
| 139 | // Glob if needed for ~/, otherwise copy in case src_path is same as dst_path... |
| 140 | char unglobbed_path[PATH_MAX]; |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 141 | if (src_path[0] == '~') |
| 142 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 143 | size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path)); |
Jim Ingham | f818ca3 | 2010-07-01 01:48:53 +0000 | [diff] [blame] | 144 | |
| 145 | // If we couldn't find the user referred to, or the resultant path was too long, |
| 146 | // then just copy over the src_path. |
| 147 | if (return_count == 0 || return_count >= sizeof(unglobbed_path)) |
| 148 | ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", src_path); |
| 149 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | else |
| 151 | ::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path); |
| 152 | |
| 153 | // Now resolve the path if needed |
| 154 | char resolved_path[PATH_MAX]; |
| 155 | if (::realpath (unglobbed_path, resolved_path)) |
| 156 | { |
| 157 | // Success, copy the resolved path |
| 158 | return ::snprintf(dst_path, dst_len, "%s", resolved_path); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | // Failed, just copy the unglobbed path |
| 163 | return ::snprintf(dst_path, dst_len, "%s", unglobbed_path); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | FileSpec::FileSpec() : |
| 168 | m_directory(), |
| 169 | m_filename() |
| 170 | { |
| 171 | } |
| 172 | |
| 173 | //------------------------------------------------------------------ |
| 174 | // Default constructor that can take an optional full path to a |
| 175 | // file on disk. |
| 176 | //------------------------------------------------------------------ |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 177 | FileSpec::FileSpec(const char *pathname, bool resolve_path) : |
| 178 | m_directory(), |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 179 | m_filename(), |
| 180 | m_is_resolved(false) |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 181 | { |
| 182 | if (pathname && pathname[0]) |
| 183 | SetFile(pathname, resolve_path); |
| 184 | } |
| 185 | |
| 186 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | // Copy constructor |
| 188 | //------------------------------------------------------------------ |
| 189 | FileSpec::FileSpec(const FileSpec& rhs) : |
| 190 | m_directory (rhs.m_directory), |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 191 | m_filename (rhs.m_filename), |
| 192 | m_is_resolved (rhs.m_is_resolved) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | { |
| 194 | } |
| 195 | |
| 196 | //------------------------------------------------------------------ |
| 197 | // Copy constructor |
| 198 | //------------------------------------------------------------------ |
| 199 | FileSpec::FileSpec(const FileSpec* rhs) : |
| 200 | m_directory(), |
| 201 | m_filename() |
| 202 | { |
| 203 | if (rhs) |
| 204 | *this = *rhs; |
| 205 | } |
| 206 | |
| 207 | //------------------------------------------------------------------ |
| 208 | // Virtual destrcuctor in case anyone inherits from this class. |
| 209 | //------------------------------------------------------------------ |
| 210 | FileSpec::~FileSpec() |
| 211 | { |
| 212 | } |
| 213 | |
| 214 | //------------------------------------------------------------------ |
| 215 | // Assignment operator. |
| 216 | //------------------------------------------------------------------ |
| 217 | const FileSpec& |
| 218 | FileSpec::operator= (const FileSpec& rhs) |
| 219 | { |
| 220 | if (this != &rhs) |
| 221 | { |
| 222 | m_directory = rhs.m_directory; |
| 223 | m_filename = rhs.m_filename; |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 224 | m_is_resolved = rhs.m_is_resolved; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 225 | } |
| 226 | return *this; |
| 227 | } |
| 228 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | //------------------------------------------------------------------ |
| 230 | // Update the contents of this object with a new path. The path will |
| 231 | // be split up into a directory and filename and stored as uniqued |
| 232 | // string values for quick comparison and efficient memory usage. |
| 233 | //------------------------------------------------------------------ |
| 234 | void |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 235 | FileSpec::SetFile (const char *pathname, bool resolve) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | { |
| 237 | m_filename.Clear(); |
| 238 | m_directory.Clear(); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 239 | m_is_resolved = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 240 | if (pathname == NULL || pathname[0] == '\0') |
| 241 | return; |
| 242 | |
| 243 | char resolved_path[PATH_MAX]; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 244 | bool path_fit = true; |
| 245 | |
| 246 | if (resolve) |
| 247 | { |
| 248 | 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] | 249 | m_is_resolved = path_fit; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 250 | } |
| 251 | else |
| 252 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 253 | // Copy the path because "basename" and "dirname" want to muck with the |
| 254 | // path buffer |
| 255 | if (::strlen (pathname) > sizeof(resolved_path) - 1) |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 256 | path_fit = false; |
| 257 | else |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 258 | ::strcpy (resolved_path, pathname); |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 259 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 261 | |
| 262 | if (path_fit) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | { |
| 264 | char *filename = ::basename (resolved_path); |
| 265 | if (filename) |
| 266 | { |
| 267 | m_filename.SetCString (filename); |
| 268 | // Truncate the basename off the end of the resolved path |
| 269 | |
| 270 | // Only attempt to get the dirname if it looks like we have a path |
| 271 | if (strchr(resolved_path, '/')) |
| 272 | { |
| 273 | char *directory = ::dirname (resolved_path); |
| 274 | |
| 275 | // Make sure we didn't get our directory resolved to "." without having |
| 276 | // specified |
| 277 | if (directory) |
| 278 | m_directory.SetCString(directory); |
| 279 | else |
| 280 | { |
| 281 | char *last_resolved_path_slash = strrchr(resolved_path, '/'); |
| 282 | if (last_resolved_path_slash) |
| 283 | { |
| 284 | *last_resolved_path_slash = '\0'; |
| 285 | m_directory.SetCString(resolved_path); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | else |
| 291 | m_directory.SetCString(resolved_path); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | //---------------------------------------------------------------------- |
| 296 | // Convert to pointer operator. This allows code to check any FileSpec |
| 297 | // objects to see if they contain anything valid using code such as: |
| 298 | // |
| 299 | // if (file_spec) |
| 300 | // {} |
| 301 | //---------------------------------------------------------------------- |
| 302 | FileSpec::operator |
| 303 | void*() const |
| 304 | { |
| 305 | return (m_directory || m_filename) ? const_cast<FileSpec*>(this) : NULL; |
| 306 | } |
| 307 | |
| 308 | //---------------------------------------------------------------------- |
| 309 | // Logical NOT operator. This allows code to check any FileSpec |
| 310 | // objects to see if they are invalid using code such as: |
| 311 | // |
| 312 | // if (!file_spec) |
| 313 | // {} |
| 314 | //---------------------------------------------------------------------- |
| 315 | bool |
| 316 | FileSpec::operator!() const |
| 317 | { |
| 318 | return !m_directory && !m_filename; |
| 319 | } |
| 320 | |
| 321 | //------------------------------------------------------------------ |
| 322 | // Equal to operator |
| 323 | //------------------------------------------------------------------ |
| 324 | bool |
| 325 | FileSpec::operator== (const FileSpec& rhs) const |
| 326 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 327 | if (m_filename == rhs.m_filename) |
| 328 | { |
| 329 | if (m_directory == rhs.m_directory) |
| 330 | return true; |
| 331 | |
| 332 | // TODO: determine if we want to keep this code in here. |
| 333 | // The code below was added to handle a case where we were |
| 334 | // trying to set a file and line breakpoint and one path |
| 335 | // was resolved, and the other not and the directory was |
| 336 | // in a mount point that resolved to a more complete path: |
| 337 | // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling |
| 338 | // this out... |
| 339 | if (IsResolved() && rhs.IsResolved()) |
| 340 | { |
| 341 | // Both paths are resolved, no need to look further... |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | FileSpec resolved_lhs(*this); |
| 346 | |
| 347 | // If "this" isn't resolved, resolve it |
| 348 | if (!IsResolved()) |
| 349 | { |
| 350 | if (resolved_lhs.ResolvePath()) |
| 351 | { |
| 352 | // This path wasn't resolved but now it is. Check if the resolved |
| 353 | // directory is the same as our unresolved directory, and if so, |
| 354 | // we can mark this object as resolved to avoid more future resolves |
| 355 | m_is_resolved = (m_directory == resolved_lhs.m_directory); |
| 356 | } |
| 357 | else |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | FileSpec resolved_rhs(rhs); |
| 362 | if (!rhs.IsResolved()) |
| 363 | { |
| 364 | if (resolved_rhs.ResolvePath()) |
| 365 | { |
| 366 | // rhs's path wasn't resolved but now it is. Check if the resolved |
| 367 | // directory is the same as rhs's unresolved directory, and if so, |
| 368 | // we can mark this object as resolved to avoid more future resolves |
| 369 | rhs.m_is_resolved = (m_directory == resolved_rhs.m_directory); |
| 370 | } |
| 371 | else |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | // If we reach this point in the code we were able to resolve both paths |
| 376 | // and since we only resolve the paths if the basenames are equal, then |
| 377 | // we can just check if both directories are equal... |
| 378 | return resolved_lhs.GetDirectory() == resolved_rhs.GetDirectory(); |
| 379 | } |
| 380 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | //------------------------------------------------------------------ |
| 384 | // Not equal to operator |
| 385 | //------------------------------------------------------------------ |
| 386 | bool |
| 387 | FileSpec::operator!= (const FileSpec& rhs) const |
| 388 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 389 | return !(*this == rhs); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | //------------------------------------------------------------------ |
| 393 | // Less than operator |
| 394 | //------------------------------------------------------------------ |
| 395 | bool |
| 396 | FileSpec::operator< (const FileSpec& rhs) const |
| 397 | { |
| 398 | return FileSpec::Compare(*this, rhs, true) < 0; |
| 399 | } |
| 400 | |
| 401 | //------------------------------------------------------------------ |
| 402 | // Dump a FileSpec object to a stream |
| 403 | //------------------------------------------------------------------ |
| 404 | Stream& |
| 405 | lldb_private::operator << (Stream &s, const FileSpec& f) |
| 406 | { |
| 407 | f.Dump(&s); |
| 408 | return s; |
| 409 | } |
| 410 | |
| 411 | //------------------------------------------------------------------ |
| 412 | // Clear this object by releasing both the directory and filename |
| 413 | // string values and making them both the empty string. |
| 414 | //------------------------------------------------------------------ |
| 415 | void |
| 416 | FileSpec::Clear() |
| 417 | { |
| 418 | m_directory.Clear(); |
| 419 | m_filename.Clear(); |
| 420 | } |
| 421 | |
| 422 | //------------------------------------------------------------------ |
| 423 | // Compare two FileSpec objects. If "full" is true, then both |
| 424 | // the directory and the filename must match. If "full" is false, |
| 425 | // then the directory names for "a" and "b" are only compared if |
| 426 | // they are both non-empty. This allows a FileSpec object to only |
| 427 | // contain a filename and it can match FileSpec objects that have |
| 428 | // matching filenames with different paths. |
| 429 | // |
| 430 | // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" |
| 431 | // and "1" if "a" is greater than "b". |
| 432 | //------------------------------------------------------------------ |
| 433 | int |
| 434 | FileSpec::Compare(const FileSpec& a, const FileSpec& b, bool full) |
| 435 | { |
| 436 | int result = 0; |
| 437 | |
| 438 | // If full is true, then we must compare both the directory and filename. |
| 439 | |
| 440 | // If full is false, then if either directory is empty, then we match on |
| 441 | // the basename only, and if both directories have valid values, we still |
| 442 | // do a full compare. This allows for matching when we just have a filename |
| 443 | // in one of the FileSpec objects. |
| 444 | |
| 445 | if (full || (a.m_directory && b.m_directory)) |
| 446 | { |
| 447 | result = ConstString::Compare(a.m_directory, b.m_directory); |
| 448 | if (result) |
| 449 | return result; |
| 450 | } |
| 451 | return ConstString::Compare (a.m_filename, b.m_filename); |
| 452 | } |
| 453 | |
| 454 | bool |
| 455 | FileSpec::Equal (const FileSpec& a, const FileSpec& b, bool full) |
| 456 | { |
| 457 | if (full) |
| 458 | return a == b; |
| 459 | else |
| 460 | return a.m_filename == b.m_filename; |
| 461 | } |
| 462 | |
| 463 | |
| 464 | |
| 465 | //------------------------------------------------------------------ |
| 466 | // Dump the object to the supplied stream. If the object contains |
| 467 | // a valid directory name, it will be displayed followed by a |
| 468 | // directory delimiter, and the filename. |
| 469 | //------------------------------------------------------------------ |
| 470 | void |
| 471 | FileSpec::Dump(Stream *s) const |
| 472 | { |
| 473 | if (m_filename) |
| 474 | m_directory.Dump(s, ""); // Provide a default for m_directory when we dump it in case it is invalid |
| 475 | |
| 476 | if (m_directory) |
| 477 | { |
| 478 | // If dirname was valid, then we need to print a slash between |
| 479 | // the directory and the filename |
| 480 | s->PutChar('/'); |
| 481 | } |
| 482 | m_filename.Dump(s); |
| 483 | } |
| 484 | |
| 485 | //------------------------------------------------------------------ |
| 486 | // Returns true if the file exists. |
| 487 | //------------------------------------------------------------------ |
| 488 | bool |
| 489 | FileSpec::Exists () const |
| 490 | { |
| 491 | struct stat file_stats; |
| 492 | return GetFileStats (this, &file_stats); |
| 493 | } |
| 494 | |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 495 | bool |
| 496 | FileSpec::ResolveExecutableLocation () |
| 497 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 498 | if (!m_directory) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 499 | { |
| 500 | const std::string file_str (m_filename.AsCString()); |
| 501 | llvm::sys::Path path = llvm::sys::Program::FindProgramByName (file_str); |
| 502 | llvm::StringRef dir_ref = path.getDirname(); |
| 503 | if (! dir_ref.empty()) |
| 504 | { |
| 505 | // FindProgramByName returns "." if it can't find the file. |
| 506 | if (strcmp (".", dir_ref.data()) == 0) |
| 507 | return false; |
| 508 | |
| 509 | m_directory.SetCString (dir_ref.data()); |
| 510 | if (Exists()) |
| 511 | return true; |
| 512 | else |
| 513 | { |
| 514 | // If FindProgramByName found the file, it returns the directory + filename in its return results. |
| 515 | // We need to separate them. |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 516 | FileSpec tmp_file (dir_ref.data(), false); |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 517 | if (tmp_file.Exists()) |
| 518 | { |
| 519 | m_directory = tmp_file.m_directory; |
| 520 | return true; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | return false; |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 529 | bool |
| 530 | FileSpec::ResolvePath () |
| 531 | { |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 532 | if (m_is_resolved) |
| 533 | return true; // We have already resolved this path |
| 534 | |
| 535 | char path_buf[PATH_MAX]; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 536 | if (!GetPath (path_buf, PATH_MAX)) |
| 537 | return false; |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 538 | // 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] | 539 | SetFile (path_buf, true); |
Greg Clayton | 7481c20 | 2010-11-08 00:28:40 +0000 | [diff] [blame] | 540 | return m_is_resolved; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 543 | uint64_t |
| 544 | FileSpec::GetByteSize() const |
| 545 | { |
| 546 | struct stat file_stats; |
| 547 | if (GetFileStats (this, &file_stats)) |
| 548 | return file_stats.st_size; |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | FileSpec::FileType |
| 553 | FileSpec::GetFileType () const |
| 554 | { |
| 555 | struct stat file_stats; |
| 556 | if (GetFileStats (this, &file_stats)) |
| 557 | { |
| 558 | mode_t file_type = file_stats.st_mode & S_IFMT; |
| 559 | switch (file_type) |
| 560 | { |
| 561 | case S_IFDIR: return eFileTypeDirectory; |
| 562 | case S_IFIFO: return eFileTypePipe; |
| 563 | case S_IFREG: return eFileTypeRegular; |
| 564 | case S_IFSOCK: return eFileTypeSocket; |
| 565 | case S_IFLNK: return eFileTypeSymbolicLink; |
| 566 | default: |
| 567 | break; |
| 568 | } |
| 569 | return eFileTypeUknown; |
| 570 | } |
| 571 | return eFileTypeInvalid; |
| 572 | } |
| 573 | |
| 574 | TimeValue |
| 575 | FileSpec::GetModificationTime () const |
| 576 | { |
| 577 | TimeValue mod_time; |
| 578 | struct stat file_stats; |
| 579 | if (GetFileStats (this, &file_stats)) |
Eli Friedman | 6abb634 | 2010-06-11 04:52:22 +0000 | [diff] [blame] | 580 | mod_time.OffsetWithSeconds(file_stats.st_mtime); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 581 | return mod_time; |
| 582 | } |
| 583 | |
| 584 | //------------------------------------------------------------------ |
| 585 | // Directory string get accessor. |
| 586 | //------------------------------------------------------------------ |
| 587 | ConstString & |
| 588 | FileSpec::GetDirectory() |
| 589 | { |
| 590 | return m_directory; |
| 591 | } |
| 592 | |
| 593 | //------------------------------------------------------------------ |
| 594 | // Directory string const get accessor. |
| 595 | //------------------------------------------------------------------ |
| 596 | const ConstString & |
| 597 | FileSpec::GetDirectory() const |
| 598 | { |
| 599 | return m_directory; |
| 600 | } |
| 601 | |
| 602 | //------------------------------------------------------------------ |
| 603 | // Filename string get accessor. |
| 604 | //------------------------------------------------------------------ |
| 605 | ConstString & |
| 606 | FileSpec::GetFilename() |
| 607 | { |
| 608 | return m_filename; |
| 609 | } |
| 610 | |
| 611 | //------------------------------------------------------------------ |
| 612 | // Filename string const get accessor. |
| 613 | //------------------------------------------------------------------ |
| 614 | const ConstString & |
| 615 | FileSpec::GetFilename() const |
| 616 | { |
| 617 | return m_filename; |
| 618 | } |
| 619 | |
| 620 | //------------------------------------------------------------------ |
| 621 | // Extract the directory and path into a fixed buffer. This is |
| 622 | // needed as the directory and path are stored in separate string |
| 623 | // values. |
| 624 | //------------------------------------------------------------------ |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 625 | size_t |
| 626 | FileSpec::GetPath(char *path, size_t path_max_len) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 628 | if (path_max_len) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 629 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 630 | const char *dirname = m_directory.GetCString(); |
| 631 | const char *filename = m_filename.GetCString(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 632 | if (dirname) |
| 633 | { |
| 634 | if (filename) |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 635 | return ::snprintf (path, path_max_len, "%s/%s", dirname, filename); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 636 | else |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 637 | return ::snprintf (path, path_max_len, "%s", dirname); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 638 | } |
| 639 | else if (filename) |
| 640 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 641 | return ::snprintf (path, path_max_len, "%s", filename); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 644 | path[0] = '\0'; |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 645 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | //------------------------------------------------------------------ |
| 649 | // Returns a shared pointer to a data buffer that contains all or |
| 650 | // part of the contents of a file. The data is memory mapped and |
| 651 | // will lazily page in data from the file as memory is accessed. |
| 652 | // The data that is mappped will start "file_offset" bytes into the |
| 653 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 654 | // greater than the number of bytes available in the file starting |
| 655 | // at "file_offset", the number of bytes will be appropriately |
| 656 | // truncated. The final number of bytes that get mapped can be |
| 657 | // verified using the DataBuffer::GetByteSize() function. |
| 658 | //------------------------------------------------------------------ |
| 659 | DataBufferSP |
| 660 | FileSpec::MemoryMapFileContents(off_t file_offset, size_t file_size) const |
| 661 | { |
| 662 | DataBufferSP data_sp; |
| 663 | auto_ptr<DataBufferMemoryMap> mmap_data(new DataBufferMemoryMap()); |
| 664 | if (mmap_data.get()) |
| 665 | { |
| 666 | if (mmap_data->MemoryMapFromFileSpec (this, file_offset, file_size) >= file_size) |
| 667 | data_sp.reset(mmap_data.release()); |
| 668 | } |
| 669 | return data_sp; |
| 670 | } |
| 671 | |
| 672 | |
| 673 | //------------------------------------------------------------------ |
| 674 | // Return the size in bytes that this object takes in memory. This |
| 675 | // returns the size in bytes of this object, not any shared string |
| 676 | // values it may refer to. |
| 677 | //------------------------------------------------------------------ |
| 678 | size_t |
| 679 | FileSpec::MemorySize() const |
| 680 | { |
| 681 | return m_filename.MemorySize() + m_directory.MemorySize(); |
| 682 | } |
| 683 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 684 | |
| 685 | size_t |
| 686 | FileSpec::ReadFileContents (off_t file_offset, void *dst, size_t dst_len) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 687 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 688 | size_t bytes_read = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 689 | char resolved_path[PATH_MAX]; |
| 690 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 691 | { |
| 692 | int fd = ::open (resolved_path, O_RDONLY, 0); |
| 693 | if (fd != -1) |
| 694 | { |
| 695 | struct stat file_stats; |
| 696 | if (::fstat (fd, &file_stats) == 0) |
| 697 | { |
| 698 | // Read bytes directly into our basic_string buffer |
| 699 | if (file_stats.st_size > 0) |
| 700 | { |
| 701 | off_t lseek_result = 0; |
| 702 | if (file_offset > 0) |
| 703 | lseek_result = ::lseek (fd, file_offset, SEEK_SET); |
| 704 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 705 | if (lseek_result == file_offset) |
| 706 | { |
| 707 | ssize_t n = ::read (fd, dst, dst_len); |
| 708 | if (n >= 0) |
| 709 | bytes_read = n; |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | close(fd); |
| 715 | } |
| 716 | return bytes_read; |
| 717 | } |
| 718 | |
| 719 | //------------------------------------------------------------------ |
| 720 | // Returns a shared pointer to a data buffer that contains all or |
| 721 | // part of the contents of a file. The data copies into a heap based |
| 722 | // buffer that lives in the DataBuffer shared pointer object returned. |
| 723 | // The data that is cached will start "file_offset" bytes into the |
| 724 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 725 | // greater than the number of bytes available in the file starting |
| 726 | // at "file_offset", the number of bytes will be appropriately |
| 727 | // truncated. The final number of bytes that get mapped can be |
| 728 | // verified using the DataBuffer::GetByteSize() function. |
| 729 | //------------------------------------------------------------------ |
| 730 | DataBufferSP |
| 731 | FileSpec::ReadFileContents (off_t file_offset, size_t file_size) const |
| 732 | { |
| 733 | DataBufferSP data_sp; |
| 734 | char resolved_path[PATH_MAX]; |
| 735 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 736 | { |
| 737 | int fd = ::open (resolved_path, O_RDONLY, 0); |
| 738 | if (fd != -1) |
| 739 | { |
| 740 | struct stat file_stats; |
| 741 | if (::fstat (fd, &file_stats) == 0) |
| 742 | { |
| 743 | if (file_stats.st_size > 0) |
| 744 | { |
| 745 | off_t lseek_result = 0; |
| 746 | if (file_offset > 0) |
| 747 | lseek_result = ::lseek (fd, file_offset, SEEK_SET); |
| 748 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | if (lseek_result < 0) |
| 750 | { |
| 751 | // Get error from errno |
| 752 | } |
| 753 | else if (lseek_result == file_offset) |
| 754 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 755 | const size_t bytes_left = file_stats.st_size - file_offset; |
| 756 | size_t num_bytes_to_read = file_size; |
| 757 | if (num_bytes_to_read > bytes_left) |
| 758 | num_bytes_to_read = bytes_left; |
| 759 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | std::auto_ptr<DataBufferHeap> data_heap_ap; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 761 | data_heap_ap.reset(new DataBufferHeap(num_bytes_to_read, '\0')); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 762 | |
| 763 | if (data_heap_ap.get()) |
| 764 | { |
| 765 | ssize_t bytesRead = ::read (fd, (void *)data_heap_ap->GetBytes(), data_heap_ap->GetByteSize()); |
| 766 | if (bytesRead >= 0) |
| 767 | { |
| 768 | // Make sure we read exactly what we asked for and if we got |
| 769 | // less, adjust the array |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 770 | if ((size_t)bytesRead < data_heap_ap->GetByteSize()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 771 | data_heap_ap->SetByteSize(bytesRead); |
| 772 | data_sp.reset(data_heap_ap.release()); |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | close(fd); |
| 780 | } |
| 781 | return data_sp; |
| 782 | } |
| 783 | |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 784 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 785 | FileSpec::ReadFileLines (STLStringArray &lines) |
| 786 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 787 | lines.clear(); |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 788 | char path[PATH_MAX]; |
| 789 | if (GetPath(path, sizeof(path))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 790 | { |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 791 | ifstream file_stream (path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 792 | |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 793 | if (file_stream) |
| 794 | { |
| 795 | std::string line; |
| 796 | while (getline (file_stream, line)) |
| 797 | lines.push_back (line); |
| 798 | } |
| 799 | } |
| 800 | return lines.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 801 | } |