Debian Codegraph @

src/sysupdate/sysupdate-feature.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "conf-parser.h"
5 #include "hash-funcs.h"
6 #include "path-util.h"
7 #include "string-util.h"
8 #include "sysupdate.h"
9 #include "sysupdate-feature.h"
10 #include "web-util.h"
11
12 static Feature *feature_free(Feature *f) {
13 if (!f)
14 return NULL;
15
16 free(f->id);
17
18 free(f->description);
19 free(f->documentation);
20 free(f->appstream);
21
22 return mfree(f);
23 }
24
25 Feature *feature_new(void) {
26 Feature *f;
27
28 f = new(Feature, 1);
29 if (!f)
30 return NULL;
31
32 *f = (Feature) {
33 .n_ref = 1,
34 };
35
36 return f;
37 }
38
39 DEFINE_TRIVIAL_REF_UNREF_FUNC(Feature, feature, feature_free);
40
41 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(feature_hash_ops,
42 char, string_hash_func, string_compare_func,
43 Feature, feature_unref);
44
45 static int config_parse_url_specifiers(
46 const char *unit,
47 const char *filename,
48 unsigned line,
49 const char *section,
50 unsigned section_line,
51 const char *lvalue,
52 int ltype,
53 const char *rvalue,
54 void *data,
55 void *userdata) {
56 char **s = ASSERT_PTR(data);
57 _cleanup_free_ char *resolved = NULL;
58 int r;
59
60 assert(rvalue);
61
62 if (isempty(rvalue)) {
63 *s = mfree(*s);
64 return 0;
65 }
66
67 r = specifier_printf(rvalue, NAME_MAX, specifier_table, arg_root, NULL, &resolved);
68 if (r < 0) {
69 log_syntax(unit, LOG_WARNING, filename, line, r,
70 "Failed to expand specifiers in %s=, ignoring: %s", lvalue, rvalue);
71 return 0;
72 }
73
74 if (!http_url_is_valid(resolved)) {
75 log_syntax(unit, LOG_WARNING, filename, line, 0,
76 "%s= URL is not valid, ignoring: %s", lvalue, rvalue);
77 return 0;
78 }
79
80 return free_and_replace(*s, resolved);
81 }
82
83 int feature_read_definition(Feature *f, const char *path, const char *const *dirs) {
84 assert(f);
85
86 ConfigTableItem table[] = {
87 { "Feature", "Description", config_parse_string, 0, &f->description },
88 { "Feature", "Documentation", config_parse_url_specifiers, 0, &f->documentation },
89 { "Feature", "AppStream", config_parse_url_specifiers, 0, &f->appstream },
90 { "Feature", "Enabled", config_parse_bool, 0, &f->enabled },
91 {}
92 };
93
94 _cleanup_free_ char *filename = NULL;
95 int r;
96
97 assert(path);
98 assert(dirs);
99
100 r = path_extract_filename(path, &filename);
101 if (r < 0)
102 return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);
103
104 r = config_parse_many_full(
105 STRV_MAKE_CONST(path),
106 dirs,
107 strjoina(filename, ".d"),
108 arg_root,
109 /* root_fd= */ -EBADF,
110 "Feature\0",
111 config_item_table_lookup, table,
112 CONFIG_PARSE_WARN,
113 /* userdata= */ NULL,
114 /* stats_by_path= */ NULL,
115 /* drop_in_files= */ NULL);
116 if (r < 0)
117 return r;
118
119 *ASSERT_PTR(endswith(filename, ".feature")) = 0; /* Remove the file extension */
120 f->id = TAKE_PTR(filename);
121
122 return 0;
123 }