Merge "Refactor some pieces of grp_pwd.cpp"

This commit is contained in:
Tom Cherry 2019-04-24 23:02:06 +00:00 committed by Gerrit Code Review
commit 731ed0fb52

View file

@ -163,37 +163,19 @@ static group* android_iinfo_to_group(group_state_t* state,
return gr; return gr;
} }
static passwd* android_id_to_passwd(passwd_state_t* state, unsigned id) { static const android_id_info* find_android_id_info(unsigned id) {
for (size_t n = 0; n < android_id_count; ++n) { for (size_t n = 0; n < android_id_count; ++n) {
if (android_ids[n].aid == id) { if (android_ids[n].aid == id) {
return android_iinfo_to_passwd(state, android_ids + n); return &android_ids[n];
} }
} }
return nullptr; return nullptr;
} }
static passwd* android_name_to_passwd(passwd_state_t* state, const char* name) { static const android_id_info* find_android_id_info(const char* name) {
for (size_t n = 0; n < android_id_count; ++n) { for (size_t n = 0; n < android_id_count; ++n) {
if (!strcmp(android_ids[n].name, name)) { if (!strcmp(android_ids[n].name, name)) {
return android_iinfo_to_passwd(state, android_ids + n); return &android_ids[n];
}
}
return nullptr;
}
static group* android_id_to_group(group_state_t* state, unsigned id) {
for (size_t n = 0; n < android_id_count; ++n) {
if (android_ids[n].aid == id) {
return android_iinfo_to_group(state, android_ids + n);
}
}
return nullptr;
}
static group* android_name_to_group(group_state_t* state, const char* name) {
for (size_t n = 0; n < android_id_count; ++n) {
if (!strcmp(android_ids[n].name, name)) {
return android_iinfo_to_group(state, android_ids + n);
} }
} }
return nullptr; return nullptr;
@ -332,15 +314,9 @@ static id_t app_id_from_name(const char* name, bool is_group) {
} else if (end[1] == 'i' && isdigit(end[2])) { } else if (end[1] == 'i' && isdigit(end[2])) {
// end will point to \0 if the strtoul below succeeds. // end will point to \0 if the strtoul below succeeds.
appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START; appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
} else { } else if (auto* android_id_info = find_android_id_info(end + 1); android_id_info != nullptr) {
for (size_t n = 0; n < android_id_count; n++) { appid = android_id_info->aid;
if (!strcmp(android_ids[n].name, end + 1)) { end += strlen(android_id_info->name) + 1;
appid = android_ids[n].aid;
// Move the end pointer to the null terminator.
end += strlen(android_ids[n].name) + 1;
break;
}
}
} }
// Check that the entire string was consumed by one of the 3 cases above. // Check that the entire string was consumed by one of the 3 cases above.
@ -370,11 +346,8 @@ static void print_app_name_from_uid(const uid_t uid, char* buffer, const int buf
if (appid >= AID_ISOLATED_START) { if (appid >= AID_ISOLATED_START) {
snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START); snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
} else if (appid < AID_APP_START) { } else if (appid < AID_APP_START) {
for (size_t n = 0; n < android_id_count; n++) { if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
if (android_ids[n].aid == appid) { snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
return;
}
} }
} else { } else {
snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START); snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
@ -391,11 +364,8 @@ static void print_app_name_from_gid(const gid_t gid, char* buffer, const int buf
} else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) { } else if (appid >= AID_CACHE_GID_START && appid <= AID_CACHE_GID_END) {
snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START); snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
} else if (appid < AID_APP_START) { } else if (appid < AID_APP_START) {
for (size_t n = 0; n < android_id_count; n++) { if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
if (android_ids[n].aid == appid) { snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
return;
}
} }
} else { } else {
snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START); snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP_START);
@ -520,12 +490,12 @@ passwd* getpwuid(uid_t uid) { // NOLINT: implementing bad function.
return nullptr; return nullptr;
} }
passwd* pw = android_id_to_passwd(state, uid); if (auto* android_id_info = find_android_id_info(uid); android_id_info != nullptr) {
if (pw != nullptr) { return android_iinfo_to_passwd(state, android_id_info);
return pw;
} }
// Handle OEM range. // Handle OEM range.
pw = oem_id_to_passwd(uid, state); passwd* pw = oem_id_to_passwd(uid, state);
if (pw != nullptr) { if (pw != nullptr) {
return pw; return pw;
} }
@ -538,9 +508,8 @@ passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
return nullptr; return nullptr;
} }
passwd* pw = android_name_to_passwd(state, login); if (auto* android_id_info = find_android_id_info(login); android_id_info != nullptr) {
if (pw != nullptr) { return android_iinfo_to_passwd(state, android_id_info);
return pw;
} }
if (vendor_passwd.FindByName(login, state)) { if (vendor_passwd.FindByName(login, state)) {
@ -550,7 +519,7 @@ passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
} }
// Handle OEM range. // Handle OEM range.
pw = oem_id_to_passwd(oem_id_from_name(login), state); passwd* pw = oem_id_to_passwd(oem_id_from_name(login), state);
if (pw != nullptr) { if (pw != nullptr) {
return pw; return pw;
} }
@ -634,12 +603,12 @@ passwd* getpwent() {
} }
static group* getgrgid_internal(gid_t gid, group_state_t* state) { static group* getgrgid_internal(gid_t gid, group_state_t* state) {
group* grp = android_id_to_group(state, gid); if (auto* android_id_info = find_android_id_info(gid); android_id_info != nullptr) {
if (grp != nullptr) { return android_iinfo_to_group(state, android_id_info);
return grp;
} }
// Handle OEM range. // Handle OEM range.
grp = oem_id_to_group(gid, state); group* grp = oem_id_to_group(gid, state);
if (grp != nullptr) { if (grp != nullptr) {
return grp; return grp;
} }
@ -655,9 +624,8 @@ group* getgrgid(gid_t gid) { // NOLINT: implementing bad function.
} }
static group* getgrnam_internal(const char* name, group_state_t* state) { static group* getgrnam_internal(const char* name, group_state_t* state) {
group* grp = android_name_to_group(state, name); if (auto* android_id_info = find_android_id_info(name); android_id_info != nullptr) {
if (grp != nullptr) { return android_iinfo_to_group(state, android_id_info);
return grp;
} }
if (vendor_group.FindByName(name, state)) { if (vendor_group.FindByName(name, state)) {
@ -667,7 +635,7 @@ static group* getgrnam_internal(const char* name, group_state_t* state) {
} }
// Handle OEM range. // Handle OEM range.
grp = oem_id_to_group(oem_id_from_name(name), state); group* grp = oem_id_to_group(oem_id_from_name(name), state);
if (grp != nullptr) { if (grp != nullptr) {
return grp; return grp;
} }