libwreport  3.12
sys.h
1 #ifndef WREPORT_SYS_H
2 #define WREPORT_SYS_H
3 
11 #include <string>
12 #include <memory>
13 #include <iterator>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/time.h>
17 #include <sys/resource.h>
18 #include <unistd.h>
19 #include <dirent.h>
20 #include <fcntl.h>
21 
22 namespace wreport {
23 namespace sys {
24 
30 std::unique_ptr<struct stat> stat(const std::string& pathname);
31 
36 void stat(const std::string& pathname, struct stat& st);
37 
43 bool isdir(const std::string& pathname);
44 
46 bool isblk(const std::string& pathname);
47 
49 bool ischr(const std::string& pathname);
50 
52 bool isfifo(const std::string& pathname);
53 
55 bool islnk(const std::string& pathname);
56 
58 bool isreg(const std::string& pathname);
59 
61 bool issock(const std::string& pathname);
62 
64 time_t timestamp(const std::string& file);
65 
67 time_t timestamp(const std::string& file, time_t def);
68 
70 size_t size(const std::string& file);
71 
73 size_t size(const std::string& file, size_t def);
74 
76 ino_t inode(const std::string& file);
77 
79 ino_t inode(const std::string& file, ino_t def);
80 
82 bool access(const std::string& s, int m);
83 
85 bool exists(const std::string& s);
86 
88 std::string getcwd();
89 
91 std::string abspath(const std::string& pathname);
92 
98 class MMap
99 {
100  void* addr;
101  size_t length;
102 
103 public:
104  MMap(const MMap&) = delete;
105  MMap(MMap&&);
106  MMap(void* addr, size_t length);
107  ~MMap();
108 
109  MMap& operator=(const MMap&) = delete;
110  MMap& operator=(MMap&&);
111 
112  size_t size() const { return length; }
113 
114  void munmap();
115 
116  template<typename T>
117  operator const T*() const { return reinterpret_cast<const T*>(addr); }
118 
119  template<typename T>
120  operator T*() const { return reinterpret_cast<T*>(addr); };
121 };
122 
135 {
136 protected:
137  int fd = -1;
138 
139 public:
140  FileDescriptor();
142  FileDescriptor(int fd);
143  virtual ~FileDescriptor();
144 
145  // We can copy at the FileDescriptor level because the destructor does not
146  // close fd
147  FileDescriptor(const FileDescriptor& o) = default;
148  FileDescriptor& operator=(const FileDescriptor& o) = default;
149 
157  [[noreturn]] virtual void throw_error(const char* desc);
158 
166  [[noreturn]] virtual void throw_runtime_error(const char* desc);
167 
169  bool is_open() const;
170 
176  void close();
177 
178  void fstat(struct stat& st);
179  void fchmod(mode_t mode);
180 
181  void futimens(const struct ::timespec ts[2]);
182 
183  void fsync();
184  void fdatasync();
185 
186  int dup();
187 
188  size_t read(void* buf, size_t count);
189 
194  void read_all_or_throw(void* buf, size_t count);
195 
196  size_t write(const void* buf, size_t count);
197 
198  template<typename Container>
199  size_t write(const Container& c)
200  {
201  return write(c.data(), c.size() * sizeof(Container::value_type));
202  }
203 
205  void write_all_or_retry(const void* buf, size_t count);
206 
207  template<typename Container>
208  void write_all_or_retry(const Container& c)
209  {
210  write_all_or_retry(c.data(), c.size() * sizeof(typename Container::value_type));
211  }
212 
217  void write_all_or_throw(const void* buf, size_t count);
218 
219  template<typename Container>
220  void write_all_or_throw(const Container& c)
221  {
222  write_all_or_throw(c.data(), c.size() * sizeof(typename Container::value_type));
223  }
224 
225  off_t lseek(off_t offset, int whence=SEEK_SET);
226 
227  size_t pread(void* buf, size_t count, off_t offset);
228  size_t pwrite(const void* buf, size_t count, off_t offset);
229 
230  template<typename Container>
231  size_t pwrite(const Container& c, off_t offset)
232  {
233  return pwrite(c.data(), c.size() * sizeof(typename Container::value_type), offset);
234  }
235 
236  void ftruncate(off_t length);
237 
238  MMap mmap(size_t length, int prot, int flags, off_t offset=0);
239 
246  bool ofd_setlk(struct ::flock&);
247 
257  bool ofd_setlkw(struct ::flock&, bool retry_on_signal=true);
258 
264  bool ofd_getlk(struct ::flock&);
265 
266  operator int() const { return fd; }
267 };
268 
269 
274 {
275 protected:
276  FileDescriptor fd;
277  struct ::timespec ts[2];
278 
279 public:
282 };
283 
284 
285 
290 {
291 protected:
292  std::string pathname;
293 
294 public:
295  NamedFileDescriptor(int fd, const std::string& pathname);
298 
299  // We can copy at the NamedFileDescriptor level because the destructor does not
300  // close fd
301  NamedFileDescriptor(const NamedFileDescriptor& o) = default;
302  NamedFileDescriptor& operator=(const NamedFileDescriptor& o) = default;
303 
304  [[noreturn]] virtual void throw_error(const char* desc);
305  [[noreturn]] virtual void throw_runtime_error(const char* desc);
306 
308  const std::string& name() const { return pathname; }
309 };
310 
311 
316 {
317  using NamedFileDescriptor::NamedFileDescriptor;
318 
321 
330 
331  ManagedNamedFileDescriptor& operator=(const ManagedNamedFileDescriptor&) = delete;
333 };
334 
335 
340 {
344  struct iterator : public std::iterator<std::input_iterator_tag, struct dirent>
345  {
346  Path* path = nullptr;
347  DIR* dir = nullptr;
348  struct dirent* cur_entry = nullptr;
349 
350  // End iterator
351  iterator();
352  // Start iteration on dir
353  iterator(Path& dir);
354  iterator(iterator&) = delete;
355  iterator(iterator&& o)
356  : dir(o.dir), cur_entry(o.cur_entry)
357  {
358  o.dir = nullptr;
359  o.cur_entry = nullptr;
360  }
361  ~iterator();
362  iterator& operator=(iterator&) = delete;
363  iterator& operator=(iterator&&) = delete;
364 
365  bool operator==(const iterator& i) const;
366  bool operator!=(const iterator& i) const;
367  struct dirent& operator*() const { return *cur_entry; }
368  struct dirent* operator->() const { return cur_entry; }
369  void operator++();
370 
372  bool isdir() const;
373 
375  bool isblk() const;
376 
378  bool ischr() const;
379 
381  bool isfifo() const;
382 
384  bool islnk() const;
385 
387  bool isreg() const;
388 
390  bool issock() const;
391 
393  Path open_path(int flags=0) const;
394  };
395 
396  using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
397 
401  Path(const char* pathname, int flags=0);
405  Path(const std::string& pathname, int flags=0);
409  Path(Path& parent, const char* pathname, int flags=0);
410  Path(const Path&) = delete;
411  Path(Path&&) = default;
412  Path& operator=(const Path&) = delete;
413  Path& operator=(Path&&) = default;
414 
415  DIR* fdopendir();
416 
418  iterator begin();
419 
421  iterator end();
422 
423  int openat(const char* pathname, int flags, mode_t mode=0777);
424 
426  int openat_ifexists(const char* pathname, int flags, mode_t mode=0777);
427 
428  bool faccessat(const char* pathname, int mode, int flags=0);
429 
430  void fstatat(const char* pathname, struct stat& st);
431 
433  bool fstatat_ifexists(const char* pathname, struct stat& st);
434 
436  void lstatat(const char* pathname, struct stat& st);
437 
439  bool lstatat_ifexists(const char* pathname, struct stat& st);
440 
441  void unlinkat(const char* pathname);
442 
444  void rmdirat(const char* pathname);
445 
451  void rmtree();
452 };
453 
454 
459 {
460 public:
461  using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
462 
463  File(File&&) = default;
464  File(const File&) = delete;
465 
469  File(const std::string& pathname);
470 
472  File(const std::string& pathname, int flags, mode_t mode=0777);
473 
474  File& operator=(const File&) = delete;
475  File& operator=(File&&) = default;
476 
478  void open(int flags, mode_t mode=0777);
479 
484  bool open_ifexists(int flags, mode_t mode=0777);
485 
486  static File mkstemp(const std::string& prefix);
487  static File mkstemp(const char* prefix);
488  static File mkstemp(char* pathname_template);
489 };
490 
492 std::string read_file(const std::string &file);
493 
500 void write_file(const std::string& file, const std::string& data, mode_t mode=0777);
501 
508 void write_file(const std::string& file, const void* data, size_t size, mode_t mode=0777);
509 
519 void write_file_atomically(const std::string& file, const std::string& data, mode_t mode=0777);
520 
530 void write_file_atomically(const std::string& file, const void* data, size_t size, mode_t mode=0777);
531 
532 #if 0
533 // Create a temporary directory based on a template.
534 std::string mkdtemp(std::string templ);
535 
538 void mkFilePath(const std::string& file);
539 #endif
540 
546 bool unlink_ifexists(const std::string& file);
547 
553 bool rename_ifexists(const std::string& src, const std::string& dst);
554 
563 bool mkdir_ifmissing(const char* pathname, mode_t mode=0777);
564 
565 bool mkdir_ifmissing(const std::string& pathname, mode_t mode=0777);
566 
573 bool makedirs(const std::string& pathname, mode_t=0777);
574 
582 std::string which(const std::string& name);
583 
585 void unlink(const std::string& pathname);
586 
588 void rmdir(const std::string& pathname);
589 
591 void rmtree(const std::string& pathname);
592 
598 bool rmtree_ifexists(const std::string& pathname);
599 
606 void rename(const std::string& src_pathname, const std::string& dst_pathname);
607 
611 void touch(const std::string& pathname, time_t ts);
612 
616 void clock_gettime(::clockid_t clk_id, struct ::timespec& ts);
617 
621 unsigned long long timesec_elapsed(const struct ::timespec& begin, const struct ::timespec& until);
622 
626 struct Clock
627 {
628  ::clockid_t clk_id;
629  struct ::timespec ts;
630 
634  Clock(::clockid_t clk_id);
635 
640  unsigned long long elapsed();
641 };
642 
647 void getrlimit(int resource, struct ::rlimit& rlim);
649 
651 void setrlimit(int resource, const struct ::rlimit& rlim);
652 
655 {
656  int resource;
657  struct ::rlimit orig;
658 
659  OverrideRlimit(int resource, rlim_t rlim);
660  ~OverrideRlimit();
661 
663  void set(rlim_t rlim);
664 };
665 
666 }
667 }
668 
669 #endif
RAII mechanism to save restore file times at the end of some file operations.
Definition: sys.h:273
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:98
Common operations on file descriptors.
Definition: sys.h:134
File in the file system.
Definition: sys.h:458
File descriptor with a name.
Definition: sys.h:289
Iterator for directory entries.
Definition: sys.h:344
File descriptor that gets automatically closed in the object destructor.
Definition: sys.h:315
Override a soft resource limit during the lifetime of the object.
Definition: sys.h:654
const std::string & name() const
Return the file pathname.
Definition: sys.h:308
String functions.
Definition: benchmark.h:13
Access to clock_gettime.
Definition: sys.h:626
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:339