]> wimlib.net Git - wimlib/blobdiff - src/write.c
read_wim_lookup_table(): Ignore metadata entries with refcnt == 0
[wimlib] / src / write.c
index 1a429039b38dd80fb3141705ef8d1a5aa8c7ac84..80022182896cfd36caa9f051bf6715d8c00fdd03 100644 (file)
@@ -2083,9 +2083,11 @@ close_wim_writable(WIMStruct *wim, int write_flags)
 }
 
 /*
+ * finish_write():
+ *
  * Finish writing a WIM file: write the lookup table, xml data, and integrity
- * table, then overwrite the WIM header.  Always closes the WIM file descriptor
- * (wim->out_fd).
+ * table, then overwrite the WIM header.  By default, closes the WIM file
+ * descriptor (@wim->out_fd) in both success and error cases.
  *
  * write_flags is a bitwise OR of the following:
  *
@@ -2095,7 +2097,7 @@ close_wim_writable(WIMStruct *wim, int write_flags)
  *     (public) WIMLIB_WRITE_FLAG_FSYNC:
  *             fsync() the output file before closing it.
  *
- *     (public)  WIMLIB_WRITE_FLAG_PIPABLE:
+ *     (public) WIMLIB_WRITE_FLAG_PIPABLE:
  *             Writing a pipable WIM, possibly to a pipe; include pipable WIM
  *             stream headers before the lookup table and XML data, and also
  *             write the WIM header at the end instead of seeking to the
@@ -2118,6 +2120,9 @@ close_wim_writable(WIMStruct *wim, int write_flags)
  *             Instead of overwriting the WIM header at the beginning of the
  *             file, simply append it to the end of the file.  (Used when
  *             writing to pipe.)
+ *     (private) WIMLIB_WRITE_FLAG_FILE_DESCRIPTOR:
+ *             Do not close the file descriptor @wim->out_fd on either success
+ *             on failure.
  *     (private) WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES:
  *             Use the existing <TOTALBYTES> stored in the in-memory XML
  *             information, rather than setting it to the offset of the XML
@@ -2171,6 +2176,7 @@ finish_write(WIMStruct *wim, int image, int write_flags,
        if (ret)
                goto out_close_wim;
 
+       /* Write integrity table (optional).  */
        if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
                if (write_flags & WIMLIB_WRITE_FLAG_CHECKPOINT_AFTER_XML) {
                        struct wim_header checkpoint_hdr;
@@ -2196,9 +2202,14 @@ finish_write(WIMStruct *wim, int image, int write_flags,
                if (ret)
                        goto out_close_wim;
        } else {
+               /* No integrity table.  */
                zero_resource_entry(&wim->hdr.integrity);
        }
 
+       /* Now that all information in the WIM header has been determined, the
+        * preliminary header written earlier can be overwritten, the header of
+        * the existing WIM file can be overwritten, or the final header can be
+        * written to the end of the pipable WIM.  */
        wim->hdr.flags &= ~WIM_HDR_FLAG_WRITE_IN_PROGRESS;
        hdr_offset = 0;
        if (write_flags & WIMLIB_WRITE_FLAG_HEADER_AT_END)
@@ -2207,6 +2218,12 @@ finish_write(WIMStruct *wim, int image, int write_flags,
        if (ret)
                goto out_close_wim;
 
+       /* Possibly sync file data to disk before closing.  On POSIX systems, it
+        * is necessary to do this before using rename() to overwrite an
+        * existing file with a new file.  Otherwise, data loss would occur if
+        * the system is abruptly terminated when the metadata for the rename
+        * operation has been written to disk, but the new file data has not.
+        */
        if (write_flags & WIMLIB_WRITE_FLAG_FSYNC) {
                if (fsync(wim->out_fd.fd)) {
                        ERROR_WITH_ERRNO("Error syncing data to WIM file");