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