Close out_fp in finish_write()
authorEric Biggers <ebiggers3@gmail.com>
Sun, 28 Oct 2012 06:23:30 +0000 (01:23 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sun, 28 Oct 2012 06:23:30 +0000 (01:23 -0500)
src/join.c
src/split.c
src/write.c

index d32fc1fe936c2f364965fa7a6fa1a31eb87c22e2..a6e8c9cb9e523a9290c0eae45580f2c0a09f4f5d 100644 (file)
@@ -329,14 +329,13 @@ WIMLIBAPI int wimlib_join(const char **swm_names, unsigned num_swms,
                goto out;
        ret = join_wims(swms, num_swms, joined_wim, write_flags);
 out:
+       /* out_fp is the same in all the swms and joined_wim.  And it was
+        * already closed in the call to finish_write(). */
        for (i = 0; i < num_swms; i++) {
-               /* out_fp is the same in all the swms and joined_wim; only close
-                * it one time, when freeing joined_wim. */
-               if (swms[i]) {
-                       swms[i]->out_fp = NULL;
-                       wimlib_free(swms[i]);
-               }
+               swms[i]->out_fp = NULL;
+               wimlib_free(swms[i]);
        }
+       joined_wim->out_fp = NULL;
        wimlib_free(joined_wim);
        return ret;
 }
index 2d96deb2eeec84df4679bb7b6af2688a8b797ac5..4167ecdfdb59648676f6d242f1bd87f9c7ebabe9 100644 (file)
@@ -67,16 +67,8 @@ static int finish_swm(WIMStruct *w, struct lookup_table_entry *lte_chain_head,
        w->hdr.lookup_table_res_entry.offset = lookup_table_offset;
        w->hdr.lookup_table_res_entry.size =
                                xml_data_offset - lookup_table_offset;
-       ret = finish_write(w, WIM_ALL_IMAGES,
-                          write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE);
-       if (ret != 0)
-               return ret;
-
-       ret = fclose(w->out_fp);
-       if (ret != 0)
-               ret = WIMLIB_ERR_WRITE;
-       w->out_fp = NULL;
-       return ret;
+       return finish_write(w, WIM_ALL_IMAGES,
+                           write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE);
 }
 
 static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args)
index 7c4d985c7885903faaca9c0d0b9c73707ae5f4b4..b1a593f8d3f6f6888972e04713e4f2709db7a9eb 100644 (file)
@@ -415,7 +415,18 @@ int finish_write(WIMStruct *w, int image, int write_flags)
        if (fseeko(out, 0, SEEK_SET) != 0)
                return WIMLIB_ERR_WRITE;
 
-       return write_header(&hdr, out);
+       ret = write_header(&hdr, out);
+       if (ret != 0)
+               return ret;
+
+       DEBUG("Closing output file.");
+       wimlib_assert(w->out_fp != NULL);
+       if (fclose(w->out_fp) != 0) {
+               ERROR_WITH_ERRNO("Failed to close the WIM file");
+               ret = WIMLIB_ERR_WRITE;
+       }
+       w->out_fp = NULL;
+       return ret;
 }
 
 /* Open file stream and write dummy header for WIM. */
@@ -433,6 +444,9 @@ int begin_write(WIMStruct *w, const char *path, int write_flags)
        else
                mode = "wb";
 
+       if (w->out_fp)
+               fclose(w->out_fp);
+
        w->out_fp = fopen(path, mode);
        if (!w->out_fp) {
                ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
@@ -472,7 +486,7 @@ WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path,
 
        ret = begin_write(w, path, write_flags);
        if (ret != 0)
-               goto out;
+               return ret;
 
        for_lookup_table_entry(w->lookup_table, lte_zero_out_refcnt, NULL);
 
@@ -481,27 +495,15 @@ WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path,
        ret = for_image(w, image, write_file_resources);
        if (ret != 0) {
                ERROR("Failed to write WIM file resources to `%s'", path);
-               goto out;
+               return ret;
        }
 
        ret = for_image(w, image, write_metadata_resource);
 
        if (ret != 0) {
                /*ERROR("Failed to write WIM image metadata to `%s'", path);*/
-               goto out;
+               return ret;
        }
 
-
-       ret = finish_write(w, image, write_flags);
-
-out:
-       DEBUG("Closing output file.");
-       if (w->out_fp != NULL) {
-               if (fclose(w->out_fp) != 0) {
-                       ERROR_WITH_ERRNO("Failed to close the file `%s'", path);
-                       ret = WIMLIB_ERR_WRITE;
-               }
-               w->out_fp = NULL;
-       }
-       return ret;
+       return finish_write(w, image, write_flags);
 }