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)
16 #include "lldb/lldb-private.h"
17 #include "lldb/Core/ConstString.h"
18 #include "lldb/Core/STLUtils.h"
19 #include "lldb/Host/TimeValue.h"
21 namespace lldb_private {
23 //----------------------------------------------------------------------
24 /// @class FileSpec FileSpec.h "lldb/Host/FileSpec.h"
25 /// @brief A file utility class.
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.
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 //----------------------------------------------------------------------
46 eFileTypeInvalid = -1,
52 eFileTypeSymbolicLink,
65 //------------------------------------------------------------------
66 /// Constructor with path.
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).
73 /// The full or partial path to a file.
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.
79 /// @see FileSpec::SetFile (const char *path, bool resolve)
80 //------------------------------------------------------------------
81 explicit FileSpec (const char *path, bool resolve_path, PathSyntax syntax = ePathSyntaxHostNative);
83 explicit FileSpec (const char *path, bool resolve_path, ArchSpec arch);
85 explicit FileSpec(const std::string &path, bool resolve_path, PathSyntax syntax = ePathSyntaxHostNative);
87 explicit FileSpec(const std::string &path, bool resolve_path, ArchSpec arch);
89 //------------------------------------------------------------------
92 /// Makes a copy of the uniqued directory and filename strings from
96 /// A const FileSpec object reference to copy.
97 //------------------------------------------------------------------
98 FileSpec (const FileSpec& rhs);
100 //------------------------------------------------------------------
103 /// Makes a copy of the uniqued directory and filename strings from
104 /// \a rhs if it is not NULL.
107 /// A const FileSpec object pointer to copy if non-NULL.
108 //------------------------------------------------------------------
109 FileSpec (const FileSpec* rhs);
111 //------------------------------------------------------------------
113 //------------------------------------------------------------------
116 //------------------------------------------------------------------
117 /// Assignment operator.
119 /// Makes a copy of the uniqued directory and filename strings from
123 /// A const FileSpec object reference to assign to this object.
126 /// A const reference to this object.
127 //------------------------------------------------------------------
129 operator= (const FileSpec& rhs);
131 //------------------------------------------------------------------
132 /// Equal to operator
134 /// Tests if this object is equal to \a rhs.
137 /// A const FileSpec object reference to compare this object
141 /// \b true if this object is equal to \a rhs, \b false
143 //------------------------------------------------------------------
145 operator== (const FileSpec& rhs) const;
147 //------------------------------------------------------------------
148 /// Not equal to operator
150 /// Tests if this object is not equal to \a rhs.
153 /// A const FileSpec object reference to compare this object
157 /// \b true if this object is equal to \a rhs, \b false
159 //------------------------------------------------------------------
161 operator!= (const FileSpec& rhs) const;
163 //------------------------------------------------------------------
164 /// Less than to operator
166 /// Tests if this object is less than \a rhs.
169 /// A const FileSpec object reference to compare this object
173 /// \b true if this object is less than \a rhs, \b false
175 //------------------------------------------------------------------
177 operator< (const FileSpec& rhs) const;
179 //------------------------------------------------------------------
180 /// Convert to pointer operator.
182 /// This allows code to check a FileSpec object to see if it
183 /// contains anything valid using code such as:
186 /// FileSpec file_spec(...);
192 /// A pointer to this object if either the directory or filename
193 /// is valid, NULL otherwise.
194 //------------------------------------------------------------------
195 explicit operator bool() const;
197 //------------------------------------------------------------------
198 /// Logical NOT operator.
200 /// This allows code to check a FileSpec object to see if it is
201 /// invalid using code such as:
204 /// FileSpec file_spec(...);
210 /// Returns \b true if the object has an empty directory and
211 /// filename, \b false otherwise.
212 //------------------------------------------------------------------
216 //------------------------------------------------------------------
217 /// Clears the object state.
219 /// Clear this object by releasing both the directory and filename
220 /// string values and reverting them to empty strings.
221 //------------------------------------------------------------------
225 //------------------------------------------------------------------
226 /// Compare two FileSpec objects.
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.
236 /// A const reference to the Left Hand Side object to compare.
239 /// A const reference to the Right Hand Side object to compare.
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
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 //------------------------------------------------------------------
254 Compare (const FileSpec& lhs, const FileSpec& rhs, bool full);
257 Equal (const FileSpec& a, const FileSpec& b, bool full, bool remove_backups = false);
259 //------------------------------------------------------------------
260 /// Dump this object to a Stream.
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.
267 /// The stream to which to dump the object description.
268 //------------------------------------------------------------------
270 Dump(Stream *s) const;
272 //------------------------------------------------------------------
276 /// \b true if the file exists on disk, \b false otherwise.
277 //------------------------------------------------------------------
281 //------------------------------------------------------------------
282 /// Check if a file is readable by the current user
285 /// \b true if the file exists on disk and is readable, \b false
287 //------------------------------------------------------------------
291 //------------------------------------------------------------------
292 /// Expanded existence test.
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.).
297 /// If found, sets the value of m_directory to the directory where
298 /// the file was found.
301 /// \b true if was able to find the file using expanded search
302 /// methods, \b false otherwise.
303 //------------------------------------------------------------------
305 ResolveExecutableLocation ();
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 //------------------------------------------------------------------
319 GetPathSyntax() const;
321 //------------------------------------------------------------------
322 /// Directory string get accessor.
325 /// A reference to the directory string object.
326 //------------------------------------------------------------------
330 //------------------------------------------------------------------
331 /// Directory string const get accessor.
334 /// A const reference to the directory string object.
335 //------------------------------------------------------------------
337 GetDirectory () const;
339 //------------------------------------------------------------------
340 /// Filename string get accessor.
343 /// A reference to the filename string object.
344 //------------------------------------------------------------------
348 //------------------------------------------------------------------
349 /// Filename string const get accessor.
352 /// A const reference to the filename string object.
353 //------------------------------------------------------------------
355 GetFilename () const;
357 //------------------------------------------------------------------
358 /// Returns true if the filespec represents an implementation source
359 /// file (files with a ".c", ".cpp", ".m", ".mm" (many more)
363 /// \b true if the filespec represents an implementation source
364 /// file, \b false otherwise.
365 //------------------------------------------------------------------
367 IsSourceImplementationFile () const;
369 //------------------------------------------------------------------
370 /// Returns true if the filespec represents a relative path.
373 /// \b true if the filespec represents a relative path,
374 /// \b false otherwise.
375 //------------------------------------------------------------------
379 //------------------------------------------------------------------
380 /// Returns true if the filespec represents an absolute path.
383 /// \b true if the filespec represents an absolute path,
384 /// \b false otherwise.
385 //------------------------------------------------------------------
390 GetModificationTime () const;
392 //------------------------------------------------------------------
393 /// Extract the full path to the file.
395 /// Extract the directory and path into a fixed buffer. This is
396 /// needed as the directory and path are stored in separate string
400 /// The buffer in which to place the extracted full path.
402 /// @param[in] max_path_length
403 /// The maximum length of \a path.
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 //------------------------------------------------------------------
414 GetPath (char *path, size_t max_path_length, bool denormalize = true) const;
416 //------------------------------------------------------------------
417 /// Extract the full path to the file.
419 /// Extract the directory and path into a std::string, which is returned.
422 /// Returns a std::string with the directory and filename
424 //------------------------------------------------------------------
426 GetPath (bool denormalize = true) const;
429 GetCString(bool denormalize = true) const;
431 //------------------------------------------------------------------
432 /// Extract the full path to the file.
434 /// Extract the directory and path into an llvm::SmallVectorImpl<>
437 /// Returns a std::string with the directory and filename
439 //------------------------------------------------------------------
440 void GetPath(llvm::SmallVectorImpl<char> &path, bool denormalize = true) const;
442 //------------------------------------------------------------------
443 /// Extract the extension of the file.
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
451 /// Returns the extension of the file as a ConstString object.
452 //------------------------------------------------------------------
454 GetFileNameExtension () const;
456 //------------------------------------------------------------------
457 /// Return the filename without the extension part
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"
464 /// Returns the filename without extension
465 /// as a ConstString object.
466 //------------------------------------------------------------------
468 GetFileNameStrippingExtension () const;
471 GetFileType () const;
473 //------------------------------------------------------------------
474 /// Return the current permissions of the path.
476 /// Returns a bitmask for the current permissions of the file
477 /// ( zero or more of the permission bits defined in
478 /// File::Permissions).
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 //------------------------------------------------------------------
486 GetPermissions () const;
491 return GetFileType() == FileSpec::eFileTypeDirectory;
497 return GetFileType() == FileSpec::eFileTypePipe;
501 IsRegularFile () const
503 return GetFileType() == FileSpec::eFileTypeRegular;
509 return GetFileType() == FileSpec::eFileTypeSocket;
513 IsSymbolicLink () const
515 return GetFileType() == FileSpec::eFileTypeSymbolicLink;
518 //------------------------------------------------------------------
519 /// Get the memory cost of this object.
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.
526 /// The number of bytes that this object occupies in memory.
528 /// @see ConstString::StaticMemorySize ()
529 //------------------------------------------------------------------
533 //------------------------------------------------------------------
534 /// Memory map part of, or the entire contents of, a file.
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.
547 /// @param[in] offset
548 /// The offset in bytes from the beginning of the file where
549 /// memory mapping should begin.
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.
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 //------------------------------------------------------------------
562 MemoryMapFileContents (off_t offset = 0, size_t length = SIZE_MAX) const;
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).
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.
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.
586 /// @param[in] offset
587 /// The offset in bytes from the beginning of the file where
588 /// memory mapping should begin.
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.
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 //------------------------------------------------------------------
601 MemoryMapFileContentsIfLocal(off_t file_offset, size_t file_size) const;
603 //------------------------------------------------------------------
604 /// Read part of, or the entire contents of, a file into a heap based data buffer.
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.
616 /// @param[in] offset
617 /// The offset in bytes from the beginning of the file where
618 /// memory mapping should begin.
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.
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 //------------------------------------------------------------------
631 ReadFileContents (off_t offset = 0, size_t length = SIZE_MAX, Error *error_ptr = NULL) const;
634 ReadFileContents (off_t file_offset, void *dst, size_t dst_len, Error *error_ptr) const;
637 //------------------------------------------------------------------
638 /// Read the entire contents of a file as data that can be used
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.
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 //------------------------------------------------------------------
650 ReadFileContentsAsCString(Error *error_ptr = NULL);
652 //------------------------------------------------------------------
653 /// Normalize a pathname by collapsing redundant separators and
654 /// up-level references.
655 //------------------------------------------------------------------
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.
665 /// The input path to analyze.
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);
672 //------------------------------------------------------------------
673 /// Change the file specified with a new path.
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.
680 /// A full, partial, or relative path to a file.
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 //------------------------------------------------------------------
687 SetFile (const char *path, bool resolve_path, PathSyntax syntax = ePathSyntaxHostNative);
690 SetFile(const char *path, bool resolve_path, ArchSpec arch);
693 SetFile(const std::string &path, bool resolve_path, PathSyntax syntax = ePathSyntaxHostNative);
696 SetFile(const std::string &path, bool resolve_path, ArchSpec arch);
701 return m_is_resolved;
704 //------------------------------------------------------------------
705 /// Set if the file path has been resolved or not.
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.
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 //------------------------------------------------------------------
718 SetIsResolved (bool is_resolved)
720 m_is_resolved = is_resolved;
722 //------------------------------------------------------------------
723 /// Read the file into an array of strings, one per line.
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.
729 /// @param[out] lines
730 /// The string array into which to read the file.
733 /// Returns the number of lines that were read from the file.
734 //------------------------------------------------------------------
736 ReadFileLines (STLStringArray &lines);
738 //------------------------------------------------------------------
739 /// Resolves user name and links in \a path, and overwrites the input
740 /// argument with the resolved path.
743 /// Input path to be resolved, in the form of a llvm::SmallString or similar.
744 //------------------------------------------------------------------
746 Resolve (llvm::SmallVectorImpl<char> &path);
749 CopyByAppendingPathComponent (const char *new_path) const;
752 CopyByRemovingLastPathComponent () const;
755 PrependPathComponent(const char *new_path);
758 PrependPathComponent(const std::string &new_path);
761 PrependPathComponent(const FileSpec &new_path);
764 AppendPathComponent(const char *new_path);
767 AppendPathComponent(const std::string &new_path);
770 AppendPathComponent(const FileSpec &new_path);
773 RemoveLastPathComponent ();
776 GetLastPathComponent () const;
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.
784 /// @param[in] src_path
785 /// Input path to be resolved.
787 /// @param[in] dst_path
788 /// Buffer to store the resolved path.
789 //------------------------------------------------------------------
791 ResolveUsername (llvm::SmallVectorImpl<char> &path);
794 ResolvePartialUsername (const char *partial_name, StringList &matches);
796 enum EnumerateDirectoryResult
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
804 typedef EnumerateDirectoryResult (*EnumerateDirectoryCallbackType) (void *baton,
806 const FileSpec &spec);
808 static EnumerateDirectoryResult
809 EnumerateDirectory (const char *dir_path,
810 bool find_directories,
813 EnumerateDirectoryCallbackType callback,
814 void *callback_baton);
816 typedef std::function <EnumerateDirectoryResult(FileType file_type, const FileSpec &spec)> DirectoryCallback;
818 static EnumerateDirectoryResult
819 ForEachItemInDirectory (const char *dir_path, DirectoryCallback const &callback);
822 //------------------------------------------------------------------
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)
831 //----------------------------------------------------------------------
832 /// Dump a FileSpec object to a stream
833 //----------------------------------------------------------------------
834 Stream& operator << (Stream& s, const FileSpec& f);
836 } // namespace lldb_private
838 #endif // #if defined(__cplusplus)
839 #endif // liblldb_FileSpec_h_