From 1e77e0862a2bcd4fb63dc1d6358851daeea2321a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 2 Jan 2022 15:49:12 -0800 Subject: [PATCH] Separate fstab finding to its own function --- native/jni/init/init.hpp | 1 + native/jni/init/twostage.cpp | 73 +++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/native/jni/init/init.hpp b/native/jni/init/init.hpp index 901b19a10..343e2b10a 100644 --- a/native/jni/init/init.hpp +++ b/native/jni/init/init.hpp @@ -92,6 +92,7 @@ public: class FirstStageInit : public BaseInit { private: void prepare(); + void get_default_fstab(char *buf, size_t len); public: FirstStageInit(char *argv[], BootConfig *cmd) : BaseInit(argv, cmd) { LOGD("%s\n", __FUNCTION__); diff --git a/native/jni/init/twostage.cpp b/native/jni/init/twostage.cpp index 1f447c09c..158c7bff7 100644 --- a/native/jni/init/twostage.cpp +++ b/native/jni/init/twostage.cpp @@ -109,6 +109,41 @@ static void append_oplus(vector &fstab) { unlink("oplus.fstab"); } +void FirstStageInit::get_default_fstab(char *buf, size_t len) { + buf[0] = '\0'; + + // Find existing fstab file + for (const char *suffix : { config->fstab_suffix, config->hardware, config->hardware_plat }) { + if (suffix[0] == '\0') + continue; + for (const char *prefix: { "odm/etc/fstab", "vendor/etc/fstab", "system/etc/fstab", "fstab" }) { + snprintf(buf, len, "%s.%s", prefix, suffix); + if (access(buf, F_OK) == 0) { + LOGD("Found fstab file: %s\n", buf); + return; + } + } + } + + buf[0] = '\0'; + + // No existing fstab file is found, create a valid path + const char *suffix = [&]() -> const char * { + if (config->fstab_suffix[0]) + return config->fstab_suffix; + if (config->hardware[0]) + return config->hardware; + if (config->hardware_plat[0]) + return config->hardware_plat; + return nullptr; + }(); + if (suffix == nullptr) { + LOGE("Cannot determine fstab suffix!\n"); + return; + } + snprintf(buf, len, "fstab.%s", suffix); +} + void FirstStageInit::prepare() { if (is_dsu()) { rename(backup_init(), "/init"); @@ -135,23 +170,11 @@ void FirstStageInit::prepare() { } char fstab_file[128]; - fstab_file[0] = '\0'; + get_default_fstab(fstab_file, sizeof(fstab_file)); - // Find existing fstab file - for (const char *suffix : { config->fstab_suffix, config->hardware, config->hardware_plat }) { - if (suffix[0] == '\0') - continue; - for (const char *prefix: { "odm/etc/fstab", "vendor/etc/fstab", "system/etc/fstab", "fstab" }) { - snprintf(fstab_file, sizeof(fstab_file), "%s.%s", prefix, suffix); - if (access(fstab_file, F_OK) != 0) { - fstab_file[0] = '\0'; - } else { - LOGD("Found fstab file: %s\n", fstab_file); - goto exit_loop; - } - } - } -exit_loop: + // Empty fstab file path is an error + if (fstab_file[0] == '\0') + return; // Try to load dt fstab vector fstab; @@ -177,24 +200,6 @@ exit_loop: } // TODO: else if expected_field checks and fs_mgr_flags checks } - if (fstab_file[0] == '\0') { - const char *suffix = [&]() -> const char * { - if (config->fstab_suffix[0]) - return config->fstab_suffix; - if (config->hardware[0]) - return config->hardware; - if (config->hardware_plat[0]) - return config->hardware_plat; - return nullptr; - }(); - - if (suffix == nullptr) { - LOGE("Cannot determine fstab suffix!\n"); - return; - } - snprintf(fstab_file, sizeof(fstab_file), "fstab.%s", suffix); - } - if (skip) { // When dt fstab fails, fall back to default fstab LOGI("dt fstab contains error, fall back to default fstab\n");