Windows replacement for fopen(path, "a")
authorEric Biggers <ebiggers3@gmail.com>
Thu, 26 Jun 2014 01:03:50 +0000 (20:03 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Thu, 26 Jun 2014 01:04:04 +0000 (20:04 -0500)
include/wimlib/win32.h
src/util.c
src/win32_replacements.c

index 9d47ec6cc87990b594be2d2a9975ec9ff127e32a..c8be8c80f8345f0d6a7dfa88e963a7990e763037 100644 (file)
@@ -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 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);
 
 extern ssize_t
 pread(int fd, void *buf, size_t count, off_t offset);
 
index 3b45ce6fa26626e8d9690151a1ee92f879d613c5..60bab76acbb7abcc03c2aaaa1795d5ea497e19c2 100644 (file)
@@ -210,7 +210,11 @@ wimlib_set_error_file_by_name(const tchar *path)
 #ifdef ENABLE_ERROR_MESSAGES
        FILE *fp;
 
 #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);
        if (!fp)
                return WIMLIB_ERR_OPEN;
        wimlib_set_error_file(fp);
index 5b3017a33af07437d178b1215dadef2c94b52241..3082255033b3123cec78f70d965df4cb16e9c39c 100644 (file)
@@ -31,6 +31,7 @@
 #include <errno.h>
 #include <pthread.h>
 #include <io.h>        /* for _get_osfhandle()  */
 #include <errno.h>
 #include <pthread.h>
 #include <io.h>        /* for _get_osfhandle()  */
+#include <fcntl.h>
 
 #include "wimlib/win32_common.h"
 
 
 #include "wimlib/win32_common.h"
 
@@ -372,4 +373,33 @@ globfree(glob_t *pglob)
        FREE(pglob->gl_pathv);
 }
 
        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__ */
 #endif /* __WIN32__ */