mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-11-23 18:15:30 +00:00
Cleanup libbase
This commit is contained in:
parent
90013e486d
commit
741b679306
@ -286,26 +286,7 @@ void fclone_attr(int src, int dest) {
|
||||
fsetattr(dest, &a);
|
||||
}
|
||||
|
||||
void fd_full_read(int fd, void **buf, size_t *size) {
|
||||
*size = lseek(fd, 0, SEEK_END);
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
*buf = xmalloc(*size + 1);
|
||||
xxread(fd, *buf, *size);
|
||||
((char *) *buf)[*size] = '\0';
|
||||
}
|
||||
|
||||
void full_read(const char *filename, void **buf, size_t *size) {
|
||||
int fd = xopen(filename, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
*buf = nullptr;
|
||||
*size = 0;
|
||||
return;
|
||||
}
|
||||
fd_full_read(fd, buf, size);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void fd_full_read(int fd, string &str) {
|
||||
void full_read(int fd, string &str) {
|
||||
char buf[4096];
|
||||
for (ssize_t len; (len = xread(fd, buf, sizeof(buf))) > 0;)
|
||||
str.insert(str.end(), buf, buf + len);
|
||||
@ -313,14 +294,14 @@ void fd_full_read(int fd, string &str) {
|
||||
|
||||
void full_read(const char *filename, string &str) {
|
||||
if (int fd = xopen(filename, O_RDONLY | O_CLOEXEC); fd >= 0) {
|
||||
fd_full_read(fd, str);
|
||||
full_read(fd, str);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
string fd_full_read(int fd) {
|
||||
string full_read(int fd) {
|
||||
string str;
|
||||
fd_full_read(fd, str);
|
||||
full_read(fd, str);
|
||||
return str;
|
||||
}
|
||||
|
||||
@ -429,12 +410,10 @@ void backup_folder(const char *dir, vector<raw_file> &files) {
|
||||
if (fgetattr(fd, &file.attr) < 0)
|
||||
return SKIP;
|
||||
if (entry->d_type == DT_REG) {
|
||||
fd_full_read(fd, file.buf, file.sz);
|
||||
file.content = full_read(fd);
|
||||
} else if (entry->d_type == DT_LNK) {
|
||||
xreadlinkat(dfd, entry->d_name, path, sizeof(path));
|
||||
file.sz = strlen(path) + 1;
|
||||
file.buf = (uint8_t *) xmalloc(file.sz);
|
||||
memcpy(file.buf, path, file.sz);
|
||||
file.content = path;
|
||||
}
|
||||
files.emplace_back(std::move(file));
|
||||
return CONTINUE;
|
||||
@ -449,10 +428,10 @@ void restore_folder(const char *dir, vector<raw_file> &files) {
|
||||
if (S_ISDIR(file.attr.st.st_mode)) {
|
||||
mkdirs(path, 0);
|
||||
} else if (S_ISREG(file.attr.st.st_mode)) {
|
||||
auto fp = xopen_file(path.data(), "we");
|
||||
if (fp) fwrite(file.buf, 1, file.sz, fp.get());
|
||||
if (auto fp = xopen_file(path.data(), "we"))
|
||||
fwrite(file.content.data(), 1, file.content.size(), fp.get());
|
||||
} else if (S_ISLNK(file.attr.st.st_mode)) {
|
||||
symlink((char *)file.buf, path.data());
|
||||
symlink(file.content.data(), path.data());
|
||||
}
|
||||
setattr(path.data(), &file.attr);
|
||||
}
|
||||
|
@ -39,14 +39,14 @@ protected:
|
||||
void swap(byte_data &o);
|
||||
};
|
||||
|
||||
struct raw_file : public byte_data {
|
||||
struct raw_file {
|
||||
std::string path;
|
||||
file_attr attr;
|
||||
std::string content;
|
||||
|
||||
raw_file() : attr{} {}
|
||||
raw_file(const raw_file&) = delete;
|
||||
raw_file(raw_file &&o) : path(std::move(o.path)), attr(o.attr) { swap(o); }
|
||||
~raw_file() { free(buf); }
|
||||
raw_file(raw_file &&o) : path(std::move(o.path)), attr(o.attr), content(std::move(o.content)) {}
|
||||
};
|
||||
|
||||
struct mmap_data : public byte_data {
|
||||
@ -75,11 +75,9 @@ 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 fd_full_read(int fd, void **buf, size_t *size);
|
||||
void full_read(const char *filename, void **buf, size_t *size);
|
||||
void fd_full_read(int fd, std::string &str);
|
||||
void full_read(int fd, std::string &str);
|
||||
void full_read(const char *filename, std::string &str);
|
||||
std::string fd_full_read(int fd);
|
||||
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<bool(std::string_view)> &fn);
|
||||
@ -103,18 +101,6 @@ void backup_folder(const char *dir, std::vector<raw_file> &files);
|
||||
void restore_folder(const char *dir, std::vector<raw_file> &files);
|
||||
std::string find_apk_path(const char *pkg);
|
||||
|
||||
template <typename T>
|
||||
void full_read(const char *filename, T &buf, size_t &size) {
|
||||
static_assert(std::is_pointer<T>::value);
|
||||
full_read(filename, reinterpret_cast<void**>(&buf), &size);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void fd_full_read(int fd, T &buf, size_t &size) {
|
||||
static_assert(std::is_pointer<T>::value);
|
||||
fd_full_read(fd, reinterpret_cast<void**>(&buf), &size);
|
||||
}
|
||||
|
||||
using sFILE = std::unique_ptr<FILE, decltype(&fclose)>;
|
||||
using sDIR = std::unique_ptr<DIR, decltype(&closedir)>;
|
||||
sDIR make_dir(DIR *dp);
|
||||
|
@ -72,7 +72,7 @@ static void load_overlay_rc(const char *overlay) {
|
||||
if (str_ends(entry->d_name, ".rc")) {
|
||||
LOGD("Found rc script [%s]\n", entry->d_name);
|
||||
int rc = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC);
|
||||
rc_list.push_back(fd_full_read(rc));
|
||||
rc_list.push_back(full_read(rc));
|
||||
close(rc);
|
||||
unlinkat(dfd, entry->d_name, 0);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user