14 #include <sys/types.h>
17 #include <sys/resource.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 void chdir(
const std::string& dir);
94 void chroot(
const std::string& dir);
97 mode_t umask(mode_t mask);
100 std::string abspath(
const std::string& pathname);
115 MMap(
void* addr,
size_t length);
118 MMap& operator=(
const MMap&) =
delete;
121 size_t size()
const {
return length; }
126 operator const T*()
const {
return reinterpret_cast<const T*>(addr); }
129 operator T*()
const {
return reinterpret_cast<T*>(addr); };
166 [[noreturn]]
virtual void throw_error(
const char* desc);
187 void fstat(
struct stat& st);
188 void fchmod(mode_t mode);
190 void futimens(
const struct ::timespec ts[2]);
197 size_t read(
void* buf,
size_t count);
205 size_t write(
const void* buf,
size_t count);
207 template<
typename Container>
208 size_t write(
const Container& c)
210 return write(c.data(), c.size() *
sizeof(Container::value_type));
216 template<
typename Container>
228 template<
typename Container>
234 off_t lseek(off_t offset,
int whence=SEEK_SET);
236 size_t pread(
void* buf,
size_t count, off_t offset);
237 size_t pwrite(
const void* buf,
size_t count, off_t offset);
239 template<
typename Container>
240 size_t pwrite(
const Container& c, off_t offset)
242 return pwrite(c.data(), c.size() *
sizeof(
typename Container::value_type), offset);
245 void ftruncate(off_t length);
247 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
266 bool ofd_setlkw(struct ::flock&,
bool retry_on_signal=
true);
283 operator int()
const {
return fd; }
294 struct ::timespec ts[2];
309 std::string pathname;
321 [[noreturn]]
virtual void throw_error(
const char* desc);
325 const std::string&
name()
const {
return pathname; }
334 using NamedFileDescriptor::NamedFileDescriptor;
361 struct iterator :
public std::iterator<std::input_iterator_tag, struct dirent>
363 Path* path =
nullptr;
365 struct dirent* cur_entry =
nullptr;
373 : dir(o.dir), cur_entry(o.cur_entry)
376 o.cur_entry =
nullptr;
382 bool operator==(
const iterator& i)
const;
383 bool operator!=(
const iterator& i)
const;
384 struct dirent& operator*()
const {
return *cur_entry; }
385 struct dirent* operator->()
const {
return cur_entry; }
413 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
418 Path(
const char* pathname,
int flags=0);
422 Path(
const std::string& pathname,
int flags=0);
426 Path(
Path& parent,
const char* pathname,
int flags=0);
429 Path& operator=(
const Path&) =
delete;
440 int openat(
const char* pathname,
int flags, mode_t mode=0777);
443 int openat_ifexists(
const char* pathname,
int flags, mode_t mode=0777);
445 bool faccessat(
const char* pathname,
int mode,
int flags=0);
447 void fstatat(
const char* pathname,
struct stat& st);
453 void lstatat(
const char* pathname,
struct stat& st);
458 void unlinkat(
const char* pathname);
461 void rmdirat(
const char* pathname);
478 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
486 File(
const std::string& pathname);
489 File(
const std::string& pathname,
int flags, mode_t mode=0777);
491 File& operator=(
const File&) =
delete;
495 void open(
int flags, mode_t mode=0777);
503 static File mkstemp(
const std::string& prefix);
504 static File mkstemp(
const char* prefix);
505 static File mkstemp(
char* pathname_template);
517 bool m_unlink_on_exit =
true;
521 Tempfile(
const std::string& prefix);
534 std::string read_file(
const std::string &file);
542 void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
550 void write_file(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
561 void write_file_atomically(
const std::string& file,
const std::string& data, mode_t mode=0777);
572 void write_file_atomically(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
576 std::string mkdtemp(std::string templ);
580 void mkFilePath(
const std::string& file);
588 bool unlink_ifexists(
const std::string& file);
595 bool rename_ifexists(
const std::string& src,
const std::string& dst);
605 bool mkdir_ifmissing(
const char* pathname, mode_t mode=0777);
607 bool mkdir_ifmissing(
const std::string& pathname, mode_t mode=0777);
615 bool makedirs(
const std::string& pathname, mode_t=0777);
624 std::string which(
const std::string& name);
627 void unlink(
const std::string& pathname);
630 void rmdir(
const std::string& pathname);
633 void rmtree(
const std::string& pathname);
640 bool rmtree_ifexists(
const std::string& pathname);
648 void rename(
const std::string& src_pathname,
const std::string& dst_pathname);
653 void touch(
const std::string& pathname, time_t ts);
658 void clock_gettime(::clockid_t clk_id, struct ::timespec& ts);
663 unsigned long long timesec_elapsed(
const struct ::timespec& begin,
const struct ::timespec& until);
671 struct ::timespec ts;
676 Clock(::clockid_t clk_id);
689 void getrlimit(
int resource, struct ::rlimit& rlim);
693 void setrlimit(
int resource,
const struct ::rlimit& rlim);
699 struct ::rlimit orig;
705 void set(rlim_t rlim);
void set(rlim_t rlim)
Change the limit value again.
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
unsigned long long elapsed()
Return the number of nanoseconds elapsed since the last time ts was updated.
bool ofd_getlk(struct ::flock &)
Open file description locks F_OFD_GETLK operation.
const std::string & name() const
Return the file pathname.
Definition: sys.h:325
void rmdirat(const char *pathname)
unlinkat with the AT_REMOVEDIR flag set
iterator begin()
Begin iterator on all directory entries.
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.
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:356
void sendfile(FileDescriptor &out_fd, off_t offset, size_t count)
Call sendfile with this file as in_fd, falling back on write if it is not available.
void lstatat(const char *pathname, struct stat &st)
fstatat with the AT_SYMLINK_NOFOLLOW flag set
void open(int flags, mode_t mode=0777)
Wrapper around open(2)
void write_all_or_retry(const void *buf, size_t count)
Write all the data in buf, retrying partial writes.
Path open_path(int flags=0) const
Return a Path object for this entry.
File descriptor that gets automatically closed in the object destructor.
Definition: sys.h:332
Override a soft resource limit during the lifetime of the object.
Definition: sys.h:696
~ManagedNamedFileDescriptor()
The destructor closes the file descriptor, but does not check errors on ::close().
Open a temporary file.
Definition: sys.h:514
Path(const char *pathname, int flags=0)
Open the given pathname with flags | O_PATH.
Iterator for directory entries.
Definition: sys.h:361
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.
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.
void rmtree()
Delete the directory pointed to by this Path, with all its contents.
iterator end()
End iterator on all directory entries.
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:107
void unlink_on_exit(bool val)
Change the unlink-on-exit behaviour.
RAII mechanism to save restore file times at the end of some file operations.
Definition: sys.h:290
File descriptor with a name.
Definition: sys.h:306
void unlink()
Unlink the file right now.
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
Common operations on file descriptors.
Definition: sys.h:143
bool ofd_setlk(struct ::flock &)
Open file description locks F_OFD_SETLK operation.
int openat_ifexists(const char *pathname, int flags, mode_t mode=0777)
Same as openat, but returns -1 if the file does not exist.
bool ofd_setlkw(struct ::flock &, bool retry_on_signal=true)
Open file description locks F_OFD_SETLKW operation.
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 lstatat_ifexists(const char *pathname, struct stat &st)
lstatat, but in case of ENOENT returns false instead of throwing
void close()
Close the file descriptor, setting its value to -1.
File in the file system.
Definition: sys.h:475
bool is_open() const
Check if the file descriptor is open (that is, if it is not -1)
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.
String functions.
Definition: benchmark.h:13
bool fstatat_ifexists(const char *pathname, struct stat &st)
fstatat, but in case of ENOENT returns false instead of throwing
Clock(::clockid_t clk_id)
Initialize ts with the value of the given clock.
Access to clock_gettime.
Definition: sys.h:668