17#include <sys/resource.h>
30std::unique_ptr<struct stat> stat(
const std::string& pathname);
36void stat(
const std::string& pathname,
struct stat& st);
43bool isdir(
const std::string& pathname);
46bool isblk(
const std::string& pathname);
49bool ischr(
const std::string& pathname);
52bool isfifo(
const std::string& pathname);
55bool islnk(
const std::string& pathname);
58bool isreg(
const std::string& pathname);
61bool issock(
const std::string& pathname);
64time_t timestamp(
const std::string& file);
67time_t timestamp(
const std::string& file, time_t def);
70size_t size(
const std::string& file);
73size_t size(
const std::string& file,
size_t def);
76ino_t inode(
const std::string& file);
79ino_t inode(
const std::string& file, ino_t def);
82bool access(
const std::string& s,
int m);
85bool exists(
const std::string& s);
91void chdir(
const std::string& dir);
94void chroot(
const std::string& dir);
97mode_t umask(mode_t mask);
100std::string abspath(
const std::string& pathname);
113 MMap(
const MMap&) =
delete;
115 MMap(
void* addr,
size_t length);
118 MMap& operator=(
const MMap&) =
delete;
119 MMap& operator=(MMap&&);
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); }
150 FileDescriptor(FileDescriptor&& o);
151 FileDescriptor(
int fd);
152 virtual ~FileDescriptor();
156 FileDescriptor(
const FileDescriptor& o) =
default;
157 FileDescriptor& operator=(
const FileDescriptor& o) =
default;
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);
214 size_t write(
const void* buf,
size_t count);
216 template<
typename Container>
217 size_t write(
const Container& c)
219 return write(c.data(), c.size() *
sizeof(Container::value_type));
225 template<
typename Container>
237 template<
typename Container>
243 off_t lseek(off_t offset,
int whence=SEEK_SET);
245 size_t pread(
void* buf,
size_t count, off_t offset);
246 size_t pwrite(
const void* buf,
size_t count, off_t offset);
248 template<
typename Container>
249 size_t pwrite(
const Container& c, off_t offset)
251 return pwrite(c.data(), c.size() *
sizeof(
typename Container::value_type), offset);
254 void ftruncate(off_t length);
256 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
290 operator int()
const {
return fd; }
297class PreserveFileTimes
301 struct ::timespec ts[2];
305 ~PreserveFileTimes();
313class NamedFileDescriptor :
public FileDescriptor
316 std::string pathname;
319 NamedFileDescriptor(
int fd,
const std::string& pathname);
320 NamedFileDescriptor(NamedFileDescriptor&&);
321 NamedFileDescriptor& operator=(NamedFileDescriptor&&);
325 NamedFileDescriptor(
const NamedFileDescriptor& o) =
default;
326 NamedFileDescriptor& operator=(
const NamedFileDescriptor& o) =
default;
332 const std::string&
name()
const {
return pathname; }
339struct ManagedNamedFileDescriptor :
public NamedFileDescriptor
341 using NamedFileDescriptor::NamedFileDescriptor;
343 ManagedNamedFileDescriptor(ManagedNamedFileDescriptor&&) =
default;
344 ManagedNamedFileDescriptor(
const ManagedNamedFileDescriptor&) =
delete;
355 ManagedNamedFileDescriptor& operator=(
const ManagedNamedFileDescriptor&) =
delete;
356 ManagedNamedFileDescriptor& operator=(ManagedNamedFileDescriptor&&);
363struct Path :
public ManagedNamedFileDescriptor
370 using iterator_category = std::input_iterator_tag;
371 using value_type =
struct dirent;
372 using difference_type = int;
373 using pointer =
struct dirent*;
374 using reference =
struct dirent&;
376 Path* path =
nullptr;
378 struct dirent* cur_entry =
nullptr;
384 iterator(iterator&) =
delete;
385 iterator(iterator&& o)
386 : dir(o.dir), cur_entry(o.cur_entry)
389 o.cur_entry =
nullptr;
392 iterator& operator=(iterator&) =
delete;
393 iterator& operator=(iterator&&) =
delete;
395 bool operator==(
const iterator& i)
const;
396 bool operator!=(
const iterator& i)
const;
397 struct dirent& operator*()
const {
return *cur_entry; }
398 struct dirent* operator->()
const {
return cur_entry; }
426 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
431 Path(
const char* pathname,
int flags=0, mode_t mode=0777);
435 Path(
const std::string& pathname,
int flags=0, mode_t mode=0777);
439 Path(
Path& parent,
const char* pathname,
int flags=0, mode_t mode=0777);
442 Path& operator=(
const Path&) =
delete;
446 void open(
int flags, mode_t mode=0777);
456 int openat(
const char* pathname,
int flags, mode_t mode=0777);
461 bool faccessat(
const char* pathname,
int mode,
int flags=0);
463 void fstatat(
const char* pathname,
struct stat& st);
469 void lstatat(
const char* pathname,
struct stat& st);
474 void unlinkat(
const char* pathname);
476 void mkdirat(
const char* pathname, mode_t mode=0777);
481 void symlinkat(
const char* target,
const char* linkpath);
483 std::string readlinkat(
const char* pathname);
492 static std::string mkdtemp(
const std::string& prefix);
493 static std::string mkdtemp(
const char* prefix);
494 static std::string mkdtemp(
char* pathname_template);
501class File :
public ManagedNamedFileDescriptor
504 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
506 File(File&&) =
default;
507 File(
const File&) =
delete;
512 File(
const std::string& pathname);
515 File(
const std::string& pathname,
int flags, mode_t mode=0777);
517 File& operator=(
const File&) =
delete;
518 File& operator=(File&&) =
default;
521 void open(
int flags, mode_t mode=0777);
529 static File mkstemp(
const std::string& prefix);
530 static File mkstemp(
const char* prefix);
531 static File mkstemp(
char* pathname_template);
540class Tempfile :
public File
543 bool m_unlink_on_exit =
true;
547 Tempfile(
const std::string& prefix);
548 Tempfile(
const char* prefix);
568 bool m_rmtree_on_exit =
true;
572 Tempdir(
const std::string& prefix);
573 Tempdir(
const char* prefix);
582std::string read_file(
const std::string &file);
590void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
598void write_file(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
609void write_file_atomically(
const std::string& file,
const std::string& data, mode_t mode=0777);
620void write_file_atomically(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
624std::string mkdtemp(std::string templ);
628void mkFilePath(
const std::string& file);
636bool unlink_ifexists(
const std::string& file);
643bool rename_ifexists(
const std::string& src,
const std::string& dst);
653bool mkdir_ifmissing(
const char* pathname, mode_t mode=0777);
655bool mkdir_ifmissing(
const std::string& pathname, mode_t mode=0777);
663bool makedirs(
const std::string& pathname, mode_t=0777);
672std::string which(
const std::string& name);
675void unlink(
const std::string& pathname);
678void rmdir(
const std::string& pathname);
681void rmtree(
const std::string& pathname);
688bool rmtree_ifexists(
const std::string& pathname);
696void rename(
const std::string& src_pathname,
const std::string& dst_pathname);
701void touch(
const std::string& pathname, time_t ts);
706void clock_gettime(::clockid_t clk_id, struct ::timespec& ts);
711unsigned long long timesec_elapsed(
const struct ::timespec& begin,
const struct ::timespec& until);
719 struct ::timespec ts;
738void getrlimit(
int resource, struct ::rlimit& rlim);
741void setrlimit(
int resource,
const struct ::rlimit& rlim);
747 struct ::rlimit orig;
749 OverrideRlimit(
int resource, rlim_t rlim);
Common operations on file descriptors.
Definition sys.h:144
void write_all_or_retry(const void *buf, size_t count)
Write all the data in buf, retrying partial writes.
void setfl(int flags)
Set open flags for the file.
bool ofd_setlk(struct ::flock &)
Open file description locks F_OFD_SETLK operation.
bool read_all_or_retry(void *buf, size_t count)
Read count bytes into bufr, retrying partial reads, stopping at EOF.
bool ofd_setlkw(struct ::flock &, bool retry_on_signal=true)
Open file description locks F_OFD_SETLKW operation.
bool ofd_getlk(struct ::flock &)
Open file description locks F_OFD_GETLK 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.
void close()
Close the file descriptor, setting its value to -1.
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.
int getfl()
Get open flags for the file.
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.
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).
File(const std::string &pathname, int flags, mode_t mode=0777)
Wrapper around open(2).
File(const std::string &pathname)
Create an unopened File object for the given pathname.
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:332
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.
void rmtree_on_exit(bool val)
Change the rmtree-on-exit behaviour.
void unlink_on_exit(bool val)
Change the unlink-on-exit behaviour.
void unlink()
Unlink the file right now.
String functions.
Definition benchmark.h:13
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().
void set(rlim_t rlim)
Change the limit value again.
Iterator for directory entries.
Definition sys.h:369
Path open_path(int flags=0) const
Return a Path object for this entry.
bool lstatat_ifexists(const char *pathname, struct stat &st)
lstatat, but in case of ENOENT returns false instead of throwing
Path(const std::string &pathname, int flags=0, mode_t mode=0777)
Open the given pathname with flags | O_PATH.
void lstatat(const char *pathname, struct stat &st)
fstatat with the AT_SYMLINK_NOFOLLOW flag set
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 open(int flags, mode_t mode=0777)
Wrapper around open(2) with flags | O_PATH.
iterator begin()
Begin iterator on all directory entries.
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
void rmtree()
Delete the directory pointed to by this Path, with all its contents.
Path(const char *pathname, int flags=0, mode_t mode=0777)
Open the given pathname with flags | O_PATH.
iterator end()
End iterator on all directory entries.
Path(Path &parent, const char *pathname, int flags=0, mode_t mode=0777)
Open the given pathname calling parent.openat, with flags | O_PATH.