1 #ifndef RADARELAB_UTILS_SYS_H
2 #define RADARELAB_UTILS_SYS_H
15 #include <sys/types.h>
30 std::unique_ptr<struct stat> stat(
const std::string& pathname);
36 void stat(
const std::string& pathname,
struct stat& st);
43 bool isdir(
const std::string& pathname);
46 bool isblk(
const std::string& pathname);
49 bool ischr(
const std::string& pathname);
52 bool isfifo(
const std::string& pathname);
55 bool islnk(
const std::string& pathname);
58 bool isreg(
const std::string& pathname);
61 bool issock(
const std::string& pathname);
64 time_t timestamp(
const std::string& file);
67 time_t timestamp(
const std::string& file, time_t def);
70 size_t size(
const std::string& file);
73 size_t size(
const std::string& file,
size_t def);
76 ino_t inode(
const std::string& file);
79 ino_t inode(
const std::string& file, ino_t def);
82 bool access(
const std::string& s,
int m);
85 bool exists(
const std::string& s);
91 std::string abspath(
const std::string& pathname);
106 MMap(
void* addr,
size_t length);
109 MMap& operator=(
const MMap&) =
delete;
112 size_t size()
const {
return length; }
117 operator const T*()
const {
return reinterpret_cast<const T*
>(addr); }
120 operator T*()
const {
return reinterpret_cast<T*
>(addr); };
157 [[noreturn]]
virtual void throw_error(
const char* desc);
178 void fstat(
struct stat& st);
179 void fchmod(mode_t mode);
183 size_t read(
void* buf,
size_t count);
191 size_t write(
const void* buf,
size_t count);
193 template<
typename Container>
194 size_t write(
const Container& c)
196 return write(c.data(), c.size() *
sizeof(Container::value_type));
202 template<
typename Container>
214 template<
typename Container>
220 off_t lseek(off_t offset,
int whence=SEEK_SET);
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);
225 template<
typename Container>
226 size_t pwrite(
const Container& c, off_t offset)
228 return pwrite(c.data(), c.size() *
sizeof(
typename Container::value_type), offset);
231 void ftruncate(off_t length);
233 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
252 bool ofd_setlkw(struct ::flock&,
bool retry_on_signal=
true);
261 operator int()
const {
return fd; }
271 std::string pathname;
283 [[noreturn]]
virtual void throw_error(
const char* desc);
287 const std::string&
name()
const {
return pathname; }
296 using NamedFileDescriptor::NamedFileDescriptor;
323 struct iterator :
public std::iterator<std::input_iterator_tag, struct dirent>
325 Path* path =
nullptr;
327 struct dirent* cur_entry =
nullptr;
335 : dir(o.dir), cur_entry(o.cur_entry)
338 o.cur_entry =
nullptr;
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; }
375 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
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);
391 Path& operator=(
const Path&) =
delete;
402 int openat(
const char* pathname,
int flags, mode_t mode=0777);
404 void fstatat(
const char* pathname,
struct stat& st);
410 void lstatat(
const char* pathname,
struct stat& st);
415 void unlinkat(
const char* pathname);
418 void rmdirat(
const char* pathname);
435 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
443 File(
const std::string& pathname);
446 File(
const std::string& pathname,
int flags, mode_t mode=0777);
448 File& operator=(
const File&) =
delete;
452 void open(
int flags, mode_t mode=0777);
460 static File mkstemp(
const std::string& prefix);
464 std::string read_file(
const std::string &file);
472 void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
480 void write_file(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
491 void write_file_atomically(
const std::string& file,
const std::string& data, mode_t mode=0777);
502 void write_file_atomically(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
506 std::string mkdtemp(std::string templ);
510 void mkFilePath(
const std::string& file);
518 bool unlink_ifexists(
const std::string& file);
525 bool rename_ifexists(
const std::string& src,
const std::string& dst);
535 bool mkdir_ifmissing(
const char* pathname, mode_t mode=0777);
537 bool mkdir_ifmissing(
const std::string& pathname, mode_t mode=0777);
545 bool makedirs(
const std::string& pathname, mode_t=0777);
554 std::string which(
const std::string& name);
557 void unlink(
const std::string& pathname);
560 void rmdir(
const std::string& pathname);
563 void rmtree(
const std::string& pathname);
571 void rename(
const std::string& src_pathname,
const std::string& dst_pathname);
589 struct dirent* direntbuf;
600 const_iterator(
const const_iterator& i);
601 const_iterator& operator=(
const const_iterator& i);
604 const_iterator& operator++();
607 std::string operator*()
const;
609 bool operator==(
const const_iterator& iter)
const;
610 bool operator!=(
const const_iterator& iter)
const;
623 const_iterator
begin()
const;
626 const_iterator
end()
const;
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.
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.
Common operations on file descriptors.
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.
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.
iterator end()
End iterator on all directory entries.
Iterator for directory entries.
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.
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.
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.
const std::string & name() const
Return the file pathname.