]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/FileSpec.h
Update LLDB snapshot to upstream r241361
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Host / FileSpec.h
1 //===-- FileSpec.h ----------------------------------------------*- 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 #ifndef liblldb_FileSpec_h_
11 #define liblldb_FileSpec_h_
12 #if defined(__cplusplus)
13
14 #include <functional>
15
16 #include "lldb/lldb-private.h"
17 #include "lldb/Core/ConstString.h"
18 #include "lldb/Core/STLUtils.h"
19 #include "lldb/Host/TimeValue.h"
20
21 namespace lldb_private {
22
23 //----------------------------------------------------------------------
24 /// @class FileSpec FileSpec.h "lldb/Host/FileSpec.h"
25 /// @brief A file utility class.
26 ///
27 /// A file specification class that divides paths up into a directory
28 /// and basename. These string values of the paths are put into uniqued
29 /// string pools for fast comparisons and efficient memory usage.
30 ///
31 /// Another reason the paths are split into the directory and basename 
32 /// is to allow efficient debugger searching. Often in a debugger the 
33 /// user types in the basename of the file, for example setting a 
34 /// breakpoint by file and line, or specifying a module (shared library)
35 /// to limit the scope in which to execute a command. The user rarely
36 /// types in a full path. When the paths are already split up, it makes
37 /// it easy for us to compare only the basenames of a lot of file 
38 /// specifications without having to split up the file path each time
39 /// to get to the basename.
40 //----------------------------------------------------------------------
41 class FileSpec
42 {
43 public:
44     typedef enum FileType
45     {
46         eFileTypeInvalid = -1,
47         eFileTypeUnknown = 0,
48         eFileTypeDirectory,
49         eFileTypePipe,
50         eFileTypeRegular,
51         eFileTypeSocket,
52         eFileTypeSymbolicLink,
53         eFileTypeOther
54     } FileType;
55
56     enum PathSyntax
57     {
58         ePathSyntaxPosix,
59         ePathSyntaxWindows,
60         ePathSyntaxHostNative
61     };
62
63     FileSpec();
64
65     //------------------------------------------------------------------
66     /// Constructor with path.
67     ///
68     /// Takes a path to a file which can be just a filename, or a full
69     /// path. If \a path is not NULL or empty, this function will call
70     /// FileSpec::SetFile (const char *path, bool resolve).
71     ///
72     /// @param[in] path
73     ///     The full or partial path to a file.
74     ///
75     /// @param[in] resolve_path
76     ///     If \b true, then we resolve the path with realpath,
77     ///     if \b false we trust the path is in canonical form already.
78     ///
79     /// @see FileSpec::SetFile (const char *path, bool resolve)
80     //------------------------------------------------------------------
81     explicit FileSpec (const char *path, bool resolve_path, PathSyntax syntax = ePathSyntaxHostNative);
82
83     explicit FileSpec (const char *path, bool resolve_path, ArchSpec arch);
84
85     explicit FileSpec(const std::string &path, bool resolve_path, PathSyntax syntax = ePathSyntaxHostNative);
86
87     explicit FileSpec(const std::string &path, bool resolve_path, ArchSpec arch);
88
89     //------------------------------------------------------------------
90     /// Copy constructor
91     ///
92     /// Makes a copy of the uniqued directory and filename strings from
93     /// \a rhs.
94     ///
95     /// @param[in] rhs
96     ///     A const FileSpec object reference to copy.
97     //------------------------------------------------------------------
98     FileSpec (const FileSpec& rhs);
99
100     //------------------------------------------------------------------
101     /// Copy constructor
102     ///
103     /// Makes a copy of the uniqued directory and filename strings from
104     /// \a rhs if it is not NULL.
105     ///
106     /// @param[in] rhs
107     ///     A const FileSpec object pointer to copy if non-NULL.
108     //------------------------------------------------------------------
109     FileSpec (const FileSpec* rhs);
110
111     //------------------------------------------------------------------
112     /// Destructor.
113     //------------------------------------------------------------------
114     ~FileSpec ();
115
116     //------------------------------------------------------------------
117     /// Assignment operator.
118     ///
119     /// Makes a copy of the uniqued directory and filename strings from
120     /// \a rhs.
121     ///
122     /// @param[in] rhs
123     ///     A const FileSpec object reference to assign to this object.
124     ///
125     /// @return
126     ///     A const reference to this object.
127     //------------------------------------------------------------------
128     const FileSpec&
129     operator= (const FileSpec& rhs);
130
131     //------------------------------------------------------------------
132     /// Equal to operator
133     ///
134     /// Tests if this object is equal to \a rhs.
135     ///
136     /// @param[in] rhs
137     ///     A const FileSpec object reference to compare this object
138     ///     to.
139     ///
140     /// @return
141     ///     \b true if this object is equal to \a rhs, \b false
142     ///     otherwise.
143     //------------------------------------------------------------------
144     bool
145     operator== (const FileSpec& rhs) const;
146
147     //------------------------------------------------------------------
148     /// Not equal to operator
149     ///
150     /// Tests if this object is not equal to \a rhs.
151     ///
152     /// @param[in] rhs
153     ///     A const FileSpec object reference to compare this object
154     ///     to.
155     ///
156     /// @return
157     ///     \b true if this object is equal to \a rhs, \b false
158     ///     otherwise.
159     //------------------------------------------------------------------
160     bool
161     operator!= (const FileSpec& rhs) const;
162
163     //------------------------------------------------------------------
164     /// Less than to operator
165     ///
166     /// Tests if this object is less than \a rhs.
167     ///
168     /// @param[in] rhs
169     ///     A const FileSpec object reference to compare this object
170     ///     to.
171     ///
172     /// @return
173     ///     \b true if this object is less than \a rhs, \b false
174     ///     otherwise.
175     //------------------------------------------------------------------
176     bool
177     operator< (const FileSpec& rhs) const;
178
179     //------------------------------------------------------------------
180     /// Convert to pointer operator.
181     ///
182     /// This allows code to check a FileSpec object to see if it
183     /// contains anything valid using code such as:
184     ///
185     /// @code
186     /// FileSpec file_spec(...);
187     /// if (file_spec)
188     /// { ...
189     /// @endcode
190     ///
191     /// @return
192     ///     A pointer to this object if either the directory or filename
193     ///     is valid, NULL otherwise.
194     //------------------------------------------------------------------
195     explicit operator bool() const;
196
197     //------------------------------------------------------------------
198     /// Logical NOT operator.
199     ///
200     /// This allows code to check a FileSpec object to see if it is
201     /// invalid using code such as:
202     ///
203     /// @code
204     /// FileSpec file_spec(...);
205     /// if (!file_spec)
206     /// { ...
207     /// @endcode
208     ///
209     /// @return
210     ///     Returns \b true if the object has an empty directory and
211     ///     filename, \b false otherwise.
212     //------------------------------------------------------------------
213     bool
214     operator! () const;
215
216     //------------------------------------------------------------------
217     /// Clears the object state.
218     ///
219     /// Clear this object by releasing both the directory and filename
220     /// string values and reverting them to empty strings.
221     //------------------------------------------------------------------
222     void
223     Clear ();
224
225     //------------------------------------------------------------------
226     /// Compare two FileSpec objects.
227     ///
228     /// If \a full is true, then both the directory and the filename
229     /// must match. If \a full is false, then the directory names for
230     /// \a lhs and \a rhs are only compared if they are both not empty.
231     /// This allows a FileSpec object to only contain a filename
232     /// and it can match FileSpec objects that have matching
233     /// filenames with different paths.
234     ///
235     /// @param[in] lhs
236     ///     A const reference to the Left Hand Side object to compare.
237     ///
238     /// @param[in] rhs
239     ///     A const reference to the Right Hand Side object to compare.
240     ///
241     /// @param[in] full
242     ///     If true, then both the directory and filenames will have to
243     ///     match for a compare to return zero (equal to). If false
244     ///     and either directory from \a lhs or \a rhs is empty, then
245     ///     only the filename will be compared, else a full comparison
246     ///     is done.
247     ///
248     /// @return
249     ///     @li -1 if \a lhs is less than \a rhs
250     ///     @li 0 if \a lhs is equal to \a rhs
251     ///     @li 1 if \a lhs is greater than \a rhs
252     //------------------------------------------------------------------
253     static int
254     Compare (const FileSpec& lhs, const FileSpec& rhs, bool full);
255
256     static bool
257     Equal (const FileSpec& a, const FileSpec& b, bool full, bool remove_backups = false);
258
259     //------------------------------------------------------------------
260     /// Dump this object to a Stream.
261     ///
262     /// Dump the object to the supplied stream \a s. If the object
263     /// contains a valid directory name, it will be displayed followed
264     /// by a directory delimiter, and the filename.
265     ///
266     /// @param[in] s
267     ///     The stream to which to dump the object description.
268     //------------------------------------------------------------------
269     void
270     Dump(Stream *s) const;
271
272     //------------------------------------------------------------------
273     /// Existence test.
274     ///
275     /// @return
276     ///     \b true if the file exists on disk, \b false otherwise.
277     //------------------------------------------------------------------
278     bool
279     Exists () const;
280
281     //------------------------------------------------------------------
282     /// Check if a file is readable by the current user
283     ///
284     /// @return
285     ///     \b true if the file exists on disk and is readable, \b false
286     ///     otherwise.
287     //------------------------------------------------------------------
288     bool
289     Readable () const;
290      
291     //------------------------------------------------------------------
292     /// Expanded existence test.
293     ///
294     /// Call into the Host to see if it can help find the file (e.g. by
295     /// searching paths set in the environment, etc.).
296     ///
297     /// If found, sets the value of m_directory to the directory where 
298     /// the file was found.
299     ///
300     /// @return
301     ///     \b true if was able to find the file using expanded search 
302     ///     methods, \b false otherwise.
303     //------------------------------------------------------------------
304     bool
305     ResolveExecutableLocation ();
306     
307     //------------------------------------------------------------------
308     /// Canonicalize this file path (basically running the static 
309     /// FileSpec::Resolve method on it). Useful if you asked us not to 
310     /// resolve the file path when you set the file.
311     //------------------------------------------------------------------
312     bool
313     ResolvePath ();
314
315     uint64_t
316     GetByteSize() const;
317
318     PathSyntax
319     GetPathSyntax() const;
320
321     //------------------------------------------------------------------
322     /// Directory string get accessor.
323     ///
324     /// @return
325     ///     A reference to the directory string object.
326     //------------------------------------------------------------------
327     ConstString &
328     GetDirectory ();
329
330     //------------------------------------------------------------------
331     /// Directory string const get accessor.
332     ///
333     /// @return
334     ///     A const reference to the directory string object.
335     //------------------------------------------------------------------
336     const ConstString &
337     GetDirectory () const;
338
339     //------------------------------------------------------------------
340     /// Filename string get accessor.
341     ///
342     /// @return
343     ///     A reference to the filename string object.
344     //------------------------------------------------------------------
345     ConstString &
346     GetFilename ();
347
348     //------------------------------------------------------------------
349     /// Filename string const get accessor.
350     ///
351     /// @return
352     ///     A const reference to the filename string object.
353     //------------------------------------------------------------------
354     const ConstString &
355     GetFilename () const;
356
357     //------------------------------------------------------------------
358     /// Returns true if the filespec represents an implementation source
359     /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
360     /// extension).
361     ///
362     /// @return
363     ///     \b true if the filespec represents an implementation source
364     ///     file, \b false otherwise.
365     //------------------------------------------------------------------
366     bool
367     IsSourceImplementationFile () const;
368
369     //------------------------------------------------------------------
370     /// Returns true if the filespec represents a relative path.
371     ///
372     /// @return
373     ///     \b true if the filespec represents a relative path,
374     ///     \b false otherwise.
375     //------------------------------------------------------------------
376     bool
377     IsRelative() const;
378
379     //------------------------------------------------------------------
380     /// Returns true if the filespec represents an absolute path.
381     ///
382     /// @return
383     ///     \b true if the filespec represents an absolute path,
384     ///     \b false otherwise.
385     //------------------------------------------------------------------
386     bool
387     IsAbsolute() const;
388
389     TimeValue
390     GetModificationTime () const;
391
392     //------------------------------------------------------------------
393     /// Extract the full path to the file.
394     ///
395     /// Extract the directory and path into a fixed buffer. This is
396     /// needed as the directory and path are stored in separate string
397     /// values.
398     ///
399     /// @param[out] path
400     ///     The buffer in which to place the extracted full path.
401     ///
402     /// @param[in] max_path_length
403     ///     The maximum length of \a path.
404     ///
405     /// @return
406     ///     Returns the number of characters that would be needed to 
407     ///     properly copy the full path into \a path. If the returned
408     ///     number is less than \a max_path_length, then the path is
409     ///     properly copied and terminated. If the return value is 
410     ///     >= \a max_path_length, then the path was truncated (but is
411     ///     still NULL terminated).
412     //------------------------------------------------------------------
413     size_t
414     GetPath (char *path, size_t max_path_length, bool denormalize = true) const;
415
416     //------------------------------------------------------------------
417     /// Extract the full path to the file.
418     ///
419     /// Extract the directory and path into a std::string, which is returned.
420     ///
421     /// @return
422     ///     Returns a std::string with the directory and filename 
423     ///     concatenated.
424     //------------------------------------------------------------------
425     std::string
426     GetPath (bool denormalize = true) const;
427
428     const char *
429     GetCString(bool denormalize = true) const;
430
431     //------------------------------------------------------------------
432     /// Extract the full path to the file.
433     ///
434     /// Extract the directory and path into an llvm::SmallVectorImpl<>
435     ///
436     /// @return
437     ///     Returns a std::string with the directory and filename
438     ///     concatenated.
439     //------------------------------------------------------------------
440     void GetPath(llvm::SmallVectorImpl<char> &path, bool denormalize = true) const;
441
442     //------------------------------------------------------------------
443     /// Extract the extension of the file.
444     ///
445     /// Returns a ConstString that represents the extension of the filename
446     /// for this FileSpec object. If this object does not represent a file,
447     /// or the filename has no extension, ConstString(NULL) is returned.
448     /// The dot ('.') character is not returned as part of the extension
449     ///
450     /// @return
451     ///     Returns the extension of the file as a ConstString object.
452     //------------------------------------------------------------------
453     ConstString
454     GetFileNameExtension () const;
455
456     //------------------------------------------------------------------
457     /// Return the filename without the extension part
458     ///
459     /// Returns a ConstString that represents the filename of this object
460     /// without the extension part (e.g. for a file named "foo.bar", "foo"
461     /// is returned)
462     ///
463     /// @return
464     ///     Returns the filename without extension
465     ///     as a ConstString object.
466     //------------------------------------------------------------------
467     ConstString
468     GetFileNameStrippingExtension () const;
469     
470     FileType
471     GetFileType () const;
472
473     //------------------------------------------------------------------
474     /// Return the current permissions of the path.
475     ///
476     /// Returns a bitmask for the current permissions of the file
477     /// ( zero or more of the permission bits defined in
478     /// File::Permissions).
479     ///
480     /// @return
481     ///     Zero if the file doesn't exist or we are unable to get
482     ///     information for the file, otherwise one or more permission
483     ///     bits from the File::Permissions enumeration.
484     //------------------------------------------------------------------
485     uint32_t
486     GetPermissions () const;
487     
488     bool
489     IsDirectory () const
490     {
491         return GetFileType() == FileSpec::eFileTypeDirectory;
492     }
493
494     bool
495     IsPipe () const
496     {
497         return GetFileType() == FileSpec::eFileTypePipe;
498     }
499
500     bool
501     IsRegularFile () const
502     {
503         return GetFileType() == FileSpec::eFileTypeRegular;
504     }
505
506     bool
507     IsSocket () const
508     {
509         return GetFileType() == FileSpec::eFileTypeSocket;
510     }
511
512     bool
513     IsSymbolicLink () const
514     {
515         return GetFileType() == FileSpec::eFileTypeSymbolicLink;
516     }
517
518     //------------------------------------------------------------------
519     /// Get the memory cost of this object.
520     ///
521     /// Return the size in bytes that this object takes in memory. This
522     /// returns the size in bytes of this object, not any shared string
523     /// values it may refer to.
524     ///
525     /// @return
526     ///     The number of bytes that this object occupies in memory.
527     ///
528     /// @see ConstString::StaticMemorySize ()
529     //------------------------------------------------------------------
530     size_t
531     MemorySize () const;
532
533     //------------------------------------------------------------------
534     /// Memory map part of, or the entire contents of, a file.
535     ///
536     /// Returns a shared pointer to a data buffer that contains all or
537     /// part of the contents of a file. The data is memory mapped and
538     /// will lazily page in data from the file as memory is accessed.
539     /// The data that is mapped will start \a offset bytes into the
540     /// file, and \a length bytes will be mapped. If \a length is
541     /// greater than the number of bytes available in the file starting
542     /// at \a offset, the number of bytes will be appropriately
543     /// truncated. The final number of bytes that get mapped can be
544     /// verified using the DataBuffer::GetByteSize() function on the return
545     /// shared data pointer object contents.
546     ///
547     /// @param[in] offset
548     ///     The offset in bytes from the beginning of the file where
549     ///     memory mapping should begin.
550     ///
551     /// @param[in] length
552     ///     The size in bytes that should be mapped starting \a offset
553     ///     bytes into the file. If \a length is \c SIZE_MAX, map
554     ///     as many bytes as possible.
555     ///
556     /// @return
557     ///     A shared pointer to the memory mapped data. This shared
558     ///     pointer can contain a NULL DataBuffer pointer, so the contained
559     ///     pointer must be checked prior to using it.
560     //------------------------------------------------------------------
561     lldb::DataBufferSP
562     MemoryMapFileContents (off_t offset = 0, size_t length = SIZE_MAX) const;
563
564
565     //------------------------------------------------------------------
566     /// Memory map part of, or the entire contents of, a file only if
567     /// the file is local (not on a network mount).
568     ///
569     /// Returns a shared pointer to a data buffer that contains all or
570     /// part of the contents of a file. The data will be memory mapped
571     /// if the file is local and will lazily page in data from the file
572     /// as memory is accessed. If the data is memory mapped, the data
573     /// that is mapped will start \a offset bytes into the file, and
574     /// \a length bytes will be mapped. If \a length is
575     /// greater than the number of bytes available in the file starting
576     /// at \a offset, the number of bytes will be appropriately
577     /// truncated. The final number of bytes that get mapped can be
578     /// verified using the DataBuffer::GetByteSize() function on the return
579     /// shared data pointer object contents.
580     ///
581     /// If the file is on a network mount the data will be read into a
582     /// heap buffer immediately so that accesses to the data won't later
583     /// cause a crash if we touch a page that isn't paged in and the
584     /// network mount has been disconnected or gone away.
585     ///
586     /// @param[in] offset
587     ///     The offset in bytes from the beginning of the file where
588     ///     memory mapping should begin.
589     ///
590     /// @param[in] length
591     ///     The size in bytes that should be mapped starting \a offset
592     ///     bytes into the file. If \a length is \c SIZE_MAX, map
593     ///     as many bytes as possible.
594     ///
595     /// @return
596     ///     A shared pointer to the memory mapped data. This shared
597     ///     pointer can contain a NULL DataBuffer pointer, so the contained
598     ///     pointer must be checked prior to using it.
599     //------------------------------------------------------------------
600     lldb::DataBufferSP
601     MemoryMapFileContentsIfLocal(off_t file_offset, size_t file_size) const;
602
603     //------------------------------------------------------------------
604     /// Read part of, or the entire contents of, a file into a heap based data buffer.
605     ///
606     /// Returns a shared pointer to a data buffer that contains all or
607     /// part of the contents of a file. The data copies into a heap based
608     /// buffer that lives in the DataBuffer shared pointer object returned.
609     /// The data that is cached will start \a offset bytes into the
610     /// file, and \a length bytes will be mapped. If \a length is
611     /// greater than the number of bytes available in the file starting
612     /// at \a offset, the number of bytes will be appropriately
613     /// truncated. The final number of bytes that get mapped can be
614     /// verified using the DataBuffer::GetByteSize() function.
615     ///
616     /// @param[in] offset
617     ///     The offset in bytes from the beginning of the file where
618     ///     memory mapping should begin.
619     ///
620     /// @param[in] length
621     ///     The size in bytes that should be mapped starting \a offset
622     ///     bytes into the file. If \a length is \c SIZE_MAX, map
623     ///     as many bytes as possible.
624     ///
625     /// @return
626     ///     A shared pointer to the memory mapped data. This shared
627     ///     pointer can contain a NULL DataBuffer pointer, so the contained
628     ///     pointer must be checked prior to using it.
629     //------------------------------------------------------------------
630     lldb::DataBufferSP
631     ReadFileContents (off_t offset = 0, size_t length = SIZE_MAX, Error *error_ptr = NULL) const;
632
633     size_t
634     ReadFileContents (off_t file_offset, void *dst, size_t dst_len, Error *error_ptr) const;
635
636     
637     //------------------------------------------------------------------
638     /// Read the entire contents of a file as data that can be used
639     /// as a C string.
640     ///
641     /// Read the entire contents of a file and ensure that the data
642     /// is NULL terminated so it can be used as a C string.
643     ///
644     /// @return
645     ///     A shared pointer to the data. This shared pointer can
646     ///     contain a NULL DataBuffer pointer, so the contained pointer
647     ///     must be checked prior to using it.
648     //------------------------------------------------------------------
649     lldb::DataBufferSP
650     ReadFileContentsAsCString(Error *error_ptr = NULL);
651
652     //------------------------------------------------------------------
653     /// Normalize a pathname by collapsing redundant separators and
654     /// up-level references.
655     //------------------------------------------------------------------
656     void
657     NormalizePath ();
658
659     //------------------------------------------------------------------
660     /// Run through the input string, replaying the effect of any ".." and produce
661     /// the resultant path.  The input path is not required to be in the host file system
662     /// format, but it is required to be normalized to that system.
663     ///
664     /// @param[in] input
665     ///     The input path to analyze.
666     ///
667     /// @param[out] result
668     ///     The backup-resolved path will be written here.
669     //------------------------------------------------------------------
670     static void RemoveBackupDots (const ConstString &input_const_str, ConstString &result_const_str);
671
672     //------------------------------------------------------------------
673     /// Change the file specified with a new path.
674     ///
675     /// Update the contents of this object with a new path. The path will
676     /// be split up into a directory and filename and stored as uniqued
677     /// string values for quick comparison and efficient memory usage.
678     ///
679     /// @param[in] path
680     ///     A full, partial, or relative path to a file.
681     ///
682     /// @param[in] resolve_path
683     ///     If \b true, then we will try to resolve links the path using
684     ///     the static FileSpec::Resolve.
685     //------------------------------------------------------------------
686     void
687     SetFile (const char *path, bool resolve_path, PathSyntax syntax = ePathSyntaxHostNative);
688
689     void
690     SetFile(const char *path, bool resolve_path, ArchSpec arch);
691
692     void
693     SetFile(const std::string &path, bool resolve_path, PathSyntax syntax = ePathSyntaxHostNative);
694
695     void
696     SetFile(const std::string &path, bool resolve_path, ArchSpec arch);
697
698     bool
699     IsResolved () const
700     {
701         return m_is_resolved;
702     }
703
704     //------------------------------------------------------------------
705     /// Set if the file path has been resolved or not.
706     ///
707     /// If you know a file path is already resolved and avoided passing
708     /// a \b true parameter for any functions that take a "bool 
709     /// resolve_path" parameter, you can set the value manually using
710     /// this call to make sure we don't try and resolve it later, or try
711     /// and resolve a path that has already been resolved.
712     ///
713     /// @param[in] is_resolved
714     ///     A boolean value that will replace the current value that
715     ///     indicates if the paths in this object have been resolved.
716     //------------------------------------------------------------------
717     void
718     SetIsResolved (bool is_resolved)
719     {
720         m_is_resolved = is_resolved;
721     }
722     //------------------------------------------------------------------
723     /// Read the file into an array of strings, one per line.
724     ///
725     /// Opens and reads the file in this object into an array of strings,
726     /// one string per line of the file. Returns a boolean indicating
727     /// success or failure.
728     ///
729     /// @param[out] lines
730     ///     The string array into which to read the file.
731     ///
732     /// @result
733     ///     Returns the number of lines that were read from the file.
734     //------------------------------------------------------------------
735     size_t
736     ReadFileLines (STLStringArray &lines);
737
738     //------------------------------------------------------------------
739     /// Resolves user name and links in \a path, and overwrites the input
740     /// argument with the resolved path.
741     ///
742     /// @param[in] path
743     ///     Input path to be resolved, in the form of a llvm::SmallString or similar.
744     //------------------------------------------------------------------
745     static void
746     Resolve (llvm::SmallVectorImpl<char> &path);
747
748     FileSpec
749     CopyByAppendingPathComponent (const char *new_path) const;
750     
751     FileSpec
752     CopyByRemovingLastPathComponent () const;
753
754     void
755     PrependPathComponent(const char *new_path);
756
757     void
758     PrependPathComponent(const std::string &new_path);
759
760     void
761     PrependPathComponent(const FileSpec &new_path);
762
763     void
764     AppendPathComponent(const char *new_path);
765
766     void
767     AppendPathComponent(const std::string &new_path);
768
769     void
770     AppendPathComponent(const FileSpec &new_path);
771
772     void
773     RemoveLastPathComponent ();
774     
775     ConstString
776     GetLastPathComponent () const;
777     
778     //------------------------------------------------------------------
779     /// Resolves the user name at the beginning of \a src_path, and writes the output
780     /// to \a dst_path.  Note, \a src_path can contain other path components after the
781     /// user name, they will be copied over, and if the path doesn't start with "~" it
782     /// will also be copied over to \a dst_path.
783     ///
784     /// @param[in] src_path
785     ///     Input path to be resolved.
786     ///
787     /// @param[in] dst_path
788     ///     Buffer to store the resolved path.
789     //------------------------------------------------------------------
790     static void
791     ResolveUsername (llvm::SmallVectorImpl<char> &path);
792     
793     static size_t
794     ResolvePartialUsername (const char *partial_name, StringList &matches);
795
796     enum EnumerateDirectoryResult
797     {
798         eEnumerateDirectoryResultNext,  // Enumerate next entry in the current directory
799         eEnumerateDirectoryResultEnter, // Recurse into the current entry if it is a directory or symlink, or next if not
800         eEnumerateDirectoryResultExit,  // Exit from the current directory at the current level.
801         eEnumerateDirectoryResultQuit   // Stop directory enumerations at any level
802     };
803
804     typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType) (void *baton,
805                                                                         FileType file_type,
806                                                                         const FileSpec &spec);
807
808     static EnumerateDirectoryResult
809     EnumerateDirectory (const char *dir_path,
810                         bool find_directories,
811                         bool find_files,
812                         bool find_other,
813                         EnumerateDirectoryCallbackType callback,
814                         void *callback_baton);
815
816     typedef std::function <EnumerateDirectoryResult(FileType file_type, const FileSpec &spec)> DirectoryCallback;
817
818     static EnumerateDirectoryResult
819     ForEachItemInDirectory (const char *dir_path, DirectoryCallback const &callback);
820
821 protected:
822     //------------------------------------------------------------------
823     // Member variables
824     //------------------------------------------------------------------
825     ConstString m_directory;    ///< The uniqued directory path
826     ConstString m_filename;     ///< The uniqued filename path
827     mutable bool m_is_resolved; ///< True if this path has been resolved.
828     PathSyntax m_syntax;        ///< The syntax that this path uses (e.g. Windows / Posix)
829 };
830
831 //----------------------------------------------------------------------
832 /// Dump a FileSpec object to a stream
833 //----------------------------------------------------------------------
834 Stream& operator << (Stream& s, const FileSpec& f);
835
836 } // namespace lldb_private
837
838 #endif  // #if defined(__cplusplus)
839 #endif  // liblldb_FileSpec_h_