15 #include <sys/types.h>
29 std::unique_ptr<struct stat> stat(
const std::string& pathname);
35 void stat(
const std::string& pathname,
struct stat& st);
42 bool isdir(
const std::string& pathname);
45 bool isblk(
const std::string& pathname);
48 bool ischr(
const std::string& pathname);
51 bool isfifo(
const std::string& pathname);
54 bool islnk(
const std::string& pathname);
57 bool isreg(
const std::string& pathname);
60 bool issock(
const std::string& pathname);
63 time_t timestamp(
const std::string& file);
66 time_t timestamp(
const std::string& file, time_t def);
69 size_t size(
const std::string& file);
72 size_t size(
const std::string& file,
size_t def);
75 ino_t inode(
const std::string& file);
78 ino_t inode(
const std::string& file, ino_t def);
81 bool access(
const std::string& s,
int m);
84 bool exists(
const std::string& s);
90 std::string abspath(
const std::string& pathname);
105 MMap(
void* addr,
size_t length);
108 MMap& operator=(
const MMap&) =
delete;
111 size_t size()
const {
return length; }
116 operator const T*()
const {
return reinterpret_cast<const T*
>(addr); }
119 operator T*()
const {
return reinterpret_cast<T*
>(addr); };
156 [[noreturn]]
virtual void throw_error(
const char* desc);
177 void fstat(
struct stat& st);
178 void fchmod(mode_t mode);
180 void futimens(
const struct timespec ts[2]);
187 size_t read(
void* buf,
size_t count);
195 size_t write(
const void* buf,
size_t count);
197 template<
typename Container>
198 size_t write(
const Container& c)
200 return write(c.data(), c.size() *
sizeof(Container::value_type));
206 template<
typename Container>
218 template<
typename Container>
224 off_t lseek(off_t offset,
int whence=SEEK_SET);
226 size_t pread(
void* buf,
size_t count, off_t offset);
227 size_t pwrite(
const void* buf,
size_t count, off_t offset);
229 template<
typename Container>
230 size_t pwrite(
const Container& c, off_t offset)
232 return pwrite(c.data(), c.size() *
sizeof(
typename Container::value_type), offset);
235 void ftruncate(off_t length);
237 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
256 bool ofd_setlkw(struct ::flock&,
bool retry_on_signal=
true);
265 operator int()
const {
return fd; }
276 struct timespec ts[2];
291 std::string pathname;
303 [[noreturn]]
virtual void throw_error(
const char* desc);
307 const std::string&
name()
const {
return pathname; }
316 using NamedFileDescriptor::NamedFileDescriptor;
343 struct iterator :
public std::iterator<std::input_iterator_tag, struct dirent>
345 Path* path =
nullptr;
347 struct dirent* cur_entry =
nullptr;
355 : dir(o.dir), cur_entry(o.cur_entry)
358 o.cur_entry =
nullptr;
364 bool operator==(
const iterator& i)
const;
365 bool operator!=(
const iterator& i)
const;
366 struct dirent& operator*()
const {
return *cur_entry; }
367 struct dirent* operator->()
const {
return cur_entry; }
395 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
400 Path(
const char* pathname,
int flags=0);
404 Path(
const std::string& pathname,
int flags=0);
408 Path(
Path& parent,
const char* pathname,
int flags=0);
411 Path& operator=(
const Path&) =
delete;
422 int openat(
const char* pathname,
int flags, mode_t mode=0777);
424 bool faccessat(
const char* pathname,
int mode,
int flags=0);
426 void fstatat(
const char* pathname,
struct stat& st);
432 void lstatat(
const char* pathname,
struct stat& st);
437 void unlinkat(
const char* pathname);
440 void rmdirat(
const char* pathname);
457 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
465 File(
const std::string& pathname);
468 File(
const std::string& pathname,
int flags, mode_t mode=0777);
470 File& operator=(
const File&) =
delete;
474 void open(
int flags, mode_t mode=0777);
482 static File mkstemp(
const std::string& prefix);
483 static File mkstemp(
const char* prefix);
484 static File mkstemp(
char* pathname_template);
488 std::string read_file(
const std::string &file);
496 void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
504 void write_file(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
515 void write_file_atomically(
const std::string& file,
const std::string& data, mode_t mode=0777);
526 void write_file_atomically(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
530 std::string mkdtemp(std::string templ);
534 void mkFilePath(
const std::string& file);
542 bool unlink_ifexists(
const std::string& file);
549 bool rename_ifexists(
const std::string& src,
const std::string& dst);
559 bool mkdir_ifmissing(
const char* pathname, mode_t mode=0777);
561 bool mkdir_ifmissing(
const std::string& pathname, mode_t mode=0777);
569 bool makedirs(
const std::string& pathname, mode_t=0777);
578 std::string which(
const std::string& name);
581 void unlink(
const std::string& pathname);
584 void rmdir(
const std::string& pathname);
587 void rmtree(
const std::string& pathname);
594 bool rmtree_ifexists(
const std::string& pathname);
602 void rename(
const std::string& src_pathname,
const std::string& dst_pathname);
607 void touch(
const std::string& pathname, time_t ts);
621 const Directory* dir;
625 struct dirent* direntbuf;
631 const_iterator(
const Directory& dir);
636 const_iterator(
const const_iterator& i);
637 const_iterator& operator=(
const const_iterator& i);
640 const_iterator& operator++();
643 std::string operator*()
const;
645 bool operator==(
const const_iterator& iter)
const;
646 bool operator!=(
const const_iterator& iter)
const;
649 Directory(
const std::string& path);
653 const std::string& path()
const {
return m_path; }
659 const_iterator begin()
const;
662 const_iterator end()
const;
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
bool ofd_setlkw(struct::flock &, bool retry_on_signal=true)
Open file description locks F_OFD_SETLKW operation.
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...
const std::string & name() const
Return the file pathname.
Definition: sys.h:307
RAII mechanism to save restore file times at the end of some file operations.
Definition: sys.h:272
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:97
void close()
Close the file descriptor, setting its value to -1.
Common operations on file descriptors.
Definition: sys.h:133
bool ofd_setlk(struct::flock &)
Open file description locks F_OFD_SETLK operation.
File in the file system.
Definition: sys.h:454
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.
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.
iterator begin()
Begin iterator on all directory entries.
File descriptor with a name.
Definition: sys.h:288
void lstatat(const char *pathname, struct stat &st)
fstatat with the AT_SYMLINK_NOFOLLOW flag set
void write_all_or_retry(const void *buf, size_t count)
Write all the data in buf, retrying partial writes.
Iterator for directory entries.
Definition: sys.h:343
File descriptor that gets automatically closed in the object destructor.
Definition: sys.h:314
void open(int flags, mode_t mode=0777)
Wrapper around open(2)
Path open_path(int flags=0) const
Return a Path object for this entry.
Path(const char *pathname, int flags=0)
Open the given pathname with flags | O_PATH.
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.
iterator end()
End iterator on all directory entries.
void rmtree()
Delete the directory pointed to by this Path, with all its contents.
~ManagedNamedFileDescriptor()
The destructor closes the file descriptor, but does not check errors on ::close().
bool fstatat_ifexists(const char *pathname, struct stat &st)
fstatat, but in case of ENOENT returns false instead of throwing
bool ofd_getlk(struct::flock &)
Open file description locks F_OFD_GETLK operation.
bool lstatat_ifexists(const char *pathname, struct stat &st)
lstatat, but in case of ENOENT returns false instead of throwing
bool is_open() const
Check if the file descriptor is open (that is, if it is not -1)
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:338
void rmdirat(const char *pathname)
unlinkat with the AT_REMOVEDIR flag set
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.