Make parse prop file a util function

This commit is contained in:
topjohnwu 2019-03-05 20:27:09 -05:00
parent b278d07b05
commit 04ef1e6405
8 changed files with 35 additions and 59 deletions

View File

@ -380,7 +380,7 @@ static bool magisk_env() {
LOGI("* Mounting mirrors");
bool system_as_root = false;
file_readline("/proc/mounts", [&](string_view &line) -> bool {
file_readline("/proc/mounts", [&](string_view line) -> bool {
if (str_contains(line, " /system_root ")) {
bind_mount("/system_root/system", MIRRDIR "/system");
sscanf(line.data(), "%s", buf);
@ -494,7 +494,7 @@ static void collect_modules() {
static bool check_data() {
bool mnt = false;
bool data = false;
file_readline("/proc/mounts", [&](string_view &s) -> bool {
file_readline("/proc/mounts", [&](string_view s) -> bool {
if (str_contains(s, " /data ") && !str_contains(s, "tmpfs"))
mnt = true;
return true;

View File

@ -118,9 +118,9 @@ static void main_daemon() {
// Get API level
parse_prop_file("/system/build.prop", [](auto key, auto val) -> bool {
if (strcmp(key, "ro.build.version.sdk") == 0) {
LOGI("* Device API level: %s\n", val);
SDK_INT = atoi(val);
if (key == "ro.build.version.sdk") {
LOGI("* Device API level: %s\n", val.data());
SDK_INT = atoi(val.data());
return false;
}
return true;

View File

@ -11,5 +11,4 @@ int setprop(const char *name, const char *value, bool trigger = true);
std::string getprop(const char *name, bool persist = false);
void getprop(void (*callback)(const char *, const char *, void *), void *cookie, bool persist = false);
int deleteprop(const char *name, bool persist = false);
int parse_prop_file(const char *filename, const std::function<bool (const char *, const char *)> &cb);
int load_prop_file(const char *filename, bool trigger = true);
void load_prop_file(const char *filename, bool trigger = true);

View File

@ -233,7 +233,7 @@ bool init_list() {
// Migrate old hide list into database
if (access(LEGACY_LIST, R_OK) == 0) {
file_readline(LEGACY_LIST, [](string_view &s) -> bool {
file_readline(LEGACY_LIST, [](string_view s) -> bool {
add_list(s.data());
return true;
});

View File

@ -79,7 +79,7 @@ static long xptrace(int request, pid_t pid, void *addr = nullptr, intptr_t data
return xptrace(true, request, pid, addr, reinterpret_cast<void *>(data));
}
static bool parse_packages_xml(string_view &s) {
static bool parse_packages_xml(string_view s) {
if (!str_starts(s, "<package "))
return true;
/* <package key1="value1" key2="value2"....> */
@ -144,7 +144,7 @@ static void hide_daemon(int pid) {
vector<string> targets;
// Unmount dummy skeletons and /sbin links
file_readline("/proc/self/mounts", [&](string_view &s) -> bool {
file_readline("/proc/self/mounts", [&](string_view s) -> bool {
if (str_contains(s, "tmpfs /system/") || str_contains(s, "tmpfs /vendor/") ||
str_contains(s, "tmpfs /sbin")) {
char *path = (char *) s.data();
@ -160,7 +160,7 @@ static void hide_daemon(int pid) {
targets.clear();
// Unmount everything under /system, /vendor, and data mounts
file_readline("/proc/self/mounts", [&](string_view &s) -> bool {
file_readline("/proc/self/mounts", [&](string_view s) -> bool {
if ((str_contains(s, " /system/") || str_contains(s, " /vendor/")) &&
(str_contains(s, system_block) || str_contains(s, vendor_block) ||
str_contains(s, data_block))) {

View File

@ -206,49 +206,11 @@ int deleteprop(const char *name, bool persist) {
return __system_property_del(name) && !(persist && strncmp(name, "persist.", 8) == 0);
}
int parse_prop_file(const char *filename, const function<bool (const char *, const char *)> &cb) {
void load_prop_file(const char *filename, bool trigger) {
if (init_resetprop()) return;
LOGD("resetprop: Parse prop file [%s]\n", filename);
FILE *fp = xfopen(filename, "re");
if (fp == nullptr) {
LOGE("Cannot open [%s]\n", filename);
return 1;
}
char *line = nullptr, *eql;
size_t len;
ssize_t read;
int i;
while ((read = getline(&line, &len, fp)) != -1) {
// Remove the trailing newline
if (line[read - 1] == '\n') {
line[--read] = '\0';
}
bool comment = false;
for (i = 0; i < read; ++i) {
// Ignore starting spaces
if (line[i] == ' ') continue;
// A line starting with # is ignored
if (line[i] == '#') comment = true;
break;
}
if (comment) continue;
eql = strchr(line, '=');
// Ignore invalid formats
if ( ((eql == nullptr) || (i >= (eql - line))) || (eql >= line + read - 1) )
continue;
// Separate the string
*eql = '\0';
if (!cb(line + i, eql + 1))
break;
}
free(line);
fclose(fp);
return 0;
}
int load_prop_file(const char *filename, bool trigger) {
if (init_resetprop()) return -1;
return parse_prop_file(filename, [=](auto key, auto val) -> bool {
setprop(key, val, trigger);
parse_prop_file(filename, [=](auto key, auto val) -> bool {
setprop(key.data(), val.data(), trigger);
return true;
});
}
@ -269,7 +231,8 @@ int resetprop_main(int argc, char *argv[]) {
switch (argv[0][idx]) {
case '-':
if (strcmp(argv[0], "--file") == 0 && argc == 2) {
return load_prop_file(argv[1], trigger);
load_prop_file(argv[1], trigger);
return 0;
} else if (strcmp(argv[0], "--delete") == 0 && argc == 2) {
return deleteprop(argv[1], persist);
} else if (strcmp(argv[0], "--help") == 0) {

View File

@ -367,8 +367,8 @@ void write_zero(int fd, size_t size) {
}
}
void file_readline(const char *filename, const function<bool (string_view&)> &fn, bool trim) {
FILE *fp = xfopen(filename, "re");
void file_readline(const char *file, const function<bool (string_view)> &fn, bool trim) {
FILE *fp = xfopen(file, "re");
if (fp == nullptr)
return;
size_t len = 1024;
@ -383,10 +383,22 @@ void file_readline(const char *filename, const function<bool (string_view&)> &fn
while (*start == ' ')
++start;
}
string_view s(start);
if (!fn(s))
if (!fn(start))
break;
}
fclose(fp);
free(buf);
}
void parse_prop_file(const char *file, const function<bool (string_view, string_view)> &fn) {
file_readline(file, [&](string_view line_view) -> bool {
char *line = (char *) line_view.data();
if (line[0] == '#')
return true;
char *eql = strchr(line, '=');
if (eql == nullptr || eql == line)
return true;
*eql = '\0';
return fn(line, eql + 1);
}, true);
}

View File

@ -172,7 +172,9 @@ private:
// file.cpp
void file_readline(const char *filename, const std::function<bool (std::string_view&)> &fn, bool trim = false);
void file_readline(const char *file, const std::function<bool (std::string_view)> &fn, bool trim = false);
void parse_prop_file(const char *file, const std::function
<bool(std::string_view, std::string_view)> &fn);
void *__mmap(const char *filename, size_t *size, bool rw);
template <typename B>