17#include <sys/resource.h>
31std::filesystem::path
with_suffix(
const std::filesystem::path& path,
32 const std::string& suffix);
39std::unique_ptr<struct stat>
stat(
const char* pathname);
46std::unique_ptr<struct stat>
stat(
const std::string& pathname);
53std::unique_ptr<struct stat>
stat(
const std::filesystem::path& path);
59void stat(
const char* pathname,
struct stat& st);
65void stat(
const std::string& pathname,
struct stat& st);
71void stat(
const std::filesystem::path& path,
struct stat& st);
78[[deprecated(
"Use std::filesystem::is_directory")]]
bool
79isdir(
const std::string& pathname);
82[[deprecated(
"Use std::filesystem::is_block_file")]]
bool
83isblk(
const std::string& pathname);
86[[deprecated(
"Use std::filesystem::is_character_file")]]
bool
87ischr(
const std::string& pathname);
90[[deprecated(
"Use std::filesystem::is_fifo")]]
bool
94[[deprecated(
"Use std::filesystem::is_symlink")]]
bool
95islnk(
const std::string& pathname);
98[[deprecated(
"Use std::filesystem::is_regular_file")]]
bool
99isreg(
const std::string& pathname);
102[[deprecated(
"Use std::filesystem::is_socket")]]
bool
109time_t
timestamp(
const std::filesystem::path& file, time_t def);
112size_t size(
const std::filesystem::path& file);
115size_t size(
const std::filesystem::path& file,
size_t def);
118ino_t
inode(
const std::filesystem::path& file);
121ino_t
inode(
const std::filesystem::path& file, ino_t def);
124bool access(
const std::filesystem::path& s,
int m);
127[[deprecated(
"Use std::filesystem::exists")]]
bool exists(
const std::string& s);
130[[deprecated(
"Use std::filesystem::current_path")]] std::string
getcwd();
133[[deprecated(
"Use std::filesystem::current_path")]]
void
137void chroot(
const std::filesystem::path& dir);
143std::filesystem::path
abspath(
const std::filesystem::path& path);
144[[deprecated(
"Use abspath(const std::filesystem::path&)")]] std::string
145abspath(
const std::string& pathname);
146std::filesystem::path
abspath(
const char* path);
159 MMap(
const MMap&) =
delete;
161 MMap(
void* addr,
size_t length);
164 MMap& operator=(
const MMap&) =
delete;
165 MMap& operator=(MMap&&);
167 size_t size()
const {
return length; }
171 template <
typename T>
operator const T*()
const
173 return reinterpret_cast<const T*
>(addr);
176 template <
typename T>
operator T*()
const
178 return reinterpret_cast<T*
>(addr);
200 FileDescriptor(FileDescriptor&& o);
201 explicit FileDescriptor(
int fd);
202 virtual ~FileDescriptor();
206 FileDescriptor(
const FileDescriptor& o) =
default;
207 FileDescriptor& operator=(
const FileDescriptor& o) =
default;
246 void fstat(
struct stat& st);
247 void fchmod(mode_t mode);
249 void futimens(const ::timespec ts[2]);
256 size_t read(
void* buf,
size_t count);
273 size_t write(
const void* buf,
size_t count);
275 template <
typename Container>
size_t write(
const Container& c)
277 return write(c.data(),
278 c.size() *
sizeof(
typename Container::value_type));
287 c.size() *
sizeof(
typename Container::value_type));
299 c.size() *
sizeof(
typename Container::value_type));
302 off_t lseek(off_t offset,
int whence = SEEK_SET);
304 size_t pread(
void* buf,
size_t count, off_t offset);
305 size_t pwrite(
const void* buf,
size_t count, off_t offset);
307 template <
typename Container>
308 size_t pwrite(
const Container& c, off_t offset)
310 return pwrite(c.data(),
311 c.size() *
sizeof(
typename Container::value_type),
315 void ftruncate(off_t length);
317 MMap mmap(
size_t length,
int prot,
int flags, off_t offset = 0);
351 operator int()
const {
return fd; }
357class PreserveFileTimes
365 ~PreserveFileTimes();
371class NamedFileDescriptor :
public FileDescriptor
374 std::filesystem::path path_;
377 NamedFileDescriptor(
int fd,
const std::filesystem::path& path);
378 NamedFileDescriptor(NamedFileDescriptor&&);
379 NamedFileDescriptor& operator=(NamedFileDescriptor&&);
383 NamedFileDescriptor(
const NamedFileDescriptor& o) =
default;
384 NamedFileDescriptor& operator=(
const NamedFileDescriptor& o) =
default;
387 [[noreturn]]
virtual void
392 [[deprecated(
"use path() instead")]] std::string
name()
const
394 return path_.string();
396 const std::filesystem::path& path()
const {
return path_; }
402struct ManagedNamedFileDescriptor :
public NamedFileDescriptor
404 using NamedFileDescriptor::NamedFileDescriptor;
406 ManagedNamedFileDescriptor(ManagedNamedFileDescriptor&&) =
default;
407 ManagedNamedFileDescriptor(
const ManagedNamedFileDescriptor&) =
delete;
418 ManagedNamedFileDescriptor&
419 operator=(
const ManagedNamedFileDescriptor&) =
delete;
420 ManagedNamedFileDescriptor& operator=(ManagedNamedFileDescriptor&&);
426struct Path :
public ManagedNamedFileDescriptor
433 using iterator_category = std::input_iterator_tag;
434 using value_type = ::dirent;
435 using difference_type = int;
436 using pointer = ::dirent*;
437 using reference = ::dirent&;
439 Path* path =
nullptr;
441 ::dirent* cur_entry =
nullptr;
446 explicit iterator(
Path& dir);
447 iterator(iterator&) =
delete;
448 iterator(iterator&& o) : dir(o.dir), cur_entry(o.cur_entry)
451 o.cur_entry =
nullptr;
454 iterator& operator=(iterator&) =
delete;
455 iterator& operator=(iterator&&) =
delete;
457 bool operator==(
const iterator& i)
const;
458 bool operator!=(
const iterator& i)
const;
459 ::dirent& operator*()
const {
return *cur_entry; }
460 ::dirent* operator->()
const {
return cur_entry; }
461 iterator& operator++();
488 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
493 explicit Path(
const std::filesystem::path& pathname,
int flags = 0,
498 Path(
Path& parent,
const char* pathname,
int flags = 0, mode_t mode = 0777);
501 Path& operator=(
const Path&) =
delete;
505 void open(
int flags, mode_t mode = 0777);
515 int openat(
const char* pathname,
int flags, mode_t mode = 0777);
520 bool faccessat(
const char* pathname,
int mode,
int flags = 0);
522 void fstatat(
const char* pathname,
struct stat& st);
533 void unlinkat(
const char* pathname);
535 void mkdirat(
const char* pathname, mode_t mode = 0777);
540 void symlinkat(
const char* target,
const char* linkpath);
542 std::string readlinkat(
const char* pathname);
551 static std::string mkdtemp(
const std::filesystem::path& prefix);
553 "Use mkdtemp(const std::filesystem::path&)")]]
static std::string
554 mkdtemp(
const std::string& prefix);
555 static std::string mkdtemp(
const char* prefix);
556 static std::string mkdtemp(
char* pathname_template);
562class File :
public ManagedNamedFileDescriptor
565 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
567 File(File&&) =
default;
568 File(
const File&) =
delete;
573 explicit File(
const std::filesystem::path& path);
574 explicit File(
const char* path);
575 [[deprecated(
"Use File(const std::filesystem::path&)")]]
explicit File(
576 const std::string& path);
579 File(
const std::filesystem::path& path,
int flags, mode_t mode = 0777);
581 File& operator=(
const File&) =
delete;
582 File& operator=(File&&) =
default;
585 void open(
int flags, mode_t mode = 0777);
593 static File mkstemp(
const std::filesystem::path& prefix);
594 [[deprecated(
"Use mkstemp(const std::filesysten::path&)")]]
static File
595 mkstemp(
const std::string& prefix);
596 static File mkstemp(
const char* prefix);
597 static File mkstemp(
char* pathname_template);
605class Tempfile :
public File
608 bool m_unlink_on_exit =
true;
612 explicit Tempfile(
const std::filesystem::path& prefix);
613 [[deprecated(
"Use Tempfile(const std::string&)")]]
explicit Tempfile(
614 const std::string& prefix);
615 explicit Tempfile(
const char* prefix);
634 bool m_rmtree_on_exit =
true;
638 explicit Tempdir(
const std::filesystem::path& prefix);
641 "std::filesystem::path&)")]]
explicit Tempdir(
const std::string&
643 explicit Tempdir(
const char* prefix);
651std::string
read_file(
const std::filesystem::path& file);
652[[deprecated(
"Use read_file(const std::filesystem::path&)")]] std::string
662void write_file(
const std::filesystem::path& file,
const std::string& data,
664[[deprecated(
"Use write_file(const std::filesystem::path&, …)")]]
void
665write_file(
const std::string& file,
const std::string& data,
667void write_file(
const char* file,
const std::string& data, mode_t mode = 0777);
675void write_file(
const std::filesystem::path& file,
const void* data,
676 size_t size, mode_t mode = 0777);
677[[deprecated(
"Use write_file(const std::filesystem::path&, …)")]]
void
693 const std::string& data, mode_t mode = 0777);
695 "Use write_file_atomically(const std::filesystem::path&, …)")]]
void
711 size_t size, mode_t mode = 0777);
713 "Use write_file_atomically(const std::filesystem::path&, …)")]]
void
719std::string mkdtemp(std::string templ);
723void mkFilePath(
const std::string& file);
731[[deprecated(
"use std::filesystem::remove")]]
bool
733[[deprecated(
"use std::filesystem::remove")]]
bool
735[[deprecated(
"use std::filesystem::remove")]]
bool
744 const std::filesystem::path& dst);
754[[deprecated(
"use std::filesystem::create_directory")]]
bool
763[[deprecated(
"use std::filesystem::create_directories")]]
bool
773std::filesystem::path
which(
const std::string& name);
776void unlink(
const std::filesystem::path& pathname);
779void rmdir(
const std::filesystem::path& pathname);
782void rmtree(
const std::filesystem::path& pathname);
790[[deprecated(
"use rmtree_ifexists(const std::filesystem::path&)")]]
bool
800[[deprecated(
"use std::filesystem::rename")]]
void
801rename(
const std::string& src_pathname,
const std::string& dst_pathname);
808void touch(
const std::filesystem::path& pathname, time_t ts);
826 const ::timespec& until);
864 OverrideRlimit(
int resource, rlim_t rlim);
872struct OverrideEnvironment
875 bool was_set =
false;
876 std::string orig_value;
879 explicit OverrideEnvironment(
const std::string& name);
881 OverrideEnvironment(
const std::string& name,
const std::string& value);
882 ~OverrideEnvironment();
886template <
typename T =
char>
class TempBuffer
891 explicit TempBuffer(
size_t size) : buffer(
new T[
size]) {}
892 ~TempBuffer() {
delete[] buffer; }
893 TempBuffer(
const TempBuffer&) =
delete;
894 TempBuffer(TempBuffer&&) =
delete;
895 TempBuffer& operator=(
const TempBuffer&) =
delete;
896 TempBuffer& operator=(TempBuffer&&) =
delete;
898 T* data() {
return buffer; }
899 const T* data()
const {
return buffer; }
900 operator T*() {
return buffer; }
901 operator const T*()
const {
return buffer; }
Common operations on file descriptors.
Definition sys.h:194
virtual void throw_error_string(const std::string &desc)
Throw an exception based on errno and the given message.
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:392
virtual void throw_error_string(const std::string &desc) override
Throw an exception based on errno and the given message.
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.
Operating system functions.
Definition sys.h:23
std::filesystem::path which(const std::string &name)
Compute the absolute path of an executable.
bool isdir(const std::string &pathname)
Returns true if the given pathname is a directory, else false.
ino_t inode(const std::filesystem::path &file)
File inode number.
bool rename_ifexists(const std::filesystem::path &src, const std::filesystem::path &dst)
Move src to dst, without raising exception if src does not exist.
std::string read_file(const std::filesystem::path &file)
Read whole file into memory. Throws exceptions on failure.
void rmtree(const std::filesystem::path &pathname)
Delete the directory pathname and all its contents.
void touch(const std::filesystem::path &pathname, time_t ts)
Set mtime and atime for the file.
bool exists(const std::string &s)
Same as access(s, F_OK);.
bool unlink_ifexists(const char *file)
Delete a file if it exists.
bool ischr(const std::string &pathname)
Same as isdir but checks for character devices.
std::string getcwd()
Get the absolute path of the current working directory.
void setrlimit(int resource, const ::rlimit &rlim)
Call setrlimit, raising an exception if it fails.
bool access(const std::filesystem::path &s, int m)
access() a filename
void clock_gettime(::clockid_t clk_id, ::timespec &ts)
Call clock_gettime, raising an exception if it fails.
void unlink(const std::filesystem::path &pathname)
Delete the file using unlink()
void chdir(const std::string &dir)
Change working directory.
bool makedirs(const std::filesystem::path &path)
Create all the component of the given directory, including the directory itself.
void rename(const std::string &src_pathname, const std::string &dst_pathname)
Rename src_pathname into dst_pathname.
bool isreg(const std::string &pathname)
Same as isdir but checks for regular files.
void write_file(const std::filesystem::path &file, const std::string &data, mode_t mode=0777)
Write data to file, replacing existing contents if it already exists.
void breakpoint()
Set a breakpoint at this code location.
std::unique_ptr< struct stat > stat(const char *pathname)
stat() the given file and return the struct stat with the results.
bool islnk(const std::string &pathname)
Same as isdir but checks for symbolic links.
void rmdir(const std::filesystem::path &pathname)
Remove the directory using rmdir(2)
void getrlimit(int resource, ::rlimit &rlim)
rlimit wrappers
bool rmtree_ifexists(const std::filesystem::path &pathname)
Delete the directory pathname and all its contents.
void chroot(const std::filesystem::path &dir)
Change root directory.
bool touch_ifexists(const std::filesystem::path &pathname, time_t ts)
Set mtime and atime for the file.
void write_file_atomically(const std::filesystem::path &file, const std::string &data, mode_t mode=0777)
Write data to file, replacing existing contents if it already exists.
std::filesystem::path abspath(const std::filesystem::path &path)
Get the absolute path of a file.
std::filesystem::path with_suffix(const std::filesystem::path &path, const std::string &suffix)
Return the path with suffix appended to its filename.
bool isblk(const std::string &pathname)
Same as isdir but checks for block devices.
mode_t umask(mode_t mask)
Change umask (always succeeds and returns the previous umask)
bool mkdir_ifmissing(const std::filesystem::path &path)
Create the given directory, if it does not already exists.
unsigned long long timesec_elapsed(const ::timespec &begin, const ::timespec &until)
Return the time elapsed between two timesec structures, in nanoseconds.
bool isfifo(const std::string &pathname)
Same as isdir but checks for FIFOs.
bool issock(const std::string &pathname)
Same as isdir but checks for sockets.
time_t timestamp(const std::filesystem::path &file)
File mtime.
size_t size(const std::filesystem::path &file)
File size.
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:432
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.