From 8745c7884ea4bb7afd7688301d28d3ba7f74584c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 8 Nov 2018 05:03:59 -0500 Subject: [PATCH] Rename Array to Vector Finally get rid of the C style vector, rename the template class to its proper name --- native/jni/daemon/bootstages.cpp | 12 ++++++------ native/jni/daemon/log_daemon.cpp | 2 +- native/jni/magiskboot/cpio.cpp | 4 ++-- native/jni/magiskboot/cpio.h | 6 +++--- native/jni/magiskboot/ramdisk.cpp | 9 ++++----- native/jni/magiskhide/hide_utils.cpp | 6 +++--- native/jni/magiskhide/magiskhide.h | 4 ++-- native/jni/magiskhide/proc_monitor.cpp | 6 +++--- native/jni/magiskpolicy/magiskpolicy.cpp | 16 ++++++++-------- native/jni/resetprop/persist_properties.cpp | 5 ++--- native/jni/resetprop/resetprop.cpp | 9 ++++----- native/jni/utils/file.cpp | 2 +- native/jni/utils/include/{array.h => Vector.h} | 14 +++++++------- native/jni/utils/include/utils.h | 11 +++++------ native/jni/utils/misc.cpp | 2 +- 15 files changed, 52 insertions(+), 56 deletions(-) rename native/jni/utils/include/{array.h => Vector.h} (93%) diff --git a/native/jni/daemon/bootstages.cpp b/native/jni/daemon/bootstages.cpp index 8c88e6ded..20735471f 100644 --- a/native/jni/daemon/bootstages.cpp +++ b/native/jni/daemon/bootstages.cpp @@ -24,7 +24,7 @@ #include "flags.h" static char buf[PATH_MAX], buf2[PATH_MAX]; -static Array module_list; +static Vector module_list; static int bind_mount(const char *from, const char *to); @@ -56,7 +56,7 @@ private: uint8_t type; uint8_t status; node_entry *parent; - Array children; + Vector children; node_entry(const char *, const char *, uint8_t type); bool is_root(); @@ -478,8 +478,8 @@ static void install_apk(const char *apk) { static bool check_data() { bool mnt = false; bool data = false; - Array mounts; - file_to_array("/proc/mounts", mounts); + Vector mounts; + file_to_vector("/proc/mounts", mounts); for (auto &line : mounts) { if (line.contains(" /data ") && !line.contains("tmpfs")) mnt = true; @@ -705,8 +705,8 @@ void startup() { xmkdir(BLOCKDIR, 0755); LOGI("* Mounting mirrors"); - Array mounts; - file_to_array("/proc/mounts", mounts); + Vector mounts; + file_to_vector("/proc/mounts", mounts); bool system_as_root = false; for (auto &line : mounts) { if (line.contains(" /system_root ")) { diff --git a/native/jni/daemon/log_daemon.cpp b/native/jni/daemon/log_daemon.cpp index 75e5236f5..36923aeaf 100644 --- a/native/jni/daemon/log_daemon.cpp +++ b/native/jni/daemon/log_daemon.cpp @@ -19,7 +19,7 @@ #include "flags.h" bool log_daemon_started = false; -static Array log_cmd, clear_cmd; +static Vector log_cmd, clear_cmd; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; enum { diff --git a/native/jni/magiskboot/cpio.cpp b/native/jni/magiskboot/cpio.cpp index 183bdb51b..fc03e2754 100644 --- a/native/jni/magiskboot/cpio.cpp +++ b/native/jni/magiskboot/cpio.cpp @@ -56,7 +56,7 @@ cpio_entry::~cpio_entry() { // Define the way to sort cpio_entry template<> -int(*Array::_cmp)(cpio_entry*&, cpio_entry*&) = [](auto a, auto b) -> int { +int(*Vector::_cmp)(cpio_entry*&, cpio_entry*&) = [](auto a, auto b) -> int { if (a == b) return 0; if (a == nullptr) return 1; if (b == nullptr) return -1; @@ -148,7 +148,7 @@ void cpio::insert(cpio_entry *e) { } } -void cpio::insert(Array &arr) { +void cpio::insert(Vector &arr) { for (auto &e : arr) insert(e); } diff --git a/native/jni/magiskboot/cpio.h b/native/jni/magiskboot/cpio.h index fee9f200d..497e472d7 100644 --- a/native/jni/magiskboot/cpio.h +++ b/native/jni/magiskboot/cpio.h @@ -3,7 +3,7 @@ #include -#include "array.h" +#include "Vector.h" #include "CharArray.h" struct cpio_newc_header { @@ -57,14 +57,14 @@ public: void makedir(mode_t mode, const char *name); void ln(const char *target, const char *name); void add(mode_t mode, const char *name, const char *file); - void insert(Array &arr); + void insert(Vector &arr); bool mv(const char *from, const char *to); void extract(); bool extract(const char *name, const char *file); void sort(); protected: - Array arr; + Vector arr; }; #endif diff --git a/native/jni/magiskboot/ramdisk.cpp b/native/jni/magiskboot/ramdisk.cpp index 07533862d..6de04637b 100644 --- a/native/jni/magiskboot/ramdisk.cpp +++ b/native/jni/magiskboot/ramdisk.cpp @@ -3,7 +3,6 @@ #include #include "magiskboot.h" -#include "array.h" #include "cpio.h" #include "utils.h" @@ -14,7 +13,7 @@ public: int test(); char * sha1(); void restore(); - void backup(Array &bak, const char *orig, const char *sha1); + void backup(Vector &bak, const char *orig, const char *sha1); }; void magisk_cpio::patch(bool keepverity, bool keepforceencrypt) { @@ -110,7 +109,7 @@ void magisk_cpio::restore() { rm("magisk", true); } -void magisk_cpio::backup(Array &bak, const char *orig, const char *sha1) { +void magisk_cpio::backup(Vector &bak, const char *orig, const char *sha1) { cpio_entry *m, *n, *rem, *cksm; char buf[PATH_MAX]; @@ -230,13 +229,13 @@ int cpio_commands(int argc, char *argv[]) { free(sha1); return 0; } else if (cmdc >= 2 && strcmp(cmdv[0], "backup") == 0) { - Array bak; + Vector bak; cpio.backup(bak, cmdv[1], cmdv[2]); cpio.insert(bak); } else if (cmdc >= 4 && strcmp(cmdv[0], "magisk") == 0) { cpio.patch(strcmp(cmdv[2], "true") == 0, strcmp(cmdv[3], "true") == 0); - Array bak; + Vector bak; cpio.backup(bak, cmdv[1], cmdv[4]); auto e = new cpio_entry(); diff --git a/native/jni/magiskhide/hide_utils.cpp b/native/jni/magiskhide/hide_utils.cpp index b50b5c45b..1879177c1 100644 --- a/native/jni/magiskhide/hide_utils.cpp +++ b/native/jni/magiskhide/hide_utils.cpp @@ -15,7 +15,7 @@ #include "daemon.h" #include "db.h" -Array hide_list; +Vector hide_list; static const char *prop_key[] = { "ro.boot.vbmeta.device_state", "ro.boot.verifiedbootstate", "ro.boot.flash.locked", @@ -232,8 +232,8 @@ bool init_list() { // Migrate old hide list into database if (access(LEGACY_LIST, R_OK) == 0) { - Array tmp; - file_to_array(LEGACY_LIST, tmp); + Vector tmp; + file_to_vector(LEGACY_LIST, tmp); for (auto &s : tmp) add_list(db, s); unlink(LEGACY_LIST); diff --git a/native/jni/magiskhide/magiskhide.h b/native/jni/magiskhide/magiskhide.h index af2c0f7dc..f180377c1 100644 --- a/native/jni/magiskhide/magiskhide.h +++ b/native/jni/magiskhide/magiskhide.h @@ -4,7 +4,7 @@ #include #include "daemon.h" -#include "array.h" +#include "Vector.h" #include "CharArray.h" #define TERM_THREAD SIGUSR1 @@ -30,7 +30,7 @@ bool init_list(); extern int hide_enabled; extern pthread_mutex_t list_lock; -extern Array hide_list; +extern Vector hide_list; enum { LAUNCH_MAGISKHIDE, diff --git a/native/jni/magiskhide/proc_monitor.cpp b/native/jni/magiskhide/proc_monitor.cpp index 0f72ee972..a7f7c1556 100644 --- a/native/jni/magiskhide/proc_monitor.cpp +++ b/native/jni/magiskhide/proc_monitor.cpp @@ -63,7 +63,7 @@ static void hide_daemon(int pid) { LOGD("hide_daemon: start unmount for pid=[%d]\n", pid); char buffer[PATH_MAX]; - Array mounts; + Vector mounts; manage_selinux(); clean_magisk_props(); @@ -72,7 +72,7 @@ static void hide_daemon(int pid) { goto exit; snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid); - file_to_array(buffer, mounts); + file_to_vector(buffer, mounts); // Unmount dummy skeletons and /sbin links for (auto &s : mounts) { @@ -85,7 +85,7 @@ static void hide_daemon(int pid) { // Re-read mount infos snprintf(buffer, sizeof(buffer), "/proc/%d/mounts", pid); - file_to_array(buffer, mounts); + file_to_vector(buffer, mounts); // Unmount everything under /system, /vendor, and loop mounts for (auto &s : mounts) { diff --git a/native/jni/magiskpolicy/magiskpolicy.cpp b/native/jni/magiskpolicy/magiskpolicy.cpp index dc3c17c1f..43f02140a 100644 --- a/native/jni/magiskpolicy/magiskpolicy.cpp +++ b/native/jni/magiskpolicy/magiskpolicy.cpp @@ -92,7 +92,7 @@ static const char *type_msg_5 = exit(1); } -static int parse_bracket(char *tok, char *&stmt, Array *vec) { +static int parse_bracket(char *tok, char *&stmt, Vector *vec) { if (tok == nullptr || tok[0] != '{') { // Not in a bracket vec->push_back(tok); @@ -134,10 +134,10 @@ static int parse_pattern_1(int action, const char *action_str, char *stmt) { int state = 0; char *cur, *cls; - Array source, target, permission; + Vector source, target, permission; while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) { if (cur[0] == '*') cur = ALL; - Array *vec; + Vector *vec; switch (state) { case 0: vec = &source; @@ -191,10 +191,10 @@ static int parse_pattern_2(int action, const char *action_str, char *stmt) { int state = 0; char *cur, *range; - Array source, target, classes; + Vector source, target, classes; while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) { if (cur[0] == '*') cur = ALL; - Array *vec; + Vector *vec; switch (state) { case 0: vec = &source; @@ -251,7 +251,7 @@ static int parse_pattern_3(int action, const char *action_str, char* stmt) { } char *cur; - Array domains; + Vector domains; while ((cur = strtok_r(nullptr, " {}", &stmt)) != nullptr) { if (cur[0] == '*') cur = ALL; domains.push_back(cur); @@ -280,10 +280,10 @@ static int parse_pattern_4(int action, const char *action_str, char *stmt) { int state = 0; char *cur; - Array classes, attribute; + Vector classes, attribute; while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) { if (cur[0] == '*') cur = ALL; - Array *vec; + Vector *vec; switch (state) { case 0: vec = &classes; diff --git a/native/jni/resetprop/persist_properties.cpp b/native/jni/resetprop/persist_properties.cpp index 701ffef37..1a087d36e 100644 --- a/native/jni/resetprop/persist_properties.cpp +++ b/native/jni/resetprop/persist_properties.cpp @@ -11,7 +11,6 @@ #include "_resetprop.h" #include "utils.h" -#include "array.h" /* *********************************************************************** * Auto generated header and constant definitions compiled from @@ -114,7 +113,7 @@ static bool prop_encode(pb_ostream_t *stream, const pb_field_t *field, void * co PersistentProperties_PersistentPropertyRecord prop = {}; prop.name.funcs.encode = name_encode; prop.has_value = true; - Array &prop_list = *(Array *) *arg; + Vector &prop_list = *(Vector *) *arg; for (auto &p : prop_list) { if (!pb_encode_tag_for_field(stream, field)) return false; @@ -209,7 +208,7 @@ CharArray persist_getprop(const char *name) { bool persist_deleteprop(const char *name) { if (use_pb) { - Array prop_list; + Vector prop_list; read_cb_t read_cb(collect_props, &prop_list); persist_getprop(&read_cb); diff --git a/native/jni/resetprop/resetprop.cpp b/native/jni/resetprop/resetprop.cpp index d181f3e11..8fa0599a1 100644 --- a/native/jni/resetprop/resetprop.cpp +++ b/native/jni/resetprop/resetprop.cpp @@ -15,7 +15,6 @@ #include "resetprop.h" #include "_resetprop.h" #include "utils.h" -#include "array.h" #include "flags.h" bool use_pb = false; @@ -77,7 +76,7 @@ illegal: // Define the way to sort prop_t template<> -int(*Array::_cmp)(prop_t&, prop_t&) = [](auto a, auto b) -> int { +int(*Vector::_cmp)(prop_t&, prop_t&) = [](auto a, auto b) -> int { return strcmp(a.name, b.name); }; @@ -90,12 +89,12 @@ static void read_props(const prop_info *pi, void *read_cb) { } void collect_props(const char *name, const char *value, void *v_plist) { - Array &prop_list = *static_cast *>(v_plist); + Vector &prop_list = *static_cast *>(v_plist); prop_list.push_back(prop_t(name, value)); } static void collect_unique_props(const char *name, const char *value, void *v_plist) { - Array &prop_list = *static_cast *>(v_plist); + Vector &prop_list = *static_cast *>(v_plist); for (auto &prop : prop_list) { if (strcmp(name, prop.name) == 0) return; @@ -113,7 +112,7 @@ static int init_resetprop() { } static void print_props(bool persist) { - Array prop_list; + Vector prop_list; getprop(collect_props, &prop_list, persist); prop_list.sort(); for (auto &prop : prop_list) diff --git a/native/jni/utils/file.cpp b/native/jni/utils/file.cpp index eb32a130a..a1063d6ea 100644 --- a/native/jni/utils/file.cpp +++ b/native/jni/utils/file.cpp @@ -388,7 +388,7 @@ void write_zero(int fd, size_t size) { lseek(fd, pos + size, SEEK_SET); } -int file_to_array(const char *filename, Array &arr) { +int file_to_vector(const char *filename, Vector &arr) { if (access(filename, R_OK) != 0) return 1; char *line = nullptr; diff --git a/native/jni/utils/include/array.h b/native/jni/utils/include/Vector.h similarity index 93% rename from native/jni/utils/include/array.h rename to native/jni/utils/include/Vector.h index c64a70e00..5a8832ad6 100644 --- a/native/jni/utils/include/array.h +++ b/native/jni/utils/include/Vector.h @@ -4,13 +4,13 @@ #include "cpputils.h" template -class Array { +class Vector { public: - Array() : _data(0), _size(0), _capacity(0) {} - ~Array() { delete []_data; } + Vector() : _data(0), _size(0), _capacity(0) {} + ~Vector() { delete []_data; } class iterator { - friend class Array; + friend class Vector; public: iterator(T* n= 0): _node(n) {} @@ -67,7 +67,7 @@ public: T* _node; }; - Array &operator=(const Array& a) { + Vector &operator=(const Vector& a) { delete [] _data; _data = nullptr; _size = a._size; @@ -80,7 +80,7 @@ public: return *this; } - Array &operator=(Array&& a) { + Vector &operator=(Vector&& a) { delete [] _data; _size = a._size; _capacity = a._capacity; @@ -188,4 +188,4 @@ private: }; template -int(* Array::_cmp)(T&, T&) = nullptr; +int(* Vector::_cmp)(T&, T&) = nullptr; diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index 7b4b37ac0..a91480f5e 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -13,14 +13,13 @@ #ifdef __cplusplus -#include "array.h" +#include "Vector.h" #include "CharArray.h" #include "cpputils.h" -int file_to_array(const char *filename, Array &arr); +int file_to_vector(const char *filename, Vector &arr); char *strdup2(const char *s, size_t *size = nullptr); - extern "C" { #endif @@ -29,7 +28,7 @@ extern "C" { #define UID_SYSTEM (get_system_uid()) #define UID_RADIO (get_radio_uid()) -// xwrap.c +// xwrap.cpp FILE *xfopen(const char *pathname, const char *mode); FILE *xfdopen(int fd, const char *mode); @@ -85,7 +84,7 @@ ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count); pid_t xfork(); int xpoll(struct pollfd *fds, nfds_t nfds, int timeout); -// misc.c +// misc.cpp unsigned get_shell_uid(); unsigned get_system_uid(); @@ -108,7 +107,7 @@ ssize_t __getline(char **lineptr, size_t *n, FILE *stream); ssize_t __getdelim(char **lineptr, size_t *n, int delim, FILE *stream); int __fsetxattr(int fd, const char *name, const void *value, size_t size, int flags); -// file.c +// file.cpp #define align(p, a) (((p) + (a) - 1) / (a) * (a)) #define align_off(p, a) (align(p, a) - (p)) diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index 8c9dcfee2..d7b145ed0 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -240,7 +240,7 @@ int exec_array(int err, int *fd, void (*cb)(void), const char *argv[]) { static int v_exec_command(int err, int *fd, void (*cb)(void), const char *argv0, va_list argv) { // Collect va_list into vector - Array args; + Vector args; args.push_back(argv0); for (const char *arg = va_arg(argv, char*); arg; arg = va_arg(argv, char*)) args.push_back(arg);