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;
}
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) {
if (android_ids[n].aid == id) {
return android_iinfo_to_passwd(state, android_ids + n);
return &android_ids[n];
}
}
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) {
if (!strcmp(android_ids[n].name, name)) {
return android_iinfo_to_passwd(state, 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 &android_ids[n];
}
}
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])) {
// end will point to \0 if the strtoul below succeeds.
appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
} else {
for (size_t n = 0; n < android_id_count; n++) {
if (!strcmp(android_ids[n].name, end + 1)) {
appid = android_ids[n].aid;
// Move the end pointer to the null terminator.
end += strlen(android_ids[n].name) + 1;
break;
}
}
} else if (auto* android_id_info = find_android_id_info(end + 1); android_id_info != nullptr) {
appid = android_id_info->aid;
end += strlen(android_id_info->name) + 1;
}
// 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) {
snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
} else if (appid < AID_APP_START) {
for (size_t n = 0; n < android_id_count; n++) {
if (android_ids[n].aid == appid) {
snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
return;
}
if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
}
} else {
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) {
snprintf(buffer, bufferlen, "u%u_a%u_cache", userid, appid - AID_CACHE_GID_START);
} else if (appid < AID_APP_START) {
for (size_t n = 0; n < android_id_count; n++) {
if (android_ids[n].aid == appid) {
snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
return;
}
if (auto* android_id_info = find_android_id_info(appid); android_id_info != nullptr) {
snprintf(buffer, bufferlen, "u%u_%s", userid, android_id_info->name);
}
} else {
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;
}
passwd* pw = android_id_to_passwd(state, uid);
if (pw != nullptr) {
return pw;
if (auto* android_id_info = find_android_id_info(uid); android_id_info != nullptr) {
return android_iinfo_to_passwd(state, android_id_info);
}
// Handle OEM range.
pw = oem_id_to_passwd(uid, state);
passwd* pw = oem_id_to_passwd(uid, state);
if (pw != nullptr) {
return pw;
}
@ -538,9 +508,8 @@ passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
return nullptr;
}
passwd* pw = android_name_to_passwd(state, login);
if (pw != nullptr) {
return pw;
if (auto* android_id_info = find_android_id_info(login); android_id_info != nullptr) {
return android_iinfo_to_passwd(state, android_id_info);
}
if (vendor_passwd.FindByName(login, state)) {
@ -550,7 +519,7 @@ passwd* getpwnam(const char* login) { // NOLINT: implementing bad function.
}
// 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) {
return pw;
}
@ -634,12 +603,12 @@ passwd* getpwent() {
}
static group* getgrgid_internal(gid_t gid, group_state_t* state) {
group* grp = android_id_to_group(state, gid);
if (grp != nullptr) {
return grp;
if (auto* android_id_info = find_android_id_info(gid); android_id_info != nullptr) {
return android_iinfo_to_group(state, android_id_info);
}
// Handle OEM range.
grp = oem_id_to_group(gid, state);
group* grp = oem_id_to_group(gid, state);
if (grp != nullptr) {
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) {
group* grp = android_name_to_group(state, name);
if (grp != nullptr) {
return grp;
if (auto* android_id_info = find_android_id_info(name); android_id_info != nullptr) {
return android_iinfo_to_group(state, android_id_info);
}
if (vendor_group.FindByName(name, state)) {
@ -667,7 +635,7 @@ static group* getgrnam_internal(const char* name, group_state_t* state) {
}
// 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) {
return grp;
}