Separate fstab finding to its own function

This commit is contained in:
topjohnwu 2022-01-02 15:49:12 -08:00
parent 8c696cb8ca
commit 1e77e0862a
2 changed files with 40 additions and 34 deletions

View File

@ -92,6 +92,7 @@ public:
class FirstStageInit : public BaseInit { class FirstStageInit : public BaseInit {
private: private:
void prepare(); void prepare();
void get_default_fstab(char *buf, size_t len);
public: public:
FirstStageInit(char *argv[], BootConfig *cmd) : BaseInit(argv, cmd) { FirstStageInit(char *argv[], BootConfig *cmd) : BaseInit(argv, cmd) {
LOGD("%s\n", __FUNCTION__); LOGD("%s\n", __FUNCTION__);

View File

@ -109,6 +109,41 @@ static void append_oplus(vector<fstab_entry> &fstab) {
unlink("oplus.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() { void FirstStageInit::prepare() {
if (is_dsu()) { if (is_dsu()) {
rename(backup_init(), "/init"); rename(backup_init(), "/init");
@ -135,23 +170,11 @@ void FirstStageInit::prepare() {
} }
char fstab_file[128]; char fstab_file[128];
fstab_file[0] = '\0'; get_default_fstab(fstab_file, sizeof(fstab_file));
// Find existing fstab file // Empty fstab file path is an error
for (const char *suffix : { config->fstab_suffix, config->hardware, config->hardware_plat }) { if (fstab_file[0] == '\0')
if (suffix[0] == '\0') return;
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:
// Try to load dt fstab // Try to load dt fstab
vector<fstab_entry> fstab; vector<fstab_entry> fstab;
@ -177,24 +200,6 @@ exit_loop:
} // TODO: else if expected_field checks and fs_mgr_flags checks } // 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) { if (skip) {
// When dt fstab fails, fall back to default fstab // When dt fstab fails, fall back to default fstab
LOGI("dt fstab contains error, fall back to default fstab\n"); LOGI("dt fstab contains error, fall back to default fstab\n");