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