Remove fopen usage in magiskinit

This commit is contained in:
topjohnwu 2024-02-24 00:45:07 -08:00
parent 3185e5a7ca
commit b7505c3c9c
2 changed files with 20 additions and 7 deletions

View File

@ -75,9 +75,14 @@ void file_readline(bool trim, FILE *fp, const function<bool(string_view)> &fn) {
}
void file_readline(bool trim, const char *file, const function<bool(string_view)> &fn) {
if (auto fp = open_file(file, "re"))
file_readline(trim, fp.get(), fn);
int fd = xopen(file, O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
auto fp = fdopen(fd, "re");
file_readline(trim, fp, fn);
fclose(fp);
}
}
void file_readline(const char *file, const function<bool(string_view)> &fn) {
file_readline(false, file, fn);
}
@ -96,8 +101,12 @@ void parse_prop_file(FILE *fp, const function<bool(string_view, string_view)> &f
}
void parse_prop_file(const char *file, const function<bool(string_view, string_view)> &fn) {
if (auto fp = open_file(file, "re"))
parse_prop_file(fp.get(), fn);
int fd = xopen(file, O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
auto fp = fdopen(fd, "re");
parse_prop_file(fp, fn);
fclose(fp);
}
}
std::vector<mount_info> parse_mount_info(const char *pid) {

View File

@ -103,7 +103,8 @@ sepolicy *sepolicy::from_file(const char *file) {
policy_file_t pf;
policy_file_init(&pf);
auto fp = xopen_file(file, "re");
int fd = xopen(file, O_RDONLY | O_CLOEXEC);
auto fp = make_file(fdopen(fd, "re"));
pf.fp = fp.get();
pf.type = PF_USE_STDIO;
@ -123,6 +124,7 @@ sepolicy *sepolicy::compile_split() {
cil_db_t *db = nullptr;
sepol_policydb_t *pdb = nullptr;
FILE *f;
int fd;
int policy_ver;
const char *cil_file;
#if MAGISK_DEBUG
@ -148,13 +150,15 @@ sepolicy *sepolicy::compile_split() {
cil_set_target_platform(db, SEPOL_TARGET_SELINUX);
cil_set_attrs_expand_generated(db, 1);
f = xfopen(SELINUX_VERSION, "re");
fd = xopen(SELINUX_VERSION, O_RDONLY | O_CLOEXEC);
f = fdopen(fd, "re");
fscanf(f, "%d", &policy_ver);
fclose(f);
cil_set_policy_version(db, policy_ver);
// Get mapping version
f = xfopen(VEND_POLICY_DIR "plat_sepolicy_vers.txt", "re");
fd = xopen(VEND_POLICY_DIR "plat_sepolicy_vers.txt", O_RDONLY | O_CLOEXEC);
f = fdopen(fd, "re");
fscanf(f, "%s", plat_ver);
fclose(f);