2019-07-02 05:58:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-12-13 05:37:06 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2019-11-23 22:18:55 +00:00
|
|
|
#include <mntent.h>
|
2019-11-19 07:04:47 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <string_view>
|
2020-04-01 11:39:28 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-11-19 07:04:47 +00:00
|
|
|
|
2020-03-09 08:50:30 +00:00
|
|
|
#include "xwrap.hpp"
|
2019-12-13 05:37:06 +00:00
|
|
|
|
2021-11-30 03:56:37 +00:00
|
|
|
template <typename T>
|
|
|
|
static inline T align_to(T v, int a) {
|
|
|
|
static_assert(std::is_integral<T>::value);
|
|
|
|
return (v + a - 1) / a * a;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline T align_padding(T v, int a) {
|
|
|
|
return align_to(v, a) - v;
|
|
|
|
}
|
2019-07-02 05:58:19 +00:00
|
|
|
|
|
|
|
struct file_attr {
|
2020-12-31 06:11:24 +00:00
|
|
|
struct stat st;
|
|
|
|
char con[128];
|
2019-07-02 05:58:19 +00:00
|
|
|
};
|
|
|
|
|
2020-04-01 11:39:28 +00:00
|
|
|
struct raw_file {
|
2020-12-31 06:11:24 +00:00
|
|
|
std::string path;
|
|
|
|
file_attr attr;
|
2021-01-13 06:50:55 +00:00
|
|
|
uint8_t *buf;
|
|
|
|
size_t sz;
|
2020-12-31 06:11:24 +00:00
|
|
|
|
2021-01-13 06:50:55 +00:00
|
|
|
raw_file() : attr({}), buf(nullptr), sz(0) {}
|
2020-12-31 06:11:24 +00:00
|
|
|
raw_file(const raw_file&) = delete;
|
2021-01-13 06:50:55 +00:00
|
|
|
raw_file(raw_file &&o);
|
|
|
|
~raw_file();
|
2020-04-01 11:39:28 +00:00
|
|
|
};
|
|
|
|
|
2019-07-02 05:58:19 +00:00
|
|
|
ssize_t fd_path(int fd, char *path, size_t size);
|
|
|
|
int fd_pathat(int dirfd, const char *name, char *path, size_t size);
|
2021-09-16 12:27:34 +00:00
|
|
|
int mkdirs(std::string path, mode_t mode);
|
2019-07-02 05:58:19 +00:00
|
|
|
void rm_rf(const char *path);
|
2020-04-02 09:17:45 +00:00
|
|
|
void mv_path(const char *src, const char *dest);
|
2019-07-02 05:58:19 +00:00
|
|
|
void mv_dir(int src, int dest);
|
2020-04-02 09:17:45 +00:00
|
|
|
void cp_afc(const char *src, const char *dest);
|
|
|
|
void link_path(const char *src, const char *dest);
|
2019-07-02 05:58:19 +00:00
|
|
|
void link_dir(int src, int dest);
|
2020-04-02 09:17:45 +00:00
|
|
|
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);
|
2019-07-02 05:58:19 +00:00
|
|
|
void fd_full_read(int fd, void **buf, size_t *size);
|
|
|
|
void full_read(const char *filename, void **buf, size_t *size);
|
2020-04-26 06:19:36 +00:00
|
|
|
std::string fd_full_read(int fd);
|
|
|
|
std::string full_read(const char *filename);
|
2019-07-02 05:58:19 +00:00
|
|
|
void write_zero(int fd, size_t size);
|
2019-12-13 05:37:06 +00:00
|
|
|
void file_readline(bool trim, const char *file, const std::function<bool(std::string_view)> &fn);
|
|
|
|
static inline void file_readline(const char *file,
|
2020-12-31 06:11:24 +00:00
|
|
|
const std::function<bool(std::string_view)> &fn) {
|
|
|
|
file_readline(false, file, fn);
|
2019-12-13 05:37:06 +00:00
|
|
|
}
|
|
|
|
void parse_prop_file(const char *file,
|
2020-12-31 06:11:24 +00:00
|
|
|
const std::function<bool(std::string_view, std::string_view)> &fn);
|
2019-07-02 05:58:19 +00:00
|
|
|
void *__mmap(const char *filename, size_t *size, bool rw);
|
2020-04-02 06:37:11 +00:00
|
|
|
void frm_rf(int dirfd);
|
2020-04-02 09:17:45 +00:00
|
|
|
void clone_dir(int src, int dest);
|
2019-11-23 22:18:55 +00:00
|
|
|
void parse_mnt(const char *file, const std::function<bool(mntent*)> &fn);
|
2020-04-01 11:39:28 +00:00
|
|
|
void backup_folder(const char *dir, std::vector<raw_file> &files);
|
|
|
|
void restore_folder(const char *dir, std::vector<raw_file> &files);
|
2019-07-02 05:58:19 +00:00
|
|
|
|
2019-07-16 08:08:28 +00:00
|
|
|
template <typename T>
|
|
|
|
void full_read(const char *filename, T &buf, size_t &size) {
|
2020-12-31 06:11:24 +00:00
|
|
|
static_assert(std::is_pointer<T>::value);
|
|
|
|
full_read(filename, reinterpret_cast<void**>(&buf), &size);
|
2019-07-16 08:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void fd_full_read(int fd, T &buf, size_t &size) {
|
2020-12-31 06:11:24 +00:00
|
|
|
static_assert(std::is_pointer<T>::value);
|
|
|
|
fd_full_read(fd, reinterpret_cast<void**>(&buf), &size);
|
2019-07-16 08:08:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 05:58:19 +00:00
|
|
|
template <typename B>
|
|
|
|
void mmap_ro(const char *filename, B &buf, size_t &sz) {
|
2020-12-31 06:11:24 +00:00
|
|
|
buf = (B) __mmap(filename, &sz, false);
|
2019-07-02 05:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename B, typename L>
|
|
|
|
void mmap_ro(const char *filename, B &buf, L &sz) {
|
2020-12-31 06:11:24 +00:00
|
|
|
size_t __sz;
|
|
|
|
buf = (B) __mmap(filename, &__sz, false);
|
|
|
|
sz = __sz;
|
2019-07-02 05:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename B>
|
|
|
|
void mmap_rw(const char *filename, B &buf, size_t &sz) {
|
2020-12-31 06:11:24 +00:00
|
|
|
buf = (B) __mmap(filename, &sz, true);
|
2019-07-02 05:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename B, typename L>
|
|
|
|
void mmap_rw(const char *filename, B &buf, L &sz) {
|
2020-12-31 06:11:24 +00:00
|
|
|
size_t __sz;
|
|
|
|
buf = (B) __mmap(filename, &__sz, true);
|
|
|
|
sz = __sz;
|
2019-07-02 05:58:19 +00:00
|
|
|
}
|
2019-12-13 05:37:06 +00:00
|
|
|
|
|
|
|
using sFILE = std::unique_ptr<FILE, decltype(&fclose)>;
|
|
|
|
using sDIR = std::unique_ptr<DIR, decltype(&closedir)>;
|
2020-12-04 04:15:18 +00:00
|
|
|
sDIR make_dir(DIR *dp);
|
|
|
|
sFILE make_file(FILE *fp);
|
2019-12-13 05:37:06 +00:00
|
|
|
|
|
|
|
static inline sDIR open_dir(const char *path) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return make_dir(opendir(path));
|
2019-12-13 05:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline sDIR xopen_dir(const char *path) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return make_dir(xopendir(path));
|
2019-12-13 05:37:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 11:39:28 +00:00
|
|
|
static inline sDIR xopen_dir(int dirfd) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return make_dir(xfdopendir(dirfd));
|
2020-04-01 11:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:37:06 +00:00
|
|
|
static inline sFILE open_file(const char *path, const char *mode) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return make_file(fopen(path, mode));
|
2019-12-13 05:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline sFILE xopen_file(const char *path, const char *mode) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return make_file(xfopen(path, mode));
|
2019-12-13 05:37:06 +00:00
|
|
|
}
|
2020-06-19 10:52:25 +00:00
|
|
|
|
|
|
|
static inline sFILE xopen_file(int fd, const char *mode) {
|
2020-12-31 06:11:24 +00:00
|
|
|
return make_file(xfdopen(fd, mode));
|
2020-06-19 10:52:25 +00:00
|
|
|
}
|