mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-15 18:20:21 +00:00
Migrate to STL
This commit is contained in:
@@ -5,15 +5,15 @@
|
||||
#ifndef MAGISK_PROPS_H
|
||||
#define MAGISK_PROPS_H
|
||||
|
||||
#include <CharArray.h>
|
||||
#include <string>
|
||||
#include "resetprop/private/system_properties.h"
|
||||
#include "logging.h"
|
||||
|
||||
struct prop_t {
|
||||
char *name;
|
||||
char value[PROP_VALUE_MAX];
|
||||
prop_t() = default;
|
||||
prop_t(const char *name) {
|
||||
prop_t() : name(nullptr) {}
|
||||
explicit prop_t(const char *name) {
|
||||
this->name = strdup(name);
|
||||
value[0] = '\0';
|
||||
}
|
||||
@@ -21,7 +21,13 @@ struct prop_t {
|
||||
this->name = strdup(name);
|
||||
strcpy(this->value, value);
|
||||
}
|
||||
prop_t& operator= (prop_t&& prop) {
|
||||
prop_t(prop_t &&prop): name(nullptr) {
|
||||
operator=(std::move(prop));
|
||||
}
|
||||
bool operator<(const prop_t &prop) const {
|
||||
return strcmp(name, prop.name) < 0;
|
||||
}
|
||||
prop_t& operator= (prop_t &&prop) {
|
||||
if (this != &prop) {
|
||||
free(name);
|
||||
name = prop.name;
|
||||
@@ -38,7 +44,7 @@ struct prop_t {
|
||||
struct read_cb_t {
|
||||
void (*cb)(const char *, const char *, void *);
|
||||
void *arg;
|
||||
read_cb_t(void (*cb)(const char *, const char *, void *) = nullptr, void *arg = nullptr)
|
||||
explicit read_cb_t(void (*cb)(const char *, const char *, void *) = nullptr, void *arg = nullptr)
|
||||
: cb(cb), arg(arg) {}
|
||||
void exec(const char *name, const char *value) {
|
||||
cb(name, value, arg);
|
||||
@@ -49,7 +55,7 @@ struct read_cb_t {
|
||||
|
||||
extern bool use_pb;
|
||||
|
||||
CharArray persist_getprop(const char *name);
|
||||
std::string persist_getprop(const char *name);
|
||||
void persist_getprop(read_cb_t *read_cb);
|
||||
bool persist_deleteprop(const char *name);
|
||||
void collect_props(const char *name, const char *value, void *v_plist);
|
||||
|
@@ -5,6 +5,8 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <pb.h>
|
||||
#include <pb_decode.h>
|
||||
#include <pb_encode.h>
|
||||
@@ -12,6 +14,8 @@
|
||||
#include "_resetprop.h"
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ***********************************************************************
|
||||
* Auto generated header and constant definitions compiled from
|
||||
* android/platform/system/core/master/init/persistent_properties.proto
|
||||
@@ -113,7 +117,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;
|
||||
Vector<prop_t> &prop_list = *(Vector<prop_t> *) *arg;
|
||||
auto &prop_list = *(vector<prop_t> *) *arg;
|
||||
for (auto &p : prop_list) {
|
||||
if (!pb_encode_tag_for_field(stream, field))
|
||||
return false;
|
||||
@@ -160,16 +164,16 @@ static void pb_getprop(read_cb_t *read_cb) {
|
||||
munmap(buf, size);
|
||||
}
|
||||
|
||||
static void file_getprop(const char *name, char *value) {
|
||||
value[0] = '\0';
|
||||
static bool file_getprop(const char *name, char *value) {
|
||||
char path[PATH_MAX];
|
||||
snprintf(path, sizeof(path), PERSISTENT_PROPERTY_DIR "/%s", name);
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return;
|
||||
return false;
|
||||
LOGD("resetprop: read prop from [%s]\n", path);
|
||||
value[read(fd, value, sizeof(PROP_VALUE_MAX))] = '\0'; // Null terminate the read value
|
||||
value[read(fd, value, PROP_VALUE_MAX)] = '\0'; // Null terminate the read value
|
||||
close(fd);
|
||||
return value[0] != '\0';
|
||||
}
|
||||
|
||||
void persist_getprop(read_cb_t *read_cb) {
|
||||
@@ -182,14 +186,13 @@ void persist_getprop(read_cb_t *read_cb) {
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0 )
|
||||
continue;
|
||||
char value[PROP_VALUE_MAX];
|
||||
file_getprop(entry->d_name, value);
|
||||
if (value[0])
|
||||
if (file_getprop(entry->d_name, value))
|
||||
read_cb->exec(entry->d_name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CharArray persist_getprop(const char *name) {
|
||||
string persist_getprop(const char *name) {
|
||||
prop_t prop(name);
|
||||
if (use_pb) {
|
||||
read_cb_t read_cb(pb_getprop_cb, &prop);
|
||||
@@ -199,21 +202,20 @@ CharArray persist_getprop(const char *name) {
|
||||
} else {
|
||||
// Try to read from file
|
||||
char value[PROP_VALUE_MAX];
|
||||
file_getprop(name, value);
|
||||
if (value[0])
|
||||
if (file_getprop(name, value))
|
||||
return value;
|
||||
}
|
||||
return CharArray();
|
||||
return string();
|
||||
}
|
||||
|
||||
bool persist_deleteprop(const char *name) {
|
||||
if (use_pb) {
|
||||
Vector<prop_t> prop_list;
|
||||
vector<prop_t> prop_list;
|
||||
read_cb_t read_cb(collect_props, &prop_list);
|
||||
persist_getprop(&read_cb);
|
||||
|
||||
for (auto it = prop_list.begin(); it != prop_list.end(); ++it) {
|
||||
if (strcmp((*it).name, name) == 0) {
|
||||
if (strcmp(it->name, name) == 0) {
|
||||
prop_list.erase(it);
|
||||
// Dump the props back
|
||||
PersistentProperties props = PersistentProperties_init_zero;
|
||||
|
@@ -6,6 +6,8 @@
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
||||
#include "private/_system_properties.h"
|
||||
@@ -17,6 +19,8 @@
|
||||
#include "utils.h"
|
||||
#include "flags.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool use_pb = false;
|
||||
static bool verbose = false;
|
||||
|
||||
@@ -74,12 +78,6 @@ illegal:
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Define the way to sort prop_t
|
||||
template<>
|
||||
int(*Vector<prop_t>::_cmp)(prop_t&, prop_t&) = [](auto a, auto b) -> int {
|
||||
return strcmp(a.name, b.name);
|
||||
};
|
||||
|
||||
static void read_props(const prop_info *pi, void *read_cb) {
|
||||
__system_property_read_callback(
|
||||
pi, [](auto cb, auto name, auto value, auto) -> void
|
||||
@@ -89,12 +87,12 @@ static void read_props(const prop_info *pi, void *read_cb) {
|
||||
}
|
||||
|
||||
void collect_props(const char *name, const char *value, void *v_plist) {
|
||||
Vector<prop_t> &prop_list = *static_cast<Vector<prop_t> *>(v_plist);
|
||||
prop_list.push_back(prop_t(name, value));
|
||||
auto &prop_list = *static_cast<vector<prop_t> *>(v_plist);
|
||||
prop_list.emplace_back(name, value);
|
||||
}
|
||||
|
||||
static void collect_unique_props(const char *name, const char *value, void *v_plist) {
|
||||
Vector<prop_t> &prop_list = *static_cast<Vector<prop_t> *>(v_plist);
|
||||
auto &prop_list = *static_cast<vector<prop_t> *>(v_plist);
|
||||
for (auto &prop : prop_list) {
|
||||
if (strcmp(name, prop.name) == 0)
|
||||
return;
|
||||
@@ -112,9 +110,9 @@ static int init_resetprop() {
|
||||
}
|
||||
|
||||
static void print_props(bool persist) {
|
||||
Vector<prop_t> prop_list;
|
||||
vector<prop_t> prop_list;
|
||||
getprop(collect_props, &prop_list, persist);
|
||||
prop_list.sort();
|
||||
sort(prop_list.begin(), prop_list.end());
|
||||
for (auto &prop : prop_list)
|
||||
printf("[%s]: [%s]\n", prop.name, prop.value);
|
||||
}
|
||||
@@ -128,19 +126,19 @@ int prop_exist(const char *name) {
|
||||
return __system_property_find(name) != nullptr;
|
||||
}
|
||||
|
||||
// Get prop by name, return string (should free manually!)
|
||||
CharArray getprop(const char *name, bool persist) {
|
||||
// Get prop by name, return string
|
||||
string getprop(const char *name, bool persist) {
|
||||
if (!check_legal_property_name(name) || init_resetprop())
|
||||
return nullptr;
|
||||
const prop_info *pi = __system_property_find(name);
|
||||
if (pi == nullptr) {
|
||||
if (persist && strncmp(name, "persist.", 8) == 0) {
|
||||
CharArray value = persist_getprop(name);
|
||||
if (!value.empty())
|
||||
return value;
|
||||
auto value = persist_getprop(name);
|
||||
if (value.empty())
|
||||
LOGD("resetprop: prop [%s] does not exist\n", name);
|
||||
return value;
|
||||
}
|
||||
LOGD("resetprop: prop [%s] does not exist\n", name);
|
||||
return CharArray();
|
||||
return string();
|
||||
} else {
|
||||
char value[PROP_VALUE_MAX];
|
||||
read_cb_t read_cb;
|
||||
@@ -162,7 +160,7 @@ void getprop(void (*callback)(const char *, const char *, void *), void *cookie,
|
||||
}
|
||||
}
|
||||
|
||||
int setprop(const char *name, const char *value, const bool trigger) {
|
||||
int setprop(const char *name, const char *value, bool trigger) {
|
||||
if (!check_legal_property_name(name))
|
||||
return 1;
|
||||
if (init_resetprop())
|
||||
@@ -170,7 +168,7 @@ int setprop(const char *name, const char *value, const bool trigger) {
|
||||
|
||||
int ret;
|
||||
|
||||
prop_info *pi = (prop_info*) __system_property_find(name);
|
||||
auto pi = (prop_info*) __system_property_find(name);
|
||||
if (pi != nullptr) {
|
||||
if (trigger) {
|
||||
if (strncmp(name, "ro.", 3) == 0) deleteprop(name);
|
||||
@@ -208,7 +206,7 @@ int deleteprop(const char *name, bool persist) {
|
||||
return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0);
|
||||
}
|
||||
|
||||
int load_prop_file(const char *filename, const bool trigger) {
|
||||
int load_prop_file(const char *filename, bool trigger) {
|
||||
if (init_resetprop()) return -1;
|
||||
LOGD("resetprop: Load prop file [%s]\n", filename);
|
||||
FILE *fp = xfopen(filename, "re");
|
||||
@@ -254,14 +252,14 @@ int resetprop_main(int argc, char *argv[]) {
|
||||
|
||||
bool trigger = true, persist = false;
|
||||
char *argv0 = argv[0];
|
||||
CharArray prop;
|
||||
string prop;
|
||||
|
||||
--argc;
|
||||
++argv;
|
||||
|
||||
// Parse flags and -- options
|
||||
while (argc && argv[0][0] == '-') {
|
||||
for (int idx = 1; 1; ++idx) {
|
||||
for (int idx = 1; true; ++idx) {
|
||||
switch (argv[0][idx]) {
|
||||
case '-':
|
||||
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
|
||||
@@ -275,10 +273,10 @@ int resetprop_main(int argc, char *argv[]) {
|
||||
verbose = true;
|
||||
continue;
|
||||
case 'p':
|
||||
persist = 1;
|
||||
persist = true;
|
||||
continue;
|
||||
case 'n':
|
||||
trigger = 0;
|
||||
trigger = false;
|
||||
continue;
|
||||
case '\0':
|
||||
break;
|
||||
@@ -297,8 +295,8 @@ int resetprop_main(int argc, char *argv[]) {
|
||||
print_props(persist);
|
||||
return 0;
|
||||
case 1:
|
||||
prop = utils::move(getprop(argv[0], persist));
|
||||
if (!prop) return 1;
|
||||
prop = getprop(argv[0], persist);
|
||||
if (prop.empty()) return 1;
|
||||
printf("%s\n", prop.c_str());
|
||||
return 0;
|
||||
case 2:
|
||||
|
Reference in New Issue
Block a user