85314842dd258c517f232e0310f1c63d927f2341
[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 { return Component; }
85   pointer   operator->() const { return &Component; }
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   /// @brief Difference in bytes between this and RHS.
94   ptrdiff_t operator-(const const_iterator &RHS) const;
95 };
96
97 typedef std::reverse_iterator<const_iterator> reverse_iterator;
98
99 /// @brief Get begin iterator over \a path.
100 /// @param path Input path.
101 /// @returns Iterator initialized with the first component of \a path.
102 const_iterator begin(const StringRef &path);
103
104 /// @brief Get end iterator over \a path.
105 /// @param path Input path.
106 /// @returns Iterator initialized to the end of \a path.
107 const_iterator end(const StringRef &path);
108
109 /// @brief Get reverse begin iterator over \a path.
110 /// @param path Input path.
111 /// @returns Iterator initialized with the first reverse component of \a path.
112 inline reverse_iterator rbegin(const StringRef &path) {
113   return reverse_iterator(end(path));
114 }
115
116 /// @brief Get reverse end iterator over \a path.
117 /// @param path Input path.
118 /// @returns Iterator initialized to the reverse end of \a path.
119 inline reverse_iterator rend(const StringRef &path) {
120   return reverse_iterator(begin(path));
121 }
122
123 /// @}
124 /// @name Lexical Modifiers
125 /// @{
126
127 /// @brief Make \a path an absolute path.
128 ///
129 /// Makes \a path absolute using the current directory if it is not already. An
130 /// empty \a path will result in the current directory.
131 ///
132 /// /absolute/path   => /absolute/path
133 /// relative/../path => <current-directory>/path
134 ///
135 /// @param path A path that is modified to be an absolute path.
136 /// @returns errc::success if \a path has been made absolute, otherwise a
137 ///          platform specific error_code.
138 error_code make_absolute(SmallVectorImpl<char> &path);
139
140 /// @brief Remove the last component from \a path if it exists.
141 ///
142 /// directory/filename.cpp => directory/
143 /// directory/             => directory
144 ///
145 /// @param path A path that is modified to not have a file component.
146 /// @returns errc::success if \a path's file name has been removed (or there was
147 ///          not one to begin with), otherwise a platform specific error_code.
148 error_code remove_filename(SmallVectorImpl<char> &path);
149
150 /// @brief Replace the file extension of \a path with \a extension.
151 ///
152 /// ./filename.cpp => ./filename.extension
153 /// ./filename     => ./filename.extension
154 /// ./             => ? TODO: decide what semantics this has.
155 ///
156 /// @param path A path that has its extension replaced with \a extension.
157 /// @param extension The extension to be added. It may be empty. It may also
158 ///                  optionally start with a '.', if it does not, one will be
159 ///                  prepended.
160 /// @returns errc::success if \a path's extension has been replaced, otherwise a
161 ///          platform specific error_code.
162 error_code replace_extension(SmallVectorImpl<char> &path,
163                              const Twine &extension);
164
165 /// @brief Append to path.
166 ///
167 /// /foo  + bar/f => /foo/bar/f
168 /// /foo/ + bar/f => /foo/bar/f
169 /// foo   + bar/f => foo/bar/f
170 ///
171 /// @param path Set to \a path + \a component.
172 /// @param component The component to be appended to \a path.
173 /// @returns errc::success if \a component has been appended to \a path,
174 ///          otherwise a platform specific error_code.
175 error_code append(SmallVectorImpl<char> &path, const Twine &a,
176                                                const Twine &b = "",
177                                                const Twine &c = "",
178                                                const Twine &d = "");
179
180 /// @brief Append to path.
181 ///
182 /// /foo  + [bar,f] => /foo/bar/f
183 /// /foo/ + [bar,f] => /foo/bar/f
184 /// foo   + [bar,f] => foo/bar/f
185 ///
186 /// @param path Set to \a path + [\a begin, \a end).
187 /// @param begin Start of components to append.
188 /// @param end One past the end of components to append.
189 /// @returns errc::success if [\a begin, \a end) has been appended to \a path,
190 ///          otherwise a platform specific error_code.
191 error_code append(SmallVectorImpl<char> &path,
192                   const_iterator begin, const_iterator end);
193
194 /// @}
195 /// @name Transforms (or some other better name)
196 /// @{
197
198 /// Convert path to the native form. This is used to give paths to users and
199 /// operating system calls in the platform's normal way. For example, on Windows
200 /// all '/' are converted to '\'.
201 ///
202 /// @param path A path that is transformed to native format.
203 /// @param result Holds the result of the transformation.
204 /// @returns errc::success if \a path has been transformed and stored in result,
205 ///          otherwise a platform specific error_code.
206 error_code native(const Twine &path, SmallVectorImpl<char> &result);
207
208 /// @}
209 /// @name Lexical Observers
210 /// @{
211
212 /// @brief Get the current path.
213 ///
214 /// @param result Holds the current path on return.
215 /// @results errc::success if the current path has been stored in result,
216 ///          otherwise a platform specific error_code.
217 error_code current_path(SmallVectorImpl<char> &result);
218
219 // The following are purely lexical.
220
221 /// @brief Get root name.
222 ///
223 /// //net/hello => //net
224 /// c:/hello    => c: (on Windows, on other platforms nothing)
225 /// /hello      => <empty>
226 ///
227 /// @param path Input path.
228 /// @param result Set to the root name of \a path if it has one, otherwise "".
229 /// @results errc::success if result has been successfully set, otherwise a
230 ///          platform specific error_code.
231 error_code root_name(const StringRef &path, StringRef &result);
232
233 /// @brief Get root directory.
234 ///
235 /// /goo/hello => /
236 /// c:/hello   => /
237 /// d/file.txt => <empty>
238 ///
239 /// @param path Input path.
240 /// @param result Set to the root directory of \a path if it has one, otherwise
241 ///               "".
242 /// @results errc::success if result has been successfully set, otherwise a
243 ///          platform specific error_code.
244 error_code root_directory(const StringRef &path, StringRef &result);
245
246 /// @brief Get root path.
247 ///
248 /// Equivalent to root_name + root_directory.
249 ///
250 /// @param path Input path.
251 /// @param result Set to the root path of \a path if it has one, otherwise "".
252 /// @results errc::success if result has been successfully set, otherwise a
253 ///          platform specific error_code.
254 error_code root_path(const StringRef &path, StringRef &result);
255
256 /// @brief Get relative path.
257 ///
258 /// C:\hello\world => hello\world
259 /// foo/bar        => foo/bar
260 /// /foo/bar       => foo/bar
261 ///
262 /// @param path Input path.
263 /// @param result Set to the path starting after root_path if one exists,
264 ///               otherwise "".
265 /// @results errc::success if result has been successfully set, otherwise a
266 ///          platform specific error_code.
267 error_code relative_path(const StringRef &path, StringRef &result);
268
269 /// @brief Get parent path.
270 ///
271 /// /          => <empty>
272 /// /foo       => /
273 /// foo/../bar => foo/..
274 ///
275 /// @param path Input path.
276 /// @param result Set to the parent path of \a path if one exists, otherwise "".
277 /// @results errc::success if result has been successfully set, otherwise a
278 ///          platform specific error_code.
279 error_code parent_path(const StringRef &path, StringRef &result);
280
281 /// @brief Get filename.
282 ///
283 /// /foo.txt    => foo.txt
284 /// .          => .
285 /// ..         => ..
286 /// /          => /
287 ///
288 /// @param path Input path.
289 /// @param result Set to the filename part of \a path. This is defined as the
290 ///               last component of \a path.
291 /// @results errc::success if result has been successfully set, otherwise a
292 ///          platform specific error_code.
293 error_code filename(const StringRef &path, StringRef &result);
294
295 /// @brief Get stem.
296 ///
297 /// If filename contains a dot but not solely one or two dots, result is the
298 /// substring of filename ending at (but not including) the last dot. Otherwise
299 /// it is filename.
300 ///
301 /// /foo/bar.txt => bar
302 /// /foo/bar     => bar
303 /// /foo/.txt    => <empty>
304 /// /foo/.       => .
305 /// /foo/..      => ..
306 ///
307 /// @param path Input path.
308 /// @param result Set to the stem of \a path.
309 /// @results errc::success if result has been successfully set, otherwise a
310 ///          platform specific error_code.
311 error_code stem(const StringRef &path, StringRef &result);
312
313 /// @brief Get extension.
314 ///
315 /// If filename contains a dot but not solely one or two dots, result is the
316 /// substring of filename starting at (and including) the last dot, and ending
317 /// at the end of \a path. Otherwise "".
318 ///
319 /// /foo/bar.txt => .txt
320 /// /foo/bar     => <empty>
321 /// /foo/.txt    => .txt
322 ///
323 /// @param path Input path.
324 /// @param result Set to the extension of \a path.
325 /// @results errc::success if result has been successfully set, otherwise a
326 ///          platform specific error_code.
327 error_code extension(const StringRef &path, StringRef &result);
328
329 /// @brief Has root name?
330 ///
331 /// root_name != ""
332 ///
333 /// @param path Input path.
334 /// @param result Set to true if the path has a root name, false otherwise.
335 /// @results errc::success if result has been successfully set, otherwise a
336 ///          platform specific error_code.
337 error_code has_root_name(const Twine &path, bool &result);
338
339 /// @brief Has root directory?
340 ///
341 /// root_directory != ""
342 ///
343 /// @param path Input path.
344 /// @param result Set to true if the path has a root directory, false otherwise.
345 /// @results errc::success if result has been successfully set, otherwise a
346 ///          platform specific error_code.
347 error_code has_root_directory(const Twine &path, bool &result);
348
349 /// @brief Has root path?
350 ///
351 /// root_path != ""
352 ///
353 /// @param path Input path.
354 /// @param result Set to true if the path has a root path, false otherwise.
355 /// @results errc::success if result has been successfully set, otherwise a
356 ///          platform specific error_code.
357 error_code has_root_path(const Twine &path, bool &result);
358
359 /// @brief Has relative path?
360 ///
361 /// relative_path != ""
362 ///
363 /// @param path Input path.
364 /// @param result Set to true if the path has a relative path, false otherwise.
365 /// @results errc::success if result has been successfully set, otherwise a
366 ///          platform specific error_code.
367 error_code has_relative_path(const Twine &path, bool &result);
368
369 /// @brief Has parent path?
370 ///
371 /// parent_path != ""
372 ///
373 /// @param path Input path.
374 /// @param result Set to true if the path has a parent path, false otherwise.
375 /// @results errc::success if result has been successfully set, otherwise a
376 ///          platform specific error_code.
377 error_code has_parent_path(const Twine &path, bool &result);
378
379 /// @brief Has filename?
380 ///
381 /// filename != ""
382 ///
383 /// @param path Input path.
384 /// @param result Set to true if the path has a filename, false otherwise.
385 /// @results errc::success if result has been successfully set, otherwise a
386 ///          platform specific error_code.
387 error_code has_filename(const Twine &path, bool &result);
388
389 /// @brief Has stem?
390 ///
391 /// stem != ""
392 ///
393 /// @param path Input path.
394 /// @param result Set to true if the path has a stem, false otherwise.
395 /// @results errc::success if result has been successfully set, otherwise a
396 ///          platform specific error_code.
397 error_code has_stem(const Twine &path, bool &result);
398
399 /// @brief Has extension?
400 ///
401 /// extension != ""
402 ///
403 /// @param path Input path.
404 /// @param result Set to true if the path has a extension, false otherwise.
405 /// @results errc::success if result has been successfully set, otherwise a
406 ///          platform specific error_code.
407 error_code has_extension(const Twine &path, bool &result);
408
409 /// @brief Is path absolute?
410 ///
411 /// @param path Input path.
412 /// @param result Set to true if the path is absolute, false if it is not.
413 /// @results errc::success if result has been successfully set, otherwise a
414 ///          platform specific error_code.
415 error_code is_absolute(const Twine &path, bool &result);
416
417 /// @brief Is path relative?
418 ///
419 /// @param path Input path.
420 /// @param result Set to true if the path is relative, false if it is not.
421 /// @results errc::success if result has been successfully set, otherwise a
422 ///          platform specific error_code.
423 error_code is_relative(const Twine &path, bool &result);
424 // end purely lexical.
425
426 } // end namespace path
427
428 namespace fs {
429
430 /// file_type - An "enum class" enumeration for the file system's view of the
431 ///             type.
432 struct file_type {
433   enum _ {
434     status_error,
435     file_not_found,
436     regular_file,
437     directory_file,
438     symlink_file,
439     block_file,
440     character_file,
441     fifo_file,
442     socket_file,
443     type_unknown
444   };
445
446   file_type(_ v) : v_(v) {}
447   explicit file_type(int v) : v_(_(v)) {}
448   operator int() const {return v_;}
449
450 private:
451   int v_;
452 };
453
454 /// copy_option - An "enum class" enumeration of copy semantics for copy
455 ///               operations.
456 struct copy_option {
457   enum _ {
458     fail_if_exists,
459     overwrite_if_exists
460   };
461
462   copy_option(_ v) : v_(v) {}
463   explicit copy_option(int v) : v_(_(v)) {}
464   operator int() const {return v_;}
465
466 private:
467   int v_;
468 };
469
470 /// space_info - Self explanatory.
471 struct space_info {
472   uint64_t capacity;
473   uint64_t free;
474   uint64_t available;
475 };
476
477 /// file_status - Represents the result of a call to stat and friends. It has
478 ///               a platform specific member to store the result.
479 class file_status
480 {
481   // implementation defined status field.
482 public:
483   explicit file_status(file_type v=file_type::status_error);
484
485   file_type type() const;
486   void type(file_type v);
487 };
488
489 /// @}
490 /// @name Physical Operators
491 /// @{
492
493 /// @brief Copy the file at \a from to the path \a to.
494 ///
495 /// @param from The path to copy the file from.
496 /// @param to The path to copy the file to.
497 /// @param copt Behavior if \a to already exists.
498 /// @returns errc::success if the file has been successfully copied.
499 ///          errc::file_exists if \a to already exists and \a copt ==
500 ///          copy_option::fail_if_exists. Otherwise a platform specific
501 ///          error_code.
502 error_code copy_file(const Twine &from, const Twine &to,
503                      copy_option copt = copy_option::fail_if_exists);
504
505 /// @brief Create all the non-existent directories in path.
506 ///
507 /// @param path Directories to create.
508 /// @param existed Set to true if \a path already existed, false otherwise.
509 /// @returns errc::success if is_directory(path) and existed have been set,
510 ///          otherwise a platform specific error_code.
511 error_code create_directories(const Twine &path, bool &existed);
512
513 /// @brief Create the directory in path.
514 ///
515 /// @param path Directory to create.
516 /// @param existed Set to true if \a path already existed, false otherwise.
517 /// @returns errc::success if is_directory(path) and existed have been set,
518 ///          otherwise a platform specific error_code.
519 error_code create_directory(const Twine &path, bool &existed);
520
521 /// @brief Create a hard link from \a from to \a to.
522 ///
523 /// @param to The path to hard link to.
524 /// @param from The path to hard link from. This is created.
525 /// @returns errc::success if exists(to) && exists(from) && equivalent(to, from)
526 ///          , otherwise a platform specific error_code.
527 error_code create_hard_link(const Twine &to, const Twine &from);
528
529 /// @brief Create a symbolic link from \a from to \a to.
530 ///
531 /// @param to The path to symbolically link to.
532 /// @param from The path to symbolically link from. This is created.
533 /// @returns errc::success if exists(to) && exists(from) && is_symlink(from),
534 ///          otherwise a platform specific error_code.
535 error_code create_symlink(const Twine &to, const Twine &from);
536
537 /// @brief Remove path. Equivalent to POSIX remove().
538 ///
539 /// @param path Input path.
540 /// @param existed Set to true if \a path existed, false if it did not.
541 ///                undefined otherwise.
542 /// @results errc::success if path has been removed and existed has been
543 ///          successfully set, otherwise a platform specific error_code.
544 error_code remove(const Twine &path, bool &existed);
545
546 /// @brief Recursively remove all files below \a path, then \a path. Files are
547 ///        removed as if by POSIX remove().
548 ///
549 /// @param path Input path.
550 /// @param num_removed Number of files removed.
551 /// @results errc::success if path has been removed and num_removed has been
552 ///          successfully set, otherwise a platform specific error_code.
553 error_code remove_all(const Twine &path, uint32_t &num_removed);
554
555 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
556 ///
557 /// @param from The path to rename from.
558 /// @param to The path to rename to. This is created.
559 error_code rename(const Twine &from, const Twine &to);
560
561 /// @brief Resize path to size. File is resized as if by POSIX truncate().
562 ///
563 /// @param path Input path.
564 /// @param size Size to resize to.
565 /// @returns errc::success if \a path has been resized to \a size, otherwise a
566 ///          platform specific error_code.
567 error_code resize_file(const Twine &path, uint64_t size);
568
569 /// @brief Make file readable.
570 ///
571 /// @param path Input path.
572 /// @param value If true, make readable, else, make unreadable.
573 /// @results errc::success if readability has been successfully set, otherwise a
574 ///          platform specific error_code.
575 error_code set_read(const Twine &path, bool value);
576
577 /// @brief Make file writeable.
578 ///
579 /// @param path Input path.
580 /// @param value If true, make writeable, else, make unwriteable.
581 /// @results errc::success if writeability has been successfully set, otherwise
582 ///          a platform specific error_code.
583 error_code set_write(const Twine &path, bool value);
584
585 /// @brief Make file executable.
586 ///
587 /// @param path Input path.
588 /// @param value If true, make executable, else, make unexecutable.
589 /// @results errc::success if executability has been successfully set, otherwise
590 ///          a platform specific error_code.
591 error_code set_execute(const Twine &path, bool value);
592
593 /// @}
594 /// @name Physical Observers
595 /// @{
596
597 /// @brief Does file exist?
598 ///
599 /// @param status A file_status previously returned from stat.
600 /// @param result Set to true if the file represented by status exists, false if
601 ///               it does not. Undefined otherwise.
602 /// @results errc::success if result has been successfully set, otherwise a
603 ///          platform specific error_code.
604 error_code exists(file_status status, bool &result);
605
606 /// @brief Does file exist?
607 ///
608 /// @param path Input path.
609 /// @param result Set to true if the file represented by status exists, false if
610 ///               it does not. Undefined otherwise.
611 /// @results errc::success if result has been successfully set, otherwise a
612 ///          platform specific error_code.
613 error_code exists(const Twine &path, bool &result);
614
615 /// @brief Do paths represent the same thing?
616 ///
617 /// @param A Input path A.
618 /// @param B Input path B.
619 /// @param result Set to true if stat(A) and stat(B) have the same device and
620 ///               inode (or equivalent).
621 /// @results errc::success if result has been successfully set, otherwise a
622 ///          platform specific error_code.
623 error_code equivalent(const Twine &A, const Twine &B, bool &result);
624
625 /// @brief Get file size.
626 ///
627 /// @param path Input path.
628 /// @param result Set to the size of the file in \a path.
629 /// @returns errc::success if result has been successfully set, otherwise a
630 ///          platform specific error_code.
631 error_code file_size(const Twine &path, uint64_t &result);
632
633 /// @brief Does status represent a directory?
634 ///
635 /// @param status A file_status previously returned from stat.
636 /// @param result Set to true if the file represented by status is a directory,
637 ///               false if it is not. Undefined otherwise.
638 /// @results errc::success if result has been successfully set, otherwise a
639 ///          platform specific error_code.
640 error_code is_directory(file_status status, bool &result);
641
642 /// @brief Is path a directory?
643 ///
644 /// @param path Input path.
645 /// @param result Set to true if \a path is a directory, false if it is not.
646 ///               Undefined otherwise.
647 /// @results errc::success if result has been successfully set, otherwise a
648 ///          platform specific error_code.
649 error_code is_directory(const Twine &path, bool &result);
650
651 /// @brief Is path an empty file?
652 ///
653 /// @param path Input path.
654 /// @param result Set to true if \a path is a an empty file, false if it is not.
655 ///               Undefined otherwise.
656 /// @results errc::success if result has been successfully set, otherwise a
657 ///          platform specific error_code.
658 error_code is_empty(const Twine &path, bool &result);
659
660 /// @brief Does status represent a regular file?
661 ///
662 /// @param status A file_status previously returned from stat.
663 /// @param result Set to true if the file represented by status is a regular
664 ///               file, false if it is not. Undefined otherwise.
665 /// @results errc::success if result has been successfully set, otherwise a
666 ///          platform specific error_code.
667 error_code is_regular_file(file_status status, bool &result);
668
669 /// @brief Is path a regular file?
670 ///
671 /// @param path Input path.
672 /// @param result Set to true if \a path is a regular file, false if it is not.
673 ///               Undefined otherwise.
674 /// @results errc::success if result has been successfully set, otherwise a
675 ///          platform specific error_code.
676 error_code is_regular_file(const Twine &path, bool &result);
677
678 /// @brief Does status represent something that exists but is not a directory,
679 ///        regular file, or symlink?
680 ///
681 /// @param status A file_status previously returned from stat.
682 /// @param result Set to true if the file represented by status exists, but is
683 ///               not a directory, regular file, or a symlink, false if it does
684 ///               not. Undefined otherwise.
685 /// @results errc::success if result has been successfully set, otherwise a
686 ///          platform specific error_code.
687 error_code is_other(file_status status, bool &result);
688
689 /// @brief Is path something that exists but is not a directory,
690 ///        regular file, or symlink?
691 ///
692 /// @param path Input path.
693 /// @param result Set to true if \a path exists, but is not a directory, regular
694 ///               file, or a symlink, false if it does not. Undefined otherwise.
695 /// @results errc::success if result has been successfully set, otherwise a
696 ///          platform specific error_code.
697 error_code is_other(const Twine &path, bool &result);
698
699 /// @brief Does status represent a symlink?
700 ///
701 /// @param status A file_status previously returned from stat.
702 /// @param result Set to true if the file represented by status is a symlink,
703 ///               false if it is not. Undefined otherwise.
704 /// @results errc::success if result has been successfully set, otherwise a
705 ///          platform specific error_code.
706 error_code is_symlink(file_status status, bool &result);
707
708 /// @brief Is path a symlink?
709 ///
710 /// @param path Input path.
711 /// @param result Set to true if \a path is a symlink, false if it is not.
712 ///               Undefined otherwise.
713 /// @results errc::success if result has been successfully set, otherwise a
714 ///          platform specific error_code.
715 error_code is_symlink(const Twine &path, bool &result);
716
717 /// @brief Get last write time without changing it.
718 ///
719 /// @param path Input path.
720 /// @param result Set to the last write time (UNIX time) of \a path if it
721 ///               exists.
722 /// @results errc::success if result has been successfully set, otherwise a
723 ///          platform specific error_code.
724 error_code last_write_time(const Twine &path, std::time_t &result);
725
726 /// @brief Set last write time.
727 ///
728 /// @param path Input path.
729 /// @param value Time to set (UNIX time) \a path's last write time to.
730 /// @results errc::success if result has been successfully set, otherwise a
731 ///          platform specific error_code.
732 error_code set_last_write_time(const Twine &path, std::time_t value);
733
734 /// @brief Read a symlink's value.
735 ///
736 /// @param path Input path.
737 /// @param result Set to the value of the symbolic link \a path.
738 /// @results errc::success if result has been successfully set, otherwise a
739 ///          platform specific error_code.
740 error_code read_symlink(const Twine &path, SmallVectorImpl<char> &result);
741
742 /// @brief Get disk space usage information.
743 ///
744 /// @param path Input path.
745 /// @param result Set to the capacity, free, and available space on the device
746 ///               \a path is on.
747 /// @results errc::success if result has been successfully set, otherwise a
748 ///          platform specific error_code.
749 error_code disk_space(const Twine &path, space_info &result);
750
751 /// @brief Get file status as if by POSIX stat().
752 ///
753 /// @param path Input path.
754 /// @param result Set to the file status.
755 /// @results errc::success if result has been successfully set, otherwise a
756 ///          platform specific error_code.
757 error_code status(const Twine &path, file_status &result);
758
759 /// @brief Is status available?
760 ///
761 /// @param path Input path.
762 /// @param result Set to true if status() != status_error.
763 /// @results errc::success if result has been successfully set, otherwise a
764 ///          platform specific error_code.
765 error_code status_known(const Twine &path, bool &result);
766
767 /// @brief Get file status as if by POSIX lstat().
768 ///
769 /// Does not resolve symlinks.
770 ///
771 /// @param path Input path.
772 /// @param result Set to the file status.
773 /// @results errc::success if result has been successfully set, otherwise a
774 ///          platform specific error_code.
775 error_code symlink_status(const Twine &path, file_status &result);
776
777 /// @brief Get the temporary directory.
778 ///
779 /// @param result Set to the temporary directory.
780 /// @results errc::success if result has been successfully set, otherwise a
781 ///          platform specific error_code.
782 /// @see unique_file
783 error_code temp_directory_path(SmallVectorImpl<char> &result);
784
785 /// @brief Generate a unique path and open it as a file.
786 ///
787 /// Generates a unique path suitable for a temporary file and then opens it as a
788 /// file. The name is based on \a model with '%' replaced by a random char in
789 /// [0-9a-f].
790 ///
791 /// This is an atomic operation. Either the file is created and opened, or the
792 /// file system is left untouched.
793 ///
794 /// clang-%%-%%-%%-%%-%%.s => <current-directory>/clang-a0-b1-c2-d3-e4.s
795 ///
796 /// @param model Name to base unique path off of.
797 /// @param result Set to the opened file.
798 /// @results errc::success if result has been successfully set, otherwise a
799 ///          platform specific error_code.
800 /// @see temp_directory_path
801 error_code unique_file(const Twine &model, void* i_have_not_decided_the_ty_yet);
802
803 /// @brief Canonicalize path.
804 ///
805 /// Sets result to the file system's idea of what path is. The result is always
806 /// absolute and has the same capitalization as the file system.
807 ///
808 /// @param path Input path.
809 /// @param result Set to the canonicalized version of \a path.
810 /// @results errc::success if result has been successfully set, otherwise a
811 ///          platform specific error_code.
812 error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result);
813
814 /// @brief Are \a path's first bytes \a magic?
815 ///
816 /// @param path Input path.
817 /// @param magic Byte sequence to compare \a path's first len(magic) bytes to.
818 /// @results errc::success if result has been successfully set, otherwise a
819 ///          platform specific error_code.
820 error_code has_magic(const Twine &path, const Twine &magic);
821
822 /// @brief Get \a path's first \a len bytes.
823 ///
824 /// @param path Input path.
825 /// @param len Number of magic bytes to get.
826 /// @param result Set to the first \a len bytes in the file pointed to by
827 ///               \a path.
828 /// @results errc::success if result has been successfully set, otherwise a
829 ///          platform specific error_code.
830 error_code get_magic(const Twine &path, uint32_t len,
831                      SmallVectorImpl<char> &result);
832
833 /// @brief Is file bitcode?
834 ///
835 /// @param path Input path.
836 /// @param result Set to true if \a path is a bitcode file, false if it is not,
837 ///               undefined otherwise.
838 /// @results errc::success if result has been successfully set, otherwise a
839 ///          platform specific error_code.
840 error_code is_bitcode(const Twine &path, bool &result);
841
842 /// @brief Is file a dynamic library?
843 ///
844 /// @param path Input path.
845 /// @param result Set to true if \a path is a dynamic library, false if it is
846 ///               not, undefined otherwise.
847 /// @results errc::success if result has been successfully set, otherwise a
848 ///          platform specific error_code.
849 error_code is_dynamic_library(const Twine &path, bool &result);
850
851 /// @brief Is an object file?
852 ///
853 /// @param path Input path.
854 /// @param result Set to true if \a path is an object file, false if it is not,
855 ///               undefined otherwise.
856 /// @results errc::success if result has been successfully set, otherwise a
857 ///          platform specific error_code.
858 error_code is_object_file(const Twine &path, bool &result);
859
860 /// @brief Can file be read?
861 ///
862 /// @param path Input path.
863 /// @param result Set to true if \a path is readable, false it it is not,
864 ///               undefined otherwise.
865 /// @results errc::success if result has been successfully set, otherwise a
866 ///          platform specific error_code.
867 error_code can_read(const Twine &path, bool &result);
868
869 /// @brief Can file be written?
870 ///
871 /// @param path Input path.
872 /// @param result Set to true if \a path is writeable, false it it is not,
873 ///               undefined otherwise.
874 /// @results errc::success if result has been successfully set, otherwise a
875 ///          platform specific error_code.
876 error_code can_write(const Twine &path, bool &result);
877
878 /// @brief Can file be executed?
879 ///
880 /// @param path Input path.
881 /// @param result Set to true if \a path is executable, false it it is not,
882 ///               undefined otherwise.
883 /// @results errc::success if result has been successfully set, otherwise a
884 ///          platform specific error_code.
885 error_code can_execute(const Twine &path, bool &result);
886
887 /// @brief Get library paths the system linker uses.
888 ///
889 /// @param result Set to the list of system library paths.
890 /// @results errc::success if result has been successfully set, otherwise a
891 ///          platform specific error_code.
892 error_code GetSystemLibraryPaths(SmallVectorImpl<std::string> &result);
893
894 /// @brief Get bitcode library paths the system linker uses
895 ///        + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
896 ///
897 /// @param result Set to the list of bitcode library paths.
898 /// @results errc::success if result has been successfully set, otherwise a
899 ///          platform specific error_code.
900 error_code GetBitcodeLibraryPaths(SmallVectorImpl<std::string> &result);
901
902 /// @brief Find a library.
903 ///
904 /// Find the path to a library using its short name. Use the system
905 /// dependent library paths to locate the library.
906 ///
907 /// c => /usr/lib/libc.so
908 ///
909 /// @param short_name Library name one would give to the system linker.
910 /// @param result Set to the absolute path \a short_name represents.
911 /// @results errc::success if result has been successfully set, otherwise a
912 ///          platform specific error_code.
913 error_code FindLibrary(const Twine &short_name, SmallVectorImpl<char> &result);
914
915 /// @brief Get absolute path of main executable.
916 ///
917 /// @param argv0 The program name as it was spelled on the command line.
918 /// @param MainAddr Address of some symbol in the executable (not in a library).
919 /// @param result Set to the absolute path of the current executable.
920 /// @results errc::success if result has been successfully set, otherwise a
921 ///          platform specific error_code.
922 error_code GetMainExecutable(const char *argv0, void *MainAddr,
923                              SmallVectorImpl<char> &result);
924
925 /// @}
926 /// @name Iterators
927 /// @{
928
929 /// directory_entry - A single entry in a directory. Caches the status either
930 /// from the result of the iteration syscall, or the first time status or
931 /// symlink_status is called.
932 class directory_entry {
933   std::string Path;
934   mutable file_status Status;
935   mutable file_status SymlinkStatus;
936
937 public:
938   explicit directory_entry(const Twine &path, file_status st = file_status(),
939                                        file_status symlink_st = file_status());
940
941   void assign(const Twine &path, file_status st = file_status(),
942                           file_status symlink_st = file_status());
943   void replace_filename(const Twine &filename, file_status st = file_status(),
944                               file_status symlink_st = file_status());
945
946   const SmallVectorImpl<char> &path() const;
947   error_code status(file_status &result) const;
948   error_code symlink_status(file_status &result) const;
949
950   bool operator==(const directory_entry& rhs) const;
951   bool operator!=(const directory_entry& rhs) const;
952   bool operator< (const directory_entry& rhs) const;
953   bool operator<=(const directory_entry& rhs) const;
954   bool operator> (const directory_entry& rhs) const;
955   bool operator>=(const directory_entry& rhs) const;
956 };
957
958 /// directory_iterator - Iterates through the entries in path. There is no
959 /// operator++ because we need an error_code. If it's really needed we can make
960 /// it call report_fatal_error on error.
961 class directory_iterator {
962   // implementation directory iterator status
963
964 public:
965   explicit directory_iterator(const Twine &path, error_code &ec);
966   // No operator++ because we need error_code.
967   directory_iterator &increment(error_code &ec);
968
969   const directory_entry &operator*() const;
970   const directory_entry *operator->() const;
971
972   // Other members as required by
973   // C++ Std, 24.1.1 Input iterators [input.iterators]
974 };
975
976 /// recursive_directory_iterator - Same as directory_iterator except for it
977 /// recurses down into child directories.
978 class recursive_directory_iterator {
979   uint16_t  Level;
980   bool HasNoPushRequest;
981   // implementation directory iterator status
982
983 public:
984   explicit recursive_directory_iterator(const Twine &path, error_code &ec);
985   // No operator++ because we need error_code.
986   directory_iterator &increment(error_code &ec);
987
988   const directory_entry &operator*() const;
989   const directory_entry *operator->() const;
990
991   // observers
992   /// Gets the current level. path is at level 0.
993   int level() const;
994   /// Returns true if no_push has been called for this directory_entry.
995   bool no_push_request() const;
996
997   // modifiers
998   /// Goes up one level if Level > 0.
999   void pop();
1000   /// Does not go down into the current directory_entry.
1001   void no_push();
1002
1003   // Other members as required by
1004   // C++ Std, 24.1.1 Input iterators [input.iterators]
1005 };
1006
1007 /// @}
1008
1009 } // end namespace fs
1010 } // end namespace sys
1011 } // end namespace llvm
1012
1013 #endif