1#ifndef RADARELAB_UTILS_SYS_H
2#define RADARELAB_UTILS_SYS_H
17#include <sys/resource.h>
31std::unique_ptr<struct stat> stat(
const std::string& pathname);
37void stat(
const std::string& pathname,
struct stat& st);
44bool isdir(
const std::string& pathname);
47bool isblk(
const std::string& pathname);
50bool ischr(
const std::string& pathname);
53bool isfifo(
const std::string& pathname);
56bool islnk(
const std::string& pathname);
59bool isreg(
const std::string& pathname);
62bool issock(
const std::string& pathname);
65time_t timestamp(
const std::string& file);
68time_t timestamp(
const std::string& file, time_t def);
71size_t size(
const std::string& file);
74size_t size(
const std::string& file,
size_t def);
77ino_t inode(
const std::string& file);
80ino_t inode(
const std::string& file, ino_t def);
83bool access(
const std::string& s,
int m);
86bool exists(
const std::string& s);
92void chdir(
const std::string& dir);
95void chroot(
const std::string& dir);
98mode_t umask(mode_t mask);
101std::string abspath(
const std::string& pathname);
116 MMap(
void* addr,
size_t length);
119 MMap& operator=(
const MMap&) =
delete;
122 size_t size()
const {
return length; }
127 operator const T*()
const {
return reinterpret_cast<const T*
>(addr); }
130 operator T*()
const {
return reinterpret_cast<T*
>(addr); }
188 void fstat(
struct stat& st);
189 void fchmod(mode_t mode);
191 void futimens(
const struct ::timespec ts[2]);
198 size_t read(
void* buf,
size_t count);
215 size_t write(
const void* buf,
size_t count);
217 template<
typename Container>
218 size_t write(
const Container& c)
220 return write(c.data(), c.size() *
sizeof(Container::value_type));
226 template<
typename Container>
238 template<
typename Container>
244 off_t lseek(off_t offset,
int whence=SEEK_SET);
246 size_t pread(
void* buf,
size_t count, off_t offset);
247 size_t pwrite(
const void* buf,
size_t count, off_t offset);
249 template<
typename Container>
250 size_t pwrite(
const Container& c, off_t offset)
252 return pwrite(c.data(), c.size() *
sizeof(
typename Container::value_type), offset);
255 void ftruncate(off_t length);
257 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
291 operator int()
const {
return fd; }
302 struct ::timespec ts[2];
317 std::string pathname;
333 const std::string&
name()
const {
return pathname; }
342 using NamedFileDescriptor::NamedFileDescriptor;
371 using iterator_category = std::input_iterator_tag;
372 using value_type =
struct dirent;
373 using difference_type = int;
374 using pointer =
struct dirent*;
375 using reference =
struct dirent&;
377 Path* path =
nullptr;
379 struct dirent* cur_entry =
nullptr;
387 : dir(o.dir), cur_entry(o.cur_entry)
390 o.cur_entry =
nullptr;
396 bool operator==(
const iterator& i)
const;
397 bool operator!=(
const iterator& i)
const;
398 struct dirent& operator*()
const {
return *cur_entry; }
399 struct dirent* operator->()
const {
return cur_entry; }
427 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
432 Path(
const char* pathname,
int flags=0, mode_t mode=0777);
436 Path(
const std::string& pathname,
int flags=0, mode_t mode=0777);
440 Path(
Path& parent,
const char* pathname,
int flags=0, mode_t mode=0777);
443 Path& operator=(
const Path&) =
delete;
447 void open(
int flags, mode_t mode=0777);
457 int openat(
const char* pathname,
int flags, mode_t mode=0777);
462 bool faccessat(
const char* pathname,
int mode,
int flags=0);
464 void fstatat(
const char* pathname,
struct stat& st);
470 void lstatat(
const char* pathname,
struct stat& st);
475 void unlinkat(
const char* pathname);
477 void mkdirat(
const char* pathname, mode_t mode=0777);
482 void symlinkat(
const char* target,
const char* linkpath);
484 std::string readlinkat(
const char* pathname);
493 static std::string mkdtemp(
const std::string& prefix);
494 static std::string mkdtemp(
const char* prefix);
495 static std::string mkdtemp(
char* pathname_template);
505 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
513 File(
const std::string& pathname);
516 File(
const std::string& pathname,
int flags, mode_t mode=0777);
518 File& operator=(
const File&) =
delete;
522 void open(
int flags, mode_t mode=0777);
530 static File mkstemp(
const std::string& prefix);
531 static File mkstemp(
const char* prefix);
532 static File mkstemp(
char* pathname_template);
544 bool m_unlink_on_exit =
true;
548 Tempfile(
const std::string& prefix);
569 bool m_rmtree_on_exit =
true;
573 Tempdir(
const std::string& prefix);
583std::string read_file(
const std::string &file);
591void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
599void write_file(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
610void write_file_atomically(
const std::string& file,
const std::string& data, mode_t mode=0777);
621void write_file_atomically(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
625std::string mkdtemp(std::string templ);
629void mkFilePath(
const std::string& file);
637bool unlink_ifexists(
const std::string& file);
644bool rename_ifexists(
const std::string& src,
const std::string& dst);
654bool mkdir_ifmissing(
const char* pathname, mode_t mode=0777);
656bool mkdir_ifmissing(
const std::string& pathname, mode_t mode=0777);
664bool makedirs(
const std::string& pathname, mode_t=0777);
673std::string which(
const std::string& name);
676void unlink(
const std::string& pathname);
679void rmdir(
const std::string& pathname);
682void rmtree(
const std::string& pathname);
689bool rmtree_ifexists(
const std::string& pathname);
697void rename(
const std::string& src_pathname,
const std::string& dst_pathname);
702void touch(
const std::string& pathname, time_t ts);
707void clock_gettime(::clockid_t clk_id, struct ::timespec& ts);
712unsigned long long timesec_elapsed(
const struct ::timespec& begin,
const struct ::timespec& until);
720 struct ::timespec ts;
739void getrlimit(
int resource, struct ::rlimit& rlim);
742void setrlimit(
int resource,
const struct ::rlimit& rlim);
748 struct ::rlimit orig;
void close()
Close the file descriptor, setting its value to -1.
bool ofd_setlk(struct ::flock &)
Open file description locks F_OFD_SETLK operation.
bool ofd_setlkw(struct ::flock &, bool retry_on_signal=true)
Open file description locks F_OFD_SETLKW operation.
void write_all_or_retry(const void *buf, size_t count)
Write all the data in buf, retrying partial writes.
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.
void setfl(int flags)
Set open flags for the file.
int getfl()
Get open flags for the file.
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
bool is_open() const
Check if the file descriptor is open (that is, if it is not -1)
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 read_all_or_retry(void *buf, size_t count)
Read count bytes into bufr, retrying partial reads, stopping at EOF.
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.
bool ofd_getlk(struct ::flock &)
Open file description locks F_OFD_GETLK operation.
Common operations on file descriptors.
File(const std::string &pathname)
Create an unopened File object for the given pathname.
File(const std::string &pathname, int flags, mode_t mode=0777)
Wrapper around open(2)
void open(int flags, mode_t mode=0777)
Wrapper around open(2)
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.
Wraps a mmapped memory area, unmapping it on destruction.
const std::string & name() const
Return the file pathname.
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.
File descriptor with a name.
RAII mechanism to save restore file times at the end of some file operations.
void rmtree_on_exit(bool val)
Change the rmtree-on-exit behaviour.
Open a temporary directory.
void unlink_on_exit(bool val)
Change the unlink-on-exit behaviour.
void unlink()
Unlink the file right now.
Clock(::clockid_t clk_id)
Initialize ts with the value of the given clock.
unsigned long long elapsed()
Return the number of nanoseconds elapsed since the last time ts was updated.
~ManagedNamedFileDescriptor()
The destructor closes the file descriptor, but does not check errors on close().
File descriptor that gets automatically closed in the object destructor.
void set(rlim_t rlim)
Change the limit value again.
Override a soft resource limit during the lifetime of the object.
Path open_path(int flags=0) const
Return a Path object for this entry.
Iterator for directory entries.
Path(const char *pathname, int flags=0, mode_t mode=0777)
Open the given pathname with flags | O_PATH.
int openat_ifexists(const char *pathname, int flags, mode_t mode=0777)
Same as openat, but returns -1 if the file does not exist.
void rmtree()
Delete the directory pointed to by this Path, with all its contents.
bool fstatat_ifexists(const char *pathname, struct stat &st)
fstatat, but in case of ENOENT returns false instead of throwing
void rmdirat(const char *pathname)
unlinkat with the AT_REMOVEDIR flag set
iterator end()
End iterator on all directory entries.
bool lstatat_ifexists(const char *pathname, struct stat &st)
lstatat, but in case of ENOENT returns false instead of throwing
void lstatat(const char *pathname, struct stat &st)
fstatat with the AT_SYMLINK_NOFOLLOW flag set
iterator begin()
Begin iterator on all directory entries.
void open(int flags, mode_t mode=0777)
Wrapper around open(2) with flags | O_PATH.
Path(const std::string &pathname, int flags=0, mode_t mode=0777)
Open the given pathname with flags | O_PATH.
Path(Path &parent, const char *pathname, int flags=0, mode_t mode=0777)
Open the given pathname calling parent.openat, with flags | O_PATH.
Wrap a path on the file system opened with O_PATH.