]> wimlib.net Git - wimlib/commitdiff
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 47168c8f379b549258b43eb890d05e61ae7a4e79..8b67079e0f507316eba1b91f40ab0aefac143a1a 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 7bff1b5d21c5a4929920992ee45fe226683ec92f..935bbba6c2652939fb14dc7d73f56a2e981be4a2 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 c82730c8a5e9e126b2dc8ea49658578503fc6705..07c1a376d49548a44b6411fb40609b1db2613f5d 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 43336469005f17c89e18a06d35ae4c3ee10cfc07..789eacce6c5adf6dbbb7ddfd5f862053ed894be5 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 1dd35e52c047f14316ac223ee529ce39ba3def3c..b5954e342d81bdd7b2370e261da58f982b92fc45 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 6f044d60a300962bc5447768ba274534120170cf..23a088e74de2320963091d7e5718cab4ce7c7b2b 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;