From 741b679306043238aee368c76e55858b610bdfe6 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 17 Jun 2022 02:36:04 -0700 Subject: [PATCH] Cleanup libbase --- native/jni/base/files.cpp | 39 +++++++++---------------------------- native/jni/base/files.hpp | 24 +++++------------------ native/jni/init/rootdir.cpp | 2 +- 3 files changed, 15 insertions(+), 50 deletions(-) diff --git a/native/jni/base/files.cpp b/native/jni/base/files.cpp index 1105c2395..e98280bd1 100644 --- a/native/jni/base/files.cpp +++ b/native/jni/base/files.cpp @@ -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 &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 &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); } diff --git a/native/jni/base/files.hpp b/native/jni/base/files.hpp index 8e005fe03..2783a360d 100644 --- a/native/jni/base/files.hpp +++ b/native/jni/base/files.hpp @@ -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 &fn); @@ -103,18 +101,6 @@ void backup_folder(const char *dir, std::vector &files); void restore_folder(const char *dir, std::vector &files); std::string find_apk_path(const char *pkg); -template -void full_read(const char *filename, T &buf, size_t &size) { - static_assert(std::is_pointer::value); - full_read(filename, reinterpret_cast(&buf), &size); -} - -template -void fd_full_read(int fd, T &buf, size_t &size) { - static_assert(std::is_pointer::value); - fd_full_read(fd, reinterpret_cast(&buf), &size); -} - using sFILE = std::unique_ptr; using sDIR = std::unique_ptr; sDIR make_dir(DIR *dp); diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index 759ba7284..d7b2f90bb 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -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); }