14 #include <sys/types.h> 17 #include <sys/resource.h> 30 std::unique_ptr<struct stat> stat(
const std::string& pathname);
36 void stat(
const std::string& pathname,
struct stat& st);
43 bool isdir(
const std::string& pathname);
46 bool isblk(
const std::string& pathname);
49 bool ischr(
const std::string& pathname);
52 bool isfifo(
const std::string& pathname);
55 bool islnk(
const std::string& pathname);
58 bool isreg(
const std::string& pathname);
61 bool issock(
const std::string& pathname);
64 time_t timestamp(
const std::string& file);
67 time_t timestamp(
const std::string& file, time_t def);
70 size_t size(
const std::string& file);
73 size_t size(
const std::string& file,
size_t def);
76 ino_t inode(
const std::string& file);
79 ino_t inode(
const std::string& file, ino_t def);
82 bool access(
const std::string& s,
int m);
85 bool exists(
const std::string& s);
91 std::string abspath(
const std::string& pathname);
106 MMap(
void* addr,
size_t length);
109 MMap& operator=(
const MMap&) =
delete;
112 size_t size()
const {
return length; }
117 operator const T*()
const {
return reinterpret_cast<const T*
>(addr); }
120 operator T*()
const {
return reinterpret_cast<T*
>(addr); };
157 [[noreturn]]
virtual void throw_error(
const char* desc);
166 [[noreturn]]
virtual void throw_runtime_error(
const char* desc);
169 bool is_open()
const;
178 void fstat(
struct stat& st);
179 void fchmod(mode_t mode);
181 void futimens(
const struct ::timespec ts[2]);
188 size_t read(
void* buf,
size_t count);
194 void read_all_or_throw(
void* buf,
size_t count);
196 size_t write(
const void* buf,
size_t count);
198 template<
typename Container>
199 size_t write(
const Container& c)
201 return write(c.data(), c.size() *
sizeof(Container::value_type));
205 void write_all_or_retry(
const void* buf,
size_t count);
207 template<
typename Container>
208 void write_all_or_retry(
const Container& c)
210 write_all_or_retry(c.data(), c.size() *
sizeof(
typename Container::value_type));
217 void write_all_or_throw(
const void* buf,
size_t count);
219 template<
typename Container>
220 void write_all_or_throw(
const Container& c)
222 write_all_or_throw(c.data(), c.size() *
sizeof(
typename Container::value_type));
225 off_t lseek(off_t offset,
int whence=SEEK_SET);
227 size_t pread(
void* buf,
size_t count, off_t offset);
228 size_t pwrite(
const void* buf,
size_t count, off_t offset);
230 template<
typename Container>
231 size_t pwrite(
const Container& c, off_t offset)
233 return pwrite(c.data(), c.size() *
sizeof(
typename Container::value_type), offset);
236 void ftruncate(off_t length);
238 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
246 bool ofd_setlk(struct ::flock&);
257 bool ofd_setlkw(struct ::flock&,
bool retry_on_signal=
true);
264 bool ofd_getlk(struct ::flock&);
266 operator int()
const {
return fd; }
277 struct ::timespec ts[2];
292 std::string pathname;
304 [[noreturn]]
virtual void throw_error(
const char* desc);
305 [[noreturn]]
virtual void throw_runtime_error(
const char* desc);
308 const std::string&
name()
const {
return pathname; }
317 using NamedFileDescriptor::NamedFileDescriptor;
344 struct iterator :
public std::iterator<std::input_iterator_tag, struct dirent>
346 Path* path =
nullptr;
348 struct dirent* cur_entry =
nullptr;
356 : dir(o.dir), cur_entry(o.cur_entry)
359 o.cur_entry =
nullptr;
365 bool operator==(
const iterator& i)
const;
366 bool operator!=(
const iterator& i)
const;
367 struct dirent& operator*()
const {
return *cur_entry; }
368 struct dirent* operator->()
const {
return cur_entry; }
393 Path open_path(
int flags=0)
const;
396 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
401 Path(
const char* pathname,
int flags=0);
405 Path(
const std::string& pathname,
int flags=0);
409 Path(
Path& parent,
const char* pathname,
int flags=0);
412 Path& operator=(
const Path&) =
delete;
423 int openat(
const char* pathname,
int flags, mode_t mode=0777);
426 int openat_ifexists(
const char* pathname,
int flags, mode_t mode=0777);
428 bool faccessat(
const char* pathname,
int mode,
int flags=0);
430 void fstatat(
const char* pathname,
struct stat& st);
433 bool fstatat_ifexists(
const char* pathname,
struct stat& st);
436 void lstatat(
const char* pathname,
struct stat& st);
439 bool lstatat_ifexists(
const char* pathname,
struct stat& st);
441 void unlinkat(
const char* pathname);
444 void rmdirat(
const char* pathname);
461 using ManagedNamedFileDescriptor::ManagedNamedFileDescriptor;
469 File(
const std::string& pathname);
472 File(
const std::string& pathname,
int flags, mode_t mode=0777);
474 File& operator=(
const File&) =
delete;
478 void open(
int flags, mode_t mode=0777);
484 bool open_ifexists(
int flags, mode_t mode=0777);
486 static File mkstemp(
const std::string& prefix);
487 static File mkstemp(
const char* prefix);
488 static File mkstemp(
char* pathname_template);
492 std::string read_file(
const std::string &file);
500 void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
508 void write_file(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
519 void write_file_atomically(
const std::string& file,
const std::string& data, mode_t mode=0777);
530 void write_file_atomically(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
534 std::string mkdtemp(std::string templ);
538 void mkFilePath(
const std::string& file);
546 bool unlink_ifexists(
const std::string& file);
553 bool rename_ifexists(
const std::string& src,
const std::string& dst);
563 bool mkdir_ifmissing(
const char* pathname, mode_t mode=0777);
565 bool mkdir_ifmissing(
const std::string& pathname, mode_t mode=0777);
573 bool makedirs(
const std::string& pathname, mode_t=0777);
582 std::string which(
const std::string& name);
585 void unlink(
const std::string& pathname);
588 void rmdir(
const std::string& pathname);
591 void rmtree(
const std::string& pathname);
598 bool rmtree_ifexists(
const std::string& pathname);
606 void rename(
const std::string& src_pathname,
const std::string& dst_pathname);
611 void touch(
const std::string& pathname, time_t ts);
616 void clock_gettime(::clockid_t clk_id, struct ::timespec& ts);
621 unsigned long long timesec_elapsed(
const struct ::timespec& begin,
const struct ::timespec& until);
629 struct ::timespec ts;
634 Clock(::clockid_t clk_id);
640 unsigned long long elapsed();
647 void getrlimit(
int resource, struct ::rlimit& rlim);
651 void setrlimit(
int resource,
const struct ::rlimit& rlim);
657 struct ::rlimit orig;
663 void set(rlim_t rlim);
RAII mechanism to save restore file times at the end of some file operations.
Definition: sys.h:273
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:98
Common operations on file descriptors.
Definition: sys.h:134
File in the file system.
Definition: sys.h:458
File descriptor with a name.
Definition: sys.h:289
Iterator for directory entries.
Definition: sys.h:344
File descriptor that gets automatically closed in the object destructor.
Definition: sys.h:315
Override a soft resource limit during the lifetime of the object.
Definition: sys.h:654
const std::string & name() const
Return the file pathname.
Definition: sys.h:308
String functions.
Definition: benchmark.h:13
Access to clock_gettime.
Definition: sys.h:626
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:339