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/system_error.h"
42 /// file_type - An "enum class" enumeration for the file system's view of the
58 file_type(_ v) : v_(v) {}
59 explicit file_type(int v) : v_(_(v)) {}
60 operator int() const {return v_;}
66 /// copy_option - An "enum class" enumeration of copy semantics for copy
74 copy_option(_ v) : v_(v) {}
75 explicit copy_option(int v) : v_(_(v)) {}
76 operator int() const {return v_;}
82 /// space_info - Self explanatory.
89 /// file_status - Represents the result of a call to stat and friends. It has
90 /// a platform specific member to store the result.
93 // implementation defined status field.
96 explicit file_status(file_type v=file_type::status_error)
99 file_type type() const { return Type; }
100 void type(file_type v) { Type = v; }
104 /// @name Physical Operators
107 /// @brief Copy the file at \a from to the path \a to.
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
116 error_code copy_file(const Twine &from, const Twine &to,
117 copy_option copt = copy_option::fail_if_exists);
119 /// @brief Create all the non-existent directories in path.
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);
127 /// @brief Create the directory in path.
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);
135 /// @brief Create a hard link from \a from to \a to.
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);
143 /// @brief Create a symbolic link from \a from to \a to.
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);
151 /// @brief Remove path. Equivalent to POSIX remove().
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);
160 /// @brief Recursively remove all files below \a path, then \a path. Files are
161 /// removed as if by POSIX remove().
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);
169 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
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);
175 /// @brief Resize path to size. File is resized as if by POSIX truncate().
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);
183 /// @brief Make file readable.
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);
191 /// @brief Make file writeable.
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);
199 /// @brief Make file executable.
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);
208 /// @name Physical Observers
211 /// @brief Does file exist?
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);
220 /// @brief Does file exist?
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);
229 /// @brief Do paths represent the same thing?
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);
239 /// @brief Get file size.
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);
247 /// @brief Does status represent a directory?
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);
256 /// @brief Is path a directory?
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);
265 /// @brief Is path an empty file?
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);
274 /// @brief Does status represent a regular file?
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);
283 /// @brief Is path a regular file?
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);
292 /// @brief Does status represent something that exists but is not a directory,
293 /// regular file, or symlink?
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);
303 /// @brief Is path something that exists but is not a directory,
304 /// regular file, or symlink?
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);
313 /// @brief Does status represent a symlink?
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);
322 /// @brief Is path a symlink?
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);
331 /// @brief Get last write time without changing it.
333 /// @param path Input path.
334 /// @param result Set to the last write time (UNIX time) of \a path if it
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);
340 /// @brief Set last write time.
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);
348 /// @brief Read a symlink's value.
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);
356 /// @brief Get disk space usage information.
358 /// @param path Input path.
359 /// @param result Set to the capacity, free, and available space on the device
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);
365 /// @brief Get file status as if by POSIX stat().
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);
373 /// @brief Is status available?
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);
381 /// @brief Get file status as if by POSIX lstat().
383 /// Does not resolve symlinks.
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);
391 /// @brief Generate a unique path and open it as a file.
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.
398 /// This is an atomic operation. Either the file is created and opened, or the
399 /// file system is left untouched.
401 /// clang-%%-%%-%%-%%-%%.s => /tmp/clang-a0-b1-c2-d3-e4.s
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);
411 /// @brief Canonicalize path.
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.
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);
422 /// @brief Are \a path's first bytes \a magic?
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);
430 /// @brief Get \a path's first \a len bytes.
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
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);
441 /// @brief Is file bitcode?
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);
450 /// @brief Is file a dynamic library?
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);
459 /// @brief Is an object file?
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);
468 /// @brief Can file be read?
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);
477 /// @brief Can file be written?
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);
486 /// @brief Can file be executed?
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);
495 /// @brief Get library paths the system linker uses.
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);
502 /// @brief Get bitcode library paths the system linker uses
503 /// + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
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);
510 /// @brief Find a library.
512 /// Find the path to a library using its short name. Use the system
513 /// dependent library paths to locate the library.
515 /// c => /usr/lib/libc.so
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);
523 /// @brief Get absolute path of main executable.
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);
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 {
542 mutable file_status Status;
543 mutable file_status SymlinkStatus;
546 explicit directory_entry(const Twine &path, file_status st = file_status(),
547 file_status symlink_st = file_status())
550 , SymlinkStatus(symlink_st) {}
554 void assign(const Twine &path, file_status st = file_status(),
555 file_status symlink_st = file_status()) {
558 SymlinkStatus = symlink_st;
561 void replace_filename(const Twine &filename, file_status st = file_status(),
562 file_status symlink_st = file_status());
564 StringRef path() const { return Path; }
565 error_code status(file_status &result) const;
566 error_code symlink_status(file_status &result) const;
568 bool operator==(const directory_entry& rhs) const { return Path == rhs.Path; }
569 bool operator!=(const directory_entry& rhs) const { return !(*this == rhs); }
570 bool operator< (const directory_entry& rhs) const;
571 bool operator<=(const directory_entry& rhs) const;
572 bool operator> (const directory_entry& rhs) const;
573 bool operator>=(const directory_entry& rhs) const;
576 /// directory_iterator - Iterates through the entries in path. There is no
577 /// operator++ because we need an error_code. If it's really needed we can make
578 /// it call report_fatal_error on error.
579 class directory_iterator {
580 intptr_t IterationHandle;
581 directory_entry CurrentEntry;
583 // Platform implementations implement these functions to handle iteration.
584 friend error_code directory_iterator_construct(directory_iterator& it,
585 const StringRef &path);
586 friend error_code directory_iterator_increment(directory_iterator& it);
587 friend error_code directory_iterator_destruct(directory_iterator& it);
590 explicit directory_iterator(const Twine &path, error_code &ec)
591 : IterationHandle(0) {
592 SmallString<128> path_storage;
593 ec = directory_iterator_construct(*this, path.toStringRef(path_storage));
596 /// Construct end iterator.
597 directory_iterator() : IterationHandle(0) {}
599 ~directory_iterator() {
600 directory_iterator_destruct(*this);
603 // No operator++ because we need error_code.
604 directory_iterator &increment(error_code &ec) {
605 ec = directory_iterator_increment(*this);
609 const directory_entry &operator*() const { return CurrentEntry; }
610 const directory_entry *operator->() const { return &CurrentEntry; };
612 bool operator!=(const directory_iterator &RHS) const {
613 return CurrentEntry != RHS.CurrentEntry;
615 // Other members as required by
616 // C++ Std, 24.1.1 Input iterators [input.iterators]
619 /// recursive_directory_iterator - Same as directory_iterator except for it
620 /// recurses down into child directories.
621 class recursive_directory_iterator {
623 bool HasNoPushRequest;
624 // implementation directory iterator status
627 explicit recursive_directory_iterator(const Twine &path, error_code &ec);
628 // No operator++ because we need error_code.
629 directory_iterator &increment(error_code &ec);
631 const directory_entry &operator*() const;
632 const directory_entry *operator->() const;
635 /// Gets the current level. path is at level 0.
637 /// Returns true if no_push has been called for this directory_entry.
638 bool no_push_request() const;
641 /// Goes up one level if Level > 0.
643 /// Does not go down into the current directory_entry.
646 // Other members as required by
647 // C++ Std, 24.1.1 Input iterators [input.iterators]
652 } // end namespace fs
653 } // end namespace sys
654 } // end namespace llvm