blob: 557c7043b07852a13b351d8048dc25b60b0986ef [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Clayton52fd9842011-02-02 02:24:04 +000011#include <dirent.h>
Chris Lattner24943d22010-06-08 16:52:24 +000012#include <fcntl.h>
Chris Lattner24943d22010-06-08 16:52:24 +000013#include <libgen.h>
Chris Lattner24943d22010-06-08 16:52:24 +000014#include <sys/stat.h>
Greg Clayton90df5632011-02-07 17:41:11 +000015#include <string.h>
Jim Inghama39fa4b2011-02-07 19:42:39 +000016#include <fstream>
Greg Clayton8da92a72011-02-05 02:27:52 +000017
Jim Inghama39fa4b2011-02-07 19:42:39 +000018#include "lldb/Host/Config.h" // Have to include this before we test the define...
Greg Clayton14ef59f2011-02-08 00:35:34 +000019#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Jim Inghama30baf52010-07-01 01:48:53 +000020#include <pwd.h>
Greg Clayton8da92a72011-02-05 02:27:52 +000021#endif
Chris Lattner24943d22010-06-08 16:52:24 +000022
Caroline Ticee9ca3a42010-09-12 00:10:52 +000023#include "llvm/ADT/StringRef.h"
Greg Clayton22defe82010-12-02 23:20:03 +000024#include "llvm/Support/Path.h"
25#include "llvm/Support/Program.h"
Caroline Ticee9ca3a42010-09-12 00:10:52 +000026
Greg Clayton5f54ac32011-02-08 05:05:52 +000027#include "lldb/Host/FileSpec.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Core/DataBufferHeap.h"
29#include "lldb/Core/DataBufferMemoryMap.h"
30#include "lldb/Core/Stream.h"
Caroline Ticeeddffe92010-09-10 04:48:55 +000031#include "lldb/Host/Host.h"
Greg Clayton52fd9842011-02-02 02:24:04 +000032#include "lldb/Utility/CleanUp.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033
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[0], sizeof(resolved_path)))
43 return ::stat (resolved_path, stats_ptr) == 0;
44 return false;
45}
46
Greg Clayton14ef59f2011-02-08 00:35:34 +000047#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Greg Clayton8da92a72011-02-05 02:27:52 +000048
Chris Lattner24943d22010-06-08 16:52:24 +000049static const char*
50GetCachedGlobTildeSlash()
51{
52 static std::string g_tilde;
53 if (g_tilde.empty())
54 {
Jim Inghama30baf52010-07-01 01:48:53 +000055 struct passwd *user_entry;
56 user_entry = getpwuid(geteuid());
57 if (user_entry != NULL)
58 g_tilde = user_entry->pw_dir;
59
Chris Lattner24943d22010-06-08 16:52:24 +000060 if (g_tilde.empty())
61 return NULL;
62 }
63 return g_tilde.c_str();
64}
65
Jim Inghama30baf52010-07-01 01:48:53 +000066// 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 Clayton54e7afa2010-07-09 20:39:50 +000071size_t
Jim Inghama30baf52010-07-01 01:48:53 +000072FileSpec::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 Claytonb1a98622010-07-01 17:07:48 +000079
Jim Inghama30baf52010-07-01 01:48:53 +000080 // 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 Clayton54e7afa2010-07-09 20:39:50 +000083 size_t len = strlen (src_path);
Jim Inghama30baf52010-07-01 01:48:53 +000084 if (len >= dst_len)
85 {
Greg Claytonb1a98622010-07-01 17:07:48 +000086 ::bcopy (src_path, dst_path, dst_len - 1);
87 dst_path[dst_len] = '\0';
Jim Inghama30baf52010-07-01 01:48:53 +000088 }
89 else
Greg Claytonb1a98622010-07-01 17:07:48 +000090 ::bcopy (src_path, dst_path, len + 1);
91
Jim Inghama30baf52010-07-01 01:48:53 +000092 return len;
93 }
94
Eli Friedman9c3cfe32010-07-02 19:15:50 +000095 const char *first_slash = ::strchr (src_path, '/');
Jim Inghama30baf52010-07-01 01:48:53 +000096 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 Claytonb1a98622010-07-01 17:07:48 +0000107 ::memcpy (user_home, src_path + 1, user_name_len);
Jim Inghama30baf52010-07-01 01:48:53 +0000108 user_home[user_name_len] = '\0';
109 user_name = user_home;
110
Greg Claytonb1a98622010-07-01 17:07:48 +0000111 ::strcpy (remainder, first_slash);
Jim Inghama30baf52010-07-01 01:48:53 +0000112 }
Greg Claytonb1a98622010-07-01 17:07:48 +0000113
Jim Inghama30baf52010-07-01 01:48:53 +0000114 if (user_name == NULL)
115 return 0;
116 // User name of "" means the current user...
117
118 struct passwd *user_entry;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000119 const char *home_dir = NULL;
Jim Inghama30baf52010-07-01 01:48:53 +0000120
121 if (user_name[0] == '\0')
122 {
123 home_dir = GetCachedGlobTildeSlash();
124 }
125 else
126 {
Greg Claytonb1a98622010-07-01 17:07:48 +0000127 user_entry = ::getpwnam (user_name);
Jim Inghama30baf52010-07-01 01:48:53 +0000128 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 Clayton14ef59f2011-02-08 00:35:34 +0000137#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Jim Inghama30baf52010-07-01 01:48:53 +0000138
Greg Clayton54e7afa2010-07-09 20:39:50 +0000139size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000140FileSpec::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 Clayton14ef59f2011-02-08 00:35:34 +0000147#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Jim Inghama30baf52010-07-01 01:48:53 +0000148 if (src_path[0] == '~')
149 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000150 size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path));
Jim Inghama30baf52010-07-01 01:48:53 +0000151
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 Lattner24943d22010-06-08 16:52:24 +0000157 else
Greg Clayton14ef59f2011-02-08 00:35:34 +0000158#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Greg Clayton8da92a72011-02-05 02:27:52 +0000159 {
160 ::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path);
161 }
Chris Lattner24943d22010-06-08 16:52:24 +0000162
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
177FileSpec::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 Inghamc4547c52010-09-16 00:57:33 +0000187FileSpec::FileSpec(const char *pathname, bool resolve_path) :
188 m_directory(),
Greg Claytond6d806c2010-11-08 00:28:40 +0000189 m_filename(),
190 m_is_resolved(false)
Jim Inghamc4547c52010-09-16 00:57:33 +0000191{
192 if (pathname && pathname[0])
193 SetFile(pathname, resolve_path);
194}
195
196//------------------------------------------------------------------
Chris Lattner24943d22010-06-08 16:52:24 +0000197// Copy constructor
198//------------------------------------------------------------------
199FileSpec::FileSpec(const FileSpec& rhs) :
200 m_directory (rhs.m_directory),
Greg Claytond6d806c2010-11-08 00:28:40 +0000201 m_filename (rhs.m_filename),
202 m_is_resolved (rhs.m_is_resolved)
Chris Lattner24943d22010-06-08 16:52:24 +0000203{
204}
205
206//------------------------------------------------------------------
207// Copy constructor
208//------------------------------------------------------------------
209FileSpec::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//------------------------------------------------------------------
220FileSpec::~FileSpec()
221{
222}
223
224//------------------------------------------------------------------
225// Assignment operator.
226//------------------------------------------------------------------
227const FileSpec&
228FileSpec::operator= (const FileSpec& rhs)
229{
230 if (this != &rhs)
231 {
232 m_directory = rhs.m_directory;
233 m_filename = rhs.m_filename;
Greg Claytond6d806c2010-11-08 00:28:40 +0000234 m_is_resolved = rhs.m_is_resolved;
Chris Lattner24943d22010-06-08 16:52:24 +0000235 }
236 return *this;
237}
238
Chris Lattner24943d22010-06-08 16:52:24 +0000239//------------------------------------------------------------------
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//------------------------------------------------------------------
244void
Greg Claytond6d806c2010-11-08 00:28:40 +0000245FileSpec::SetFile (const char *pathname, bool resolve)
Chris Lattner24943d22010-06-08 16:52:24 +0000246{
247 m_filename.Clear();
248 m_directory.Clear();
Greg Claytond6d806c2010-11-08 00:28:40 +0000249 m_is_resolved = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000250 if (pathname == NULL || pathname[0] == '\0')
251 return;
252
253 char resolved_path[PATH_MAX];
Jim Inghamc4547c52010-09-16 00:57:33 +0000254 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 Claytond6d806c2010-11-08 00:28:40 +0000259 m_is_resolved = path_fit;
Jim Inghamc4547c52010-09-16 00:57:33 +0000260 }
261 else
262 {
Greg Claytond6d806c2010-11-08 00:28:40 +0000263 // Copy the path because "basename" and "dirname" want to muck with the
264 // path buffer
265 if (::strlen (pathname) > sizeof(resolved_path) - 1)
Jim Inghamc4547c52010-09-16 00:57:33 +0000266 path_fit = false;
267 else
Greg Claytond6d806c2010-11-08 00:28:40 +0000268 ::strcpy (resolved_path, pathname);
Jim Inghamc4547c52010-09-16 00:57:33 +0000269 }
Chris Lattner24943d22010-06-08 16:52:24 +0000270
Jim Inghamc4547c52010-09-16 00:57:33 +0000271
272 if (path_fit)
Chris Lattner24943d22010-06-08 16:52:24 +0000273 {
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//----------------------------------------------------------------------
312FileSpec::operator
313void*() 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//----------------------------------------------------------------------
325bool
326FileSpec::operator!() const
327{
328 return !m_directory && !m_filename;
329}
330
331//------------------------------------------------------------------
332// Equal to operator
333//------------------------------------------------------------------
334bool
335FileSpec::operator== (const FileSpec& rhs) const
336{
Greg Claytond6d806c2010-11-08 00:28:40 +0000337 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 Lattner24943d22010-06-08 16:52:24 +0000391}
392
393//------------------------------------------------------------------
394// Not equal to operator
395//------------------------------------------------------------------
396bool
397FileSpec::operator!= (const FileSpec& rhs) const
398{
Greg Claytond6d806c2010-11-08 00:28:40 +0000399 return !(*this == rhs);
Chris Lattner24943d22010-06-08 16:52:24 +0000400}
401
402//------------------------------------------------------------------
403// Less than operator
404//------------------------------------------------------------------
405bool
406FileSpec::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//------------------------------------------------------------------
414Stream&
415lldb_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//------------------------------------------------------------------
425void
426FileSpec::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//------------------------------------------------------------------
443int
444FileSpec::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
464bool
465FileSpec::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//------------------------------------------------------------------
480void
481FileSpec::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//------------------------------------------------------------------
498bool
499FileSpec::Exists () const
500{
501 struct stat file_stats;
502 return GetFileStats (this, &file_stats);
503}
504
Caroline Ticeeddffe92010-09-10 04:48:55 +0000505bool
506FileSpec::ResolveExecutableLocation ()
507{
Greg Clayton537a7a82010-10-20 20:54:39 +0000508 if (!m_directory)
Caroline Ticee9ca3a42010-09-12 00:10:52 +0000509 {
Greg Clayton27345db2011-01-25 21:32:01 +0000510 const char *file_cstr = m_filename.GetCString();
511 if (file_cstr)
Caroline Ticee9ca3a42010-09-12 00:10:52 +0000512 {
Greg Clayton27345db2011-01-25 21:32:01 +0000513 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 Ticee9ca3a42010-09-12 00:10:52 +0000519 {
Greg Clayton27345db2011-01-25 21:32:01 +0000520 // 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 Ticee9ca3a42010-09-12 00:10:52 +0000526 return true;
Greg Clayton27345db2011-01-25 21:32:01 +0000527 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 Ticee9ca3a42010-09-12 00:10:52 +0000537 }
538 }
539 }
540 }
541
542 return false;
Caroline Ticeeddffe92010-09-10 04:48:55 +0000543}
544
Jim Inghamc4547c52010-09-16 00:57:33 +0000545bool
546FileSpec::ResolvePath ()
547{
Greg Claytond6d806c2010-11-08 00:28:40 +0000548 if (m_is_resolved)
549 return true; // We have already resolved this path
550
551 char path_buf[PATH_MAX];
Jim Inghamc4547c52010-09-16 00:57:33 +0000552 if (!GetPath (path_buf, PATH_MAX))
553 return false;
Greg Claytond6d806c2010-11-08 00:28:40 +0000554 // SetFile(...) will set m_is_resolved correctly if it can resolve the path
Jim Inghamc4547c52010-09-16 00:57:33 +0000555 SetFile (path_buf, true);
Greg Claytond6d806c2010-11-08 00:28:40 +0000556 return m_is_resolved;
Jim Inghamc4547c52010-09-16 00:57:33 +0000557}
558
Chris Lattner24943d22010-06-08 16:52:24 +0000559uint64_t
560FileSpec::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
568FileSpec::FileType
569FileSpec::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 Clayton52fd9842011-02-02 02:24:04 +0000585 return eFileTypeUnknown;
Chris Lattner24943d22010-06-08 16:52:24 +0000586 }
587 return eFileTypeInvalid;
588}
589
590TimeValue
591FileSpec::GetModificationTime () const
592{
593 TimeValue mod_time;
594 struct stat file_stats;
595 if (GetFileStats (this, &file_stats))
Eli Friedmanb5869012010-06-11 04:52:22 +0000596 mod_time.OffsetWithSeconds(file_stats.st_mtime);
Chris Lattner24943d22010-06-08 16:52:24 +0000597 return mod_time;
598}
599
600//------------------------------------------------------------------
601// Directory string get accessor.
602//------------------------------------------------------------------
603ConstString &
604FileSpec::GetDirectory()
605{
606 return m_directory;
607}
608
609//------------------------------------------------------------------
610// Directory string const get accessor.
611//------------------------------------------------------------------
612const ConstString &
613FileSpec::GetDirectory() const
614{
615 return m_directory;
616}
617
618//------------------------------------------------------------------
619// Filename string get accessor.
620//------------------------------------------------------------------
621ConstString &
622FileSpec::GetFilename()
623{
624 return m_filename;
625}
626
627//------------------------------------------------------------------
628// Filename string const get accessor.
629//------------------------------------------------------------------
630const ConstString &
631FileSpec::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 Clayton49ce6822010-10-31 03:01:06 +0000641size_t
642FileSpec::GetPath(char *path, size_t path_max_len) const
Chris Lattner24943d22010-06-08 16:52:24 +0000643{
Greg Clayton49ce6822010-10-31 03:01:06 +0000644 if (path_max_len)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000645 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000646 const char *dirname = m_directory.GetCString();
647 const char *filename = m_filename.GetCString();
Greg Clayton24b48ff2010-10-17 22:03:32 +0000648 if (dirname)
649 {
650 if (filename)
Greg Clayton49ce6822010-10-31 03:01:06 +0000651 return ::snprintf (path, path_max_len, "%s/%s", dirname, filename);
Greg Clayton24b48ff2010-10-17 22:03:32 +0000652 else
Greg Clayton49ce6822010-10-31 03:01:06 +0000653 return ::snprintf (path, path_max_len, "%s", dirname);
Greg Clayton24b48ff2010-10-17 22:03:32 +0000654 }
655 else if (filename)
656 {
Greg Clayton49ce6822010-10-31 03:01:06 +0000657 return ::snprintf (path, path_max_len, "%s", filename);
Greg Clayton24b48ff2010-10-17 22:03:32 +0000658 }
659 }
Chris Lattner24943d22010-06-08 16:52:24 +0000660 path[0] = '\0';
Greg Clayton49ce6822010-10-31 03:01:06 +0000661 return 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000662}
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//------------------------------------------------------------------
675DataBufferSP
676FileSpec::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//------------------------------------------------------------------
694size_t
695FileSpec::MemorySize() const
696{
697 return m_filename.MemorySize() + m_directory.MemorySize();
698}
699
Greg Clayton70436352010-06-30 23:03:03 +0000700
701size_t
702FileSpec::ReadFileContents (off_t file_offset, void *dst, size_t dst_len) const
Chris Lattner24943d22010-06-08 16:52:24 +0000703{
Greg Clayton70436352010-06-30 23:03:03 +0000704 size_t bytes_read = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000705 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 Clayton70436352010-06-30 23:03:03 +0000721 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//------------------------------------------------------------------
746DataBufferSP
747FileSpec::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 Lattner24943d22010-06-08 16:52:24 +0000765 if (lseek_result < 0)
766 {
767 // Get error from errno
768 }
769 else if (lseek_result == file_offset)
770 {
Greg Clayton70436352010-06-30 23:03:03 +0000771 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 Lattner24943d22010-06-08 16:52:24 +0000776 std::auto_ptr<DataBufferHeap> data_heap_ap;
Greg Clayton70436352010-06-30 23:03:03 +0000777 data_heap_ap.reset(new DataBufferHeap(num_bytes_to_read, '\0'));
Chris Lattner24943d22010-06-08 16:52:24 +0000778
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 Clayton54e7afa2010-07-09 20:39:50 +0000786 if ((size_t)bytesRead < data_heap_ap->GetByteSize())
Chris Lattner24943d22010-06-08 16:52:24 +0000787 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 Clayton3d0e2c22010-10-20 22:52:05 +0000800size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000801FileSpec::ReadFileLines (STLStringArray &lines)
802{
Chris Lattner24943d22010-06-08 16:52:24 +0000803 lines.clear();
Greg Clayton3d0e2c22010-10-20 22:52:05 +0000804 char path[PATH_MAX];
805 if (GetPath(path, sizeof(path)))
Chris Lattner24943d22010-06-08 16:52:24 +0000806 {
Greg Clayton3d0e2c22010-10-20 22:52:05 +0000807 ifstream file_stream (path);
Chris Lattner24943d22010-06-08 16:52:24 +0000808
Greg Clayton3d0e2c22010-10-20 22:52:05 +0000809 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 Lattner24943d22010-06-08 16:52:24 +0000817}
Greg Clayton52fd9842011-02-02 02:24:04 +0000818
819FileSpec::EnumerateDirectoryResult
820FileSpec::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 Clayton90df5632011-02-07 17:41:11 +0000841 size_t len = strlen(dp->d_name);
842
843 if (len == 1 && dp->d_name[0] == '.')
Greg Clayton52fd9842011-02-02 02:24:04 +0000844 continue;
845
Greg Clayton90df5632011-02-07 17:41:11 +0000846 if (len == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.')
Greg Clayton52fd9842011-02-02 02:24:04 +0000847 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