fcdeff9b6dc3cf7f57b18b93f36c88223b83ef13
[oota-llvm.git] / include / llvm / Support / PathV2.h
1 //===- llvm/Support/PathV2.h - Path Operating System Concept ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // path class.
13 //
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
23 // operation.
24 //
25 //===----------------------------------------------------------------------===//
26
27 #ifndef LLVM_SYSTEM_PATHV2_H
28 #define LLVM_SYSTEM_PATHV2_H
29
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"
34 #include <ctime>
35 #include <iterator>
36 #include <string>
37
38 namespace llvm {
39
40 // Forward decls.
41 class StringRef;
42 class Twine;
43
44 namespace sys {
45 namespace path {
46
47 /// @name Lexical Component Iterator
48 /// @{
49
50 /// @brief Path iterator.
51 ///
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.
59 ///
60 /// Iteration examples. Each component is separated by ',':
61 /// /          => /
62 /// /foo       => /,foo
63 /// foo/       => foo,.
64 /// /foo/bar   => /,foo,bar
65 /// ../        => ..,.
66 /// C:\foo\bar => C:,/,foo,bar
67 ///
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.
72
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);
76
77 public:
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;
83
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;
92 };
93
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);
98
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);
103
104 /// @}
105 /// @name Lexical Modifiers
106 /// @{
107
108 /// @brief Make \a path an absolute path.
109 ///
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.
112 ///
113 /// /absolute/path   => /absolute/path
114 /// relative/../path => <current-directory>/path
115 ///
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);
120
121 /// @brief Remove the last component from \a path if it exists.
122 ///
123 /// directory/filename.cpp => directory/
124 /// directory/             => directory/
125 ///
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);
130
131 /// @brief Replace the file extension of \a path with \a extension.
132 ///
133 /// ./filename.cpp => ./filename.extension
134 /// ./filename     => ./filename.extension
135 /// ./             => ? TODO: decide what semantics this has.
136 ///
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
140 ///                  prepended.
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);
145
146 /// @brief Append to path.
147 ///
148 /// /foo  + bar/f => /foo/bar/f
149 /// /foo/ + bar/f => /foo/bar/f
150 /// foo   + bar/f => foo/bar/f
151 ///
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,
157                                                const Twine &b = "",
158                                                const Twine &c = "",
159                                                const Twine &d = "");
160
161 /// @brief Append to path.
162 ///
163 /// /foo  + [bar,f] => /foo/bar/f
164 /// /foo/ + [bar,f] => /foo/bar/f
165 /// foo   + [bar,f] => foo/bar/f
166 ///
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);
174
175 /// @}
176 /// @name Transforms (or some other better name)
177 /// @{
178
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 '\'.
182 ///
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);
188
189 /// @}
190 /// @name Lexical Observers
191 /// @{
192
193 /// @brief Get the current path.
194 ///
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);
199
200 // The following are purely lexical.
201
202 /// @brief Is the current path valid?
203 ///
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);
209
210 /// @brief Get root name.
211 ///
212 /// //net/hello => //net
213 /// c:/hello    => c: (on Windows, on other platforms nothing)
214 /// /hello      => <empty>
215 ///
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);
221
222 /// @brief Get root directory.
223 ///
224 /// /goo/hello => /
225 /// c:/hello   => /
226 /// d/file.txt => <empty>
227 ///
228 /// @param path Input path.
229 /// @param result Set to the root directory of \a path if it has one, otherwise
230 ///               "".
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);
234
235 /// @brief Get root path.
236 ///
237 /// Equivalent to root_name + root_directory.
238 ///
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);
244
245 /// @brief Get relative path.
246 ///
247 /// C:\hello\world => hello\world
248 /// foo/bar        => foo/bar
249 /// /foo/bar       => foo/bar
250 ///
251 /// @param path Input path.
252 /// @param result Set to the path starting after root_path if one exists,
253 ///               otherwise "".
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);
257
258 /// @brief Get parent path.
259 ///
260 /// /          => <empty>
261 /// /foo       => /
262 /// foo/../bar => foo/..
263 ///
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);
269
270 /// @brief Get filename.
271 ///
272 /// /foo.txt    => foo.txt
273 /// .          => .
274 /// ..         => ..
275 /// /          => /
276 ///
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);
283
284 /// @brief Get stem.
285 ///
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
288 /// it is filename.
289 ///
290 /// /foo/bar.txt => bar
291 /// /foo/bar     => bar
292 /// /foo/.txt    => <empty>
293 /// /foo/.       => .
294 /// /foo/..      => ..
295 ///
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);
301
302 /// @brief Get extension.
303 ///
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 "".
307 ///
308 /// /foo/bar.txt => .txt
309 /// /foo/bar     => <empty>
310 /// /foo/.txt    => .txt
311 ///
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);
317
318 /// @brief Has root name?
319 ///
320 /// root_name != ""
321 ///
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);
327
328 /// @brief Has root directory?
329 ///
330 /// root_directory != ""
331 ///
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);
337
338 /// @brief Has root path?
339 ///
340 /// root_path != ""
341 ///
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);
347
348 /// @brief Has relative path?
349 ///
350 /// relative_path != ""
351 ///
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);
357
358 /// @brief Has parent path?
359 ///
360 /// parent_path != ""
361 ///
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);
367
368 /// @brief Has filename?
369 ///
370 /// filename != ""
371 ///
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);
377
378 /// @brief Has stem?
379 ///
380 /// stem != ""
381 ///
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);
387
388 /// @brief Has extension?
389 ///
390 /// extension != ""
391 ///
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);
397
398 /// @brief Is path absolute?
399 ///
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);
405
406 /// @brief Is path relative?
407 ///
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.
414
415 } // end namespace path
416
417 namespace fs {
418
419 /// file_type - An "enum class" enumeration for the file system's view of the
420 ///             type.
421 struct file_type {
422   enum _ {
423     status_error,
424     file_not_found,
425     regular_file,
426     directory_file,
427     symlink_file,
428     block_file,
429     character_file,
430     fifo_file,
431     socket_file,
432     type_unknown
433   };
434
435   file_type(_ v) : v_(v) {}
436   explicit file_type(int v) : v_(_(v)) {}
437   operator int() const {return v_;}
438
439 private:
440   int v_;
441 };
442
443 /// copy_option - An "enum class" enumeration of copy semantics for copy
444 ///               operations.
445 struct copy_option {
446   enum _ {
447     fail_if_exists,
448     overwrite_if_exists
449   };
450
451   copy_option(_ v) : v_(v) {}
452   explicit copy_option(int v) : v_(_(v)) {}
453   operator int() const {return v_;}
454
455 private:
456   int v_;
457 };
458
459 /// space_info - Self explanatory.
460 struct space_info {
461   uint64_t capacity;
462   uint64_t free;
463   uint64_t available;
464 };
465
466 /// file_status - Represents the result of a call to stat and friends. It has
467 ///               a platform specific member to store the result.
468 class file_status
469 {
470   // implementation defined status field.
471 public:
472   explicit file_status(file_type v=file_type::status_error);
473
474   file_type type() const;
475   void type(file_type v);
476 };
477
478 /// @}
479 /// @name Physical Operators
480 /// @{
481
482 /// @brief Copy the file at \a from to the path \a to.
483 ///
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
490 ///          error_code.
491 error_code copy_file(const Twine &from, const Twine &to,
492                      copy_option copt = copy_option::fail_if_exists);
493
494 /// @brief Create all the non-existent directories in path.
495 ///
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);
501
502 /// @brief Create the directory in path.
503 ///
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);
509
510 /// @brief Create a hard link from \a from to \a to.
511 ///
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);
517
518 /// @brief Create a symbolic link from \a from to \a to.
519 ///
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);
525
526 /// @brief Remove path. Equivalent to POSIX remove().
527 ///
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);
534
535 /// @brief Recursively remove all files below \a path, then \a path. Files are
536 ///        removed as if by POSIX remove().
537 ///
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);
543
544 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
545 ///
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);
549
550 /// @brief Resize path to size. File is resized as if by POSIX truncate().
551 ///
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);
557
558 /// @brief Make file readable.
559 ///
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);
565
566 /// @brief Make file writeable.
567 ///
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);
573
574 /// @brief Make file executable.
575 ///
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);
581
582 /// @}
583 /// @name Physical Observers
584 /// @{
585
586 /// @brief Does file exist?
587 ///
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);
594
595 /// @brief Does file exist?
596 ///
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);
603
604 /// @brief Do paths represent the same thing?
605 ///
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);
613
614 /// @brief Get file size.
615 ///
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);
621
622 /// @brief Does status represent a directory?
623 ///
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);
630
631 /// @brief Is path a directory?
632 ///
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);
639
640 /// @brief Is path an empty file?
641 ///
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);
648
649 /// @brief Does status represent a regular file?
650 ///
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);
657
658 /// @brief Is path a regular file?
659 ///
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);
666
667 /// @brief Does status represent something that exists but is not a directory,
668 ///        regular file, or symlink?
669 ///
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);
677
678 /// @brief Is path something that exists but is not a directory,
679 ///        regular file, or symlink?
680 ///
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);
687
688 /// @brief Does status represent a symlink?
689 ///
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);
696
697 /// @brief Is path a symlink?
698 ///
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);
705
706 /// @brief Get last write time without changing it.
707 ///
708 /// @param path Input path.
709 /// @param result Set to the last write time (UNIX time) of \a path if it
710 ///               exists.
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);
714
715 /// @brief Set last write time.
716 ///
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);
722
723 /// @brief Read a symlink's value.
724 ///
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);
730
731 /// @brief Get disk space usage information.
732 ///
733 /// @param path Input path.
734 /// @param result Set to the capacity, free, and available space on the device
735 ///               \a path is on.
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);
739
740 /// @brief Get file status as if by POSIX stat().
741 ///
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);
747
748 /// @brief Is status available?
749 ///
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);
755
756 /// @brief Get file status as if by POSIX lstat().
757 ///
758 /// Does not resolve symlinks.
759 ///
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);
765
766 /// @brief Get the temporary directory.
767 ///
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.
771 /// @see unique_file
772 error_code temp_directory_path(SmallVectorImpl<char> &result);
773
774 /// @brief Generate a unique path and open it as a file.
775 ///
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
778 /// [0-9a-f].
779 ///
780 /// This is an atomic operation. Either the file is created and opened, or the
781 /// file system is left untouched.
782 ///
783 /// clang-%%-%%-%%-%%-%%.s => <current-directory>/clang-a0-b1-c2-d3-e4.s
784 ///
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);
791
792 /// @brief Canonicalize path.
793 ///
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.
796 ///
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);
802
803 /// @brief Are \a path's first bytes \a magic?
804 ///
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);
810
811 /// @brief Get \a path's first \a len bytes.
812 ///
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
816 ///               \a path.
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);
821
822 /// @brief Is file bitcode?
823 ///
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);
830
831 /// @brief Is file a dynamic library?
832 ///
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);
839
840 /// @brief Is an object file?
841 ///
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);
848
849 /// @brief Can file be read?
850 ///
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);
857
858 /// @brief Can file be written?
859 ///
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);
866
867 /// @brief Can file be executed?
868 ///
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);
875
876 /// @brief Get library paths the system linker uses.
877 ///
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);
882
883 /// @brief Get bitcode library paths the system linker uses
884 ///        + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
885 ///
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);
890
891 /// @brief Find a library.
892 ///
893 /// Find the path to a library using its short name. Use the system
894 /// dependent library paths to locate the library.
895 ///
896 /// c => /usr/lib/libc.so
897 ///
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);
903
904 /// @brief Get absolute path of main executable.
905 ///
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);
913
914 /// @}
915 /// @name Iterators
916 /// @{
917
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 {
922   std::string Path;
923   mutable file_status Status;
924   mutable file_status SymlinkStatus;
925
926 public:
927   explicit directory_entry(const Twine &path, file_status st = file_status(),
928                                        file_status symlink_st = file_status());
929
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());
934
935   const SmallVectorImpl<char> &path() const;
936   error_code status(file_status &result) const;
937   error_code symlink_status(file_status &result) const;
938
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;
945 };
946
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
952
953 public:
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);
957
958   const directory_entry &operator*() const;
959   const directory_entry *operator->() const;
960
961   // Other members as required by
962   // C++ Std, 24.1.1 Input iterators [input.iterators]
963 };
964
965 /// recursive_directory_iterator - Same as directory_iterator except for it
966 /// recurses down into child directories.
967 class recursive_directory_iterator {
968   uint16_t  Level;
969   bool HasNoPushRequest;
970   // implementation directory iterator status
971
972 public:
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);
976
977   const directory_entry &operator*() const;
978   const directory_entry *operator->() const;
979
980   // observers
981   /// Gets the current level. path is at level 0.
982   int level() const;
983   /// Returns true if no_push has been called for this directory_entry.
984   bool no_push_request() const;
985
986   // modifiers
987   /// Goes up one level if Level > 0.
988   void pop();
989   /// Does not go down into the current directory_entry.
990   void no_push();
991
992   // Other members as required by
993   // C++ Std, 24.1.1 Input iterators [input.iterators]
994 };
995
996 /// @}
997
998 } // end namespace fs
999 } // end namespace sys
1000 } // end namespace llvm
1001
1002 #endif