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
19 #include "llvm/System/TimeValue.h"
24 /// This class provides an abstraction for the path to a file or directory
25 /// in the operating system's filesystem and provides various basic operations
26 /// on it. Note that this class only represents the name of a path to a file
27 /// or directory which may or may not be valid for a given machine's file
28 /// system. A Path ensures that the name it encapsulates is syntactical valid
29 /// for the operating system it is running on but does not ensure correctness
30 /// for any particular file system. A Path either references a file or a
31 /// directory and the distinction is consistently maintained. Most operations
32 /// on the class have invariants that require the Path object to be either a
33 /// file path or a directory path, but not both. Those operations will also
34 /// leave the object as either a file path or object path. There is exactly
35 /// one invalid Path which is the empty path. The class should never allow any
36 /// other syntactically invalid non-empty path name to be assigned. Empty
37 /// paths are required in order to indicate an error result. If the path is
38 /// empty, the isValid operation will return false. All operations will fail
39 /// if isValid is false. Operations that change the path will either return
40 /// false if it would cause a syntactically invalid path name (in which case
41 /// the Path object is left unchanged) or throw an std::string exception
42 /// indicating the error.
44 /// @brief An abstraction for operating system paths.
49 typedef std::vector<Path> Vector;
51 /// This structure provides basic file system information about a file.
52 /// The structure is filled in by the getStatusInfo method.
53 /// @brief File status structure
55 StatusInfo() : modTime(0,0) { fileSize=0; mode=0; user=0; group=0; }
56 size_t fileSize; ///< Size of the file in bytes
57 TimeValue modTime; ///< Time of file's modification
58 uint32_t mode; ///< Mode of the file, if applicable
59 uint32_t user; ///< User ID of owner, if applicable
60 uint32_t group; ///< Group ID of owner, if applicable
61 bool isDir; ///< True if this is a directory.
66 /// @name Constructors
69 /// Construct a path to the root directory of the file system. The root
70 /// directory is a top level directory above which there are no more
71 /// directories. For example, on UNIX, the root directory is /. On Windows
72 /// it is C:\. Other operating systems may have different notions of
73 /// what the root directory is.
75 static Path GetRootDirectory();
77 /// Construct a path to a unique temporary directory that is created in
78 /// a "standard" place for the operating system. The directory is
79 /// guaranteed to be created on exit from this function. If the directory
80 /// cannot be created, the function will throw an exception.
81 /// @throws std::string indicating why the directory could not be created.
82 /// @brief Constrct a path to an new, unique, existing temporary
84 static Path GetTemporaryDirectory();
86 /// Determine the platform-specific location of a library by first
87 /// searching a list of library paths, then searching a list of "well
88 /// known" paths for the platform. T
89 /// @returns a valid Path object if the library was found, an invalid
92 /// @brief Locate a library in a platform specific manner.
93 static Path GetLibraryPath(const std::string& basename,
94 const std::vector<std::string>& LibPaths);
96 /// Construct a path to the first system library directory. The
97 /// implementation of Path on a given platform must ensure that this
98 /// directory both exists and also contains standard system libraries
99 /// suitable for linking into programs.
101 /// @brief Construct a path to the first system library directory
102 static Path GetSystemLibraryPath1();
104 /// Construct a path to the second system library directory. The
105 /// implementation of Path on a given platform must ensure that this
106 /// directory both exists and also contains standard system libraries
107 /// suitable for linking into programs. Note that the "second" system
108 /// library directory may or may not be different from the first.
110 /// @brief Construct a path to the second system library directory
111 static Path GetSystemLibraryPath2();
113 /// Construct a path to the default LLVM configuration directory. The
114 /// implementation must ensure that this is a well-known (same on many
115 /// systems) directory in which llvm configuration files exist. For
116 /// example, on Unix, the /etc/llvm directory has been selected.
118 /// @brief Construct a path to the default LLVM configuration directory
119 static Path GetLLVMDefaultConfigDir();
121 /// Construct a path to the LLVM installed configuration directory. The
122 /// implementation must ensure that this refers to the "etc" directory of
123 /// the LLVM installation. This is the location where configuration files
124 /// will be located for a particular installation of LLVM on a machine.
126 /// @brief Construct a path to the LLVM installed configuration directory
127 static Path GetLLVMConfigDir();
129 /// Construct a path to the current user's home directory. The
130 /// implementation must use an operating system specific mechanism for
131 /// determining the user's home directory. For example, the environment
132 /// variable "HOME" could be used on Unix. If a given operating system
133 /// does not have the concept of a user's home directory, this static
134 /// constructor must provide the same result as GetRootDirectory.
136 /// @brief Construct a path to the current user's "home" directory
137 static Path GetUserHomeDirectory();
139 /// Return the suffix commonly used on file names that contain a shared
140 /// object, shared archive, or dynamic link library. Such files are
141 /// linked at runtime into a process and their code images are shared
142 /// between processes.
143 /// @returns The dynamic link library suffix for the current platform.
144 /// @brief Return the dynamic link library suffix.
145 static std::string GetDLLSuffix();
147 /// This is one of the very few ways in which a path can be constructed
148 /// with a syntactically invalid name. The only *legal* invalid name is an
149 /// empty one. Other invalid names are not permitted. Empty paths are
150 /// provided so that they can be used to indicate null or error results in
151 /// other lib/System functionality.
153 /// @brief Construct an empty (and invalid) path.
156 /// This constructor will accept a std::string as a path but if verifies
157 /// that the path string has a legal syntax for the operating system on
158 /// which it is running. This allows a path to be taken in from outside
159 /// the program. However, if the path is not valid, the Path object will
160 /// be set to an empty string and an exception will be thrown.
161 /// @throws std::string if the path string is not legal.
162 /// @param unverified_path The path to verify and assign.
163 /// @brief Construct a Path from a string.
164 explicit Path(std::string unverified_path);
170 /// Makes a copy of \p that to \p this.
173 /// @brief Assignment Operator
174 Path & operator = ( const Path & that ) {
179 /// Compares \p this Path with \p that Path for equality.
180 /// @returns true if \p this and \p that refer to the same thing.
182 /// @brief Equality Operator
183 bool operator == (const Path& that) const {
184 return 0 == path.compare(that.path) ;
187 /// Compares \p this Path with \p that Path for inequality.
188 /// @returns true if \p this and \p that refer to different things.
190 /// @brief Inequality Operator
191 bool operator !=( const Path & that ) const {
192 return 0 != path.compare( that.path );
195 /// Determines if \p this Path is less than \p that Path. This is required
196 /// so that Path objects can be placed into ordered collections (e.g.
197 /// std::map). The comparison is done lexicographically as defined by
198 /// the std::string::compare method.
199 /// @returns true if \p this path is lexicographically less than \p that.
201 /// @brief Less Than Operator
202 bool operator< (const Path& that) const {
203 return 0 > path.compare( that.path );
210 /// This function will use an operating system specific algorithm to
211 /// determine if the current value of \p this is a syntactically valid
212 /// path name for the operating system. The path name does not need to
213 /// exist, validity is simply syntactical. Empty paths are always invalid.
214 /// @returns true iff the path name is syntactically legal for the
215 /// host operating system.
216 /// @brief Determine if a path is syntactically valid or not.
217 bool isValid() const;
219 /// This function determines if the contents of the path name are
220 /// empty. That is, the path has a zero length.
221 /// @returns true iff the path is empty.
222 /// @brief Determines if the path name is empty (invalid).
223 bool isEmpty() const { return path.empty(); }
225 /// This function determines if the path name in this object is intended
226 /// to reference a legal file name (as opposed to a directory name). This
227 /// function does not verify anything with the file system, it merely
228 /// determines if the syntax of the path represents a file name or not.
229 /// @returns true if this path name references a file.
230 /// @brief Determines if the path name references a file.
233 /// This function determines if the path name in this object is intended
234 /// to reference a legal directory name (as opposed to a file name). This
235 /// function does not verify anything with the file system, it merely
236 /// determines if the syntax of the path represents a directory name or
238 /// @returns true if the path name references a directory
239 /// @brief Determines if the path name references a directory.
240 bool isDirectory() const;
242 /// This function determines if the path name in this object references
243 /// the root (top level directory) of the file system. The details of what
244 /// is considered the "root" may vary from system to system so this method
245 /// will do the necessary checking.
246 /// @returns true iff the path name references the root directory.
247 /// @brief Determines if the path references the root directory.
248 bool isRootDirectory() const;
250 /// This function opens the file associated with the path name provided by
251 /// the Path object and reads its magic number. If the magic number at the
252 /// start of the file matches \p magic, true is returned. In all other
253 /// cases (file not found, file not accessible, etc.) it returns false.
254 /// @returns true if the magic number of the file matches \p magic.
255 /// @brief Determine if file has a specific magic number
256 bool hasMagicNumber(const std::string& magic) const;
258 /// This function retrieves the first \p len bytes of the file associated
259 /// with \p this. These bytes are returned as the "magic number" in the
260 /// \p Magic parameter.
261 /// @returns true if the Path is a file and the magic number is retrieved,
263 /// @brief Get the file's magic number.
264 bool getMagicNumber(std::string& Magic, unsigned len) const;
266 /// This function determines if the path name in the object references an
267 /// archive file by looking at its magic number.
268 /// @returns true if the file starts with the magic number for an archive
270 /// @brief Determine if the path references an archive file.
271 bool isArchive() const;
273 /// This function determines if the path name in the object references an
274 /// LLVM Bytecode file by looking at its magic number.
275 /// @returns true if the file starts with the magic number for LLVM
277 /// @brief Determine if the path references a bytecode file.
278 bool isBytecodeFile() const;
280 /// This function determines if the path name references an existing file
281 /// or directory in the file system. Unlike isFile and isDirectory, this
282 /// function actually checks for the existence of the file or directory.
283 /// @returns true if the pathname references an existing file.
284 /// @brief Determines if the path is a file or directory in
288 /// This function determines if the path name references a readable file
289 /// or directory in the file system. Unlike isFile and isDirectory, this
290 /// function actually checks for the existence and readability (by the
291 /// current program) of the file or directory.
292 /// @returns true if the pathname references a readable file.
293 /// @brief Determines if the path is a readable file or directory
294 /// in the file system.
295 bool readable() const;
297 /// This function determines if the path name references a writable file
298 /// or directory in the file system. Unlike isFile and isDirectory, this
299 /// function actually checks for the existence and writability (by the
300 /// current program) of the file or directory.
301 /// @returns true if the pathname references a writable file.
302 /// @brief Determines if the path is a writable file or directory
303 /// in the file system.
304 bool writable() const;
306 /// This function determines if the path name references an executable
307 /// file in the file system. Unlike isFile and isDirectory, this
308 /// function actually checks for the existence and executability (by
309 /// the current program) of the file.
310 /// @returns true if the pathname references an executable file.
311 /// @brief Determines if the path is an executable file in the file
313 bool executable() const;
315 /// This function returns the current contents of the path as a
316 /// std::string. This allows the underlying path string to be manipulated
317 /// by other software.
318 /// @returns std::string containing the path name.
319 /// @brief Returns the path as a std::string.
320 std::string get() const { return path; }
322 /// This function returns the last component of the path name. If the
323 /// isDirectory() function would return true then this returns the name
324 /// of the last directory in the path. If the isFile() function would
325 /// return true then this function returns the name of the file without
326 /// any of the preceding directories.
327 /// @returns std::string containing the last component of the path name.
328 /// @brief Returns the last component of the path name.
329 std::string getLast() const;
331 /// This function strips off the path and suffix of the file name and
332 /// returns just the basename.
333 /// @returns std::string containing the basename of the path
335 /// @brief Get the base name of the path
336 std::string getBasename() const;
338 /// This function builds a list of paths that are the names of the
339 /// files and directories in a directory.
340 /// @returns false if \p this is not a directory, true otherwise
341 /// @throws std::string if the directory cannot be searched
342 /// @brief Build a list of directory's contents.
343 bool getDirectoryContents(Vector& paths) const;
345 /// Obtain a 'C' string for the path name.
346 /// @returns a 'C' string containing the path name.
347 /// @brief Returns the path as a C string.
348 const char* const c_str() const { return path.c_str(); }
354 /// The path name is cleared and becomes empty. This is an invalid
355 /// path name but is the *only* invalid path name. This is provided
356 /// so that path objects can be used to indicate the lack of a
357 /// valid path being found.
358 void clear() { path.clear(); }
360 /// This function returns status information about the file. The type of
361 /// path (file or directory) is updated to reflect the actual contents
362 /// of the file system.
364 /// @throws std::string if an error occurs.
365 /// @brief Get file status.
366 void getStatusInfo(StatusInfo& stat);
368 /// This method attempts to set the Path object to \p unverified_path
369 /// and interpret the name as a directory name. The \p unverified_path
370 /// is verified. If verification succeeds then \p unverified_path
371 /// is accepted as a directory and true is returned. Otherwise,
372 /// the Path object remains unchanged and false is returned.
373 /// @returns true if the path was set, false otherwise.
374 /// @param unverified_path The path to be set in Path object.
376 /// @brief Set a full path from a std::string
377 bool setDirectory(const std::string& unverified_path);
379 /// This method attempts to set the Path object to \p unverified_path
380 /// and interpret the name as a file name. The \p unverified_path
381 /// is verified. If verification succeeds then \p unverified_path
382 /// is accepted as a file name and true is returned. Otherwise,
383 /// the Path object remains unchanged and false is returned.
384 /// @returns true if the path was set, false otherwise.
385 /// @param unverified_path The path to be set in Path object.
387 /// @brief Set a full path from a std::string
388 bool setFile(const std::string& unverified_path);
390 /// The \p dirname is added to the end of the Path if it is a legal
391 /// directory name for the operating system. The precondition for this
392 /// function is that the Path must reference a directory name (i.e.
393 /// isDirectory() returns true).
394 /// @param dirname A string providing the directory name to
395 /// be added to the end of the path.
396 /// @returns false if the directory name could not be added
398 /// @brief Adds the name of a directory to a Path.
399 bool appendDirectory( const std::string& dirname );
401 /// One directory component is removed from the Path name. The Path must
402 /// refer to a non-root directory name (i.e. isDirectory() returns true
403 /// but isRootDirectory() returns false). Upon exit, the Path will
404 /// refer to the directory above it.
406 /// @returns false if the directory name could not be removed.
407 /// @brief Removes the last directory component of the Path.
408 bool elideDirectory();
410 /// The \p filename is added to the end of the Path if it is a legal
411 /// directory name for the operating system. The precondition for this
412 /// function is that the Path reference a directory name (i.e.
413 /// isDirectory() returns true).
415 /// @returns false if the file name could not be added.
416 /// @brief Appends the name of a file.
417 bool appendFile( const std::string& filename );
419 /// One file component is removed from the Path name. The Path must
420 /// refer to a file (i.e. isFile() returns true). Upon exit,
421 /// the Path will refer to the directory above it.
423 /// @returns false if the file name could not be removed
424 /// @brief Removes the last file component of the path.
427 /// A period and the \p suffix are appended to the end of the pathname.
428 /// The precondition for this function is that the Path reference a file
429 /// name (i.e. isFile() returns true). If the Path is not a file, no
430 /// action is taken and the function returns false. If the path would
431 /// become invalid for the host operating system, false is returned.
432 /// @returns false if the suffix could not be added, true if it was.
434 /// @brief Adds a period and the \p suffix to the end of the pathname.
435 bool appendSuffix(const std::string& suffix);
437 /// The suffix of the filename is removed. The suffix begins with and
438 /// includes the last . character in the filename after the last directory
439 /// separator and extends until the end of the name. If no . character is
440 /// after the last directory separator, then the file name is left
441 /// unchanged (i.e. it was already without a suffix) but the function return
443 /// @returns false if there was no suffix to remove, true otherwise.
445 /// @brief Remove the suffix from a path name.
448 /// This method attempts to create a directory in the file system with the
449 /// same name as the Path object. The \p create_parents parameter controls
450 /// whether intermediate directories are created or not. if \p
451 /// create_parents is true, then an attempt will be made to create all
452 /// intermediate directories. If \p create_parents is false, then only the
453 /// final directory component of the Path name will be created. The
454 /// created directory will have no entries.
455 /// @returns false if the Path does not reference a directory, true
457 /// @param create_parents Determines whether non-existent directory
458 /// components other than the last one (the "parents") are created or not.
459 /// @throws std::string if an error occurs.
460 /// @brief Create the directory this Path refers to.
461 bool createDirectory( bool create_parents = false );
463 /// This method attempts to create a file in the file system with the same
464 /// name as the Path object. The intermediate directories must all exist
465 /// at the time this method is called. Use createDirectories to
466 /// accomplish that. The created file will be empty upon return from this
468 /// @returns false if the Path does not reference a file, true otherwise.
469 /// @throws std::string if an error occurs.
470 /// @brief Create the file this Path refers to.
473 /// This is like createFile except that it creates a temporary file. A
474 /// unique temporary file name is generated based on the contents of
475 /// \p this before the call. The new name is assigned to \p this and the
476 /// file is created. Note that this will both change the Path object
477 /// *and* create the corresponding file. The path of \p this will have
478 /// six characters added to it (per mkstemp(3)) that ensure the file
480 /// @throws std::string if there is an error
481 /// @brief Create a temporary file
482 bool createTemporaryFile();
484 /// This method attempts to destroy the directory named by the last in
485 /// the Path name. If \p remove_contents is false, an attempt will be
486 /// made to remove just the directory that this Path object refers to
487 /// (the final Path component). If \p remove_contents is true, an attempt
488 /// will be made to remove the entire contents of the directory,
490 /// @param destroy_contents Indicates whether the contents of a destroyed
491 /// directory should also be destroyed (recursively).
492 /// @returns false if the Path does not refer to a directory, true
494 /// @throws std::string if there is an error.
495 /// @brief Removes the file or directory from the filesystem.
496 bool destroyDirectory( bool destroy_contents = false );
498 /// This method attempts to destroy the file named by the last item in the
500 /// @returns false if the Path does not refer to a file, true otherwise.
501 /// @throws std::string if there is an error.
502 /// @brief Destroy the file this Path refers to.
505 /// This method renames the file referenced by \p this as \p newName. Both
506 /// files must exist before making this call.
507 /// @returns false if the Path does not refer to a file, true otherwise.
508 /// @throws std::string if there is an file system error.
509 /// @brief Rename one file as another.
510 bool renameFile(const Path& newName);
512 /// This method sets the access time, modification time, and permission
513 /// mode of the file associated with \p this as given by \p si.
514 /// @returns false if the Path does not refer to a file, true otherwise.
515 /// @throws std::string if the file could not be modified
516 /// @brief Set file times and mode.
517 bool setStatusInfo(const StatusInfo& si ) const ;
523 std::string path; ///< Platform agnostic storage for the path name.
528 /// This enumeration delineates the kinds of files that LLVM knows about.
530 UnknownFileType = 0, ///< Unrecognized file
531 BytecodeFileType = 1, ///< Uncompressed bytecode file
532 CompressedBytecodeFileType = 2, ///< Compressed bytecode file
533 ArchiveFileType = 3, ///< ar style archive file
536 /// This utility function allows any memory block to be examined in order
537 /// to determine its file type.
538 LLVMFileType IdentifyFileType(const char*magic, unsigned length);