From: Eric Biggers Date: Thu, 26 Jun 2014 01:03:50 +0000 (-0500) Subject: Windows replacement for fopen(path, "a") X-Git-Tag: v1.7.1~76 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=7d7e51b225eb2a74d26c089929aadffb1f0549c6 Windows replacement for fopen(path, "a") --- diff --git a/include/wimlib/win32.h b/include/wimlib/win32.h index 9d47ec6c..c8be8c80 100644 --- a/include/wimlib/win32.h +++ b/include/wimlib/win32.h @@ -47,6 +47,9 @@ win32_truncate_replacement(const tchar *path, off_t size); extern int win32_strerror_r_replacement(int errnum, tchar *buf, size_t buflen); +extern FILE * +win32_open_logfile(const wchar_t *path); + extern ssize_t pread(int fd, void *buf, size_t count, off_t offset); diff --git a/src/util.c b/src/util.c index 3b45ce6f..60bab76a 100644 --- a/src/util.c +++ b/src/util.c @@ -210,7 +210,11 @@ wimlib_set_error_file_by_name(const tchar *path) #ifdef ENABLE_ERROR_MESSAGES FILE *fp; - fp = tfopen(path, "a"); +#ifdef __WIN32__ + fp = win32_open_logfile(path); +#else + fp = fopen(path, "a"); +#endif if (!fp) return WIMLIB_ERR_OPEN; wimlib_set_error_file(fp); diff --git a/src/win32_replacements.c b/src/win32_replacements.c index 5b3017a3..30822550 100644 --- a/src/win32_replacements.c +++ b/src/win32_replacements.c @@ -31,6 +31,7 @@ #include #include #include /* for _get_osfhandle() */ +#include #include "wimlib/win32_common.h" @@ -372,4 +373,33 @@ globfree(glob_t *pglob) FREE(pglob->gl_pathv); } +/* Replacement for fopen(path, "a") that doesn't prevent other processes from + * reading the file */ +FILE * +win32_open_logfile(const wchar_t *path) +{ + HANDLE h; + int fd; + FILE *fp; + + h = CreateFile(path, FILE_APPEND_DATA, FILE_SHARE_VALID_FLAGS, + NULL, OPEN_ALWAYS, 0, NULL); + if (h == INVALID_HANDLE_VALUE) + return NULL; + + fd = _open_osfhandle((intptr_t)h, O_APPEND); + if (fd < 0) { + CloseHandle(h); + return NULL; + } + + fp = fdopen(fd, "a"); + if (!fp) { + close(fd); + return NULL; + } + + return fp; +} + #endif /* __WIN32__ */