Directly filter '.' and '..' in xreaddir

This commit is contained in:
topjohnwu 2020-04-18 04:20:21 -07:00
parent ed6cdb2eb4
commit dcf07ad8c7
6 changed files with 15 additions and 35 deletions

View File

@ -164,7 +164,7 @@ void remove_modules() {
int dfd = dirfd(dir.get());
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv || entry->d_name == ".core"sv)
if (entry->d_name == ".core"sv)
continue;
int modfd = xopenat(dfd, entry->d_name, O_RDONLY | O_CLOEXEC);

View File

@ -11,11 +11,6 @@
using namespace std;
#define SKIP_DOTS {\
if (entry->d_name == "."sv || entry->d_name == ".."sv) \
continue;\
}
#ifdef MAGISK_DEBUG
#define VLOGI(tag, from, to) LOGI("%-8s: %s <- %s\n", tag, to, from)
#else
@ -440,7 +435,6 @@ bool dir_node::prepare() {
string mirror = skel->mirror_path();
auto dir = xopen_dir(mirror.data());
for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
// Insert mirror nodes
skel->emplace<mirror_node>(entry->d_name, entry);
}
@ -457,7 +451,6 @@ bool dir_node::collect_files(const char *module, int dfd) {
return true;
for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
if (entry->d_name == ".replace"sv) {
// Stop traversing and tell parent to upgrade self to module
return false;
@ -550,8 +543,6 @@ static void prepare_modules() {
int mfd = xopen(MODULEROOT, O_RDONLY | O_CLOEXEC);
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
// Cleanup old module if exists
if (faccessat(mfd, entry->d_name, F_OK, 0) == 0) {
frm_rf(xopenat(mfd, entry->d_name, O_RDONLY | O_CLOEXEC));
@ -579,7 +570,7 @@ static void collect_modules() {
int dfd = dirfd(dir.get());
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_type == DT_DIR) {
if (entry->d_name == "."sv || entry->d_name == ".."sv || entry->d_name == ".core"sv)
if (entry->d_name == ".core"sv)
continue;
int modfd = xopenat(dfd, entry->d_name, O_RDONLY);

View File

@ -24,8 +24,6 @@ static void restore_syscon(int dirfd) {
dir = xfdopendir(dirfd);
while ((entry = xreaddir(dir))) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
int fd = openat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
if (entry->d_type == DT_DIR) {
restore_syscon(fd);
@ -53,8 +51,6 @@ static void restore_magiskcon(int dirfd) {
dir = xfdopendir(dirfd);
while ((entry = xreaddir(dir))) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
int fd = xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC);
if (entry->d_type == DT_DIR) {
restore_magiskcon(fd);
@ -87,8 +83,6 @@ void restore_rootcon() {
int dfd = dirfd(dir.get());
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
if (entry->d_name == "magisk"sv || entry->d_name == "magiskinit"sv)
setfilecon_at(dfd, entry->d_name, MAGISK_CON);
else

View File

@ -143,8 +143,6 @@ bool MagiskInit::patch_sepolicy(const char *file) {
// Custom rules
if (auto dir = xopen_dir(persist_dir.data()); dir) {
for (dirent *entry; (entry = xreaddir(dir.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
auto rule = persist_dir + "/" + entry->d_name + "/sepolicy.rule";
if (access(rule.data(), R_OK) == 0) {
LOGD("Loading custom sepolicy patch: %s\n", rule.data());
@ -170,8 +168,6 @@ static void recreate_sbin(const char *mirror, bool use_bind_mount) {
int src = dirfd(dp.get());
char buf[4096];
for (dirent *entry; (entry = xreaddir(dp.get()));) {
if (entry->d_name == "."sv || entry->d_name == ".."sv)
continue;
string sbin_path = "/sbin/"s + entry->d_name;
struct stat st;
fstatat(src, entry->d_name, &st, AT_SYMLINK_NOFOLLOW);

View File

@ -45,17 +45,11 @@ int mkdirs(string path, mode_t mode) {
return 0;
}
#define SKIP_DOTS {\
if (entry->d_name == "."sv || entry->d_name == ".."sv) \
continue;\
}
static void post_order_walk(int dirfd, const function<void(int, dirent *)> &&fn) {
auto dir = xopen_dir(dirfd);
if (!dir) return;
for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
if (entry->d_type == DT_DIR)
post_order_walk(xopenat(dirfd, entry->d_name, O_RDONLY | O_CLOEXEC), std::move(fn));
fn(dirfd, entry);
@ -67,7 +61,6 @@ static void pre_order_walk(int dirfd, const function<bool(int, dirent *)> &&fn)
if (!dir) return;
for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
if (!fn(dirfd, entry))
continue;
if (entry->d_type == DT_DIR)
@ -109,7 +102,6 @@ void mv_dir(int src, int dest) {
auto dir = xopen_dir(src);
run_finally f([&]{ close(dest); });
for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
switch (entry->d_type) {
case DT_DIR:
if (faccessat(dest, entry->d_name, F_OK, 0) == 0) {
@ -157,7 +149,6 @@ void clone_dir(int src, int dest) {
auto dir = xopen_dir(src);
run_finally f([&]{ close(dest); });
for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
file_attr a;
getattrat(src, entry->d_name, &a);
switch (entry->d_type) {
@ -197,7 +188,6 @@ void link_dir(int src, int dest) {
auto dir = xopen_dir(src);
run_finally f([&]{ close(dest); });
for (dirent *entry; (entry = xreaddir(dir.get()));) {
SKIP_DOTS
if (entry->d_type == DT_DIR) {
file_attr a;
getattrat(src, entry->d_name, &a);

View File

@ -13,6 +13,8 @@
#include <logging.hpp>
#include <utils.hpp>
using namespace std;
FILE *xfopen(const char *pathname, const char *mode) {
FILE *fp = fopen(pathname, mode);
if (fp == nullptr) {
@ -129,11 +131,18 @@ DIR *xfdopendir(int fd) {
struct dirent *xreaddir(DIR *dirp) {
errno = 0;
struct dirent *e = readdir(dirp);
if (errno && e == nullptr) {
PLOGE("readdir");
for (dirent *e;;) {
e = readdir(dirp);
if (e == nullptr) {
if (errno)
PLOGE("readdir");
return nullptr;
} else if (e->d_name == "."sv || e->d_name == ".."sv) {
// Filter . and .. for users
continue;
}
return e;
}
return e;
}
pid_t xsetsid() {