17#include <sys/resource.h>
32std::filesystem::path with_suffix(
const std::filesystem::path& path,
33 const std::string& suffix);
40std::unique_ptr<struct stat> stat(
const char* pathname);
47std::unique_ptr<struct stat> stat(
const std::string& pathname);
54std::unique_ptr<struct stat> stat(
const std::filesystem::path& path);
60void stat(
const char* pathname,
struct stat& st);
66void stat(
const std::string& pathname,
struct stat& st);
72void stat(
const std::filesystem::path& path,
struct stat& st);
79[[deprecated(
"Use std::filesystem::is_directory")]]
bool
80isdir(
const std::string& pathname);
83[[deprecated(
"Use std::filesystem::is_block_file")]]
bool
84isblk(
const std::string& pathname);
87[[deprecated(
"Use std::filesystem::is_character_file")]]
bool
88ischr(
const std::string& pathname);
91[[deprecated(
"Use std::filesystem::is_fifo")]]
bool
92isfifo(
const std::string& pathname);
95[[deprecated(
"Use std::filesystem::is_symlink")]]
bool
96islnk(
const std::string& pathname);
99[[deprecated(
"Use std::filesystem::is_regular_file")]]
bool
100isreg(
const std::string& pathname);
103[[deprecated(
"Use std::filesystem::is_socket")]]
bool
104issock(
const std::string& pathname);
107time_t timestamp(
const std::filesystem::path& file);
110time_t timestamp(
const std::filesystem::path& file, time_t def);
113size_t size(
const std::filesystem::path& file);
116size_t size(
const std::filesystem::path& file,
size_t def);
119ino_t inode(
const std::filesystem::path& file);
122ino_t inode(
const std::filesystem::path& file, ino_t def);
125bool access(
const std::filesystem::path& s,
int m);
128[[deprecated(
"Use std::filesystem::exists")]]
bool exists(
const std::string& s);
131[[deprecated(
"Use std::filesystem::current_path")]] std::string getcwd();
134[[deprecated(
"Use std::filesystem::current_path")]]
void
135chdir(
const std::string& dir);
138void chroot(
const std::filesystem::path& dir);
141mode_t umask(mode_t mask);
144[[deprecated(
"Use std::filesystem::canonical")]] std::string
145abspath(
const std::string& pathname);
158 MMap(
const MMap&) =
delete;
160 MMap(
void* addr,
size_t length);
163 MMap& operator=(
const MMap&) =
delete;
164 MMap& operator=(MMap&&);
166 size_t size()
const {
return length; }
170 template <
typename T>
operator const T*()
const
172 return reinterpret_cast<const T*
>(addr);
175 template <
typename T>
operator T*()
const
177 return reinterpret_cast<T*
>(addr);
199 FileDescriptor(FileDescriptor&& o);
200 explicit FileDescriptor(
int fd);
201 virtual ~FileDescriptor();
205 FileDescriptor(
const FileDescriptor& o) =
default;
206 FileDescriptor& operator=(
const FileDescriptor& o) =
default;
236 void fstat(
struct stat& st);
237 void fchmod(mode_t mode);
239 void futimens(const ::timespec ts[2]);
246 size_t read(
void* buf,
size_t count);
263 size_t write(
const void* buf,
size_t count);
265 template <
typename Container>
size_t write(
const Container& c)
267 return write(c.data(), c.size() *
sizeof(Container::value_type));
276 c.size() *
sizeof(
typename Container::value_type));
288 c.size() *
sizeof(
typename Container::value_type));
291 off_t lseek(off_t offset,
int whence = SEEK_SET);
293 size_t pread(
void* buf,
size_t count, off_t offset);
294 size_t pwrite(
const void* buf,
size_t count, off_t offset);
296 template <
typename Container>
297 size_t pwrite(
const Container& c, off_t offset)
299 return pwrite(c.data(),
300 c.size() *
sizeof(
typename Container::value_type),
304 void ftruncate(off_t length);
306 MMap mmap(
size_t length,
int prot,
int flags, off_t offset = 0);
340 operator int()
const {
return fd; }
346class PreserveFileTimes
354 ~PreserveFileTimes();
360class NamedFileDescriptor :
public FileDescriptor
363 std::filesystem::path path_;
366 NamedFileDescriptor(
int fd,
const std::filesystem::path& path);
367 NamedFileDescriptor(NamedFileDescriptor&&);
368 NamedFileDescriptor& operator=(NamedFileDescriptor&&);
372 NamedFileDescriptor(
const NamedFileDescriptor& o) =
default;
373 NamedFileDescriptor& operator=(
const NamedFileDescriptor& o) =
default;
379 [[deprecated(
"use path() instead")]] std::string
name()
const
381 return path_.string();
383 const std::filesystem::path& path()
const {
return path_; }
389struct ManagedNamedFileDescriptor :
public NamedFileDescriptor
391 using NamedFileDescriptor::NamedFileDescriptor;
393 ManagedNamedFileDescriptor(ManagedNamedFileDescriptor&&) =
default;
394 ManagedNamedFileDescriptor(
const ManagedNamedFileDescriptor&) =
delete;
405 ManagedNamedFileDescriptor&
406 operator=(
const ManagedNamedFileDescriptor&) =
delete;
407 ManagedNamedFileDescriptor& operator=(ManagedNamedFileDescriptor&&);
413struct Path :
public ManagedNamedFileDescriptor
420 using iterator_category = std::input_iterator_tag;
421 using value_type = ::dirent;
422 using difference_type = int;
423 using pointer = ::dirent*;
424 using reference = ::dirent&;
426 Path* path =
nullptr;
428 ::dirent* cur_entry =
nullptr;
433 explicit iterator(
Path& dir);
434 iterator(iterator&) =
delete;
435 iterator(iterator&& o) : dir(o.dir), cur_entry(o.cur_entry)
438 o.cur_entry =
nullptr;
441 iterator& operator=(iterator&) =
delete;
442 iterator& operator=(iterator&&) =
delete;
444 bool operator==(
const iterator& i)
const;
445 bool operator!=(
const iterator& i)
const;
446 ::dirent& operator*()
const {
return *cur_entry; }
447 ::dirent* operator->()
const {
return cur_entry; }
448 iterator& operator++();
475 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
480 explicit Path(
const std::filesystem::path& pathname,
int flags = 0,
485 Path(
Path& parent,
const char* pathname,
int flags = 0, mode_t mode = 0777);
488 Path& operator=(
const Path&) =
delete;
492 void open(
int flags, mode_t mode = 0777);
502 int openat(
const char* pathname,
int flags, mode_t mode = 0777);
507 bool faccessat(
const char* pathname,
int mode,
int flags = 0);
509 void fstatat(
const char* pathname,
struct stat& st);
515 void lstatat(
const char* pathname,
struct stat& st);
520 void unlinkat(
const char* pathname);
522 void mkdirat(
const char* pathname, mode_t mode = 0777);
527 void symlinkat(
const char* target,
const char* linkpath);
529 std::string readlinkat(
const char* pathname);
538 static std::string mkdtemp(
const std::filesystem::path& prefix);
540 "Use mkdtemp(const std::filesystem::path&)")]]
static std::string
541 mkdtemp(
const std::string& prefix);
542 static std::string mkdtemp(
const char* prefix);
543 static std::string mkdtemp(
char* pathname_template);
549class File :
public ManagedNamedFileDescriptor
552 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
554 File(File&&) =
default;
555 File(
const File&) =
delete;
560 explicit File(
const std::filesystem::path& path);
561 explicit File(
const char* path);
562 [[deprecated(
"Use File(const std::filesystem::path&)")]]
explicit File(
563 const std::string& path);
566 File(
const std::filesystem::path& path,
int flags, mode_t mode = 0777);
568 File& operator=(
const File&) =
delete;
569 File& operator=(File&&) =
default;
572 void open(
int flags, mode_t mode = 0777);
580 static File mkstemp(
const std::filesystem::path& prefix);
581 [[deprecated(
"Use mkstemp(const std::filesysten::path&)")]]
static File
582 mkstemp(
const std::string& prefix);
583 static File mkstemp(
const char* prefix);
584 static File mkstemp(
char* pathname_template);
592class Tempfile :
public File
595 bool m_unlink_on_exit =
true;
599 explicit Tempfile(
const std::filesystem::path& prefix);
600 [[deprecated(
"Use Tempfile(const std::string&)")]]
explicit Tempfile(
601 const std::string& prefix);
602 explicit Tempfile(
const char* prefix);
621 bool m_rmtree_on_exit =
true;
625 explicit Tempdir(
const std::filesystem::path& prefix);
628 "std::filesystem::path&)")]]
explicit Tempdir(
const std::string&
630 explicit Tempdir(
const char* prefix);
638std::string read_file(
const std::filesystem::path& file);
639[[deprecated(
"Use read_file(const std::filesystem::path&)")]] std::string
640read_file(
const std::string& file);
641std::string read_file(
const char* file);
649void write_file(
const std::filesystem::path& file,
const std::string& data,
651[[deprecated(
"Use write_file(const std::filesystem::path&, …)")]]
void
652write_file(
const std::string& file,
const std::string& data,
654void write_file(
const char* file,
const std::string& data, mode_t mode = 0777);
662void write_file(
const std::filesystem::path& file,
const void* data,
663 size_t size, mode_t mode = 0777);
664[[deprecated(
"Use write_file(const std::filesystem::path&, …)")]]
void
665write_file(
const std::string& file,
const void* data,
size_t size,
667void write_file(
const char* file,
const void* data,
size_t size,
679void write_file_atomically(
const std::filesystem::path& file,
680 const std::string& data, mode_t mode = 0777);
682 "Use write_file_atomically(const std::filesystem::path&, …)")]]
void
683write_file_atomically(
const std::string& file,
const std::string& data,
685void write_file_atomically(
const char* file,
const std::string& data,
697void write_file_atomically(
const std::filesystem::path& file,
const void* data,
698 size_t size, mode_t mode = 0777);
700 "Use write_file_atomically(const std::filesystem::path&, …)")]]
void
701write_file_atomically(
const std::string& file,
const void* data,
size_t size,
706std::string mkdtemp(std::string templ);
710void mkFilePath(
const std::string& file);
718[[deprecated(
"use std::filesystem::remove")]]
bool
719unlink_ifexists(
const char* file);
720[[deprecated(
"use std::filesystem::remove")]]
bool
721unlink_ifexists(
const std::string& file);
722[[deprecated(
"use std::filesystem::remove")]]
bool
723unlink_ifexists(
const std::filesystem::path& file);
730bool rename_ifexists(
const std::filesystem::path& src,
731 const std::filesystem::path& dst);
741[[deprecated(
"use std::filesystem::create_directory")]]
bool
742mkdir_ifmissing(
const std::filesystem::path& path);
750[[deprecated(
"use std::filesystem::create_directories")]]
bool
751makedirs(
const std::filesystem::path& path);
760std::filesystem::path which(
const std::string& name);
763void unlink(
const std::filesystem::path& pathname);
766void rmdir(
const std::filesystem::path& pathname);
769void rmtree(
const std::filesystem::path& pathname);
776bool rmtree_ifexists(
const std::filesystem::path& pathname);
777[[deprecated(
"use rmtree_ifexists(const std::filesystem::path&)")]]
bool
778rmtree_ifexists(
const std::string& pathname);
779bool rmtree_ifexists(
const char* pathname);
787[[deprecated(
"use std::filesystem::rename")]]
void
788rename(
const std::string& src_pathname,
const std::string& dst_pathname);
795void touch(
const std::filesystem::path& pathname, time_t ts);
802bool touch_ifexists(
const std::filesystem::path& pathname, time_t ts);
807void clock_gettime(::clockid_t clk_id, ::timespec& ts);
812unsigned long long timesec_elapsed(const ::timespec& begin,
813 const ::timespec& until);
840void getrlimit(
int resource, ::rlimit& rlim);
843void setrlimit(
int resource, const ::rlimit& rlim);
851 OverrideRlimit(
int resource, rlim_t rlim);
859struct OverrideEnvironment
862 bool was_set =
false;
863 std::string orig_value;
866 explicit OverrideEnvironment(
const std::string& name);
868 OverrideEnvironment(
const std::string& name,
const std::string& value);
869 ~OverrideEnvironment();
873template <
typename T =
char>
class TempBuffer
878 explicit TempBuffer(
size_t size) : buffer(
new T[size]) {}
879 ~TempBuffer() {
delete[] buffer; }
880 TempBuffer(
const TempBuffer&) =
delete;
881 TempBuffer(TempBuffer&&) =
delete;
882 TempBuffer& operator=(
const TempBuffer&) =
delete;
883 TempBuffer& operator=(TempBuffer&&) =
delete;
885 T* data() {
return buffer; }
886 const T* data()
const {
return buffer; }
887 operator T*() {
return buffer; }
888 operator const T*()
const {
return buffer; }
Common operations on file descriptors.
Definition sys.h:193
void write_all_or_retry(const void *buf, size_t count)
Write all the data in buf, retrying partial writes.
bool ofd_setlkw(::flock &, bool retry_on_signal=true)
Open file description locks F_OFD_SETLKW operation.
void setfl(int flags)
Set open flags for the file.
bool read_all_or_retry(void *buf, size_t count)
Read count bytes into bufr, retrying partial reads, stopping at EOF.
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.
bool ofd_setlk(::flock &)
Open file description locks F_OFD_SETLK operation.
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)
bool ofd_getlk(::flock &)
Open file description locks F_OFD_GETLK operation.
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::filesystem::path &path, 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.
File(const std::filesystem::path &path)
Create an unopened File object for the given pathname.
std::string name() const
Return the file pathname.
Definition sys.h:379
virtual void throw_error(const char *desc) override
Throw an exception based on errno and the given message.
virtual void throw_runtime_error(const char *desc) override
Throw a runtime_error unrelated from errno.
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:419
Path open_path(int flags=0) const
Return a Path object for this entry.
Path(const std::filesystem::path &pathname, int flags=0, mode_t mode=0777)
Open the given pathname with flags | O_PATH.
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
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.
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.