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(), |
| 179 | m_filename() |
| 180 | { |
| 181 | if (pathname && pathname[0]) |
| 182 | SetFile(pathname, resolve_path); |
| 183 | } |
| 184 | |
| 185 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | // Copy constructor |
| 187 | //------------------------------------------------------------------ |
| 188 | FileSpec::FileSpec(const FileSpec& rhs) : |
| 189 | m_directory (rhs.m_directory), |
| 190 | m_filename (rhs.m_filename) |
| 191 | { |
| 192 | } |
| 193 | |
| 194 | //------------------------------------------------------------------ |
| 195 | // Copy constructor |
| 196 | //------------------------------------------------------------------ |
| 197 | FileSpec::FileSpec(const FileSpec* rhs) : |
| 198 | m_directory(), |
| 199 | m_filename() |
| 200 | { |
| 201 | if (rhs) |
| 202 | *this = *rhs; |
| 203 | } |
| 204 | |
| 205 | //------------------------------------------------------------------ |
| 206 | // Virtual destrcuctor in case anyone inherits from this class. |
| 207 | //------------------------------------------------------------------ |
| 208 | FileSpec::~FileSpec() |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | //------------------------------------------------------------------ |
| 213 | // Assignment operator. |
| 214 | //------------------------------------------------------------------ |
| 215 | const FileSpec& |
| 216 | FileSpec::operator= (const FileSpec& rhs) |
| 217 | { |
| 218 | if (this != &rhs) |
| 219 | { |
| 220 | m_directory = rhs.m_directory; |
| 221 | m_filename = rhs.m_filename; |
| 222 | } |
| 223 | return *this; |
| 224 | } |
| 225 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | //------------------------------------------------------------------ |
| 227 | // Update the contents of this object with a new path. The path will |
| 228 | // be split up into a directory and filename and stored as uniqued |
| 229 | // string values for quick comparison and efficient memory usage. |
| 230 | //------------------------------------------------------------------ |
| 231 | void |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 232 | FileSpec::SetFile(const char *pathname, bool resolve) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | { |
| 234 | m_filename.Clear(); |
| 235 | m_directory.Clear(); |
| 236 | if (pathname == NULL || pathname[0] == '\0') |
| 237 | return; |
| 238 | |
| 239 | char resolved_path[PATH_MAX]; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 240 | bool path_fit = true; |
| 241 | |
| 242 | if (resolve) |
| 243 | { |
| 244 | path_fit = (FileSpec::Resolve (pathname, resolved_path, sizeof(resolved_path)) < sizeof(resolved_path) - 1); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | if (strlen (pathname) > sizeof(resolved_path) - 1) |
| 249 | path_fit = false; |
| 250 | else |
| 251 | strcpy (resolved_path, pathname); |
| 252 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 254 | |
| 255 | if (path_fit) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 256 | { |
| 257 | char *filename = ::basename (resolved_path); |
| 258 | if (filename) |
| 259 | { |
| 260 | m_filename.SetCString (filename); |
| 261 | // Truncate the basename off the end of the resolved path |
| 262 | |
| 263 | // Only attempt to get the dirname if it looks like we have a path |
| 264 | if (strchr(resolved_path, '/')) |
| 265 | { |
| 266 | char *directory = ::dirname (resolved_path); |
| 267 | |
| 268 | // Make sure we didn't get our directory resolved to "." without having |
| 269 | // specified |
| 270 | if (directory) |
| 271 | m_directory.SetCString(directory); |
| 272 | else |
| 273 | { |
| 274 | char *last_resolved_path_slash = strrchr(resolved_path, '/'); |
| 275 | if (last_resolved_path_slash) |
| 276 | { |
| 277 | *last_resolved_path_slash = '\0'; |
| 278 | m_directory.SetCString(resolved_path); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | else |
| 284 | m_directory.SetCString(resolved_path); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | //---------------------------------------------------------------------- |
| 289 | // Convert to pointer operator. This allows code to check any FileSpec |
| 290 | // objects to see if they contain anything valid using code such as: |
| 291 | // |
| 292 | // if (file_spec) |
| 293 | // {} |
| 294 | //---------------------------------------------------------------------- |
| 295 | FileSpec::operator |
| 296 | void*() const |
| 297 | { |
| 298 | return (m_directory || m_filename) ? const_cast<FileSpec*>(this) : NULL; |
| 299 | } |
| 300 | |
| 301 | //---------------------------------------------------------------------- |
| 302 | // Logical NOT operator. This allows code to check any FileSpec |
| 303 | // objects to see if they are invalid using code such as: |
| 304 | // |
| 305 | // if (!file_spec) |
| 306 | // {} |
| 307 | //---------------------------------------------------------------------- |
| 308 | bool |
| 309 | FileSpec::operator!() const |
| 310 | { |
| 311 | return !m_directory && !m_filename; |
| 312 | } |
| 313 | |
| 314 | //------------------------------------------------------------------ |
| 315 | // Equal to operator |
| 316 | //------------------------------------------------------------------ |
| 317 | bool |
| 318 | FileSpec::operator== (const FileSpec& rhs) const |
| 319 | { |
| 320 | return m_directory == rhs.m_directory && m_filename == rhs.m_filename; |
| 321 | } |
| 322 | |
| 323 | //------------------------------------------------------------------ |
| 324 | // Not equal to operator |
| 325 | //------------------------------------------------------------------ |
| 326 | bool |
| 327 | FileSpec::operator!= (const FileSpec& rhs) const |
| 328 | { |
| 329 | return m_filename != rhs.m_filename || m_directory != rhs.m_directory; |
| 330 | } |
| 331 | |
| 332 | //------------------------------------------------------------------ |
| 333 | // Less than operator |
| 334 | //------------------------------------------------------------------ |
| 335 | bool |
| 336 | FileSpec::operator< (const FileSpec& rhs) const |
| 337 | { |
| 338 | return FileSpec::Compare(*this, rhs, true) < 0; |
| 339 | } |
| 340 | |
| 341 | //------------------------------------------------------------------ |
| 342 | // Dump a FileSpec object to a stream |
| 343 | //------------------------------------------------------------------ |
| 344 | Stream& |
| 345 | lldb_private::operator << (Stream &s, const FileSpec& f) |
| 346 | { |
| 347 | f.Dump(&s); |
| 348 | return s; |
| 349 | } |
| 350 | |
| 351 | //------------------------------------------------------------------ |
| 352 | // Clear this object by releasing both the directory and filename |
| 353 | // string values and making them both the empty string. |
| 354 | //------------------------------------------------------------------ |
| 355 | void |
| 356 | FileSpec::Clear() |
| 357 | { |
| 358 | m_directory.Clear(); |
| 359 | m_filename.Clear(); |
| 360 | } |
| 361 | |
| 362 | //------------------------------------------------------------------ |
| 363 | // Compare two FileSpec objects. If "full" is true, then both |
| 364 | // the directory and the filename must match. If "full" is false, |
| 365 | // then the directory names for "a" and "b" are only compared if |
| 366 | // they are both non-empty. This allows a FileSpec object to only |
| 367 | // contain a filename and it can match FileSpec objects that have |
| 368 | // matching filenames with different paths. |
| 369 | // |
| 370 | // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" |
| 371 | // and "1" if "a" is greater than "b". |
| 372 | //------------------------------------------------------------------ |
| 373 | int |
| 374 | FileSpec::Compare(const FileSpec& a, const FileSpec& b, bool full) |
| 375 | { |
| 376 | int result = 0; |
| 377 | |
| 378 | // If full is true, then we must compare both the directory and filename. |
| 379 | |
| 380 | // If full is false, then if either directory is empty, then we match on |
| 381 | // the basename only, and if both directories have valid values, we still |
| 382 | // do a full compare. This allows for matching when we just have a filename |
| 383 | // in one of the FileSpec objects. |
| 384 | |
| 385 | if (full || (a.m_directory && b.m_directory)) |
| 386 | { |
| 387 | result = ConstString::Compare(a.m_directory, b.m_directory); |
| 388 | if (result) |
| 389 | return result; |
| 390 | } |
| 391 | return ConstString::Compare (a.m_filename, b.m_filename); |
| 392 | } |
| 393 | |
| 394 | bool |
| 395 | FileSpec::Equal (const FileSpec& a, const FileSpec& b, bool full) |
| 396 | { |
| 397 | if (full) |
| 398 | return a == b; |
| 399 | else |
| 400 | return a.m_filename == b.m_filename; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | //------------------------------------------------------------------ |
| 406 | // Dump the object to the supplied stream. If the object contains |
| 407 | // a valid directory name, it will be displayed followed by a |
| 408 | // directory delimiter, and the filename. |
| 409 | //------------------------------------------------------------------ |
| 410 | void |
| 411 | FileSpec::Dump(Stream *s) const |
| 412 | { |
| 413 | if (m_filename) |
| 414 | m_directory.Dump(s, ""); // Provide a default for m_directory when we dump it in case it is invalid |
| 415 | |
| 416 | if (m_directory) |
| 417 | { |
| 418 | // If dirname was valid, then we need to print a slash between |
| 419 | // the directory and the filename |
| 420 | s->PutChar('/'); |
| 421 | } |
| 422 | m_filename.Dump(s); |
| 423 | } |
| 424 | |
| 425 | //------------------------------------------------------------------ |
| 426 | // Returns true if the file exists. |
| 427 | //------------------------------------------------------------------ |
| 428 | bool |
| 429 | FileSpec::Exists () const |
| 430 | { |
| 431 | struct stat file_stats; |
| 432 | return GetFileStats (this, &file_stats); |
| 433 | } |
| 434 | |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 435 | bool |
| 436 | FileSpec::ResolveExecutableLocation () |
| 437 | { |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 438 | if (!m_directory) |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 439 | { |
| 440 | const std::string file_str (m_filename.AsCString()); |
| 441 | llvm::sys::Path path = llvm::sys::Program::FindProgramByName (file_str); |
| 442 | llvm::StringRef dir_ref = path.getDirname(); |
| 443 | if (! dir_ref.empty()) |
| 444 | { |
| 445 | // FindProgramByName returns "." if it can't find the file. |
| 446 | if (strcmp (".", dir_ref.data()) == 0) |
| 447 | return false; |
| 448 | |
| 449 | m_directory.SetCString (dir_ref.data()); |
| 450 | if (Exists()) |
| 451 | return true; |
| 452 | else |
| 453 | { |
| 454 | // If FindProgramByName found the file, it returns the directory + filename in its return results. |
| 455 | // We need to separate them. |
Greg Clayton | 274060b | 2010-10-20 20:54:39 +0000 | [diff] [blame] | 456 | FileSpec tmp_file (dir_ref.data(), false); |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 457 | if (tmp_file.Exists()) |
| 458 | { |
| 459 | m_directory = tmp_file.m_directory; |
| 460 | return true; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return false; |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 469 | bool |
| 470 | FileSpec::ResolvePath () |
| 471 | { |
| 472 | char path_buf[PATH_MAX]; |
| 473 | |
| 474 | if (!GetPath (path_buf, PATH_MAX)) |
| 475 | return false; |
| 476 | SetFile (path_buf, true); |
| 477 | return true; |
| 478 | } |
| 479 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 480 | uint64_t |
| 481 | FileSpec::GetByteSize() const |
| 482 | { |
| 483 | struct stat file_stats; |
| 484 | if (GetFileStats (this, &file_stats)) |
| 485 | return file_stats.st_size; |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | FileSpec::FileType |
| 490 | FileSpec::GetFileType () const |
| 491 | { |
| 492 | struct stat file_stats; |
| 493 | if (GetFileStats (this, &file_stats)) |
| 494 | { |
| 495 | mode_t file_type = file_stats.st_mode & S_IFMT; |
| 496 | switch (file_type) |
| 497 | { |
| 498 | case S_IFDIR: return eFileTypeDirectory; |
| 499 | case S_IFIFO: return eFileTypePipe; |
| 500 | case S_IFREG: return eFileTypeRegular; |
| 501 | case S_IFSOCK: return eFileTypeSocket; |
| 502 | case S_IFLNK: return eFileTypeSymbolicLink; |
| 503 | default: |
| 504 | break; |
| 505 | } |
| 506 | return eFileTypeUknown; |
| 507 | } |
| 508 | return eFileTypeInvalid; |
| 509 | } |
| 510 | |
| 511 | TimeValue |
| 512 | FileSpec::GetModificationTime () const |
| 513 | { |
| 514 | TimeValue mod_time; |
| 515 | struct stat file_stats; |
| 516 | if (GetFileStats (this, &file_stats)) |
Eli Friedman | 6abb634 | 2010-06-11 04:52:22 +0000 | [diff] [blame] | 517 | mod_time.OffsetWithSeconds(file_stats.st_mtime); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 518 | return mod_time; |
| 519 | } |
| 520 | |
| 521 | //------------------------------------------------------------------ |
| 522 | // Directory string get accessor. |
| 523 | //------------------------------------------------------------------ |
| 524 | ConstString & |
| 525 | FileSpec::GetDirectory() |
| 526 | { |
| 527 | return m_directory; |
| 528 | } |
| 529 | |
| 530 | //------------------------------------------------------------------ |
| 531 | // Directory string const get accessor. |
| 532 | //------------------------------------------------------------------ |
| 533 | const ConstString & |
| 534 | FileSpec::GetDirectory() const |
| 535 | { |
| 536 | return m_directory; |
| 537 | } |
| 538 | |
| 539 | //------------------------------------------------------------------ |
| 540 | // Filename string get accessor. |
| 541 | //------------------------------------------------------------------ |
| 542 | ConstString & |
| 543 | FileSpec::GetFilename() |
| 544 | { |
| 545 | return m_filename; |
| 546 | } |
| 547 | |
| 548 | //------------------------------------------------------------------ |
| 549 | // Filename string const get accessor. |
| 550 | //------------------------------------------------------------------ |
| 551 | const ConstString & |
| 552 | FileSpec::GetFilename() const |
| 553 | { |
| 554 | return m_filename; |
| 555 | } |
| 556 | |
| 557 | //------------------------------------------------------------------ |
| 558 | // Extract the directory and path into a fixed buffer. This is |
| 559 | // needed as the directory and path are stored in separate string |
| 560 | // values. |
| 561 | //------------------------------------------------------------------ |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame^] | 562 | size_t |
| 563 | FileSpec::GetPath(char *path, size_t path_max_len) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 564 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame^] | 565 | if (path_max_len) |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 566 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame^] | 567 | const char *dirname = m_directory.GetCString(); |
| 568 | const char *filename = m_filename.GetCString(); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 569 | if (dirname) |
| 570 | { |
| 571 | if (filename) |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame^] | 572 | return ::snprintf (path, path_max_len, "%s/%s", dirname, filename); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 573 | else |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame^] | 574 | return ::snprintf (path, path_max_len, "%s", dirname); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 575 | } |
| 576 | else if (filename) |
| 577 | { |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame^] | 578 | return ::snprintf (path, path_max_len, "%s", filename); |
Greg Clayton | dd36def | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 581 | path[0] = '\0'; |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame^] | 582 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | //------------------------------------------------------------------ |
| 586 | // Returns a shared pointer to a data buffer that contains all or |
| 587 | // part of the contents of a file. The data is memory mapped and |
| 588 | // will lazily page in data from the file as memory is accessed. |
| 589 | // The data that is mappped will start "file_offset" bytes into the |
| 590 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 591 | // greater than the number of bytes available in the file starting |
| 592 | // at "file_offset", the number of bytes will be appropriately |
| 593 | // truncated. The final number of bytes that get mapped can be |
| 594 | // verified using the DataBuffer::GetByteSize() function. |
| 595 | //------------------------------------------------------------------ |
| 596 | DataBufferSP |
| 597 | FileSpec::MemoryMapFileContents(off_t file_offset, size_t file_size) const |
| 598 | { |
| 599 | DataBufferSP data_sp; |
| 600 | auto_ptr<DataBufferMemoryMap> mmap_data(new DataBufferMemoryMap()); |
| 601 | if (mmap_data.get()) |
| 602 | { |
| 603 | if (mmap_data->MemoryMapFromFileSpec (this, file_offset, file_size) >= file_size) |
| 604 | data_sp.reset(mmap_data.release()); |
| 605 | } |
| 606 | return data_sp; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | //------------------------------------------------------------------ |
| 611 | // Return the size in bytes that this object takes in memory. This |
| 612 | // returns the size in bytes of this object, not any shared string |
| 613 | // values it may refer to. |
| 614 | //------------------------------------------------------------------ |
| 615 | size_t |
| 616 | FileSpec::MemorySize() const |
| 617 | { |
| 618 | return m_filename.MemorySize() + m_directory.MemorySize(); |
| 619 | } |
| 620 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 621 | |
| 622 | size_t |
| 623 | 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] | 624 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 625 | size_t bytes_read = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 626 | char resolved_path[PATH_MAX]; |
| 627 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 628 | { |
| 629 | int fd = ::open (resolved_path, O_RDONLY, 0); |
| 630 | if (fd != -1) |
| 631 | { |
| 632 | struct stat file_stats; |
| 633 | if (::fstat (fd, &file_stats) == 0) |
| 634 | { |
| 635 | // Read bytes directly into our basic_string buffer |
| 636 | if (file_stats.st_size > 0) |
| 637 | { |
| 638 | off_t lseek_result = 0; |
| 639 | if (file_offset > 0) |
| 640 | lseek_result = ::lseek (fd, file_offset, SEEK_SET); |
| 641 | |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 642 | if (lseek_result == file_offset) |
| 643 | { |
| 644 | ssize_t n = ::read (fd, dst, dst_len); |
| 645 | if (n >= 0) |
| 646 | bytes_read = n; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | close(fd); |
| 652 | } |
| 653 | return bytes_read; |
| 654 | } |
| 655 | |
| 656 | //------------------------------------------------------------------ |
| 657 | // Returns a shared pointer to a data buffer that contains all or |
| 658 | // part of the contents of a file. The data copies into a heap based |
| 659 | // buffer that lives in the DataBuffer shared pointer object returned. |
| 660 | // The data that is cached will start "file_offset" bytes into the |
| 661 | // file, and "file_size" bytes will be mapped. If "file_size" is |
| 662 | // greater than the number of bytes available in the file starting |
| 663 | // at "file_offset", the number of bytes will be appropriately |
| 664 | // truncated. The final number of bytes that get mapped can be |
| 665 | // verified using the DataBuffer::GetByteSize() function. |
| 666 | //------------------------------------------------------------------ |
| 667 | DataBufferSP |
| 668 | FileSpec::ReadFileContents (off_t file_offset, size_t file_size) const |
| 669 | { |
| 670 | DataBufferSP data_sp; |
| 671 | char resolved_path[PATH_MAX]; |
| 672 | if (GetPath(resolved_path, sizeof(resolved_path))) |
| 673 | { |
| 674 | int fd = ::open (resolved_path, O_RDONLY, 0); |
| 675 | if (fd != -1) |
| 676 | { |
| 677 | struct stat file_stats; |
| 678 | if (::fstat (fd, &file_stats) == 0) |
| 679 | { |
| 680 | if (file_stats.st_size > 0) |
| 681 | { |
| 682 | off_t lseek_result = 0; |
| 683 | if (file_offset > 0) |
| 684 | lseek_result = ::lseek (fd, file_offset, SEEK_SET); |
| 685 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 686 | if (lseek_result < 0) |
| 687 | { |
| 688 | // Get error from errno |
| 689 | } |
| 690 | else if (lseek_result == file_offset) |
| 691 | { |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 692 | const size_t bytes_left = file_stats.st_size - file_offset; |
| 693 | size_t num_bytes_to_read = file_size; |
| 694 | if (num_bytes_to_read > bytes_left) |
| 695 | num_bytes_to_read = bytes_left; |
| 696 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 697 | std::auto_ptr<DataBufferHeap> data_heap_ap; |
Greg Clayton | dda4f7b | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 698 | data_heap_ap.reset(new DataBufferHeap(num_bytes_to_read, '\0')); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 699 | |
| 700 | if (data_heap_ap.get()) |
| 701 | { |
| 702 | ssize_t bytesRead = ::read (fd, (void *)data_heap_ap->GetBytes(), data_heap_ap->GetByteSize()); |
| 703 | if (bytesRead >= 0) |
| 704 | { |
| 705 | // Make sure we read exactly what we asked for and if we got |
| 706 | // less, adjust the array |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 707 | if ((size_t)bytesRead < data_heap_ap->GetByteSize()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 708 | data_heap_ap->SetByteSize(bytesRead); |
| 709 | data_sp.reset(data_heap_ap.release()); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | close(fd); |
| 717 | } |
| 718 | return data_sp; |
| 719 | } |
| 720 | |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 721 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 722 | FileSpec::ReadFileLines (STLStringArray &lines) |
| 723 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 724 | lines.clear(); |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 725 | char path[PATH_MAX]; |
| 726 | if (GetPath(path, sizeof(path))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 727 | { |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 728 | ifstream file_stream (path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 729 | |
Greg Clayton | 58fc50e | 2010-10-20 22:52:05 +0000 | [diff] [blame] | 730 | if (file_stream) |
| 731 | { |
| 732 | std::string line; |
| 733 | while (getline (file_stream, line)) |
| 734 | lines.push_back (line); |
| 735 | } |
| 736 | } |
| 737 | return lines.size(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | } |