Support/FileSystem: Add status implementation.
[oota-llvm.git] / include / llvm / Support / FileSystem.h
1 //===- llvm/Support/FileSystem.h - File System OS 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::fs 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 #ifndef LLVM_SUPPORT_FILE_SYSTEM_H
28 #define LLVM_SUPPORT_FILE_SYSTEM_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 namespace sys {
40 namespace fs {
41
42 /// file_type - An "enum class" enumeration for the file system's view of the
43 ///             type.
44 struct file_type {
45   enum _ {
46     status_error,
47     file_not_found,
48     regular_file,
49     directory_file,
50     symlink_file,
51     block_file,
52     character_file,
53     fifo_file,
54     socket_file,
55     type_unknown
56   };
57
58   file_type(_ v) : v_(v) {}
59   explicit file_type(int v) : v_(_(v)) {}
60   operator int() const {return v_;}
61
62 private:
63   int v_;
64 };
65
66 /// copy_option - An "enum class" enumeration of copy semantics for copy
67 ///               operations.
68 struct copy_option {
69   enum _ {
70     fail_if_exists,
71     overwrite_if_exists
72   };
73
74   copy_option(_ v) : v_(v) {}
75   explicit copy_option(int v) : v_(_(v)) {}
76   operator int() const {return v_;}
77
78 private:
79   int v_;
80 };
81
82 /// space_info - Self explanatory.
83 struct space_info {
84   uint64_t capacity;
85   uint64_t free;
86   uint64_t available;
87 };
88
89 /// file_status - Represents the result of a call to stat and friends. It has
90 ///               a platform specific member to store the result.
91 class file_status
92 {
93   // implementation defined status field.
94   file_type Type;
95 public:
96   explicit file_status(file_type v=file_type::status_error)
97     : Type(v) {}
98
99   file_type type() const { return Type; }
100   void type(file_type v) { Type = v; }
101 };
102
103 /// @}
104 /// @name Physical Operators
105 /// @{
106
107 /// @brief Copy the file at \a from to the path \a to.
108 ///
109 /// @param from The path to copy the file from.
110 /// @param to The path to copy the file to.
111 /// @param copt Behavior if \a to already exists.
112 /// @returns errc::success if the file has been successfully copied.
113 ///          errc::file_exists if \a to already exists and \a copt ==
114 ///          copy_option::fail_if_exists. Otherwise a platform specific
115 ///          error_code.
116 error_code copy_file(const Twine &from, const Twine &to,
117                      copy_option copt = copy_option::fail_if_exists);
118
119 /// @brief Create all the non-existent directories in path.
120 ///
121 /// @param path Directories to create.
122 /// @param existed Set to true if \a path already existed, false otherwise.
123 /// @returns errc::success if is_directory(path) and existed have been set,
124 ///          otherwise a platform specific error_code.
125 error_code create_directories(const Twine &path, bool &existed);
126
127 /// @brief Create the directory in path.
128 ///
129 /// @param path Directory to create.
130 /// @param existed Set to true if \a path already existed, false otherwise.
131 /// @returns errc::success if is_directory(path) and existed have been set,
132 ///          otherwise a platform specific error_code.
133 error_code create_directory(const Twine &path, bool &existed);
134
135 /// @brief Create a hard link from \a from to \a to.
136 ///
137 /// @param to The path to hard link to.
138 /// @param from The path to hard link from. This is created.
139 /// @returns errc::success if exists(to) && exists(from) && equivalent(to, from)
140 ///          , otherwise a platform specific error_code.
141 error_code create_hard_link(const Twine &to, const Twine &from);
142
143 /// @brief Create a symbolic link from \a from to \a to.
144 ///
145 /// @param to The path to symbolically link to.
146 /// @param from The path to symbolically link from. This is created.
147 /// @returns errc::success if exists(to) && exists(from) && is_symlink(from),
148 ///          otherwise a platform specific error_code.
149 error_code create_symlink(const Twine &to, const Twine &from);
150
151 /// @brief Remove path. Equivalent to POSIX remove().
152 ///
153 /// @param path Input path.
154 /// @param existed Set to true if \a path existed, false if it did not.
155 ///                undefined otherwise.
156 /// @results errc::success if path has been removed and existed has been
157 ///          successfully set, otherwise a platform specific error_code.
158 error_code remove(const Twine &path, bool &existed);
159
160 /// @brief Recursively remove all files below \a path, then \a path. Files are
161 ///        removed as if by POSIX remove().
162 ///
163 /// @param path Input path.
164 /// @param num_removed Number of files removed.
165 /// @results errc::success if path has been removed and num_removed has been
166 ///          successfully set, otherwise a platform specific error_code.
167 error_code remove_all(const Twine &path, uint32_t &num_removed);
168
169 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
170 ///
171 /// @param from The path to rename from.
172 /// @param to The path to rename to. This is created.
173 error_code rename(const Twine &from, const Twine &to);
174
175 /// @brief Resize path to size. File is resized as if by POSIX truncate().
176 ///
177 /// @param path Input path.
178 /// @param size Size to resize to.
179 /// @returns errc::success if \a path has been resized to \a size, otherwise a
180 ///          platform specific error_code.
181 error_code resize_file(const Twine &path, uint64_t size);
182
183 /// @brief Make file readable.
184 ///
185 /// @param path Input path.
186 /// @param value If true, make readable, else, make unreadable.
187 /// @results errc::success if readability has been successfully set, otherwise a
188 ///          platform specific error_code.
189 error_code set_read(const Twine &path, bool value);
190
191 /// @brief Make file writeable.
192 ///
193 /// @param path Input path.
194 /// @param value If true, make writeable, else, make unwriteable.
195 /// @results errc::success if writeability has been successfully set, otherwise
196 ///          a platform specific error_code.
197 error_code set_write(const Twine &path, bool value);
198
199 /// @brief Make file executable.
200 ///
201 /// @param path Input path.
202 /// @param value If true, make executable, else, make unexecutable.
203 /// @results errc::success if executability has been successfully set, otherwise
204 ///          a platform specific error_code.
205 error_code set_execute(const Twine &path, bool value);
206
207 /// @}
208 /// @name Physical Observers
209 /// @{
210
211 /// @brief Does file exist?
212 ///
213 /// @param status A file_status previously returned from stat.
214 /// @param result Set to true if the file represented by status exists, false if
215 ///               it does not. Undefined otherwise.
216 /// @results errc::success if result has been successfully set, otherwise a
217 ///          platform specific error_code.
218 error_code exists(file_status status, bool &result);
219
220 /// @brief Does file exist?
221 ///
222 /// @param path Input path.
223 /// @param result Set to true if the file represented by status exists, false if
224 ///               it does not. Undefined otherwise.
225 /// @results errc::success if result has been successfully set, otherwise a
226 ///          platform specific error_code.
227 error_code exists(const Twine &path, bool &result);
228
229 /// @brief Do paths represent the same thing?
230 ///
231 /// @param A Input path A.
232 /// @param B Input path B.
233 /// @param result Set to true if stat(A) and stat(B) have the same device and
234 ///               inode (or equivalent).
235 /// @results errc::success if result has been successfully set, otherwise a
236 ///          platform specific error_code.
237 error_code equivalent(const Twine &A, const Twine &B, bool &result);
238
239 /// @brief Get file size.
240 ///
241 /// @param path Input path.
242 /// @param result Set to the size of the file in \a path.
243 /// @returns errc::success if result has been successfully set, otherwise a
244 ///          platform specific error_code.
245 error_code file_size(const Twine &path, uint64_t &result);
246
247 /// @brief Does status represent a directory?
248 ///
249 /// @param status A file_status previously returned from stat.
250 /// @param result Set to true if the file represented by status is a directory,
251 ///               false if it is not. Undefined otherwise.
252 /// @results errc::success if result has been successfully set, otherwise a
253 ///          platform specific error_code.
254 error_code is_directory(file_status status, bool &result);
255
256 /// @brief Is path a directory?
257 ///
258 /// @param path Input path.
259 /// @param result Set to true if \a path is a directory, false if it is not.
260 ///               Undefined otherwise.
261 /// @results errc::success if result has been successfully set, otherwise a
262 ///          platform specific error_code.
263 error_code is_directory(const Twine &path, bool &result);
264
265 /// @brief Is path an empty file?
266 ///
267 /// @param path Input path.
268 /// @param result Set to true if \a path is a an empty file, false if it is not.
269 ///               Undefined otherwise.
270 /// @results errc::success if result has been successfully set, otherwise a
271 ///          platform specific error_code.
272 error_code is_empty(const Twine &path, bool &result);
273
274 /// @brief Does status represent a regular file?
275 ///
276 /// @param status A file_status previously returned from stat.
277 /// @param result Set to true if the file represented by status is a regular
278 ///               file, false if it is not. Undefined otherwise.
279 /// @results errc::success if result has been successfully set, otherwise a
280 ///          platform specific error_code.
281 error_code is_regular_file(file_status status, bool &result);
282
283 /// @brief Is path a regular file?
284 ///
285 /// @param path Input path.
286 /// @param result Set to true if \a path is a regular file, false if it is not.
287 ///               Undefined otherwise.
288 /// @results errc::success if result has been successfully set, otherwise a
289 ///          platform specific error_code.
290 error_code is_regular_file(const Twine &path, bool &result);
291
292 /// @brief Does status represent something that exists but is not a directory,
293 ///        regular file, or symlink?
294 ///
295 /// @param status A file_status previously returned from stat.
296 /// @param result Set to true if the file represented by status exists, but is
297 ///               not a directory, regular file, or a symlink, false if it does
298 ///               not. Undefined otherwise.
299 /// @results errc::success if result has been successfully set, otherwise a
300 ///          platform specific error_code.
301 error_code is_other(file_status status, bool &result);
302
303 /// @brief Is path something that exists but is not a directory,
304 ///        regular file, or symlink?
305 ///
306 /// @param path Input path.
307 /// @param result Set to true if \a path exists, but is not a directory, regular
308 ///               file, or a symlink, false if it does not. Undefined otherwise.
309 /// @results errc::success if result has been successfully set, otherwise a
310 ///          platform specific error_code.
311 error_code is_other(const Twine &path, bool &result);
312
313 /// @brief Does status represent a symlink?
314 ///
315 /// @param status A file_status previously returned from stat.
316 /// @param result Set to true if the file represented by status is a symlink,
317 ///               false if it is not. Undefined otherwise.
318 /// @results errc::success if result has been successfully set, otherwise a
319 ///          platform specific error_code.
320 error_code is_symlink(file_status status, bool &result);
321
322 /// @brief Is path a symlink?
323 ///
324 /// @param path Input path.
325 /// @param result Set to true if \a path is a symlink, false if it is not.
326 ///               Undefined otherwise.
327 /// @results errc::success if result has been successfully set, otherwise a
328 ///          platform specific error_code.
329 error_code is_symlink(const Twine &path, bool &result);
330
331 /// @brief Get last write time without changing it.
332 ///
333 /// @param path Input path.
334 /// @param result Set to the last write time (UNIX time) of \a path if it
335 ///               exists.
336 /// @results errc::success if result has been successfully set, otherwise a
337 ///          platform specific error_code.
338 error_code last_write_time(const Twine &path, std::time_t &result);
339
340 /// @brief Set last write time.
341 ///
342 /// @param path Input path.
343 /// @param value Time to set (UNIX time) \a path's last write time to.
344 /// @results errc::success if result has been successfully set, otherwise a
345 ///          platform specific error_code.
346 error_code set_last_write_time(const Twine &path, std::time_t value);
347
348 /// @brief Read a symlink's value.
349 ///
350 /// @param path Input path.
351 /// @param result Set to the value of the symbolic link \a path.
352 /// @results errc::success if result has been successfully set, otherwise a
353 ///          platform specific error_code.
354 error_code read_symlink(const Twine &path, SmallVectorImpl<char> &result);
355
356 /// @brief Get disk space usage information.
357 ///
358 /// @param path Input path.
359 /// @param result Set to the capacity, free, and available space on the device
360 ///               \a path is on.
361 /// @results errc::success if result has been successfully set, otherwise a
362 ///          platform specific error_code.
363 error_code disk_space(const Twine &path, space_info &result);
364
365 /// @brief Get file status as if by POSIX stat().
366 ///
367 /// @param path Input path.
368 /// @param result Set to the file status.
369 /// @results errc::success if result has been successfully set, otherwise a
370 ///          platform specific error_code.
371 error_code status(const Twine &path, file_status &result);
372
373 /// @brief Is status available?
374 ///
375 /// @param path Input path.
376 /// @param result Set to true if status() != status_error.
377 /// @results errc::success if result has been successfully set, otherwise a
378 ///          platform specific error_code.
379 error_code status_known(const Twine &path, bool &result);
380
381 /// @brief Get file status as if by POSIX lstat().
382 ///
383 /// Does not resolve symlinks.
384 ///
385 /// @param path Input path.
386 /// @param result Set to the file status.
387 /// @results errc::success if result has been successfully set, otherwise a
388 ///          platform specific error_code.
389 error_code symlink_status(const Twine &path, file_status &result);
390
391 /// @brief Generate a unique path and open it as a file.
392 ///
393 /// Generates a unique path suitable for a temporary file and then opens it as a
394 /// file. The name is based on \a model with '%' replaced by a random char in
395 /// [0-9a-f]. If \a model is not an absolute path, a suitable temporary
396 /// directory will be prepended.
397 ///
398 /// This is an atomic operation. Either the file is created and opened, or the
399 /// file system is left untouched.
400 ///
401 /// clang-%%-%%-%%-%%-%%.s => /tmp/clang-a0-b1-c2-d3-e4.s
402 ///
403 /// @param model Name to base unique path off of.
404 /// @param result_fs Set to the opened file's file descriptor.
405 /// @param result_path Set to the opened file's absolute path.
406 /// @results errc::success if result_{fd,path} have been successfully set,
407 ///          otherwise a platform specific error_code.
408 error_code unique_file(const Twine &model, int &result_fd,
409                              SmallVectorImpl<char> &result_path);
410
411 /// @brief Canonicalize path.
412 ///
413 /// Sets result to the file system's idea of what path is. The result is always
414 /// absolute and has the same capitalization as the file system.
415 ///
416 /// @param path Input path.
417 /// @param result Set to the canonicalized version of \a path.
418 /// @results errc::success if result has been successfully set, otherwise a
419 ///          platform specific error_code.
420 error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result);
421
422 /// @brief Are \a path's first bytes \a magic?
423 ///
424 /// @param path Input path.
425 /// @param magic Byte sequence to compare \a path's first len(magic) bytes to.
426 /// @results errc::success if result has been successfully set, otherwise a
427 ///          platform specific error_code.
428 error_code has_magic(const Twine &path, const Twine &magic);
429
430 /// @brief Get \a path's first \a len bytes.
431 ///
432 /// @param path Input path.
433 /// @param len Number of magic bytes to get.
434 /// @param result Set to the first \a len bytes in the file pointed to by
435 ///               \a path.
436 /// @results errc::success if result has been successfully set, otherwise a
437 ///          platform specific error_code.
438 error_code get_magic(const Twine &path, uint32_t len,
439                      SmallVectorImpl<char> &result);
440
441 /// @brief Is file bitcode?
442 ///
443 /// @param path Input path.
444 /// @param result Set to true if \a path is a bitcode file, false if it is not,
445 ///               undefined otherwise.
446 /// @results errc::success if result has been successfully set, otherwise a
447 ///          platform specific error_code.
448 error_code is_bitcode(const Twine &path, bool &result);
449
450 /// @brief Is file a dynamic library?
451 ///
452 /// @param path Input path.
453 /// @param result Set to true if \a path is a dynamic library, false if it is
454 ///               not, undefined otherwise.
455 /// @results errc::success if result has been successfully set, otherwise a
456 ///          platform specific error_code.
457 error_code is_dynamic_library(const Twine &path, bool &result);
458
459 /// @brief Is an object file?
460 ///
461 /// @param path Input path.
462 /// @param result Set to true if \a path is an object file, false if it is not,
463 ///               undefined otherwise.
464 /// @results errc::success if result has been successfully set, otherwise a
465 ///          platform specific error_code.
466 error_code is_object_file(const Twine &path, bool &result);
467
468 /// @brief Can file be read?
469 ///
470 /// @param path Input path.
471 /// @param result Set to true if \a path is readable, false it it is not,
472 ///               undefined otherwise.
473 /// @results errc::success if result has been successfully set, otherwise a
474 ///          platform specific error_code.
475 error_code can_read(const Twine &path, bool &result);
476
477 /// @brief Can file be written?
478 ///
479 /// @param path Input path.
480 /// @param result Set to true if \a path is writeable, false it it is not,
481 ///               undefined otherwise.
482 /// @results errc::success if result has been successfully set, otherwise a
483 ///          platform specific error_code.
484 error_code can_write(const Twine &path, bool &result);
485
486 /// @brief Can file be executed?
487 ///
488 /// @param path Input path.
489 /// @param result Set to true if \a path is executable, false it it is not,
490 ///               undefined otherwise.
491 /// @results errc::success if result has been successfully set, otherwise a
492 ///          platform specific error_code.
493 error_code can_execute(const Twine &path, bool &result);
494
495 /// @brief Get library paths the system linker uses.
496 ///
497 /// @param result Set to the list of system library paths.
498 /// @results errc::success if result has been successfully set, otherwise a
499 ///          platform specific error_code.
500 error_code GetSystemLibraryPaths(SmallVectorImpl<std::string> &result);
501
502 /// @brief Get bitcode library paths the system linker uses
503 ///        + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
504 ///
505 /// @param result Set to the list of bitcode library paths.
506 /// @results errc::success if result has been successfully set, otherwise a
507 ///          platform specific error_code.
508 error_code GetBitcodeLibraryPaths(SmallVectorImpl<std::string> &result);
509
510 /// @brief Find a library.
511 ///
512 /// Find the path to a library using its short name. Use the system
513 /// dependent library paths to locate the library.
514 ///
515 /// c => /usr/lib/libc.so
516 ///
517 /// @param short_name Library name one would give to the system linker.
518 /// @param result Set to the absolute path \a short_name represents.
519 /// @results errc::success if result has been successfully set, otherwise a
520 ///          platform specific error_code.
521 error_code FindLibrary(const Twine &short_name, SmallVectorImpl<char> &result);
522
523 /// @brief Get absolute path of main executable.
524 ///
525 /// @param argv0 The program name as it was spelled on the command line.
526 /// @param MainAddr Address of some symbol in the executable (not in a library).
527 /// @param result Set to the absolute path of the current executable.
528 /// @results errc::success if result has been successfully set, otherwise a
529 ///          platform specific error_code.
530 error_code GetMainExecutable(const char *argv0, void *MainAddr,
531                              SmallVectorImpl<char> &result);
532
533 /// @}
534 /// @name Iterators
535 /// @{
536
537 /// directory_entry - A single entry in a directory. Caches the status either
538 /// from the result of the iteration syscall, or the first time status or
539 /// symlink_status is called.
540 class directory_entry {
541   std::string Path;
542   mutable file_status Status;
543   mutable file_status SymlinkStatus;
544
545 public:
546   explicit directory_entry(const Twine &path, file_status st = file_status(),
547                                        file_status symlink_st = file_status());
548
549   void assign(const Twine &path, file_status st = file_status(),
550                           file_status symlink_st = file_status());
551   void replace_filename(const Twine &filename, file_status st = file_status(),
552                               file_status symlink_st = file_status());
553
554   const SmallVectorImpl<char> &path() const;
555   error_code status(file_status &result) const;
556   error_code symlink_status(file_status &result) const;
557
558   bool operator==(const directory_entry& rhs) const;
559   bool operator!=(const directory_entry& rhs) const;
560   bool operator< (const directory_entry& rhs) const;
561   bool operator<=(const directory_entry& rhs) const;
562   bool operator> (const directory_entry& rhs) const;
563   bool operator>=(const directory_entry& rhs) const;
564 };
565
566 /// directory_iterator - Iterates through the entries in path. There is no
567 /// operator++ because we need an error_code. If it's really needed we can make
568 /// it call report_fatal_error on error.
569 class directory_iterator {
570   // implementation directory iterator status
571
572 public:
573   explicit directory_iterator(const Twine &path, error_code &ec);
574   // No operator++ because we need error_code.
575   directory_iterator &increment(error_code &ec);
576
577   const directory_entry &operator*() const;
578   const directory_entry *operator->() const;
579
580   // Other members as required by
581   // C++ Std, 24.1.1 Input iterators [input.iterators]
582 };
583
584 /// recursive_directory_iterator - Same as directory_iterator except for it
585 /// recurses down into child directories.
586 class recursive_directory_iterator {
587   uint16_t  Level;
588   bool HasNoPushRequest;
589   // implementation directory iterator status
590
591 public:
592   explicit recursive_directory_iterator(const Twine &path, error_code &ec);
593   // No operator++ because we need error_code.
594   directory_iterator &increment(error_code &ec);
595
596   const directory_entry &operator*() const;
597   const directory_entry *operator->() const;
598
599   // observers
600   /// Gets the current level. path is at level 0.
601   int level() const;
602   /// Returns true if no_push has been called for this directory_entry.
603   bool no_push_request() const;
604
605   // modifiers
606   /// Goes up one level if Level > 0.
607   void pop();
608   /// Does not go down into the current directory_entry.
609   void no_push();
610
611   // Other members as required by
612   // C++ Std, 24.1.1 Input iterators [input.iterators]
613 };
614
615 /// @}
616
617 } // end namespace fs
618 } // end namespace sys
619 } // end namespace llvm
620
621 #endif