]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hwdb: simplify error handling in trie_store
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 29 Nov 2016 20:27:40 +0000 (15:27 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 30 Nov 2016 20:51:13 +0000 (15:51 -0500)
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.

src/hwdb/hwdb.c

index 41f74ad133eb7cfcb811193ec5c12d635825d39c..fba0c309c5f460a1b85ca81553808d68e64e3c8b 100644 (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,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,