1 //===-- FileSpec.h ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_FileSpec_h_
11 #define liblldb_FileSpec_h_
12 #if defined(__cplusplus)
14 #include "lldb/lldb-private.h"
15 #include "lldb/Core/ConstString.h"
16 #include "lldb/Core/STLUtils.h"
17 #include "lldb/Host/TimeValue.h"
19 namespace lldb_private {
21 //----------------------------------------------------------------------
22 /// @class FileSpec FileSpec.h "lldb/Host/FileSpec.h"
23 /// @brief A file utility class.
25 /// A file specification class that divides paths up into a directory
26 /// and basename. These string values of the paths are put into uniqued
27 /// string pools for fast comparisons and efficient memory usage.
29 /// Another reason the paths are split into the directory and basename
30 /// is to allow efficient debugger searching. Often in a debugger the
31 /// user types in the basename of the file, for example setting a
32 /// breakpoint by file and line, or specifying a module (shared library)
33 /// to limit the scope in which to execute a command. The user rarely
34 /// types in a full path. When the paths are already split up, it makes
35 /// it easy for us to compare only the basenames of a lot of file
36 /// specifications without having to split up the file path each time
37 /// to get to the basename.
38 //----------------------------------------------------------------------
44 eFileTypeInvalid = -1,
50 eFileTypeSymbolicLink,
56 //------------------------------------------------------------------
57 /// Constructor with path.
59 /// Takes a path to a file which can be just a filename, or a full
60 /// path. If \a path is not NULL or empty, this function will call
61 /// FileSpec::SetFile (const char *path, bool resolve).
64 /// The full or partial path to a file.
66 /// @param[in] resolve_path
67 /// If \b true, then we resolve the path with realpath,
68 /// if \b false we trust the path is in canonical form already.
70 /// @see FileSpec::SetFile (const char *path, bool resolve)
71 //------------------------------------------------------------------
72 explicit FileSpec (const char *path, bool resolve_path);
74 //------------------------------------------------------------------
77 /// Makes a copy of the uniqued directory and filename strings from
81 /// A const FileSpec object reference to copy.
82 //------------------------------------------------------------------
83 FileSpec (const FileSpec& rhs);
85 //------------------------------------------------------------------
88 /// Makes a copy of the uniqued directory and filename strings from
89 /// \a rhs if it is not NULL.
92 /// A const FileSpec object pointer to copy if non-NULL.
93 //------------------------------------------------------------------
94 FileSpec (const FileSpec* rhs);
96 //------------------------------------------------------------------
98 //------------------------------------------------------------------
101 //------------------------------------------------------------------
102 /// Assignment operator.
104 /// Makes a copy of the uniqued directory and filename strings from
108 /// A const FileSpec object reference to assign to this object.
111 /// A const reference to this object.
112 //------------------------------------------------------------------
114 operator= (const FileSpec& rhs);
116 //------------------------------------------------------------------
117 /// Equal to operator
119 /// Tests if this object is equal to \a rhs.
122 /// A const FileSpec object reference to compare this object
126 /// \b true if this object is equal to \a rhs, \b false
128 //------------------------------------------------------------------
130 operator== (const FileSpec& rhs) const;
132 //------------------------------------------------------------------
133 /// Not equal to operator
135 /// Tests if this object is not equal to \a rhs.
138 /// A const FileSpec object reference to compare this object
142 /// \b true if this object is equal to \a rhs, \b false
144 //------------------------------------------------------------------
146 operator!= (const FileSpec& rhs) const;
148 //------------------------------------------------------------------
149 /// Less than to operator
151 /// Tests if this object is less than \a rhs.
154 /// A const FileSpec object reference to compare this object
158 /// \b true if this object is less than \a rhs, \b false
160 //------------------------------------------------------------------
162 operator< (const FileSpec& rhs) const;
164 //------------------------------------------------------------------
165 /// Convert to pointer operator.
167 /// This allows code to check a FileSpec object to see if it
168 /// contains anything valid using code such as:
171 /// FileSpec file_spec(...);
177 /// A pointer to this object if either the directory or filename
178 /// is valid, NULL otherwise.
179 //------------------------------------------------------------------
180 operator bool() const;
182 //------------------------------------------------------------------
183 /// Logical NOT operator.
185 /// This allows code to check a FileSpec object to see if it is
186 /// invalid using code such as:
189 /// FileSpec file_spec(...);
195 /// Returns \b true if the object has an empty directory and
196 /// filename, \b false otherwise.
197 //------------------------------------------------------------------
201 //------------------------------------------------------------------
202 /// Clears the object state.
204 /// Clear this object by releasing both the directory and filename
205 /// string values and reverting them to empty strings.
206 //------------------------------------------------------------------
210 //------------------------------------------------------------------
211 /// Compare two FileSpec objects.
213 /// If \a full is true, then both the directory and the filename
214 /// must match. If \a full is false, then the directory names for
215 /// \a lhs and \a rhs are only compared if they are both not empty.
216 /// This allows a FileSpec object to only contain a filename
217 /// and it can match FileSpec objects that have matching
218 /// filenames with different paths.
221 /// A const reference to the Left Hand Side object to compare.
224 /// A const reference to the Right Hand Side object to compare.
227 /// If true, then both the directory and filenames will have to
228 /// match for a compare to return zero (equal to). If false
229 /// and either directory from \a lhs or \a rhs is empty, then
230 /// only the filename will be compared, else a full comparison
234 /// @li -1 if \a lhs is less than \a rhs
235 /// @li 0 if \a lhs is equal to \a rhs
236 /// @li 1 if \a lhs is greater than \a rhs
237 //------------------------------------------------------------------
239 Compare (const FileSpec& lhs, const FileSpec& rhs, bool full);
242 Equal (const FileSpec& a, const FileSpec& b, bool full);
244 //------------------------------------------------------------------
245 /// Dump this object to a Stream.
247 /// Dump the object to the supplied stream \a s. If the object
248 /// contains a valid directory name, it will be displayed followed
249 /// by a directory delimiter, and the filename.
252 /// The stream to which to dump the object descripton.
253 //------------------------------------------------------------------
255 Dump (Stream *s) const;
257 //------------------------------------------------------------------
261 /// \b true if the file exists on disk, \b false otherwise.
262 //------------------------------------------------------------------
267 //------------------------------------------------------------------
268 /// Expanded existence test.
270 /// Call into the Host to see if it can help find the file (e.g. by
271 /// searching paths set in the environment, etc.).
273 /// If found, sets the value of m_directory to the directory where
274 /// the file was found.
277 /// \b true if was able to find the file using expanded search
278 /// methods, \b false otherwise.
279 //------------------------------------------------------------------
281 ResolveExecutableLocation ();
283 //------------------------------------------------------------------
284 /// Canonicalize this file path (basically running the static
285 /// FileSpec::Resolve method on it). Useful if you asked us not to
286 /// resolve the file path when you set the file.
287 //------------------------------------------------------------------
294 //------------------------------------------------------------------
295 /// Directory string get accessor.
298 /// A reference to the directory string object.
299 //------------------------------------------------------------------
303 //------------------------------------------------------------------
304 /// Directory string const get accessor.
307 /// A const reference to the directory string object.
308 //------------------------------------------------------------------
310 GetDirectory () const;
312 //------------------------------------------------------------------
313 /// Filename string get accessor.
316 /// A reference to the filename string object.
317 //------------------------------------------------------------------
321 //------------------------------------------------------------------
322 /// Filename string const get accessor.
325 /// A const reference to the filename string object.
326 //------------------------------------------------------------------
328 GetFilename () const;
330 //------------------------------------------------------------------
331 /// Returns true if the filespec represents an implementation source
332 /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
336 /// \b true if the filespec represents an implementation source
337 /// file, \b false otherwise.
338 //------------------------------------------------------------------
340 IsSourceImplementationFile () const;
342 //------------------------------------------------------------------
343 /// Returns true if the filespec represents path that is relative
344 /// path to the current working directory.
347 /// \b true if the filespec represents a current working
348 /// directory relative path, \b false otherwise.
349 //------------------------------------------------------------------
351 IsRelativeToCurrentWorkingDirectory () const;
354 GetModificationTime () const;
356 //------------------------------------------------------------------
357 /// Extract the full path to the file.
359 /// Extract the directory and path into a fixed buffer. This is
360 /// needed as the directory and path are stored in separate string
364 /// The buffer in which to place the extracted full path.
366 /// @param[in] max_path_length
367 /// The maximum length of \a path.
370 /// Returns the number of characters that would be needed to
371 /// properly copy the full path into \a path. If the returned
372 /// number is less than \a max_path_length, then the path is
373 /// properly copied and terminated. If the return value is
374 /// >= \a max_path_length, then the path was truncated (but is
375 /// still NULL terminated).
376 //------------------------------------------------------------------
378 GetPath (char *path, size_t max_path_length) const;
380 //------------------------------------------------------------------
381 /// Extract the full path to the file.
383 /// Extract the directory and path into a std::string, which is returned.
386 /// Returns a std::string with the directory and filename
388 //------------------------------------------------------------------
392 //------------------------------------------------------------------
393 /// Extract the extension of the file.
395 /// Returns a ConstString that represents the extension of the filename
396 /// for this FileSpec object. If this object does not represent a file,
397 /// or the filename has no extension, ConstString(NULL) is returned.
398 /// The dot ('.') character is not returned as part of the extension
401 /// Returns the extension of the file as a ConstString object.
402 //------------------------------------------------------------------
404 GetFileNameExtension () const;
406 //------------------------------------------------------------------
407 /// Return the filename without the extension part
409 /// Returns a ConstString that represents the filename of this object
410 /// without the extension part (e.g. for a file named "foo.bar", "foo"
414 /// Returns the filename without extension
415 /// as a ConstString object.
416 //------------------------------------------------------------------
418 GetFileNameStrippingExtension () const;
421 GetFileType () const;
426 return GetFileType() == FileSpec::eFileTypeDirectory;
432 return GetFileType() == FileSpec::eFileTypePipe;
436 IsRegularFile () const
438 return GetFileType() == FileSpec::eFileTypeRegular;
444 return GetFileType() == FileSpec::eFileTypeSocket;
448 IsSymbolicLink () const
450 return GetFileType() == FileSpec::eFileTypeSymbolicLink;
453 //------------------------------------------------------------------
454 /// Get the memory cost of this object.
456 /// Return the size in bytes that this object takes in memory. This
457 /// returns the size in bytes of this object, not any shared string
458 /// values it may refer to.
461 /// The number of bytes that this object occupies in memory.
463 /// @see ConstString::StaticMemorySize ()
464 //------------------------------------------------------------------
468 //------------------------------------------------------------------
469 /// Memory map part of, or the entire contents of, a file.
471 /// Returns a shared pointer to a data buffer that contains all or
472 /// part of the contents of a file. The data is memory mapped and
473 /// will lazily page in data from the file as memory is accessed.
474 /// The data that is mappped will start \a offset bytes into the
475 /// file, and \a length bytes will be mapped. If \a length is
476 /// greater than the number of bytes available in the file starting
477 /// at \a offset, the number of bytes will be appropriately
478 /// truncated. The final number of bytes that get mapped can be
479 /// verified using the DataBuffer::GetByteSize() function on the return
480 /// shared data pointer object contents.
482 /// @param[in] offset
483 /// The offset in bytes from the beginning of the file where
484 /// memory mapping should begin.
486 /// @param[in] length
487 /// The size in bytes that should be mapped starting \a offset
488 /// bytes into the file. If \a length is \c SIZE_MAX, map
489 /// as many bytes as possible.
492 /// A shared pointer to the memeory mapped data. This shared
493 /// pointer can contain a NULL DataBuffer pointer, so the contained
494 /// pointer must be checked prior to using it.
495 //------------------------------------------------------------------
497 MemoryMapFileContents (off_t offset = 0, size_t length = SIZE_MAX) const;
499 //------------------------------------------------------------------
500 /// Read part of, or the entire contents of, a file into a heap based data buffer.
502 /// Returns a shared pointer to a data buffer that contains all or
503 /// part of the contents of a file. The data copies into a heap based
504 /// buffer that lives in the DataBuffer shared pointer object returned.
505 /// The data that is cached will start \a offset bytes into the
506 /// file, and \a length bytes will be mapped. If \a length is
507 /// greater than the number of bytes available in the file starting
508 /// at \a offset, the number of bytes will be appropriately
509 /// truncated. The final number of bytes that get mapped can be
510 /// verified using the DataBuffer::GetByteSize() function.
512 /// @param[in] offset
513 /// The offset in bytes from the beginning of the file where
514 /// memory mapping should begin.
516 /// @param[in] length
517 /// The size in bytes that should be mapped starting \a offset
518 /// bytes into the file. If \a length is \c SIZE_MAX, map
519 /// as many bytes as possible.
522 /// A shared pointer to the memeory mapped data. This shared
523 /// pointer can contain a NULL DataBuffer pointer, so the contained
524 /// pointer must be checked prior to using it.
525 //------------------------------------------------------------------
527 ReadFileContents (off_t offset = 0, size_t length = SIZE_MAX, Error *error_ptr = NULL) const;
530 ReadFileContents (off_t file_offset, void *dst, size_t dst_len, Error *error_ptr) const;
533 //------------------------------------------------------------------
534 /// Read the entire contents of a file as data that can be used
537 /// Read the entire contents of a file and ensure that the data
538 /// is NULL terminated so it can be used as a C string.
541 /// A shared pointer to the data. This shared pointer can
542 /// contain a NULL DataBuffer pointer, so the contained pointer
543 /// must be checked prior to using it.
544 //------------------------------------------------------------------
546 ReadFileContentsAsCString(Error *error_ptr = NULL);
547 //------------------------------------------------------------------
548 /// Change the file specificed with a new path.
550 /// Update the contents of this object with a new path. The path will
551 /// be split up into a directory and filename and stored as uniqued
552 /// string values for quick comparison and efficient memory usage.
555 /// A full, partial, or relative path to a file.
557 /// @param[in] resolve_path
558 /// If \b true, then we will try to resolve links the path using
559 /// the static FileSpec::Resolve.
560 //------------------------------------------------------------------
562 SetFile (const char *path, bool resolve_path);
567 return m_is_resolved;
570 //------------------------------------------------------------------
571 /// Set if the file path has been resolved or not.
573 /// If you know a file path is already resolved and avoided passing
574 /// a \b true parameter for any functions that take a "bool
575 /// resolve_path" parameter, you can set the value manually using
576 /// this call to make sure we don't try and resolve it later, or try
577 /// and resolve a path that has already been resolved.
579 /// @param[in] is_resolved
580 /// A boolean value that will replace the current value that
581 /// indicates if the paths in this object have been resolved.
582 //------------------------------------------------------------------
584 SetIsResolved (bool is_resolved)
586 m_is_resolved = is_resolved;
588 //------------------------------------------------------------------
589 /// Read the file into an array of strings, one per line.
591 /// Opens and reads the file in this object into an array of strings,
592 /// one string per line of the file. Returns a boolean indicating
593 /// success or failure.
595 /// @param[out] lines
596 /// The string array into which to read the file.
599 /// Returns the number of lines that were read from the file.
600 //------------------------------------------------------------------
602 ReadFileLines (STLStringArray &lines);
604 //------------------------------------------------------------------
605 /// Resolves user name and links in \a src_path, and writes the output
606 /// to \a dst_path. Note if the path pointed to by \a src_path does not
607 /// exist, the contents of \a src_path will be copied to \a dst_path
610 /// @param[in] src_path
611 /// Input path to be resolved.
613 /// @param[in] dst_path
614 /// Buffer to store the resolved path.
616 /// @param[in] dst_len
617 /// Size of the buffer pointed to by dst_path.
620 /// The number of characters required to write the resolved path. If the
621 /// resolved path doesn't fit in dst_len, dst_len-1 characters will
622 /// be written to \a dst_path, but the actual required length will still be returned.
623 //------------------------------------------------------------------
625 Resolve (const char *src_path, char *dst_path, size_t dst_len);
627 //------------------------------------------------------------------
628 /// Resolves the user name at the beginning of \a src_path, and writes the output
629 /// to \a dst_path. Note, \a src_path can contain other path components after the
630 /// user name, they will be copied over, and if the path doesn't start with "~" it
631 /// will also be copied over to \a dst_path.
633 /// @param[in] src_path
634 /// Input path to be resolved.
636 /// @param[in] dst_path
637 /// Buffer to store the resolved path.
639 /// @param[in] dst_len
640 /// Size of the buffer pointed to by dst_path.
643 /// The number of characters required to write the resolved path, or 0 if
644 /// the user name could not be found. If the
645 /// resolved path doesn't fit in dst_len, dst_len-1 characters will
646 /// be written to \a dst_path, but the actual required length will still be returned.
647 //------------------------------------------------------------------
649 ResolveUsername (const char *src_path, char *dst_path, size_t dst_len);
652 ResolvePartialUsername (const char *partial_name, StringList &matches);
654 enum EnumerateDirectoryResult
656 eEnumerateDirectoryResultNext, // Enumerate next entry in the current directory
657 eEnumerateDirectoryResultEnter, // Recurse into the current entry if it is a directory or symlink, or next if not
658 eEnumerateDirectoryResultExit, // Exit from the current directory at the current level.
659 eEnumerateDirectoryResultQuit // Stop directory enumerations at any level
662 typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType) (void *baton,
667 static EnumerateDirectoryResult
668 EnumerateDirectory (const char *dir_path,
669 bool find_directories,
672 EnumerateDirectoryCallbackType callback,
673 void *callback_baton);
676 //------------------------------------------------------------------
678 //------------------------------------------------------------------
679 ConstString m_directory; ///< The uniqued directory path
680 ConstString m_filename; ///< The uniqued filename path
681 mutable bool m_is_resolved; ///< True if this path has been resolved.
684 //----------------------------------------------------------------------
685 /// Dump a FileSpec object to a stream
686 //----------------------------------------------------------------------
687 Stream& operator << (Stream& s, const FileSpec& f);
689 } // namespace lldb_private
691 #endif // #if defined(__cplusplus)
692 #endif // liblldb_FileSpec_h_