blob: 0bf7681aa48bd4eb27b45226a0defc9acc49e32f [file] [log] [blame]
Chris Lattner30fdc8d2010-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
Virgile Bellob2f1fb22013-08-23 12:44:05 +000011#ifndef _WIN32
Greg Clayton4272cc72011-02-02 02:24:04 +000012#include <dirent.h>
Virgile Bellob2f1fb22013-08-23 12:44:05 +000013#else
14#include "lldb/Host/windows/windows.h"
15#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include <fcntl.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include <libgen.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include <sys/stat.h>
Rafael Espindola09079162013-06-13 20:10:23 +000019#include <set>
Greg Claytone0f3c022011-02-07 17:41:11 +000020#include <string.h>
Jim Ingham9035e7c2011-02-07 19:42:39 +000021#include <fstream>
Greg Claytonfd184262011-02-05 02:27:52 +000022
Jim Ingham9035e7c2011-02-07 19:42:39 +000023#include "lldb/Host/Config.h" // Have to include this before we test the define...
Greg Clayton45319462011-02-08 00:35:34 +000024#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Jim Inghamf818ca32010-07-01 01:48:53 +000025#include <pwd.h>
Greg Claytonfd184262011-02-05 02:27:52 +000026#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Caroline Tice391a9602010-09-12 00:10:52 +000028#include "llvm/ADT/StringRef.h"
Greg Clayton38a61402010-12-02 23:20:03 +000029#include "llvm/Support/Path.h"
30#include "llvm/Support/Program.h"
Caroline Tice391a9602010-09-12 00:10:52 +000031
Greg Clayton96c09682012-01-04 22:56:43 +000032#include "lldb/Host/File.h"
Greg Clayton53239f02011-02-08 05:05:52 +000033#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Core/DataBufferHeap.h"
35#include "lldb/Core/DataBufferMemoryMap.h"
Greg Clayton1f746072012-08-29 21:13:06 +000036#include "lldb/Core/RegularExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Core/Stream.h"
Caroline Tice428a9a52010-09-10 04:48:55 +000038#include "lldb/Host/Host.h"
Greg Clayton4272cc72011-02-02 02:24:04 +000039#include "lldb/Utility/CleanUp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040
41using namespace lldb;
42using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
44static bool
45GetFileStats (const FileSpec *file_spec, struct stat *stats_ptr)
46{
47 char resolved_path[PATH_MAX];
Greg Clayton7e14f912011-04-23 02:04:55 +000048 if (file_spec->GetPath (resolved_path, sizeof(resolved_path)))
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 return ::stat (resolved_path, stats_ptr) == 0;
50 return false;
51}
52
Greg Clayton45319462011-02-08 00:35:34 +000053#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Greg Claytonfd184262011-02-05 02:27:52 +000054
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055static const char*
56GetCachedGlobTildeSlash()
57{
58 static std::string g_tilde;
59 if (g_tilde.empty())
60 {
Jim Inghamf818ca32010-07-01 01:48:53 +000061 struct passwd *user_entry;
62 user_entry = getpwuid(geteuid());
63 if (user_entry != NULL)
64 g_tilde = user_entry->pw_dir;
65
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066 if (g_tilde.empty())
67 return NULL;
68 }
69 return g_tilde.c_str();
70}
71
Greg Clayton87e5ff02011-02-08 05:19:06 +000072#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
73
Jim Inghamf818ca32010-07-01 01:48:53 +000074// Resolves the username part of a path of the form ~user/other/directories, and
75// writes the result into dst_path.
76// Returns 0 if there WAS a ~ in the path but the username couldn't be resolved.
77// Otherwise returns the number of characters copied into dst_path. If the return
78// is >= dst_len, then the resolved path is too long...
Greg Claytonc982c762010-07-09 20:39:50 +000079size_t
Jim Inghamf818ca32010-07-01 01:48:53 +000080FileSpec::ResolveUsername (const char *src_path, char *dst_path, size_t dst_len)
81{
Greg Clayton87e5ff02011-02-08 05:19:06 +000082 if (src_path == NULL || src_path[0] == '\0')
83 return 0;
84
85#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
86
Jim Inghamf818ca32010-07-01 01:48:53 +000087 char user_home[PATH_MAX];
88 const char *user_name;
89
Greg Claytona5d24f62010-07-01 17:07:48 +000090
Jim Inghamf818ca32010-07-01 01:48:53 +000091 // If there's no ~, then just copy src_path straight to dst_path (they may be the same string...)
92 if (src_path[0] != '~')
93 {
Greg Claytonc982c762010-07-09 20:39:50 +000094 size_t len = strlen (src_path);
Jim Inghamf818ca32010-07-01 01:48:53 +000095 if (len >= dst_len)
96 {
Greg Claytona5d24f62010-07-01 17:07:48 +000097 ::bcopy (src_path, dst_path, dst_len - 1);
98 dst_path[dst_len] = '\0';
Jim Inghamf818ca32010-07-01 01:48:53 +000099 }
100 else
Greg Claytona5d24f62010-07-01 17:07:48 +0000101 ::bcopy (src_path, dst_path, len + 1);
102
Jim Inghamf818ca32010-07-01 01:48:53 +0000103 return len;
104 }
105
Eli Friedmanfeaeebf2010-07-02 19:15:50 +0000106 const char *first_slash = ::strchr (src_path, '/');
Jim Inghamf818ca32010-07-01 01:48:53 +0000107 char remainder[PATH_MAX];
108
109 if (first_slash == NULL)
110 {
111 // The whole name is the username (minus the ~):
112 user_name = src_path + 1;
113 remainder[0] = '\0';
114 }
115 else
116 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000117 size_t user_name_len = first_slash - src_path - 1;
Greg Claytona5d24f62010-07-01 17:07:48 +0000118 ::memcpy (user_home, src_path + 1, user_name_len);
Jim Inghamf818ca32010-07-01 01:48:53 +0000119 user_home[user_name_len] = '\0';
120 user_name = user_home;
121
Greg Claytona5d24f62010-07-01 17:07:48 +0000122 ::strcpy (remainder, first_slash);
Jim Inghamf818ca32010-07-01 01:48:53 +0000123 }
Greg Claytona5d24f62010-07-01 17:07:48 +0000124
Jim Inghamf818ca32010-07-01 01:48:53 +0000125 if (user_name == NULL)
126 return 0;
127 // User name of "" means the current user...
128
129 struct passwd *user_entry;
Greg Claytonc982c762010-07-09 20:39:50 +0000130 const char *home_dir = NULL;
Jim Inghamf818ca32010-07-01 01:48:53 +0000131
132 if (user_name[0] == '\0')
133 {
134 home_dir = GetCachedGlobTildeSlash();
135 }
136 else
137 {
Greg Claytona5d24f62010-07-01 17:07:48 +0000138 user_entry = ::getpwnam (user_name);
Jim Inghamf818ca32010-07-01 01:48:53 +0000139 if (user_entry != NULL)
140 home_dir = user_entry->pw_dir;
141 }
142
143 if (home_dir == NULL)
144 return 0;
145 else
146 return ::snprintf (dst_path, dst_len, "%s%s", home_dir, remainder);
Greg Clayton87e5ff02011-02-08 05:19:06 +0000147#else
148 // Resolving home directories is not supported, just copy the path...
149 return ::snprintf (dst_path, dst_len, "%s", src_path);
150#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Jim Inghamf818ca32010-07-01 01:48:53 +0000151}
152
Greg Claytonc982c762010-07-09 20:39:50 +0000153size_t
Jim Ingham84363072011-02-08 23:24:09 +0000154FileSpec::ResolvePartialUsername (const char *partial_name, StringList &matches)
155{
156#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
157 size_t extant_entries = matches.GetSize();
158
159 setpwent();
160 struct passwd *user_entry;
161 const char *name_start = partial_name + 1;
162 std::set<std::string> name_list;
163
164 while ((user_entry = getpwent()) != NULL)
165 {
166 if (strstr(user_entry->pw_name, name_start) == user_entry->pw_name)
167 {
168 std::string tmp_buf("~");
169 tmp_buf.append(user_entry->pw_name);
170 tmp_buf.push_back('/');
171 name_list.insert(tmp_buf);
172 }
173 }
174 std::set<std::string>::iterator pos, end = name_list.end();
175 for (pos = name_list.begin(); pos != end; pos++)
176 {
177 matches.AppendString((*pos).c_str());
178 }
179 return matches.GetSize() - extant_entries;
180#else
181 // Resolving home directories is not supported, just copy the path...
182 return 0;
183#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
184}
185
186
187
188size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189FileSpec::Resolve (const char *src_path, char *dst_path, size_t dst_len)
190{
191 if (src_path == NULL || src_path[0] == '\0')
192 return 0;
193
194 // Glob if needed for ~/, otherwise copy in case src_path is same as dst_path...
195 char unglobbed_path[PATH_MAX];
Greg Clayton45319462011-02-08 00:35:34 +0000196#ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Jim Inghamf818ca32010-07-01 01:48:53 +0000197 if (src_path[0] == '~')
198 {
Greg Claytonc982c762010-07-09 20:39:50 +0000199 size_t return_count = ResolveUsername(src_path, unglobbed_path, sizeof(unglobbed_path));
Jim Inghamf818ca32010-07-01 01:48:53 +0000200
201 // If we couldn't find the user referred to, or the resultant path was too long,
202 // then just copy over the src_path.
203 if (return_count == 0 || return_count >= sizeof(unglobbed_path))
204 ::snprintf (unglobbed_path, sizeof(unglobbed_path), "%s", src_path);
205 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 else
Greg Clayton45319462011-02-08 00:35:34 +0000207#endif // #ifdef LLDB_CONFIG_TILDE_RESOLVES_TO_USER
Greg Claytonfd184262011-02-05 02:27:52 +0000208 {
209 ::snprintf(unglobbed_path, sizeof(unglobbed_path), "%s", src_path);
210 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211
212 // Now resolve the path if needed
213 char resolved_path[PATH_MAX];
214 if (::realpath (unglobbed_path, resolved_path))
215 {
216 // Success, copy the resolved path
217 return ::snprintf(dst_path, dst_len, "%s", resolved_path);
218 }
219 else
220 {
221 // Failed, just copy the unglobbed path
222 return ::snprintf(dst_path, dst_len, "%s", unglobbed_path);
223 }
224}
225
226FileSpec::FileSpec() :
227 m_directory(),
228 m_filename()
229{
230}
231
232//------------------------------------------------------------------
233// Default constructor that can take an optional full path to a
234// file on disk.
235//------------------------------------------------------------------
Jim Ingham0909e5f2010-09-16 00:57:33 +0000236FileSpec::FileSpec(const char *pathname, bool resolve_path) :
237 m_directory(),
Greg Clayton7481c202010-11-08 00:28:40 +0000238 m_filename(),
239 m_is_resolved(false)
Jim Ingham0909e5f2010-09-16 00:57:33 +0000240{
241 if (pathname && pathname[0])
242 SetFile(pathname, resolve_path);
243}
244
245//------------------------------------------------------------------
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246// Copy constructor
247//------------------------------------------------------------------
248FileSpec::FileSpec(const FileSpec& rhs) :
249 m_directory (rhs.m_directory),
Greg Clayton7481c202010-11-08 00:28:40 +0000250 m_filename (rhs.m_filename),
251 m_is_resolved (rhs.m_is_resolved)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252{
253}
254
255//------------------------------------------------------------------
256// Copy constructor
257//------------------------------------------------------------------
258FileSpec::FileSpec(const FileSpec* rhs) :
259 m_directory(),
260 m_filename()
261{
262 if (rhs)
263 *this = *rhs;
264}
265
266//------------------------------------------------------------------
267// Virtual destrcuctor in case anyone inherits from this class.
268//------------------------------------------------------------------
269FileSpec::~FileSpec()
270{
271}
272
273//------------------------------------------------------------------
274// Assignment operator.
275//------------------------------------------------------------------
276const FileSpec&
277FileSpec::operator= (const FileSpec& rhs)
278{
279 if (this != &rhs)
280 {
281 m_directory = rhs.m_directory;
282 m_filename = rhs.m_filename;
Greg Clayton7481c202010-11-08 00:28:40 +0000283 m_is_resolved = rhs.m_is_resolved;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 }
285 return *this;
286}
287
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288//------------------------------------------------------------------
289// Update the contents of this object with a new path. The path will
290// be split up into a directory and filename and stored as uniqued
291// string values for quick comparison and efficient memory usage.
292//------------------------------------------------------------------
293void
Greg Clayton7481c202010-11-08 00:28:40 +0000294FileSpec::SetFile (const char *pathname, bool resolve)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295{
296 m_filename.Clear();
297 m_directory.Clear();
Greg Clayton7481c202010-11-08 00:28:40 +0000298 m_is_resolved = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299 if (pathname == NULL || pathname[0] == '\0')
300 return;
301
302 char resolved_path[PATH_MAX];
Jim Ingham0909e5f2010-09-16 00:57:33 +0000303 bool path_fit = true;
304
305 if (resolve)
306 {
307 path_fit = (FileSpec::Resolve (pathname, resolved_path, sizeof(resolved_path)) < sizeof(resolved_path) - 1);
Greg Clayton7481c202010-11-08 00:28:40 +0000308 m_is_resolved = path_fit;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000309 }
310 else
311 {
Greg Clayton7481c202010-11-08 00:28:40 +0000312 // Copy the path because "basename" and "dirname" want to muck with the
313 // path buffer
314 if (::strlen (pathname) > sizeof(resolved_path) - 1)
Jim Ingham0909e5f2010-09-16 00:57:33 +0000315 path_fit = false;
316 else
Greg Clayton7481c202010-11-08 00:28:40 +0000317 ::strcpy (resolved_path, pathname);
Jim Ingham0909e5f2010-09-16 00:57:33 +0000318 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319
Jim Ingham0909e5f2010-09-16 00:57:33 +0000320
321 if (path_fit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 {
323 char *filename = ::basename (resolved_path);
324 if (filename)
325 {
326 m_filename.SetCString (filename);
327 // Truncate the basename off the end of the resolved path
328
329 // Only attempt to get the dirname if it looks like we have a path
330 if (strchr(resolved_path, '/'))
331 {
332 char *directory = ::dirname (resolved_path);
333
334 // Make sure we didn't get our directory resolved to "." without having
335 // specified
336 if (directory)
337 m_directory.SetCString(directory);
338 else
339 {
340 char *last_resolved_path_slash = strrchr(resolved_path, '/');
341 if (last_resolved_path_slash)
342 {
343 *last_resolved_path_slash = '\0';
344 m_directory.SetCString(resolved_path);
345 }
346 }
347 }
348 }
349 else
350 m_directory.SetCString(resolved_path);
351 }
352}
353
354//----------------------------------------------------------------------
355// Convert to pointer operator. This allows code to check any FileSpec
356// objects to see if they contain anything valid using code such as:
357//
358// if (file_spec)
359// {}
360//----------------------------------------------------------------------
Greg Clayton6372d1c2011-09-12 04:00:42 +0000361FileSpec::operator bool() const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362{
Greg Clayton6372d1c2011-09-12 04:00:42 +0000363 return m_filename || m_directory;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364}
365
366//----------------------------------------------------------------------
367// Logical NOT operator. This allows code to check any FileSpec
368// objects to see if they are invalid using code such as:
369//
370// if (!file_spec)
371// {}
372//----------------------------------------------------------------------
373bool
374FileSpec::operator!() const
375{
376 return !m_directory && !m_filename;
377}
378
379//------------------------------------------------------------------
380// Equal to operator
381//------------------------------------------------------------------
382bool
383FileSpec::operator== (const FileSpec& rhs) const
384{
Greg Clayton7481c202010-11-08 00:28:40 +0000385 if (m_filename == rhs.m_filename)
386 {
387 if (m_directory == rhs.m_directory)
388 return true;
389
390 // TODO: determine if we want to keep this code in here.
391 // The code below was added to handle a case where we were
392 // trying to set a file and line breakpoint and one path
393 // was resolved, and the other not and the directory was
394 // in a mount point that resolved to a more complete path:
395 // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling
396 // this out...
397 if (IsResolved() && rhs.IsResolved())
398 {
399 // Both paths are resolved, no need to look further...
400 return false;
401 }
402
403 FileSpec resolved_lhs(*this);
404
405 // If "this" isn't resolved, resolve it
406 if (!IsResolved())
407 {
408 if (resolved_lhs.ResolvePath())
409 {
410 // This path wasn't resolved but now it is. Check if the resolved
411 // directory is the same as our unresolved directory, and if so,
412 // we can mark this object as resolved to avoid more future resolves
413 m_is_resolved = (m_directory == resolved_lhs.m_directory);
414 }
415 else
416 return false;
417 }
418
419 FileSpec resolved_rhs(rhs);
420 if (!rhs.IsResolved())
421 {
422 if (resolved_rhs.ResolvePath())
423 {
424 // rhs's path wasn't resolved but now it is. Check if the resolved
425 // directory is the same as rhs's unresolved directory, and if so,
426 // we can mark this object as resolved to avoid more future resolves
Jim Inghama537f6c2012-11-03 02:12:46 +0000427 rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory);
Greg Clayton7481c202010-11-08 00:28:40 +0000428 }
429 else
430 return false;
431 }
432
433 // If we reach this point in the code we were able to resolve both paths
434 // and since we only resolve the paths if the basenames are equal, then
435 // we can just check if both directories are equal...
436 return resolved_lhs.GetDirectory() == resolved_rhs.GetDirectory();
437 }
438 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439}
440
441//------------------------------------------------------------------
442// Not equal to operator
443//------------------------------------------------------------------
444bool
445FileSpec::operator!= (const FileSpec& rhs) const
446{
Greg Clayton7481c202010-11-08 00:28:40 +0000447 return !(*this == rhs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448}
449
450//------------------------------------------------------------------
451// Less than operator
452//------------------------------------------------------------------
453bool
454FileSpec::operator< (const FileSpec& rhs) const
455{
456 return FileSpec::Compare(*this, rhs, true) < 0;
457}
458
459//------------------------------------------------------------------
460// Dump a FileSpec object to a stream
461//------------------------------------------------------------------
462Stream&
463lldb_private::operator << (Stream &s, const FileSpec& f)
464{
465 f.Dump(&s);
466 return s;
467}
468
469//------------------------------------------------------------------
470// Clear this object by releasing both the directory and filename
471// string values and making them both the empty string.
472//------------------------------------------------------------------
473void
474FileSpec::Clear()
475{
476 m_directory.Clear();
477 m_filename.Clear();
478}
479
480//------------------------------------------------------------------
481// Compare two FileSpec objects. If "full" is true, then both
482// the directory and the filename must match. If "full" is false,
483// then the directory names for "a" and "b" are only compared if
484// they are both non-empty. This allows a FileSpec object to only
485// contain a filename and it can match FileSpec objects that have
486// matching filenames with different paths.
487//
488// Return -1 if the "a" is less than "b", 0 if "a" is equal to "b"
489// and "1" if "a" is greater than "b".
490//------------------------------------------------------------------
491int
492FileSpec::Compare(const FileSpec& a, const FileSpec& b, bool full)
493{
494 int result = 0;
495
496 // If full is true, then we must compare both the directory and filename.
497
498 // If full is false, then if either directory is empty, then we match on
499 // the basename only, and if both directories have valid values, we still
500 // do a full compare. This allows for matching when we just have a filename
501 // in one of the FileSpec objects.
502
503 if (full || (a.m_directory && b.m_directory))
504 {
505 result = ConstString::Compare(a.m_directory, b.m_directory);
506 if (result)
507 return result;
508 }
509 return ConstString::Compare (a.m_filename, b.m_filename);
510}
511
512bool
513FileSpec::Equal (const FileSpec& a, const FileSpec& b, bool full)
514{
Jim Ingham87df91b2011-09-23 00:54:11 +0000515 if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516 return a.m_filename == b.m_filename;
Jim Ingham87df91b2011-09-23 00:54:11 +0000517 else
518 return a == b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519}
520
521
522
523//------------------------------------------------------------------
524// Dump the object to the supplied stream. If the object contains
525// a valid directory name, it will be displayed followed by a
526// directory delimiter, and the filename.
527//------------------------------------------------------------------
528void
529FileSpec::Dump(Stream *s) const
530{
Jason Molendadb7d11c2013-05-06 10:21:11 +0000531 static ConstString g_slash_only ("/");
Enrico Granata80fcdd42012-11-03 00:09:46 +0000532 if (s)
533 {
534 m_directory.Dump(s);
Jason Molendadb7d11c2013-05-06 10:21:11 +0000535 if (m_directory && m_directory != g_slash_only)
Enrico Granata80fcdd42012-11-03 00:09:46 +0000536 s->PutChar('/');
537 m_filename.Dump(s);
538 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539}
540
541//------------------------------------------------------------------
542// Returns true if the file exists.
543//------------------------------------------------------------------
544bool
545FileSpec::Exists () const
546{
547 struct stat file_stats;
548 return GetFileStats (this, &file_stats);
549}
550
Caroline Tice428a9a52010-09-10 04:48:55 +0000551bool
552FileSpec::ResolveExecutableLocation ()
553{
Greg Clayton274060b2010-10-20 20:54:39 +0000554 if (!m_directory)
Caroline Tice391a9602010-09-12 00:10:52 +0000555 {
Greg Clayton58f41712011-01-25 21:32:01 +0000556 const char *file_cstr = m_filename.GetCString();
557 if (file_cstr)
Caroline Tice391a9602010-09-12 00:10:52 +0000558 {
Greg Clayton58f41712011-01-25 21:32:01 +0000559 const std::string file_str (file_cstr);
Rafael Espindolaff89ff22013-06-13 19:25:41 +0000560 std::string path = llvm::sys::FindProgramByName (file_str);
561 llvm::StringRef dir_ref = llvm::sys::path::parent_path(path);
Greg Clayton58f41712011-01-25 21:32:01 +0000562 //llvm::StringRef dir_ref = path.getDirname();
563 if (! dir_ref.empty())
Caroline Tice391a9602010-09-12 00:10:52 +0000564 {
Greg Clayton58f41712011-01-25 21:32:01 +0000565 // FindProgramByName returns "." if it can't find the file.
566 if (strcmp (".", dir_ref.data()) == 0)
567 return false;
568
569 m_directory.SetCString (dir_ref.data());
570 if (Exists())
Caroline Tice391a9602010-09-12 00:10:52 +0000571 return true;
Greg Clayton58f41712011-01-25 21:32:01 +0000572 else
573 {
574 // If FindProgramByName found the file, it returns the directory + filename in its return results.
575 // We need to separate them.
576 FileSpec tmp_file (dir_ref.data(), false);
577 if (tmp_file.Exists())
578 {
579 m_directory = tmp_file.m_directory;
580 return true;
581 }
Caroline Tice391a9602010-09-12 00:10:52 +0000582 }
583 }
584 }
585 }
586
587 return false;
Caroline Tice428a9a52010-09-10 04:48:55 +0000588}
589
Jim Ingham0909e5f2010-09-16 00:57:33 +0000590bool
591FileSpec::ResolvePath ()
592{
Greg Clayton7481c202010-11-08 00:28:40 +0000593 if (m_is_resolved)
594 return true; // We have already resolved this path
595
596 char path_buf[PATH_MAX];
Jim Ingham0909e5f2010-09-16 00:57:33 +0000597 if (!GetPath (path_buf, PATH_MAX))
598 return false;
Greg Clayton7481c202010-11-08 00:28:40 +0000599 // SetFile(...) will set m_is_resolved correctly if it can resolve the path
Jim Ingham0909e5f2010-09-16 00:57:33 +0000600 SetFile (path_buf, true);
Greg Clayton7481c202010-11-08 00:28:40 +0000601 return m_is_resolved;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000602}
603
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604uint64_t
605FileSpec::GetByteSize() const
606{
607 struct stat file_stats;
608 if (GetFileStats (this, &file_stats))
609 return file_stats.st_size;
610 return 0;
611}
612
613FileSpec::FileType
614FileSpec::GetFileType () const
615{
616 struct stat file_stats;
617 if (GetFileStats (this, &file_stats))
618 {
619 mode_t file_type = file_stats.st_mode & S_IFMT;
620 switch (file_type)
621 {
622 case S_IFDIR: return eFileTypeDirectory;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 case S_IFREG: return eFileTypeRegular;
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000624#ifndef _WIN32
625 case S_IFIFO: return eFileTypePipe;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000626 case S_IFSOCK: return eFileTypeSocket;
627 case S_IFLNK: return eFileTypeSymbolicLink;
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000628#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 default:
630 break;
631 }
Greg Clayton4272cc72011-02-02 02:24:04 +0000632 return eFileTypeUnknown;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 }
634 return eFileTypeInvalid;
635}
636
637TimeValue
638FileSpec::GetModificationTime () const
639{
640 TimeValue mod_time;
641 struct stat file_stats;
642 if (GetFileStats (this, &file_stats))
Eli Friedman6abb6342010-06-11 04:52:22 +0000643 mod_time.OffsetWithSeconds(file_stats.st_mtime);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 return mod_time;
645}
646
647//------------------------------------------------------------------
648// Directory string get accessor.
649//------------------------------------------------------------------
650ConstString &
651FileSpec::GetDirectory()
652{
653 return m_directory;
654}
655
656//------------------------------------------------------------------
657// Directory string const get accessor.
658//------------------------------------------------------------------
659const ConstString &
660FileSpec::GetDirectory() const
661{
662 return m_directory;
663}
664
665//------------------------------------------------------------------
666// Filename string get accessor.
667//------------------------------------------------------------------
668ConstString &
669FileSpec::GetFilename()
670{
671 return m_filename;
672}
673
674//------------------------------------------------------------------
675// Filename string const get accessor.
676//------------------------------------------------------------------
677const ConstString &
678FileSpec::GetFilename() const
679{
680 return m_filename;
681}
682
683//------------------------------------------------------------------
684// Extract the directory and path into a fixed buffer. This is
685// needed as the directory and path are stored in separate string
686// values.
687//------------------------------------------------------------------
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000688size_t
689FileSpec::GetPath(char *path, size_t path_max_len) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690{
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000691 if (path_max_len)
Greg Claytondd36def2010-10-17 22:03:32 +0000692 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000693 const char *dirname = m_directory.GetCString();
694 const char *filename = m_filename.GetCString();
Greg Claytondd36def2010-10-17 22:03:32 +0000695 if (dirname)
696 {
697 if (filename)
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000698 return ::snprintf (path, path_max_len, "%s/%s", dirname, filename);
Greg Claytondd36def2010-10-17 22:03:32 +0000699 else
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000700 return ::snprintf (path, path_max_len, "%s", dirname);
Greg Claytondd36def2010-10-17 22:03:32 +0000701 }
702 else if (filename)
703 {
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000704 return ::snprintf (path, path_max_len, "%s", filename);
Greg Claytondd36def2010-10-17 22:03:32 +0000705 }
706 }
Enrico Granataa9dbf432011-10-17 21:45:27 +0000707 if (path)
708 path[0] = '\0';
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000709 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710}
711
Greg Claytona44c1e62013-04-29 16:36:27 +0000712std::string
713FileSpec::GetPath (void) const
Jason Molendaa7ae4672013-04-29 09:46:43 +0000714{
Jason Molendadb7d11c2013-05-06 10:21:11 +0000715 static ConstString g_slash_only ("/");
Greg Claytona44c1e62013-04-29 16:36:27 +0000716 std::string path;
Jason Molendaa7ae4672013-04-29 09:46:43 +0000717 const char *dirname = m_directory.GetCString();
718 const char *filename = m_filename.GetCString();
Jason Molendaa7ae4672013-04-29 09:46:43 +0000719 if (dirname)
720 {
721 path.append (dirname);
Jason Molendadb7d11c2013-05-06 10:21:11 +0000722 if (filename && m_directory != g_slash_only)
Jason Molendaa7ae4672013-04-29 09:46:43 +0000723 path.append ("/");
724 }
725 if (filename)
Jason Molendaa7ae4672013-04-29 09:46:43 +0000726 path.append (filename);
Jason Molendaa7ae4672013-04-29 09:46:43 +0000727 return path;
728}
729
Enrico Granataa9dbf432011-10-17 21:45:27 +0000730ConstString
731FileSpec::GetFileNameExtension () const
732{
Greg Clayton1f746072012-08-29 21:13:06 +0000733 if (m_filename)
734 {
735 const char *filename = m_filename.GetCString();
736 const char* dot_pos = strrchr(filename, '.');
737 if (dot_pos && dot_pos[1] != '\0')
738 return ConstString(dot_pos+1);
739 }
740 return ConstString();
Enrico Granataa9dbf432011-10-17 21:45:27 +0000741}
742
743ConstString
744FileSpec::GetFileNameStrippingExtension () const
745{
746 const char *filename = m_filename.GetCString();
747 if (filename == NULL)
748 return ConstString();
749
Johnny Chenf5df5372011-10-18 19:28:30 +0000750 const char* dot_pos = strrchr(filename, '.');
Enrico Granataa9dbf432011-10-17 21:45:27 +0000751 if (dot_pos == NULL)
752 return m_filename;
753
754 return ConstString(filename, dot_pos-filename);
755}
756
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757//------------------------------------------------------------------
758// Returns a shared pointer to a data buffer that contains all or
759// part of the contents of a file. The data is memory mapped and
760// will lazily page in data from the file as memory is accessed.
761// The data that is mappped will start "file_offset" bytes into the
762// file, and "file_size" bytes will be mapped. If "file_size" is
763// greater than the number of bytes available in the file starting
764// at "file_offset", the number of bytes will be appropriately
765// truncated. The final number of bytes that get mapped can be
766// verified using the DataBuffer::GetByteSize() function.
767//------------------------------------------------------------------
768DataBufferSP
769FileSpec::MemoryMapFileContents(off_t file_offset, size_t file_size) const
770{
771 DataBufferSP data_sp;
Greg Clayton7b0992d2013-04-18 22:45:39 +0000772 std::unique_ptr<DataBufferMemoryMap> mmap_data(new DataBufferMemoryMap());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 if (mmap_data.get())
774 {
Greg Claytond398a1c2013-04-20 00:23:26 +0000775 const size_t mapped_length = mmap_data->MemoryMapFromFileSpec (this, file_offset, file_size);
776 if (((file_size == SIZE_MAX) && (mapped_length > 0)) || (mapped_length >= file_size))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 data_sp.reset(mmap_data.release());
778 }
779 return data_sp;
780}
781
782
783//------------------------------------------------------------------
784// Return the size in bytes that this object takes in memory. This
785// returns the size in bytes of this object, not any shared string
786// values it may refer to.
787//------------------------------------------------------------------
788size_t
789FileSpec::MemorySize() const
790{
791 return m_filename.MemorySize() + m_directory.MemorySize();
792}
793
Greg Claytondda4f7b2010-06-30 23:03:03 +0000794
795size_t
Greg Clayton4017fa32012-01-06 02:01:06 +0000796FileSpec::ReadFileContents (off_t file_offset, void *dst, size_t dst_len, Error *error_ptr) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797{
Greg Clayton4017fa32012-01-06 02:01:06 +0000798 Error error;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000799 size_t bytes_read = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800 char resolved_path[PATH_MAX];
801 if (GetPath(resolved_path, sizeof(resolved_path)))
802 {
Greg Clayton96c09682012-01-04 22:56:43 +0000803 File file;
804 error = file.Open(resolved_path, File::eOpenOptionRead);
805 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 {
Greg Clayton96c09682012-01-04 22:56:43 +0000807 off_t file_offset_after_seek = file_offset;
808 bytes_read = dst_len;
809 error = file.Read(dst, bytes_read, file_offset_after_seek);
Greg Claytondda4f7b2010-06-30 23:03:03 +0000810 }
Greg Claytondda4f7b2010-06-30 23:03:03 +0000811 }
Greg Clayton4017fa32012-01-06 02:01:06 +0000812 else
813 {
814 error.SetErrorString("invalid file specification");
815 }
816 if (error_ptr)
817 *error_ptr = error;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000818 return bytes_read;
819}
820
821//------------------------------------------------------------------
822// Returns a shared pointer to a data buffer that contains all or
823// part of the contents of a file. The data copies into a heap based
824// buffer that lives in the DataBuffer shared pointer object returned.
825// The data that is cached will start "file_offset" bytes into the
826// file, and "file_size" bytes will be mapped. If "file_size" is
827// greater than the number of bytes available in the file starting
828// at "file_offset", the number of bytes will be appropriately
829// truncated. The final number of bytes that get mapped can be
830// verified using the DataBuffer::GetByteSize() function.
831//------------------------------------------------------------------
832DataBufferSP
Greg Clayton4017fa32012-01-06 02:01:06 +0000833FileSpec::ReadFileContents (off_t file_offset, size_t file_size, Error *error_ptr) const
Greg Claytondda4f7b2010-06-30 23:03:03 +0000834{
Greg Clayton4017fa32012-01-06 02:01:06 +0000835 Error error;
Greg Claytondda4f7b2010-06-30 23:03:03 +0000836 DataBufferSP data_sp;
837 char resolved_path[PATH_MAX];
838 if (GetPath(resolved_path, sizeof(resolved_path)))
839 {
Greg Clayton96c09682012-01-04 22:56:43 +0000840 File file;
841 error = file.Open(resolved_path, File::eOpenOptionRead);
842 if (error.Success())
Greg Clayton0b0b5122012-08-30 18:15:10 +0000843 {
844 const bool null_terminate = false;
845 error = file.Read (file_size, file_offset, null_terminate, data_sp);
846 }
847 }
848 else
849 {
850 error.SetErrorString("invalid file specification");
851 }
852 if (error_ptr)
853 *error_ptr = error;
854 return data_sp;
855}
856
857DataBufferSP
858FileSpec::ReadFileContentsAsCString(Error *error_ptr)
859{
860 Error error;
861 DataBufferSP data_sp;
862 char resolved_path[PATH_MAX];
863 if (GetPath(resolved_path, sizeof(resolved_path)))
864 {
865 File file;
866 error = file.Open(resolved_path, File::eOpenOptionRead);
867 if (error.Success())
868 {
869 off_t offset = 0;
870 size_t length = SIZE_MAX;
871 const bool null_terminate = true;
872 error = file.Read (length, offset, null_terminate, data_sp);
873 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 }
Greg Clayton4017fa32012-01-06 02:01:06 +0000875 else
876 {
877 error.SetErrorString("invalid file specification");
878 }
879 if (error_ptr)
880 *error_ptr = error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881 return data_sp;
882}
883
Greg Clayton58fc50e2010-10-20 22:52:05 +0000884size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885FileSpec::ReadFileLines (STLStringArray &lines)
886{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887 lines.clear();
Greg Clayton58fc50e2010-10-20 22:52:05 +0000888 char path[PATH_MAX];
889 if (GetPath(path, sizeof(path)))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000890 {
Greg Claytone01e07b2013-04-18 18:10:51 +0000891 std::ifstream file_stream (path);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892
Greg Clayton58fc50e2010-10-20 22:52:05 +0000893 if (file_stream)
894 {
895 std::string line;
896 while (getline (file_stream, line))
897 lines.push_back (line);
898 }
899 }
900 return lines.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901}
Greg Clayton4272cc72011-02-02 02:24:04 +0000902
903FileSpec::EnumerateDirectoryResult
904FileSpec::EnumerateDirectory
905(
906 const char *dir_path,
907 bool find_directories,
908 bool find_files,
909 bool find_other,
910 EnumerateDirectoryCallbackType callback,
911 void *callback_baton
912)
913{
914 if (dir_path && dir_path[0])
915 {
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000916#if _WIN32
917 char szDir[MAX_PATH];
918 strcpy_s(szDir, MAX_PATH, dir_path);
919 strcat_s(szDir, MAX_PATH, "\\*");
920
921 WIN32_FIND_DATA ffd;
922 HANDLE hFind = FindFirstFile(szDir, &ffd);
923
924 if (hFind == INVALID_HANDLE_VALUE)
925 {
926 return eEnumerateDirectoryResultNext;
927 }
928
929 do
930 {
931 bool call_callback = false;
932 FileSpec::FileType file_type = eFileTypeUnknown;
933 if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
934 {
935 size_t len = strlen(ffd.cFileName);
936
937 if (len == 1 && ffd.cFileName[0] == '.')
938 continue;
939
940 if (len == 2 && ffd.cFileName[0] == '.' && ffd.cFileName[1] == '.')
941 continue;
942
943 file_type = eFileTypeDirectory;
944 call_callback = find_directories;
945 }
946 else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DEVICE)
947 {
948 file_type = eFileTypeOther;
949 call_callback = find_other;
950 }
951 else
952 {
953 file_type = eFileTypeRegular;
954 call_callback = find_files;
955 }
956 if (call_callback)
957 {
958 char child_path[MAX_PATH];
959 const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s\\%s", dir_path, ffd.cFileName);
960 if (child_path_len < (int)(sizeof(child_path) - 1))
961 {
962 // Don't resolve the file type or path
963 FileSpec child_path_spec (child_path, false);
964
965 EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec);
966
967 switch (result)
968 {
969 case eEnumerateDirectoryResultNext:
970 // Enumerate next entry in the current directory. We just
971 // exit this switch and will continue enumerating the
972 // current directory as we currently are...
973 break;
974
975 case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not
976 if (FileSpec::EnumerateDirectory(child_path,
977 find_directories,
978 find_files,
979 find_other,
980 callback,
981 callback_baton) == eEnumerateDirectoryResultQuit)
982 {
983 // The subdirectory returned Quit, which means to
984 // stop all directory enumerations at all levels.
985 return eEnumerateDirectoryResultQuit;
986 }
987 break;
988
989 case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level.
990 // Exit from this directory level and tell parent to
991 // keep enumerating.
992 return eEnumerateDirectoryResultNext;
993
994 case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level
995 return eEnumerateDirectoryResultQuit;
996 }
997 }
998 }
999 } while (FindNextFile(hFind, &ffd) != 0);
1000
1001 FindClose(hFind);
1002#else
1003 lldb_utility::CleanUp <DIR *, int> dir_path_dir(opendir(dir_path), NULL, closedir);
Greg Clayton4272cc72011-02-02 02:24:04 +00001004 if (dir_path_dir.is_valid())
1005 {
Jason Molenda14aef122013-04-04 03:19:27 +00001006 long path_max = fpathconf (dirfd (dir_path_dir.get()), _PC_NAME_MAX);
1007#if defined (__APPLE_) && defined (__DARWIN_MAXPATHLEN)
1008 if (path_max < __DARWIN_MAXPATHLEN)
1009 path_max = __DARWIN_MAXPATHLEN;
1010#endif
1011 struct dirent *buf, *dp;
1012 buf = (struct dirent *) malloc (offsetof (struct dirent, d_name) + path_max + 1);
1013
1014 while (buf && readdir_r(dir_path_dir.get(), buf, &dp) == 0 && dp)
Greg Clayton4272cc72011-02-02 02:24:04 +00001015 {
1016 // Only search directories
1017 if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN)
1018 {
Greg Claytone0f3c022011-02-07 17:41:11 +00001019 size_t len = strlen(dp->d_name);
1020
1021 if (len == 1 && dp->d_name[0] == '.')
Greg Clayton4272cc72011-02-02 02:24:04 +00001022 continue;
1023
Greg Claytone0f3c022011-02-07 17:41:11 +00001024 if (len == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.')
Greg Clayton4272cc72011-02-02 02:24:04 +00001025 continue;
1026 }
1027
1028 bool call_callback = false;
1029 FileSpec::FileType file_type = eFileTypeUnknown;
1030
1031 switch (dp->d_type)
1032 {
1033 default:
1034 case DT_UNKNOWN: file_type = eFileTypeUnknown; call_callback = true; break;
1035 case DT_FIFO: file_type = eFileTypePipe; call_callback = find_other; break;
1036 case DT_CHR: file_type = eFileTypeOther; call_callback = find_other; break;
1037 case DT_DIR: file_type = eFileTypeDirectory; call_callback = find_directories; break;
1038 case DT_BLK: file_type = eFileTypeOther; call_callback = find_other; break;
1039 case DT_REG: file_type = eFileTypeRegular; call_callback = find_files; break;
1040 case DT_LNK: file_type = eFileTypeSymbolicLink; call_callback = find_other; break;
1041 case DT_SOCK: file_type = eFileTypeSocket; call_callback = find_other; break;
Greg Clayton2b4d9b72011-04-01 18:18:34 +00001042#if !defined(__OpenBSD__)
Greg Clayton4272cc72011-02-02 02:24:04 +00001043 case DT_WHT: file_type = eFileTypeOther; call_callback = find_other; break;
Greg Clayton2b4d9b72011-04-01 18:18:34 +00001044#endif
Greg Clayton4272cc72011-02-02 02:24:04 +00001045 }
1046
1047 if (call_callback)
1048 {
1049 char child_path[PATH_MAX];
1050 const int child_path_len = ::snprintf (child_path, sizeof(child_path), "%s/%s", dir_path, dp->d_name);
Johnny Chen44805302011-07-19 19:48:13 +00001051 if (child_path_len < (int)(sizeof(child_path) - 1))
Greg Clayton4272cc72011-02-02 02:24:04 +00001052 {
1053 // Don't resolve the file type or path
1054 FileSpec child_path_spec (child_path, false);
1055
1056 EnumerateDirectoryResult result = callback (callback_baton, file_type, child_path_spec);
1057
1058 switch (result)
1059 {
Greg Clayton4272cc72011-02-02 02:24:04 +00001060 case eEnumerateDirectoryResultNext:
1061 // Enumerate next entry in the current directory. We just
1062 // exit this switch and will continue enumerating the
1063 // current directory as we currently are...
1064 break;
1065
1066 case eEnumerateDirectoryResultEnter: // Recurse into the current entry if it is a directory or symlink, or next if not
1067 if (FileSpec::EnumerateDirectory (child_path,
1068 find_directories,
1069 find_files,
1070 find_other,
1071 callback,
1072 callback_baton) == eEnumerateDirectoryResultQuit)
1073 {
1074 // The subdirectory returned Quit, which means to
1075 // stop all directory enumerations at all levels.
Jim Ingham5c42d8a2013-05-15 18:27:08 +00001076 if (buf)
1077 free (buf);
Greg Clayton4272cc72011-02-02 02:24:04 +00001078 return eEnumerateDirectoryResultQuit;
1079 }
1080 break;
1081
1082 case eEnumerateDirectoryResultExit: // Exit from the current directory at the current level.
1083 // Exit from this directory level and tell parent to
1084 // keep enumerating.
Jason Molendafe806902013-05-04 00:39:52 +00001085 if (buf)
1086 free (buf);
Greg Clayton4272cc72011-02-02 02:24:04 +00001087 return eEnumerateDirectoryResultNext;
1088
1089 case eEnumerateDirectoryResultQuit: // Stop directory enumerations at any level
Jason Molendafe806902013-05-04 00:39:52 +00001090 if (buf)
1091 free (buf);
Greg Clayton4272cc72011-02-02 02:24:04 +00001092 return eEnumerateDirectoryResultQuit;
1093 }
1094 }
1095 }
1096 }
Jason Molenda14aef122013-04-04 03:19:27 +00001097 if (buf)
1098 {
1099 free (buf);
1100 }
Greg Clayton4272cc72011-02-02 02:24:04 +00001101 }
Virgile Bellob2f1fb22013-08-23 12:44:05 +00001102#endif
Greg Clayton4272cc72011-02-02 02:24:04 +00001103 }
1104 // By default when exiting a directory, we tell the parent enumeration
1105 // to continue enumerating.
1106 return eEnumerateDirectoryResultNext;
1107}
1108
Greg Clayton1f746072012-08-29 21:13:06 +00001109//------------------------------------------------------------------
1110/// Returns true if the filespec represents an implementation source
1111/// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
1112/// extension).
1113///
1114/// @return
1115/// \b true if the filespec represents an implementation source
1116/// file, \b false otherwise.
1117//------------------------------------------------------------------
1118bool
1119FileSpec::IsSourceImplementationFile () const
1120{
1121 ConstString extension (GetFileNameExtension());
1122 if (extension)
1123 {
1124 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)$",
1125 REG_EXTENDED | REG_ICASE);
1126 return g_source_file_regex.Execute (extension.GetCString());
1127 }
1128 return false;
1129}
1130
Greg Claytona0ca6602012-10-18 16:33:33 +00001131bool
1132FileSpec::IsRelativeToCurrentWorkingDirectory () const
1133{
1134 const char *directory = m_directory.GetCString();
1135 if (directory && directory[0])
1136 {
1137 // If the path doesn't start with '/' or '~', return true
1138 switch (directory[0])
1139 {
1140 case '/':
1141 case '~':
1142 return false;
1143 default:
1144 return true;
1145 }
1146 }
1147 else if (m_filename)
1148 {
1149 // No directory, just a basename, return true
1150 return true;
1151 }
1152 return false;
1153}