X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fwin32_replacements.c;h=ebf6e20e6cc2d4414b6270df65f7a3a214893982;hb=b5fae0e5ddac332b25856e3df7556aa3ee7f69fb;hp=5b3017a33af07437d178b1215dadef2c94b52241;hpb=ca0419160ac10e3bb3836a01d53a9960105dc608;p=wimlib diff --git a/src/win32_replacements.c b/src/win32_replacements.c index 5b3017a3..ebf6e20e 100644 --- a/src/win32_replacements.c +++ b/src/win32_replacements.c @@ -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 #include #include /* for _get_osfhandle() */ +#include #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__ */