15 #include <sys/types.h> 18 #include <sys/resource.h> 32 std::filesystem::path with_suffix(
const std::filesystem::path& path,
const std::string& suffix);
40 std::unique_ptr<struct stat> stat(
const char* pathname);
47 std::unique_ptr<struct stat> stat(
const std::string& pathname);
54 std::unique_ptr<struct stat> stat(
const std::filesystem::path& path);
60 void stat(
const char* pathname,
struct stat& st);
66 void stat(
const std::string& pathname,
struct stat& st);
72 void stat(
const std::filesystem::path& path,
struct stat& st);
79 [[deprecated(
"Use std::filesystem::is_directory")]]
bool isdir(
const std::string& pathname);
82 [[deprecated(
"Use std::filesystem::is_block_file")]]
bool isblk(
const std::string& pathname);
85 [[deprecated(
"Use std::filesystem::is_character_file")]]
bool ischr(
const std::string& pathname);
88 [[deprecated(
"Use std::filesystem::is_fifo")]]
bool isfifo(
const std::string& pathname);
91 [[deprecated(
"Use std::filesystem::is_symlink")]]
bool islnk(
const std::string& pathname);
94 [[deprecated(
"Use std::filesystem::is_regular_file")]]
bool isreg(
const std::string& pathname);
97 [[deprecated(
"Use std::filesystem::is_socket")]]
bool issock(
const std::string& pathname);
100 time_t timestamp(
const std::filesystem::path& file);
103 time_t timestamp(
const std::filesystem::path& file, time_t def);
106 size_t size(
const std::filesystem::path& file);
109 size_t size(
const std::filesystem::path& file,
size_t def);
112 ino_t inode(
const std::filesystem::path& file);
115 ino_t inode(
const std::filesystem::path& file, ino_t def);
118 bool access(
const std::filesystem::path& s,
int m);
121 [[deprecated(
"Use std::filesystem::exists")]]
bool exists(
const std::string& s);
124 [[deprecated(
"Use std::filesystem::current_path")]] std::string getcwd();
127 [[deprecated(
"Use std::filesystem::current_path")]]
void chdir(
const std::string& dir);
130 void chroot(
const std::filesystem::path& dir);
133 mode_t umask(mode_t mask);
136 [[deprecated(
"Use std::filesystem::canonical")]] std::string abspath(
const std::string& pathname);
151 MMap(
void* addr,
size_t length);
154 MMap& operator=(
const MMap&) =
delete;
157 size_t size()
const {
return length; }
162 operator const T*()
const {
return reinterpret_cast<const T*
>(addr); }
165 operator T*()
const {
return reinterpret_cast<T*
>(addr); }
202 [[noreturn]]
virtual void throw_error(
const char* desc);
223 void fstat(
struct stat& st);
224 void fchmod(mode_t mode);
226 void futimens(const ::timespec ts[2]);
233 size_t read(
void* buf,
size_t count);
250 size_t write(
const void* buf,
size_t count);
252 template<
typename Container>
253 size_t write(
const Container& c)
255 return write(c.data(), c.size() *
sizeof(Container::value_type));
261 template<
typename Container>
273 template<
typename Container>
279 off_t lseek(off_t offset,
int whence=SEEK_SET);
281 size_t pread(
void* buf,
size_t count, off_t offset);
282 size_t pwrite(
const void* buf,
size_t count, off_t offset);
284 template<
typename Container>
285 size_t pwrite(
const Container& c, off_t offset)
287 return pwrite(c.data(), c.size() *
sizeof(
typename Container::value_type), offset);
290 void ftruncate(off_t length);
292 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
311 bool ofd_setlkw(::flock&,
bool retry_on_signal=
true);
324 void setfl(
int flags);
326 operator int()
const {
return fd; }
352 std::filesystem::path path_;
364 [[noreturn]]
virtual void throw_error(
const char* desc)
override;
368 [[deprecated(
"use path() instead")]] std::string
name()
const {
return path_.string(); }
369 const std::filesystem::path& path()
const {
return path_; }
378 using NamedFileDescriptor::NamedFileDescriptor;
407 using iterator_category = std::input_iterator_tag;
408 using value_type = ::dirent;
409 using difference_type = int;
410 using pointer = ::dirent*;
411 using reference = ::dirent&;
413 Path* path =
nullptr;
415 ::dirent* cur_entry =
nullptr;
423 : dir(o.dir), cur_entry(o.cur_entry)
426 o.cur_entry =
nullptr;
432 bool operator==(
const iterator& i)
const;
433 bool operator!=(
const iterator& i)
const;
434 ::dirent& operator*()
const {
return *cur_entry; }
435 ::dirent* operator->()
const {
return cur_entry; }
463 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
468 explicit Path(
const std::filesystem::path& pathname,
int flags=0, mode_t mode=0777);
472 Path(
Path& parent,
const char* pathname,
int flags=0, mode_t mode=0777);
475 Path& operator=(
const Path&) =
delete;
479 void open(
int flags, mode_t mode=0777);
489 int openat(
const char* pathname,
int flags, mode_t mode=0777);
492 int openat_ifexists(
const char* pathname,
int flags, mode_t mode=0777);
494 bool faccessat(
const char* pathname,
int mode,
int flags=0);
496 void fstatat(
const char* pathname,
struct stat& st);
502 void lstatat(
const char* pathname,
struct stat& st);
507 void unlinkat(
const char* pathname);
509 void mkdirat(
const char* pathname, mode_t mode=0777);
512 void rmdirat(
const char* pathname);
514 void symlinkat(
const char* target,
const char* linkpath);
516 std::string readlinkat(
const char* pathname);
525 static std::string mkdtemp(
const std::filesystem::path& prefix);
526 [[deprecated(
"Use mkdtemp(const std::filesystem::path&)")]]
static std::string mkdtemp(
const std::string& prefix);
527 static std::string mkdtemp(
const char* prefix);
528 static std::string mkdtemp(
char* pathname_template);
538 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
546 explicit File(
const std::filesystem::path& path);
547 explicit File(
const char* path);
548 [[deprecated(
"Use File(const std::filesystem::path&)")]]
explicit File(
const std::string& path);
551 File(
const std::filesystem::path& path,
int flags, mode_t mode=0777);
553 File& operator=(
const File&) =
delete;
557 void open(
int flags, mode_t mode=0777);
565 static File mkstemp(
const std::filesystem::path& prefix);
566 [[deprecated(
"Use mkstemp(const std::filesysten::path&)")]]
static File mkstemp(
const std::string& prefix);
567 static File mkstemp(
const char* prefix);
568 static File mkstemp(
char* pathname_template);
580 bool m_unlink_on_exit =
true;
584 explicit Tempfile(
const std::filesystem::path& prefix);
585 [[deprecated(
"Use Tempfile(const std::string&)")]]
explicit Tempfile(
const std::string& prefix);
586 explicit Tempfile(
const char* prefix);
606 bool m_rmtree_on_exit =
true;
610 explicit Tempdir(
const std::filesystem::path& prefix);
611 [[deprecated(
"Use Tempdir(const std::filesystem::path&)")]]
explicit Tempdir(
const std::string& prefix);
612 explicit Tempdir(
const char* prefix);
620 std::string read_file(
const std::filesystem::path& file);
621 [[deprecated(
"Use read_file(const std::filesystem::path&)")]] std::string read_file(
const std::string& file);
622 std::string read_file(
const char* file);
630 void write_file(
const std::filesystem::path& file,
const std::string& data, mode_t mode=0777);
631 [[deprecated(
"Use write_file(const std::filesystem::path&, …)")]]
void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
632 void write_file(
const char* file,
const std::string& data, mode_t mode=0777);
640 void write_file(
const std::filesystem::path& file,
const void* data,
size_t size, mode_t mode=0777);
641 [[deprecated(
"Use write_file(const std::filesystem::path&, …)")]]
void write_file(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
642 void write_file(
const char* file,
const void* data,
size_t size, mode_t mode=0777);
653 void write_file_atomically(
const std::filesystem::path& file,
const std::string& data, mode_t mode=0777);
654 [[deprecated(
"Use write_file_atomically(const std::filesystem::path&, …)")]]
void write_file_atomically(
const std::string& file,
const std::string& data, mode_t mode=0777);
655 void write_file_atomically(
const char* file,
const std::string& data, mode_t mode=0777);
666 void write_file_atomically(
const std::filesystem::path& file,
const void* data,
size_t size, mode_t mode=0777);
667 [[deprecated(
"Use write_file_atomically(const std::filesystem::path&, …)")]]
void write_file_atomically(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
671 std::string mkdtemp(std::string templ);
675 void mkFilePath(
const std::string& file);
683 [[deprecated(
"use std::filesystem::remove")]]
bool unlink_ifexists(
const char* file);
684 [[deprecated(
"use std::filesystem::remove")]]
bool unlink_ifexists(
const std::string& file);
685 [[deprecated(
"use std::filesystem::remove")]]
bool unlink_ifexists(
const std::filesystem::path& file);
692 bool rename_ifexists(
const std::filesystem::path& src,
const std::filesystem::path& dst);
702 [[deprecated(
"use std::filesystem::create_directory")]]
bool mkdir_ifmissing(
const std::filesystem::path& path);
710 [[deprecated(
"use std::filesystem::create_directories")]]
bool makedirs(
const std::filesystem::path& path);
719 std::filesystem::path which(
const std::string& name);
722 void unlink(
const std::filesystem::path& pathname);
725 void rmdir(
const std::filesystem::path& pathname);
728 void rmtree(
const std::filesystem::path& pathname);
735 bool rmtree_ifexists(
const std::filesystem::path& pathname);
736 [[deprecated(
"use rmtree_ifexists(const std::filesystem::path&)")]]
bool rmtree_ifexists(
const std::string& pathname);
737 bool rmtree_ifexists(
const char* pathname);
745 [[deprecated(
"use std::filesystem::rename")]]
void rename(
const std::string& src_pathname,
const std::string& dst_pathname);
750 void touch(
const std::filesystem::path& pathname, time_t ts);
755 void clock_gettime(::clockid_t clk_id, ::timespec& ts);
760 unsigned long long timesec_elapsed(const ::timespec& begin, const ::timespec& until);
773 explicit Clock(::clockid_t clk_id);
786 void getrlimit(
int resource, ::rlimit& rlim);
790 void setrlimit(
int resource, const ::rlimit& rlim);
802 void set(rlim_t rlim);
807 template<
typename T =
char>
814 : buffer(
new T[size])
826 T* data() {
return buffer; }
827 const T* data()
const {
return buffer; }
828 operator T*() {
return buffer; }
829 operator const T*()
const {
return buffer; }
virtual void throw_error(const char *desc)
Throw an exception based on errno and the given message.
std::string name() const
Return the file pathname.
Definition: sys.h:368
Clock(::clockid_t clk_id)
Initialize ts with the value of the given clock.
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...
void unlink()
Unlink the file right now.
RAII mechanism to save restore file times at the end of some file operations.
Definition: sys.h:333
bool read_all_or_retry(void *buf, size_t count)
Read count bytes into bufr, retrying partial reads, stopping at EOF.
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:143
bool is_open() const
Check if the file descriptor is open (that is, if it is not -1)
bool ofd_setlk(::flock &)
Open file description locks F_OFD_SETLK operation.
virtual void throw_runtime_error(const char *desc) override
Throw a runtime_error unrelated from errno.
void close()
Close the file descriptor, setting its value to -1.
Common operations on file descriptors.
Definition: sys.h:179
File in the file system.
Definition: sys.h:535
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.
int getfl()
Get open flags for the file.
bool ofd_setlkw(::flock &, bool retry_on_signal=true)
Open file description locks F_OFD_SETLKW operation.
virtual void throw_runtime_error(const char *desc)
Throw a runtime_error unrelated from errno.
iterator begin()
Begin iterator on all directory entries.
Path open_path(int flags=0) const
Return a Path object for this entry.
File descriptor with a name.
Definition: sys.h:349
void unlink_on_exit(bool val)
Change the unlink-on-exit behaviour.
RAII local memory buffer.
Definition: sys.h:808
void lstatat(const char *pathname, struct stat &st)
fstatat with the AT_SYMLINK_NOFOLLOW flag set
void write_all_or_retry(const void *buf, size_t count)
Write all the data in buf, retrying partial writes.
Iterator for directory entries.
Definition: sys.h:405
void rmtree_on_exit(bool val)
Change the rmtree-on-exit behaviour.
File descriptor that gets automatically closed in the object destructor.
Definition: sys.h:376
void open(int flags, mode_t mode=0777)
Wrapper around open(2)
Open a temporary directory.
Definition: sys.h:603
void setfl(int flags)
Set open flags for the file.
void open(int flags, mode_t mode=0777)
Wrapper around open(2) with flags | O_PATH.
Override a soft resource limit during the lifetime of the object.
Definition: sys.h:793
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.
iterator end()
End iterator on all directory entries.
String functions.
Definition: benchmark.h:13
unsigned long long elapsed()
Return the number of nanoseconds elapsed since the last time ts was updated.
void rmtree()
Delete the directory pointed to by this Path, with all its contents.
Path(const std::filesystem::path &pathname, int flags=0, mode_t mode=0777)
Open the given pathname with flags | O_PATH.
~ManagedNamedFileDescriptor()
The destructor closes the file descriptor, but does not check errors on ::close().
bool ofd_getlk(::flock &)
Open file description locks F_OFD_GETLK 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 fstatat_ifexists(const char *pathname, struct stat &st)
fstatat, but in case of ENOENT returns false instead of throwing
bool lstatat_ifexists(const char *pathname, struct stat &st)
lstatat, but in case of ENOENT returns false instead of throwing
virtual void throw_error(const char *desc) override
Throw an exception based on errno and the given message.
Access to clock_gettime.
Definition: sys.h:765
Open a temporary file.
Definition: sys.h:577
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:400
void rmdirat(const char *pathname)
unlinkat with the AT_REMOVEDIR flag set