Rename Array to Vector

Finally get rid of the C style vector, rename the template class to its proper name
This commit is contained in:
topjohnwu 2018-11-08 05:03:59 -05:00
parent b6965105b7
commit 8745c7884e
15 changed files with 52 additions and 56 deletions

View File

@ -24,7 +24,7 @@
#include "flags.h"
static char buf[PATH_MAX], buf2[PATH_MAX];
static Array<CharArray> module_list;
static Vector<CharArray> 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<node_entry *> children;
Vector<node_entry *> 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<CharArray> mounts;
file_to_array("/proc/mounts", mounts);
Vector<CharArray> 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<CharArray> mounts;
file_to_array("/proc/mounts", mounts);
Vector<CharArray> mounts;
file_to_vector("/proc/mounts", mounts);
bool system_as_root = false;
for (auto &line : mounts) {
if (line.contains(" /system_root ")) {

View File

@ -19,7 +19,7 @@
#include "flags.h"
bool log_daemon_started = false;
static Array<const char *> log_cmd, clear_cmd;
static Vector<const char *> log_cmd, clear_cmd;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
enum {

View File

@ -56,7 +56,7 @@ cpio_entry::~cpio_entry() {
// Define the way to sort cpio_entry
template<>
int(*Array<cpio_entry*>::_cmp)(cpio_entry*&, cpio_entry*&) = [](auto a, auto b) -> int {
int(*Vector<cpio_entry*>::_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<cpio_entry *> &arr) {
void cpio::insert(Vector<cpio_entry *> &arr) {
for (auto &e : arr)
insert(e);
}

View File

@ -3,7 +3,7 @@
#include <stdint.h>
#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<cpio_entry *> &arr);
void insert(Vector<cpio_entry *> &arr);
bool mv(const char *from, const char *to);
void extract();
bool extract(const char *name, const char *file);
void sort();
protected:
Array<cpio_entry *> arr;
Vector<cpio_entry *> arr;
};
#endif

View File

@ -3,7 +3,6 @@
#include <sys/stat.h>
#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<cpio_entry*> &bak, const char *orig, const char *sha1);
void backup(Vector<cpio_entry*> &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<cpio_entry*> &bak, const char *orig, const char *sha1) {
void magisk_cpio::backup(Vector<cpio_entry*> &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<cpio_entry*> bak;
Vector<cpio_entry*> 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<cpio_entry*> bak;
Vector<cpio_entry*> bak;
cpio.backup(bak, cmdv[1], cmdv[4]);
auto e = new cpio_entry();

View File

@ -15,7 +15,7 @@
#include "daemon.h"
#include "db.h"
Array<CharArray> hide_list;
Vector<CharArray> 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<CharArray> tmp;
file_to_array(LEGACY_LIST, tmp);
Vector<CharArray> tmp;
file_to_vector(LEGACY_LIST, tmp);
for (auto &s : tmp)
add_list(db, s);
unlink(LEGACY_LIST);

View File

@ -4,7 +4,7 @@
#include <pthread.h>
#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<CharArray> hide_list;
extern Vector<CharArray> hide_list;
enum {
LAUNCH_MAGISKHIDE,

View File

@ -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<CharArray> mounts;
Vector<CharArray> 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) {

View File

@ -92,7 +92,7 @@ static const char *type_msg_5 =
exit(1);
}
static int parse_bracket(char *tok, char *&stmt, Array<const char *> *vec) {
static int parse_bracket(char *tok, char *&stmt, Vector<const char *> *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<const char*> source, target, permission;
Vector<const char*> source, target, permission;
while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) {
if (cur[0] == '*') cur = ALL;
Array<const char *> *vec;
Vector<const char *> *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<const char *> source, target, classes;
Vector<const char *> source, target, classes;
while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) {
if (cur[0] == '*') cur = ALL;
Array<const char *> *vec;
Vector<const char *> *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<const char *> domains;
Vector<const char *> 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<const char *> classes, attribute;
Vector<const char *> classes, attribute;
while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) {
if (cur[0] == '*') cur = ALL;
Array<const char *> *vec;
Vector<const char *> *vec;
switch (state) {
case 0:
vec = &classes;

View File

@ -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_t> &prop_list = *(Array<prop_t> *) *arg;
Vector<prop_t> &prop_list = *(Vector<prop_t> *) *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_t> prop_list;
Vector<prop_t> prop_list;
read_cb_t read_cb(collect_props, &prop_list);
persist_getprop(&read_cb);

View File

@ -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<prop_t>::_cmp)(prop_t&, prop_t&) = [](auto a, auto b) -> int {
int(*Vector<prop_t>::_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_t> &prop_list = *static_cast<Array<prop_t> *>(v_plist);
Vector<prop_t> &prop_list = *static_cast<Vector<prop_t> *>(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_t> &prop_list = *static_cast<Array<prop_t> *>(v_plist);
Vector<prop_t> &prop_list = *static_cast<Vector<prop_t> *>(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_t> prop_list;
Vector<prop_t> prop_list;
getprop(collect_props, &prop_list, persist);
prop_list.sort();
for (auto &prop : prop_list)

View File

@ -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<CharArray> &arr) {
int file_to_vector(const char *filename, Vector<CharArray> &arr) {
if (access(filename, R_OK) != 0)
return 1;
char *line = nullptr;

View File

@ -4,13 +4,13 @@
#include "cpputils.h"
template <class T>
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<class T>
int(* Array<T>::_cmp)(T&, T&) = nullptr;
int(* Vector<T>::_cmp)(T&, T&) = nullptr;

View File

@ -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<CharArray> &arr);
int file_to_vector(const char *filename, Vector<CharArray> &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))

View File

@ -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<const char *> args;
Vector<const char *> args;
args.push_back(argv0);
for (const char *arg = va_arg(argv, char*); arg; arg = va_arg(argv, char*))
args.push_back(arg);