From: Eric Biggers Date: Sun, 5 May 2013 03:24:10 +0000 (-0500) Subject: Win32: Use O_BINARY to stop Windows from messing with our data X-Git-Tag: v1.4.0~131 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=3b5ae49e2ed3c288a00f8ad4dc45c443939204f2 Win32: Use O_BINARY to stop Windows from messing with our data --- diff --git a/src/split.c b/src/split.c index 47168c8f..8b67079e 100644 --- a/src/split.c +++ b/src/split.c @@ -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; diff --git a/src/util.h b/src/util.h index 7bff1b5d..935bbba6 100644 --- a/src/util.h +++ b/src/util.h @@ -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 */ diff --git a/src/wim.c b/src/wim.c index c82730c8..07c1a376 100644 --- 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; diff --git a/src/win32.c b/src/win32.c index 43336469..789eacce 100644 --- a/src/win32.c +++ b/src/win32.c @@ -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; } diff --git a/src/write.c b/src/write.c index 1dd35e52..b5954e34 100644 --- a/src/write.c +++ b/src/write.c @@ -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; diff --git a/src/xml.c b/src/xml.c index 6f044d60..23a088e7 100644 --- 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;