1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 17:51:22 +03:00

hwdb: simplify error handling in trie_store

fclose() can also set errno, so the attempts to protect errno that the
code made were not successful. Simplify things by immediately saving
errno to r.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2016-11-29 15:27:40 -05:00
parent 1cd592b916
commit d702dcdfef

View File

@ -396,24 +396,21 @@ static int trie_store(struct trie *trie, const char *filename) {
.child_entry_size = htole64(sizeof(struct trie_child_entry_f)),
.value_entry_size = htole64(sizeof(struct trie_value_entry2_f)),
};
int err;
int r;
/* calculate size of header, nodes, children entries, value entries */
t.strings_off = sizeof(struct trie_header_f);
trie_store_nodes_size(&t, trie->root);
err = fopen_temporary(filename , &t.f, &filename_tmp);
if (err < 0)
return err;
r = fopen_temporary(filename , &t.f, &filename_tmp);
if (r < 0)
return r;
fchmod(fileno(t.f), 0444);
/* write nodes */
err = fseeko(t.f, sizeof(struct trie_header_f), SEEK_SET);
if (err < 0) {
fclose(t.f);
unlink_noerrno(filename_tmp);
return -errno;
}
if (fseeko(t.f, sizeof(struct trie_header_f), SEEK_SET) < 0)
goto error;
root_off = trie_store_nodes(&t, trie->root);
h.nodes_root_off = htole64(root_off);
pos = ftello(t.f);
@ -426,21 +423,14 @@ static int trie_store(struct trie *trie, const char *filename) {
/* write header */
size = ftello(t.f);
h.file_size = htole64(size);
err = fseeko(t.f, 0, SEEK_SET);
if (err < 0) {
fclose(t.f);
if (fseeko(t.f, 0, SEEK_SET) < 0)
goto error;
fwrite(&h, sizeof(struct trie_header_f), 1, t.f);
if (fclose(t.f) < 0 || rename(filename_tmp, filename) < 0) {
unlink_noerrno(filename_tmp);
return -errno;
}
fwrite(&h, sizeof(struct trie_header_f), 1, t.f);
err = ferror(t.f);
if (err)
err = -errno;
fclose(t.f);
if (err < 0 || rename(filename_tmp, filename) < 0) {
unlink_noerrno(filename_tmp);
return err < 0 ? err : -errno;
}
log_debug("=== trie on-disk ===");
log_debug("size: %8"PRIi64" bytes", size);
@ -453,8 +443,13 @@ static int trie_store(struct trie *trie, const char *filename) {
t.values_count * sizeof(struct trie_value_entry2_f), t.values_count);
log_debug("string store: %8zu bytes", trie->strings->len);
log_debug("strings start: %8"PRIu64, t.strings_off);
return 0;
error:
r = -errno;
fclose(t.f);
unlink(filename_tmp);
return r;
}
static int insert_data(struct trie *trie, char **match_list, char *line,