1 //===- llvm/Support/FileSystem.h - File System OS Concept -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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
25 //===----------------------------------------------------------------------===//
27 #ifndef LLVM_SUPPORT_FILE_SYSTEM_H
28 #define LLVM_SUPPORT_FILE_SYSTEM_H
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"
43 /// file_type - An "enum class" enumeration for the file system's view of the
59 file_type(_ v) : v_(v) {}
60 explicit file_type(int v) : v_(_(v)) {}
61 operator int() const {return v_;}
67 /// copy_option - An "enum class" enumeration of copy semantics for copy
75 copy_option(_ v) : v_(v) {}
76 explicit copy_option(int v) : v_(_(v)) {}
77 operator int() const {return v_;}
83 /// space_info - Self explanatory.
90 /// file_status - Represents the result of a call to stat and friends. It has
91 /// a platform specific member to store the result.
94 // implementation defined status field.
97 explicit file_status(file_type v=file_type::status_error)
100 file_type type() const { return Type; }
101 void type(file_type v) { Type = v; }
105 /// @name Physical Operators
108 /// @brief Make \a path an absolute path.
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.
113 /// /absolute/path => /absolute/path
114 /// relative/../path => <current-directory>/relative/../path
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);
121 /// @brief Copy the file at \a from to the path \a to.
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
130 error_code copy_file(const Twine &from, const Twine &to,
131 copy_option copt = copy_option::fail_if_exists);
133 /// @brief Create all the non-existent directories in path.
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);
141 /// @brief Create the directory in path.
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);
149 /// @brief Create a hard link from \a from to \a to.
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);
157 /// @brief Create a symbolic link from \a from to \a to.
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);
165 /// @brief Get the current path.
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);
172 /// @brief Remove path. Equivalent to POSIX remove().
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);
181 /// @brief Recursively remove all files below \a path, then \a path. Files are
182 /// removed as if by POSIX remove().
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);
190 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
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);
196 /// @brief Resize path to size. File is resized as if by POSIX truncate().
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);
204 /// @brief Make file readable.
206 /// @param path Input path.
207 /// @param value If true, make readable, else, make unreadable.
208 /// @results errc::success if readability has been successfully set, otherwise a
209 /// platform specific error_code.
210 error_code set_read(const Twine &path, bool value);
212 /// @brief Make file writeable.
214 /// @param path Input path.
215 /// @param value If true, make writeable, else, make unwriteable.
216 /// @results errc::success if writeability has been successfully set, otherwise
217 /// a platform specific error_code.
218 error_code set_write(const Twine &path, bool value);
220 /// @brief Make file executable.
222 /// @param path Input path.
223 /// @param value If true, make executable, else, make unexecutable.
224 /// @results errc::success if executability has been successfully set, otherwise
225 /// a platform specific error_code.
226 error_code set_execute(const Twine &path, bool value);
229 /// @name Physical Observers
232 /// @brief Does file exist?
234 /// @param status A file_status previously returned from stat.
235 /// @results True if the file represented by status exists, false if it does
237 bool exists(file_status status);
239 /// @brief Does file exist?
241 /// @param path Input path.
242 /// @param result Set to true if the file represented by status exists, false if
243 /// it does not. Undefined otherwise.
244 /// @results errc::success if result has been successfully set, otherwise a
245 /// platform specific error_code.
246 error_code exists(const Twine &path, bool &result);
248 /// @brief Do file_status's represent the same thing?
250 /// @param A Input file_status.
251 /// @param B Input file_status.
253 /// assert(status_known(A) || status_known(B));
255 /// @results True if A and B both represent the same file system entity, false
257 bool equivalent(file_status A, file_status B);
259 /// @brief Do paths represent the same thing?
261 /// @param A Input path A.
262 /// @param B Input path B.
263 /// @param result Set to true if stat(A) and stat(B) have the same device and
264 /// inode (or equivalent).
265 /// @results errc::success if result has been successfully set, otherwise a
266 /// platform specific error_code.
267 error_code equivalent(const Twine &A, const Twine &B, bool &result);
269 /// @brief Get file size.
271 /// @param path Input path.
272 /// @param result Set to the size of the file in \a path.
273 /// @returns errc::success if result has been successfully set, otherwise a
274 /// platform specific error_code.
275 error_code file_size(const Twine &path, uint64_t &result);
277 /// @brief Does status represent a directory?
279 /// @param status A file_status previously returned from status.
280 /// @results status.type() == file_type::directory_file.
281 bool is_directory(file_status status);
283 /// @brief Is path a directory?
285 /// @param path Input path.
286 /// @param result Set to true if \a path is a directory, 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_directory(const Twine &path, bool &result);
292 /// @brief Is path an empty file?
294 /// @param path Input path.
295 /// @param result Set to true if \a path is a an empty file, false if it is not.
296 /// Undefined otherwise.
297 /// @results errc::success if result has been successfully set, otherwise a
298 /// platform specific error_code.
299 error_code is_empty(const Twine &path, bool &result);
301 /// @brief Does status represent a regular file?
303 /// @param status A file_status previously returned from status.
304 /// @results status_known(status) && status.type() == file_type::regular_file.
305 bool is_regular_file(file_status status);
307 /// @brief Is path a regular file?
309 /// @param path Input path.
310 /// @param result Set to true if \a path is a regular file, 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_regular_file(const Twine &path, bool &result);
316 /// @brief Does this status represent something that exists but is not a
317 /// directory, regular file, or symlink?
319 /// @param status A file_status previously returned from status.
320 /// @results exists(s) && !is_regular_file(s) && !is_directory(s) &&
322 bool is_other(file_status status);
324 /// @brief Is path something that exists but is not a directory,
325 /// regular file, or symlink?
327 /// @param path Input path.
328 /// @param result Set to true if \a path exists, but is not a directory, regular
329 /// file, or a symlink, false if it does not. Undefined otherwise.
330 /// @results errc::success if result has been successfully set, otherwise a
331 /// platform specific error_code.
332 error_code is_other(const Twine &path, bool &result);
334 /// @brief Does status represent a symlink?
336 /// @param status A file_status previously returned from stat.
337 /// @param result status.type() == symlink_file.
338 bool is_symlink(file_status status);
340 /// @brief Is path a symlink?
342 /// @param path Input path.
343 /// @param result Set to true if \a path is a symlink, false if it is not.
344 /// Undefined otherwise.
345 /// @results errc::success if result has been successfully set, otherwise a
346 /// platform specific error_code.
347 error_code is_symlink(const Twine &path, bool &result);
349 /// @brief Get last write time without changing it.
351 /// @param path Input path.
352 /// @param result Set to the last write time (UNIX time) of \a path if it
354 /// @results errc::success if result has been successfully set, otherwise a
355 /// platform specific error_code.
356 error_code last_write_time(const Twine &path, std::time_t &result);
358 /// @brief Set last write time.
360 /// @param path Input path.
361 /// @param value Time to set (UNIX time) \a path's last write time to.
362 /// @results errc::success if result has been successfully set, otherwise a
363 /// platform specific error_code.
364 error_code set_last_write_time(const Twine &path, std::time_t value);
366 /// @brief Read a symlink's value.
368 /// @param path Input path.
369 /// @param result Set to the value of the symbolic link \a path.
370 /// @results errc::success if result has been successfully set, otherwise a
371 /// platform specific error_code.
372 error_code read_symlink(const Twine &path, SmallVectorImpl<char> &result);
374 /// @brief Get disk space usage information.
376 /// @param path Input path.
377 /// @param result Set to the capacity, free, and available space on the device
379 /// @results errc::success if result has been successfully set, otherwise a
380 /// platform specific error_code.
381 error_code disk_space(const Twine &path, space_info &result);
383 /// @brief Get file status as if by POSIX stat().
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 status(const Twine &path, file_status &result);
391 /// @brief Is status available?
393 /// @param path Input path.
394 /// @results True if status() != status_error.
395 bool status_known(file_status s);
397 /// @brief Is status available?
399 /// @param path Input path.
400 /// @param result Set to true if status() != status_error.
401 /// @results errc::success if result has been successfully set, otherwise a
402 /// platform specific error_code.
403 error_code status_known(const Twine &path, bool &result);
405 /// @brief Get file status as if by POSIX lstat().
407 /// Does not resolve symlinks.
409 /// @param path Input path.
410 /// @param result Set to the file status.
411 /// @results errc::success if result has been successfully set, otherwise a
412 /// platform specific error_code.
413 error_code symlink_status(const Twine &path, file_status &result);
415 /// @brief Generate a unique path and open it as a file.
417 /// Generates a unique path suitable for a temporary file and then opens it as a
418 /// file. The name is based on \a model with '%' replaced by a random char in
419 /// [0-9a-f]. If \a model is not an absolute path, a suitable temporary
420 /// directory will be prepended.
422 /// This is an atomic operation. Either the file is created and opened, or the
423 /// file system is left untouched.
425 /// clang-%%-%%-%%-%%-%%.s => /tmp/clang-a0-b1-c2-d3-e4.s
427 /// @param model Name to base unique path off of.
428 /// @param result_fs Set to the opened file's file descriptor.
429 /// @param result_path Set to the opened file's absolute path.
430 /// @param makeAbsolute If true and @model is not an absolute path, a temp
431 /// directory will be prepended.
432 /// @results errc::success if result_{fd,path} have been successfully set,
433 /// otherwise a platform specific error_code.
434 error_code unique_file(const Twine &model, int &result_fd,
435 SmallVectorImpl<char> &result_path,
436 bool makeAbsolute = true);
438 /// @brief Canonicalize path.
440 /// Sets result to the file system's idea of what path is. The result is always
441 /// absolute and has the same capitalization as the file system.
443 /// @param path Input path.
444 /// @param result Set to the canonicalized version of \a path.
445 /// @results errc::success if result has been successfully set, otherwise a
446 /// platform specific error_code.
447 error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result);
449 /// @brief Are \a path's first bytes \a magic?
451 /// @param path Input path.
452 /// @param magic Byte sequence to compare \a path's first len(magic) bytes to.
453 /// @results errc::success if result has been successfully set, otherwise a
454 /// platform specific error_code.
455 error_code has_magic(const Twine &path, const Twine &magic, bool &result);
457 /// @brief Get \a path's first \a len bytes.
459 /// @param path Input path.
460 /// @param len Number of magic bytes to get.
461 /// @param result Set to the first \a len bytes in the file pointed to by
462 /// \a path. Or the entire file if file_size(path) < len, in which
463 /// case result.size() returns the size of the file.
464 /// @results errc::success if result has been successfully set,
465 /// errc::value_too_large if len is larger then the file pointed to by
466 /// \a path, otherwise a platform specific error_code.
467 error_code get_magic(const Twine &path, uint32_t len,
468 SmallVectorImpl<char> &result);
470 /// @brief Get and identify \a path's type based on its content.
472 /// @param path Input path.
473 /// @param result Set to the type of file, or LLVMFileType::Unknown_FileType.
474 /// @results errc::success if result has been successfully set, otherwise a
475 /// platform specific error_code.
476 error_code identify_magic(const Twine &path, LLVMFileType &result);
478 /// @brief Is file bitcode?
480 /// @param path Input path.
481 /// @param result Set to true if \a path is a bitcode file, false if it is not,
482 /// undefined otherwise.
483 /// @results errc::success if result has been successfully set, otherwise a
484 /// platform specific error_code.
485 error_code is_bitcode(const Twine &path, bool &result);
487 /// @brief Is file a dynamic library?
489 /// @param path Input path.
490 /// @param result Set to true if \a path is a dynamic library, false if it is
491 /// not, undefined otherwise.
492 /// @results errc::success if result has been successfully set, otherwise a
493 /// platform specific error_code.
494 error_code is_dynamic_library(const Twine &path, bool &result);
496 /// @brief Is an object file?
498 /// @param path Input path.
499 /// @param result Set to true if \a path is an object file, false if it is not,
500 /// undefined otherwise.
501 /// @results errc::success if result has been successfully set, otherwise a
502 /// platform specific error_code.
503 error_code is_object_file(const Twine &path, bool &result);
505 /// @brief Can file be read?
507 /// @param path Input path.
508 /// @param result Set to true if \a path is readable, false it it is not,
509 /// undefined otherwise.
510 /// @results errc::success if result has been successfully set, otherwise a
511 /// platform specific error_code.
512 error_code can_read(const Twine &path, bool &result);
514 /// @brief Can file be written?
516 /// @param path Input path.
517 /// @param result Set to true if \a path is writeable, false it it is not,
518 /// undefined otherwise.
519 /// @results errc::success if result has been successfully set, otherwise a
520 /// platform specific error_code.
521 error_code can_write(const Twine &path, bool &result);
523 /// @brief Can file be executed?
525 /// @param path Input path.
526 /// @param result Set to true if \a path is executable, false it it is not,
527 /// undefined otherwise.
528 /// @results errc::success if result has been successfully set, otherwise a
529 /// platform specific error_code.
530 error_code can_execute(const Twine &path, bool &result);
532 /// @brief Get library paths the system linker uses.
534 /// @param result Set to the list of system library paths.
535 /// @results errc::success if result has been successfully set, otherwise a
536 /// platform specific error_code.
537 error_code GetSystemLibraryPaths(SmallVectorImpl<std::string> &result);
539 /// @brief Get bitcode library paths the system linker uses
540 /// + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
542 /// @param result Set to the list of bitcode library paths.
543 /// @results errc::success if result has been successfully set, otherwise a
544 /// platform specific error_code.
545 error_code GetBitcodeLibraryPaths(SmallVectorImpl<std::string> &result);
547 /// @brief Find a library.
549 /// Find the path to a library using its short name. Use the system
550 /// dependent library paths to locate the library.
552 /// c => /usr/lib/libc.so
554 /// @param short_name Library name one would give to the system linker.
555 /// @param result Set to the absolute path \a short_name represents.
556 /// @results errc::success if result has been successfully set, otherwise a
557 /// platform specific error_code.
558 error_code FindLibrary(const Twine &short_name, SmallVectorImpl<char> &result);
560 /// @brief Get absolute path of main executable.
562 /// @param argv0 The program name as it was spelled on the command line.
563 /// @param MainAddr Address of some symbol in the executable (not in a library).
564 /// @param result Set to the absolute path of the current executable.
565 /// @results errc::success if result has been successfully set, otherwise a
566 /// platform specific error_code.
567 error_code GetMainExecutable(const char *argv0, void *MainAddr,
568 SmallVectorImpl<char> &result);
574 /// directory_entry - A single entry in a directory. Caches the status either
575 /// from the result of the iteration syscall, or the first time status or
576 /// symlink_status is called.
577 class directory_entry {
579 mutable file_status Status;
580 mutable file_status SymlinkStatus;
583 explicit directory_entry(const Twine &path, file_status st = file_status(),
584 file_status symlink_st = file_status())
587 , SymlinkStatus(symlink_st) {}
591 void assign(const Twine &path, file_status st = file_status(),
592 file_status symlink_st = file_status()) {
595 SymlinkStatus = symlink_st;
598 void replace_filename(const Twine &filename, file_status st = file_status(),
599 file_status symlink_st = file_status());
601 const std::string &path() const { return Path; }
602 error_code status(file_status &result) const;
603 error_code symlink_status(file_status &result) const;
605 bool operator==(const directory_entry& rhs) const { return Path == rhs.Path; }
606 bool operator!=(const directory_entry& rhs) const { return !(*this == rhs); }
607 bool operator< (const directory_entry& rhs) const;
608 bool operator<=(const directory_entry& rhs) const;
609 bool operator> (const directory_entry& rhs) const;
610 bool operator>=(const directory_entry& rhs) const;
613 /// directory_iterator - Iterates through the entries in path. There is no
614 /// operator++ because we need an error_code. If it's really needed we can make
615 /// it call report_fatal_error on error.
616 class directory_iterator {
617 intptr_t IterationHandle;
618 directory_entry CurrentEntry;
620 // Platform implementations implement these functions to handle iteration.
621 friend error_code directory_iterator_construct(directory_iterator &it,
623 friend error_code directory_iterator_increment(directory_iterator &it);
624 friend error_code directory_iterator_destruct(directory_iterator &it);
627 explicit directory_iterator(const Twine &path, error_code &ec)
628 : IterationHandle(0) {
629 SmallString<128> path_storage;
630 ec = directory_iterator_construct(*this, path.toStringRef(path_storage));
633 /// Construct end iterator.
634 directory_iterator() : IterationHandle(0) {}
636 ~directory_iterator() {
637 directory_iterator_destruct(*this);
640 // No operator++ because we need error_code.
641 directory_iterator &increment(error_code &ec) {
642 ec = directory_iterator_increment(*this);
646 const directory_entry &operator*() const { return CurrentEntry; }
647 const directory_entry *operator->() const { return &CurrentEntry; }
649 bool operator!=(const directory_iterator &RHS) const {
650 return CurrentEntry != RHS.CurrentEntry;
652 // Other members as required by
653 // C++ Std, 24.1.1 Input iterators [input.iterators]
656 /// recursive_directory_iterator - Same as directory_iterator except for it
657 /// recurses down into child directories.
658 class recursive_directory_iterator {
660 bool HasNoPushRequest;
661 // implementation directory iterator status
664 explicit recursive_directory_iterator(const Twine &path, error_code &ec);
665 // No operator++ because we need error_code.
666 directory_iterator &increment(error_code &ec);
668 const directory_entry &operator*() const;
669 const directory_entry *operator->() const;
672 /// Gets the current level. path is at level 0.
674 /// Returns true if no_push has been called for this directory_entry.
675 bool no_push_request() const;
678 /// Goes up one level if Level > 0.
680 /// Does not go down into the current directory_entry.
683 // Other members as required by
684 // C++ Std, 24.1.1 Input iterators [input.iterators]
689 } // end namespace fs
690 } // end namespace sys
691 } // end namespace llvm