Win32: Use O_BINARY to stop Windows from messing with our data
authorEric Biggers <ebiggers3@gmail.com>
Sun, 5 May 2013 03:24:10 +0000 (22:24 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sun, 5 May 2013 03:24:10 +0000 (22:24 -0500)
src/split.c
src/util.h
src/wim.c
src/win32.c
src/write.c
src/xml.c

index 47168c8..8b67079 100644 (file)
@@ -225,7 +225,7 @@ wimlib_split(WIMStruct *w, const tchar *swm_name,
                        part_name = swm_base_name;
                }
 
-               part_fd = topen(part_name, O_WRONLY);
+               part_fd = topen(part_name, O_WRONLY | O_BINARY);
                if (part_fd == INVALID_FILEDES) {
                        ERROR_WITH_ERRNO("Failed to open `%"TS"'", part_name);
                        ret = WIMLIB_ERR_OPEN;
index 7bff1b5..935bbba 100644 (file)
@@ -315,6 +315,10 @@ full_writev(int fd, struct iovec *iov, int iovcnt);
 extern off_t
 filedes_offset(filedes_t fd);
 
+#ifndef __WIN32__
+#  define O_BINARY 0
+#endif
+
 #define INVALID_FILEDES (-1)
 
 #endif /* _WIMLIB_UTIL_H */
index c82730c..07c1a37 100644 (file)
--- a/src/wim.c
+++ b/src/wim.c
@@ -389,7 +389,7 @@ do_open_wim(const tchar *filename, filedes_t *fd_ret)
 {
        int fd;
 
-       fd = topen(filename, O_RDONLY);
+       fd = topen(filename, O_RDONLY | O_BINARY);
        if (fd == INVALID_FILEDES) {
                ERROR_WITH_ERRNO("Can't open \"%"TS"\" read-only", filename);
                return WIMLIB_ERR_OPEN;
index 4333646..789eacc 100644 (file)
@@ -2853,10 +2853,11 @@ fsync(int fd)
        if (h == INVALID_HANDLE_VALUE)
                goto err;
        if (!FlushFileBuffers(h))
-               goto err;
+               goto err_set_errno;
        return 0;
-err:
+err_set_errno:
        set_errno_from_GetLastError();
+err:
        return -1;
 }
 
@@ -2985,15 +2986,13 @@ do_pread_or_pwrite(int fd, void *buf, size_t count, off_t offset,
        wimlib_assert(count <= 0xffffffff);
 
        h = (HANDLE)_get_osfhandle(fd);
-       if (h == INVALID_HANDLE_VALUE) {
-               errno = EBADF;
-               return -1;
-       }
+       if (h == INVALID_HANDLE_VALUE)
+               goto err;
 
        /* Get original position */
        relative_offset.QuadPart = 0;
        if (!SetFilePointerEx(h, relative_offset, &orig_offset, FILE_CURRENT))
-               goto err;
+               goto err_set_errno;
 
        memset(&overlapped, 0, sizeof(overlapped));
        overlapped.Offset = offset;
@@ -3005,15 +3004,16 @@ do_pread_or_pwrite(int fd, void *buf, size_t count, off_t offset,
        else
                bret = ReadFile(h, buf, count, &bytes_read_or_written, &overlapped);
        if (!bret)
-               goto err;
+               goto err_set_errno;
 
        /* Restore the original position */
        if (!SetFilePointerEx(h, orig_offset, NULL, FILE_BEGIN))
-               goto err;
+               goto err_set_errno;
 
        return bytes_read_or_written;
-err:
+err_set_errno:
        set_errno_from_GetLastError();
+err:
        return -1;
 }
 
index 1dd35e5..b5954e3 100644 (file)
@@ -1838,7 +1838,7 @@ lock_wim(WIMStruct *w, filedes_t fd)
 static int
 open_wim_writable(WIMStruct *w, const tchar *path, int open_flags)
 {
-       w->out_fd = topen(path, open_flags, 0644);
+       w->out_fd = topen(path, open_flags | O_BINARY, 0644);
        if (w->out_fd == INVALID_FILEDES) {
                ERROR_WITH_ERRNO("Failed to open `%"TS"' for writing", path);
                return WIMLIB_ERR_OPEN;
index 6f044d6..23a088e 100644 (file)
--- a/src/xml.c
+++ b/src/xml.c
@@ -1428,11 +1428,6 @@ write_xml_data(const struct wim_info *wim_info, int image, int out_fd,
 
        DEBUG("Ended XML document");
 
-       /* Call xmlFreeTextWriter() before ftello() because the former will
-        * flush the file stream. */
-       xmlFreeTextWriter(writer);
-       writer = NULL;
-
        end_offset = filedes_offset(out_fd);
        if (end_offset == -1) {
                ret = WIMLIB_ERR_WRITE;