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"
25 /// This class provides an abstraction for the path to a file or directory
26 /// in the operating system's filesystem and provides various basic operations
27 /// on it. Note that this class only represents the name of a path to a file
28 /// or directory which may or may not be valid for a given machine's file
29 /// system. A Path ensures that the name it encapsulates is syntactical valid
30 /// for the operating system it is running on but does not ensure correctness
31 /// for any particular file system. A Path either references a file or a
32 /// directory and the distinction is consistently maintained. Most operations
33 /// on the class have invariants that require the Path object to be either a
34 /// file path or a directory path, but not both. Those operations will also
35 /// leave the object as either a file path or object path. There is exactly
36 /// one invalid Path which is the empty path. The class should never allow any
37 /// other syntactically invalid non-empty path name to be assigned. Empty
38 /// paths are required in order to indicate an error result. If the path is
39 /// empty, the isValid operation will return false. All operations will fail
40 /// if isValid is false. Operations that change the path will either return
41 /// false if it would cause a syntactically invalid path name (in which case
42 /// the Path object is left unchanged) or throw an std::string exception
43 /// indicating the error.
45 /// @brief An abstraction for operating system paths.
50 /// This structure provides basic file system information about a file. It
51 /// is patterned after the stat(2) Unix operating system call but made
52 /// platform independent and eliminates many of the unix-specific fields.
53 /// However, to support llvm-ar, the mode, user, and group fields are
54 /// retained. These pertain to unix security and may not have a meaningful
55 /// value on non-Unix platforms. However, the fileSize and modTime fields
56 /// should always be applicabe on all platforms. The structure is
57 /// filled in by the getStatusInfo method.
58 /// @brief File status structure
60 StatusInfo() : fileSize(0), modTime(0,0), mode(0777), user(999),
61 group(999), isDir(false) { }
62 size_t fileSize; ///< Size of the file in bytes
63 TimeValue modTime; ///< Time of file's modification
64 uint32_t mode; ///< Mode of the file, if applicable
65 uint32_t user; ///< User ID of owner, if applicable
66 uint32_t group; ///< Group ID of owner, if applicable
67 bool isDir; ///< True if this is a directory.
71 /// @name Constructors
74 /// Construct a path to the root directory of the file system. The root
75 /// directory is a top level directory above which there are no more
76 /// directories. For example, on UNIX, the root directory is /. On Windows
77 /// it is C:\. Other operating systems may have different notions of
78 /// what the root directory is.
80 static Path GetRootDirectory();
82 /// Construct a path to a unique temporary directory that is created in
83 /// a "standard" place for the operating system. The directory is
84 /// guaranteed to be created on exit from this function. If the directory
85 /// cannot be created, the function will throw an exception.
86 /// @throws std::string indicating why the directory could not be created.
87 /// @brief Constrct a path to an new, unique, existing temporary
89 static Path GetTemporaryDirectory();
91 /// Determine the platform-specific location of a library by first
92 /// searching a list of library paths, then searching a list of "well
93 /// known" paths for the platform. T
94 /// @returns a valid Path object if the library was found, an invalid
97 /// @brief Locate a library in a platform specific manner.
98 static Path GetLibraryPath(const std::string& basename,
99 const std::vector<std::string>& LibPaths);
101 /// Construct a path to the first system library directory. The
102 /// implementation of Path on a given platform must ensure that this
103 /// directory both exists and also contains standard system libraries
104 /// suitable for linking into programs.
106 /// @brief Construct a path to the first system library directory
107 static Path GetSystemLibraryPath1();
109 /// Construct a path to the second system library directory. The
110 /// implementation of Path on a given platform must ensure that this
111 /// directory both exists and also contains standard system libraries
112 /// suitable for linking into programs. Note that the "second" system
113 /// library directory may or may not be different from the first.
115 /// @brief Construct a path to the second system library directory
116 static Path GetSystemLibraryPath2();
118 /// Construct a path to the default LLVM configuration directory. The
119 /// implementation must ensure that this is a well-known (same on many
120 /// systems) directory in which llvm configuration files exist. For
121 /// example, on Unix, the /etc/llvm directory has been selected.
123 /// @brief Construct a path to the default LLVM configuration directory
124 static Path GetLLVMDefaultConfigDir();
126 /// Construct a path to the LLVM installed configuration directory. The
127 /// implementation must ensure that this refers to the "etc" directory of
128 /// the LLVM installation. This is the location where configuration files
129 /// will be located for a particular installation of LLVM on a machine.
131 /// @brief Construct a path to the LLVM installed configuration directory
132 static Path GetLLVMConfigDir();
134 /// Construct a path to the current user's home directory. The
135 /// implementation must use an operating system specific mechanism for
136 /// determining the user's home directory. For example, the environment
137 /// variable "HOME" could be used on Unix. If a given operating system
138 /// does not have the concept of a user's home directory, this static
139 /// constructor must provide the same result as GetRootDirectory.
141 /// @brief Construct a path to the current user's "home" directory
142 static Path GetUserHomeDirectory();
144 /// Return the suffix commonly used on file names that contain a shared
145 /// object, shared archive, or dynamic link library. Such files are
146 /// linked at runtime into a process and their code images are shared
147 /// between processes.
148 /// @returns The dynamic link library suffix for the current platform.
149 /// @brief Return the dynamic link library suffix.
150 static std::string GetDLLSuffix();
152 /// This is one of the very few ways in which a path can be constructed
153 /// with a syntactically invalid name. The only *legal* invalid name is an
154 /// empty one. Other invalid names are not permitted. Empty paths are
155 /// provided so that they can be used to indicate null or error results in
156 /// other lib/System functionality.
158 /// @brief Construct an empty (and invalid) path.
161 /// This constructor will accept a std::string as a path but if verifies
162 /// that the path string has a legal syntax for the operating system on
163 /// which it is running. This allows a path to be taken in from outside
164 /// the program. However, if the path is not valid, the Path object will
165 /// be set to an empty string and an exception will be thrown.
166 /// @throws std::string if the path string is not legal.
167 /// @param unverified_path The path to verify and assign.
168 /// @brief Construct a Path from a string.
169 explicit Path(std::string unverified_path);
175 /// Makes a copy of \p that to \p this.
178 /// @brief Assignment Operator
179 Path & operator = ( const Path & that ) {
184 /// Compares \p this Path with \p that Path for equality.
185 /// @returns true if \p this and \p that refer to the same thing.
187 /// @brief Equality Operator
188 bool operator == (const Path& that) const {
189 return 0 == path.compare(that.path) ;
192 /// Compares \p this Path with \p that Path for inequality.
193 /// @returns true if \p this and \p that refer to different things.
195 /// @brief Inequality Operator
196 bool operator !=( const Path & that ) const {
197 return 0 != path.compare( that.path );
200 /// Determines if \p this Path is less than \p that Path. This is required
201 /// so that Path objects can be placed into ordered collections (e.g.
202 /// std::map). The comparison is done lexicographically as defined by
203 /// the std::string::compare method.
204 /// @returns true if \p this path is lexicographically less than \p that.
206 /// @brief Less Than Operator
207 bool operator< (const Path& that) const {
208 return 0 > path.compare( that.path );
215 /// This function will use an operating system specific algorithm to
216 /// determine if the current value of \p this is a syntactically valid
217 /// path name for the operating system. The path name does not need to
218 /// exist, validity is simply syntactical. Empty paths are always invalid.
219 /// @returns true iff the path name is syntactically legal for the
220 /// host operating system.
221 /// @brief Determine if a path is syntactically valid or not.
222 bool isValid() const;
224 /// This function determines if the contents of the path name are
225 /// empty. That is, the path has a zero length.
226 /// @returns true iff the path is empty.
227 /// @brief Determines if the path name is empty (invalid).
228 bool isEmpty() const { return path.empty(); }
230 /// This function determines if the path name in this object is intended
231 /// to reference a legal file name (as opposed to a directory name). This
232 /// function does not verify anything with the file system, it merely
233 /// determines if the syntax of the path represents a file name or not.
234 /// @returns true if this path name references a file.
235 /// @brief Determines if the path name references a file.
238 /// This function determines if the path name in this object is intended
239 /// to reference a legal directory name (as opposed to a file name). This
240 /// function does not verify anything with the file system, it merely
241 /// determines if the syntax of the path represents a directory name or
243 /// @returns true if the path name references a directory
244 /// @brief Determines if the path name references a directory.
245 bool isDirectory() const;
247 /// This function determines if the path name in this object references
248 /// the root (top level directory) of the file system. The details of what
249 /// is considered the "root" may vary from system to system so this method
250 /// will do the necessary checking.
251 /// @returns true iff the path name references the root directory.
252 /// @brief Determines if the path references the root directory.
253 bool isRootDirectory() const;
255 /// This function opens the file associated with the path name provided by
256 /// the Path object and reads its magic number. If the magic number at the
257 /// start of the file matches \p magic, true is returned. In all other
258 /// cases (file not found, file not accessible, etc.) it returns false.
259 /// @returns true if the magic number of the file matches \p magic.
260 /// @brief Determine if file has a specific magic number
261 bool hasMagicNumber(const std::string& magic) const;
263 /// This function retrieves the first \p len bytes of the file associated
264 /// with \p this. These bytes are returned as the "magic number" in the
265 /// \p Magic parameter.
266 /// @returns true if the Path is a file and the magic number is retrieved,
268 /// @brief Get the file's magic number.
269 bool getMagicNumber(std::string& Magic, unsigned len) const;
271 /// This function determines if the path name in the object references an
272 /// archive file by looking at its magic number.
273 /// @returns true if the file starts with the magic number for an archive
275 /// @brief Determine if the path references an archive file.
276 bool isArchive() const;
278 /// This function determines if the path name in the object references an
279 /// LLVM Bytecode file by looking at its magic number.
280 /// @returns true if the file starts with the magic number for LLVM
282 /// @brief Determine if the path references a bytecode file.
283 bool isBytecodeFile() const;
285 /// This function determines if the path name references an existing file
286 /// or directory in the file system. Unlike isFile and isDirectory, this
287 /// function actually checks for the existence of the file or directory.
288 /// @returns true if the pathname references an existing file.
289 /// @brief Determines if the path is a file or directory in
293 /// This function determines if the path name references a readable file
294 /// or directory in the file system. Unlike isFile and isDirectory, this
295 /// function actually checks for the existence and readability (by the
296 /// current program) of the file or directory.
297 /// @returns true if the pathname references a readable file.
298 /// @brief Determines if the path is a readable file or directory
299 /// in the file system.
300 bool readable() const;
302 /// This function determines if the path name references a writable file
303 /// or directory in the file system. Unlike isFile and isDirectory, this
304 /// function actually checks for the existence and writability (by the
305 /// current program) of the file or directory.
306 /// @returns true if the pathname references a writable file.
307 /// @brief Determines if the path is a writable file or directory
308 /// in the file system.
309 bool writable() const;
311 /// This function determines if the path name references an executable
312 /// file in the file system. Unlike isFile and isDirectory, this
313 /// function actually checks for the existence and executability (by
314 /// the current program) of the file.
315 /// @returns true if the pathname references an executable file.
316 /// @brief Determines if the path is an executable file in the file
318 bool executable() const;
320 /// This function returns the current contents of the path as a
321 /// std::string. This allows the underlying path string to be manipulated
322 /// by other software.
323 /// @returns std::string containing the path name.
324 /// @brief Returns the path as a std::string.
325 std::string get() const { return path; }
327 /// This function returns the last component of the path name. If the
328 /// isDirectory() function would return true then this returns the name
329 /// of the last directory in the path. If the isFile() function would
330 /// return true then this function returns the name of the file without
331 /// any of the preceding directories.
332 /// @returns std::string containing the last component of the path name.
333 /// @brief Returns the last component of the path name.
334 std::string getLast() const;
336 /// This function strips off the path and suffix of the file name and
337 /// returns just the basename.
338 /// @returns std::string containing the basename of the path
340 /// @brief Get the base name of the path
341 std::string getBasename() const;
343 /// This function builds a list of paths that are the names of the
344 /// files and directories in a directory.
345 /// @returns false if \p this is not a directory, true otherwise
346 /// @throws std::string if the directory cannot be searched
347 /// @brief Build a list of directory's contents.
348 bool getDirectoryContents(std::set<Path>& paths) const;
350 /// Obtain a 'C' string for the path name.
351 /// @returns a 'C' string containing the path name.
352 /// @brief Returns the path as a C string.
353 const char* const c_str() const { return path.c_str(); }
359 /// The path name is cleared and becomes empty. This is an invalid
360 /// path name but is the *only* invalid path name. This is provided
361 /// so that path objects can be used to indicate the lack of a
362 /// valid path being found.
363 void clear() { path.clear(); }
365 /// This function returns status information about the file. The type of
366 /// path (file or directory) is updated to reflect the actual contents
367 /// of the file system. If the file does not exist, false is returned.
368 /// For other (hard I/O) errors, a std::string is throwing indicating the
370 /// @throws std::string if an error occurs.
371 /// @brief Get file status.
372 void getStatusInfo(StatusInfo& info) const;
374 /// This method attempts to set the Path object to \p unverified_path
375 /// and interpret the name as a directory name. The \p unverified_path
376 /// is verified. If verification succeeds then \p unverified_path
377 /// is accepted as a directory and true is returned. Otherwise,
378 /// the Path object remains unchanged and false is returned.
379 /// @returns true if the path was set, false otherwise.
380 /// @param unverified_path The path to be set in Path object.
382 /// @brief Set a full path from a std::string
383 bool setDirectory(const std::string& unverified_path);
385 /// This method attempts to set the Path object to \p unverified_path
386 /// and interpret the name as a file name. The \p unverified_path
387 /// is verified. If verification succeeds then \p unverified_path
388 /// is accepted as a file name and true is returned. Otherwise,
389 /// the Path object remains unchanged and false is returned.
390 /// @returns true if the path was set, false otherwise.
391 /// @param unverified_path The path to be set in Path object.
393 /// @brief Set a full path from a std::string
394 bool setFile(const std::string& unverified_path);
396 /// The \p dirname is added to the end of the Path if it is a legal
397 /// directory name for the operating system. The precondition for this
398 /// function is that the Path must reference a directory name (i.e.
399 /// isDirectory() returns true).
400 /// @param dirname A string providing the directory name to
401 /// be added to the end of the path.
402 /// @returns false if the directory name could not be added
404 /// @brief Adds the name of a directory to a Path.
405 bool appendDirectory( const std::string& dirname );
407 /// One directory component is removed from the Path name. The Path must
408 /// refer to a non-root directory name (i.e. isDirectory() returns true
409 /// but isRootDirectory() returns false). Upon exit, the Path will
410 /// refer to the directory above it.
412 /// @returns false if the directory name could not be removed.
413 /// @brief Removes the last directory component of the Path.
414 bool elideDirectory();
416 /// The \p filename is added to the end of the Path if it is a legal
417 /// directory name for the operating system. The precondition for this
418 /// function is that the Path reference a directory name (i.e.
419 /// isDirectory() returns true).
421 /// @returns false if the file name could not be added.
422 /// @brief Appends the name of a file.
423 bool appendFile( const std::string& filename );
425 /// One file component is removed from the Path name. The Path must
426 /// refer to a file (i.e. isFile() returns true). Upon exit,
427 /// the Path will refer to the directory above it.
429 /// @returns false if the file name could not be removed
430 /// @brief Removes the last file component of the path.
433 /// A period and the \p suffix are appended to the end of the pathname.
434 /// The precondition for this function is that the Path reference a file
435 /// name (i.e. isFile() returns true). If the Path is not a file, no
436 /// action is taken and the function returns false. If the path would
437 /// become invalid for the host operating system, false is returned.
438 /// @returns false if the suffix could not be added, true if it was.
440 /// @brief Adds a period and the \p suffix to the end of the pathname.
441 bool appendSuffix(const std::string& suffix);
443 /// The suffix of the filename is removed. The suffix begins with and
444 /// includes the last . character in the filename after the last directory
445 /// separator and extends until the end of the name. If no . character is
446 /// after the last directory separator, then the file name is left
447 /// unchanged (i.e. it was already without a suffix) but the function
449 /// @returns false if there was no suffix to remove, true otherwise.
451 /// @brief Remove the suffix from a path name.
454 /// This method attempts to create a directory in the file system with the
455 /// same name as the Path object. The \p create_parents parameter controls
456 /// whether intermediate directories are created or not. if \p
457 /// create_parents is true, then an attempt will be made to create all
458 /// intermediate directories. If \p create_parents is false, then only the
459 /// final directory component of the Path name will be created. The
460 /// created directory will have no entries.
461 /// @returns false if the Path does not reference a directory, true
463 /// @param create_parents Determines whether non-existent directory
464 /// components other than the last one (the "parents") are created or not.
465 /// @throws std::string if an error occurs.
466 /// @brief Create the directory this Path refers to.
467 bool createDirectory( bool create_parents = false );
469 /// This method attempts to create a file in the file system with the same
470 /// name as the Path object. The intermediate directories must all exist
471 /// at the time this method is called. Use createDirectories to
472 /// accomplish that. The created file will be empty upon return from this
474 /// @returns false if the Path does not reference a file, true otherwise.
475 /// @throws std::string if an error occurs.
476 /// @brief Create the file this Path refers to.
479 /// This is like createFile except that it creates a temporary file. A
480 /// unique temporary file name is generated based on the contents of
481 /// \p this before the call. The new name is assigned to \p this and the
482 /// file is created. Note that this will both change the Path object
483 /// *and* create the corresponding file. This function will ensure that
484 /// the newly generated temporary file name is unique in the file system.
485 /// @throws std::string if there is an error
486 /// @brief Create a unique temporary file
487 bool createTemporaryFile();
489 /// This method attempts to destroy the directory named by the last in
490 /// the Path name. If \p remove_contents is false, an attempt will be
491 /// made to remove just the directory that this Path object refers to
492 /// (the final Path component). If \p remove_contents is true, an attempt
493 /// will be made to remove the entire contents of the directory,
495 /// @param destroy_contents Indicates whether the contents of a destroyed
496 /// directory should also be destroyed (recursively).
497 /// @returns false if the Path does not refer to a directory, true
499 /// @throws std::string if there is an error.
500 /// @brief Removes the file or directory from the filesystem.
501 bool destroyDirectory( bool destroy_contents = false );
503 /// This method attempts to destroy the file named by the last item in the
505 /// @returns false if the Path does not refer to a file, true otherwise.
506 /// @throws std::string if there is an error.
507 /// @brief Destroy the file this Path refers to.
510 /// This method renames the file referenced by \p this as \p newName. Both
511 /// files must exist before making this call.
512 /// @returns false if the Path does not refer to a file, true otherwise.
513 /// @throws std::string if there is an file system error.
514 /// @brief Rename one file as another.
515 bool renameFile(const Path& newName);
517 /// This method sets the access time, modification time, and permission
518 /// mode of the file associated with \p this as given by \p si.
519 /// @returns false if the Path does not refer to a file, true otherwise.
520 /// @throws std::string if the file could not be modified
521 /// @brief Set file times and mode.
522 bool setStatusInfo(const StatusInfo& si ) const ;
528 mutable std::string path; ///< Storage for the path name.
533 /// This enumeration delineates the kinds of files that LLVM knows about.
535 UnknownFileType = 0, ///< Unrecognized file
536 BytecodeFileType = 1, ///< Uncompressed bytecode file
537 CompressedBytecodeFileType = 2, ///< Compressed bytecode file
538 ArchiveFileType = 3, ///< ar style archive file
541 /// This utility function allows any memory block to be examined in order
542 /// to determine its file type.
543 LLVMFileType IdentifyFileType(const char*magic, unsigned length);