#pragma once #include #include #include #include #include #include #include #include "xwrap.hpp" template static inline T align_to(T v, int a) { static_assert(std::is_integral::value); return (v + a - 1) / a * a; } template static inline T align_padding(T v, int a) { return align_to(v, a) - v; } struct file_attr { struct stat st; char con[128]; }; struct mount_info { unsigned int id; unsigned int parent; dev_t device; std::string root; std::string target; std::string vfs_option; struct { unsigned int shared; unsigned int master; unsigned int propagate_from; } optional; std::string type; std::string source; std::string fs_option; }; struct heap_data; struct byte_view { byte_view() : _buf(nullptr), _sz(0) {} byte_view(const void *buf, size_t sz) : _buf((uint8_t *) buf), _sz(sz) {} byte_view(std::string_view str) : byte_view(str.data(), str.length()) {} byte_view(const std::vector &v) : byte_view(v.data(), v.size()) {} const uint8_t *buf() const { return _buf; } const size_t &sz() const { return _sz; } bool contains(std::string_view pattern) const; bool equals(const byte_view &o) const; heap_data clone() const; protected: uint8_t *_buf; size_t _sz; byte_view(uint8_t *buf, size_t sz) : _buf(buf), _sz(sz) {} void swap(byte_view &o); }; struct byte_data : public byte_view { using str_pairs = std::initializer_list>; using byte_pairs = std::initializer_list>; byte_data() = default; byte_data(void *buf, size_t sz) : byte_view(static_cast(buf), sz) {} using byte_view::buf; using byte_view::sz; uint8_t *buf() { return _buf; } size_t &sz() { return _sz; } int patch(str_pairs list); int patch(byte_pairs list); }; #define MOVE_ONLY(clazz) \ clazz() = default; \ clazz(const clazz&) = delete; \ clazz(clazz &&o) { swap(o); } \ clazz& operator=(clazz &&o) { swap(o); return *this; } struct heap_data : public byte_data { MOVE_ONLY(heap_data) explicit heap_data(size_t sz) : byte_data(malloc(sz), sz) {} heap_data(const void *buf, size_t sz) : heap_data(sz) { memcpy(_buf, buf, sz); } ~heap_data() { free(_buf); } void realloc(size_t sz); }; struct mmap_data : public byte_data { MOVE_ONLY(mmap_data) mmap_data(const char *name, bool rw = false); ~mmap_data() { if (_buf) munmap(_buf, _sz); } }; extern "C" { int mkdirs(const char *path, mode_t mode); ssize_t canonical_path(const char * __restrict__ path, char * __restrict__ buf, size_t bufsiz); } // extern "C" using rust::fd_path; int fd_pathat(int dirfd, const char *name, char *path, size_t size); void rm_rf(const char *path); void mv_path(const char *src, const char *dest); void mv_dir(int src, int dest); void cp_afc(const char *src, const char *dest); void link_path(const char *src, const char *dest); void link_dir(int src, int dest); static inline ssize_t realpath( const char * __restrict__ path, char * __restrict__ buf, size_t bufsiz) { return canonical_path(path, buf, bufsiz); } int getattr(const char *path, file_attr *a); int getattrat(int dirfd, const char *name, file_attr *a); int fgetattr(int fd, file_attr *a); int setattr(const char *path, file_attr *a); int setattrat(int dirfd, const char *name, file_attr *a); int fsetattr(int fd, file_attr *a); void fclone_attr(int src, int dest); void clone_attr(const char *src, const char *dest); void full_read(int fd, std::string &str); void full_read(const char *filename, std::string &str); std::string full_read(int fd); std::string full_read(const char *filename); void write_zero(int fd, size_t size); void file_readline(bool trim, FILE *fp, const std::function &fn); void file_readline(bool trim, const char *file, const std::function &fn); void file_readline(const char *file, const std::function &fn); void parse_prop_file(FILE *fp, const std::function &fn); void parse_prop_file(const char *file, const std::function &fn); void frm_rf(int dirfd); void clone_dir(int src, int dest); std::vector parse_mount_info(const char *pid); std::string find_apk_path(const char *pkg); std::string resolve_preinit_dir(const char *base_dir); using sFILE = std::unique_ptr; using sDIR = std::unique_ptr; sDIR make_dir(DIR *dp); sFILE make_file(FILE *fp); static inline sDIR open_dir(const char *path) { return make_dir(opendir(path)); } static inline sDIR xopen_dir(const char *path) { return make_dir(xopendir(path)); } static inline sDIR xopen_dir(int dirfd) { return make_dir(xfdopendir(dirfd)); } static inline sFILE open_file(const char *path, const char *mode) { return make_file(fopen(path, mode)); } static inline sFILE xopen_file(const char *path, const char *mode) { return make_file(xfopen(path, mode)); } static inline sFILE xopen_file(int fd, const char *mode) { return make_file(xfdopen(fd, mode)); }