libept
sys.h
Go to the documentation of this file.
1 #ifndef EPT_SYS_H
2 #define EPT_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 ept {
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 
150  [[noreturn]] virtual void throw_error(const char* desc);
151 
152  void close();
153 
154  void fstat(struct stat& st);
155  void fchmod(mode_t mode);
156 
157  size_t write(const void* buf, size_t count);
158 
162  void write_all(const void* buf, size_t count);
163 
164  MMap mmap(size_t length, int prot, int flags, off_t offset=0);
165 
166  operator int() const { return fd; }
167 };
168 
169 
175 {
176 protected:
177  std::string pathname;
178 
179 public:
180  NamedFileDescriptor(int fd, const std::string& pathname);
182 
184 
185  [[noreturn]] virtual void throw_error(const char* desc);
186 
188  const std::string& name() const { return pathname; }
189 };
190 
194 struct Path : public NamedFileDescriptor
195 {
199  struct iterator : public std::iterator<std::input_iterator_tag, struct dirent>
200  {
201  Path* path = nullptr;
202  DIR* dir = nullptr;
203  struct dirent* cur_entry = nullptr;
204 
205  // End iterator
206  iterator();
207  // Start iteration on dir
208  iterator(Path& dir);
209  iterator(iterator&) = delete;
211  : dir(o.dir), cur_entry(o.cur_entry)
212  {
213  o.dir = nullptr;
214  o.cur_entry = nullptr;
215  }
216  ~iterator();
219 
220  bool operator==(const iterator& i) const;
221  bool operator!=(const iterator& i) const;
222  struct dirent& operator*() const { return *cur_entry; }
223  struct dirent* operator->() const { return cur_entry; }
224  void operator++();
225 
227  bool isdir() const;
228 
230  bool isblk() const;
231 
233  bool ischr() const;
234 
236  bool isfifo() const;
237 
239  bool islnk() const;
240 
242  bool isreg() const;
243 
245  bool issock() const;
246  };
247 
249 
253  Path(const char* pathname, int flags=0);
257  Path(const std::string& pathname, int flags=0);
261  Path(Path& parent, const char* pathname, int flags=0);
262  Path(const Path&) = delete;
263  Path(Path&&) = default;
264  Path& operator=(const Path&) = delete;
265  Path& operator=(Path&&) = default;
266 
274  ~Path();
275 
276  DIR* fdopendir();
277 
279  iterator begin();
280 
282  iterator end();
283 
284  int openat(const char* pathname, int flags, mode_t mode=0777);
285 
286  void fstatat(const char* pathname, struct stat& st);
287 
289  void lstatat(const char* pathname, struct stat& st);
290 
291  void unlinkat(const char* pathname);
292 
294  void rmdirat(const char* pathname);
295 
301  void rmtree();
302 };
303 
304 
308 class File : public NamedFileDescriptor
309 {
310 public:
312 
313  File(File&&) = default;
314  File(const File&) = delete;
315 
317  File(const std::string& pathname, int flags, mode_t mode=0777);
318 
326  ~File();
327 
328  File& operator=(const File&) = delete;
329  File& operator=(File&&) = default;
330 
331  static File mkstemp(const std::string& prefix);
332 };
333 
335 std::string read_file(const std::string &file);
336 
343 void write_file(const std::string& file, const std::string& data, mode_t mode=0777);
344 
354 void write_file_atomically(const std::string& file, const std::string& data, mode_t mode=0777);
355 
356 #if 0
357 // Create a temporary directory based on a template.
358 std::string mkdtemp(std::string templ);
359 
362 void mkFilePath(const std::string& file);
363 #endif
364 
370 bool unlink_ifexists(const std::string& file);
371 
377 bool rename_ifexists(const std::string& src, const std::string& dst);
378 
382 void mkdir_ifmissing(const char* pathname, mode_t mode=0777);
383 
384 void mkdir_ifmissing(const std::string& pathname, mode_t mode=0777);
385 
388 void makedirs(const std::string& pathname, mode_t=0777);
389 
397 std::string which(const std::string& name);
398 
400 void unlink(const std::string& pathname);
401 
403 void rmdir(const std::string& pathname);
404 
406 void rmtree(const std::string& pathname);
407 
408 #if 0
409 class Directory
411 {
412 protected:
414  std::string m_path;
415 
416 public:
417  class const_iterator
418  {
420  const Directory* dir;
422  void* dirp;
424  struct dirent* direntbuf;
425 
426  public:
427  // Create an end iterator
428  const_iterator();
429  // Create a begin iterator
430  const_iterator(const Directory& dir);
431  // Cleanup properly
432  ~const_iterator();
433 
435  const_iterator(const const_iterator& i);
436  const_iterator& operator=(const const_iterator& i);
437 
439  const_iterator& operator++();
440 
442  std::string operator*() const;
443 
444  bool operator==(const const_iterator& iter) const;
445  bool operator!=(const const_iterator& iter) const;
446  };
447 
448  Directory(const std::string& path);
449  ~Directory();
450 
452  const std::string& path() const { return m_path; }
453 
455  bool exists() const;
456 
458  const_iterator begin() const;
459 
461  const_iterator end() const;
462 };
463 
464 #endif
465 }
466 }
467 
468 #endif
ept::sys::Path::Path
Path(Path &&)=default
ept::sys::MMap::operator=
MMap & operator=(const MMap &)=delete
ept::sys::File::File
File(const File &)=delete
sys.h
ept::sys::MMap::MMap
MMap(const MMap &)=delete
ept::sys::File::~File
~File()
The destructor closes the file descriptor, but does not check errors on ::close().
Definition: sys.cc:582
ept::sys::Path::lstatat
void lstatat(const char *pathname, struct stat &st)
fstatat with the AT_SYMLINK_NOFOLLOW flag set
Definition: sys.cc:384
ept::sys::write_file_atomically
void write_file_atomically(const std::string &file, const std::string &data, mode_t mode)
Write data to file, replacing existing contents if it already exists.
Definition: sys.cc:619
ept::sys::Path::iterator::dir
DIR * dir
Definition: sys.h:202
ept::str::normpath
std::string normpath(const std::string &pathname)
Normalise a pathname.
Definition: string.cc:104
ept::sys::Path::rmtree
void rmtree()
Delete the directory pointed to by this Path, with all its contents.
Definition: sys.cc:553
string.h
ept::sys::Path::iterator::ischr
bool ischr() const
Definition: sys.cc:485
ept::sys::Path::iterator::path
Path * path
Definition: sys.h:201
ept::sys::Path::Path
Path(const Path &)=delete
ept::sys::Path::iterator::~iterator
~iterator()
Definition: sys.cc:422
ept::sys::rmdir
void rmdir(const std::string &pathname)
Remove the directory using rmdir(2)
Definition: sys.cc:770
ept::sys::Path::iterator::islnk
bool islnk() const
Definition: sys.cc:513
ept::sys::unlink
void unlink(const std::string &pathname)
Delete the file using unlink()
Definition: sys.cc:764
ept::sys::Path::iterator::operator==
bool operator==(const iterator &i) const
Definition: sys.cc:428
ept::sys::NamedFileDescriptor::name
const std::string & name() const
Return the file pathname.
Definition: sys.h:188
common_stat_body
#define common_stat_body(testfunc)
Definition: sys.cc:56
ept::sys::FileDescriptor::~FileDescriptor
virtual ~FileDescriptor()
Definition: sys.cc:232
ept::sys::Path::iterator::issock
bool issock() const
Definition: sys.cc:539
ept::sys::Path::fdopendir
DIR * fdopendir()
Definition: sys.cc:344
ept::sys::NamedFileDescriptor::NamedFileDescriptor
NamedFileDescriptor(int fd, const std::string &pathname)
Definition: sys.cc:287
ept::sys::Path::iterator::operator*
struct dirent & operator*() const
Definition: sys.h:222
ept::sys::FileDescriptor::fchmod
void fchmod(mode_t mode)
Definition: sys.cc:253
ept::sys::isblk
bool isblk(const std::string &pathname)
Same as isdir but checks for block devices.
Definition: sys.cc:71
ept::sys::isdir
bool isdir(const std::string &pathname)
Returns true if the given pathname is a directory, else false.
Definition: sys.cc:66
ept::sys::Path::iterator::iterator
iterator()
Definition: sys.cc:402
ept::sys::NamedFileDescriptor::operator=
NamedFileDescriptor & operator=(NamedFileDescriptor &&)
Definition: sys.cc:297
ept::sys::Path::iterator::cur_entry
struct dirent * cur_entry
Definition: sys.h:203
ept::sys::Path
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:195
ept::sys::Path::unlinkat
void unlinkat(const char *pathname)
Definition: sys.cc:390
ept::sys::Path::iterator::operator=
iterator & operator=(iterator &)=delete
ept::sys::write_file
void write_file(const std::string &file, const std::string &data, mode_t mode)
Write data to file, replacing existing contents if it already exists.
Definition: sys.cc:612
ept::sys::Path::Path
Path(const char *pathname, int flags=0)
Open the given pathname with flags | O_PATH.
Definition: sys.cc:316
ept::str::joinpath
std::string joinpath(const std::string &path1, const std::string &path2)
Definition: string.cc:97
ept::sys::read_file
std::string read_file(const std::string &file)
Read whole file into memory. Throws exceptions on failure.
Definition: sys.cc:598
ept::sys::Path::begin
iterator begin()
Begin iterator on all directory entries.
Definition: sys.cc:357
ept::sys::NamedFileDescriptor::pathname
std::string pathname
Definition: sys.h:177
ept::sys::Path::iterator::operator->
struct dirent * operator->() const
Definition: sys.h:223
ept::sys::Path::rmdirat
void rmdirat(const char *pathname)
unlinkat with the AT_REMOVEDIR flag set
Definition: sys.cc:396
ept
String functions.
Definition: apt.cc:40
res
set< string > & res
Definition: packagerecord.cc:73
ept::sys::MMap
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:97
ept::sys::Path::iterator::operator++
void operator++()
Definition: sys.cc:441
ept::sys::rename_ifexists
bool rename_ifexists(const std::string &src, const std::string &dst)
Move src to dst, without raising exception if src does not exist.
Definition: sys.cc:659
ept::sys::Path::iterator
Iterator for directory entries.
Definition: sys.h:200
ept::sys::Path::fstatat
void fstatat(const char *pathname, struct stat &st)
Definition: sys.cc:378
ept::sys::which
std::string which(const std::string &name)
Compute the absolute path of an executable.
Definition: sys.cc:743
ept::sys::NamedFileDescriptor::throw_error
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
Definition: sys.cc:306
ept::sys::FileDescriptor::fd
int fd
Definition: sys.h:135
ept::sys::Path::openat
int openat(const char *pathname, int flags, mode_t mode=0777)
Definition: sys.cc:370
ept::sys::Path::iterator::isdir
bool isdir() const
Definition: sys.cc:457
ept::sys::Path::iterator::operator!=
bool operator!=(const iterator &i) const
Definition: sys.cc:434
ept::sys::exists
bool exists(const std::string &file)
Same as access(s, F_OK);.
Definition: sys.cc:148
ept::str::Split
Split a string where a given substring is found.
Definition: string.h:223
ept::sys::File::operator=
File & operator=(const File &)=delete
ept::sys::Path::operator=
Path & operator=(Path &&)=default
ept::sys::Path::iterator::iterator
iterator(iterator &)=delete
ept::sys::Path::end
iterator end()
End iterator on all directory entries.
Definition: sys.cc:365
ept::sys::Path::iterator::isreg
bool isreg() const
Definition: sys.cc:526
ept::sys::FileDescriptor::fstat
void fstat(struct stat &st)
Definition: sys.cc:247
ept::sys::isreg
bool isreg(const std::string &pathname)
Same as isdir but checks for regular files.
Definition: sys.cc:91
ept::sys::mkdir_ifmissing
void mkdir_ifmissing(const char *pathname, mode_t mode)
Create the given directory, if it does not already exists.
Definition: sys.cc:721
ept::sys::Path::iterator::isfifo
bool isfifo() const
Definition: sys.cc:499
ept::sys::FileDescriptor::write
size_t write(const void *buf, size_t count)
Definition: sys.cc:259
ept::sys::rmtree
void rmtree(const std::string &pathname)
Delete the directory pathname and all its contents.
Definition: sys.cc:776
ept::sys::FileDescriptor::write_all
void write_all(const void *buf, size_t count)
Write all the data in buf, retrying partial writes.
Definition: sys.cc:267
ept::sys::makedirs
void makedirs(const std::string &pathname, mode_t mode)
Create all the component of the given directory, including the directory itself.
Definition: sys.cc:731
ept::sys::Path::operator=
Path & operator=(const Path &)=delete
ept::sys::File::mkstemp
static File mkstemp(const std::string &prefix)
Definition: sys.cc:587
ept::sys::abspath
std::string abspath(const std::string &pathname)
Get the absolute path of a file.
Definition: sys.cc:171
ept::sys::size
size_t size(const std::string &file)
File size.
Definition: sys.cc:116
ept::sys::Path::iterator::iterator
iterator(iterator &&o)
Definition: sys.h:210
ept::sys::FileDescriptor::close
void close()
Definition: sys.cc:239
std
Definition: packagerecord-test.cc:4
ept::sys::FileDescriptor::throw_error
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
Definition: sys.cc:234
ept::sys::MMap::munmap
void munmap()
Definition: sys.cc:213
ept::sys::unlink_ifexists
bool unlink_ifexists(const std::string &file)
Delete a file if it exists.
Definition: sys.cc:646
ept::sys::Path::iterator::operator=
iterator & operator=(iterator &&)=delete
ept::sys::stat
std::unique_ptr< struct stat > stat(const std::string &pathname)
stat() the given file and return the struct stat with the results.
Definition: sys.cc:37
ept::sys::isfifo
bool isfifo(const std::string &pathname)
Same as isdir but checks for FIFOs.
Definition: sys.cc:81
ept::sys::Path::iterator::isblk
bool isblk() const
Definition: sys.cc:471
ept::sys::getcwd
std::string getcwd()
Get the absolute path of the current working directory.
Definition: sys.cc:153
ept::sys::islnk
bool islnk(const std::string &pathname)
Same as isdir but checks for symbolic links.
Definition: sys.cc:86
ept::sys::FileDescriptor::FileDescriptor
FileDescriptor()
Definition: sys.cc:225
ept::sys::File::operator=
File & operator=(File &&)=default
ept::sys::MMap::~MMap
~MMap()
Definition: sys.cc:208
ept::sys::File::File
File(File &&)=default
ept::sys::ischr
bool ischr(const std::string &pathname)
Same as isdir but checks for character devices.
Definition: sys.cc:76
ept::sys::FileDescriptor::mmap
MMap mmap(size_t length, int prot, int flags, off_t offset=0)
Definition: sys.cc:274
ept::sys::File
open(2) file descriptors
Definition: sys.h:309
ept::sys::Path::~Path
~Path()
The destructor closes the file descriptor, but does not check errors on ::close().
Definition: sys.cc:338
O_PATH
#define O_PATH
Definition: sys.cc:17
ept::sys::NamedFileDescriptor
File descriptor with a name.
Definition: sys.h:175
ept::sys::FileDescriptor
Common operations on file descriptors.
Definition: sys.h:133
ept::sys::inode
ino_t inode(const std::string &file)
File inode number.
Definition: sys.cc:129
ept::str::dirname
std::string dirname(const std::string &pathname)
Given a pathname, return the directory name without the file name.
Definition: string.cc:18
ept::sys::MMap::size
size_t size() const
Definition: sys.h:110
ept::sys::timestamp
time_t timestamp(const std::string &file)
File mtime.
Definition: sys.cc:103
ept::sys::issock
bool issock(const std::string &pathname)
Same as isdir but checks for sockets.
Definition: sys.cc:96
ept::sys::access
bool access(const std::string &s, int m)
access() a filename
Definition: sys.cc:143