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,fs} namespaces. 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 #ifndef LLVM_SYSTEM_PATHV2_H
28 #define LLVM_SYSTEM_PATHV2_H
30 #include "llvm/ADT/SmallString.h"
31 #include "llvm/ADT/Twine.h"
32 #include "llvm/Support/DataTypes.h"
33 #include "llvm/Support/system_error.h"
47 /// @name Lexical Component Iterator
50 /// @brief Path iterator.
52 /// This is a bidirectional iterator that iterates over the individual
53 /// components in \a path. The forward traversal order is as follows:
54 /// * The root-name element, if present.
55 /// * The root-directory element, if present.
56 /// * Each successive filename element, if present.
57 /// * Dot, if one or more trailing non-root slash characters are present.
58 /// The backwards traversal order is the reverse of forward traversal.
60 /// Iteration examples. Each component is separated by ',':
64 /// /foo/bar => /,foo,bar
66 /// C:\foo\bar => C:,/,foo,bar
68 class const_iterator {
69 StringRef Path; //< The entire path.
70 StringRef Component; //< The current component. Not necessarily in Path.
71 size_t Position; //< The iterators current position within Path.
73 // An end iterator has Position = Path.size() + 1.
74 friend const_iterator begin(const StringRef &path);
75 friend const_iterator end(const StringRef &path);
78 typedef const StringRef value_type;
79 typedef ptrdiff_t difference_type;
80 typedef value_type &reference;
81 typedef value_type *pointer;
82 typedef std::bidirectional_iterator_tag iterator_category;
84 reference operator*() const;
85 pointer operator->() const;
86 const_iterator &operator++(); // preincrement
87 const_iterator &operator++(int); // postincrement
88 const_iterator &operator--(); // predecrement
89 const_iterator &operator--(int); // postdecrement
90 bool operator==(const const_iterator &RHS) const;
91 bool operator!=(const const_iterator &RHS) const;
94 /// @brief Get begin iterator over \a path.
95 /// @param path Input path.
96 /// @returns Iterator initialized with the first component of \a path.
97 const_iterator begin(const StringRef &path);
99 /// @brief Get end iterator over \a path.
100 /// @param path Input path.
101 /// @returns Iterator initialized to the end of \a path.
102 const_iterator end(const StringRef &path);
105 /// @name Lexical Modifiers
108 /// @brief Make \a path an absolute path.
110 /// Makes \a path absolute using the current directory if it is not already. An
111 /// empty \a path will result in the current directory.
113 /// /absolute/path => /absolute/path
114 /// relative/../path => <current-directory>/path
116 /// @param path A path that is modified to be an absolute path.
117 /// @returns errc::success if \a path has been made absolute, otherwise a
118 /// platform specific error_code.
119 error_code make_absolute(SmallVectorImpl<char> &path);
121 /// @brief Remove the last component from \a path if it exists.
123 /// directory/filename.cpp => directory/
124 /// directory/ => directory/
126 /// @param path A path that is modified to not have a file component.
127 /// @returns errc::success if \a path's file name has been removed (or there was
128 /// not one to begin with), otherwise a platform specific error_code.
129 error_code remove_filename(SmallVectorImpl<char> &path);
131 /// @brief Replace the file extension of \a path with \a extension.
133 /// ./filename.cpp => ./filename.extension
134 /// ./filename => ./filename.extension
135 /// ./ => ? TODO: decide what semantics this has.
137 /// @param path A path that has its extension replaced with \a extension.
138 /// @param extension The extension to be added. It may be empty. It may also
139 /// optionally start with a '.', if it does not, one will be
141 /// @returns errc::success if \a path's extension has been replaced, otherwise a
142 /// platform specific error_code.
143 error_code replace_extension(SmallVectorImpl<char> &path,
144 const Twine &extension);
146 /// @brief Append to path.
148 /// /foo + bar/f => /foo/bar/f
149 /// /foo/ + bar/f => /foo/bar/f
150 /// foo + bar/f => foo/bar/f
152 /// @param path Set to \a path + \a component.
153 /// @param component The component to be appended to \a path.
154 /// @returns errc::success if \a component has been appended to \a path,
155 /// otherwise a platform specific error_code.
156 error_code append(SmallVectorImpl<char> &path, const Twine &a,
159 const Twine &d = "");
161 /// @brief Append to path.
163 /// /foo + [bar,f] => /foo/bar/f
164 /// /foo/ + [bar,f] => /foo/bar/f
165 /// foo + [bar,f] => foo/bar/f
167 /// @param path Set to \a path + [\a begin, \a end).
168 /// @param begin Start of components to append.
169 /// @param end One past the end of components to append.
170 /// @returns errc::success if [\a begin, \a end) has been appended to \a path,
171 /// otherwise a platform specific error_code.
172 error_code append(SmallVectorImpl<char> &path,
173 const_iterator begin, const_iterator end);
176 /// @name Transforms (or some other better name)
179 /// Convert path to the native form. This is used to give paths to users and
180 /// operating system calls in the platform's normal way. For example, on Windows
181 /// all '/' are converted to '\'.
183 /// @param path A path that is transformed to native format.
184 /// @param result Holds the result of the transformation.
185 /// @returns errc::success if \a path has been transformed and stored in result,
186 /// otherwise a platform specific error_code.
187 error_code native(const Twine &path, SmallVectorImpl<char> &result);
190 /// @name Lexical Observers
193 /// @brief Get the current path.
195 /// @param result Holds the current path on return.
196 /// @results errc::success if the current path has been stored in result,
197 /// otherwise a platform specific error_code.
198 error_code current_path(SmallVectorImpl<char> &result);
200 // The following are purely lexical.
202 /// @brief Is the current path valid?
204 /// @param path Input path.
205 /// @param result Set to true if the path is valid, false if it is not.
206 /// @results errc::success if result has been successfully set, otherwise a
207 /// platform specific error_code.
208 error_code is_valid(const Twine &path, bool &result);
210 /// @brief Get root name.
212 /// //net/hello => //net
213 /// c:/hello => c: (on Windows, on other platforms nothing)
214 /// /hello => <empty>
216 /// @param path Input path.
217 /// @param result Set to the root name of \a path if it has one, otherwise "".
218 /// @results errc::success if result has been successfully set, otherwise a
219 /// platform specific error_code.
220 error_code root_name(const StringRef &path, StringRef &result);
222 /// @brief Get root directory.
226 /// d/file.txt => <empty>
228 /// @param path Input path.
229 /// @param result Set to the root directory of \a path if it has one, otherwise
231 /// @results errc::success if result has been successfully set, otherwise a
232 /// platform specific error_code.
233 error_code root_directory(const StringRef &path, StringRef &result);
235 /// @brief Get root path.
237 /// Equivalent to root_name + root_directory.
239 /// @param path Input path.
240 /// @param result Set to the root path of \a path if it has one, otherwise "".
241 /// @results errc::success if result has been successfully set, otherwise a
242 /// platform specific error_code.
243 error_code root_path(const StringRef &path, StringRef &result);
245 /// @brief Get relative path.
247 /// C:\hello\world => hello\world
248 /// foo/bar => foo/bar
249 /// /foo/bar => foo/bar
251 /// @param path Input path.
252 /// @param result Set to the path starting after root_path if one exists,
254 /// @results errc::success if result has been successfully set, otherwise a
255 /// platform specific error_code.
256 error_code relative_path(const StringRef &path, StringRef &result);
258 /// @brief Get parent path.
262 /// foo/../bar => foo/..
264 /// @param path Input path.
265 /// @param result Set to the parent path of \a path if one exists, otherwise "".
266 /// @results errc::success if result has been successfully set, otherwise a
267 /// platform specific error_code.
268 error_code parent_path(const StringRef &path, StringRef &result);
270 /// @brief Get filename.
272 /// /foo.txt => foo.txt
277 /// @param path Input path.
278 /// @param result Set to the filename part of \a path. This is defined as the
279 /// last component of \a path.
280 /// @results errc::success if result has been successfully set, otherwise a
281 /// platform specific error_code.
282 error_code filename(const StringRef &path, StringRef &result);
286 /// If filename contains a dot but not solely one or two dots, result is the
287 /// substring of filename ending at (but not including) the last dot. Otherwise
290 /// /foo/bar.txt => bar
292 /// /foo/.txt => <empty>
296 /// @param path Input path.
297 /// @param result Set to the stem of \a path.
298 /// @results errc::success if result has been successfully set, otherwise a
299 /// platform specific error_code.
300 error_code stem(const StringRef &path, StringRef &result);
302 /// @brief Get extension.
304 /// If filename contains a dot but not solely one or two dots, result is the
305 /// substring of filename starting at (and including) the last dot, and ending
306 /// at the end of \a path. Otherwise "".
308 /// /foo/bar.txt => .txt
309 /// /foo/bar => <empty>
310 /// /foo/.txt => .txt
312 /// @param path Input path.
313 /// @param result Set to the extension of \a path.
314 /// @results errc::success if result has been successfully set, otherwise a
315 /// platform specific error_code.
316 error_code extension(const StringRef &path, StringRef &result);
318 /// @brief Has root name?
322 /// @param path Input path.
323 /// @param result Set to true if the path has a root name, false otherwise.
324 /// @results errc::success if result has been successfully set, otherwise a
325 /// platform specific error_code.
326 error_code has_root_name(const Twine &path, bool &result);
328 /// @brief Has root directory?
330 /// root_directory != ""
332 /// @param path Input path.
333 /// @param result Set to true if the path has a root directory, false otherwise.
334 /// @results errc::success if result has been successfully set, otherwise a
335 /// platform specific error_code.
336 error_code has_root_directory(const Twine &path, bool &result);
338 /// @brief Has root path?
342 /// @param path Input path.
343 /// @param result Set to true if the path has a root path, false otherwise.
344 /// @results errc::success if result has been successfully set, otherwise a
345 /// platform specific error_code.
346 error_code has_root_path(const Twine &path, bool &result);
348 /// @brief Has relative path?
350 /// relative_path != ""
352 /// @param path Input path.
353 /// @param result Set to true if the path has a relative path, false otherwise.
354 /// @results errc::success if result has been successfully set, otherwise a
355 /// platform specific error_code.
356 error_code has_relative_path(const Twine &path, bool &result);
358 /// @brief Has parent path?
360 /// parent_path != ""
362 /// @param path Input path.
363 /// @param result Set to true if the path has a parent path, false otherwise.
364 /// @results errc::success if result has been successfully set, otherwise a
365 /// platform specific error_code.
366 error_code has_parent_path(const Twine &path, bool &result);
368 /// @brief Has filename?
372 /// @param path Input path.
373 /// @param result Set to true if the path has a filename, false otherwise.
374 /// @results errc::success if result has been successfully set, otherwise a
375 /// platform specific error_code.
376 error_code has_filename(const Twine &path, bool &result);
382 /// @param path Input path.
383 /// @param result Set to true if the path has a stem, false otherwise.
384 /// @results errc::success if result has been successfully set, otherwise a
385 /// platform specific error_code.
386 error_code has_stem(const Twine &path, bool &result);
388 /// @brief Has extension?
392 /// @param path Input path.
393 /// @param result Set to true if the path has a extension, false otherwise.
394 /// @results errc::success if result has been successfully set, otherwise a
395 /// platform specific error_code.
396 error_code has_extension(const Twine &path, bool &result);
398 /// @brief Is path absolute?
400 /// @param path Input path.
401 /// @param result Set to true if the path is absolute, false if it is not.
402 /// @results errc::success if result has been successfully set, otherwise a
403 /// platform specific error_code.
404 error_code is_absolute(const Twine &path, bool &result);
406 /// @brief Is path relative?
408 /// @param path Input path.
409 /// @param result Set to true if the path is relative, false if it is not.
410 /// @results errc::success if result has been successfully set, otherwise a
411 /// platform specific error_code.
412 error_code is_relative(const Twine &path, bool &result);
413 // end purely lexical.
415 } // end namespace path
419 /// file_type - An "enum class" enumeration for the file system's view of the
435 file_type(_ v) : v_(v) {}
436 explicit file_type(int v) : v_(_(v)) {}
437 operator int() const {return v_;}
443 /// copy_option - An "enum class" enumeration of copy semantics for copy
451 copy_option(_ v) : v_(v) {}
452 explicit copy_option(int v) : v_(_(v)) {}
453 operator int() const {return v_;}
459 /// space_info - Self explanatory.
466 /// file_status - Represents the result of a call to stat and friends. It has
467 /// a platform specific member to store the result.
470 // implementation defined status field.
472 explicit file_status(file_type v=file_type::status_error);
474 file_type type() const;
475 void type(file_type v);
479 /// @name Physical Operators
482 /// @brief Copy the file at \a from to the path \a to.
484 /// @param from The path to copy the file from.
485 /// @param to The path to copy the file to.
486 /// @param copt Behavior if \a to already exists.
487 /// @returns errc::success if the file has been successfully copied.
488 /// errc::file_exists if \a to already exists and \a copt ==
489 /// copy_option::fail_if_exists. Otherwise a platform specific
491 error_code copy_file(const Twine &from, const Twine &to,
492 copy_option copt = copy_option::fail_if_exists);
494 /// @brief Create all the non-existent directories in path.
496 /// @param path Directories to create.
497 /// @param existed Set to true if \a path already existed, false otherwise.
498 /// @returns errc::success if is_directory(path) and existed have been set,
499 /// otherwise a platform specific error_code.
500 error_code create_directories(const Twine &path, bool &existed);
502 /// @brief Create the directory in path.
504 /// @param path Directory to create.
505 /// @param existed Set to true if \a path already existed, false otherwise.
506 /// @returns errc::success if is_directory(path) and existed have been set,
507 /// otherwise a platform specific error_code.
508 error_code create_directory(const Twine &path, bool &existed);
510 /// @brief Create a hard link from \a from to \a to.
512 /// @param to The path to hard link to.
513 /// @param from The path to hard link from. This is created.
514 /// @returns errc::success if exists(to) && exists(from) && equivalent(to, from)
515 /// , otherwise a platform specific error_code.
516 error_code create_hard_link(const Twine &to, const Twine &from);
518 /// @brief Create a symbolic link from \a from to \a to.
520 /// @param to The path to symbolically link to.
521 /// @param from The path to symbolically link from. This is created.
522 /// @returns errc::success if exists(to) && exists(from) && is_symlink(from),
523 /// otherwise a platform specific error_code.
524 error_code create_symlink(const Twine &to, const Twine &from);
526 /// @brief Remove path. Equivalent to POSIX remove().
528 /// @param path Input path.
529 /// @param existed Set to true if \a path existed, false if it did not.
530 /// undefined otherwise.
531 /// @results errc::success if path has been removed and existed has been
532 /// successfully set, otherwise a platform specific error_code.
533 error_code remove(const Twine &path, bool &existed);
535 /// @brief Recursively remove all files below \a path, then \a path. Files are
536 /// removed as if by POSIX remove().
538 /// @param path Input path.
539 /// @param num_removed Number of files removed.
540 /// @results errc::success if path has been removed and num_removed has been
541 /// successfully set, otherwise a platform specific error_code.
542 error_code remove_all(const Twine &path, uint32_t &num_removed);
544 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
546 /// @param from The path to rename from.
547 /// @param to The path to rename to. This is created.
548 error_code rename(const Twine &from, const Twine &to);
550 /// @brief Resize path to size. File is resized as if by POSIX truncate().
552 /// @param path Input path.
553 /// @param size Size to resize to.
554 /// @returns errc::success if \a path has been resized to \a size, otherwise a
555 /// platform specific error_code.
556 error_code resize_file(const Twine &path, uint64_t size);
558 /// @brief Make file readable.
560 /// @param path Input path.
561 /// @param value If true, make readable, else, make unreadable.
562 /// @results errc::success if readability has been successfully set, otherwise a
563 /// platform specific error_code.
564 error_code set_read(const Twine &path, bool value);
566 /// @brief Make file writeable.
568 /// @param path Input path.
569 /// @param value If true, make writeable, else, make unwriteable.
570 /// @results errc::success if writeability has been successfully set, otherwise
571 /// a platform specific error_code.
572 error_code set_write(const Twine &path, bool value);
574 /// @brief Make file executable.
576 /// @param path Input path.
577 /// @param value If true, make executable, else, make unexecutable.
578 /// @results errc::success if executability has been successfully set, otherwise
579 /// a platform specific error_code.
580 error_code set_execute(const Twine &path, bool value);
583 /// @name Physical Observers
586 /// @brief Does file exist?
588 /// @param status A file_status previously returned from stat.
589 /// @param result Set to true if the file represented by status exists, false if
590 /// it does not. Undefined otherwise.
591 /// @results errc::success if result has been successfully set, otherwise a
592 /// platform specific error_code.
593 error_code exists(file_status status, bool &result);
595 /// @brief Does file exist?
597 /// @param path Input path.
598 /// @param result Set to true if the file represented by status exists, false if
599 /// it does not. Undefined otherwise.
600 /// @results errc::success if result has been successfully set, otherwise a
601 /// platform specific error_code.
602 error_code exists(const Twine &path, bool &result);
604 /// @brief Do paths represent the same thing?
606 /// @param A Input path A.
607 /// @param B Input path B.
608 /// @param result Set to true if stat(A) and stat(B) have the same device and
609 /// inode (or equivalent).
610 /// @results errc::success if result has been successfully set, otherwise a
611 /// platform specific error_code.
612 error_code equivalent(const Twine &A, const Twine &B, bool &result);
614 /// @brief Get file size.
616 /// @param path Input path.
617 /// @param result Set to the size of the file in \a path.
618 /// @returns errc::success if result has been successfully set, otherwise a
619 /// platform specific error_code.
620 error_code file_size(const Twine &path, uint64_t &result);
622 /// @brief Does status represent a directory?
624 /// @param status A file_status previously returned from stat.
625 /// @param result Set to true if the file represented by status is a directory,
626 /// false if it is not. Undefined otherwise.
627 /// @results errc::success if result has been successfully set, otherwise a
628 /// platform specific error_code.
629 error_code is_directory(file_status status, bool &result);
631 /// @brief Is path a directory?
633 /// @param path Input path.
634 /// @param result Set to true if \a path is a directory, false if it is not.
635 /// Undefined otherwise.
636 /// @results errc::success if result has been successfully set, otherwise a
637 /// platform specific error_code.
638 error_code is_directory(const Twine &path, bool &result);
640 /// @brief Is path an empty file?
642 /// @param path Input path.
643 /// @param result Set to true if \a path is a an empty file, false if it is not.
644 /// Undefined otherwise.
645 /// @results errc::success if result has been successfully set, otherwise a
646 /// platform specific error_code.
647 error_code is_empty(const Twine &path, bool &result);
649 /// @brief Does status represent a regular file?
651 /// @param status A file_status previously returned from stat.
652 /// @param result Set to true if the file represented by status is a regular
653 /// file, false if it is not. Undefined otherwise.
654 /// @results errc::success if result has been successfully set, otherwise a
655 /// platform specific error_code.
656 error_code is_regular_file(file_status status, bool &result);
658 /// @brief Is path a regular file?
660 /// @param path Input path.
661 /// @param result Set to true if \a path is a regular file, false if it is not.
662 /// Undefined otherwise.
663 /// @results errc::success if result has been successfully set, otherwise a
664 /// platform specific error_code.
665 error_code is_regular_file(const Twine &path, bool &result);
667 /// @brief Does status represent something that exists but is not a directory,
668 /// regular file, or symlink?
670 /// @param status A file_status previously returned from stat.
671 /// @param result Set to true if the file represented by status exists, but is
672 /// not a directory, regular file, or a symlink, false if it does
673 /// not. Undefined otherwise.
674 /// @results errc::success if result has been successfully set, otherwise a
675 /// platform specific error_code.
676 error_code is_other(file_status status, bool &result);
678 /// @brief Is path something that exists but is not a directory,
679 /// regular file, or symlink?
681 /// @param path Input path.
682 /// @param result Set to true if \a path exists, but is not a directory, regular
683 /// file, or a symlink, false if it does not. Undefined otherwise.
684 /// @results errc::success if result has been successfully set, otherwise a
685 /// platform specific error_code.
686 error_code is_other(const Twine &path, bool &result);
688 /// @brief Does status represent a symlink?
690 /// @param status A file_status previously returned from stat.
691 /// @param result Set to true if the file represented by status is a symlink,
692 /// false if it is not. Undefined otherwise.
693 /// @results errc::success if result has been successfully set, otherwise a
694 /// platform specific error_code.
695 error_code is_symlink(file_status status, bool &result);
697 /// @brief Is path a symlink?
699 /// @param path Input path.
700 /// @param result Set to true if \a path is a symlink, false if it is not.
701 /// Undefined otherwise.
702 /// @results errc::success if result has been successfully set, otherwise a
703 /// platform specific error_code.
704 error_code is_symlink(const Twine &path, bool &result);
706 /// @brief Get last write time without changing it.
708 /// @param path Input path.
709 /// @param result Set to the last write time (UNIX time) of \a path if it
711 /// @results errc::success if result has been successfully set, otherwise a
712 /// platform specific error_code.
713 error_code last_write_time(const Twine &path, std::time_t &result);
715 /// @brief Set last write time.
717 /// @param path Input path.
718 /// @param value Time to set (UNIX time) \a path's last write time to.
719 /// @results errc::success if result has been successfully set, otherwise a
720 /// platform specific error_code.
721 error_code set_last_write_time(const Twine &path, std::time_t value);
723 /// @brief Read a symlink's value.
725 /// @param path Input path.
726 /// @param result Set to the value of the symbolic link \a path.
727 /// @results errc::success if result has been successfully set, otherwise a
728 /// platform specific error_code.
729 error_code read_symlink(const Twine &path, SmallVectorImpl<char> &result);
731 /// @brief Get disk space usage information.
733 /// @param path Input path.
734 /// @param result Set to the capacity, free, and available space on the device
736 /// @results errc::success if result has been successfully set, otherwise a
737 /// platform specific error_code.
738 error_code disk_space(const Twine &path, space_info &result);
740 /// @brief Get file status as if by POSIX stat().
742 /// @param path Input path.
743 /// @param result Set to the file status.
744 /// @results errc::success if result has been successfully set, otherwise a
745 /// platform specific error_code.
746 error_code status(const Twine &path, file_status &result);
748 /// @brief Is status available?
750 /// @param path Input path.
751 /// @param result Set to true if status() != status_error.
752 /// @results errc::success if result has been successfully set, otherwise a
753 /// platform specific error_code.
754 error_code status_known(const Twine &path, bool &result);
756 /// @brief Get file status as if by POSIX lstat().
758 /// Does not resolve symlinks.
760 /// @param path Input path.
761 /// @param result Set to the file status.
762 /// @results errc::success if result has been successfully set, otherwise a
763 /// platform specific error_code.
764 error_code symlink_status(const Twine &path, file_status &result);
766 /// @brief Get the temporary directory.
768 /// @param result Set to the temporary directory.
769 /// @results errc::success if result has been successfully set, otherwise a
770 /// platform specific error_code.
772 error_code temp_directory_path(SmallVectorImpl<char> &result);
774 /// @brief Generate a unique path and open it as a file.
776 /// Generates a unique path suitable for a temporary file and then opens it as a
777 /// file. The name is based on \a model with '%' replaced by a random char in
780 /// This is an atomic operation. Either the file is created and opened, or the
781 /// file system is left untouched.
783 /// clang-%%-%%-%%-%%-%%.s => <current-directory>/clang-a0-b1-c2-d3-e4.s
785 /// @param model Name to base unique path off of.
786 /// @param result Set to the opened file.
787 /// @results errc::success if result has been successfully set, otherwise a
788 /// platform specific error_code.
789 /// @see temp_directory_path
790 error_code unique_file(const Twine &model, void* i_have_not_decided_the_ty_yet);
792 /// @brief Canonicalize path.
794 /// Sets result to the file system's idea of what path is. The result is always
795 /// absolute and has the same capitalization as the file system.
797 /// @param path Input path.
798 /// @param result Set to the canonicalized version of \a path.
799 /// @results errc::success if result has been successfully set, otherwise a
800 /// platform specific error_code.
801 error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result);
803 /// @brief Are \a path's first bytes \a magic?
805 /// @param path Input path.
806 /// @param magic Byte sequence to compare \a path's first len(magic) bytes to.
807 /// @results errc::success if result has been successfully set, otherwise a
808 /// platform specific error_code.
809 error_code has_magic(const Twine &path, const Twine &magic);
811 /// @brief Get \a path's first \a len bytes.
813 /// @param path Input path.
814 /// @param len Number of magic bytes to get.
815 /// @param result Set to the first \a len bytes in the file pointed to by
817 /// @results errc::success if result has been successfully set, otherwise a
818 /// platform specific error_code.
819 error_code get_magic(const Twine &path, uint32_t len,
820 SmallVectorImpl<char> &result);
822 /// @brief Is file bitcode?
824 /// @param path Input path.
825 /// @param result Set to true if \a path is a bitcode file, false if it is not,
826 /// undefined otherwise.
827 /// @results errc::success if result has been successfully set, otherwise a
828 /// platform specific error_code.
829 error_code is_bitcode(const Twine &path, bool &result);
831 /// @brief Is file a dynamic library?
833 /// @param path Input path.
834 /// @param result Set to true if \a path is a dynamic library, false if it is
835 /// not, undefined otherwise.
836 /// @results errc::success if result has been successfully set, otherwise a
837 /// platform specific error_code.
838 error_code is_dynamic_library(const Twine &path, bool &result);
840 /// @brief Is an object file?
842 /// @param path Input path.
843 /// @param result Set to true if \a path is an object file, false if it is not,
844 /// undefined otherwise.
845 /// @results errc::success if result has been successfully set, otherwise a
846 /// platform specific error_code.
847 error_code is_object_file(const Twine &path, bool &result);
849 /// @brief Can file be read?
851 /// @param path Input path.
852 /// @param result Set to true if \a path is readable, false it it is not,
853 /// undefined otherwise.
854 /// @results errc::success if result has been successfully set, otherwise a
855 /// platform specific error_code.
856 error_code can_read(const Twine &path, bool &result);
858 /// @brief Can file be written?
860 /// @param path Input path.
861 /// @param result Set to true if \a path is writeable, false it it is not,
862 /// undefined otherwise.
863 /// @results errc::success if result has been successfully set, otherwise a
864 /// platform specific error_code.
865 error_code can_write(const Twine &path, bool &result);
867 /// @brief Can file be executed?
869 /// @param path Input path.
870 /// @param result Set to true if \a path is executable, false it it is not,
871 /// undefined otherwise.
872 /// @results errc::success if result has been successfully set, otherwise a
873 /// platform specific error_code.
874 error_code can_execute(const Twine &path, bool &result);
876 /// @brief Get library paths the system linker uses.
878 /// @param result Set to the list of system library paths.
879 /// @results errc::success if result has been successfully set, otherwise a
880 /// platform specific error_code.
881 error_code GetSystemLibraryPaths(SmallVectorImpl<std::string> &result);
883 /// @brief Get bitcode library paths the system linker uses
884 /// + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
886 /// @param result Set to the list of bitcode library paths.
887 /// @results errc::success if result has been successfully set, otherwise a
888 /// platform specific error_code.
889 error_code GetBitcodeLibraryPaths(SmallVectorImpl<std::string> &result);
891 /// @brief Find a library.
893 /// Find the path to a library using its short name. Use the system
894 /// dependent library paths to locate the library.
896 /// c => /usr/lib/libc.so
898 /// @param short_name Library name one would give to the system linker.
899 /// @param result Set to the absolute path \a short_name represents.
900 /// @results errc::success if result has been successfully set, otherwise a
901 /// platform specific error_code.
902 error_code FindLibrary(const Twine &short_name, SmallVectorImpl<char> &result);
904 /// @brief Get absolute path of main executable.
906 /// @param argv0 The program name as it was spelled on the command line.
907 /// @param MainAddr Address of some symbol in the executable (not in a library).
908 /// @param result Set to the absolute path of the current executable.
909 /// @results errc::success if result has been successfully set, otherwise a
910 /// platform specific error_code.
911 error_code GetMainExecutable(const char *argv0, void *MainAddr,
912 SmallVectorImpl<char> &result);
918 /// directory_entry - A single entry in a directory. Caches the status either
919 /// from the result of the iteration syscall, or the first time status or
920 /// symlink_status is called.
921 class directory_entry {
923 mutable file_status Status;
924 mutable file_status SymlinkStatus;
927 explicit directory_entry(const Twine &path, file_status st = file_status(),
928 file_status symlink_st = file_status());
930 void assign(const Twine &path, file_status st = file_status(),
931 file_status symlink_st = file_status());
932 void replace_filename(const Twine &filename, file_status st = file_status(),
933 file_status symlink_st = file_status());
935 const SmallVectorImpl<char> &path() const;
936 error_code status(file_status &result) const;
937 error_code symlink_status(file_status &result) const;
939 bool operator==(const directory_entry& rhs) const;
940 bool operator!=(const directory_entry& rhs) const;
941 bool operator< (const directory_entry& rhs) const;
942 bool operator<=(const directory_entry& rhs) const;
943 bool operator> (const directory_entry& rhs) const;
944 bool operator>=(const directory_entry& rhs) const;
947 /// directory_iterator - Iterates through the entries in path. There is no
948 /// operator++ because we need an error_code. If it's really needed we can make
949 /// it call report_fatal_error on error.
950 class directory_iterator {
951 // implementation directory iterator status
954 explicit directory_iterator(const Twine &path, error_code &ec);
955 // No operator++ because we need error_code.
956 directory_iterator &increment(error_code &ec);
958 const directory_entry &operator*() const;
959 const directory_entry *operator->() const;
961 // Other members as required by
962 // C++ Std, 24.1.1 Input iterators [input.iterators]
965 /// recursive_directory_iterator - Same as directory_iterator except for it
966 /// recurses down into child directories.
967 class recursive_directory_iterator {
969 bool HasNoPushRequest;
970 // implementation directory iterator status
973 explicit recursive_directory_iterator(const Twine &path, error_code &ec);
974 // No operator++ because we need error_code.
975 directory_iterator &increment(error_code &ec);
977 const directory_entry &operator*() const;
978 const directory_entry *operator->() const;
981 /// Gets the current level. path is at level 0.
983 /// Returns true if no_push has been called for this directory_entry.
984 bool no_push_request() const;
987 /// Goes up one level if Level > 0.
989 /// Does not go down into the current directory_entry.
992 // Other members as required by
993 // C++ Std, 24.1.1 Input iterators [input.iterators]
998 } // end namespace fs
999 } // end namespace sys
1000 } // end namespace llvm