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 explicit 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;
423 //------------------------------------------------------------------
424 /// Return the current permissions of the path.
426 /// Returns a bitmask for the current permissions of the file
427 /// ( zero or more of the permission bits defined in
428 /// File::Permissions).
431 /// Zero if the file doesn't exist or we are unable to get
432 /// information for the file, otherwise one or more permission
433 /// bits from the File::Permissions enumeration.
434 //------------------------------------------------------------------
436 GetPermissions () const;
441 return GetFileType() == FileSpec::eFileTypeDirectory;
447 return GetFileType() == FileSpec::eFileTypePipe;
451 IsRegularFile () const
453 return GetFileType() == FileSpec::eFileTypeRegular;
459 return GetFileType() == FileSpec::eFileTypeSocket;
463 IsSymbolicLink () const
465 return GetFileType() == FileSpec::eFileTypeSymbolicLink;
468 //------------------------------------------------------------------
469 /// Get the memory cost of this object.
471 /// Return the size in bytes that this object takes in memory. This
472 /// returns the size in bytes of this object, not any shared string
473 /// values it may refer to.
476 /// The number of bytes that this object occupies in memory.
478 /// @see ConstString::StaticMemorySize ()
479 //------------------------------------------------------------------
483 //------------------------------------------------------------------
484 /// Memory map part of, or the entire contents of, a file.
486 /// Returns a shared pointer to a data buffer that contains all or
487 /// part of the contents of a file. The data is memory mapped and
488 /// will lazily page in data from the file as memory is accessed.
489 /// The data that is mappped will start \a offset bytes into the
490 /// file, and \a length bytes will be mapped. If \a length is
491 /// greater than the number of bytes available in the file starting
492 /// at \a offset, the number of bytes will be appropriately
493 /// truncated. The final number of bytes that get mapped can be
494 /// verified using the DataBuffer::GetByteSize() function on the return
495 /// shared data pointer object contents.
497 /// @param[in] offset
498 /// The offset in bytes from the beginning of the file where
499 /// memory mapping should begin.
501 /// @param[in] length
502 /// The size in bytes that should be mapped starting \a offset
503 /// bytes into the file. If \a length is \c SIZE_MAX, map
504 /// as many bytes as possible.
507 /// A shared pointer to the memeory mapped data. This shared
508 /// pointer can contain a NULL DataBuffer pointer, so the contained
509 /// pointer must be checked prior to using it.
510 //------------------------------------------------------------------
512 MemoryMapFileContents (off_t offset = 0, size_t length = SIZE_MAX) const;
514 //------------------------------------------------------------------
515 /// Read part of, or the entire contents of, a file into a heap based data buffer.
517 /// Returns a shared pointer to a data buffer that contains all or
518 /// part of the contents of a file. The data copies into a heap based
519 /// buffer that lives in the DataBuffer shared pointer object returned.
520 /// The data that is cached will start \a offset bytes into the
521 /// file, and \a length bytes will be mapped. If \a length is
522 /// greater than the number of bytes available in the file starting
523 /// at \a offset, the number of bytes will be appropriately
524 /// truncated. The final number of bytes that get mapped can be
525 /// verified using the DataBuffer::GetByteSize() function.
527 /// @param[in] offset
528 /// The offset in bytes from the beginning of the file where
529 /// memory mapping should begin.
531 /// @param[in] length
532 /// The size in bytes that should be mapped starting \a offset
533 /// bytes into the file. If \a length is \c SIZE_MAX, map
534 /// as many bytes as possible.
537 /// A shared pointer to the memory mapped data. This shared
538 /// pointer can contain a NULL DataBuffer pointer, so the contained
539 /// pointer must be checked prior to using it.
540 //------------------------------------------------------------------
542 ReadFileContents (off_t offset = 0, size_t length = SIZE_MAX, Error *error_ptr = NULL) const;
545 ReadFileContents (off_t file_offset, void *dst, size_t dst_len, Error *error_ptr) const;
548 //------------------------------------------------------------------
549 /// Read the entire contents of a file as data that can be used
552 /// Read the entire contents of a file and ensure that the data
553 /// is NULL terminated so it can be used as a C string.
556 /// A shared pointer to the data. This shared pointer can
557 /// contain a NULL DataBuffer pointer, so the contained pointer
558 /// must be checked prior to using it.
559 //------------------------------------------------------------------
561 ReadFileContentsAsCString(Error *error_ptr = NULL);
562 //------------------------------------------------------------------
563 /// Change the file specificed with a new path.
565 /// Update the contents of this object with a new path. The path will
566 /// be split up into a directory and filename and stored as uniqued
567 /// string values for quick comparison and efficient memory usage.
570 /// A full, partial, or relative path to a file.
572 /// @param[in] resolve_path
573 /// If \b true, then we will try to resolve links the path using
574 /// the static FileSpec::Resolve.
575 //------------------------------------------------------------------
577 SetFile (const char *path, bool resolve_path);
582 return m_is_resolved;
585 //------------------------------------------------------------------
586 /// Set if the file path has been resolved or not.
588 /// If you know a file path is already resolved and avoided passing
589 /// a \b true parameter for any functions that take a "bool
590 /// resolve_path" parameter, you can set the value manually using
591 /// this call to make sure we don't try and resolve it later, or try
592 /// and resolve a path that has already been resolved.
594 /// @param[in] is_resolved
595 /// A boolean value that will replace the current value that
596 /// indicates if the paths in this object have been resolved.
597 //------------------------------------------------------------------
599 SetIsResolved (bool is_resolved)
601 m_is_resolved = is_resolved;
603 //------------------------------------------------------------------
604 /// Read the file into an array of strings, one per line.
606 /// Opens and reads the file in this object into an array of strings,
607 /// one string per line of the file. Returns a boolean indicating
608 /// success or failure.
610 /// @param[out] lines
611 /// The string array into which to read the file.
614 /// Returns the number of lines that were read from the file.
615 //------------------------------------------------------------------
617 ReadFileLines (STLStringArray &lines);
619 //------------------------------------------------------------------
620 /// Resolves user name and links in \a src_path, and writes the output
621 /// to \a dst_path. Note if the path pointed to by \a src_path does not
622 /// exist, the contents of \a src_path will be copied to \a dst_path
625 /// @param[in] src_path
626 /// Input path to be resolved.
628 /// @param[in] dst_path
629 /// Buffer to store the resolved path.
631 /// @param[in] dst_len
632 /// Size of the buffer pointed to by dst_path.
635 /// The number of characters required to write the resolved path. If the
636 /// resolved path doesn't fit in dst_len, dst_len-1 characters will
637 /// be written to \a dst_path, but the actual required length will still be returned.
638 //------------------------------------------------------------------
640 Resolve (const char *src_path, char *dst_path, size_t dst_len);
643 CopyByAppendingPathComponent (const char *new_path) const;
646 CopyByRemovingLastPathComponent () const;
649 AppendPathComponent (const char *new_path);
652 RemoveLastPathComponent ();
655 GetLastPathComponent () const;
657 //------------------------------------------------------------------
658 /// Resolves the user name at the beginning of \a src_path, and writes the output
659 /// to \a dst_path. Note, \a src_path can contain other path components after the
660 /// user name, they will be copied over, and if the path doesn't start with "~" it
661 /// will also be copied over to \a dst_path.
663 /// @param[in] src_path
664 /// Input path to be resolved.
666 /// @param[in] dst_path
667 /// Buffer to store the resolved path.
669 /// @param[in] dst_len
670 /// Size of the buffer pointed to by dst_path.
673 /// The number of characters required to write the resolved path, or 0 if
674 /// the user name could not be found. If the
675 /// resolved path doesn't fit in dst_len, dst_len-1 characters will
676 /// be written to \a dst_path, but the actual required length will still be returned.
677 //------------------------------------------------------------------
679 ResolveUsername (const char *src_path, char *dst_path, size_t dst_len);
682 ResolvePartialUsername (const char *partial_name, StringList &matches);
684 enum EnumerateDirectoryResult
686 eEnumerateDirectoryResultNext, // Enumerate next entry in the current directory
687 eEnumerateDirectoryResultEnter, // Recurse into the current entry if it is a directory or symlink, or next if not
688 eEnumerateDirectoryResultExit, // Exit from the current directory at the current level.
689 eEnumerateDirectoryResultQuit // Stop directory enumerations at any level
692 typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType) (void *baton,
697 static EnumerateDirectoryResult
698 EnumerateDirectory (const char *dir_path,
699 bool find_directories,
702 EnumerateDirectoryCallbackType callback,
703 void *callback_baton);
706 //------------------------------------------------------------------
708 //------------------------------------------------------------------
709 ConstString m_directory; ///< The uniqued directory path
710 ConstString m_filename; ///< The uniqued filename path
711 mutable bool m_is_resolved; ///< True if this path has been resolved.
714 //----------------------------------------------------------------------
715 /// Dump a FileSpec object to a stream
716 //----------------------------------------------------------------------
717 Stream& operator << (Stream& s, const FileSpec& f);
719 } // namespace lldb_private
721 #endif // #if defined(__cplusplus)
722 #endif // liblldb_FileSpec_h_