summaryrefslogtreecommitdiff
path: root/src/hwdb
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2016-11-29 15:27:40 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2016-11-30 15:51:13 -0500
commitd702dcdfef04512fa1529acece6d84ea58740d74 (patch)
tree1972a8aca9eed08230ce9bb84b51a15c4d9ea350 /src/hwdb
parent1cd592b91679f3a04fe884d51dd2b9d65eb5f2f9 (diff)
downloadsystemd-d702dcdfef04512fa1529acece6d84ea58740d74.tar.gz
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.
Diffstat (limited to 'src/hwdb')
-rw-r--r--src/hwdb/hwdb.c41
1 files changed, 18 insertions, 23 deletions
diff --git a/src/hwdb/hwdb.c b/src/hwdb/hwdb.c
index 41f74ad133..fba0c309c5 100644
--- a/src/hwdb/hwdb.c
+++ b/src/hwdb/hwdb.c
@@ -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,20 +423,13 @@ 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);
- unlink_noerrno(filename_tmp);
- return -errno;
- }
+ if (fseeko(t.f, 0, SEEK_SET) < 0)
+ goto error;
+
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) {
+ if (fclose(t.f) < 0 || rename(filename_tmp, filename) < 0) {
unlink_noerrno(filename_tmp);
- return err < 0 ? err : -errno;
+ return -errno;
}
log_debug("=== trie on-disk ===");
@@ -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,