]> wimlib.net Git - wimlib/blobdiff - src/split.c
write_xml_data(): Flush data correctly
[wimlib] / src / split.c
index 417b29ed468bfccc769524e3af4df31e8c19f283..b1231b59450d3d5368bc9bcee96fd885bb0e3844 100644 (file)
  * along with wimlib; if not, see http://www.gnu.org/licenses/.
  */
 
-#include "wimlib_internal.h"
-#include "lookup_table.h"
-#include "xml.h"
-#include "buffer_io.h"
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "wimlib.h"
+#include "wimlib/buffer_io.h"
+#include "wimlib/error.h"
+#include "wimlib/file_io.h"
+#include "wimlib/lookup_table.h"
+#include "wimlib/metadata.h"
+#include "wimlib/types.h"
+#include "wimlib/write.h"
+#include "wimlib/list.h"
+
+#include <fcntl.h> /* for open() */
+#include <unistd.h> /* for close() */
 
 struct split_args {
        WIMStruct *w;
@@ -211,6 +223,10 @@ wimlib_split(WIMStruct *w, const tchar *swm_name,
        int total_parts = args.cur_part_number;
        for (int i = 1; i <= total_parts; i++) {
                const tchar *part_name;
+               int part_fd;
+               u8 part_data_buf[4];
+               size_t bytes_written;
+
                if (i == 1) {
                        part_name = swm_name;
                } else {
@@ -219,26 +235,26 @@ wimlib_split(WIMStruct *w, const tchar *swm_name,
                        part_name = swm_base_name;
                }
 
-               FILE *fp = tfopen(part_name, T("r+b"));
-               if (!fp) {
+               part_fd = topen(part_name, O_WRONLY | O_BINARY);
+               if (part_fd == -1) {
                        ERROR_WITH_ERRNO("Failed to open `%"TS"'", part_name);
                        ret = WIMLIB_ERR_OPEN;
                        goto out;
                }
-               u8 buf[4];
-               put_u16(&buf[0], i);
-               put_u16(&buf[2], total_parts);
-
-               if (fseek(fp, 40, SEEK_SET) != 0 ||
-                   fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf) ||
-                   fclose(fp) != 0)
-               {
-                       ERROR_WITH_ERRNO("Error overwriting header of `%"TS"'",
+               put_u16(&part_data_buf[0], i);
+               put_u16(&part_data_buf[2], total_parts);
+
+               bytes_written = full_pwrite(part_fd, part_data_buf,
+                                           sizeof(part_data_buf), 40);
+               ret = close(part_fd);
+               if (bytes_written != sizeof(part_data_buf) || ret != 0) {
+                       ERROR_WITH_ERRNO("Error updating header of `%"TS"'",
                                         part_name);
                        ret = WIMLIB_ERR_WRITE;
-                       break;
+                       goto out;
                }
        }
+       ret = 0;
 out:
        close_wim_writable(w);
        memcpy(&w->hdr, &hdr_save, sizeof(struct wim_header));