Debian Codegraph @

src/lib/profiles/list.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2018 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <errno.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
26
27 #include "common/common.h"
28 #include "lib/constants.h"
29 #include "lib/profiles/profiles.h"
30 #include "lib/util/util.h"
31
32 static errno_t
33 authselect_profile_dir_read(const char *path,
34 char ***array,
35 bool is_custom)
36 {
37 char *id;
38 char **subdirs;
39 errno_t ret;
40 int i;
41
42 INFO("Reading profile directory [%s]", path);
43
44 ret = dir_list(path, DIR_LIST_DIRS, &subdirs, NULL);
45 if (ret == ENOENT) {
46 /* This is just a warning so it should not be treated as error. */
47 WARN("Directory [%s] is missing!", path);
48 return EOK;
49 } else if (ret != EOK) {
50 ERROR("Unable to list directory [%s] [%d]: %s",
51 path, ret, strerror(ret));
52 return ret;
53 }
54
55 for (i = 0; subdirs[i] != NULL; i++) {
56 if (is_custom) {
57 /* Custom profile needs to be prefixed with custom/ */
58 id = authselect_profile_custom_id(subdirs[i]);
59 if (id == NULL) {
60 ret = ENOMEM;
61 goto done;
62 }
63
64 free(subdirs[i]);
65 subdirs[i] = id;
66
67 }
68
69 INFO("Found profile [%s]", subdirs[i]);
70 }
71
72 *array = string_array_concat(*array, subdirs, true);
73 if (*array == NULL) {
74 ret = ENOMEM;
75 goto done;
76 }
77
78 ret = EOK;
79
80 done:
81 string_array_free(subdirs);
82
83 return ret;
84 }
85
86 static int
87 authselect_profile_list_sort(const void *a, const void *b)
88 {
89 const char *str_a = *(const char **)a;
90 const char *str_b = *(const char **)b;
91
92 if (str_a == NULL) {
93 return 1;
94 }
95
96 if (str_b == NULL) {
97 return -1;
98 }
99
100 bool is_custom_a = authselect_profile_is_custom(str_a);
101 bool is_custom_b = authselect_profile_is_custom(str_b);
102
103 /* Custom profiles go last, otherwise we sort alphabetically. */
104 if (is_custom_a && !is_custom_b) {
105 return 1;
106 }
107
108 if (!is_custom_a && is_custom_b) {
109 return -1;
110 }
111
112 return strcmp(str_a, str_b);
113 }
114
115
116 errno_t
117 authselect_profile_list(char ***_profiles)
118 {
119 char **profiles;
120 errno_t ret;
121
122 profiles = string_array_create(1);
123 if (profiles == NULL) {
124 return ENOMEM;
125 }
126
127 ret = authselect_profile_dir_read(DIR_DEFAULT_PROFILES, &profiles, false);
128 if (ret != EOK) {
129 goto done;
130 }
131
132 ret = authselect_profile_dir_read(DIR_VENDOR_PROFILES, &profiles, false);
133 if (ret != EOK) {
134 goto done;
135 }
136
137 ret = authselect_profile_dir_read(DIR_CUSTOM_PROFILES, &profiles, true);
138 if (ret != EOK) {
139 goto done;
140 }
141
142 qsort(profiles, string_array_count(profiles), sizeof(char *),
143 authselect_profile_list_sort);
144
145 *_profiles = profiles;
146
147 ret = EOK;
148
149 done:
150 if (ret != EOK) {
151 ERROR("Unable to list profiles [%d]: %s", ret, strerror(ret));
152 string_array_free(profiles);
153 }
154
155 return ret;
156 }