libwreport  3.12
fs.h
1 #ifndef WREPORT_INTERNALS_FS_H
2 #define WREPORT_INTERNALS_FS_H
3 
4 #include <string>
5 #include <iterator>
6 #include <dirent.h>
7 #include <sys/stat.h>
8 
9 namespace wreport {
10 namespace fs {
11 
15 struct Directory
16 {
18  const std::string& pathname;
19 
24  int fd;
25 
29  struct const_iterator : public std::iterator<std::input_iterator_tag, struct dirent>
30  {
31  DIR* dir = 0;
32  struct dirent* cur_entry = 0;
33 
34  // End iterator
36  // Start iteration on dir
37  const_iterator(const Directory& dir);
38  const_iterator(const const_iterator&) = delete;
40  : dir(o.dir), cur_entry(o.cur_entry)
41  {
42  o.dir = nullptr;
43  o.cur_entry = nullptr;
44  }
45  ~const_iterator();
46  const_iterator& operator=(const const_iterator&) = delete;
47  const_iterator& operator=(const_iterator&&) = delete;
48 
49  bool operator==(const const_iterator& i) const;
50  bool operator!=(const const_iterator& i) const;
51  struct dirent& operator*() const { return *cur_entry; }
52  struct dirent* operator->() const { return cur_entry; }
53  void operator++();
54  };
55 
56  Directory(const std::string& pathname);
57  ~Directory();
58 
60  const_iterator begin() const { return const_iterator(*this); }
61 
63  const_iterator end() const { return const_iterator(); }
64 
66  bool exists() const { return fd != -1; }
67 
69  void stat(struct stat& st);
70 };
71 
72 }
73 }
74 
75 #endif
const_iterator end() const
End iterator on all directory entries.
Definition: fs.h:63
Access a directory on the file system.
Definition: fs.h:15
int fd
O_PATH file descriptor pointing at the directory, or -1 if the directory does not exist...
Definition: fs.h:24
bool exists() const
Check if the directory exists.
Definition: fs.h:66
void stat(struct stat &st)
Call stat(2) on the directory.
const_iterator begin() const
Begin iterator on all directory entries.
Definition: fs.h:60
const std::string & pathname
Pathname of the directory.
Definition: fs.h:18
String functions.
Definition: benchmark.h:13
Iterator for directory entries.
Definition: fs.h:29