15 #include <sys/types.h> 28 std::unique_ptr<struct stat> stat(
const std::string& pathname);
34 void stat(
const std::string& pathname,
struct stat& st);
41 bool isdir(
const std::string& pathname);
44 bool isblk(
const std::string& pathname);
47 bool ischr(
const std::string& pathname);
50 bool isfifo(
const std::string& pathname);
53 bool islnk(
const std::string& pathname);
56 bool isreg(
const std::string& pathname);
59 bool issock(
const std::string& pathname);
62 time_t timestamp(
const std::string& file);
65 time_t timestamp(
const std::string& file, time_t def);
68 size_t size(
const std::string& file);
71 size_t size(
const std::string& file,
size_t def);
74 ino_t inode(
const std::string& file);
77 ino_t inode(
const std::string& file, ino_t def);
80 bool access(
const std::string& s,
int m);
83 bool exists(
const std::string& s);
89 std::string abspath(
const std::string& pathname);
104 MMap(
void* addr,
size_t length);
107 MMap& operator=(
const MMap&) =
delete;
110 size_t size()
const {
return length; }
115 operator const T*()
const {
return reinterpret_cast<const T*
>(addr); }
118 operator T*()
const {
return reinterpret_cast<T*
>(addr); };
155 [[noreturn]]
virtual void throw_error(
const char* desc);
164 [[noreturn]]
virtual void throw_runtime_error(
const char* desc);
168 void fstat(
struct stat& st);
169 void fchmod(mode_t mode);
173 size_t read(
void* buf,
size_t count);
179 void read_all_or_throw(
void* buf,
size_t count);
181 size_t write(
const void* buf,
size_t count);
183 template<
typename Container>
184 size_t write(
const Container& c)
186 return write(c.data(), c.size() *
sizeof(Container::value_type));
190 void write_all_or_retry(
const void* buf,
size_t count);
192 template<
typename Container>
193 void write_all_or_retry(
const Container& c)
195 write_all_or_retry(c.data(), c.size() *
sizeof(
typename Container::value_type));
202 void write_all_or_throw(
const void* buf,
size_t count);
204 template<
typename Container>
205 void write_all_or_throw(
const Container& c)
207 write_all_or_throw(c.data(), c.size() *
sizeof(
typename Container::value_type));
210 off_t lseek(off_t offset,
int whence=SEEK_SET);
212 size_t pread(
void* buf,
size_t count, off_t offset);
213 size_t pwrite(
const void* buf,
size_t count, off_t offset);
215 template<
typename Container>
216 size_t pwrite(
const Container& c, off_t offset)
218 return pwrite(c.data(), c.size() *
sizeof(
typename Container::value_type), offset);
221 void ftruncate(off_t length);
223 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
225 operator int()
const {
return fd; }
235 std::string pathname;
247 [[noreturn]]
virtual void throw_error(
const char* desc);
248 [[noreturn]]
virtual void throw_runtime_error(
const char* desc);
251 const std::string&
name()
const {
return pathname; }
262 struct iterator :
public std::iterator<std::input_iterator_tag, struct dirent>
264 Path* path =
nullptr;
266 struct dirent* cur_entry =
nullptr;
274 : dir(o.dir), cur_entry(o.cur_entry)
277 o.cur_entry =
nullptr;
283 bool operator==(
const iterator& i)
const;
284 bool operator!=(
const iterator& i)
const;
285 struct dirent& operator*()
const {
return *cur_entry; }
286 struct dirent* operator->()
const {
return cur_entry; }
311 using NamedFileDescriptor::NamedFileDescriptor;
316 Path(
const char* pathname,
int flags=0);
320 Path(
const std::string& pathname,
int flags=0);
324 Path(
Path& parent,
const char* pathname,
int flags=0);
327 Path& operator=(
const Path&) =
delete;
347 int openat(
const char* pathname,
int flags, mode_t mode=0777);
349 void fstatat(
const char* pathname,
struct stat& st);
352 void lstatat(
const char* pathname,
struct stat& st);
354 void unlinkat(
const char* pathname);
357 void rmdirat(
const char* pathname);
374 using NamedFileDescriptor::NamedFileDescriptor;
382 File(
const std::string& pathname);
385 File(
const std::string& pathname,
int flags, mode_t mode=0777);
396 File& operator=(
const File&) =
delete;
400 void open(
int flags, mode_t mode=0777);
406 bool open_ifexists(
int flags, mode_t mode=0777);
408 static File mkstemp(
const std::string& prefix);
412 std::string read_file(
const std::string &file);
420 void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
428 void write_file(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
439 void write_file_atomically(
const std::string& file,
const std::string& data, mode_t mode=0777);
450 void write_file_atomically(
const std::string& file,
const void* data,
size_t size, mode_t mode=0777);
454 std::string mkdtemp(std::string templ);
458 void mkFilePath(
const std::string& file);
466 bool unlink_ifexists(
const std::string& file);
473 bool rename_ifexists(
const std::string& src,
const std::string& dst);
483 bool mkdir_ifmissing(
const char* pathname, mode_t mode=0777);
485 bool mkdir_ifmissing(
const std::string& pathname, mode_t mode=0777);
493 bool makedirs(
const std::string& pathname, mode_t=0777);
502 std::string which(
const std::string& name);
505 void unlink(
const std::string& pathname);
508 void rmdir(
const std::string& pathname);
511 void rmtree(
const std::string& pathname);
525 const Directory* dir;
529 struct dirent* direntbuf;
535 const_iterator(
const Directory& dir);
540 const_iterator(
const const_iterator& i);
541 const_iterator& operator=(
const const_iterator& i);
544 const_iterator& operator++();
547 std::string operator*()
const;
549 bool operator==(
const const_iterator& iter)
const;
550 bool operator!=(
const const_iterator& iter)
const;
553 Directory(
const std::string& path);
557 const std::string& path()
const {
return m_path; }
563 const_iterator begin()
const;
566 const_iterator end()
const;
const std::string & name() const
Return the file pathname.
Definition: sys.h:251
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:96
Common operations on file descriptors.
Definition: sys.h:132
open(2) file descriptors
Definition: sys.h:371
File descriptor with a name.
Definition: sys.h:232
Iterator for directory entries.
Definition: sys.h:262
String functions.
Definition: benchmark.h:13
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:257