17 #include <sys/resource.h> 20 #include <sys/types.h> 31 std::filesystem::path
with_suffix(
const std::filesystem::path& path,
32 const std::string& suffix);
39 std::unique_ptr<struct stat>
stat(
const char* pathname);
46 std::unique_ptr<struct stat>
stat(
const std::string& pathname);
53 std::unique_ptr<struct stat>
stat(
const std::filesystem::path& path);
59 void stat(
const char* pathname,
struct stat& st);
65 void stat(
const std::string& pathname,
struct stat& st);
71 void stat(
const std::filesystem::path& path,
struct stat& st);
78 [[deprecated(
"Use std::filesystem::is_directory")]]
bool 79 isdir(
const std::string& pathname);
82 [[deprecated(
"Use std::filesystem::is_block_file")]]
bool 83 isblk(
const std::string& pathname);
86 [[deprecated(
"Use std::filesystem::is_character_file")]]
bool 87 ischr(
const std::string& pathname);
90 [[deprecated(
"Use std::filesystem::is_fifo")]]
bool 91 isfifo(
const std::string& pathname);
94 [[deprecated(
"Use std::filesystem::is_symlink")]]
bool 95 islnk(
const std::string& pathname);
98 [[deprecated(
"Use std::filesystem::is_regular_file")]]
bool 99 isreg(
const std::string& pathname);
102 [[deprecated(
"Use std::filesystem::is_socket")]]
bool 103 issock(
const std::string& pathname);
106 time_t
timestamp(
const std::filesystem::path& file);
109 time_t
timestamp(
const std::filesystem::path& file, time_t def);
112 size_t size(
const std::filesystem::path& file);
115 size_t size(
const std::filesystem::path& file,
size_t def);
118 ino_t
inode(
const std::filesystem::path& file);
121 ino_t
inode(
const std::filesystem::path& file, ino_t def);
124 bool 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 134 chdir(
const std::string& dir);
137 void chroot(
const std::filesystem::path& dir);
140 mode_t
umask(mode_t mask);
143 std::filesystem::path
abspath(
const std::filesystem::path& path);
144 [[deprecated(
"Use abspath(const std::filesystem::path&)")]] std::string
145 abspath(
const std::string& pathname);
146 std::filesystem::path
abspath(
const char* path);
161 MMap(
void* addr,
size_t length);
164 MMap& operator=(
const MMap&) =
delete;
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);
216 [[noreturn]]
virtual void throw_error(
const char* desc);
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);
336 bool ofd_setlkw(::flock&,
bool retry_on_signal =
true);
349 void setfl(
int flags);
351 operator int()
const {
return fd; }
374 std::filesystem::path path_;
386 [[noreturn]]
virtual void throw_error(
const char* desc)
override;
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_; }
404 using NamedFileDescriptor::NamedFileDescriptor;
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;
451 o.cur_entry =
nullptr;
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; }
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);
518 int openat_ifexists(
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);
528 void lstatat(
const char* pathname,
struct stat& st);
533 void unlinkat(
const char* pathname);
535 void mkdirat(
const char* pathname, mode_t mode = 0777);
538 void rmdirat(
const char* pathname);
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);
565 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
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;
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);
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);
651 std::string
read_file(
const std::filesystem::path& file);
652 [[deprecated(
"Use read_file(const std::filesystem::path&)")]] std::string
662 void write_file(
const std::filesystem::path& file,
const std::string& data,
664 [[deprecated(
"Use write_file(const std::filesystem::path&, …)")]]
void 665 write_file(
const std::string& file,
const std::string& data,
667 void write_file(
const char* file,
const std::string& data, mode_t mode = 0777);
675 void 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 678 write_file(
const std::string& file,
const void* data,
size_t size,
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 719 std::string mkdtemp(std::string templ);
723 void 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 764 makedirs(
const std::filesystem::path& path);
773 std::filesystem::path
which(
const std::string& name);
776 void unlink(
const std::filesystem::path& pathname);
779 void rmdir(
const std::filesystem::path& pathname);
782 void rmtree(
const std::filesystem::path& pathname);
790 [[deprecated(
"use rmtree_ifexists(const std::filesystem::path&)")]]
bool 800 [[deprecated(
"use std::filesystem::rename")]]
void 801 rename(
const std::string& src_pathname,
const std::string& dst_pathname);
808 void touch(
const std::filesystem::path& pathname, time_t ts);
815 bool touch_ifexists(
const std::filesystem::path& pathname, time_t ts);
826 const ::timespec& until);
839 explicit Clock(::clockid_t clk_id);
852 void getrlimit(
int resource, ::rlimit& rlim);
856 void setrlimit(
int resource, const ::rlimit& rlim);
868 void set(rlim_t rlim);
875 bool was_set =
false;
876 std::string orig_value;
898 T* data() {
return buffer; }
899 const T* data()
const {
return buffer; }
900 operator T*() {
return buffer; }
901 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:392
mode_t umask(mode_t mask)
Change umask (always succeeds and returns the previous umask)
bool isdir(const std::string &pathname)
Returns true if the given pathname is a directory, else false.
virtual void throw_error_string(const std::string &desc)
Throw an exception based on errno and the given message.
Clock(::clockid_t clk_id)
Initialize ts with the value of the given clock.
void clock_gettime(::clockid_t clk_id, ::timespec &ts)
Call clock_gettime, raising an exception if it fails.
bool isblk(const std::string &pathname)
Same as isdir but checks for block devices.
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:357
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:153
void breakpoint()
Set a breakpoint at this code location.
bool is_open() const
Check if the file descriptor is open (that is, if it is not -1)
bool makedirs(const std::filesystem::path &path)
Create all the component of the given directory, including the directory itself.
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.
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.
Common operations on file descriptors.
Definition: sys.h:193
File in the file system.
Definition: sys.h:562
void rename(const std::string &src_pathname, const std::string &dst_pathname)
Rename src_pathname into dst_pathname.
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.
void unlink(const std::filesystem::path &pathname)
Delete the file using unlink()
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.
void rmdir(const std::filesystem::path &pathname)
Remove the directory using rmdir(2)
ino_t inode(const std::filesystem::path &file)
File inode number.
Operating system functions.
Definition: sys.h:23
void setrlimit(int resource, const ::rlimit &rlim)
Call setrlimit, raising an exception if it fails.
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.
Override an environment variable for the duration of this object.
Definition: sys.h:872
iterator begin()
Begin iterator on all directory entries.
std::filesystem::path abspath(const std::filesystem::path &path)
Get the absolute path of a file.
Path open_path(int flags=0) const
Return a Path object for this entry.
bool touch_ifexists(const std::filesystem::path &pathname, time_t ts)
Set mtime and atime for the file.
File descriptor with a name.
Definition: sys.h:371
void unlink_on_exit(bool val)
Change the unlink-on-exit behaviour.
bool unlink_ifexists(const char *file)
Delete a file if it exists.
RAII local memory buffer.
Definition: sys.h:886
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:431
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:402
std::string getcwd()
Get the absolute path of the current working directory.
bool mkdir_ifmissing(const std::filesystem::path &path)
Create the given directory, if it does not already exists.
void open(int flags, mode_t mode=0777)
Wrapper around open(2)
void chdir(const std::string &dir)
Change working directory.
bool islnk(const std::string &pathname)
Same as isdir but checks for symbolic links.
Open a temporary directory.
Definition: sys.h:631
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.
void touch(const std::filesystem::path &pathname, time_t ts)
Set mtime and atime for the file.
std::filesystem::path with_suffix(const std::filesystem::path &path, const std::string &suffix)
Return the path with suffix appended to its filename.
void getrlimit(int resource, ::rlimit &rlim)
rlimit wrappers
Override a soft resource limit during the lifetime of the object.
Definition: sys.h:859
virtual void throw_error_string(const std::string &desc) override
Throw an exception based on errno and the given message.
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 isfifo(const std::string &pathname)
Same as isdir but checks for FIFOs.
std::filesystem::path which(const std::string &name)
Compute the absolute path of an executable.
iterator end()
End iterator on all directory entries.
time_t timestamp(const std::filesystem::path &file)
File mtime.
std::string read_file(const std::filesystem::path &file)
Read whole file into memory. Throws exceptions on failure.
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.
bool ischr(const std::string &pathname)
Same as isdir but checks for character devices.
void rmtree(const std::filesystem::path &pathname)
Delete the directory pathname and all its contents.
~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.
void chroot(const std::filesystem::path &dir)
Change root directory.
bool issock(const std::string &pathname)
Same as isdir but checks for sockets.
unsigned long long timesec_elapsed(const ::timespec &begin, const ::timespec &until)
Return the time elapsed between two timesec structures, in nanoseconds.
int openat_ifexists(const char *pathname, int flags, mode_t mode=0777)
Same as openat, but returns -1 if the file does not exist.
std::unique_ptr< struct stat > stat(const char *pathname)
stat() the given file and return the struct stat with the results.
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
bool rmtree_ifexists(const std::filesystem::path &pathname)
Delete the directory pathname and all its contents.
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:831
bool access(const std::filesystem::path &s, int m)
access() a filename
Open a temporary file.
Definition: sys.h:605
bool isreg(const std::string &pathname)
Same as isdir but checks for regular files.
bool exists(const std::string &s)
Same as access(s, F_OK);.
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:426
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.
size_t size(const std::filesystem::path &file)
File size.
void rmdirat(const char *pathname)
unlinkat with the AT_REMOVEDIR flag set