passwd-util: adds sorting logic for sysuser_entry

Per tile, added logic for sysusers_sorting. The comparison
for each sysuer_ent is like the following:
1: g > u > m for sysent_type and this takes precedence
2: if type matches, we compare the name for sysent using strcmp
3: If both type and name matches, we want to error out because it indicates a duplicate entry

Closes: #1519
Approved by: cgwalters
This commit is contained in:
Ruixin Bao 2018-06-11 15:55:38 +00:00 committed by Atomic Bot
parent d7dc6fe33c
commit 6facef9733

View File

@ -228,6 +228,31 @@ compare_group_ents (gconstpointer a, gconstpointer b)
return strcmp ((*sa)->name, (*sb)->name);
}
static int
compare_sysuser_ents (gconstpointer a, gconstpointer b)
{
const struct sysuser_ent **sa = (const struct sysuser_ent **)a;
const struct sysuser_ent **sb = (const struct sysuser_ent **)b;
/* g > u > m */
if (!g_str_equal ((*sa)->type, (*sb)->type))
{
gboolean is_group_type = g_str_equal ((*sa)->type, "g") || g_str_equal ((*sa)->type, "g");
if (is_group_type)
return !strcmp ((*sa)->type, (*sb)->type); /* g is smaller than m and u, we want g > other type here*/
gboolean is_user_type = g_str_equal ((*sa)->type, "u") || g_str_equal ((*sb)->type, "u");
if (is_user_type)
return strcmp ((*sa)->type, (*sb)->type); /* u > m */
}
/* We sort the entry name if type happens to be the same */
if (!g_str_equal ((*sa)->name, (*sb)->name))
return strcmp ((*sa)->name, (*sb)->name);
/* This is a collision, when both name and type matches, could error out here */
return 0;
}
gboolean
rpmostree_passwdents2sysusers (GPtrArray *passwd_ents,
GPtrArray **out_sysusers_entries,
@ -306,6 +331,8 @@ rpmostree_passwd_sysusers2char (GPtrArray *sysusers_entries,
{
GString* sysuser_content = g_string_new (NULL);
/* We do the sorting before conversion */
g_ptr_array_sort (sysusers_entries, compare_sysuser_ents);
for (int counter = 0; counter < sysusers_entries->len; counter++)
{
struct sysuser_ent *sysent = sysusers_entries->pdata[counter];