Skip to content

Commit 2c41722

Browse files
authored
Merge pull request #32804 from YHNdnzj/bootspec-modernization
shared/bootspec: some fixes/modernizations
2 parents 7273d16 + 97dfed1 commit 2c41722

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

src/shared/bootspec.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ static int mangle_path(
7979
assert(ret);
8080

8181
/* Spec leaves open if prefixed with "/" or not, let's normalize that */
82-
if (path_is_absolute(p))
83-
c = strdup(p);
84-
else
85-
c = strjoin("/", p);
82+
c = path_make_absolute(p, "/");
8683
if (!c)
8784
return -ENOMEM;
8885

@@ -289,7 +286,6 @@ static int boot_entry_load_type1(
289286
BootEntry *entry) {
290287

291288
_cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_CONF);
292-
unsigned line = 1;
293289
char *c;
294290
int r;
295291

@@ -324,18 +320,16 @@ static int boot_entry_load_type1(
324320
if (!tmp.root)
325321
return log_oom();
326322

327-
for (;;) {
323+
for (unsigned line = 1;; line++) {
328324
_cleanup_free_ char *buf = NULL, *field = NULL;
329325

330326
r = read_stripped_line(f, LONG_LINE_MAX, &buf);
331-
if (r == 0)
332-
break;
333327
if (r == -ENOBUFS)
334328
return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Line too long.");
335329
if (r < 0)
336330
return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while reading: %m");
337-
338-
line++;
331+
if (r == 0)
332+
break;
339333

340334
if (IN_SET(buf[0], '#', '\0'))
341335
continue;
@@ -427,34 +421,31 @@ void boot_config_free(BootConfig *config) {
427421
free(config->entry_default);
428422
free(config->entry_selected);
429423

430-
for (size_t i = 0; i < config->n_entries; i++)
431-
boot_entry_free(config->entries + i);
424+
FOREACH_ARRAY(i, config->entries, config->n_entries)
425+
boot_entry_free(i);
432426
free(config->entries);
433427
free(config->global_addons.items);
434428

435429
set_free(config->inodes_seen);
436430
}
437431

438432
int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) {
439-
unsigned line = 1;
440433
int r;
441434

442435
assert(config);
443436
assert(file);
444437
assert(path);
445438

446-
for (;;) {
439+
for (unsigned line = 1;; line++) {
447440
_cleanup_free_ char *buf = NULL, *field = NULL;
448441

449442
r = read_stripped_line(file, LONG_LINE_MAX, &buf);
450-
if (r == 0)
451-
break;
452443
if (r == -ENOBUFS)
453444
return log_syntax(NULL, LOG_ERR, path, line, r, "Line too long.");
454445
if (r < 0)
455446
return log_syntax(NULL, LOG_ERR, path, line, r, "Error while reading: %m");
456-
457-
line++;
447+
if (r == 0)
448+
break;
458449

459450
if (IN_SET(buf[0], '#', '\0'))
460451
continue;
@@ -595,8 +586,8 @@ static int boot_entries_find_type1(
595586
if (r < 0)
596587
return log_error_errno(r, "Failed to read directory '%s': %m", full);
597588

598-
for (size_t i = 0; i < dentries->n_entries; i++) {
599-
const struct dirent *de = dentries->entries[i];
589+
FOREACH_ARRAY(i, dentries->entries, dentries->n_entries) {
590+
const struct dirent *de = *i;
600591
_cleanup_fclose_ FILE *f = NULL;
601592

602593
if (!dirent_is_file(de))
@@ -619,7 +610,7 @@ static int boot_entries_find_type1(
619610

620611
r = boot_config_load_type1(config, f, root, full, de->d_name);
621612
if (r == -ENOMEM) /* ignore all other errors */
622-
return r;
613+
return log_oom();
623614
}
624615

625616
return 0;
@@ -796,7 +787,7 @@ static int find_cmdline_section(
796787

797788
/* Quick test to check if there is actual content in the addon cmdline */
798789
t = delete_chars(word, NULL);
799-
if (t[0] == 0)
790+
if (isempty(t))
800791
*ret_cmdline = NULL;
801792
else
802793
*ret_cmdline = TAKE_PTR(cmdline);
@@ -880,19 +871,22 @@ static int insert_boot_entry_addon(
880871
char *location,
881872
char *cmdline) {
882873

874+
assert(addons);
875+
883876
if (!GREEDY_REALLOC(addons->items, addons->n_items + 1))
884877
return log_oom();
885878

886-
addons->items[addons->n_items] = (BootEntryAddon) {
879+
addons->items[addons->n_items++] = (BootEntryAddon) {
887880
.location = location,
888881
.cmdline = cmdline,
889882
};
890-
addons->n_items++;
891883

892884
return 0;
893885
}
894886

895887
static void boot_entry_addons_done(BootEntryAddons *addons) {
888+
assert(addons);
889+
896890
FOREACH_ARRAY(addon, addons->items, addons->n_items) {
897891
free(addon->cmdline);
898892
free(addon->location);
@@ -1542,7 +1536,11 @@ static int json_addon(
15421536

15431537
int r;
15441538

1545-
r = json_variant_append_arrayb(array,
1539+
assert(addon);
1540+
assert(addon_str);
1541+
1542+
r = json_variant_append_arrayb(
1543+
array,
15461544
JSON_BUILD_OBJECT(
15471545
JSON_BUILD_PAIR(addon_str, JSON_BUILD_STRING(addon->location)),
15481546
JSON_BUILD_PAIR("options", JSON_BUILD_STRING(addon->cmdline))));
@@ -1783,7 +1781,7 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
17831781
}
17841782

17851783
return json_variant_dump(array, json_format | JSON_FORMAT_EMPTY_ARRAY, NULL, NULL);
1786-
} else {
1784+
} else
17871785
for (size_t n = 0; n < config->n_entries; n++) {
17881786
r = show_boot_entry(
17891787
config->entries + n,
@@ -1797,7 +1795,6 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) {
17971795
if (n+1 < config->n_entries)
17981796
putchar('\n');
17991797
}
1800-
}
18011798

18021799
return 0;
18031800
}

0 commit comments

Comments
 (0)