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 Get the current path.
153 /// @param result Holds the current path on return.
154 /// @results errc::success if the current path has been stored in result,
155 /// otherwise a platform specific error_code.
156 error_code current_path(SmallVectorImpl<char> &result);
158 /// @brief Remove path. Equivalent to POSIX remove().
160 /// @param path Input path.
161 /// @param existed Set to true if \a path existed, false if it did not.
162 /// undefined otherwise.
163 /// @results errc::success if path has been removed and existed has been
164 /// successfully set, otherwise a platform specific error_code.
165 error_code remove(const Twine &path, bool &existed);
167 /// @brief Recursively remove all files below \a path, then \a path. Files are
168 /// removed as if by POSIX remove().
170 /// @param path Input path.
171 /// @param num_removed Number of files removed.
172 /// @results errc::success if path has been removed and num_removed has been
173 /// successfully set, otherwise a platform specific error_code.
174 error_code remove_all(const Twine &path, uint32_t &num_removed);
176 /// @brief Rename \a from to \a to. Files are renamed as if by POSIX rename().
178 /// @param from The path to rename from.
179 /// @param to The path to rename to. This is created.
180 error_code rename(const Twine &from, const Twine &to);
182 /// @brief Resize path to size. File is resized as if by POSIX truncate().
184 /// @param path Input path.
185 /// @param size Size to resize to.
186 /// @returns errc::success if \a path has been resized to \a size, otherwise a
187 /// platform specific error_code.
188 error_code resize_file(const Twine &path, uint64_t size);
190 /// @brief Make file readable.
192 /// @param path Input path.
193 /// @param value If true, make readable, else, make unreadable.
194 /// @results errc::success if readability has been successfully set, otherwise a
195 /// platform specific error_code.
196 error_code set_read(const Twine &path, bool value);
198 /// @brief Make file writeable.
200 /// @param path Input path.
201 /// @param value If true, make writeable, else, make unwriteable.
202 /// @results errc::success if writeability has been successfully set, otherwise
203 /// a platform specific error_code.
204 error_code set_write(const Twine &path, bool value);
206 /// @brief Make file executable.
208 /// @param path Input path.
209 /// @param value If true, make executable, else, make unexecutable.
210 /// @results errc::success if executability has been successfully set, otherwise
211 /// a platform specific error_code.
212 error_code set_execute(const Twine &path, bool value);
215 /// @name Physical Observers
218 /// @brief Does file exist?
220 /// @param status A file_status previously returned from stat.
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(file_status status, bool &result);
227 /// @brief Does file exist?
229 /// @param path Input path.
230 /// @param result Set to true if the file represented by status exists, false if
231 /// it does not. Undefined otherwise.
232 /// @results errc::success if result has been successfully set, otherwise a
233 /// platform specific error_code.
234 error_code exists(const Twine &path, bool &result);
236 /// @brief Do paths represent the same thing?
238 /// @param A Input path A.
239 /// @param B Input path B.
240 /// @param result Set to true if stat(A) and stat(B) have the same device and
241 /// inode (or equivalent).
242 /// @results errc::success if result has been successfully set, otherwise a
243 /// platform specific error_code.
244 error_code equivalent(const Twine &A, const Twine &B, bool &result);
246 /// @brief Get file size.
248 /// @param path Input path.
249 /// @param result Set to the size of the file in \a path.
250 /// @returns errc::success if result has been successfully set, otherwise a
251 /// platform specific error_code.
252 error_code file_size(const Twine &path, uint64_t &result);
254 /// @brief Does status represent a directory?
256 /// @param status A file_status previously returned from stat.
257 /// @param result Set to true if the file represented by status is a directory,
258 /// false if it is not. Undefined otherwise.
259 /// @results errc::success if result has been successfully set, otherwise a
260 /// platform specific error_code.
261 error_code is_directory(file_status status, bool &result);
263 /// @brief Is path a directory?
265 /// @param path Input path.
266 /// @param result Set to true if \a path is a directory, 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_directory(const Twine &path, bool &result);
272 /// @brief Is path an empty file?
274 /// @param path Input path.
275 /// @param result Set to true if \a path is a an empty file, false if it is not.
276 /// Undefined otherwise.
277 /// @results errc::success if result has been successfully set, otherwise a
278 /// platform specific error_code.
279 error_code is_empty(const Twine &path, bool &result);
281 /// @brief Does status represent a regular file?
283 /// @param status A file_status previously returned from stat.
284 /// @param result Set to true if the file represented by status is a regular
285 /// file, false if it is not. 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(file_status status, bool &result);
290 /// @brief Is path a regular file?
292 /// @param path Input path.
293 /// @param result Set to true if \a path is a regular file, false if it is not.
294 /// Undefined otherwise.
295 /// @results errc::success if result has been successfully set, otherwise a
296 /// platform specific error_code.
297 error_code is_regular_file(const Twine &path, bool &result);
299 /// @brief Does status represent something that exists but is not a directory,
300 /// regular file, or symlink?
302 /// @param status A file_status previously returned from stat.
303 /// @param result Set to true if the file represented by status exists, but is
304 /// not a directory, regular file, or a symlink, false if it does
305 /// not. Undefined otherwise.
306 /// @results errc::success if result has been successfully set, otherwise a
307 /// platform specific error_code.
308 error_code is_other(file_status status, bool &result);
310 /// @brief Is path something that exists but is not a directory,
311 /// regular file, or symlink?
313 /// @param path Input path.
314 /// @param result Set to true if \a path exists, but is not a directory, regular
315 /// file, or a symlink, false if it does not. Undefined otherwise.
316 /// @results errc::success if result has been successfully set, otherwise a
317 /// platform specific error_code.
318 error_code is_other(const Twine &path, bool &result);
320 /// @brief Does status represent a symlink?
322 /// @param status A file_status previously returned from stat.
323 /// @param result Set to true if the file represented by status is a symlink,
324 /// false if it is not. Undefined otherwise.
325 /// @results errc::success if result has been successfully set, otherwise a
326 /// platform specific error_code.
327 error_code is_symlink(file_status status, bool &result);
329 /// @brief Is path a symlink?
331 /// @param path Input path.
332 /// @param result Set to true if \a path is a symlink, false if it is not.
333 /// Undefined otherwise.
334 /// @results errc::success if result has been successfully set, otherwise a
335 /// platform specific error_code.
336 error_code is_symlink(const Twine &path, bool &result);
338 /// @brief Get last write time without changing it.
340 /// @param path Input path.
341 /// @param result Set to the last write time (UNIX time) of \a path if it
343 /// @results errc::success if result has been successfully set, otherwise a
344 /// platform specific error_code.
345 error_code last_write_time(const Twine &path, std::time_t &result);
347 /// @brief Set last write time.
349 /// @param path Input path.
350 /// @param value Time to set (UNIX time) \a path's last write time to.
351 /// @results errc::success if result has been successfully set, otherwise a
352 /// platform specific error_code.
353 error_code set_last_write_time(const Twine &path, std::time_t value);
355 /// @brief Read a symlink's value.
357 /// @param path Input path.
358 /// @param result Set to the value of the symbolic link \a path.
359 /// @results errc::success if result has been successfully set, otherwise a
360 /// platform specific error_code.
361 error_code read_symlink(const Twine &path, SmallVectorImpl<char> &result);
363 /// @brief Get disk space usage information.
365 /// @param path Input path.
366 /// @param result Set to the capacity, free, and available space on the device
368 /// @results errc::success if result has been successfully set, otherwise a
369 /// platform specific error_code.
370 error_code disk_space(const Twine &path, space_info &result);
372 /// @brief Get file status as if by POSIX stat().
374 /// @param path Input path.
375 /// @param result Set to the file status.
376 /// @results errc::success if result has been successfully set, otherwise a
377 /// platform specific error_code.
378 error_code status(const Twine &path, file_status &result);
380 /// @brief Is status available?
382 /// @param path Input path.
383 /// @param result Set to true if status() != status_error.
384 /// @results errc::success if result has been successfully set, otherwise a
385 /// platform specific error_code.
386 error_code status_known(const Twine &path, bool &result);
388 /// @brief Get file status as if by POSIX lstat().
390 /// Does not resolve symlinks.
392 /// @param path Input path.
393 /// @param result Set to the file status.
394 /// @results errc::success if result has been successfully set, otherwise a
395 /// platform specific error_code.
396 error_code symlink_status(const Twine &path, file_status &result);
398 /// @brief Generate a unique path and open it as a file.
400 /// Generates a unique path suitable for a temporary file and then opens it as a
401 /// file. The name is based on \a model with '%' replaced by a random char in
402 /// [0-9a-f]. If \a model is not an absolute path, a suitable temporary
403 /// directory will be prepended.
405 /// This is an atomic operation. Either the file is created and opened, or the
406 /// file system is left untouched.
408 /// clang-%%-%%-%%-%%-%%.s => /tmp/clang-a0-b1-c2-d3-e4.s
410 /// @param model Name to base unique path off of.
411 /// @param result_fs Set to the opened file's file descriptor.
412 /// @param result_path Set to the opened file's absolute path.
413 /// @results errc::success if result_{fd,path} have been successfully set,
414 /// otherwise a platform specific error_code.
415 error_code unique_file(const Twine &model, int &result_fd,
416 SmallVectorImpl<char> &result_path);
418 /// @brief Canonicalize path.
420 /// Sets result to the file system's idea of what path is. The result is always
421 /// absolute and has the same capitalization as the file system.
423 /// @param path Input path.
424 /// @param result Set to the canonicalized version of \a path.
425 /// @results errc::success if result has been successfully set, otherwise a
426 /// platform specific error_code.
427 error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result);
429 /// @brief Are \a path's first bytes \a magic?
431 /// @param path Input path.
432 /// @param magic Byte sequence to compare \a path's first len(magic) bytes to.
433 /// @results errc::success if result has been successfully set, otherwise a
434 /// platform specific error_code.
435 error_code has_magic(const Twine &path, const Twine &magic);
437 /// @brief Get \a path's first \a len bytes.
439 /// @param path Input path.
440 /// @param len Number of magic bytes to get.
441 /// @param result Set to the first \a len bytes in the file pointed to by
443 /// @results errc::success if result has been successfully set, otherwise a
444 /// platform specific error_code.
445 error_code get_magic(const Twine &path, uint32_t len,
446 SmallVectorImpl<char> &result);
448 /// @brief Is file bitcode?
450 /// @param path Input path.
451 /// @param result Set to true if \a path is a bitcode file, false if it is not,
452 /// undefined otherwise.
453 /// @results errc::success if result has been successfully set, otherwise a
454 /// platform specific error_code.
455 error_code is_bitcode(const Twine &path, bool &result);
457 /// @brief Is file a dynamic library?
459 /// @param path Input path.
460 /// @param result Set to true if \a path is a dynamic library, false if it is
461 /// not, undefined otherwise.
462 /// @results errc::success if result has been successfully set, otherwise a
463 /// platform specific error_code.
464 error_code is_dynamic_library(const Twine &path, bool &result);
466 /// @brief Is an object file?
468 /// @param path Input path.
469 /// @param result Set to true if \a path is an object file, false if it is not,
470 /// undefined otherwise.
471 /// @results errc::success if result has been successfully set, otherwise a
472 /// platform specific error_code.
473 error_code is_object_file(const Twine &path, bool &result);
475 /// @brief Can file be read?
477 /// @param path Input path.
478 /// @param result Set to true if \a path is readable, false it it is not,
479 /// undefined otherwise.
480 /// @results errc::success if result has been successfully set, otherwise a
481 /// platform specific error_code.
482 error_code can_read(const Twine &path, bool &result);
484 /// @brief Can file be written?
486 /// @param path Input path.
487 /// @param result Set to true if \a path is writeable, false it it is not,
488 /// undefined otherwise.
489 /// @results errc::success if result has been successfully set, otherwise a
490 /// platform specific error_code.
491 error_code can_write(const Twine &path, bool &result);
493 /// @brief Can file be executed?
495 /// @param path Input path.
496 /// @param result Set to true if \a path is executable, false it it is not,
497 /// undefined otherwise.
498 /// @results errc::success if result has been successfully set, otherwise a
499 /// platform specific error_code.
500 error_code can_execute(const Twine &path, bool &result);
502 /// @brief Get library paths the system linker uses.
504 /// @param result Set to the list of system library paths.
505 /// @results errc::success if result has been successfully set, otherwise a
506 /// platform specific error_code.
507 error_code GetSystemLibraryPaths(SmallVectorImpl<std::string> &result);
509 /// @brief Get bitcode library paths the system linker uses
510 /// + LLVM_LIB_SEARCH_PATH + LLVM_LIBDIR.
512 /// @param result Set to the list of bitcode library paths.
513 /// @results errc::success if result has been successfully set, otherwise a
514 /// platform specific error_code.
515 error_code GetBitcodeLibraryPaths(SmallVectorImpl<std::string> &result);
517 /// @brief Find a library.
519 /// Find the path to a library using its short name. Use the system
520 /// dependent library paths to locate the library.
522 /// c => /usr/lib/libc.so
524 /// @param short_name Library name one would give to the system linker.
525 /// @param result Set to the absolute path \a short_name represents.
526 /// @results errc::success if result has been successfully set, otherwise a
527 /// platform specific error_code.
528 error_code FindLibrary(const Twine &short_name, SmallVectorImpl<char> &result);
530 /// @brief Get absolute path of main executable.
532 /// @param argv0 The program name as it was spelled on the command line.
533 /// @param MainAddr Address of some symbol in the executable (not in a library).
534 /// @param result Set to the absolute path of the current executable.
535 /// @results errc::success if result has been successfully set, otherwise a
536 /// platform specific error_code.
537 error_code GetMainExecutable(const char *argv0, void *MainAddr,
538 SmallVectorImpl<char> &result);
544 /// directory_entry - A single entry in a directory. Caches the status either
545 /// from the result of the iteration syscall, or the first time status or
546 /// symlink_status is called.
547 class directory_entry {
549 mutable file_status Status;
550 mutable file_status SymlinkStatus;
553 explicit directory_entry(const Twine &path, file_status st = file_status(),
554 file_status symlink_st = file_status())
557 , SymlinkStatus(symlink_st) {}
561 void assign(const Twine &path, file_status st = file_status(),
562 file_status symlink_st = file_status()) {
565 SymlinkStatus = symlink_st;
568 void replace_filename(const Twine &filename, file_status st = file_status(),
569 file_status symlink_st = file_status());
571 StringRef path() const { return Path; }
572 error_code status(file_status &result) const;
573 error_code symlink_status(file_status &result) const;
575 bool operator==(const directory_entry& rhs) const { return Path == rhs.Path; }
576 bool operator!=(const directory_entry& rhs) const { return !(*this == rhs); }
577 bool operator< (const directory_entry& rhs) const;
578 bool operator<=(const directory_entry& rhs) const;
579 bool operator> (const directory_entry& rhs) const;
580 bool operator>=(const directory_entry& rhs) const;
583 /// directory_iterator - Iterates through the entries in path. There is no
584 /// operator++ because we need an error_code. If it's really needed we can make
585 /// it call report_fatal_error on error.
586 class directory_iterator {
587 intptr_t IterationHandle;
588 directory_entry CurrentEntry;
590 // Platform implementations implement these functions to handle iteration.
591 friend error_code directory_iterator_construct(directory_iterator& it,
592 const StringRef &path);
593 friend error_code directory_iterator_increment(directory_iterator& it);
594 friend error_code directory_iterator_destruct(directory_iterator& it);
597 explicit directory_iterator(const Twine &path, error_code &ec)
598 : IterationHandle(0) {
599 SmallString<128> path_storage;
600 ec = directory_iterator_construct(*this, path.toStringRef(path_storage));
603 /// Construct end iterator.
604 directory_iterator() : IterationHandle(0) {}
606 ~directory_iterator() {
607 directory_iterator_destruct(*this);
610 // No operator++ because we need error_code.
611 directory_iterator &increment(error_code &ec) {
612 ec = directory_iterator_increment(*this);
616 const directory_entry &operator*() const { return CurrentEntry; }
617 const directory_entry *operator->() const { return &CurrentEntry; }
619 bool operator!=(const directory_iterator &RHS) const {
620 return CurrentEntry != RHS.CurrentEntry;
622 // Other members as required by
623 // C++ Std, 24.1.1 Input iterators [input.iterators]
626 /// recursive_directory_iterator - Same as directory_iterator except for it
627 /// recurses down into child directories.
628 class recursive_directory_iterator {
630 bool HasNoPushRequest;
631 // implementation directory iterator status
634 explicit recursive_directory_iterator(const Twine &path, error_code &ec);
635 // No operator++ because we need error_code.
636 directory_iterator &increment(error_code &ec);
638 const directory_entry &operator*() const;
639 const directory_entry *operator->() const;
642 /// Gets the current level. path is at level 0.
644 /// Returns true if no_push has been called for this directory_entry.
645 bool no_push_request() const;
648 /// Goes up one level if Level > 0.
650 /// Does not go down into the current directory_entry.
653 // Other members as required by
654 // C++ Std, 24.1.1 Input iterators [input.iterators]
659 } // end namespace fs
660 } // end namespace sys
661 } // end namespace llvm