]> wimlib.net Git - wimlib/blobdiff - src/win32_replacements.c
wimlib_iterate_dir_tree(): iterate in default case order
[wimlib] / src / win32_replacements.c
index 5b3017a33af07437d178b1215dadef2c94b52241..ebf6e20e6cc2d4414b6270df65f7a3a214893982 100644 (file)
@@ -6,20 +6,18 @@
 /*
  * Copyright (C) 2013 Eric Biggers
  *
- * This file is part of wimlib, a library for working with WIM files.
+ * This file is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option) any
+ * later version.
  *
- * wimlib is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version.
- *
- * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  * details.
  *
- * You should have received a copy of the GNU General Public License
- * along with wimlib; if not, see http://www.gnu.org/licenses/.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this file; if not, see http://www.gnu.org/licenses/.
  */
 
 #ifdef __WIN32__
@@ -31,6 +29,7 @@
 #include <errno.h>
 #include <pthread.h>
 #include <io.h>        /* for _get_osfhandle()  */
+#include <fcntl.h>
 
 #include "wimlib/win32_common.h"
 
@@ -74,7 +73,7 @@ win32_get_avail_memory(void)
                .dwLength = sizeof(status),
        };
        GlobalMemoryStatusEx(&status);
-       return status.ullTotalPhys;
+       return (u64)min(status.ullTotalPhys, status.ullTotalVirtual) * 85 / 100;
 }
 
 /* Replacement for POSIX-2008 realpath().  Warning: partial functionality only
@@ -372,4 +371,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__ */