Elaboradar  0.1
 Tutto Classi Namespace File Funzioni Variabili Tipi enumerati (enum) Gruppi
sys.h
1 #ifndef RADARELAB_UTILS_SYS_H
2 #define RADARELAB_UTILS_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 #include <fcntl.h>
20 
21 namespace radarelab {
22 namespace utils {
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  int dup();
182 
183  size_t read(void* buf, size_t count);
184 
189  void read_all_or_throw(void* buf, size_t count);
190 
191  size_t write(const void* buf, size_t count);
192 
193  template<typename Container>
194  size_t write(const Container& c)
195  {
196  return write(c.data(), c.size() * sizeof(Container::value_type));
197  }
198 
200  void write_all_or_retry(const void* buf, size_t count);
201 
202  template<typename Container>
203  void write_all_or_retry(const Container& c)
204  {
205  write_all_or_retry(c.data(), c.size() * sizeof(typename Container::value_type));
206  }
207 
212  void write_all_or_throw(const void* buf, size_t count);
213 
214  template<typename Container>
215  void write_all_or_throw(const Container& c)
216  {
217  write_all_or_throw(c.data(), c.size() * sizeof(typename Container::value_type));
218  }
219 
220  off_t lseek(off_t offset, int whence=SEEK_SET);
221 
222  size_t pread(void* buf, size_t count, off_t offset);
223  size_t pwrite(const void* buf, size_t count, off_t offset);
224 
225  template<typename Container>
226  size_t pwrite(const Container& c, off_t offset)
227  {
228  return pwrite(c.data(), c.size() * sizeof(typename Container::value_type), offset);
229  }
230 
231  void ftruncate(off_t length);
232 
233  MMap mmap(size_t length, int prot, int flags, off_t offset=0);
234 
241  bool ofd_setlk(struct ::flock&);
242 
252  bool ofd_setlkw(struct ::flock&, bool retry_on_signal=true);
253 
259  bool ofd_getlk(struct ::flock&);
260 
261  operator int() const { return fd; }
262 };
263 
264 
269 {
270 protected:
271  std::string pathname;
272 
273 public:
274  NamedFileDescriptor(int fd, const std::string& pathname);
277 
278  // We can copy at the NamedFileDescriptor level because the destructor does not
279  // close fd
280  NamedFileDescriptor(const NamedFileDescriptor& o) = default;
281  NamedFileDescriptor& operator=(const NamedFileDescriptor& o) = default;
282 
283  [[noreturn]] virtual void throw_error(const char* desc);
284  [[noreturn]] virtual void throw_runtime_error(const char* desc);
285 
287  const std::string& name() const { return pathname; }
288 };
289 
290 
295 {
296  using NamedFileDescriptor::NamedFileDescriptor;
297 
300 
309 
310  ManagedNamedFileDescriptor& operator=(const ManagedNamedFileDescriptor&) = delete;
312 };
313 
314 
319 {
323  struct iterator : public std::iterator<std::input_iterator_tag, struct dirent>
324  {
325  Path* path = nullptr;
326  DIR* dir = nullptr;
327  struct dirent* cur_entry = nullptr;
328 
329  // End iterator
330  iterator();
331  // Start iteration on dir
332  iterator(Path& dir);
333  iterator(iterator&) = delete;
334  iterator(iterator&& o)
335  : dir(o.dir), cur_entry(o.cur_entry)
336  {
337  o.dir = nullptr;
338  o.cur_entry = nullptr;
339  }
340  ~iterator();
341  iterator& operator=(iterator&) = delete;
342  iterator& operator=(iterator&&) = delete;
343 
344  bool operator==(const iterator& i) const;
345  bool operator!=(const iterator& i) const;
346  struct dirent& operator*() const { return *cur_entry; }
347  struct dirent* operator->() const { return cur_entry; }
348  void operator++();
349 
351  bool isdir() const;
352 
354  bool isblk() const;
355 
357  bool ischr() const;
358 
360  bool isfifo() const;
361 
363  bool islnk() const;
364 
366  bool isreg() const;
367 
369  bool issock() const;
370 
372  Path open_path(int flags=0) const;
373  };
374 
375  using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
376 
380  Path(const char* pathname, int flags=0);
384  Path(const std::string& pathname, int flags=0);
388  Path(Path& parent, const char* pathname, int flags=0);
389  Path(const Path&) = delete;
390  Path(Path&&) = default;
391  Path& operator=(const Path&) = delete;
392  Path& operator=(Path&&) = default;
393 
394  DIR* fdopendir();
395 
397  iterator begin();
398 
400  iterator end();
401 
402  int openat(const char* pathname, int flags, mode_t mode=0777);
403 
404  void fstatat(const char* pathname, struct stat& st);
405 
407  bool fstatat_ifexists(const char* pathname, struct stat& st);
408 
410  void lstatat(const char* pathname, struct stat& st);
411 
413  bool lstatat_ifexists(const char* pathname, struct stat& st);
414 
415  void unlinkat(const char* pathname);
416 
418  void rmdirat(const char* pathname);
419 
425  void rmtree();
426 };
427 
428 
433 {
434 public:
435  using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
436 
437  File(File&&) = default;
438  File(const File&) = delete;
439 
443  File(const std::string& pathname);
444 
446  File(const std::string& pathname, int flags, mode_t mode=0777);
447 
448  File& operator=(const File&) = delete;
449  File& operator=(File&&) = default;
450 
452  void open(int flags, mode_t mode=0777);
453 
458  bool open_ifexists(int flags, mode_t mode=0777);
459 
460  static File mkstemp(const std::string& prefix);
461 };
462 
464 std::string read_file(const std::string &file);
465 
472 void write_file(const std::string& file, const std::string& data, mode_t mode=0777);
473 
480 void write_file(const std::string& file, const void* data, size_t size, mode_t mode=0777);
481 
491 void write_file_atomically(const std::string& file, const std::string& data, mode_t mode=0777);
492 
502 void write_file_atomically(const std::string& file, const void* data, size_t size, mode_t mode=0777);
503 
504 #if 0
505 // Create a temporary directory based on a template.
506 std::string mkdtemp(std::string templ);
507 
510 void mkFilePath(const std::string& file);
511 #endif
512 
518 bool unlink_ifexists(const std::string& file);
519 
525 bool rename_ifexists(const std::string& src, const std::string& dst);
526 
535 bool mkdir_ifmissing(const char* pathname, mode_t mode=0777);
536 
537 bool mkdir_ifmissing(const std::string& pathname, mode_t mode=0777);
538 
545 bool makedirs(const std::string& pathname, mode_t=0777);
546 
554 std::string which(const std::string& name);
555 
557 void unlink(const std::string& pathname);
558 
560 void rmdir(const std::string& pathname);
561 
563 void rmtree(const std::string& pathname);
564 
571 void rename(const std::string& src_pathname, const std::string& dst_pathname);
572 
573 #if 0
574 class Directory
576 {
577 protected:
579  std::string m_path;
580 
581 public:
582  class const_iterator
583  {
585  const Directory* dir;
587  void* dirp;
589  struct dirent* direntbuf;
590 
591  public:
592  // Create an end iterator
593  const_iterator();
594  // Create a begin iterator
595  const_iterator(const Directory& dir);
596  // Cleanup properly
597  ~const_iterator();
598 
600  const_iterator(const const_iterator& i);
601  const_iterator& operator=(const const_iterator& i);
602 
604  const_iterator& operator++();
605 
607  std::string operator*() const;
608 
609  bool operator==(const const_iterator& iter) const;
610  bool operator!=(const const_iterator& iter) const;
611  };
612 
613  Directory(const std::string& path);
614  ~Directory();
615 
617  const std::string& path() const { return m_path; }
618 
620  bool exists() const;
621 
623  const_iterator begin() const;
624 
626  const_iterator end() const;
627 };
628 
629 #endif
630 }
631 }
632 }
633 
634 #endif
bool open_ifexists(int flags, mode_t mode=0777)
Wrap open(2) and return false instead of throwing an exception if open fails with ENOENT...
void rmtree()
Delete the directory pointed to by this Path, with all its contents.
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:318
bool fstatat_ifexists(const char *pathname, struct stat &st)
fstatat, but in case of ENOENT returns false instead of throwing
const_iterator end() const
End iterator.
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:98
Common operations on file descriptors.
Definition: sys.h:134
bool is_open() const
Check if the file descriptor is open (that is, if it is not -1)
File descriptor that gets automatically closed in the object destructor.
Definition: sys.h:294
void lstatat(const char *pathname, struct stat &st)
fstatat with the AT_SYMLINK_NOFOLLOW flag set
void write_all_or_throw(const void *buf, size_t count)
Write all the data in buf, throwing runtime_error in case of a partial write.
bool ofd_setlkw(struct::flock &, bool retry_on_signal=true)
Open file description locks F_OFD_SETLKW operation.
void close()
Close the file descriptor, setting its value to -1.
bool ofd_getlk(struct::flock &)
Open file description locks F_OFD_GETLK operation.
void read_all_or_throw(void *buf, size_t count)
Read all the data into buf, throwing runtime_error in case of a partial read.
const_iterator begin() const
Begin iterator.
Path(const char *pathname, int flags=0)
Open the given pathname with flags | O_PATH.
Path open_path(int flags=0) const
Return a Path object for this entry.
void write_all_or_retry(const void *buf, size_t count)
Write all the data in buf, retrying partial writes.
std::string m_path
Directory pathname.
Definition: sys.h:579
File in the file system.
Definition: sys.h:432
iterator end()
End iterator on all directory entries.
Iterator for directory entries.
Definition: sys.h:323
bool ofd_setlk(struct::flock &)
Open file description locks F_OFD_SETLK operation.
bool exists() const
Check if the directory exists.
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
const std::string & path() const
Pathname of the directory.
Definition: sys.h:617
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.
bool lstatat_ifexists(const char *pathname, struct stat &st)
lstatat, but in case of ENOENT returns false instead of throwing
iterator begin()
Begin iterator on all directory entries.
File descriptor with a name.
Definition: sys.h:268
void rmdirat(const char *pathname)
unlinkat with the AT_REMOVEDIR flag set
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
void open(int flags, mode_t mode=0777)
Wrapper around open(2)
~ManagedNamedFileDescriptor()
The destructor closes the file descriptor, but does not check errors on ::close().
Nicely wrap access to directories.
Definition: sys.h:575
const std::string & name() const
Return the file pathname.
Definition: sys.h:287