1 //===- llvm/Support/PathV2.h - Path Operating System Concept -----*- 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 // This file declares the llvm::sys::path namespace. It is designed after
11 // TR2/boost filesystem (v3), but modified to remove exception handling and the
14 // All functions return an error_code and their actual work via the last out
15 // argument. The out argument is defined if and only if errc::success is
16 // returned. A function may return any error code in the generic or system
17 // category. However, they shall be equivalent to any error conditions listed
18 // in each functions respective documentation if the condition applies. [ note:
19 // this does not guarantee that error_code will be in the set of explicitly
20 // listed codes, but it does guarantee that if any of the explicitly listed
21 // errors occur, the correct error_code will be used ]. All functions may
22 // return errc::not_enough_memory if there is not enough memory to complete the
25 //===----------------------------------------------------------------------===//
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/ADT/Twine.h"
29 #include "llvm/Support/DataTypes.h"
30 #include "llvm/Support/system_error.h"
44 /// @name Lexical Component Iterator
47 /// @brief Path iterator.
49 /// This is a bidirectional iterator that iterates over the individual
50 /// components in \a path. The forward traversal order is as follows:
51 /// * The root-name element, if present.
52 /// * The root-directory element, if present.
53 /// * Each successive filename element, if present.
54 /// * Dot, if one or more trailing non-root slash characters are present.
55 /// The backwards traversal order is the reverse of forward traversal.
57 /// Iteration examples. Each component is separated by ',':
61 /// /foo/bar => /,foo,bar
63 /// C:\foo\bar => C:,/,foo,bar
65 class const_iterator {
66 StringRef Path; //< The entire path.
67 StringRef Component; //< The current component.
70 typedef const StringRef value_type;
71 typedef value_type &reference;
72 typedef value_type *pointer;
73 typedef std::bidirectional_iterator_tag iterator_category;
74 reference operator*() const;
75 pointer operator->() const;
76 const_iterator &operator++(); // preincrement
77 const_iterator &operator++(int); // postincrement
78 const_iterator &operator--(); // predecrement
79 const_iterator &operator--(int); // postdecrement
80 bool operator==(const const_iterator &RHS) const;
81 bool operator!=(const const_iterator &RHS) const;
83 const_iterator(); //< Default construct end iterator.
84 const_iterator(const StringRef &path);
88 /// @name Lexical Modifiers
91 /// @brief Make \a path an absolute path.
93 /// Makes \a path absolute using the current directory if it is not already. An
94 /// empty \a path will result in the current directory.
96 /// /absolute/path => /absolute/path
97 /// relative/../path => <current-directory>/path
99 /// @param path A path that is modified to be an absolute path.
100 /// @returns errc::success if \a path has been made absolute, otherwise a
101 /// platform specific error_code.
102 error_code make_absolute(SmallVectorImpl<char> &path);
104 /// @brief Remove the last component from \a path if it exists.
106 /// directory/filename.cpp => directory/
107 /// directory/ => directory/
109 /// @param path A path that is modified to not have a file component.
110 /// @returns errc::success if \a path's file name has been removed (or there was
111 /// not one to begin with), otherwise a platform specific error_code.
112 error_code remove_filename(SmallVectorImpl<char> &path);
114 /// @brief Replace the file extension of \a path with \a extension.
116 /// ./filename.cpp => ./filename.extension
117 /// ./filename => ./filename.extension
118 /// ./ => ? TODO: decide what semantics this has.
120 /// @param path A path that has its extension replaced with \a extension.
121 /// @param extension The extension to be added. It may be empty. It may also
122 /// optionally start with a '.', if it does not, one will be
124 /// @returns errc::success if \a path's extension has been replaced, otherwise a
125 /// platform specific error_code.
126 error_code replace_extension(SmallVectorImpl<char> &path,
127 const Twine &extension);
129 /// @brief Append to path.
131 /// /foo + bar/f => /foo/bar/f
132 /// /foo/ + bar/f => /foo/bar/f
133 /// foo + bar/f => foo/bar/f
135 /// @param path Set to \a path + \a component.
136 /// @param component The component to be appended to \a path.
137 /// @returns errc::success if \a component has been appended to \a path,
138 /// otherwise a platform specific error_code.
139 error_code append(SmallVectorImpl<char> &path, const Twine &component);
141 /// @brief Append to path.
143 /// /foo + [bar,f] => /foo/bar/f
144 /// /foo/ + [bar,f] => /foo/bar/f
145 /// foo + [bar,f] => foo/bar/f
147 /// @param path Set to \a path + [\a begin, \a end).
148 /// @param begin Start of components to append.
149 /// @param end One past the end of components to append.
150 /// @returns errc::success if [\a begin, \a end) has been appended to \a path,
151 /// otherwise a platform specific error_code.
152 error_code append(SmallVectorImpl<char> &path,
153 const_iterator begin, const_iterator end);
156 /// @name Transforms (or some other better name)
159 /// Convert path to the native form. This is used to give paths to users and
160 /// operating system calls in the platform's normal way. For example, on Windows
161 /// all '/' are converted to '\'.
163 /// @param path A path that is transformed to native format.
164 /// @param result Holds the result of the transformation.
165 /// @returns errc::success if \a path has been transformed and stored in result,
166 /// otherwise a platform specific error_code.
167 error_code native(const Twine &path, SmallVectorImpl<char> &result);
170 /// @name Lexical Observers
173 /// @brief Get the current path.
175 /// @param result Holds the current path on return.
176 /// @results errc::success if the current path has been stored in result,
177 /// otherwise a platform specific error_code.
178 error_code current_path(SmallVectorImpl<char> &result);
180 // The following are purely lexical.
182 /// @brief Is the current path valid?
184 /// @param path Input path.
185 /// @param result Set to true if the path is valid, false if it is not.
186 /// @results errc::success if result has been successfully set, otherwise a
187 /// platform specific error_code.
188 error_code is_valid(const Twine &path, bool &result);
190 /// @brief Get root name.
192 /// //net/hello => //net
193 /// c:/hello => c: (on Windows, on other platforms nothing)
194 /// /hello => <empty>
196 /// @param path Input path.
197 /// @param result Set to the root name of \a path if it has one, otherwise "".
198 /// @results errc::success if result has been successfully set, otherwise a
199 /// platform specific error_code.
200 error_code root_name(const StringRef &path, StringRef &result);
202 /// @brief Get root directory.
206 /// d/file.txt => <empty>
208 /// @param path Input path.
209 /// @param result Set to the root directory of \a path if it has one, otherwise
211 /// @results errc::success if result has been successfully set, otherwise a
212 /// platform specific error_code.
213 error_code root_directory(const StringRef &path, StringRef &result);
215 /// @brief Get root path.
217 /// Equivalent to root_name + root_directory.
219 /// @param path Input path.
220 /// @param result Set to the root path of \a path if it has one, otherwise "".
221 /// @results errc::success if result has been successfully set, otherwise a
222 /// platform specific error_code.
223 error_code root_path(const StringRef &path, StringRef &result);
225 /// @brief Get relative path.
227 /// C:\hello\world => hello\world
228 /// foo/bar => foo/bar
229 /// /foo/bar => foo/bar
231 /// @param path Input path.
232 /// @param result Set to the path starting after root_path if one exists,
234 /// @results errc::success if result has been successfully set, otherwise a
235 /// platform specific error_code.
236 error_code relative_path(const StringRef &path, StringRef &result);
238 /// @brief Get parent path.
242 /// foo/../bar => foo/..
244 /// @param path Input path.
245 /// @param result Set to the parent path of \a path if one exists, otherwise "".
246 /// @results errc::success if result has been successfully set, otherwise a
247 /// platform specific error_code.
248 error_code parent_path(const StringRef &path, StringRef &result);
250 /// @brief Get filename.
252 /// /foo.txt => foo.txt
257 /// @param path Input path.
258 /// @param result Set to the filename part of \a path. This is defined as the
259 /// last component of \a path.
260 /// @results errc::success if result has been successfully set, otherwise a
261 /// platform specific error_code.
262 error_code filename(const StringRef &path, StringRef &result);
266 /// If filename contains a dot but not solely one or two dots, result is the
267 /// substring of filename ending at (but not including) the last dot. Otherwise
270 /// /foo/bar.txt => bar
272 /// /foo/.txt => <empty>
276 /// @param path Input path.
277 /// @param result Set to the stem of \a path.
278 /// @results errc::success if result has been successfully set, otherwise a
279 /// platform specific error_code.
280 error_code stem(const StringRef &path, StringRef &result);
282 /// @brief Get extension.
284 /// If filename contains a dot but not solely one or two dots, result is the
285 /// substring of filename starting at (and including) the last dot, and ending
286 /// at the end of \a path. Otherwise "".
288 /// /foo/bar.txt => .txt
289 /// /foo/bar => <empty>
290 /// /foo/.txt => .txt
292 /// @param path Input path.
293 /// @param result Set to the extension of \a path.
294 /// @results errc::success if result has been successfully set, otherwise a
295 /// platform specific error_code.
296 error_code extension(const StringRef &path, StringRef &result);
298 /// @brief Has root name?
302 /// @param path Input path.
303 /// @param result Set to true if the path has a root name, false otherwise.
304 /// @results errc::success if result has been successfully set, otherwise a
305 /// platform specific error_code.
306 error_code has_root_name(const Twine &path, bool &result);
308 /// @brief Has root directory?
310 /// root_directory != ""
312 /// @param path Input path.
313 /// @param result Set to true if the path has a root directory, false otherwise.
314 /// @results errc::success if result has been successfully set, otherwise a
315 /// platform specific error_code.
316 error_code has_root_directory(const Twine &path, bool &result);
318 /// @brief Has root path?
322 /// @param path Input path.
323 /// @param result Set to true if the path has a root path, false otherwise.
324 /// @results errc::success if result has been successfully set, otherwise a
325 /// platform specific error_code.
326 error_code has_root_path(const Twine &path, bool &result);
328 /// @brief Has relative path?
330 /// relative_path != ""
332 /// @param path Input path.
333 /// @param result Set to true if the path has a relative path, false otherwise.
334 /// @results errc::success if result has been successfully set, otherwise a
335 /// platform specific error_code.
336 error_code has_relative_path(const Twine &path, bool &result);
338 /// @brief Has parent path?
340 /// parent_path != ""
342 /// @param path Input path.
343 /// @param result Set to true if the path has a parent path, false otherwise.
344 /// @results errc::success if result has been successfully set, otherwise a
345 /// platform specific error_code.
346 error_code has_parent_path(const Twine &path, bool &result);
348 /// @brief Has filename?
352 /// @param path Input path.
353 /// @param result Set to true if the path has a filename, false otherwise.
354 /// @results errc::success if result has been successfully set, otherwise a
355 /// platform specific error_code.
356 error_code has_filename(const Twine &path, bool &result);
362 /// @param path Input path.
363 /// @param result Set to true if the path has a stem, false otherwise.
364 /// @results errc::success if result has been successfully set, otherwise a
365 /// platform specific error_code.
366 error_code has_stem(const Twine &path, bool &result);
368 /// @brief Has extension?
372 /// @param path Input path.
373 /// @param result Set to true if the path has a extension, false otherwise.
374 /// @results errc::success if result has been successfully set, otherwise a
375 /// platform specific error_code.
376 error_code has_extension(const Twine &path, bool &result);
378 /// @brief Is path absolute?
380 /// @param path Input path.
381 /// @param result Set to true if the path is absolute, false if it is not.
382 /// @results errc::success if result has been successfully set, otherwise a
383 /// platform specific error_code.
384 error_code is_absolute(const Twine &path, bool &result);
386 /// @brief Is path relative?
388 /// @param path Input path.
389 /// @param result Set to true if the path is relative, false if it is not.
390 /// @results errc::success if result has been successfully set, otherwise a
391 /// platform specific error_code.
392 error_code is_relative(const Twine &path, bool &result);
393 // end purely lexical.
395 } // end namespace path
399 /// file_type - An "enum class" enumeration for the file system's view of the
415 file_type(_ v) : v_(v) {}
416 explicit file_type(int v) : v_(_(v)) {}
417 operator int() const {return v_;}
423 /// copy_option - An "enum class" enumeration of copy semantics for copy
431 copy_option(_ v) : v_(v) {}
432 explicit copy_option(int v) : v_(_(v)) {}
433 operator int() const {return v_;}
439 /// space_info - Self explanatory.
446 /// file_status - Represents the result of a call to stat and friends. It has
447 /// a platform specific member to store the result.
450 // implementation defined status field.
452 explicit file_status(file_type v=file_type::status_error);
454 file_type type() const;
455 void type(file_type v);
459 /// @name Physical Operators
462 /// @brief Copy the file at \a from to the path \a to.
464 /// @param from The path to copy the file from.
465 /// @param to The path to copy the file to.
466 /// @param copt Behavior if \a to already exists.
467 /// @returns errc::success if the file has been successfully copied.
468 /// errc::file_exists if \a to already exists and \a copt ==
469 /// copy_option::fail_if_exists. Otherwise a platform specific
471 error_code copy_file(const Twine &from, const Twine &to,
472 copy_option copt = copy_option::fail_if_exists);
474 /// @brief Create all the non-existent directories in path.
476 /// @param path Directories to create.
477 /// @param existed Set to true if \a path already existed, false otherwise.
478 /// @returns errc::success if is_directory(path) and existed have been set,
479 /// otherwise a platform specific error_code.
480 error_code create_directories(const Twine &path, bool &existed);
482 /// @brief Create the directory in path.
484 /// @param path Directory to create.
485 /// @param existed Set to true if \a path already existed, false otherwise.
486 /// @returns errc::success if is_directory(path) and existed have been set,
487 /// otherwise a platform specific error_code.
488 error_code create_directory(const Twine &path, bool &existed);
490 /// @brief Create a hard link from \a from to \a to.
492 /// @param to The path to hard link to.
493 /// @param from The path to hard link from. This is created.
494 /// @returns errc::success if exists(to) && exists(from) && equivalent(to, from)
495 /// , otherwise a platform specific error_code.
496 error_code create_hard_link(const Twine &to, const Twine &from);
498 /// @brief Create a symbolic link from \a from to \a to.
500 /// @param to The path to symbolically link to.
501 /// @param from The path to symbolically link from. This is created.
502 /// @returns errc::success if exists(to) && exists(from) && is_symlink(from),
503 /// otherwise a platform specific error_code.
504 error_code create_symlink(const Twine &to, const Twine &from);
506 /// @brief Remove path. Equivalent to POSIX remove().
508 /// @param path Input path.
509 /// @param existed Set to true if \a path existed, false if it did not.
510 /// undefined otherwise.
511 /// @results errc::success if path has been removed and existed has been
512 /// successfully set, otherwise a platform specific error_code.
513 error_code remove(const Twine &path, bool &existed);
515 /// @brief Recursively remove all files below \a path, then \a path. Files are
516 /// removed as if by POSIX remove().
518 /// @param path Input path.
519 /// @param num_removed Number of files removed.
520 /// @results errc::success if path has been removed and num_removed has been
521 /// successfully set, otherwise a platform specific error_code.
522 error_code remove_all(const Twine &path, uint32_t &num_removed);
524 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
526 /// @param from The path to rename from.
527 /// @param to The path to rename to. This is created.
528 error_code rename(const Twine &from, const Twine &to);
530 /// @brief Resize path to size. File is resized as if by POSIX truncate().
532 /// @param path Input path.
533 /// @param size Size to resize to.
534 /// @returns errc::success if \a path has been resized to \a size, otherwise a
535 /// platform specific error_code.
536 error_code resize_file(const Twine &path, uint64_t size);
538 /// @brief Make file readable.
540 /// @param path Input path.
541 /// @param value If true, make readable, else, make unreadable.
542 /// @results errc::success if readability has been successfully set, otherwise a
543 /// platform specific error_code.
544 error_code set_read(const Twine &path, bool value);
546 /// @brief Make file writeable.
548 /// @param path Input path.
549 /// @param value If true, make writeable, else, make unwriteable.
550 /// @results errc::success if writeability has been successfully set, otherwise
551 /// a platform specific error_code.
552 error_code set_write(const Twine &path, bool value);
554 /// @brief Make file executable.
556 /// @param path Input path.
557 /// @param value If true, make executable, else, make unexecutable.
558 /// @results errc::success if executability has been successfully set, otherwise
559 /// a platform specific error_code.
560 error_code set_execute(const Twine &path, bool value);
563 /// @name Physical Observers
566 /// @brief Does file exist?
568 /// @param status A file_status previously returned from stat.
569 /// @param result Set to true if the file represented by status exists, false if
570 /// it does not. Undefined otherwise.
571 /// @results errc::success if result has been successfully set, otherwise a
572 /// platform specific error_code.
573 error_code exists(file_status status, bool &result);
575 /// @brief Does file exist?
577 /// @param path Input path.
578 /// @param result Set to true if the file represented by status exists, false if
579 /// it does not. Undefined otherwise.
580 /// @results errc::success if result has been successfully set, otherwise a
581 /// platform specific error_code.
582 error_code exists(const Twine &path, bool &result);
584 /// @brief Do paths represent the same thing?
586 /// @param A Input path A.
587 /// @param B Input path B.
588 /// @param result Set to true if stat(A) and stat(B) have the same device and
589 /// inode (or equivalent).
590 /// @results errc::success if result has been successfully set, otherwise a
591 /// platform specific error_code.
592 error_code equivalent(const Twine &A, const Twine &B, bool &result);
594 /// @brief Get file size.
596 /// @param path Input path.
597 /// @param result Set to the size of the file in \a path.
598 /// @returns errc::success if result has been successfully set, otherwise a
599 /// platform specific error_code.
600 error_code file_size(const Twine &path, uint64_t &result);
602 /// @brief Does status represent a directory?
604 /// @param status A file_status previously returned from stat.
605 /// @param result Set to true if the file represented by status is a directory,
606 /// false if it is not. Undefined otherwise.
607 /// @results errc::success if result has been successfully set, otherwise a
608 /// platform specific error_code.
609 error_code is_directory(file_status status, bool &result);
611 /// @brief Is path a directory?
613 /// @param path Input path.
614 /// @param result Set to true if \a path is a directory, false if it is not.
615 /// Undefined otherwise.
616 /// @results errc::success if result has been successfully set, otherwise a
617 /// platform specific error_code.
618 error_code is_directory(const Twine &path, bool &result);
620 /// @brief Is path an empty file?
622 /// @param path Input path.
623 /// @param result Set to true if \a path is a an empty file, false if it is not.
624 /// Undefined otherwise.
625 /// @results errc::success if result has been successfully set, otherwise a
626 /// platform specific error_code.
627 error_code is_empty(const Twine &path, bool &result);
629 /// @brief Does status represent a regular file?
631 /// @param status A file_status previously returned from stat.
632 /// @param result Set to true if the file represented by status is a regular
633 /// file, false if it is not. Undefined otherwise.
634 /// @results errc::success if result has been successfully set, otherwise a
635 /// platform specific error_code.
636 error_code is_regular_file(file_status status, bool &result);
638 /// @brief Is path a regular file?
640 /// @param path Input path.
641 /// @param result Set to true if \a path is a regular file, false if it is not.
642 /// Undefined otherwise.
643 /// @results errc::success if result has been successfully set, otherwise a
644 /// platform specific error_code.
645 error_code is_regular_file(const Twine &path, bool &result);
647 /// @brief Does status represent something that exists but is not a directory,
648 /// regular file, or symlink?
650 /// @param status A file_status previously returned from stat.
651 /// @param result Set to true if the file represented by status exists, but is
652 /// not a directory, regular file, or a symlink, false if it does
653 /// not. Undefined otherwise.
654 /// @results errc::success if result has been successfully set, otherwise a
655 /// platform specific error_code.
656 error_code is_other(file_status status, bool &result);
658 /// @brief Is path something that exists but is not a directory,
659 /// regular file, or symlink?
661 /// @param path Input path.
662 /// @param result Set to true if \a path exists, but is not a directory, regular
663 /// file, or a symlink, false if it does not. Undefined otherwise.
664 /// @results errc::success if result has been successfully set, otherwise a
665 /// platform specific error_code.
666 error_code is_other(const Twine &path, bool &result);
668 /// @brief Does status represent a symlink?
670 /// @param status A file_status previously returned from stat.
671 /// @param result Set to true if the file represented by status is a symlink,
672 /// false if it is not. Undefined otherwise.
673 /// @results errc::success if result has been successfully set, otherwise a
674 /// platform specific error_code.
675 error_code is_symlink(file_status status, bool &result);
677 /// @brief Is path a symlink?
679 /// @param path Input path.
680 /// @param result Set to true if \a path is a symlink, false if it is not.
681 /// Undefined otherwise.
682 /// @results errc::success if result has been successfully set, otherwise a
683 /// platform specific error_code.
684 error_code is_symlink(const Twine &path, bool &result);
686 /// @brief Get last write time without changing it.
688 /// @param path Input path.
689 /// @param result Set to the last write time (UNIX time) of \a path if it
691 /// @results errc::success if result has been successfully set, otherwise a
692 /// platform specific error_code.
693 error_code last_write_time(const Twine &path, std::time_t &result);
695 /// @brief Set last write time.
697 /// @param path Input path.
698 /// @param value Time to set (UNIX time) \a path's last write time to.
699 /// @results errc::success if result has been successfully set, otherwise a
700 /// platform specific error_code.
701 error_code set_last_write_time(const Twine &path, std::time_t value);
703 /// @brief Read a symlink's value.
705 /// @param path Input path.
706 /// @param result Set to the value of the symbolic link \a path.
707 /// @results errc::success if result has been successfully set, otherwise a
708 /// platform specific error_code.
709 error_code read_symlink(const Twine &path, SmallVectorImpl<char> &result);
711 /// @brief Get disk space usage information.
713 /// @param path Input path.
714 /// @param result Set to the capacity, free, and available space on the device
716 /// @results errc::success if result has been successfully set, otherwise a
717 /// platform specific error_code.
718 error_code disk_space(const Twine &path, space_info &result);
720 /// @brief Get file status as if by POSIX stat().
722 /// @param path Input path.
723 /// @param result Set to the file status.
724 /// @results errc::success if result has been successfully set, otherwise a
725 /// platform specific error_code.
726 error_code status(const Twine &path, file_status &result);
728 /// @brief Is status available?
730 /// @param path Input path.
731 /// @param result Set to true if status() != status_error.
732 /// @results errc::success if result has been successfully set, otherwise a
733 /// platform specific error_code.
734 error_code status_known(const Twine &path, bool &result);
736 /// @brief Get file status as if by POSIX lstat().
738 /// Does not resolve symlinks.
740 /// @param path Input path.
741 /// @param result Set to the file status.
742 /// @results errc::success if result has been successfully set, otherwise a
743 /// platform specific error_code.
744 error_code symlink_status(const Twine &path, file_status &result);
746 /// @brief Get the temporary directory.
748 /// @param result Set to the temporary directory.
749 /// @results errc::success if result has been successfully set, otherwise a
750 /// platform specific error_code.
752 error_code temp_directory_path(SmallVectorImpl<char> &result);
754 /// @brief Generate a unique path and open it as a file.
756 /// Generates a unique path suitable for a temporary file and then opens it as a
757 /// file. The name is based on \a model with '%' replaced by a random char in
760 /// This is an atomic operation. Either the file is created and opened, or the
761 /// file system is left untouched.
763 /// clang-%%-%%-%%-%%-%%.s => <current-directory>/clang-a0-b1-c2-d3-e4.s
765 /// @param model Name to base unique path off of.
766 /// @param result Set to the opened file.
767 /// @results errc::success if result has been successfully set, otherwise a
768 /// platform specific error_code.
769 /// @see temp_directory_path
770 error_code unique_file(const Twine &model, void* i_have_not_decided_the_ty_yet);
772 /// @brief Canonicalize path.
774 /// Sets result to the file system's idea of what path is. The result is always
775 /// absolute and has the same capitalization as the file system.
777 /// @param path Input path.
778 /// @param result Set to the canonicalized version of \a path.
779 /// @results errc::success if result has been successfully set, otherwise a
780 /// platform specific error_code.
781 error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result);
783 /// @brief Are \a path's first bytes \a magic?
785 /// @param path Input path.
786 /// @param magic Byte sequence to compare \a path's first len(magic) bytes to.
787 /// @results errc::success if result has been successfully set, otherwise a
788 /// platform specific error_code.
789 error_code has_magic(const Twine &path, const Twine &magic);
791 /// @brief Get \a path's first \a len bytes.
793 /// @param path Input path.
794 /// @param len Number of magic bytes to get.
795 /// @param result Set to the first \a len bytes in the file pointed to by
797 /// @results errc::success if result has been successfully set, otherwise a
798 /// platform specific error_code.
799 error_code get_magic(const Twine &path, uint32_t len,
800 SmallVectorImpl<char> &result);
802 /// @brief Is file bitcode?
804 /// @param path Input path.
805 /// @param result Set to true if \a path is a bitcode file, false if it is not,
806 /// undefined otherwise.
807 /// @results errc::success if result has been successfully set, otherwise a
808 /// platform specific error_code.
809 error_code is_bitcode(const Twine &path, bool &result);
811 /// @brief Is file a dynamic library?
813 /// @param path Input path.
814 /// @param result Set to true if \a path is a dynamic library, false if it is
815 /// not, undefined otherwise.
816 /// @results errc::success if result has been successfully set, otherwise a
817 /// platform specific error_code.
818 error_code is_dynamic_library(const Twine &path, bool &result);
820 /// @brief Is an object file?
822 /// @param path Input path.
823 /// @param result Set to true if \a path is an object file, false if it is not,
824 /// undefined otherwise.
825 /// @results errc::success if result has been successfully set, otherwise a
826 /// platform specific error_code.
827 error_code is_object_file(const Twine &path, bool &result);
829 /// @brief Can file be read?
831 /// @param path Input path.
832 /// @param result Set to true if \a path is readable, false it it is not,
833 /// undefined otherwise.
834 /// @results errc::success if result has been successfully set, otherwise a
835 /// platform specific error_code.
836 error_code can_read(const Twine &path, bool &result);
838 /// @brief Can file be written?
840 /// @param path Input path.
841 /// @param result Set to true if \a path is writeable, false it it is not,
842 /// undefined otherwise.
843 /// @results errc::success if result has been successfully set, otherwise a
844 /// platform specific error_code.
845 error_code can_write(const Twine &path, bool &result);
847 /// @brief Can file be executed?
849 /// @param path Input path.
850 /// @param result Set to true if \a path is executable, false it it is not,
851 /// undefined otherwise.
852 /// @results errc::success if result has been successfully set, otherwise a
853 /// platform specific error_code.
854 error_code can_execute(const Twine &path, bool &result);
856 /// @brief Get library paths the system linker uses.
858 /// @param result Set to the list of system library paths.
859 /// @results errc::success if result has been successfully set, otherwise a
860 /// platform specific error_code.
861 error_code GetSystemLibraryPaths(SmallVectorImpl<std::string> &result);
863 /// @brief Get bitcode library paths the system linker uses
864 /// + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
866 /// @param result Set to the list of bitcode library paths.
867 /// @results errc::success if result has been successfully set, otherwise a
868 /// platform specific error_code.
869 error_code GetBitcodeLibraryPaths(SmallVectorImpl<std::string> &result);
871 /// @brief Find a library.
873 /// Find the path to a library using its short name. Use the system
874 /// dependent library paths to locate the library.
876 /// c => /usr/lib/libc.so
878 /// @param short_name Library name one would give to the system linker.
879 /// @param result Set to the absolute path \a short_name represents.
880 /// @results errc::success if result has been successfully set, otherwise a
881 /// platform specific error_code.
882 error_code FindLibrary(const Twine &short_name, SmallVectorImpl<char> &result);
884 /// @brief Get absolute path of main executable.
886 /// @param argv0 The program name as it was spelled on the command line.
887 /// @param MainAddr Address of some symbol in the executable (not in a library).
888 /// @param result Set to the absolute path of the current executable.
889 /// @results errc::success if result has been successfully set, otherwise a
890 /// platform specific error_code.
891 error_code GetMainExecutable(const char *argv0, void *MainAddr,
892 SmallVectorImpl<char> &result);
898 /// directory_entry - A single entry in a directory. Caches the status either
899 /// from the result of the iteration syscall, or the first time status or
900 /// symlink_status is called.
901 class directory_entry {
903 mutable file_status Status;
904 mutable file_status SymlinkStatus;
907 explicit directory_entry(const Twine &path, file_status st = file_status(),
908 file_status symlink_st = file_status());
910 void assign(const Twine &path, file_status st = file_status(),
911 file_status symlink_st = file_status());
912 void replace_filename(const Twine &filename, file_status st = file_status(),
913 file_status symlink_st = file_status());
915 const SmallVectorImpl<char> &path() const;
916 error_code status(file_status &result) const;
917 error_code symlink_status(file_status &result) const;
919 bool operator==(const directory_entry& rhs) const;
920 bool operator!=(const directory_entry& rhs) const;
921 bool operator< (const directory_entry& rhs) const;
922 bool operator<=(const directory_entry& rhs) const;
923 bool operator> (const directory_entry& rhs) const;
924 bool operator>=(const directory_entry& rhs) const;
927 /// directory_iterator - Iterates through the entries in path. There is no
928 /// operator++ because we need an error_code. If it's really needed we can make
929 /// it call report_fatal_error on error.
930 class directory_iterator {
931 // implementation directory iterator status
934 explicit directory_iterator(const Twine &path, error_code &ec);
935 // No operator++ because we need error_code.
936 directory_iterator &increment(error_code &ec);
938 const directory_entry &operator*() const;
939 const directory_entry *operator->() const;
941 // Other members as required by
942 // C++ Std, 24.1.1 Input iterators [input.iterators]
945 /// recursive_directory_iterator - Same as directory_iterator except for it
946 /// recurses down into child directories.
947 class recursive_directory_iterator {
949 bool HasNoPushRequest;
950 // implementation directory iterator status
953 explicit recursive_directory_iterator(const Twine &path, error_code &ec);
954 // No operator++ because we need error_code.
955 directory_iterator &increment(error_code &ec);
957 const directory_entry &operator*() const;
958 const directory_entry *operator->() const;
961 /// Gets the current level. path is at level 0.
963 /// Returns true if no_push has been called for this directory_entry.
964 bool no_push_request() const;
967 /// Goes up one level if Level > 0.
969 /// Does not go down into the current directory_entry.
972 // Other members as required by
973 // C++ Std, 24.1.1 Input iterators [input.iterators]
978 } // end namespace fs
979 } // end namespace sys
980 } // end namespace llvm