WPILibC++  2018.4.1-20180924031742-1200-g1aa8446
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 wpi::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 WPIUTIL_WPI_FILESYSTEM_H
28 #define WPIUTIL_WPI_FILESYSTEM_H
29 
30 #include "wpi/SmallString.h"
31 #include "wpi/StringRef.h"
32 #include "wpi/Twine.h"
33 #include "wpi/ErrorOr.h"
34 #include <cassert>
35 #include <cstdint>
36 #include <ctime>
37 #include <memory>
38 #include <stack>
39 #include <string>
40 #include <system_error>
41 #include <tuple>
42 #include <vector>
43 
44 #include <sys/stat.h>
45 
46 namespace wpi {
47 namespace sys {
48 namespace fs {
49 
51 enum class file_type {
52  status_error,
53  file_not_found,
54  regular_file,
55  directory_file,
56  symlink_file,
57  block_file,
58  character_file,
59  fifo_file,
60  socket_file,
61  type_unknown
62 };
63 
64 enum perms {
65  no_perms = 0,
66  owner_read = 0400,
67  owner_write = 0200,
68  owner_exe = 0100,
69  owner_all = owner_read | owner_write | owner_exe,
70  group_read = 040,
71  group_write = 020,
72  group_exe = 010,
73  group_all = group_read | group_write | group_exe,
74  others_read = 04,
75  others_write = 02,
76  others_exe = 01,
77  others_all = others_read | others_write | others_exe,
78  all_read = owner_read | group_read | others_read,
79  all_write = owner_write | group_write | others_write,
80  all_exe = owner_exe | group_exe | others_exe,
81  all_all = owner_all | group_all | others_all,
82  set_uid_on_exe = 04000,
83  set_gid_on_exe = 02000,
84  sticky_bit = 01000,
85  all_perms = all_all | set_uid_on_exe | set_gid_on_exe | sticky_bit,
86  perms_not_known = 0xFFFF
87 };
88 
89 // Helper functions so that you can use & and | to manipulate perms bits:
90 inline perms operator|(perms l, perms r) {
91  return static_cast<perms>(static_cast<unsigned short>(l) |
92  static_cast<unsigned short>(r));
93 }
94 inline perms operator&(perms l, perms r) {
95  return static_cast<perms>(static_cast<unsigned short>(l) &
96  static_cast<unsigned short>(r));
97 }
98 inline perms &operator|=(perms &l, perms r) {
99  l = l | r;
100  return l;
101 }
102 inline perms &operator&=(perms &l, perms r) {
103  l = l & r;
104  return l;
105 }
106 inline perms operator~(perms x) {
107  // Avoid UB by explicitly truncating the (unsigned) ~ result.
108  return static_cast<perms>(
109  static_cast<unsigned short>(~static_cast<unsigned short>(x)));
110 }
111 
112 class UniqueID {
113  uint64_t Device;
114  uint64_t File;
115 
116 public:
117  UniqueID() = default;
118  UniqueID(uint64_t Device, uint64_t File) : Device(Device), File(File) {}
119 
120  bool operator==(const UniqueID &Other) const {
121  return Device == Other.Device && File == Other.File;
122  }
123  bool operator!=(const UniqueID &Other) const { return !(*this == Other); }
124  bool operator<(const UniqueID &Other) const {
125  return std::tie(Device, File) < std::tie(Other.Device, Other.File);
126  }
127 
128  uint64_t getDevice() const { return Device; }
129  uint64_t getFile() const { return File; }
130 };
131 
136 protected:
137  #ifndef _WIN32
138  time_t fs_st_atime = 0;
139  time_t fs_st_mtime = 0;
140  uid_t fs_st_uid = 0;
141  gid_t fs_st_gid = 0;
142  off_t fs_st_size = 0;
143  #else
144  uint32_t LastAccessedTimeHigh = 0;
145  uint32_t LastAccessedTimeLow = 0;
146  uint32_t LastWriteTimeHigh = 0;
147  uint32_t LastWriteTimeLow = 0;
148  uint32_t FileSizeHigh = 0;
149  uint32_t FileSizeLow = 0;
150  #endif
151  file_type Type = file_type::status_error;
152  perms Perms = perms_not_known;
153 
154 public:
155  basic_file_status() = default;
156 
157  explicit basic_file_status(file_type Type) : Type(Type) {}
158 
159  #ifndef _WIN32
160  basic_file_status(file_type Type, perms Perms, time_t ATime, time_t MTime,
161  uid_t UID, gid_t GID, off_t Size)
162  : fs_st_atime(ATime), fs_st_mtime(MTime), fs_st_uid(UID), fs_st_gid(GID),
163  fs_st_size(Size), Type(Type), Perms(Perms) {}
164  #else
165  basic_file_status(file_type Type, perms Perms, uint32_t LastAccessTimeHigh,
166  uint32_t LastAccessTimeLow, uint32_t LastWriteTimeHigh,
167  uint32_t LastWriteTimeLow, uint32_t FileSizeHigh,
168  uint32_t FileSizeLow)
169  : LastAccessedTimeHigh(LastAccessTimeHigh),
170  LastAccessedTimeLow(LastAccessTimeLow),
171  LastWriteTimeHigh(LastWriteTimeHigh),
172  LastWriteTimeLow(LastWriteTimeLow), FileSizeHigh(FileSizeHigh),
173  FileSizeLow(FileSizeLow), Type(Type), Perms(Perms) {}
174  #endif
175 
176  // getters
177  file_type type() const { return Type; }
178  perms permissions() const { return Perms; }
179 
180  #ifndef _WIN32
181  uint32_t getUser() const { return fs_st_uid; }
182  uint32_t getGroup() const { return fs_st_gid; }
183  uint64_t getSize() const { return fs_st_size; }
184  #else
185  uint32_t getUser() const {
186  return 9999; // Not applicable to Windows, so...
187  }
188 
189  uint32_t getGroup() const {
190  return 9999; // Not applicable to Windows, so...
191  }
192 
193  uint64_t getSize() const {
194  return (uint64_t(FileSizeHigh) << 32) + FileSizeLow;
195  }
196  #endif
197 
198  // setters
199  void type(file_type v) { Type = v; }
200  void permissions(perms p) { Perms = p; }
201 };
202 
205  friend bool equivalent(file_status A, file_status B);
206 
207  #ifndef _WIN32
208  dev_t fs_st_dev = 0;
209  nlink_t fs_st_nlinks = 0;
210  ino_t fs_st_ino = 0;
211  #else
212  uint32_t NumLinks = 0;
213  uint32_t VolumeSerialNumber = 0;
214  uint32_t FileIndexHigh = 0;
215  uint32_t FileIndexLow = 0;
216  #endif
217 
218 public:
219  file_status() = default;
220 
221  explicit file_status(file_type Type) : basic_file_status(Type) {}
222 
223  #ifndef _WIN32
224  file_status(file_type Type, perms Perms, dev_t Dev, nlink_t Links, ino_t Ino,
225  time_t ATime, time_t MTime, uid_t UID, gid_t GID, off_t Size)
226  : basic_file_status(Type, Perms, ATime, MTime, UID, GID, Size),
227  fs_st_dev(Dev), fs_st_nlinks(Links), fs_st_ino(Ino) {}
228  #else
229  file_status(file_type Type, perms Perms, uint32_t LinkCount,
230  uint32_t LastAccessTimeHigh, uint32_t LastAccessTimeLow,
231  uint32_t LastWriteTimeHigh, uint32_t LastWriteTimeLow,
232  uint32_t VolumeSerialNumber, uint32_t FileSizeHigh,
233  uint32_t FileSizeLow, uint32_t FileIndexHigh,
234  uint32_t FileIndexLow)
235  : basic_file_status(Type, Perms, LastAccessTimeHigh, LastAccessTimeLow,
236  LastWriteTimeHigh, LastWriteTimeLow, FileSizeHigh,
237  FileSizeLow),
238  NumLinks(LinkCount), VolumeSerialNumber(VolumeSerialNumber),
239  FileIndexHigh(FileIndexHigh), FileIndexLow(FileIndexLow) {}
240  #endif
241 
242  UniqueID getUniqueID() const;
243  uint32_t getLinkCount() const;
244 };
245 
249 
261 std::error_code make_absolute(const Twine &current_directory,
262  SmallVectorImpl<char> &path);
263 
275 std::error_code make_absolute(SmallVectorImpl<char> &path);
276 
282 std::error_code current_path(SmallVectorImpl<char> &result);
283 
287 
293 bool exists(const basic_file_status &status);
294 
295 enum class AccessMode { Exist, Write, Execute };
296 
302 std::error_code access(const Twine &Path, AccessMode Mode);
303 
308 inline bool exists(const Twine &Path) {
309  return !access(Path, AccessMode::Exist);
310 }
311 
316 inline bool can_write(const Twine &Path) {
317  return !access(Path, AccessMode::Write);
318 }
319 
329 bool equivalent(file_status A, file_status B);
330 
341 std::error_code equivalent(const Twine &A, const Twine &B, bool &result);
342 
345 inline bool equivalent(const Twine &A, const Twine &B) {
346  bool result;
347  return !equivalent(A, B, result) && result;
348 }
349 
354 bool is_directory(const basic_file_status &status);
355 
363 std::error_code is_directory(const Twine &path, bool &result);
364 
367 inline bool is_directory(const Twine &Path) {
368  bool Result;
369  return !is_directory(Path, Result) && Result;
370 }
371 
376 bool is_regular_file(const basic_file_status &status);
377 
385 std::error_code is_regular_file(const Twine &path, bool &result);
386 
389 inline bool is_regular_file(const Twine &Path) {
390  bool Result;
391  if (is_regular_file(Path, Result))
392  return false;
393  return Result;
394 }
395 
400 bool is_symlink_file(const basic_file_status &status);
401 
409 std::error_code is_symlink_file(const Twine &path, bool &result);
410 
413 inline bool is_symlink_file(const Twine &Path) {
414  bool Result;
415  if (is_symlink_file(Path, Result))
416  return false;
417  return Result;
418 }
419 
425 bool is_other(const basic_file_status &status);
426 
435 std::error_code is_other(const Twine &path, bool &result);
436 
445 std::error_code status(const Twine &path, file_status &result,
446  bool follow = true);
447 
449 std::error_code status(int FD, file_status &Result);
450 
455 bool status_known(const basic_file_status &s);
456 
463 std::error_code status_known(const Twine &path, bool &result);
464 
465 enum OpenFlags : unsigned {
466  F_None = 0,
467 
470  F_Excl = 1,
471 
475  F_Append = 2,
476 
480  F_NoTrunc = 4,
481 
484  F_Text = 8,
485 
487  F_RW = 16,
488 
490  F_Delete = 32
491 };
492 
493 inline OpenFlags operator|(OpenFlags A, OpenFlags B) {
494  return OpenFlags(unsigned(A) | unsigned(B));
495 }
496 
497 inline OpenFlags &operator|=(OpenFlags &A, OpenFlags B) {
498  A = A | B;
499  return A;
500 }
501 
517 std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
518  OpenFlags Flags, unsigned Mode = 0666);
519 
534 std::error_code openFileForRead(const Twine &Name, int &ResultFD,
535  SmallVectorImpl<char> *RealPath = nullptr);
536 
537 std::error_code getUniqueID(const Twine Path, UniqueID &Result);
538 
542 
547  std::string Path;
548  bool FollowSymlinks;
549  basic_file_status Status;
550 
551 public:
552  explicit directory_entry(const Twine &path, bool follow_symlinks = true,
554  : Path(path.str()), FollowSymlinks(follow_symlinks), Status(st) {}
555 
556  directory_entry() = default;
557 
558  void assign(const Twine &path, basic_file_status st = basic_file_status()) {
559  Path = path.str();
560  Status = st;
561  }
562 
563  void replace_filename(const Twine &filename,
565 
566  const std::string &path() const { return Path; }
567  ErrorOr<basic_file_status> status() const;
568 
569  bool operator==(const directory_entry& rhs) const { return Path == rhs.Path; }
570  bool operator!=(const directory_entry& rhs) const { return !(*this == rhs); }
571  bool operator< (const directory_entry& rhs) const;
572  bool operator<=(const directory_entry& rhs) const;
573  bool operator> (const directory_entry& rhs) const;
574  bool operator>=(const directory_entry& rhs) const;
575 };
576 
577 namespace detail {
578 
579  struct DirIterState;
580 
581  std::error_code directory_iterator_construct(DirIterState &, StringRef, bool);
582  std::error_code directory_iterator_increment(DirIterState &);
583  std::error_code directory_iterator_destruct(DirIterState &);
584 
586  struct DirIterState {
587  ~DirIterState() {
588  directory_iterator_destruct(*this);
589  }
590 
591  intptr_t IterationHandle = 0;
592  directory_entry CurrentEntry;
593  };
594 
595 } // end namespace detail
596 
601  std::shared_ptr<detail::DirIterState> State;
602  bool FollowSymlinks = true;
603 
604 public:
605  explicit directory_iterator(const Twine &path, std::error_code &ec,
606  bool follow_symlinks = true)
607  : FollowSymlinks(follow_symlinks) {
608  State = std::make_shared<detail::DirIterState>();
609  SmallString<128> path_storage;
610  ec = detail::directory_iterator_construct(
611  *State, path.toStringRef(path_storage), FollowSymlinks);
612  update_error_code_for_current_entry(ec);
613  }
614 
615  explicit directory_iterator(const directory_entry &de, std::error_code &ec,
616  bool follow_symlinks = true)
617  : FollowSymlinks(follow_symlinks) {
618  State = std::make_shared<detail::DirIterState>();
619  ec = detail::directory_iterator_construct(
620  *State, de.path(), FollowSymlinks);
621  update_error_code_for_current_entry(ec);
622  }
623 
625  directory_iterator() = default;
626 
627  // No operator++ because we need error_code.
628  directory_iterator &increment(std::error_code &ec) {
629  ec = directory_iterator_increment(*State);
630  update_error_code_for_current_entry(ec);
631  return *this;
632  }
633 
634  const directory_entry &operator*() const { return State->CurrentEntry; }
635  const directory_entry *operator->() const { return &State->CurrentEntry; }
636 
637  bool operator==(const directory_iterator &RHS) const {
638  if (State == RHS.State)
639  return true;
640  if (!RHS.State)
641  return State->CurrentEntry == directory_entry();
642  if (!State)
643  return RHS.State->CurrentEntry == directory_entry();
644  return State->CurrentEntry == RHS.State->CurrentEntry;
645  }
646 
647  bool operator!=(const directory_iterator &RHS) const {
648  return !(*this == RHS);
649  }
650  // Other members as required by
651  // C++ Std, 24.1.1 Input iterators [input.iterators]
652 
653 private:
654  // Checks if current entry is valid and populates error code. For example,
655  // current entry may not exist due to broken symbol links.
656  void update_error_code_for_current_entry(std::error_code &ec) {
657  // Bail out if error has already occured earlier to avoid overwriting it.
658  if (ec)
659  return;
660 
661  // Empty directory entry is used to mark the end of an interation, it's not
662  // an error.
663  if (State->CurrentEntry == directory_entry())
664  return;
665 
666  ErrorOr<basic_file_status> status = State->CurrentEntry.status();
667  if (!status)
668  ec = status.getError();
669  }
670 };
671 
672 namespace detail {
673 
676  std::stack<directory_iterator, std::vector<directory_iterator>> Stack;
677  uint16_t Level = 0;
678  bool HasNoPushRequest = false;
679  };
680 
681 } // end namespace detail
682 
686  std::shared_ptr<detail::RecDirIterState> State;
687  bool Follow;
688 
689 public:
690  recursive_directory_iterator() = default;
691  explicit recursive_directory_iterator(const Twine &path, std::error_code &ec,
692  bool follow_symlinks = true)
693  : State(std::make_shared<detail::RecDirIterState>()),
694  Follow(follow_symlinks) {
695  State->Stack.push(directory_iterator(path, ec, Follow));
696  if (State->Stack.top() == directory_iterator())
697  State.reset();
698  }
699 
700  // No operator++ because we need error_code.
701  recursive_directory_iterator &increment(std::error_code &ec) {
702  const directory_iterator end_itr = {};
703 
704  if (State->HasNoPushRequest)
705  State->HasNoPushRequest = false;
706  else {
707  ErrorOr<basic_file_status> status = State->Stack.top()->status();
708  if (status && is_directory(*status)) {
709  State->Stack.push(directory_iterator(*State->Stack.top(), ec, Follow));
710  if (State->Stack.top() != end_itr) {
711  ++State->Level;
712  return *this;
713  }
714  State->Stack.pop();
715  }
716  }
717 
718  while (!State->Stack.empty()
719  && State->Stack.top().increment(ec) == end_itr) {
720  State->Stack.pop();
721  --State->Level;
722  }
723 
724  // Check if we are done. If so, create an end iterator.
725  if (State->Stack.empty())
726  State.reset();
727 
728  return *this;
729  }
730 
731  const directory_entry &operator*() const { return *State->Stack.top(); }
732  const directory_entry *operator->() const { return &*State->Stack.top(); }
733 
734  // observers
736  int level() const { return State->Level; }
737 
739  bool no_push_request() const { return State->HasNoPushRequest; }
740 
741  // modifiers
743  void pop() {
744  assert(State && "Cannot pop an end iterator!");
745  assert(State->Level > 0 && "Cannot pop an iterator with level < 1");
746 
747  const directory_iterator end_itr = {};
748  std::error_code ec;
749  do {
750  if (ec) {
751  //report_fatal_error("Error incrementing directory iterator.");
752  while (!State->Stack.empty()) State->Stack.pop();
753  break;
754  }
755  State->Stack.pop();
756  --State->Level;
757  } while (!State->Stack.empty()
758  && State->Stack.top().increment(ec) == end_itr);
759 
760  // Check if we are done. If so, create an end iterator.
761  if (State->Stack.empty())
762  State.reset();
763  }
764 
766  void no_push() { State->HasNoPushRequest = true; }
767 
768  bool operator==(const recursive_directory_iterator &RHS) const {
769  return State == RHS.State;
770  }
771 
772  bool operator!=(const recursive_directory_iterator &RHS) const {
773  return !(*this == RHS);
774  }
775  // Other members as required by
776  // C++ Std, 24.1.1 Input iterators [input.iterators]
777 };
778 
780 
781 } // end namespace fs
782 } // end namespace sys
783 } // end namespace wpi
784 
785 #endif // LLVM_SUPPORT_FILESYSTEM_H
recursive_directory_iterator - Same as directory_iterator except for it recurses down into child dire...
Definition: FileSystem.h:685
int level() const
Gets the current level. Starting path is at level 0.
Definition: FileSystem.h:736
directory_entry - A single entry in a directory.
Definition: FileSystem.h:546
friend bool equivalent(file_status A, file_status B)
Do file_status's represent the same thing?
Definition: FileSystem.h:112
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:204
Keeps state for the recursive_directory_iterator.
Definition: FileSystem.h:675
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
void pop()
Goes up one level if Level > 0.
Definition: FileSystem.h:743
directory_iterator()=default
Construct end iterator.
directory_iterator - Iterates through the entries in path.
Definition: FileSystem.h:600
bool no_push_request() const
Returns true if no_push has been called for this directory_entry.
Definition: FileSystem.h:739
std::string str() const
Return the twine contents as a std::string.
void no_push()
Does not go down into the current directory_entry.
Definition: FileSystem.h:766
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Keeps state for the directory_iterator.
Definition: FileSystem.h:586
Represents either an error or a value T.
Definition: ErrorOr.h:69
Provides ErrorOr smart pointer.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:444
Represents the result of a call to directory_iterator::status().
Definition: FileSystem.h:135