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