1 //===- llvm/System/Path.h - Path Operating System Concept -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the llvm::sys::Path class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SYSTEM_PATH_H
15 #define LLVM_SYSTEM_PATH_H
17 #include "llvm/System/TimeValue.h"
26 /// This class provides an abstraction for the path to a file or directory
27 /// in the operating system's filesystem and provides various basic operations
28 /// on it. Note that this class only represents the name of a path to a file
29 /// or directory which may or may not be valid for a given machine's file
30 /// system. A Path ensures that the name it encapsulates is syntactical valid
31 /// for the operating system it is running on but does not ensure correctness
32 /// for any particular file system. A Path either references a file or a
33 /// directory and the distinction is consistently maintained. Most operations
34 /// on the class have invariants that require the Path object to be either a
35 /// file path or a directory path, but not both. Those operations will also
36 /// leave the object as either a file path or object path. There is exactly
37 /// one invalid Path which is the empty path. The class should never allow any
38 /// other syntactically invalid non-empty path name to be assigned. Empty
39 /// paths are required in order to indicate an error result. If the path is
40 /// empty, the isValid operation will return false. All operations will fail
41 /// if isValid is false. Operations that change the path will either return
42 /// false if it would cause a syntactically invalid path name (in which case
43 /// the Path object is left unchanged) or throw an std::string exception
44 /// indicating the error.
46 /// @brief An abstraction for operating system paths.
51 /// This structure provides basic file system information about a file. It
52 /// is patterned after the stat(2) Unix operating system call but made
53 /// platform independent and eliminates many of the unix-specific fields.
54 /// However, to support llvm-ar, the mode, user, and group fields are
55 /// retained. These pertain to unix security and may not have a meaningful
56 /// value on non-Unix platforms. However, the fileSize and modTime fields
57 /// should always be applicabe on all platforms. The structure is
58 /// filled in by the getStatusInfo method.
59 /// @brief File status structure
61 StatusInfo() : fileSize(0), modTime(0,0), mode(0777), user(999),
62 group(999), isDir(false) { }
63 size_t fileSize; ///< Size of the file in bytes
64 TimeValue modTime; ///< Time of file's modification
65 uint32_t mode; ///< Mode of the file, if applicable
66 uint32_t user; ///< User ID of owner, if applicable
67 uint32_t group; ///< Group ID of owner, if applicable
68 bool isDir; ///< True if this is a directory.
72 /// @name Constructors
75 /// Construct a path to the root directory of the file system. The root
76 /// directory is a top level directory above which there are no more
77 /// directories. For example, on UNIX, the root directory is /. On Windows
78 /// it is C:\. Other operating systems may have different notions of
79 /// what the root directory is.
81 static Path GetRootDirectory();
83 /// Construct a path to a unique temporary directory that is created in
84 /// a "standard" place for the operating system. The directory is
85 /// guaranteed to be created on exit from this function. If the directory
86 /// cannot be created, the function will throw an exception.
87 /// @throws std::string indicating why the directory could not be created.
88 /// @brief Constrct a path to an new, unique, existing temporary
90 static Path GetTemporaryDirectory();
92 /// Construct a vector of sys::Path that contains the "standard" system
93 /// library paths suitable for linking into programs. This function *must*
94 /// return the value of LLVM_LIB_SEARCH_PATH as the first item in \p Paths
95 /// if that environment variable is set and it references a directory.
97 /// @brief Construct a path to the first system library directory
98 static void GetSystemLibraryPaths(std::vector<sys::Path>& Paths);
100 /// Construct a vector of sys::Path that contains the "standard" bytecode
101 /// library paths suitable for linking into an llvm program. This function
102 /// *must* return the value of LLVM_LIB_SEARCH_PATH as well as the value
103 /// of LLVM_LIBDIR. It also must provide the System library paths as
104 /// returned by GetSystemLibraryPaths.
105 /// @see GetSystemLibraryPaths
106 /// @brief Construct a list of directories in which bytecode could be
108 static void GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths);
110 /// Find the path to a library using its short name. Use the system
111 /// dependent library paths to locate the library.
112 /// @brief Find a library.
113 static Path FindLibrary(std::string& short_name);
115 /// Construct a path to the default LLVM configuration directory. The
116 /// implementation must ensure that this is a well-known (same on many
117 /// systems) directory in which llvm configuration files exist. For
118 /// example, on Unix, the /etc/llvm directory has been selected.
120 /// @brief Construct a path to the default LLVM configuration directory
121 static Path GetLLVMDefaultConfigDir();
123 /// Construct a path to the LLVM installed configuration directory. The
124 /// implementation must ensure that this refers to the "etc" directory of
125 /// the LLVM installation. This is the location where configuration files
126 /// will be located for a particular installation of LLVM on a machine.
128 /// @brief Construct a path to the LLVM installed configuration directory
129 static Path GetLLVMConfigDir();
131 /// Construct a path to the current user's home directory. The
132 /// implementation must use an operating system specific mechanism for
133 /// determining the user's home directory. For example, the environment
134 /// variable "HOME" could be used on Unix. If a given operating system
135 /// does not have the concept of a user's home directory, this static
136 /// constructor must provide the same result as GetRootDirectory.
138 /// @brief Construct a path to the current user's "home" directory
139 static Path GetUserHomeDirectory();
141 /// Return the suffix commonly used on file names that contain a shared
142 /// object, shared archive, or dynamic link library. Such files are
143 /// linked at runtime into a process and their code images are shared
144 /// between processes.
145 /// @returns The dynamic link library suffix for the current platform.
146 /// @brief Return the dynamic link library suffix.
147 static std::string GetDLLSuffix();
149 /// This is one of the very few ways in which a path can be constructed
150 /// with a syntactically invalid name. The only *legal* invalid name is an
151 /// empty one. Other invalid names are not permitted. Empty paths are
152 /// provided so that they can be used to indicate null or error results in
153 /// other lib/System functionality.
155 /// @brief Construct an empty (and invalid) path.
158 /// This constructor will accept a std::string as a path but if verifies
159 /// that the path string has a legal syntax for the operating system on
160 /// which it is running. This allows a path to be taken in from outside
161 /// the program. However, if the path is not valid, the Path object will
162 /// be set to an empty string and an exception will be thrown.
163 /// @throws std::string if the path string is not legal.
164 /// @param unverified_path The path to verify and assign.
165 /// @brief Construct a Path from a string.
166 explicit Path(const std::string& unverified_path);
172 /// Makes a copy of \p that to \p this.
175 /// @brief Assignment Operator
176 Path & operator = ( const Path & that ) {
181 /// Compares \p this Path with \p that Path for equality.
182 /// @returns true if \p this and \p that refer to the same thing.
184 /// @brief Equality Operator
185 bool operator == (const Path& that) const {
186 return 0 == path.compare(that.path) ;
189 /// Compares \p this Path with \p that Path for inequality.
190 /// @returns true if \p this and \p that refer to different things.
192 /// @brief Inequality Operator
193 bool operator !=( const Path & that ) const {
194 return 0 != path.compare( that.path );
197 /// Determines if \p this Path is less than \p that Path. This is required
198 /// so that Path objects can be placed into ordered collections (e.g.
199 /// std::map). The comparison is done lexicographically as defined by
200 /// the std::string::compare method.
201 /// @returns true if \p this path is lexicographically less than \p that.
203 /// @brief Less Than Operator
204 bool operator< (const Path& that) const {
205 return 0 > path.compare( that.path );
212 /// This function will use an operating system specific algorithm to
213 /// determine if the current value of \p this is a syntactically valid
214 /// path name for the operating system. The path name does not need to
215 /// exist, validity is simply syntactical. Empty paths are always invalid.
216 /// @returns true iff the path name is syntactically legal for the
217 /// host operating system.
218 /// @brief Determine if a path is syntactically valid or not.
219 bool isValid() const;
221 /// This function determines if the contents of the path name are
222 /// empty. That is, the path has a zero length.
223 /// @returns true iff the path is empty.
224 /// @brief Determines if the path name is empty (invalid).
225 bool isEmpty() const { return path.empty(); }
227 /// This function determines if the path name in this object is intended
228 /// to reference a legal file name (as opposed to a directory name). This
229 /// function does not verify anything with the file system, it merely
230 /// determines if the syntax of the path represents a file name or not.
231 /// @returns true if this path name references a file.
232 /// @brief Determines if the path name references a file.
235 /// This function determines if the path name in this object is intended
236 /// to reference a legal directory name (as opposed to a file name). This
237 /// function does not verify anything with the file system, it merely
238 /// determines if the syntax of the path represents a directory name or
240 /// @returns true if the path name references a directory
241 /// @brief Determines if the path name references a directory.
242 bool isDirectory() const;
244 /// This function determines if the path name in this object references
245 /// the root (top level directory) of the file system. The details of what
246 /// is considered the "root" may vary from system to system so this method
247 /// will do the necessary checking.
248 /// @returns true iff the path name references the root directory.
249 /// @brief Determines if the path references the root directory.
250 bool isRootDirectory() const;
252 /// This function opens the file associated with the path name provided by
253 /// the Path object and reads its magic number. If the magic number at the
254 /// start of the file matches \p magic, true is returned. In all other
255 /// cases (file not found, file not accessible, etc.) it returns false.
256 /// @returns true if the magic number of the file matches \p magic.
257 /// @brief Determine if file has a specific magic number
258 bool hasMagicNumber(const std::string& magic) const;
260 /// This function retrieves the first \p len bytes of the file associated
261 /// with \p this. These bytes are returned as the "magic number" in the
262 /// \p Magic parameter.
263 /// @returns true if the Path is a file and the magic number is retrieved,
265 /// @brief Get the file's magic number.
266 bool getMagicNumber(std::string& Magic, unsigned len) const;
268 /// This function determines if the path name in the object references an
269 /// archive file by looking at its magic number.
270 /// @returns true if the file starts with the magic number for an archive
272 /// @brief Determine if the path references an archive file.
273 bool isArchive() const;
275 /// This function determines if the path name in the object references an
276 /// LLVM Bytecode file by looking at its magic number.
277 /// @returns true if the file starts with the magic number for LLVM
279 /// @brief Determine if the path references a bytecode file.
280 bool isBytecodeFile() const;
282 /// This function determines if the path name in the object references a
283 /// native Dynamic Library (shared library, shared object) by looking at
284 /// the file's magic number. The Path object must reference a file, not a
286 /// @return strue if the file starts with the magid number for a native
288 /// @brief Determine if the path reference a dynamic library.
289 bool isDynamicLibrary() const;
291 /// This function determines if the path name references an existing file
292 /// or directory in the file system. Unlike isFile and isDirectory, this
293 /// function actually checks for the existence of the file or directory.
294 /// @returns true if the pathname references an existing file.
295 /// @brief Determines if the path is a file or directory in
299 /// This function determines if the path name references a readable file
300 /// or directory in the file system. Unlike isFile and isDirectory, this
301 /// function actually checks for the existence and readability (by the
302 /// current program) of the file or directory.
303 /// @returns true if the pathname references a readable file.
304 /// @brief Determines if the path is a readable file or directory
305 /// in the file system.
306 bool readable() const;
308 /// This function determines if the path name references a writable file
309 /// or directory in the file system. Unlike isFile and isDirectory, this
310 /// function actually checks for the existence and writability (by the
311 /// current program) of the file or directory.
312 /// @returns true if the pathname references a writable file.
313 /// @brief Determines if the path is a writable file or directory
314 /// in the file system.
315 bool writable() const;
317 /// This function determines if the path name references an executable
318 /// file in the file system. Unlike isFile and isDirectory, this
319 /// function actually checks for the existence and executability (by
320 /// the current program) of the file.
321 /// @returns true if the pathname references an executable file.
322 /// @brief Determines if the path is an executable file in the file
324 bool executable() const;
326 /// This function returns the current contents of the path as a
327 /// std::string. This allows the underlying path string to be manipulated
328 /// by other software.
329 /// @returns std::string containing the path name.
330 /// @brief Returns the path as a std::string.
331 const std::string& toString() const { return path; }
333 /// This function returns the last component of the path name. If the
334 /// isDirectory() function would return true then this returns the name
335 /// of the last directory in the path. If the isFile() function would
336 /// return true then this function returns the name of the file without
337 /// any of the preceding directories.
338 /// @returns std::string containing the last component of the path name.
339 /// @brief Returns the last component of the path name.
340 std::string getLast() const;
342 /// This function strips off the path and suffix of the file name and
343 /// returns just the basename.
344 /// @returns std::string containing the basename of the path
346 /// @brief Get the base name of the path
347 std::string getBasename() const;
349 /// This function builds a list of paths that are the names of the
350 /// files and directories in a directory.
351 /// @returns false if \p this is not a directory, true otherwise
352 /// @throws std::string if the directory cannot be searched
353 /// @brief Build a list of directory's contents.
354 bool getDirectoryContents(std::set<Path>& paths) const;
356 /// This method attempts to destroy the directory named by the last in
357 /// the Path name. If \p remove_contents is false, an attempt will be
358 /// made to remove just the directory that this Path object refers to
359 /// (the final Path component). If \p remove_contents is true, an attempt
360 /// will be made to remove the entire contents of the directory,
362 /// @param destroy_contents Indicates whether the contents of a destroyed
363 /// directory should also be destroyed (recursively).
364 /// @returns false if the Path does not refer to a directory, true
366 /// @throws std::string if there is an error.
367 /// @brief Removes the file or directory from the filesystem.
368 bool destroyDirectory( bool destroy_contents = false ) const;
370 /// This method attempts to destroy the file named by the last item in the
372 /// @returns false if the Path does not refer to a file, true otherwise.
373 /// @throws std::string if there is an error.
374 /// @brief Destroy the file this Path refers to.
375 bool destroyFile() const;
377 /// Obtain a 'C' string for the path name.
378 /// @returns a 'C' string containing the path name.
379 /// @brief Returns the path as a C string.
380 const char* const c_str() const { return path.c_str(); }
386 /// The path name is cleared and becomes empty. This is an invalid
387 /// path name but is the *only* invalid path name. This is provided
388 /// so that path objects can be used to indicate the lack of a
389 /// valid path being found.
390 void clear() { path.clear(); }
392 /// This function returns status information about the file. The type of
393 /// path (file or directory) is updated to reflect the actual contents
394 /// of the file system. If the file does not exist, false is returned.
395 /// For other (hard I/O) errors, a std::string is throwing indicating the
397 /// @throws std::string if an error occurs.
398 /// @brief Get file status.
399 void getStatusInfo(StatusInfo& info) const;
401 /// This function returns the last modified time stamp for the file
402 /// referenced by this path. The Path may reference a file or a directory.
403 /// If the file does not exist, a ZeroTime timestamp is returned.
404 /// @returns last modified timestamp of the file/directory or ZeroTime
405 /// @brief Get file timestamp.
406 inline TimeValue getTimestamp() const {
407 StatusInfo info; getStatusInfo(info); return info.modTime;
410 /// This function returns the size of the file referenced by this path.
411 /// @brief Get file size.
412 inline size_t getSize() const {
413 StatusInfo info; getStatusInfo(info); return info.fileSize;
416 /// This method attempts to make the file referenced by the Path object
417 /// available for reading so that the readable() method will return true.
418 /// @brief Make the file readable;
421 /// This method attempts to make the file referenced by the Path object
422 /// available for writing so that the writable() method will return true.
423 /// @brief Make the file writable;
424 void makeWriteable();
426 /// This method attempts to make the file referenced by the Path object
427 /// available for execution so that the executable() method will return
429 /// @brief Make the file readable;
430 void makeExecutable();
432 /// This method attempts to set the Path object to \p unverified_path
433 /// and interpret the name as a directory name. The \p unverified_path
434 /// is verified. If verification succeeds then \p unverified_path
435 /// is accepted as a directory and true is returned. Otherwise,
436 /// the Path object remains unchanged and false is returned.
437 /// @returns true if the path was set, false otherwise.
438 /// @param unverified_path The path to be set in Path object.
440 /// @brief Set a full path from a std::string
441 bool setDirectory(const std::string& unverified_path);
443 /// This method attempts to set the Path object to \p unverified_path
444 /// and interpret the name as a file name. The \p unverified_path
445 /// is verified. If verification succeeds then \p unverified_path
446 /// is accepted as a file name and true is returned. Otherwise,
447 /// the Path object remains unchanged and false is returned.
448 /// @returns true if the path was set, false otherwise.
449 /// @param unverified_path The path to be set in Path object.
451 /// @brief Set a full path from a std::string
452 bool setFile(const std::string& unverified_path);
454 /// The \p dirname is added to the end of the Path if it is a legal
455 /// directory name for the operating system. The precondition for this
456 /// function is that the Path must reference a directory name (i.e.
457 /// isDirectory() returns true).
458 /// @param dirname A string providing the directory name to
459 /// be added to the end of the path.
460 /// @returns false if the directory name could not be added
462 /// @brief Adds the name of a directory to a Path.
463 bool appendDirectory( const std::string& dirname );
465 /// One directory component is removed from the Path name. The Path must
466 /// refer to a non-root directory name (i.e. isDirectory() returns true
467 /// but isRootDirectory() returns false). Upon exit, the Path will
468 /// refer to the directory above it.
470 /// @returns false if the directory name could not be removed.
471 /// @brief Removes the last directory component of the Path.
472 bool elideDirectory();
474 /// The \p filename is added to the end of the Path if it is a legal
475 /// directory name for the operating system. The precondition for this
476 /// function is that the Path reference a directory name (i.e.
477 /// isDirectory() returns true).
479 /// @returns false if the file name could not be added.
480 /// @brief Appends the name of a file.
481 bool appendFile( const std::string& filename );
483 /// One file component is removed from the Path name. The Path must
484 /// refer to a file (i.e. isFile() returns true). Upon exit,
485 /// the Path will refer to the directory above it.
487 /// @returns false if the file name could not be removed
488 /// @brief Removes the last file component of the path.
491 /// A period and the \p suffix are appended to the end of the pathname.
492 /// The precondition for this function is that the Path reference a file
493 /// name (i.e. isFile() returns true). If the Path is not a file, no
494 /// action is taken and the function returns false. If the path would
495 /// become invalid for the host operating system, false is returned.
496 /// @returns false if the suffix could not be added, true if it was.
498 /// @brief Adds a period and the \p suffix to the end of the pathname.
499 bool appendSuffix(const std::string& suffix);
501 /// The suffix of the filename is removed. The suffix begins with and
502 /// includes the last . character in the filename after the last directory
503 /// separator and extends until the end of the name. If no . character is
504 /// after the last directory separator, then the file name is left
505 /// unchanged (i.e. it was already without a suffix) but the function
507 /// @returns false if there was no suffix to remove, true otherwise.
509 /// @brief Remove the suffix from a path name.
512 /// The current Path name is made unique in the file system. Upon return,
513 /// the Path will have been changed to make a unique file in the file
514 /// system or it will not have been changed if the current path name is
516 /// @throws std::string if an unrecoverable error occurs.
517 /// @brief Make the current path name unique in the file system.
518 void makeUnique( bool reuse_current = true );
520 /// This method attempts to create a directory in the file system with the
521 /// same name as the Path object. The \p create_parents parameter controls
522 /// whether intermediate directories are created or not. if \p
523 /// create_parents is true, then an attempt will be made to create all
524 /// intermediate directories. If \p create_parents is false, then only the
525 /// final directory component of the Path name will be created. The
526 /// created directory will have no entries.
527 /// @returns false if the Path does not reference a directory, true
529 /// @param create_parents Determines whether non-existent directory
530 /// components other than the last one (the "parents") are created or not.
531 /// @throws std::string if an error occurs.
532 /// @brief Create the directory this Path refers to.
533 bool createDirectory( bool create_parents = false );
535 /// This method attempts to create a file in the file system with the same
536 /// name as the Path object. The intermediate directories must all exist
537 /// at the time this method is called. Use createDirectories to
538 /// accomplish that. The created file will be empty upon return from this
540 /// @returns false if the Path does not reference a file, true otherwise.
541 /// @throws std::string if an error occurs.
542 /// @brief Create the file this Path refers to.
545 /// This is like createFile except that it creates a temporary file. A
546 /// unique temporary file name is generated based on the contents of
547 /// \p this before the call. The new name is assigned to \p this and the
548 /// file is created. Note that this will both change the Path object
549 /// *and* create the corresponding file. This function will ensure that
550 /// the newly generated temporary file name is unique in the file system.
551 /// @throws std::string if there is an error
552 /// @brief Create a unique temporary file
553 bool createTemporaryFile(bool reuse_current = false);
556 /// This method renames the file referenced by \p this as \p newName. Both
557 /// files must exist before making this call.
558 /// @returns false if the Path does not refer to a file, true otherwise.
559 /// @throws std::string if there is an file system error.
560 /// @brief Rename one file as another.
561 bool renameFile(const Path& newName);
563 /// This method sets the access time, modification time, and permission
564 /// mode of the file associated with \p this as given by \p si.
565 /// @returns false if the Path does not refer to a file, true otherwise.
566 /// @throws std::string if the file could not be modified
567 /// @brief Set file times and mode.
568 bool setStatusInfo(const StatusInfo& si ) const ;
574 mutable std::string path; ///< Storage for the path name.
579 /// This enumeration delineates the kinds of files that LLVM knows about.
581 UnknownFileType = 0, ///< Unrecognized file
582 BytecodeFileType = 1, ///< Uncompressed bytecode file
583 CompressedBytecodeFileType = 2, ///< Compressed bytecode file
584 ArchiveFileType = 3, ///< ar style archive file
587 /// This utility function allows any memory block to be examined in order
588 /// to determine its file type.
589 LLVMFileType IdentifyFileType(const char*magic, unsigned length);
591 /// This function can be used to copy the file specified by Src to the
592 /// file specified by Dest. If an error occurs, Dest is removed.
593 /// @throws std::string if an error opening or writing the files occurs.
594 /// @brief Copy one file to another.
595 void CopyFile(const Path& Dest, const Path& Src);
598 inline std::ostream& operator<<(std::ostream& strm, const sys::Path& aPath) {
599 strm << aPath.toString();