2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2017-09-28 04:26:09 -07:00
|
|
|
#define PSAPI_VERSION 1
|
2013-09-30 19:37:13 -07:00
|
|
|
#include <windows.h>
|
2014-03-05 09:43:14 -08:00
|
|
|
#include <mmsystem.h>
|
2013-11-01 14:33:00 -07:00
|
|
|
#include <shellapi.h>
|
|
|
|
#include <shlobj.h>
|
2015-11-18 12:09:13 -08:00
|
|
|
#include <intrin.h>
|
2017-06-26 22:54:26 -07:00
|
|
|
#include <psapi.h>
|
2013-11-01 14:33:00 -07:00
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
#include "base.h"
|
|
|
|
#include "platform.h"
|
2014-07-27 01:29:39 -07:00
|
|
|
#include "darray.h"
|
2013-11-01 14:33:00 -07:00
|
|
|
#include "dstr.h"
|
2017-10-08 23:29:34 -07:00
|
|
|
#include "windows/win-registry.h"
|
2015-08-01 14:19:41 -07:00
|
|
|
#include "windows/win-version.h"
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2013-10-28 06:29:12 -07:00
|
|
|
#include "../../deps/w32-pthreads/pthread.h"
|
2013-10-01 18:16:23 -07:00
|
|
|
|
2020-01-04 13:10:51 -08:00
|
|
|
#define MAX_SZ_LEN 256
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
static bool have_clockfreq = false;
|
|
|
|
static LARGE_INTEGER clock_freq;
|
|
|
|
static uint32_t winver = 0;
|
2020-01-04 14:25:28 -08:00
|
|
|
static char win_release_id[MAX_SZ_LEN] = "unavailable";
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
static inline uint64_t get_clockfreq(void)
|
|
|
|
{
|
2018-02-06 06:50:28 -08:00
|
|
|
if (!have_clockfreq) {
|
2013-09-30 19:37:13 -07:00
|
|
|
QueryPerformanceFrequency(&clock_freq);
|
2018-02-06 06:50:28 -08:00
|
|
|
have_clockfreq = true;
|
|
|
|
}
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
return clock_freq.QuadPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t get_winver(void)
|
|
|
|
{
|
|
|
|
if (!winver) {
|
2015-08-01 14:42:39 -07:00
|
|
|
struct win_version_info ver;
|
|
|
|
get_win_ver(&ver);
|
2019-02-05 00:29:29 -08:00
|
|
|
winver = (ver.major << 8) | ver.minor;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
2017-09-28 04:26:09 -07:00
|
|
|
return winver;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void *os_dlopen(const char *path)
|
|
|
|
{
|
2013-11-01 17:01:56 -07:00
|
|
|
struct dstr dll_name;
|
2013-09-30 19:37:13 -07:00
|
|
|
wchar_t *wpath;
|
2015-07-16 13:33:34 -07:00
|
|
|
wchar_t *wpath_slash;
|
2013-09-30 19:37:13 -07:00
|
|
|
HMODULE h_library = NULL;
|
|
|
|
|
2014-03-04 06:07:13 -08:00
|
|
|
if (!path)
|
|
|
|
return NULL;
|
|
|
|
|
2013-11-01 17:01:56 -07:00
|
|
|
dstr_init_copy(&dll_name, path);
|
2015-07-16 13:33:34 -07:00
|
|
|
dstr_replace(&dll_name, "\\", "/");
|
2014-01-26 14:36:15 -08:00
|
|
|
if (!dstr_find(&dll_name, ".dll"))
|
|
|
|
dstr_cat(&dll_name, ".dll");
|
2013-11-01 17:01:56 -07:00
|
|
|
|
2014-03-04 06:07:13 -08:00
|
|
|
os_utf8_to_wcs_ptr(dll_name.array, 0, &wpath);
|
2015-07-16 13:33:34 -07:00
|
|
|
|
|
|
|
/* to make module dependency issues easier to deal with, allow
|
|
|
|
* dynamically loaded libraries on windows to search for dependent
|
|
|
|
* libraries that are within the library's own directory */
|
|
|
|
wpath_slash = wcsrchr(wpath, L'/');
|
|
|
|
if (wpath_slash) {
|
|
|
|
*wpath_slash = 0;
|
|
|
|
SetDllDirectoryW(wpath);
|
|
|
|
*wpath_slash = L'/';
|
|
|
|
}
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
h_library = LoadLibraryW(wpath);
|
|
|
|
bfree(wpath);
|
2013-11-01 17:01:56 -07:00
|
|
|
dstr_free(&dll_name);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2015-07-16 13:33:34 -07:00
|
|
|
if (wpath_slash)
|
|
|
|
SetDllDirectoryW(NULL);
|
|
|
|
|
2016-08-17 11:09:36 -07:00
|
|
|
if (!h_library) {
|
|
|
|
DWORD error = GetLastError();
|
2019-01-12 16:50:55 -08:00
|
|
|
|
|
|
|
/* don't print error for libraries that aren't meant to be
|
|
|
|
* dynamically linked */
|
|
|
|
if (error == ERROR_PROC_NOT_FOUND)
|
|
|
|
return NULL;
|
|
|
|
|
2016-08-17 11:09:36 -07:00
|
|
|
char *message = NULL;
|
|
|
|
|
|
|
|
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
|
2019-06-22 22:13:45 -07:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS |
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
NULL, error,
|
|
|
|
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
|
|
|
(LPSTR)&message, 0, NULL);
|
2016-08-17 11:09:36 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_INFO, "LoadLibrary failed for '%s': %s (%lu)", path,
|
|
|
|
message, error);
|
2016-08-17 11:09:36 -07:00
|
|
|
|
|
|
|
if (message)
|
|
|
|
LocalFree(message);
|
|
|
|
}
|
|
|
|
|
2013-09-30 19:37:13 -07:00
|
|
|
return h_library;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *os_dlsym(void *module, const char *func)
|
|
|
|
{
|
2013-11-01 14:33:00 -07:00
|
|
|
void *handle;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
handle = (void *)GetProcAddress(module, func);
|
2013-11-01 14:33:00 -07:00
|
|
|
|
|
|
|
return handle;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void os_dlclose(void *module)
|
|
|
|
{
|
|
|
|
FreeLibrary(module);
|
|
|
|
}
|
|
|
|
|
2014-07-04 22:20:20 -07:00
|
|
|
union time_data {
|
2019-06-22 22:13:45 -07:00
|
|
|
FILETIME ft;
|
2014-07-04 22:20:20 -07:00
|
|
|
unsigned long long val;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct os_cpu_usage_info {
|
|
|
|
union time_data last_time, last_sys_time, last_user_time;
|
|
|
|
DWORD core_count;
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
os_cpu_usage_info_t *os_cpu_usage_info_start(void)
|
2014-07-04 22:20:20 -07:00
|
|
|
{
|
|
|
|
struct os_cpu_usage_info *info = bzalloc(sizeof(*info));
|
2019-06-22 22:13:45 -07:00
|
|
|
SYSTEM_INFO si;
|
|
|
|
FILETIME dummy;
|
2014-07-04 22:20:20 -07:00
|
|
|
|
|
|
|
GetSystemInfo(&si);
|
|
|
|
GetSystemTimeAsFileTime(&info->last_time.ft);
|
|
|
|
GetProcessTimes(GetCurrentProcess(), &dummy, &dummy,
|
|
|
|
&info->last_sys_time.ft, &info->last_user_time.ft);
|
|
|
|
info->core_count = si.dwNumberOfProcessors;
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
|
2014-07-04 22:20:20 -07:00
|
|
|
{
|
|
|
|
union time_data cur_time, cur_sys_time, cur_user_time;
|
2019-06-22 22:13:45 -07:00
|
|
|
FILETIME dummy;
|
|
|
|
double percent;
|
2014-07-04 22:20:20 -07:00
|
|
|
|
|
|
|
if (!info)
|
|
|
|
return 0.0;
|
|
|
|
|
|
|
|
GetSystemTimeAsFileTime(&cur_time.ft);
|
2019-06-22 22:13:45 -07:00
|
|
|
GetProcessTimes(GetCurrentProcess(), &dummy, &dummy, &cur_sys_time.ft,
|
|
|
|
&cur_user_time.ft);
|
2014-07-04 22:20:20 -07:00
|
|
|
|
|
|
|
percent = (double)(cur_sys_time.val - info->last_sys_time.val +
|
2019-06-22 22:13:45 -07:00
|
|
|
(cur_user_time.val - info->last_user_time.val));
|
2014-07-04 22:20:20 -07:00
|
|
|
percent /= (double)(cur_time.val - info->last_time.val);
|
|
|
|
percent /= (double)info->core_count;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
info->last_time.val = cur_time.val;
|
|
|
|
info->last_sys_time.val = cur_sys_time.val;
|
2014-07-04 22:20:20 -07:00
|
|
|
info->last_user_time.val = cur_user_time.val;
|
|
|
|
|
|
|
|
return percent * 100.0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
|
2014-07-04 22:20:20 -07:00
|
|
|
{
|
|
|
|
if (info)
|
|
|
|
bfree(info);
|
|
|
|
}
|
|
|
|
|
2014-02-09 04:51:06 -08:00
|
|
|
bool os_sleepto_ns(uint64_t time_target)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
uint64_t t = os_gettime_ns();
|
|
|
|
uint32_t milliseconds;
|
|
|
|
|
|
|
|
if (t >= time_target)
|
2014-02-09 04:51:06 -08:00
|
|
|
return false;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
milliseconds = (uint32_t)((time_target - t) / 1000000);
|
2013-09-30 19:37:13 -07:00
|
|
|
if (milliseconds > 1)
|
2019-06-22 22:13:45 -07:00
|
|
|
Sleep(milliseconds - 1);
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
t = os_gettime_ns();
|
|
|
|
if (t >= time_target)
|
2014-02-09 04:51:06 -08:00
|
|
|
return true;
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2015-07-08 02:56:18 -07:00
|
|
|
#if 0
|
2013-09-30 19:37:13 -07:00
|
|
|
Sleep(1);
|
|
|
|
#else
|
|
|
|
Sleep(0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void os_sleep_ms(uint32_t duration)
|
|
|
|
{
|
|
|
|
/* windows 8+ appears to have decreased sleep precision */
|
|
|
|
if (get_winver() >= 0x0602 && duration > 0)
|
|
|
|
duration--;
|
|
|
|
|
|
|
|
Sleep(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t os_gettime_ns(void)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER current_time;
|
|
|
|
double time_val;
|
|
|
|
|
|
|
|
QueryPerformanceCounter(¤t_time);
|
|
|
|
time_val = (double)current_time.QuadPart;
|
|
|
|
time_val *= 1000000000.0;
|
|
|
|
time_val /= (double)get_clockfreq();
|
|
|
|
|
|
|
|
return (uint64_t)time_val;
|
|
|
|
}
|
|
|
|
|
2016-07-06 01:29:05 -07:00
|
|
|
/* returns [folder]\[name] on windows */
|
|
|
|
static int os_get_path_internal(char *dst, size_t size, const char *name,
|
2019-06-22 22:13:45 -07:00
|
|
|
int folder)
|
2015-01-15 23:44:38 -08:00
|
|
|
{
|
|
|
|
wchar_t path_utf16[MAX_PATH];
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path_utf16);
|
2015-01-15 23:44:38 -08:00
|
|
|
|
|
|
|
if (os_wcs_to_utf8(path_utf16, 0, dst, size) != 0) {
|
2015-07-08 09:19:35 -07:00
|
|
|
if (!name || !*name) {
|
|
|
|
return (int)strlen(dst);
|
|
|
|
}
|
|
|
|
|
2015-01-15 23:44:38 -08:00
|
|
|
if (strcat_s(dst, size, "\\") == 0) {
|
|
|
|
if (strcat_s(dst, size, name) == 0) {
|
|
|
|
return (int)strlen(dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-06 01:29:05 -07:00
|
|
|
static char *os_get_path_ptr_internal(const char *name, int folder)
|
2013-11-01 14:33:00 -07:00
|
|
|
{
|
2013-12-23 17:59:54 -08:00
|
|
|
char *ptr;
|
2013-11-01 14:33:00 -07:00
|
|
|
wchar_t path_utf16[MAX_PATH];
|
2013-12-23 17:59:54 -08:00
|
|
|
struct dstr path;
|
2013-11-23 22:35:03 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
SHGetFolderPathW(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path_utf16);
|
2013-11-01 14:33:00 -07:00
|
|
|
|
2014-03-04 06:07:13 -08:00
|
|
|
os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
|
2013-12-23 17:59:54 -08:00
|
|
|
dstr_init_move_array(&path, ptr);
|
|
|
|
dstr_cat(&path, "\\");
|
|
|
|
dstr_cat(&path, name);
|
|
|
|
return path.array;
|
2013-11-01 14:33:00 -07:00
|
|
|
}
|
|
|
|
|
2016-07-06 01:29:05 -07:00
|
|
|
int os_get_config_path(char *dst, size_t size, const char *name)
|
|
|
|
{
|
|
|
|
return os_get_path_internal(dst, size, name, CSIDL_APPDATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *os_get_config_path_ptr(const char *name)
|
|
|
|
{
|
|
|
|
return os_get_path_ptr_internal(name, CSIDL_APPDATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_get_program_data_path(char *dst, size_t size, const char *name)
|
|
|
|
{
|
|
|
|
return os_get_path_internal(dst, size, name, CSIDL_COMMON_APPDATA);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *os_get_program_data_path_ptr(const char *name)
|
|
|
|
{
|
|
|
|
return os_get_path_ptr_internal(name, CSIDL_COMMON_APPDATA);
|
|
|
|
}
|
|
|
|
|
2019-04-26 13:24:06 -07:00
|
|
|
char *os_get_executable_path_ptr(const char *name)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
char *slash;
|
|
|
|
wchar_t path_utf16[MAX_PATH];
|
|
|
|
struct dstr path;
|
|
|
|
|
|
|
|
GetModuleFileNameW(NULL, path_utf16, MAX_PATH);
|
|
|
|
|
|
|
|
os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
|
|
|
|
dstr_init_move_array(&path, ptr);
|
|
|
|
dstr_replace(&path, "\\", "/");
|
|
|
|
slash = strrchr(path.array, '/');
|
|
|
|
if (slash) {
|
|
|
|
size_t len = slash - path.array + 1;
|
|
|
|
dstr_resize(&path, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name && *name) {
|
|
|
|
dstr_cat(&path, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.array;
|
|
|
|
}
|
|
|
|
|
2013-12-12 20:41:46 -08:00
|
|
|
bool os_file_exists(const char *path)
|
|
|
|
{
|
2014-01-20 07:58:58 -08:00
|
|
|
WIN32_FIND_DATAW wfd;
|
2013-12-12 20:41:46 -08:00
|
|
|
HANDLE hFind;
|
|
|
|
wchar_t *path_utf16;
|
|
|
|
|
2014-03-04 06:07:13 -08:00
|
|
|
if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
|
2013-12-12 20:41:46 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
hFind = FindFirstFileW(path_utf16, &wfd);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE)
|
|
|
|
FindClose(hFind);
|
|
|
|
|
|
|
|
bfree(path_utf16);
|
|
|
|
return hFind != INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
2015-10-14 20:16:44 -07:00
|
|
|
size_t os_get_abs_path(const char *path, char *abspath, size_t size)
|
|
|
|
{
|
2020-02-26 18:44:58 -08:00
|
|
|
wchar_t wpath[MAX_PATH];
|
|
|
|
wchar_t wabspath[MAX_PATH];
|
2015-10-14 20:16:44 -07:00
|
|
|
size_t out_len = 0;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (!abspath)
|
|
|
|
return 0;
|
|
|
|
|
2020-02-26 18:44:58 -08:00
|
|
|
len = os_utf8_to_wcs(path, 0, wpath, MAX_PATH);
|
2015-10-14 20:16:44 -07:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
2020-02-26 18:44:58 -08:00
|
|
|
if (_wfullpath(wabspath, wpath, MAX_PATH) != NULL)
|
2015-10-14 20:16:44 -07:00
|
|
|
out_len = os_wcs_to_utf8(wabspath, 0, abspath, size);
|
|
|
|
return out_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *os_get_abs_path_ptr(const char *path)
|
|
|
|
{
|
2020-02-26 18:44:58 -08:00
|
|
|
char *ptr = bmalloc(MAX_PATH);
|
2015-10-14 20:16:44 -07:00
|
|
|
|
2020-02-26 18:44:58 -08:00
|
|
|
if (!os_get_abs_path(path, ptr, MAX_PATH)) {
|
2015-10-14 20:16:44 -07:00
|
|
|
bfree(ptr);
|
|
|
|
ptr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:44:32 -07:00
|
|
|
struct os_dir {
|
2019-06-22 22:13:45 -07:00
|
|
|
HANDLE handle;
|
|
|
|
WIN32_FIND_DATA wfd;
|
|
|
|
bool first;
|
2014-05-14 17:44:32 -07:00
|
|
|
struct os_dirent out;
|
|
|
|
};
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
os_dir_t *os_opendir(const char *path)
|
2014-05-14 17:44:32 -07:00
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
struct dstr path_str = {0};
|
|
|
|
struct os_dir *dir = NULL;
|
2014-05-14 17:44:32 -07:00
|
|
|
WIN32_FIND_DATA wfd;
|
2019-06-22 22:13:45 -07:00
|
|
|
HANDLE handle;
|
|
|
|
wchar_t *w_path;
|
2014-05-14 17:44:32 -07:00
|
|
|
|
|
|
|
dstr_copy(&path_str, path);
|
|
|
|
dstr_cat(&path_str, "/*.*");
|
|
|
|
|
|
|
|
if (os_utf8_to_wcs_ptr(path_str.array, path_str.len, &w_path) > 0) {
|
|
|
|
handle = FindFirstFileW(w_path, &wfd);
|
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
2019-06-22 22:13:45 -07:00
|
|
|
dir = bzalloc(sizeof(struct os_dir));
|
2014-05-14 17:44:32 -07:00
|
|
|
dir->handle = handle;
|
2019-06-22 22:13:45 -07:00
|
|
|
dir->first = true;
|
|
|
|
dir->wfd = wfd;
|
2014-05-14 17:44:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bfree(w_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
dstr_free(&path_str);
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2014-07-27 01:29:39 -07:00
|
|
|
static inline bool is_dir(WIN32_FIND_DATA *wfd)
|
|
|
|
{
|
|
|
|
return !!(wfd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
struct os_dirent *os_readdir(os_dir_t *dir)
|
2014-05-14 17:44:32 -07:00
|
|
|
{
|
|
|
|
if (!dir)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (dir->first) {
|
|
|
|
dir->first = false;
|
|
|
|
} else {
|
|
|
|
if (!FindNextFileW(dir->handle, &dir->wfd))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-22 03:42:43 -07:00
|
|
|
os_wcs_to_utf8(dir->wfd.cFileName, 0, dir->out.d_name,
|
2019-06-22 22:13:45 -07:00
|
|
|
sizeof(dir->out.d_name));
|
2014-05-22 03:42:43 -07:00
|
|
|
|
2014-07-27 01:29:39 -07:00
|
|
|
dir->out.directory = is_dir(&dir->wfd);
|
2014-05-14 17:44:32 -07:00
|
|
|
|
|
|
|
return &dir->out;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_closedir(os_dir_t *dir)
|
2014-05-14 17:44:32 -07:00
|
|
|
{
|
|
|
|
if (dir) {
|
|
|
|
FindClose(dir->handle);
|
|
|
|
bfree(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 14:52:13 -08:00
|
|
|
int64_t os_get_free_space(const char *path)
|
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
ULARGE_INTEGER remainingSpace;
|
|
|
|
char abs_path[512];
|
|
|
|
wchar_t w_abs_path[512];
|
2015-02-20 14:52:13 -08:00
|
|
|
|
|
|
|
if (os_get_abs_path(path, abs_path, 512) > 0) {
|
|
|
|
if (os_utf8_to_wcs(abs_path, 0, w_abs_path, 512) > 0) {
|
2019-06-22 22:13:45 -07:00
|
|
|
BOOL success = GetDiskFreeSpaceExW(
|
|
|
|
w_abs_path, (PULARGE_INTEGER)&remainingSpace,
|
|
|
|
NULL, NULL);
|
2015-02-20 14:52:13 -08:00
|
|
|
if (success)
|
|
|
|
return (int64_t)remainingSpace.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-27 01:29:39 -07:00
|
|
|
static void make_globent(struct os_globent *ent, WIN32_FIND_DATA *wfd,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *pattern)
|
2014-07-27 01:29:39 -07:00
|
|
|
{
|
|
|
|
struct dstr name = {0};
|
|
|
|
struct dstr path = {0};
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
dstr_from_wcs(&name, wfd->cFileName);
|
|
|
|
dstr_copy(&path, pattern);
|
|
|
|
slash = strrchr(path.array, '/');
|
|
|
|
if (slash)
|
|
|
|
dstr_resize(&path, slash + 1 - path.array);
|
|
|
|
else
|
|
|
|
dstr_free(&path);
|
|
|
|
|
|
|
|
dstr_cat_dstr(&path, &name);
|
|
|
|
ent->path = path.array;
|
|
|
|
ent->directory = is_dir(wfd);
|
|
|
|
|
|
|
|
dstr_free(&name);
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_glob(const char *pattern, int flags, os_glob_t **pglob)
|
2014-07-27 01:29:39 -07:00
|
|
|
{
|
|
|
|
DARRAY(struct os_globent) files;
|
2019-06-22 22:13:45 -07:00
|
|
|
HANDLE handle;
|
|
|
|
WIN32_FIND_DATA wfd;
|
|
|
|
int ret = -1;
|
|
|
|
wchar_t *w_path;
|
2014-07-27 01:29:39 -07:00
|
|
|
|
|
|
|
da_init(files);
|
|
|
|
|
|
|
|
if (os_utf8_to_wcs_ptr(pattern, 0, &w_path) > 0) {
|
|
|
|
handle = FindFirstFileW(w_path, &wfd);
|
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
|
|
do {
|
|
|
|
struct os_globent ent = {0};
|
|
|
|
make_globent(&ent, &wfd, pattern);
|
|
|
|
if (ent.path)
|
|
|
|
da_push_back(files, &ent);
|
|
|
|
} while (FindNextFile(handle, &wfd));
|
|
|
|
FindClose(handle);
|
|
|
|
|
|
|
|
*pglob = bmalloc(sizeof(**pglob));
|
|
|
|
(*pglob)->gl_pathc = files.num;
|
|
|
|
(*pglob)->gl_pathv = files.array;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bfree(w_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != 0)
|
|
|
|
*pglob = NULL;
|
|
|
|
|
|
|
|
UNUSED_PARAMETER(flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_globfree(os_glob_t *pglob)
|
2014-07-27 01:29:39 -07:00
|
|
|
{
|
|
|
|
if (pglob) {
|
|
|
|
for (size_t i = 0; i < pglob->gl_pathc; i++)
|
|
|
|
bfree(pglob->gl_pathv[i].path);
|
|
|
|
bfree(pglob->gl_pathv);
|
|
|
|
bfree(pglob);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:44:32 -07:00
|
|
|
int os_unlink(const char *path)
|
|
|
|
{
|
|
|
|
wchar_t *w_path;
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
os_utf8_to_wcs_ptr(path, 0, &w_path);
|
|
|
|
if (!w_path)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
success = !!DeleteFileW(w_path);
|
|
|
|
bfree(w_path);
|
|
|
|
|
|
|
|
return success ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2015-06-29 00:39:21 -07:00
|
|
|
int os_rmdir(const char *path)
|
|
|
|
{
|
|
|
|
wchar_t *w_path;
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
os_utf8_to_wcs_ptr(path, 0, &w_path);
|
|
|
|
if (!w_path)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
success = !!RemoveDirectoryW(w_path);
|
|
|
|
bfree(w_path);
|
|
|
|
|
|
|
|
return success ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2013-11-23 22:35:03 -08:00
|
|
|
int os_mkdir(const char *path)
|
|
|
|
{
|
|
|
|
wchar_t *path_utf16;
|
|
|
|
BOOL success;
|
|
|
|
|
2014-03-04 06:07:13 -08:00
|
|
|
if (!os_utf8_to_wcs_ptr(path, 0, &path_utf16))
|
2013-11-23 22:35:03 -08:00
|
|
|
return MKDIR_ERROR;
|
|
|
|
|
|
|
|
success = CreateDirectory(path_utf16, NULL);
|
|
|
|
bfree(path_utf16);
|
|
|
|
|
|
|
|
if (!success)
|
2019-06-22 22:13:45 -07:00
|
|
|
return (GetLastError() == ERROR_ALREADY_EXISTS) ? MKDIR_EXISTS
|
|
|
|
: MKDIR_ERROR;
|
2013-11-23 22:35:03 -08:00
|
|
|
|
|
|
|
return MKDIR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-06-21 22:33:48 -07:00
|
|
|
int os_rename(const char *old_path, const char *new_path)
|
|
|
|
{
|
|
|
|
wchar_t *old_path_utf16 = NULL;
|
|
|
|
wchar_t *new_path_utf16 = NULL;
|
|
|
|
int code = -1;
|
|
|
|
|
|
|
|
if (!os_utf8_to_wcs_ptr(old_path, 0, &old_path_utf16)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!os_utf8_to_wcs_ptr(new_path, 0, &new_path_utf16)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2017-05-04 18:44:15 -07:00
|
|
|
code = MoveFileExW(old_path_utf16, new_path_utf16,
|
2019-06-22 22:13:45 -07:00
|
|
|
MOVEFILE_REPLACE_EXISTING)
|
|
|
|
? 0
|
|
|
|
: -1;
|
2015-06-21 22:33:48 -07:00
|
|
|
|
|
|
|
error:
|
|
|
|
bfree(old_path_utf16);
|
|
|
|
bfree(new_path_utf16);
|
|
|
|
return code;
|
|
|
|
}
|
2013-10-01 18:16:23 -07:00
|
|
|
|
2017-05-04 18:21:51 -07:00
|
|
|
int os_safe_replace(const char *target, const char *from, const char *backup)
|
|
|
|
{
|
|
|
|
wchar_t *wtarget = NULL;
|
|
|
|
wchar_t *wfrom = NULL;
|
|
|
|
wchar_t *wbackup = NULL;
|
|
|
|
int code = -1;
|
|
|
|
|
|
|
|
if (!target || !from)
|
|
|
|
return -1;
|
|
|
|
if (!os_utf8_to_wcs_ptr(target, 0, &wtarget))
|
|
|
|
return -1;
|
|
|
|
if (!os_utf8_to_wcs_ptr(from, 0, &wfrom))
|
|
|
|
goto fail;
|
|
|
|
if (backup && !os_utf8_to_wcs_ptr(backup, 0, &wbackup))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (ReplaceFileW(wtarget, wfrom, wbackup, 0, NULL, NULL)) {
|
|
|
|
code = 0;
|
|
|
|
} else if (GetLastError() == ERROR_FILE_NOT_FOUND) {
|
2017-05-04 18:44:15 -07:00
|
|
|
code = MoveFileExW(wfrom, wtarget, MOVEFILE_REPLACE_EXISTING)
|
2019-06-22 22:13:45 -07:00
|
|
|
? 0
|
|
|
|
: -1;
|
2017-05-04 18:21:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
bfree(wtarget);
|
|
|
|
bfree(wfrom);
|
|
|
|
bfree(wbackup);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2013-10-01 18:16:23 -07:00
|
|
|
BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
|
|
|
|
{
|
|
|
|
switch (reason) {
|
|
|
|
|
|
|
|
case DLL_PROCESS_ATTACH:
|
2014-03-05 09:43:14 -08:00
|
|
|
timeBeginPeriod(1);
|
|
|
|
#ifdef PTW32_STATIC_LIB
|
2013-10-01 18:16:23 -07:00
|
|
|
pthread_win32_process_attach_np();
|
2014-03-05 09:43:14 -08:00
|
|
|
#endif
|
2013-10-01 18:16:23 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
2014-03-05 09:43:14 -08:00
|
|
|
timeEndPeriod(1);
|
|
|
|
#ifdef PTW32_STATIC_LIB
|
2013-10-01 18:16:23 -07:00
|
|
|
pthread_win32_process_detach_np();
|
2014-03-05 09:43:14 -08:00
|
|
|
#endif
|
2013-10-01 18:16:23 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_THREAD_ATTACH:
|
2014-03-05 09:43:14 -08:00
|
|
|
#ifdef PTW32_STATIC_LIB
|
2013-10-01 18:16:23 -07:00
|
|
|
pthread_win32_thread_attach_np();
|
2014-03-05 09:43:14 -08:00
|
|
|
#endif
|
2013-10-01 18:16:23 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_THREAD_DETACH:
|
2014-03-05 09:43:14 -08:00
|
|
|
#ifdef PTW32_STATIC_LIB
|
2013-10-01 18:16:23 -07:00
|
|
|
pthread_win32_thread_detach_np();
|
2014-03-05 09:43:14 -08:00
|
|
|
#endif
|
2013-10-01 18:16:23 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-12 11:59:07 -07:00
|
|
|
UNUSED_PARAMETER(hinst_dll);
|
|
|
|
UNUSED_PARAMETER(reserved);
|
2013-10-01 18:16:23 -07:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-21 20:21:27 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
os_performance_token_t *os_request_high_performance(const char *reason)
|
2014-08-21 20:21:27 -07:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(reason);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_end_high_performance(os_performance_token_t *token)
|
2014-08-21 20:21:27 -07:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(token);
|
|
|
|
}
|
|
|
|
|
2015-06-29 00:39:56 -07:00
|
|
|
int os_copyfile(const char *file_in, const char *file_out)
|
|
|
|
{
|
|
|
|
wchar_t *file_in_utf16 = NULL;
|
|
|
|
wchar_t *file_out_utf16 = NULL;
|
|
|
|
int code = -1;
|
|
|
|
|
|
|
|
if (!os_utf8_to_wcs_ptr(file_in, 0, &file_in_utf16)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!os_utf8_to_wcs_ptr(file_out, 0, &file_out_utf16)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
code = CopyFileW(file_in_utf16, file_out_utf16, true) ? 0 : -1;
|
|
|
|
|
|
|
|
error:
|
|
|
|
bfree(file_in_utf16);
|
|
|
|
bfree(file_out_utf16);
|
|
|
|
return code;
|
|
|
|
}
|
2015-07-16 00:50:21 -07:00
|
|
|
|
|
|
|
char *os_getcwd(char *path, size_t size)
|
|
|
|
{
|
|
|
|
wchar_t *path_w;
|
|
|
|
DWORD len;
|
|
|
|
|
|
|
|
len = GetCurrentDirectoryW(0, NULL);
|
|
|
|
if (!len)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
path_w = bmalloc((len + 1) * sizeof(wchar_t));
|
|
|
|
GetCurrentDirectoryW(len + 1, path_w);
|
|
|
|
os_wcs_to_utf8(path_w, (size_t)len, path, size);
|
|
|
|
bfree(path_w);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_chdir(const char *path)
|
|
|
|
{
|
|
|
|
wchar_t *path_w = NULL;
|
|
|
|
size_t size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
size = os_utf8_to_wcs_ptr(path, 0, &path_w);
|
|
|
|
if (!path_w)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = SetCurrentDirectoryW(path_w) ? 0 : -1;
|
|
|
|
bfree(path_w);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2015-08-01 14:19:41 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
typedef DWORD(WINAPI *get_file_version_info_size_w_t)(LPCWSTR module,
|
|
|
|
LPDWORD unused);
|
|
|
|
typedef BOOL(WINAPI *get_file_version_info_w_t)(LPCWSTR module, DWORD unused,
|
|
|
|
DWORD len, LPVOID data);
|
|
|
|
typedef BOOL(WINAPI *ver_query_value_w_t)(LPVOID data, LPCWSTR subblock,
|
|
|
|
LPVOID *buf, PUINT sizeout);
|
2015-08-01 14:19:41 -07:00
|
|
|
|
2015-10-03 22:02:47 -07:00
|
|
|
static get_file_version_info_size_w_t get_file_version_info_size = NULL;
|
|
|
|
static get_file_version_info_w_t get_file_version_info = NULL;
|
|
|
|
static ver_query_value_w_t ver_query_value = NULL;
|
|
|
|
static bool ver_initialized = false;
|
|
|
|
static bool ver_initialize_success = false;
|
|
|
|
|
|
|
|
static bool initialize_version_functions(void)
|
2015-08-01 14:19:41 -07:00
|
|
|
{
|
|
|
|
HMODULE ver = GetModuleHandleW(L"version");
|
|
|
|
|
2015-10-03 22:02:47 -07:00
|
|
|
ver_initialized = true;
|
2015-08-01 14:19:41 -07:00
|
|
|
|
|
|
|
if (!ver) {
|
|
|
|
ver = LoadLibraryW(L"version");
|
|
|
|
if (!ver) {
|
2015-10-03 22:02:47 -07:00
|
|
|
blog(LOG_ERROR, "Failed to load windows "
|
|
|
|
"version library");
|
|
|
|
return false;
|
2015-08-01 14:19:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
get_file_version_info_size =
|
|
|
|
(get_file_version_info_size_w_t)GetProcAddress(
|
|
|
|
ver, "GetFileVersionInfoSizeW");
|
|
|
|
get_file_version_info = (get_file_version_info_w_t)GetProcAddress(
|
|
|
|
ver, "GetFileVersionInfoW");
|
|
|
|
ver_query_value =
|
|
|
|
(ver_query_value_w_t)GetProcAddress(ver, "VerQueryValueW");
|
2015-08-01 14:19:41 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
if (!get_file_version_info_size || !get_file_version_info ||
|
2015-08-01 14:19:41 -07:00
|
|
|
!ver_query_value) {
|
2015-10-03 22:02:47 -07:00
|
|
|
blog(LOG_ERROR, "Failed to load windows version "
|
|
|
|
"functions");
|
|
|
|
return false;
|
2015-08-01 14:19:41 -07:00
|
|
|
}
|
|
|
|
|
2015-10-03 22:02:47 -07:00
|
|
|
ver_initialize_success = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get_dll_ver(const wchar_t *lib, struct win_version_info *ver_info)
|
|
|
|
{
|
|
|
|
VS_FIXEDFILEINFO *info = NULL;
|
|
|
|
UINT len = 0;
|
|
|
|
BOOL success;
|
|
|
|
LPVOID data;
|
|
|
|
DWORD size;
|
2016-09-24 09:07:10 -07:00
|
|
|
char utf8_lib[512];
|
2015-10-03 22:02:47 -07:00
|
|
|
|
|
|
|
if (!ver_initialized && !initialize_version_functions())
|
|
|
|
return false;
|
|
|
|
if (!ver_initialize_success)
|
|
|
|
return false;
|
|
|
|
|
2016-09-24 09:07:10 -07:00
|
|
|
os_wcs_to_utf8(lib, 0, utf8_lib, sizeof(utf8_lib));
|
|
|
|
|
2015-10-03 22:02:47 -07:00
|
|
|
size = get_file_version_info_size(lib, NULL);
|
2015-08-01 14:19:41 -07:00
|
|
|
if (!size) {
|
2016-09-24 09:07:10 -07:00
|
|
|
blog(LOG_ERROR, "Failed to get %s version info size", utf8_lib);
|
2015-10-03 22:02:47 -07:00
|
|
|
return false;
|
2015-08-01 14:19:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
data = bmalloc(size);
|
2016-09-19 17:43:31 -07:00
|
|
|
if (!get_file_version_info(lib, 0, size, data)) {
|
2016-09-24 09:07:10 -07:00
|
|
|
blog(LOG_ERROR, "Failed to get %s version info", utf8_lib);
|
2015-08-01 14:19:41 -07:00
|
|
|
bfree(data);
|
2015-10-03 22:02:47 -07:00
|
|
|
return false;
|
2015-08-01 14:19:41 -07:00
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
success = ver_query_value(data, L"\\", (LPVOID *)&info, &len);
|
2015-08-01 14:19:41 -07:00
|
|
|
if (!success || !info || !len) {
|
2019-06-22 22:13:45 -07:00
|
|
|
blog(LOG_ERROR, "Failed to get %s version info value",
|
|
|
|
utf8_lib);
|
2015-08-01 14:19:41 -07:00
|
|
|
bfree(data);
|
2015-10-03 22:02:47 -07:00
|
|
|
return false;
|
2015-08-01 14:19:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ver_info->major = (int)HIWORD(info->dwFileVersionMS);
|
|
|
|
ver_info->minor = (int)LOWORD(info->dwFileVersionMS);
|
|
|
|
ver_info->build = (int)HIWORD(info->dwFileVersionLS);
|
|
|
|
ver_info->revis = (int)LOWORD(info->dwFileVersionLS);
|
|
|
|
|
|
|
|
bfree(data);
|
2015-10-03 22:02:47 -07:00
|
|
|
return true;
|
2015-08-01 14:19:41 -07:00
|
|
|
}
|
|
|
|
|
2016-11-07 12:43:05 -08:00
|
|
|
bool is_64_bit_windows(void)
|
|
|
|
{
|
|
|
|
#if defined(_WIN64)
|
|
|
|
return true;
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
BOOL b64 = false;
|
|
|
|
return IsWow64Process(GetCurrentProcess(), &b64) && b64;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:29:34 -07:00
|
|
|
void get_reg_dword(HKEY hkey, LPCWSTR sub_key, LPCWSTR value_name,
|
2019-06-22 22:13:45 -07:00
|
|
|
struct reg_dword *info)
|
2017-10-08 23:29:34 -07:00
|
|
|
{
|
|
|
|
struct reg_dword reg = {0};
|
|
|
|
HKEY key;
|
|
|
|
LSTATUS status;
|
|
|
|
|
|
|
|
status = RegOpenKeyEx(hkey, sub_key, 0, KEY_READ, &key);
|
|
|
|
|
2017-12-19 03:04:00 -08:00
|
|
|
if (status != ERROR_SUCCESS) {
|
|
|
|
info->status = status;
|
|
|
|
info->size = 0;
|
|
|
|
info->return_value = 0;
|
2017-10-08 23:29:34 -07:00
|
|
|
return;
|
2017-12-19 03:04:00 -08:00
|
|
|
}
|
2017-10-08 23:29:34 -07:00
|
|
|
|
|
|
|
reg.size = sizeof(reg.return_value);
|
|
|
|
|
|
|
|
reg.status = RegQueryValueExW(key, value_name, NULL, NULL,
|
2019-06-22 22:13:45 -07:00
|
|
|
(LPBYTE)®.return_value, ®.size);
|
2017-10-08 23:29:34 -07:00
|
|
|
|
|
|
|
RegCloseKey(key);
|
|
|
|
|
|
|
|
*info = reg;
|
|
|
|
}
|
|
|
|
|
2016-09-19 17:35:59 -07:00
|
|
|
#define WINVER_REG_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
|
|
|
|
|
2020-01-04 13:10:51 -08:00
|
|
|
static inline void rtl_get_ver(struct win_version_info *ver)
|
|
|
|
{
|
|
|
|
RTL_OSVERSIONINFOEXW osver = {0};
|
|
|
|
HMODULE ntdll = GetModuleHandleW(L"ntdll");
|
|
|
|
NTSTATUS s;
|
|
|
|
|
|
|
|
NTSTATUS(WINAPI * get_ver)
|
|
|
|
(RTL_OSVERSIONINFOEXW *) =
|
|
|
|
(void *)GetProcAddress(ntdll, "RtlGetVersion");
|
|
|
|
if (!get_ver) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
osver.dwOSVersionInfoSize = sizeof(osver);
|
|
|
|
s = get_ver(&osver);
|
|
|
|
if (s < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ver->major = osver.dwMajorVersion;
|
|
|
|
ver->minor = osver.dwMinorVersion;
|
|
|
|
ver->build = osver.dwBuildNumber;
|
|
|
|
ver->revis = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool get_reg_sz(HKEY key, const wchar_t *val, wchar_t *buf,
|
|
|
|
const size_t size)
|
|
|
|
{
|
|
|
|
DWORD dwsize = (DWORD)size;
|
|
|
|
LSTATUS status;
|
|
|
|
|
|
|
|
status = RegQueryValueExW(key, val, NULL, NULL, (LPBYTE)buf, &dwsize);
|
|
|
|
buf[(size / sizeof(wchar_t)) - 1] = 0;
|
|
|
|
return status == ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void get_reg_ver(struct win_version_info *ver)
|
|
|
|
{
|
|
|
|
HKEY key;
|
|
|
|
DWORD size, dw_val;
|
|
|
|
LSTATUS status;
|
|
|
|
wchar_t str[MAX_SZ_LEN];
|
|
|
|
|
|
|
|
status = RegOpenKeyW(HKEY_LOCAL_MACHINE, WINVER_REG_KEY, &key);
|
|
|
|
if (status != ERROR_SUCCESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size = sizeof(dw_val);
|
|
|
|
|
|
|
|
status = RegQueryValueExW(key, L"CurrentMajorVersionNumber", NULL, NULL,
|
|
|
|
(LPBYTE)&dw_val, &size);
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
ver->major = (int)dw_val;
|
|
|
|
|
|
|
|
status = RegQueryValueExW(key, L"CurrentMinorVersionNumber", NULL, NULL,
|
|
|
|
(LPBYTE)&dw_val, &size);
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
ver->minor = (int)dw_val;
|
|
|
|
|
|
|
|
status = RegQueryValueExW(key, L"UBR", NULL, NULL, (LPBYTE)&dw_val,
|
|
|
|
&size);
|
|
|
|
if (status == ERROR_SUCCESS)
|
|
|
|
ver->revis = (int)dw_val;
|
|
|
|
|
|
|
|
if (get_reg_sz(key, L"CurrentBuildNumber", str, sizeof(str))) {
|
|
|
|
ver->build = wcstol(str, NULL, 10);
|
|
|
|
}
|
|
|
|
|
2020-01-04 14:25:28 -08:00
|
|
|
if (get_reg_sz(key, L"ReleaseId", str, sizeof(str))) {
|
|
|
|
os_wcs_to_utf8(str, 0, win_release_id, MAX_SZ_LEN);
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:10:51 -08:00
|
|
|
RegCloseKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool version_higher(struct win_version_info *cur,
|
|
|
|
struct win_version_info *new)
|
|
|
|
{
|
|
|
|
if (new->major > cur->major) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new->major == cur->major) {
|
|
|
|
if (new->minor > cur->minor) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (new->minor == cur->minor) {
|
|
|
|
if (new->build > cur->build) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (new->build == cur->build) {
|
|
|
|
return new->revis > cur->revis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void use_higher_ver(struct win_version_info *cur,
|
|
|
|
struct win_version_info *new)
|
|
|
|
{
|
|
|
|
if (version_higher(cur, new))
|
|
|
|
*cur = *new;
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:19:41 -07:00
|
|
|
void get_win_ver(struct win_version_info *info)
|
|
|
|
{
|
|
|
|
static struct win_version_info ver = {0};
|
|
|
|
static bool got_version = false;
|
|
|
|
|
|
|
|
if (!info)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!got_version) {
|
2020-01-04 13:10:51 -08:00
|
|
|
struct win_version_info reg_ver = {0};
|
|
|
|
struct win_version_info rtl_ver = {0};
|
|
|
|
struct win_version_info nto_ver = {0};
|
2016-09-19 17:35:59 -07:00
|
|
|
|
2020-01-04 13:10:51 -08:00
|
|
|
get_reg_ver(®_ver);
|
|
|
|
rtl_get_ver(&rtl_ver);
|
|
|
|
get_dll_ver(L"ntoskrnl.exe", &nto_ver);
|
2016-09-19 17:35:59 -07:00
|
|
|
|
2020-01-04 13:10:51 -08:00
|
|
|
ver = reg_ver;
|
|
|
|
use_higher_ver(&ver, &rtl_ver);
|
|
|
|
use_higher_ver(&ver, &nto_ver);
|
2016-09-19 17:35:59 -07:00
|
|
|
|
2020-01-04 13:10:51 -08:00
|
|
|
got_version = true;
|
2015-08-01 14:19:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*info = ver;
|
|
|
|
}
|
2015-09-12 19:18:03 -07:00
|
|
|
|
2020-01-04 14:25:28 -08:00
|
|
|
const char *get_win_release_id(void)
|
|
|
|
{
|
|
|
|
return win_release_id;
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:41:52 -07:00
|
|
|
uint32_t get_win_ver_int(void)
|
|
|
|
{
|
|
|
|
return get_winver();
|
|
|
|
}
|
|
|
|
|
2015-09-12 19:18:03 -07:00
|
|
|
struct os_inhibit_info {
|
|
|
|
bool active;
|
|
|
|
};
|
|
|
|
|
|
|
|
os_inhibit_t *os_inhibit_sleep_create(const char *reason)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(reason);
|
|
|
|
return bzalloc(sizeof(struct os_inhibit_info));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
|
|
|
|
{
|
|
|
|
if (!info)
|
|
|
|
return false;
|
|
|
|
if (info->active == active)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (active) {
|
2019-06-22 22:13:45 -07:00
|
|
|
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED |
|
|
|
|
ES_AWAYMODE_REQUIRED |
|
|
|
|
ES_DISPLAY_REQUIRED);
|
2015-09-12 19:18:03 -07:00
|
|
|
} else {
|
|
|
|
SetThreadExecutionState(ES_CONTINUOUS);
|
|
|
|
}
|
|
|
|
|
|
|
|
info->active = active;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void os_inhibit_sleep_destroy(os_inhibit_t *info)
|
|
|
|
{
|
|
|
|
if (info) {
|
|
|
|
os_inhibit_sleep_set_active(info, false);
|
|
|
|
bfree(info);
|
|
|
|
}
|
|
|
|
}
|
2015-11-18 12:09:13 -08:00
|
|
|
|
|
|
|
void os_breakpoint(void)
|
|
|
|
{
|
|
|
|
__debugbreak();
|
|
|
|
}
|
2017-04-24 03:17:47 -07:00
|
|
|
|
|
|
|
DWORD num_logical_cores(ULONG_PTR mask)
|
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
DWORD left_shift = sizeof(ULONG_PTR) * 8 - 1;
|
|
|
|
DWORD bit_set_count = 0;
|
|
|
|
ULONG_PTR bit_test = (ULONG_PTR)1 << left_shift;
|
2017-04-24 03:17:47 -07:00
|
|
|
|
|
|
|
for (DWORD i = 0; i <= left_shift; ++i) {
|
|
|
|
bit_set_count += ((mask & bit_test) ? 1 : 0);
|
2019-06-22 22:13:45 -07:00
|
|
|
bit_test /= 2;
|
2017-04-24 03:17:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return bit_set_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int physical_cores = 0;
|
|
|
|
static int logical_cores = 0;
|
|
|
|
static bool core_count_initialized = false;
|
|
|
|
|
|
|
|
static void os_get_cores_internal(void)
|
|
|
|
{
|
|
|
|
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION info = NULL, temp = NULL;
|
|
|
|
DWORD len = 0;
|
|
|
|
|
|
|
|
if (core_count_initialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
core_count_initialized = true;
|
|
|
|
|
|
|
|
GetLogicalProcessorInformation(info, &len);
|
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
return;
|
|
|
|
|
|
|
|
info = malloc(len);
|
|
|
|
|
|
|
|
if (GetLogicalProcessorInformation(info, &len)) {
|
|
|
|
DWORD num = len / sizeof(*info);
|
|
|
|
temp = info;
|
|
|
|
|
|
|
|
for (DWORD i = 0; i < num; i++) {
|
|
|
|
if (temp->Relationship == RelationProcessorCore) {
|
|
|
|
ULONG_PTR mask = temp->ProcessorMask;
|
|
|
|
|
|
|
|
physical_cores++;
|
|
|
|
logical_cores += num_logical_cores(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
temp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_get_physical_cores(void)
|
|
|
|
{
|
|
|
|
if (!core_count_initialized)
|
|
|
|
os_get_cores_internal();
|
|
|
|
return physical_cores;
|
|
|
|
}
|
|
|
|
|
|
|
|
int os_get_logical_cores(void)
|
|
|
|
{
|
|
|
|
if (!core_count_initialized)
|
|
|
|
os_get_cores_internal();
|
|
|
|
return logical_cores;
|
|
|
|
}
|
2017-05-12 00:42:58 -07:00
|
|
|
|
2017-06-26 22:54:26 -07:00
|
|
|
static inline bool os_get_sys_memory_usage_internal(MEMORYSTATUSEX *msex)
|
|
|
|
{
|
|
|
|
if (!GlobalMemoryStatusEx(msex))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t os_get_sys_free_size(void)
|
|
|
|
{
|
|
|
|
MEMORYSTATUSEX msex = {sizeof(MEMORYSTATUSEX)};
|
|
|
|
if (!os_get_sys_memory_usage_internal(&msex))
|
|
|
|
return 0;
|
|
|
|
return msex.ullAvailPhys;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static inline bool
|
|
|
|
os_get_proc_memory_usage_internal(PROCESS_MEMORY_COUNTERS *pmc)
|
2017-06-26 22:54:26 -07:00
|
|
|
{
|
|
|
|
if (!GetProcessMemoryInfo(GetCurrentProcess(), pmc, sizeof(*pmc)))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
|
|
|
|
{
|
|
|
|
PROCESS_MEMORY_COUNTERS pmc = {sizeof(PROCESS_MEMORY_COUNTERS)};
|
|
|
|
if (!os_get_proc_memory_usage_internal(&pmc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
usage->resident_size = pmc.WorkingSetSize;
|
2019-06-22 22:13:45 -07:00
|
|
|
usage->virtual_size = pmc.PagefileUsage;
|
2017-06-26 22:54:26 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t os_get_proc_resident_size(void)
|
|
|
|
{
|
|
|
|
PROCESS_MEMORY_COUNTERS pmc = {sizeof(PROCESS_MEMORY_COUNTERS)};
|
|
|
|
if (!os_get_proc_memory_usage_internal(&pmc))
|
|
|
|
return 0;
|
|
|
|
return pmc.WorkingSetSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t os_get_proc_virtual_size(void)
|
|
|
|
{
|
|
|
|
PROCESS_MEMORY_COUNTERS pmc = {sizeof(PROCESS_MEMORY_COUNTERS)};
|
|
|
|
if (!os_get_proc_memory_usage_internal(&pmc))
|
|
|
|
return 0;
|
|
|
|
return pmc.PagefileUsage;
|
|
|
|
}
|
|
|
|
|
2017-05-12 00:42:58 -07:00
|
|
|
uint64_t os_get_free_disk_space(const char *dir)
|
|
|
|
{
|
|
|
|
wchar_t *wdir = NULL;
|
|
|
|
if (!os_utf8_to_wcs_ptr(dir, 0, &wdir))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ULARGE_INTEGER free;
|
|
|
|
bool success = !!GetDiskFreeSpaceExW(wdir, &free, NULL, NULL);
|
|
|
|
bfree(wdir);
|
|
|
|
|
|
|
|
return success ? free.QuadPart : 0;
|
|
|
|
}
|