1 //===-- FileSpec.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "lldb/Host/windows/windows.h"
25 #include "lldb/Host/Config.h" // Have to include this before we test the define...
26 #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/Program.h"
34 #include "lldb/Core/StreamString.h"
35 #include "lldb/Host/File.h"
36 #include "lldb/Host/FileSpec.h"
37 #include "lldb/Core/DataBufferHeap.h"
38 #include "lldb/Core/DataBufferMemoryMap.h"
39 #include "lldb/Core/RegularExpression.h"
40 #include "lldb/Core/Stream.h"
41 #include "lldb/Host/Host.h"
42 #include "lldb/Utility/CleanUp.h"
45 using namespace lldb_private;
48 GetFileStats (const FileSpec *file_spec, struct stat *stats_ptr)
50 char resolved_path[PATH_MAX];
51 if (file_spec->GetPath (resolved_path, sizeof(resolved_path)))
52 return ::stat (resolved_path, stats_ptr) == 0;
56 #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
59 GetCachedGlobTildeSlash()
61 static std::string g_tilde;
64 struct passwd *user_entry;
65 user_entry = getpwuid(geteuid());
66 if (user_entry != NULL)
67 g_tilde = user_entry->pw_dir;
72 return g_tilde.c_str();
75 #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
77 // Resolves the username part of a path of the form ~user/other/directories, and
78 // writes the result into dst_path.
79 // Returns 0 if there WAS a ~ in the path but the username couldn't be resolved.
80 // Otherwise returns the number of characters copied into dst_path. If the return
81 // is >= dst_len, then the resolved path is too long...
83 FileSpec::ResolveUsername (const char *src_path, char *dst_path, size_t dst_len)
85 if (src_path == NULL || src_path[0] == '\0')
88 #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
90 char user_home[PATH_MAX];
91 const char *user_name;
94 // If there's no ~, then just copy src_path straight to dst_path (they may be the same string...)
95 if (src_path[0] != '~')
97 size_t len = strlen (src_path);
100 ::bcopy (src_path, dst_path, dst_len - 1);
101 dst_path[dst_len] = '\0';
104 ::bcopy (src_path, dst_path, len + 1);
109 const char *first_slash = ::strchr (src_path, '/');
110 char remainder[PATH_MAX];
112 if (first_slash == NULL)
114 // The whole name is the username (minus the ~):
115 user_name = src_path + 1;
120 size_t user_name_len = first_slash - src_path - 1;
121 ::memcpy (user_home, src_path + 1, user_name_len);
122 user_home[user_name_len] = '\0';
123 user_name = user_home;
125 ::strcpy (remainder, first_slash);
128 if (user_name == NULL)
130 // User name of "" means the current user...
132 struct passwd *user_entry;
133 const char *home_dir = NULL;
135 if (user_name[0] == '\0')
137 home_dir = GetCachedGlobTildeSlash();
141 user_entry = ::getpwnam (user_name);
142 if (user_entry != NULL)
143 home_dir = user_entry->pw_dir;
146 if (home_dir == NULL)
149 return ::snprintf (dst_path, dst_len, "%s%s", home_dir, remainder);
151 // Resolving home directories is not supported, just copy the path...
152 return ::snprintf (dst_path, dst_len, "%s", src_path);
153 #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
157 FileSpec::ResolvePartialUsername (const char *partial_name, StringList &matches)
159 #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
160 size_t extant_entries = matches.GetSize();
163 struct passwd *user_entry;
164 const char *name_start = partial_name + 1;
165 std::set<std::string> name_list;
167 while ((user_entry = getpwent()) != NULL)
169 if (strstr(user_entry->pw_name, name_start) == user_entry->pw_name)
171 std::string tmp_buf("~");
172 tmp_buf.append(user_entry->pw_name);
173 tmp_buf.push_back('/');
174 name_list.insert(tmp_buf);
177 std::set<std::string>::iterator pos, end = name_list.end();
178 for (pos = name_list.begin(); pos != end; pos++)
180 matches.AppendString((*pos).c_str());
182 return matches.GetSize() - extant_entries;
184 // Resolving home directories is not supported, just copy the path...
186 #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
192 FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len)
194 if (src_path == NULL || src_path[0] == '\0')
197 // Glob if needed for ~/, otherwise copy in case src_path is same as dst_path...
198 char unglobbed_path[PATH_MAX];
199 #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
200 if (src_path[0] == '~')
202 size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path));
204 // If we couldn't find the user referred to, or the resultant path was too long,
205 // then just copy over the src_path.
206 if (return_count == 0 || return_count >= sizeof(unglobbed_path))
207 ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", src_path);
210 #endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
212 ::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path);
215 // Now resolve the path if needed
216 char resolved_path[PATH_MAX];
217 if (::realpath (unglobbed_path, resolved_path))
219 // Success, copy the resolved path
220 return ::snprintf(dst_path, dst_len, "%s", resolved_path);
224 // Failed, just copy the unglobbed path
225 return ::snprintf(dst_path, dst_len, "%s", unglobbed_path);
229 FileSpec::FileSpec() :
235 //------------------------------------------------------------------
236 // Default constructor that can take an optional full path to a
238 //------------------------------------------------------------------
239 FileSpec::FileSpec(const char *pathname, bool resolve_path) :
244 if (pathname && pathname[0])
245 SetFile(pathname, resolve_path);
248 //------------------------------------------------------------------
250 //------------------------------------------------------------------
251 FileSpec::FileSpec(const FileSpec& rhs) :
252 m_directory (rhs.m_directory),
253 m_filename (rhs.m_filename),
254 m_is_resolved (rhs.m_is_resolved)
258 //------------------------------------------------------------------
260 //------------------------------------------------------------------
261 FileSpec::FileSpec(const FileSpec* rhs) :
269 //------------------------------------------------------------------
270 // Virtual destrcuctor in case anyone inherits from this class.
271 //------------------------------------------------------------------
272 FileSpec::~FileSpec()
276 //------------------------------------------------------------------
277 // Assignment operator.
278 //------------------------------------------------------------------
280 FileSpec::operator= (const FileSpec& rhs)
284 m_directory = rhs.m_directory;
285 m_filename = rhs.m_filename;
286 m_is_resolved = rhs.m_is_resolved;
291 //------------------------------------------------------------------
292 // Update the contents of this object with a new path. The path will
293 // be split up into a directory and filename and stored as uniqued
294 // string values for quick comparison and efficient memory usage.
295 //------------------------------------------------------------------
297 FileSpec::SetFile (const char *pathname, bool resolve)
301 m_is_resolved = false;
302 if (pathname == NULL || pathname[0] == '\0')
305 char resolved_path[PATH_MAX];
306 bool path_fit = true;
310 path_fit = (FileSpec::Resolve (pathname, resolved_path, sizeof(resolved_path)) < sizeof(resolved_path) - 1);
311 m_is_resolved = path_fit;
315 // Copy the path because "basename" and "dirname" want to muck with the
317 if (::strlen (pathname) > sizeof(resolved_path) - 1)
320 ::strcpy (resolved_path, pathname);
326 char *filename = ::basename (resolved_path);
329 m_filename.SetCString (filename);
330 // Truncate the basename off the end of the resolved path
332 // Only attempt to get the dirname if it looks like we have a path
333 if (strchr(resolved_path, '/')
335 || strchr(resolved_path, '\\')
339 char *directory = ::dirname (resolved_path);
341 // Make sure we didn't get our directory resolved to "." without having
344 m_directory.SetCString(directory);
347 char *last_resolved_path_slash = strrchr(resolved_path, '/');
349 char* last_resolved_path_slash_windows = strrchr(resolved_path, '\\');
350 if (last_resolved_path_slash_windows > last_resolved_path_slash)
351 last_resolved_path_slash = last_resolved_path_slash_windows;
353 if (last_resolved_path_slash)
355 *last_resolved_path_slash = '\0';
356 m_directory.SetCString(resolved_path);
362 m_directory.SetCString(resolved_path);
366 //----------------------------------------------------------------------
367 // Convert to pointer operator. This allows code to check any FileSpec
368 // objects to see if they contain anything valid using code such as:
372 //----------------------------------------------------------------------
373 FileSpec::operator bool() const
375 return m_filename || m_directory;
378 //----------------------------------------------------------------------
379 // Logical NOT operator. This allows code to check any FileSpec
380 // objects to see if they are invalid using code such as:
384 //----------------------------------------------------------------------
386 FileSpec::operator!() const
388 return !m_directory && !m_filename;
391 //------------------------------------------------------------------
393 //------------------------------------------------------------------
395 FileSpec::operator== (const FileSpec& rhs) const
397 if (m_filename == rhs.m_filename)
399 if (m_directory == rhs.m_directory)
402 // TODO: determine if we want to keep this code in here.
403 // The code below was added to handle a case where we were
404 // trying to set a file and line breakpoint and one path
405 // was resolved, and the other not and the directory was
406 // in a mount point that resolved to a more complete path:
407 // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling
409 if (IsResolved() && rhs.IsResolved())
411 // Both paths are resolved, no need to look further...
415 FileSpec resolved_lhs(*this);
417 // If "this" isn't resolved, resolve it
420 if (resolved_lhs.ResolvePath())
422 // This path wasn't resolved but now it is. Check if the resolved
423 // directory is the same as our unresolved directory, and if so,
424 // we can mark this object as resolved to avoid more future resolves
425 m_is_resolved = (m_directory == resolved_lhs.m_directory);
431 FileSpec resolved_rhs(rhs);
432 if (!rhs.IsResolved())
434 if (resolved_rhs.ResolvePath())
436 // rhs's path wasn't resolved but now it is. Check if the resolved
437 // directory is the same as rhs's unresolved directory, and if so,
438 // we can mark this object as resolved to avoid more future resolves
439 rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory);
445 // If we reach this point in the code we were able to resolve both paths
446 // and since we only resolve the paths if the basenames are equal, then
447 // we can just check if both directories are equal...
448 return resolved_lhs.GetDirectory() == resolved_rhs.GetDirectory();
453 //------------------------------------------------------------------
454 // Not equal to operator
455 //------------------------------------------------------------------
457 FileSpec::operator!= (const FileSpec& rhs) const
459 return !(*this == rhs);
462 //------------------------------------------------------------------
463 // Less than operator
464 //------------------------------------------------------------------
466 FileSpec::operator< (const FileSpec& rhs) const
468 return FileSpec::Compare(*this, rhs, true) < 0;
471 //------------------------------------------------------------------
472 // Dump a FileSpec object to a stream
473 //------------------------------------------------------------------
475 lldb_private::operator << (Stream &s, const FileSpec& f)
481 //------------------------------------------------------------------
482 // Clear this object by releasing both the directory and filename
483 // string values and making them both the empty string.
484 //------------------------------------------------------------------
492 //------------------------------------------------------------------
493 // Compare two FileSpec objects. If "full" is true, then both
494 // the directory and the filename must match. If "full" is false,
495 // then the directory names for "a" and "b" are only compared if
496 // they are both non-empty. This allows a FileSpec object to only
497 // contain a filename and it can match FileSpec objects that have
498 // matching filenames with different paths.
500 // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b"
501 // and "1" if "a" is greater than "b".
502 //------------------------------------------------------------------
504 FileSpec::Compare(const FileSpec& a, const FileSpec& b, bool full)
508 // If full is true, then we must compare both the directory and filename.
510 // If full is false, then if either directory is empty, then we match on
511 // the basename only, and if both directories have valid values, we still
512 // do a full compare. This allows for matching when we just have a filename
513 // in one of the FileSpec objects.
515 if (full || (a.m_directory && b.m_directory))
517 result = ConstString::Compare(a.m_directory, b.m_directory);
521 return ConstString::Compare (a.m_filename, b.m_filename);
525 FileSpec::Equal (const FileSpec& a, const FileSpec& b, bool full)
527 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
528 return a.m_filename == b.m_filename;
535 //------------------------------------------------------------------
536 // Dump the object to the supplied stream. If the object contains
537 // a valid directory name, it will be displayed followed by a
538 // directory delimiter, and the filename.
539 //------------------------------------------------------------------
541 FileSpec::Dump(Stream *s) const
543 static ConstString g_slash_only ("/");
547 if (m_directory && m_directory != g_slash_only)
553 //------------------------------------------------------------------
554 // Returns true if the file exists.
555 //------------------------------------------------------------------
557 FileSpec::Exists () const
559 struct stat file_stats;
560 return GetFileStats (this, &file_stats);
564 FileSpec::ResolveExecutableLocation ()
568 const char *file_cstr = m_filename.GetCString();
571 const std::string file_str (file_cstr);
572 llvm::sys::Path path = llvm::sys::Program::FindProgramByName (file_str);
573 const std::string &path_str = path.str();
574 llvm::StringRef dir_ref = llvm::sys::path::parent_path(path_str);
575 //llvm::StringRef dir_ref = path.getDirname();
576 if (! dir_ref.empty())
578 // FindProgramByName returns "." if it can't find the file.
579 if (strcmp (".", dir_ref.data()) == 0)
582 m_directory.SetCString (dir_ref.data());
587 // If FindProgramByName found the file, it returns the directory + filename in its return results.
588 // We need to separate them.
589 FileSpec tmp_file (dir_ref.data(), false);
590 if (tmp_file.Exists())
592 m_directory = tmp_file.m_directory;
604 FileSpec::ResolvePath ()
607 return true; // We have already resolved this path
609 char path_buf[PATH_MAX];
610 if (!GetPath (path_buf, PATH_MAX))
612 // SetFile(...) will set m_is_resolved correctly if it can resolve the path
613 SetFile (path_buf, true);
614 return m_is_resolved;
618 FileSpec::GetByteSize() const
620 struct stat file_stats;
621 if (GetFileStats (this, &file_stats))
622 return file_stats.st_size;
627 FileSpec::GetFileType () const
629 struct stat file_stats;
630 if (GetFileStats (this, &file_stats))
632 mode_t file_type = file_stats.st_mode & S_IFMT;
635 case S_IFDIR: return eFileTypeDirectory;
636 case S_IFREG: return eFileTypeRegular;
638 case S_IFIFO: return eFileTypePipe;
639 case S_IFSOCK: return eFileTypeSocket;
640 case S_IFLNK: return eFileTypeSymbolicLink;
645 return eFileTypeUnknown;
647 return eFileTypeInvalid;
651 FileSpec::GetModificationTime () const
654 struct stat file_stats;
655 if (GetFileStats (this, &file_stats))
656 mod_time.OffsetWithSeconds(file_stats.st_mtime);
660 //------------------------------------------------------------------
661 // Directory string get accessor.
662 //------------------------------------------------------------------
664 FileSpec::GetDirectory()
669 //------------------------------------------------------------------
670 // Directory string const get accessor.
671 //------------------------------------------------------------------
673 FileSpec::GetDirectory() const
678 //------------------------------------------------------------------
679 // Filename string get accessor.
680 //------------------------------------------------------------------
682 FileSpec::GetFilename()
687 //------------------------------------------------------------------
688 // Filename string const get accessor.
689 //------------------------------------------------------------------
691 FileSpec::GetFilename() const
696 //------------------------------------------------------------------
697 // Extract the directory and path into a fixed buffer. This is
698 // needed as the directory and path are stored in separate string
700 //------------------------------------------------------------------
702 FileSpec::GetPath(char *path, size_t path_max_len) const
706 const char *dirname = m_directory.GetCString();
707 const char *filename = m_filename.GetCString();
711 return ::snprintf (path, path_max_len, "%s/%s", dirname, filename);
713 return ::snprintf (path, path_max_len, "%s", dirname);
717 return ::snprintf (path, path_max_len, "%s", filename);
726 FileSpec::GetPath (void) const
728 static ConstString g_slash_only ("/");
730 const char *dirname = m_directory.GetCString();
731 const char *filename = m_filename.GetCString();
734 path.append (dirname);
735 if (filename && m_directory != g_slash_only)
739 path.append (filename);
744 FileSpec::GetFileNameExtension () const
748 const char *filename = m_filename.GetCString();
749 const char* dot_pos = strrchr(filename, '.');
750 if (dot_pos && dot_pos[1] != '\0')
751 return ConstString(dot_pos+1);
753 return ConstString();
757 FileSpec::GetFileNameStrippingExtension () const
759 const char *filename = m_filename.GetCString();
760 if (filename == NULL)
761 return ConstString();
763 const char* dot_pos = strrchr(filename, '.');
767 return ConstString(filename, dot_pos-filename);
770 //------------------------------------------------------------------
771 // Returns a shared pointer to a data buffer that contains all or
772 // part of the contents of a file. The data is memory mapped and
773 // will lazily page in data from the file as memory is accessed.
774 // The data that is mappped will start "file_offset" bytes into the
775 // file, and "file_size" bytes will be mapped. If "file_size" is
776 // greater than the number of bytes available in the file starting
777 // at "file_offset", the number of bytes will be appropriately
778 // truncated. The final number of bytes that get mapped can be
779 // verified using the DataBuffer::GetByteSize() function.
780 //------------------------------------------------------------------
782 FileSpec::MemoryMapFileContents(off_t file_offset, size_t file_size) const
784 DataBufferSP data_sp;
785 std::unique_ptr<DataBufferMemoryMap> mmap_data(new DataBufferMemoryMap());
788 const size_t mapped_length = mmap_data->MemoryMapFromFileSpec (this, file_offset, file_size);
789 if (((file_size == SIZE_MAX) && (mapped_length > 0)) || (mapped_length >= file_size))
790 data_sp.reset(mmap_data.release());
796 //------------------------------------------------------------------
797 // Return the size in bytes that this object takes in memory. This
798 // returns the size in bytes of this object, not any shared string
799 // values it may refer to.
800 //------------------------------------------------------------------
802 FileSpec::MemorySize() const
804 return m_filename.MemorySize() + m_directory.MemorySize();
809 FileSpec::ReadFileContents (off_t file_offset, void *dst, size_t dst_len, Error *error_ptr) const
812 size_t bytes_read = 0;
813 char resolved_path[PATH_MAX];
814 if (GetPath(resolved_path, sizeof(resolved_path)))
817 error = file.Open(resolved_path, File::eOpenOptionRead);
820 off_t file_offset_after_seek = file_offset;
821 bytes_read = dst_len;
822 error = file.Read(dst, bytes_read, file_offset_after_seek);
827 error.SetErrorString("invalid file specification");
834 //------------------------------------------------------------------
835 // Returns a shared pointer to a data buffer that contains all or
836 // part of the contents of a file. The data copies into a heap based
837 // buffer that lives in the DataBuffer shared pointer object returned.
838 // The data that is cached will start "file_offset" bytes into the
839 // file, and "file_size" bytes will be mapped. If "file_size" is
840 // greater than the number of bytes available in the file starting
841 // at "file_offset", the number of bytes will be appropriately
842 // truncated. The final number of bytes that get mapped can be
843 // verified using the DataBuffer::GetByteSize() function.
844 //------------------------------------------------------------------
846 FileSpec::ReadFileContents (off_t file_offset, size_t file_size, Error *error_ptr) const
849 DataBufferSP data_sp;
850 char resolved_path[PATH_MAX];
851 if (GetPath(resolved_path, sizeof(resolved_path)))
854 error = file.Open(resolved_path, File::eOpenOptionRead);
857 const bool null_terminate = false;
858 error = file.Read (file_size, file_offset, null_terminate, data_sp);
863 error.SetErrorString("invalid file specification");
871 FileSpec::ReadFileContentsAsCString(Error *error_ptr)
874 DataBufferSP data_sp;
875 char resolved_path[PATH_MAX];
876 if (GetPath(resolved_path, sizeof(resolved_path)))
879 error = file.Open(resolved_path, File::eOpenOptionRead);
883 size_t length = SIZE_MAX;
884 const bool null_terminate = true;
885 error = file.Read (length, offset, null_terminate, data_sp);
890 error.SetErrorString("invalid file specification");
898 FileSpec::ReadFileLines (STLStringArray &lines)
902 if (GetPath(path, sizeof(path)))
904 std::ifstream file_stream (path);
909 while (getline (file_stream, line))
910 lines.push_back (line);
916 FileSpec::EnumerateDirectoryResult
917 FileSpec::EnumerateDirectory
919 const char *dir_path,
920 bool find_directories,
923 EnumerateDirectoryCallbackType callback,
927 if (dir_path && dir_path[0])
930 char szDir[MAX_PATH];
931 strcpy_s(szDir, MAX_PATH, dir_path);
932 strcat_s(szDir, MAX_PATH, "\\*");
935 HANDLE hFind = FindFirstFile(szDir, &ffd);
937 if (hFind == INVALID_HANDLE_VALUE)
939 return eEnumerateDirectoryResultNext;
944 bool call_callback = false;
945 FileSpec::FileType file_type = eFileTypeUnknown;
946 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
948 size_t len = strlen(ffd.cFileName);
950 if (len == 1 && ffd.cFileName[0] == '.')
953 if (len == 2 && ffd.cFileName[0] == '.' && ffd.cFileName[1] == '.')
956 file_type = eFileTypeDirectory;
957 call_callback = find_directories;
959 else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DEVICE)
961 file_type = eFileTypeOther;
962 call_callback = find_other;
966 file_type = eFileTypeRegular;
967 call_callback = find_files;
971 char child_path[MAX_PATH];
972 const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s\\%s", dir_path, ffd.cFileName);
973 if (child_path_len < (int)(sizeof(child_path) - 1))
975 // Don't resolve the file type or path
976 FileSpec child_path_spec (child_path, false);
978 EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec);
982 case eEnumerateDirectoryResultNext:
983 // Enumerate next entry in the current directory. We just
984 // exit this switch and will continue enumerating the
985 // current directory as we currently are...
988 case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not
989 if (FileSpec::EnumerateDirectory(child_path,
994 callback_baton) == eEnumerateDirectoryResultQuit)
996 // The subdirectory returned Quit, which means to
997 // stop all directory enumerations at all levels.
998 return eEnumerateDirectoryResultQuit;
1002 case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level.
1003 // Exit from this directory level and tell parent to
1004 // keep enumerating.
1005 return eEnumerateDirectoryResultNext;
1007 case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level
1008 return eEnumerateDirectoryResultQuit;
1012 } while (FindNextFile(hFind, &ffd) != 0);
1016 lldb_utility::CleanUp <DIR *, int> dir_path_dir(opendir(dir_path), NULL, closedir);
1017 if (dir_path_dir.is_valid())
1019 long path_max = fpathconf (dirfd (dir_path_dir.get()), _PC_NAME_MAX);
1020 #if defined (__APPLE_) && defined (__DARWIN_MAXPATHLEN)
1021 if (path_max < __DARWIN_MAXPATHLEN)
1022 path_max = __DARWIN_MAXPATHLEN;
1024 struct dirent *buf, *dp;
1025 buf = (struct dirent *) malloc (offsetof (struct dirent, d_name) + path_max + 1);
1027 while (buf && readdir_r(dir_path_dir.get(), buf, &dp) == 0 && dp)
1029 // Only search directories
1030 if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN)
1032 size_t len = strlen(dp->d_name);
1034 if (len == 1 && dp->d_name[0] == '.')
1037 if (len == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.')
1041 bool call_callback = false;
1042 FileSpec::FileType file_type = eFileTypeUnknown;
1047 case DT_UNKNOWN: file_type = eFileTypeUnknown; call_callback = true; break;
1048 case DT_FIFO: file_type = eFileTypePipe; call_callback = find_other; break;
1049 case DT_CHR: file_type = eFileTypeOther; call_callback = find_other; break;
1050 case DT_DIR: file_type = eFileTypeDirectory; call_callback = find_directories; break;
1051 case DT_BLK: file_type = eFileTypeOther; call_callback = find_other; break;
1052 case DT_REG: file_type = eFileTypeRegular; call_callback = find_files; break;
1053 case DT_LNK: file_type = eFileTypeSymbolicLink; call_callback = find_other; break;
1054 case DT_SOCK: file_type = eFileTypeSocket; call_callback = find_other; break;
1055 #if !defined(__OpenBSD__)
1056 case DT_WHT: file_type = eFileTypeOther; call_callback = find_other; break;
1062 char child_path[PATH_MAX];
1063 const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s/%s", dir_path, dp->d_name);
1064 if (child_path_len < (int)(sizeof(child_path) - 1))
1066 // Don't resolve the file type or path
1067 FileSpec child_path_spec (child_path, false);
1069 EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec);
1073 case eEnumerateDirectoryResultNext:
1074 // Enumerate next entry in the current directory. We just
1075 // exit this switch and will continue enumerating the
1076 // current directory as we currently are...
1079 case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not
1080 if (FileSpec::EnumerateDirectory (child_path,
1085 callback_baton) == eEnumerateDirectoryResultQuit)
1087 // The subdirectory returned Quit, which means to
1088 // stop all directory enumerations at all levels.
1091 return eEnumerateDirectoryResultQuit;
1095 case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level.
1096 // Exit from this directory level and tell parent to
1097 // keep enumerating.
1100 return eEnumerateDirectoryResultNext;
1102 case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level
1105 return eEnumerateDirectoryResultQuit;
1117 // By default when exiting a directory, we tell the parent enumeration
1118 // to continue enumerating.
1119 return eEnumerateDirectoryResultNext;
1123 FileSpec::CopyByAppendingPathComponent (const char *new_path) const
1125 const bool resolve = false;
1126 if (m_filename.IsEmpty() && m_directory.IsEmpty())
1127 return FileSpec(new_path,resolve);
1128 StreamString stream;
1129 if (m_filename.IsEmpty())
1130 stream.Printf("%s/%s",m_directory.GetCString(),new_path);
1131 else if (m_directory.IsEmpty())
1132 stream.Printf("%s/%s",m_filename.GetCString(),new_path);
1134 stream.Printf("%s/%s/%s",m_directory.GetCString(), m_filename.GetCString(),new_path);
1135 return FileSpec(stream.GetData(),resolve);
1139 FileSpec::CopyByRemovingLastPathComponent () const
1141 const bool resolve = false;
1142 if (m_filename.IsEmpty() && m_directory.IsEmpty())
1143 return FileSpec("",resolve);
1144 if (m_directory.IsEmpty())
1145 return FileSpec("",resolve);
1146 if (m_filename.IsEmpty())
1148 const char* dir_cstr = m_directory.GetCString();
1149 const char* last_slash_ptr = ::strrchr(dir_cstr, '/');
1151 // check for obvious cases before doing the full thing
1152 if (!last_slash_ptr)
1153 return FileSpec("",resolve);
1154 if (last_slash_ptr == dir_cstr)
1155 return FileSpec("/",resolve);
1157 size_t last_slash_pos = last_slash_ptr - dir_cstr+1;
1158 ConstString new_path(dir_cstr,last_slash_pos);
1159 return FileSpec(new_path.GetCString(),resolve);
1162 return FileSpec(m_directory.GetCString(),resolve);
1166 FileSpec::GetLastPathComponent () const
1168 if (m_filename.IsEmpty() && m_directory.IsEmpty())
1170 if (m_filename.IsEmpty())
1172 const char* dir_cstr = m_directory.GetCString();
1173 const char* last_slash_ptr = ::strrchr(dir_cstr, '/');
1174 if (last_slash_ptr == NULL)
1175 return m_directory.GetCString();
1176 if (last_slash_ptr == dir_cstr)
1178 if (last_slash_ptr[1] == 0)
1179 return last_slash_ptr;
1181 return last_slash_ptr+1;
1183 if (last_slash_ptr[1] != 0)
1184 return last_slash_ptr+1;
1185 const char* penultimate_slash_ptr = last_slash_ptr;
1186 while (*penultimate_slash_ptr)
1188 --penultimate_slash_ptr;
1189 if (penultimate_slash_ptr == dir_cstr)
1191 if (*penultimate_slash_ptr == '/')
1194 ConstString new_path(penultimate_slash_ptr+1,last_slash_ptr-penultimate_slash_ptr);
1195 return new_path.AsCString();
1197 return m_filename.GetCString();
1201 FileSpec::AppendPathComponent (const char *new_path)
1203 const bool resolve = false;
1204 if (m_filename.IsEmpty() && m_directory.IsEmpty())
1206 SetFile(new_path,resolve);
1209 StreamString stream;
1210 if (m_filename.IsEmpty())
1211 stream.Printf("%s/%s",m_directory.GetCString(),new_path);
1212 else if (m_directory.IsEmpty())
1213 stream.Printf("%s/%s",m_filename.GetCString(),new_path);
1215 stream.Printf("%s/%s/%s",m_directory.GetCString(), m_filename.GetCString(),new_path);
1216 SetFile(stream.GetData(), resolve);
1220 FileSpec::RemoveLastPathComponent ()
1222 const bool resolve = false;
1223 if (m_filename.IsEmpty() && m_directory.IsEmpty())
1225 SetFile("",resolve);
1228 if (m_directory.IsEmpty())
1230 SetFile("",resolve);
1233 if (m_filename.IsEmpty())
1235 const char* dir_cstr = m_directory.GetCString();
1236 const char* last_slash_ptr = ::strrchr(dir_cstr, '/');
1238 // check for obvious cases before doing the full thing
1239 if (!last_slash_ptr)
1241 SetFile("",resolve);
1244 if (last_slash_ptr == dir_cstr)
1246 SetFile("/",resolve);
1249 size_t last_slash_pos = last_slash_ptr - dir_cstr+1;
1250 ConstString new_path(dir_cstr,last_slash_pos);
1251 SetFile(new_path.GetCString(),resolve);
1254 SetFile(m_directory.GetCString(),resolve);
1256 //------------------------------------------------------------------
1257 /// Returns true if the filespec represents an implementation source
1258 /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
1262 /// \b true if the filespec represents an implementation source
1263 /// file, \b false otherwise.
1264 //------------------------------------------------------------------
1266 FileSpec::IsSourceImplementationFile () const
1268 ConstString extension (GetFileNameExtension());
1271 static RegularExpression g_source_file_regex ("^(c|m|mm|cpp|c\\+\\+|cxx|cc|cp|s|asm|f|f77|f90|f95|f03|for|ftn|fpp|ada|adb|ads)$",
1272 REG_EXTENDED | REG_ICASE);
1273 return g_source_file_regex.Execute (extension.GetCString());
1279 FileSpec::IsRelativeToCurrentWorkingDirectory () const
1281 const char *directory = m_directory.GetCString();
1282 if (directory && directory[0])
1284 // If the path doesn't start with '/' or '~', return true
1285 switch (directory[0])
1294 else if (m_filename)
1296 // No directory, just a basename, return true