libwreport  3.6
sys.h
1 #ifndef WREPORT_SYS_H
2 #define WREPORT_SYS_H
3 
11 #include <string>
12 //#include <iosfwd>
13 #include <memory>
14 #include <iterator>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <dirent.h>
19 
20 namespace wreport {
21 namespace sys {
22 
28 std::unique_ptr<struct stat> stat(const std::string& pathname);
29 
34 void stat(const std::string& pathname, struct stat& st);
35 
41 bool isdir(const std::string& pathname);
42 
44 bool isblk(const std::string& pathname);
45 
47 bool ischr(const std::string& pathname);
48 
50 bool isfifo(const std::string& pathname);
51 
53 bool islnk(const std::string& pathname);
54 
56 bool isreg(const std::string& pathname);
57 
59 bool issock(const std::string& pathname);
60 
62 time_t timestamp(const std::string& file);
63 
65 time_t timestamp(const std::string& file, time_t def);
66 
68 size_t size(const std::string& file);
69 
71 size_t size(const std::string& file, size_t def);
72 
74 ino_t inode(const std::string& file);
75 
77 ino_t inode(const std::string& file, ino_t def);
78 
80 bool access(const std::string& s, int m);
81 
83 bool exists(const std::string& s);
84 
86 std::string getcwd();
87 
89 std::string abspath(const std::string& pathname);
90 
96 class MMap
97 {
98  void* addr;
99  size_t length;
100 
101 public:
102  MMap(const MMap&) = delete;
103  MMap(MMap&&);
104  MMap(void* addr, size_t length);
105  ~MMap();
106 
107  MMap& operator=(const MMap&) = delete;
108  MMap& operator=(MMap&&);
109 
110  size_t size() const { return length; }
111 
112  void munmap();
113 
114  template<typename T>
115  operator const T*() const { return reinterpret_cast<const T*>(addr); }
116 
117  template<typename T>
118  operator T*() const { return reinterpret_cast<T*>(addr); };
119 };
120 
133 {
134 protected:
135  int fd = -1;
136 
137 public:
138  FileDescriptor();
140  FileDescriptor(int fd);
141  virtual ~FileDescriptor();
142 
143  // We can copy at the FileDescriptor level because the destructor does not
144  // close fd
145  FileDescriptor(const FileDescriptor& o) = default;
146  FileDescriptor& operator=(const FileDescriptor& o) = default;
147 
155  [[noreturn]] virtual void throw_error(const char* desc);
156 
164  [[noreturn]] virtual void throw_runtime_error(const char* desc);
165 
166  void close();
167 
168  void fstat(struct stat& st);
169  void fchmod(mode_t mode);
170 
171  int dup();
172 
173  size_t read(void* buf, size_t count);
174 
179  void read_all_or_throw(void* buf, size_t count);
180 
181  size_t write(const void* buf, size_t count);
182 
183  template<typename Container>
184  size_t write(const Container& c)
185  {
186  return write(c.data(), c.size() * sizeof(Container::value_type));
187  }
188 
190  void write_all_or_retry(const void* buf, size_t count);
191 
192  template<typename Container>
193  void write_all_or_retry(const Container& c)
194  {
195  write_all_or_retry(c.data(), c.size() * sizeof(typename Container::value_type));
196  }
197 
202  void write_all_or_throw(const void* buf, size_t count);
203 
204  template<typename Container>
205  void write_all_or_throw(const Container& c)
206  {
207  write_all_or_throw(c.data(), c.size() * sizeof(typename Container::value_type));
208  }
209 
210  off_t lseek(off_t offset, int whence=SEEK_SET);
211 
212  size_t pread(void* buf, size_t count, off_t offset);
213  size_t pwrite(const void* buf, size_t count, off_t offset);
214 
215  template<typename Container>
216  size_t pwrite(const Container& c, off_t offset)
217  {
218  return pwrite(c.data(), c.size() * sizeof(typename Container::value_type), offset);
219  }
220 
221  void ftruncate(off_t length);
222 
223  MMap mmap(size_t length, int prot, int flags, off_t offset=0);
224 
225  operator int() const { return fd; }
226 };
227 
228 
233 {
234 protected:
235  std::string pathname;
236 
237 public:
238  NamedFileDescriptor(int fd, const std::string& pathname);
241 
242  // We can copy at the NamedFileDescriptor level because the destructor does not
243  // close fd
244  NamedFileDescriptor(const NamedFileDescriptor& o) = default;
245  NamedFileDescriptor& operator=(const NamedFileDescriptor& o) = default;
246 
247  [[noreturn]] virtual void throw_error(const char* desc);
248  [[noreturn]] virtual void throw_runtime_error(const char* desc);
249 
251  const std::string& name() const { return pathname; }
252 };
253 
257 struct Path : public NamedFileDescriptor
258 {
262  struct iterator : public std::iterator<std::input_iterator_tag, struct dirent>
263  {
264  Path* path = nullptr;
265  DIR* dir = nullptr;
266  struct dirent* cur_entry = nullptr;
267 
268  // End iterator
269  iterator();
270  // Start iteration on dir
271  iterator(Path& dir);
272  iterator(iterator&) = delete;
273  iterator(iterator&& o)
274  : dir(o.dir), cur_entry(o.cur_entry)
275  {
276  o.dir = nullptr;
277  o.cur_entry = nullptr;
278  }
279  ~iterator();
280  iterator& operator=(iterator&) = delete;
281  iterator& operator=(iterator&&) = delete;
282 
283  bool operator==(const iterator& i) const;
284  bool operator!=(const iterator& i) const;
285  struct dirent& operator*() const { return *cur_entry; }
286  struct dirent* operator->() const { return cur_entry; }
287  void operator++();
288 
290  bool isdir() const;
291 
293  bool isblk() const;
294 
296  bool ischr() const;
297 
299  bool isfifo() const;
300 
302  bool islnk() const;
303 
305  bool isreg() const;
306 
308  bool issock() const;
309  };
310 
311  using NamedFileDescriptor::NamedFileDescriptor;
312 
316  Path(const char* pathname, int flags=0);
320  Path(const std::string& pathname, int flags=0);
324  Path(Path& parent, const char* pathname, int flags=0);
325  Path(const Path&) = delete;
326  Path(Path&&) = default;
327  Path& operator=(const Path&) = delete;
328  Path& operator=(Path&&) = default;
329 
337  ~Path();
338 
339  DIR* fdopendir();
340 
342  iterator begin();
343 
345  iterator end();
346 
347  int openat(const char* pathname, int flags, mode_t mode=0777);
348 
349  void fstatat(const char* pathname, struct stat& st);
350 
352  void lstatat(const char* pathname, struct stat& st);
353 
354  void unlinkat(const char* pathname);
355 
357  void rmdirat(const char* pathname);
358 
364  void rmtree();
365 };
366 
367 
371 class File : public NamedFileDescriptor
372 {
373 public:
374  using NamedFileDescriptor::NamedFileDescriptor;
375 
376  File(File&&) = default;
377  File(const File&) = delete;
378 
382  File(const std::string& pathname);
383 
385  File(const std::string& pathname, int flags, mode_t mode=0777);
386 
394  ~File();
395 
396  File& operator=(const File&) = delete;
397  File& operator=(File&&) = default;
398 
400  void open(int flags, mode_t mode=0777);
401 
406  bool open_ifexists(int flags, mode_t mode=0777);
407 
408  static File mkstemp(const std::string& prefix);
409 };
410 
412 std::string read_file(const std::string &file);
413 
420 void write_file(const std::string& file, const std::string& data, mode_t mode=0777);
421 
428 void write_file(const std::string& file, const void* data, size_t size, mode_t mode=0777);
429 
439 void write_file_atomically(const std::string& file, const std::string& data, mode_t mode=0777);
440 
450 void write_file_atomically(const std::string& file, const void* data, size_t size, mode_t mode=0777);
451 
452 #if 0
453 // Create a temporary directory based on a template.
454 std::string mkdtemp(std::string templ);
455 
458 void mkFilePath(const std::string& file);
459 #endif
460 
466 bool unlink_ifexists(const std::string& file);
467 
473 bool rename_ifexists(const std::string& src, const std::string& dst);
474 
483 bool mkdir_ifmissing(const char* pathname, mode_t mode=0777);
484 
485 bool mkdir_ifmissing(const std::string& pathname, mode_t mode=0777);
486 
493 bool makedirs(const std::string& pathname, mode_t=0777);
494 
502 std::string which(const std::string& name);
503 
505 void unlink(const std::string& pathname);
506 
508 void rmdir(const std::string& pathname);
509 
511 void rmtree(const std::string& pathname);
512 
513 #if 0
514 class Directory
516 {
517 protected:
519  std::string m_path;
520 
521 public:
522  class const_iterator
523  {
525  const Directory* dir;
527  void* dirp;
529  struct dirent* direntbuf;
530 
531  public:
532  // Create an end iterator
533  const_iterator();
534  // Create a begin iterator
535  const_iterator(const Directory& dir);
536  // Cleanup properly
537  ~const_iterator();
538 
540  const_iterator(const const_iterator& i);
541  const_iterator& operator=(const const_iterator& i);
542 
544  const_iterator& operator++();
545 
547  std::string operator*() const;
548 
549  bool operator==(const const_iterator& iter) const;
550  bool operator!=(const const_iterator& iter) const;
551  };
552 
553  Directory(const std::string& path);
554  ~Directory();
555 
557  const std::string& path() const { return m_path; }
558 
560  bool exists() const;
561 
563  const_iterator begin() const;
564 
566  const_iterator end() const;
567 };
568 
569 #endif
570 }
571 }
572 
573 #endif
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:96
Common operations on file descriptors.
Definition: sys.h:132
open(2) file descriptors
Definition: sys.h:371
File descriptor with a name.
Definition: sys.h:232
Iterator for directory entries.
Definition: sys.h:262
const std::string & name() const
Return the file pathname.
Definition: sys.h:251
String functions.
Definition: benchmark.h:13
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:257