FileSpec.cpp revision ff44ab42e9f5d8e4d550e11d1b69413e0bc75b71
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 <dirent.h> 12#include <fcntl.h> 13#include <libgen.h> 14#include <sys/stat.h> 15#include <string.h> 16#include <fstream> 17 18#include "lldb/Host/Config.h" // Have to include this before we test the define... 19#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 20#include <pwd.h> 21#endif 22 23#include "llvm/ADT/StringRef.h" 24#include "llvm/Support/Path.h" 25#include "llvm/Support/Program.h" 26 27#include "lldb/Host/FileSpec.h" 28#include "lldb/Core/DataBufferHeap.h" 29#include "lldb/Core/DataBufferMemoryMap.h" 30#include "lldb/Core/Stream.h" 31#include "lldb/Host/Host.h" 32#include "lldb/Utility/CleanUp.h" 33 34using namespace lldb; 35using namespace lldb_private; 36using namespace std; 37 38static bool 39GetFileStats (const FileSpec *file_spec, struct stat *stats_ptr) 40{ 41 char resolved_path[PATH_MAX]; 42 if (file_spec->GetPath (resolved_path, sizeof(resolved_path))) 43 return ::stat (resolved_path, stats_ptr) == 0; 44 return false; 45} 46 47#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 48 49static const char* 50GetCachedGlobTildeSlash() 51{ 52 static std::string g_tilde; 53 if (g_tilde.empty()) 54 { 55 struct passwd *user_entry; 56 user_entry = getpwuid(geteuid()); 57 if (user_entry != NULL) 58 g_tilde = user_entry->pw_dir; 59 60 if (g_tilde.empty()) 61 return NULL; 62 } 63 return g_tilde.c_str(); 64} 65 66#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 67 68// Resolves the username part of a path of the form ~user/other/directories, and 69// writes the result into dst_path. 70// Returns 0 if there WAS a ~ in the path but the username couldn't be resolved. 71// Otherwise returns the number of characters copied into dst_path. If the return 72// is >= dst_len, then the resolved path is too long... 73size_t 74FileSpec::ResolveUsername (const char *src_path, char *dst_path, size_t dst_len) 75{ 76 if (src_path == NULL || src_path[0] == '\0') 77 return 0; 78 79#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 80 81 char user_home[PATH_MAX]; 82 const char *user_name; 83 84 85 // If there's no ~, then just copy src_path straight to dst_path (they may be the same string...) 86 if (src_path[0] != '~') 87 { 88 size_t len = strlen (src_path); 89 if (len >= dst_len) 90 { 91 ::bcopy (src_path, dst_path, dst_len - 1); 92 dst_path[dst_len] = '\0'; 93 } 94 else 95 ::bcopy (src_path, dst_path, len + 1); 96 97 return len; 98 } 99 100 const char *first_slash = ::strchr (src_path, '/'); 101 char remainder[PATH_MAX]; 102 103 if (first_slash == NULL) 104 { 105 // The whole name is the username (minus the ~): 106 user_name = src_path + 1; 107 remainder[0] = '\0'; 108 } 109 else 110 { 111 int user_name_len = first_slash - src_path - 1; 112 ::memcpy (user_home, src_path + 1, user_name_len); 113 user_home[user_name_len] = '\0'; 114 user_name = user_home; 115 116 ::strcpy (remainder, first_slash); 117 } 118 119 if (user_name == NULL) 120 return 0; 121 // User name of "" means the current user... 122 123 struct passwd *user_entry; 124 const char *home_dir = NULL; 125 126 if (user_name[0] == '\0') 127 { 128 home_dir = GetCachedGlobTildeSlash(); 129 } 130 else 131 { 132 user_entry = ::getpwnam (user_name); 133 if (user_entry != NULL) 134 home_dir = user_entry->pw_dir; 135 } 136 137 if (home_dir == NULL) 138 return 0; 139 else 140 return ::snprintf (dst_path, dst_len, "%s%s", home_dir, remainder); 141#else 142 // Resolving home directories is not supported, just copy the path... 143 return ::snprintf (dst_path, dst_len, "%s", src_path); 144#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 145} 146 147size_t 148FileSpec::ResolvePartialUsername (const char *partial_name, StringList &matches) 149{ 150#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 151 size_t extant_entries = matches.GetSize(); 152 153 setpwent(); 154 struct passwd *user_entry; 155 const char *name_start = partial_name + 1; 156 std::set<std::string> name_list; 157 158 while ((user_entry = getpwent()) != NULL) 159 { 160 if (strstr(user_entry->pw_name, name_start) == user_entry->pw_name) 161 { 162 std::string tmp_buf("~"); 163 tmp_buf.append(user_entry->pw_name); 164 tmp_buf.push_back('/'); 165 name_list.insert(tmp_buf); 166 } 167 } 168 std::set<std::string>::iterator pos, end = name_list.end(); 169 for (pos = name_list.begin(); pos != end; pos++) 170 { 171 matches.AppendString((*pos).c_str()); 172 } 173 return matches.GetSize() - extant_entries; 174#else 175 // Resolving home directories is not supported, just copy the path... 176 return 0; 177#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 178} 179 180 181 182size_t 183FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len) 184{ 185 if (src_path == NULL || src_path[0] == '\0') 186 return 0; 187 188 // Glob if needed for ~/, otherwise copy in case src_path is same as dst_path... 189 char unglobbed_path[PATH_MAX]; 190#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 191 if (src_path[0] == '~') 192 { 193 size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path)); 194 195 // If we couldn't find the user referred to, or the resultant path was too long, 196 // then just copy over the src_path. 197 if (return_count == 0 || return_count >= sizeof(unglobbed_path)) 198 ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", src_path); 199 } 200 else 201#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER 202 { 203 ::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path); 204 } 205 206 // Now resolve the path if needed 207 char resolved_path[PATH_MAX]; 208 if (::realpath (unglobbed_path, resolved_path)) 209 { 210 // Success, copy the resolved path 211 return ::snprintf(dst_path, dst_len, "%s", resolved_path); 212 } 213 else 214 { 215 // Failed, just copy the unglobbed path 216 return ::snprintf(dst_path, dst_len, "%s", unglobbed_path); 217 } 218} 219 220FileSpec::FileSpec() : 221 m_directory(), 222 m_filename() 223{ 224} 225 226//------------------------------------------------------------------ 227// Default constructor that can take an optional full path to a 228// file on disk. 229//------------------------------------------------------------------ 230FileSpec::FileSpec(const char *pathname, bool resolve_path) : 231 m_directory(), 232 m_filename(), 233 m_is_resolved(false) 234{ 235 if (pathname && pathname[0]) 236 SetFile(pathname, resolve_path); 237} 238 239//------------------------------------------------------------------ 240// Copy constructor 241//------------------------------------------------------------------ 242FileSpec::FileSpec(const FileSpec& rhs) : 243 m_directory (rhs.m_directory), 244 m_filename (rhs.m_filename), 245 m_is_resolved (rhs.m_is_resolved) 246{ 247} 248 249//------------------------------------------------------------------ 250// Copy constructor 251//------------------------------------------------------------------ 252FileSpec::FileSpec(const FileSpec* rhs) : 253 m_directory(), 254 m_filename() 255{ 256 if (rhs) 257 *this = *rhs; 258} 259 260//------------------------------------------------------------------ 261// Virtual destrcuctor in case anyone inherits from this class. 262//------------------------------------------------------------------ 263FileSpec::~FileSpec() 264{ 265} 266 267//------------------------------------------------------------------ 268// Assignment operator. 269//------------------------------------------------------------------ 270const FileSpec& 271FileSpec::operator= (const FileSpec& rhs) 272{ 273 if (this != &rhs) 274 { 275 m_directory = rhs.m_directory; 276 m_filename = rhs.m_filename; 277 m_is_resolved = rhs.m_is_resolved; 278 } 279 return *this; 280} 281 282//------------------------------------------------------------------ 283// Update the contents of this object with a new path. The path will 284// be split up into a directory and filename and stored as uniqued 285// string values for quick comparison and efficient memory usage. 286//------------------------------------------------------------------ 287void 288FileSpec::SetFile (const char *pathname, bool resolve) 289{ 290 m_filename.Clear(); 291 m_directory.Clear(); 292 m_is_resolved = false; 293 if (pathname == NULL || pathname[0] == '\0') 294 return; 295 296 char resolved_path[PATH_MAX]; 297 bool path_fit = true; 298 299 if (resolve) 300 { 301 path_fit = (FileSpec::Resolve (pathname, resolved_path, sizeof(resolved_path)) < sizeof(resolved_path) - 1); 302 m_is_resolved = path_fit; 303 } 304 else 305 { 306 // Copy the path because "basename" and "dirname" want to muck with the 307 // path buffer 308 if (::strlen (pathname) > sizeof(resolved_path) - 1) 309 path_fit = false; 310 else 311 ::strcpy (resolved_path, pathname); 312 } 313 314 315 if (path_fit) 316 { 317 char *filename = ::basename (resolved_path); 318 if (filename) 319 { 320 m_filename.SetCString (filename); 321 // Truncate the basename off the end of the resolved path 322 323 // Only attempt to get the dirname if it looks like we have a path 324 if (strchr(resolved_path, '/')) 325 { 326 char *directory = ::dirname (resolved_path); 327 328 // Make sure we didn't get our directory resolved to "." without having 329 // specified 330 if (directory) 331 m_directory.SetCString(directory); 332 else 333 { 334 char *last_resolved_path_slash = strrchr(resolved_path, '/'); 335 if (last_resolved_path_slash) 336 { 337 *last_resolved_path_slash = '\0'; 338 m_directory.SetCString(resolved_path); 339 } 340 } 341 } 342 } 343 else 344 m_directory.SetCString(resolved_path); 345 } 346} 347 348//---------------------------------------------------------------------- 349// Convert to pointer operator. This allows code to check any FileSpec 350// objects to see if they contain anything valid using code such as: 351// 352// if (file_spec) 353// {} 354//---------------------------------------------------------------------- 355FileSpec::operator 356void*() const 357{ 358 return (m_directory || m_filename) ? const_cast<FileSpec*>(this) : NULL; 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//---------------------------------------------------------------------- 368bool 369FileSpec::operator!() const 370{ 371 return !m_directory && !m_filename; 372} 373 374//------------------------------------------------------------------ 375// Equal to operator 376//------------------------------------------------------------------ 377bool 378FileSpec::operator== (const FileSpec& rhs) const 379{ 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; 434} 435 436//------------------------------------------------------------------ 437// Not equal to operator 438//------------------------------------------------------------------ 439bool 440FileSpec::operator!= (const FileSpec& rhs) const 441{ 442 return !(*this == rhs); 443} 444 445//------------------------------------------------------------------ 446// Less than operator 447//------------------------------------------------------------------ 448bool 449FileSpec::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//------------------------------------------------------------------ 457Stream& 458lldb_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//------------------------------------------------------------------ 468void 469FileSpec::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//------------------------------------------------------------------ 486int 487FileSpec::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 507bool 508FileSpec::Equal (const FileSpec& a, const FileSpec& b, bool full) 509{ 510 if (full) 511 return a == b; 512 else 513 return a.m_filename == b.m_filename; 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//------------------------------------------------------------------ 523void 524FileSpec::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//------------------------------------------------------------------ 541bool 542FileSpec::Exists () const 543{ 544 struct stat file_stats; 545 return GetFileStats (this, &file_stats); 546} 547 548bool 549FileSpec::ResolveExecutableLocation () 550{ 551 if (!m_directory) 552 { 553 const char *file_cstr = m_filename.GetCString(); 554 if (file_cstr) 555 { 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()) 562 { 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()) 569 return true; 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 } 580 } 581 } 582 } 583 } 584 585 return false; 586} 587 588bool 589FileSpec::ResolvePath () 590{ 591 if (m_is_resolved) 592 return true; // We have already resolved this path 593 594 char path_buf[PATH_MAX]; 595 if (!GetPath (path_buf, PATH_MAX)) 596 return false; 597 // SetFile(...) will set m_is_resolved correctly if it can resolve the path 598 SetFile (path_buf, true); 599 return m_is_resolved; 600} 601 602uint64_t 603FileSpec::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 611FileSpec::FileType 612FileSpec::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 } 628 return eFileTypeUnknown; 629 } 630 return eFileTypeInvalid; 631} 632 633TimeValue 634FileSpec::GetModificationTime () const 635{ 636 TimeValue mod_time; 637 struct stat file_stats; 638 if (GetFileStats (this, &file_stats)) 639 mod_time.OffsetWithSeconds(file_stats.st_mtime); 640 return mod_time; 641} 642 643//------------------------------------------------------------------ 644// Directory string get accessor. 645//------------------------------------------------------------------ 646ConstString & 647FileSpec::GetDirectory() 648{ 649 return m_directory; 650} 651 652//------------------------------------------------------------------ 653// Directory string const get accessor. 654//------------------------------------------------------------------ 655const ConstString & 656FileSpec::GetDirectory() const 657{ 658 return m_directory; 659} 660 661//------------------------------------------------------------------ 662// Filename string get accessor. 663//------------------------------------------------------------------ 664ConstString & 665FileSpec::GetFilename() 666{ 667 return m_filename; 668} 669 670//------------------------------------------------------------------ 671// Filename string const get accessor. 672//------------------------------------------------------------------ 673const ConstString & 674FileSpec::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//------------------------------------------------------------------ 684size_t 685FileSpec::GetPath(char *path, size_t path_max_len) const 686{ 687 if (path_max_len) 688 { 689 const char *dirname = m_directory.GetCString(); 690 const char *filename = m_filename.GetCString(); 691 if (dirname) 692 { 693 if (filename) 694 return ::snprintf (path, path_max_len, "%s/%s", dirname, filename); 695 else 696 return ::snprintf (path, path_max_len, "%s", dirname); 697 } 698 else if (filename) 699 { 700 return ::snprintf (path, path_max_len, "%s", filename); 701 } 702 } 703 path[0] = '\0'; 704 return 0; 705} 706 707//------------------------------------------------------------------ 708// Returns a shared pointer to a data buffer that contains all or 709// part of the contents of a file. The data is memory mapped and 710// will lazily page in data from the file as memory is accessed. 711// The data that is mappped will start "file_offset" bytes into the 712// file, and "file_size" bytes will be mapped. If "file_size" is 713// greater than the number of bytes available in the file starting 714// at "file_offset", the number of bytes will be appropriately 715// truncated. The final number of bytes that get mapped can be 716// verified using the DataBuffer::GetByteSize() function. 717//------------------------------------------------------------------ 718DataBufferSP 719FileSpec::MemoryMapFileContents(off_t file_offset, size_t file_size) const 720{ 721 DataBufferSP data_sp; 722 auto_ptr<DataBufferMemoryMap> mmap_data(new DataBufferMemoryMap()); 723 if (mmap_data.get()) 724 { 725 if (mmap_data->MemoryMapFromFileSpec (this, file_offset, file_size) >= file_size) 726 data_sp.reset(mmap_data.release()); 727 } 728 return data_sp; 729} 730 731 732//------------------------------------------------------------------ 733// Return the size in bytes that this object takes in memory. This 734// returns the size in bytes of this object, not any shared string 735// values it may refer to. 736//------------------------------------------------------------------ 737size_t 738FileSpec::MemorySize() const 739{ 740 return m_filename.MemorySize() + m_directory.MemorySize(); 741} 742 743 744size_t 745FileSpec::ReadFileContents (off_t file_offset, void *dst, size_t dst_len) const 746{ 747 size_t bytes_read = 0; 748 char resolved_path[PATH_MAX]; 749 if (GetPath(resolved_path, sizeof(resolved_path))) 750 { 751 int fd = ::open (resolved_path, O_RDONLY, 0); 752 if (fd != -1) 753 { 754 struct stat file_stats; 755 if (::fstat (fd, &file_stats) == 0) 756 { 757 // Read bytes directly into our basic_string buffer 758 if (file_stats.st_size > 0) 759 { 760 off_t lseek_result = 0; 761 if (file_offset > 0) 762 lseek_result = ::lseek (fd, file_offset, SEEK_SET); 763 764 if (lseek_result == file_offset) 765 { 766 ssize_t n = ::read (fd, dst, dst_len); 767 if (n >= 0) 768 bytes_read = n; 769 } 770 } 771 } 772 } 773 close(fd); 774 } 775 return bytes_read; 776} 777 778//------------------------------------------------------------------ 779// Returns a shared pointer to a data buffer that contains all or 780// part of the contents of a file. The data copies into a heap based 781// buffer that lives in the DataBuffer shared pointer object returned. 782// The data that is cached will start "file_offset" bytes into the 783// file, and "file_size" bytes will be mapped. If "file_size" is 784// greater than the number of bytes available in the file starting 785// at "file_offset", the number of bytes will be appropriately 786// truncated. The final number of bytes that get mapped can be 787// verified using the DataBuffer::GetByteSize() function. 788//------------------------------------------------------------------ 789DataBufferSP 790FileSpec::ReadFileContents (off_t file_offset, size_t file_size) const 791{ 792 DataBufferSP data_sp; 793 char resolved_path[PATH_MAX]; 794 if (GetPath(resolved_path, sizeof(resolved_path))) 795 { 796 int fd = ::open (resolved_path, O_RDONLY, 0); 797 if (fd != -1) 798 { 799 struct stat file_stats; 800 if (::fstat (fd, &file_stats) == 0) 801 { 802 if (file_stats.st_size > 0) 803 { 804 off_t lseek_result = 0; 805 if (file_offset > 0) 806 lseek_result = ::lseek (fd, file_offset, SEEK_SET); 807 808 if (lseek_result < 0) 809 { 810 // Get error from errno 811 } 812 else if (lseek_result == file_offset) 813 { 814 const size_t bytes_left = file_stats.st_size - file_offset; 815 size_t num_bytes_to_read = file_size; 816 if (num_bytes_to_read > bytes_left) 817 num_bytes_to_read = bytes_left; 818 819 std::auto_ptr<DataBufferHeap> data_heap_ap; 820 data_heap_ap.reset(new DataBufferHeap(num_bytes_to_read, '\0')); 821 822 if (data_heap_ap.get()) 823 { 824 ssize_t bytesRead = ::read (fd, (void *)data_heap_ap->GetBytes(), data_heap_ap->GetByteSize()); 825 if (bytesRead >= 0) 826 { 827 // Make sure we read exactly what we asked for and if we got 828 // less, adjust the array 829 if ((size_t)bytesRead < data_heap_ap->GetByteSize()) 830 data_heap_ap->SetByteSize(bytesRead); 831 data_sp.reset(data_heap_ap.release()); 832 } 833 } 834 } 835 } 836 } 837 } 838 close(fd); 839 } 840 return data_sp; 841} 842 843size_t 844FileSpec::ReadFileLines (STLStringArray &lines) 845{ 846 lines.clear(); 847 char path[PATH_MAX]; 848 if (GetPath(path, sizeof(path))) 849 { 850 ifstream file_stream (path); 851 852 if (file_stream) 853 { 854 std::string line; 855 while (getline (file_stream, line)) 856 lines.push_back (line); 857 } 858 } 859 return lines.size(); 860} 861 862FileSpec::EnumerateDirectoryResult 863FileSpec::EnumerateDirectory 864( 865 const char *dir_path, 866 bool find_directories, 867 bool find_files, 868 bool find_other, 869 EnumerateDirectoryCallbackType callback, 870 void *callback_baton 871) 872{ 873 if (dir_path && dir_path[0]) 874 { 875 lldb_utility::CleanUp <DIR *, int> dir_path_dir (opendir(dir_path), NULL, closedir); 876 if (dir_path_dir.is_valid()) 877 { 878 struct dirent* dp; 879 while ((dp = readdir(dir_path_dir.get())) != NULL) 880 { 881 // Only search directories 882 if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) 883 { 884 size_t len = strlen(dp->d_name); 885 886 if (len == 1 && dp->d_name[0] == '.') 887 continue; 888 889 if (len == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') 890 continue; 891 } 892 893 bool call_callback = false; 894 FileSpec::FileType file_type = eFileTypeUnknown; 895 896 switch (dp->d_type) 897 { 898 default: 899 case DT_UNKNOWN: file_type = eFileTypeUnknown; call_callback = true; break; 900 case DT_FIFO: file_type = eFileTypePipe; call_callback = find_other; break; 901 case DT_CHR: file_type = eFileTypeOther; call_callback = find_other; break; 902 case DT_DIR: file_type = eFileTypeDirectory; call_callback = find_directories; break; 903 case DT_BLK: file_type = eFileTypeOther; call_callback = find_other; break; 904 case DT_REG: file_type = eFileTypeRegular; call_callback = find_files; break; 905 case DT_LNK: file_type = eFileTypeSymbolicLink; call_callback = find_other; break; 906 case DT_SOCK: file_type = eFileTypeSocket; call_callback = find_other; break; 907#if !defined(__OpenBSD__) 908 case DT_WHT: file_type = eFileTypeOther; call_callback = find_other; break; 909#endif 910 } 911 912 if (call_callback) 913 { 914 char child_path[PATH_MAX]; 915 const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s/%s", dir_path, dp->d_name); 916 if (child_path_len < sizeof(child_path) - 1) 917 { 918 // Don't resolve the file type or path 919 FileSpec child_path_spec (child_path, false); 920 921 EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec); 922 923 switch (result) 924 { 925 default: 926 case eEnumerateDirectoryResultNext: 927 // Enumerate next entry in the current directory. We just 928 // exit this switch and will continue enumerating the 929 // current directory as we currently are... 930 break; 931 932 case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not 933 if (FileSpec::EnumerateDirectory (child_path, 934 find_directories, 935 find_files, 936 find_other, 937 callback, 938 callback_baton) == eEnumerateDirectoryResultQuit) 939 { 940 // The subdirectory returned Quit, which means to 941 // stop all directory enumerations at all levels. 942 return eEnumerateDirectoryResultQuit; 943 } 944 break; 945 946 case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level. 947 // Exit from this directory level and tell parent to 948 // keep enumerating. 949 return eEnumerateDirectoryResultNext; 950 951 case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level 952 return eEnumerateDirectoryResultQuit; 953 } 954 } 955 } 956 } 957 } 958 } 959 // By default when exiting a directory, we tell the parent enumeration 960 // to continue enumerating. 961 return eEnumerateDirectoryResultNext; 962} 963 964