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