X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fwrite.c;h=b1a593f8d3f6f6888972e04713e4f2709db7a9eb;hb=e19e6cdb47421f16071fbe933494b12e7ed17504;hp=fd61ae1d747da0e427b8c45e8ebd869db5841385;hpb=16e3b6e5615abcefc9e5bb9607e2804b64d19cc2;p=wimlib diff --git a/src/write.c b/src/write.c index fd61ae1d..b1a593f8 100644 --- a/src/write.c +++ b/src/write.c @@ -85,6 +85,8 @@ WIMLIBAPI int wimlib_overwrite(WIMStruct *w, int write_flags) ret = wimlib_write(w, tmpfile, WIM_ALL_IMAGES, write_flags); if (ret != 0) { ERROR("Failed to write the WIM file `%s'", tmpfile); + if (unlink(tmpfile) != 0) + WARNING("Failed to remove `%s'", tmpfile); return ret; } @@ -413,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. */ @@ -431,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", @@ -470,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); @@ -479,26 +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; + /*ERROR("Failed to write WIM image metadata to `%s'", path);*/ + 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); }