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);
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); };
150 [[noreturn]]
virtual void throw_error(
const char* desc);
154 void fstat(
struct stat& st);
155 void fchmod(mode_t mode);
157 size_t write(
const void* buf,
size_t count);
162 void write_all(
const void* buf,
size_t count);
164 MMap mmap(
size_t length,
int prot,
int flags, off_t offset=0);
166 operator int()
const {
return fd; }
185 [[noreturn]]
virtual void throw_error(
const char* desc);
188 const std::string&
name()
const {
return pathname; }
199 struct iterator :
public std::iterator<std::input_iterator_tag, struct dirent>
203 struct dirent* cur_entry =
nullptr;
211 : dir(o.dir), cur_entry(o.cur_entry)
214 o.cur_entry =
nullptr;
220 bool operator==(
const iterator& i)
const;
221 bool operator!=(
const iterator& i)
const;
253 Path(
const char* pathname,
int flags=0);
257 Path(
const std::string& pathname,
int flags=0);
261 Path(
Path& parent,
const char* pathname,
int flags=0);
284 int openat(
const char* pathname,
int flags, mode_t mode=0777);
286 void fstatat(
const char* pathname,
struct stat& st);
289 void lstatat(
const char* pathname,
struct stat& st);
291 void unlinkat(
const char* pathname);
294 void rmdirat(
const char* pathname);
317 File(
const std::string& pathname,
int flags, mode_t mode=0777);
331 static File mkstemp(
const std::string& prefix);
335 std::string
read_file(
const std::string &file);
343 void write_file(
const std::string& file,
const std::string& data, mode_t mode=0777);
358 std::string mkdtemp(std::string templ);
362 void mkFilePath(
const std::string& file);
388 void makedirs(
const std::string& pathname, mode_t=0777);
397 std::string
which(
const std::string& name);
400 void unlink(
const std::string& pathname);
403 void rmdir(
const std::string& pathname);
406 void rmtree(
const std::string& pathname);
420 const Directory* dir;
424 struct dirent* direntbuf;
430 const_iterator(
const Directory& dir);
435 const_iterator(
const const_iterator& i);
436 const_iterator&
operator=(
const const_iterator& i);
439 const_iterator& operator++();
442 std::string operator*()
const;
444 bool operator==(
const const_iterator& iter)
const;
445 bool operator!=(
const const_iterator& iter)
const;
448 Directory(
const std::string& path);
452 const std::string& path()
const {
return m_path; }
458 const_iterator begin()
const;
461 const_iterator end()
const;
std::string abspath(const std::string &pathname)
Get the absolute path of a file.
Definition: sys.cc:171
bool ischr(const std::string &pathname)
Same as isdir but checks for character devices.
Definition: sys.cc:76
bool isreg(const std::string &pathname)
Same as isdir but checks for regular files.
Definition: sys.cc:91
void mkdir_ifmissing(const char *pathname, mode_t mode)
Create the given directory, if it does not already exists.
Definition: sys.cc:721
Wrap a path on the file system opened with O_PATH.
Definition: sys.h:194
bool rename_ifexists(const std::string &src, const std::string &dst)
Move src to dst, without raising exception if src does not exist.
Definition: sys.cc:659
~MMap()
Definition: sys.cc:208
std::string getcwd()
Get the absolute path of the current working directory.
Definition: sys.cc:153
void write_file(const std::string &file, const std::string &data, mode_t mode)
Write data to file, replacing existing contents if it already exists.
Definition: sys.cc:612
bool exists(const std::string &file)
Same as access(s, F_OK);.
Definition: sys.cc:148
struct dirent * operator->() const
Definition: sys.h:223
size_t size(const std::string &file)
File size.
Definition: sys.cc:116
void makedirs(const std::string &pathname, mode_t mode)
Create all the component of the given directory, including the directory itself.
Definition: sys.cc:731
time_t timestamp(const std::string &file)
File mtime.
Definition: sys.cc:103
bool unlink_ifexists(const std::string &file)
Delete a file if it exists.
Definition: sys.cc:646
Common operations on file descriptors.
Definition: sys.h:132
std::string read_file(const std::string &file)
Read whole file into memory. Throws exceptions on failure.
Definition: sys.cc:598
ino_t inode(const std::string &file)
File inode number.
Definition: sys.cc:129
void munmap()
Definition: sys.cc:213
String functions.
Definition: apt.cc:38
std::string pathname
Definition: sys.h:177
bool isblk(const std::string &pathname)
Same as isdir but checks for block devices.
Definition: sys.cc:71
iterator(iterator &&o)
Definition: sys.h:210
void write_file_atomically(const std::string &file, const std::string &data, mode_t mode)
Write data to file, replacing existing contents if it already exists.
Definition: sys.cc:619
void rmtree(const std::string &pathname)
Delete the directory pathname and all its contents.
Definition: sys.cc:776
void unlink(const std::string &pathname)
Delete the file using unlink()
Definition: sys.cc:764
MMap & operator=(const MMap &)=delete
void rmdir(const std::string &pathname)
Remove the directory using rmdir(2)
Definition: sys.cc:770
Wraps a mmapped memory area, unmapping it on destruction.
Definition: sys.h:96
Iterator for directory entries.
Definition: sys.h:199
bool islnk(const std::string &pathname)
Same as isdir but checks for symbolic links.
Definition: sys.cc:86
MMap(const MMap &)=delete
bool issock(const std::string &pathname)
Same as isdir but checks for sockets.
Definition: sys.cc:96
struct dirent & operator*() const
Definition: sys.h:222
NamedFileDescriptor(int fd, const std::string &pathname)
Definition: sys.cc:287
const std::string & name() const
Return the file pathname.
Definition: sys.h:188
bool isdir(const std::string &pathname)
Returns true if the given pathname is a directory, else false.
Definition: sys.cc:66
std::string which(const std::string &name)
Compute the absolute path of an executable.
Definition: sys.cc:743
bool access(const std::string &s, int m)
access() a filename
Definition: sys.cc:143
size_t size() const
Definition: sys.h:110
std::unique_ptr< struct stat > stat(const std::string &pathname)
stat() the given file and return the struct stat with the results.
Definition: sys.cc:37
bool isfifo(const std::string &pathname)
Same as isdir but checks for FIFOs.
Definition: sys.cc:81
open(2) file descriptors
Definition: sys.h:308
File descriptor with a name.
Definition: sys.h:174