New upstream version 23.2.1+dfsg1

This commit is contained in:
Simon Chopin 2019-07-27 14:47:10 +02:00
parent cdc9a9fc87
commit b14f9eae6d
1017 changed files with 37232 additions and 11111 deletions

View file

@ -0,0 +1,17 @@
#pragma once
#include "../c99defs.h"
#include "../dstr.h"
#ifdef __cplusplus
extern "C" {
#endif
EXPORT char *cfstr_copy_cstr(CFStringRef cfstr, CFStringEncoding cfstr_enc);
EXPORT bool cfstr_copy_dstr(CFStringRef cfstr, CFStringEncoding cfstr_enc,
struct dstr *str);
#ifdef __cplusplus
}
#endif

View file

@ -292,7 +292,7 @@ static inline void circlebuf_pop_front(struct circlebuf *cb, void *data,
static inline void circlebuf_pop_back(struct circlebuf *cb, void *data,
size_t size)
{
circlebuf_peek_front(cb, data, size);
circlebuf_peek_back(cb, data, size);
cb->size -= size;
if (!cb->size) {
@ -311,7 +311,7 @@ static inline void *circlebuf_data(struct circlebuf *cb, size_t idx)
uint8_t *ptr = (uint8_t*)cb->data;
size_t offset = cb->start_pos + idx;
if (idx > cb->size)
if (idx >= cb->size)
return NULL;
if (offset >= cb->capacity)

View file

@ -359,6 +359,7 @@ int config_save(config_t *config)
FILE *f;
struct dstr str, tmp;
size_t i, j;
int ret = CONFIG_ERROR;
if (!config)
return CONFIG_ERROR;
@ -405,9 +406,15 @@ int config_save(config_t *config)
}
#ifdef _WIN32
fwrite("\xEF\xBB\xBF", 1, 3, f);
if (fwrite("\xEF\xBB\xBF", 3, 1, f) != 1)
goto cleanup;
#endif
fwrite(str.array, 1, str.len, f);
if (fwrite(str.array, str.len, 1, f) != 1)
goto cleanup;
ret = CONFIG_SUCCESS;
cleanup:
fclose(f);
pthread_mutex_unlock(&config->mutex);
@ -415,7 +422,7 @@ int config_save(config_t *config)
dstr_free(&tmp);
dstr_free(&str);
return CONFIG_SUCCESS;
return ret;
}
int config_save_safe(config_t *config, const char *temp_ext,
@ -444,6 +451,8 @@ int config_save_safe(config_t *config, const char *temp_ext,
config->file = file;
if (ret != CONFIG_SUCCESS) {
blog(LOG_ERROR, "config_save_safe: failed to "
"write to %s", temp_file.array);
goto cleanup;
}

View file

@ -73,6 +73,15 @@ size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
return fread(data, 1, len, pp->file);
}
size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len)
{
/* XXX: unsupported on posix */
UNUSED_PARAMETER(pp);
UNUSED_PARAMETER(data);
UNUSED_PARAMETER(len);
return 0;
}
size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
size_t len)
{

View file

@ -24,6 +24,7 @@
struct os_process_pipe {
bool read_pipe;
HANDLE handle;
HANDLE handle_err;
HANDLE process;
};
@ -42,7 +43,7 @@ static bool create_pipe(HANDLE *input, HANDLE *output)
}
static inline bool create_process(const char *cmd_line, HANDLE stdin_handle,
HANDLE stdout_handle, HANDLE *process)
HANDLE stdout_handle, HANDLE stderr_handle, HANDLE *process)
{
PROCESS_INFORMATION pi = {0};
wchar_t *cmd_line_w = NULL;
@ -53,6 +54,7 @@ static inline bool create_process(const char *cmd_line, HANDLE stdin_handle,
si.dwFlags = STARTF_USESTDHANDLES | STARTF_FORCEOFFFEEDBACK;
si.hStdInput = stdin_handle;
si.hStdOutput = stdout_handle;
si.hStdError = stderr_handle;
os_utf8_to_wcs_ptr(cmd_line, 0, &cmd_line_w);
if (cmd_line_w) {
@ -77,6 +79,7 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
bool read_pipe;
HANDLE process;
HANDLE output;
HANDLE err_input, err_output;
HANDLE input;
bool success;
@ -90,26 +93,38 @@ os_process_pipe_t *os_process_pipe_create(const char *cmd_line,
return NULL;
}
if (!create_pipe(&err_input, &err_output)) {
return NULL;
}
read_pipe = *type == 'r';
success = !!SetHandleInformation(read_pipe ? input : output,
HANDLE_FLAG_INHERIT, false);
HANDLE_FLAG_INHERIT, false);
if (!success) {
goto error;
}
success = !!SetHandleInformation(err_input, HANDLE_FLAG_INHERIT, false);
if (!success) {
goto error;
}
success = create_process(cmd_line, read_pipe ? NULL : input,
read_pipe ? output : NULL, &process);
read_pipe ? output : NULL, err_output, &process);
if (!success) {
goto error;
}
pp = bmalloc(sizeof(*pp));
pp->handle = read_pipe ? input : output;
pp->read_pipe = read_pipe;
pp->process = process;
pp->handle_err = err_input;
CloseHandle(read_pipe ? output : input);
CloseHandle(err_output);
return pp;
error:
@ -126,6 +141,7 @@ int os_process_pipe_destroy(os_process_pipe_t *pp)
DWORD code;
CloseHandle(pp->handle);
CloseHandle(pp->handle_err);
WaitForSingleObject(pp->process, INFINITE);
if (GetExitCodeProcess(pp->process, &code))
@ -158,6 +174,25 @@ size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data, size_t len)
return 0;
}
size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data, size_t len)
{
DWORD bytes_read;
bool success;
if (!pp || !pp->handle_err) {
return 0;
}
success = !!ReadFile(pp->handle_err, data, (DWORD)len, &bytes_read, NULL);
if (success && bytes_read) {
return bytes_read;
}
else
bytes_read = GetLastError();
return 0;
}
size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
size_t len)
{

View file

@ -27,5 +27,7 @@ EXPORT int os_process_pipe_destroy(os_process_pipe_t *pp);
EXPORT size_t os_process_pipe_read(os_process_pipe_t *pp, uint8_t *data,
size_t len);
EXPORT size_t os_process_pipe_read_err(os_process_pipe_t *pp, uint8_t *data,
size_t len);
EXPORT size_t os_process_pipe_write(os_process_pipe_t *pp, const uint8_t *data,
size_t len);

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2013-2014 Ruwen Hahn <palana@stunned.de>
* Copyright (c) 2013-2018 Ruwen Hahn <palana@stunned.de>
* Hugh "Jim" Bailey <obs.jim@gmail.com>
* Marvin Scholz <epirat07@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
@ -23,16 +24,20 @@
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <CoreServices/CoreServices.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <mach-o/dyld.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#import <Cocoa/Cocoa.h>
#include "apple/cfstring-utils.h"
/* clock function selection taken from libc++ */
static uint64_t ns_time_simple()
{
@ -137,6 +142,35 @@ char *os_get_program_data_path_ptr(const char *name)
return os_get_path_ptr_internal(name, NSLocalDomainMask);
}
char *os_get_executable_path_ptr(const char *name)
{
char exe[PATH_MAX];
char abs_path[PATH_MAX];
uint32_t size = sizeof(exe);
struct dstr path;
char *slash;
if (_NSGetExecutablePath(exe, &size) != 0) {
return NULL;
}
if (!realpath(exe, abs_path)) {
return NULL;
}
dstr_init_copy(&path, abs_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;
}
struct os_cpu_usage_info {
int64_t last_cpu_time;
int64_t last_sys_time;
@ -417,3 +451,90 @@ uint64_t os_get_proc_virtual_size(void)
return 0;
return taskinfo.virtual_size;
}
/* Obtains a copy of the contents of a CFString in specified encoding.
* Returns char* (must be bfree'd by caller) or NULL on failure.
*/
char *cfstr_copy_cstr(CFStringRef cfstring, CFStringEncoding cfstring_encoding)
{
if (!cfstring)
return NULL;
// Try the quick way to obtain the buffer
const char *tmp_buffer = CFStringGetCStringPtr(cfstring,
cfstring_encoding);
if (tmp_buffer != NULL)
return bstrdup(tmp_buffer);
// The quick way did not work, try the more expensive one
CFIndex length = CFStringGetLength(cfstring);
CFIndex max_size =
CFStringGetMaximumSizeForEncoding(length, cfstring_encoding);
// If result would exceed LONG_MAX, kCFNotFound is returned
if (max_size == kCFNotFound)
return NULL;
// Account for the null terminator
max_size++;
char *buffer = bmalloc(max_size);
if (buffer == NULL) {
return NULL;
}
// Copy CFString in requested encoding to buffer
Boolean success =
CFStringGetCString(cfstring, buffer, max_size, cfstring_encoding);
if (!success) {
bfree(buffer);
buffer = NULL;
}
return buffer;
}
/* Copies the contents of a CFString in specified encoding to a given dstr.
* Returns true on success or false on failure.
* In case of failure, the dstr capacity but not size is changed.
*/
bool cfstr_copy_dstr(CFStringRef cfstring,
CFStringEncoding cfstring_encoding, struct dstr *str)
{
if (!cfstring)
return false;
// Try the quick way to obtain the buffer
const char *tmp_buffer = CFStringGetCStringPtr(cfstring,
cfstring_encoding);
if (tmp_buffer != NULL) {
dstr_copy(str, tmp_buffer);
return true;
}
// The quick way did not work, try the more expensive one
CFIndex length = CFStringGetLength(cfstring);
CFIndex max_size =
CFStringGetMaximumSizeForEncoding(length, cfstring_encoding);
// If result would exceed LONG_MAX, kCFNotFound is returned
if (max_size == kCFNotFound)
return NULL;
// Account for the null terminator
max_size++;
dstr_ensure_capacity(str, max_size);
// Copy CFString in requested encoding to dstr buffer
Boolean success = CFStringGetCString(
cfstring, str->array, max_size, cfstring_encoding);
if (success)
dstr_resize(str, max_size);
return (bool)success;
}

View file

@ -33,6 +33,7 @@
#if !defined(__APPLE__)
#include <sys/times.h>
#include <sys/wait.h>
#include <libgen.h>
#ifdef __FreeBSD__
#include <sys/param.h>
#include <sys/queue.h>
@ -66,7 +67,11 @@ void *os_dlopen(const char *path)
#endif
dstr_cat(&dylib_name, ".so");
#ifdef __APPLE__
void *res = dlopen(dylib_name.array, RTLD_LAZY | RTLD_FIRST);
#else
void *res = dlopen(dylib_name.array, RTLD_LAZY);
#endif
if (!res)
blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
path, dylib_name.array, dlerror());
@ -265,6 +270,32 @@ char *os_get_program_data_path_ptr(const char *name)
return str;
}
char *os_get_executable_path_ptr(const char *name)
{
char exe[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", exe, PATH_MAX);
const char *path_out = NULL;
struct dstr path;
if (count == -1) {
return NULL;
}
path_out = dirname(exe);
if (!path_out) {
return NULL;
}
dstr_init_copy(&path, path_out);
dstr_cat(&path, "/");
if (name && *name) {
dstr_cat(&path, name);
}
return path.array;
}
#endif
bool os_file_exists(const char *path)
@ -848,7 +879,7 @@ static inline bool os_get_proc_memory_usage_internal(statm_t *statm)
if (!f)
return false;
if (fscanf(f, "%ld %ld %ld %ld %ld %ld %ld",
if (fscanf(f, "%lu %lu %lu %lu %lu %lu %lu",
&statm->virtual_size,
&statm->resident_size,
&statm->share_pages,

View file

@ -50,7 +50,7 @@ static inline uint32_t get_winver(void)
if (!winver) {
struct win_version_info ver;
get_win_ver(&ver);
winver = (ver.major << 16) | ver.minor;
winver = (ver.major << 8) | ver.minor;
}
return winver;
@ -92,6 +92,12 @@ void *os_dlopen(const char *path)
if (!h_library) {
DWORD error = GetLastError();
/* don't print error for libraries that aren't meant to be
* dynamically linked */
if (error == ERROR_PROC_NOT_FOUND)
return NULL;
char *message = NULL;
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
@ -289,6 +295,31 @@ char *os_get_program_data_path_ptr(const char *name)
return os_get_path_ptr_internal(name, CSIDL_COMMON_APPDATA);
}
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;
}
bool os_file_exists(const char *path)
{
WIN32_FIND_DATAW wfd;
@ -869,6 +900,11 @@ void get_win_ver(struct win_version_info *info)
*info = ver;
}
uint32_t get_win_ver_int(void)
{
return get_winver();
}
struct os_inhibit_info {
bool active;
};

View file

@ -262,10 +262,19 @@ bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
if (!f)
return false;
if (marker)
fwrite("\xEF\xBB\xBF", 1, 3, f);
if (len)
fwrite(str, 1, len, f);
if (marker) {
if (fwrite("\xEF\xBB\xBF", 3, 1, f) != 1) {
fclose(f);
return false;
}
}
if (len) {
if (fwrite(str, len, 1, f) != 1) {
fclose(f);
return false;
}
}
fflush(f);
fclose(f);
@ -292,6 +301,8 @@ bool os_quick_write_utf8_file_safe(const char *path, const char *str,
dstr_cat(&temp_path, temp_ext);
if (!os_quick_write_utf8_file(temp_path.array, str, len, marker)) {
blog(LOG_ERROR, "os_quick_write_utf8_file_safe: failed to "
"write to %s", temp_path.array);
goto cleanup;
}

View file

@ -111,6 +111,8 @@ EXPORT char *os_get_config_path_ptr(const char *name);
EXPORT int os_get_program_data_path(char *dst, size_t size, const char *name);
EXPORT char *os_get_program_data_path_ptr(const char *name);
EXPORT char *os_get_executable_path_ptr(const char *name);
EXPORT bool os_file_exists(const char *path);
EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size);

View file

@ -253,6 +253,12 @@ void os_set_thread_name(const char *name)
#elif defined(__FreeBSD__)
pthread_set_name_np(pthread_self(), name);
#elif defined(__GLIBC__) && !defined(__MINGW32__)
pthread_setname_np(pthread_self(), name);
if (strlen(name) <= 15) {
pthread_setname_np(pthread_self(), name);
} else {
char *thread_name = bstrdup_n(name, 15);
pthread_setname_np(pthread_self(), thread_name);
bfree(thread_name);
}
#endif
}

View file

@ -41,7 +41,7 @@ size_t utf8_to_wchar(const char *in, size_t insize, wchar_t *out,
if (has_utf8_bom(in)) {
if (i_insize >= 3) {
in += 3;
insize -= 3;
i_insize -= 3;
}
}

View file

@ -51,11 +51,19 @@ static inline util_uint128_t util_add128(util_uint128_t a, util_uint128_t b)
return out;
}
static inline util_uint128_t util_lshift64(uint64_t a, int num)
static inline util_uint128_t util_lshift64_internal_32(uint64_t a)
{
util_uint128_t val;
val.low = a << num;
val.high = a >> (64 - num);
val.low = a << 32;
val.high = a >> 32;
return val;
}
static inline util_uint128_t util_lshift64_internal_64(uint64_t a)
{
util_uint128_t val;
val.low = 0;
val.high = a;
return val;
}
@ -69,13 +77,13 @@ static inline util_uint128_t util_mul64_64(uint64_t a, uint64_t b)
out.high = 0;
m = (a >> 32) * (b & 0xFFFFFFFFULL);
out = util_add128(out, util_lshift64(m, 32));
out = util_add128(out, util_lshift64_internal_32(m));
m = (a & 0xFFFFFFFFULL) * (b >> 32);
out = util_add128(out, util_lshift64(m, 32));
out = util_add128(out, util_lshift64_internal_32(m));
m = (a >> 32) * (b >> 32);
out = util_add128(out, util_lshift64(m, 64));
out = util_add128(out, util_lshift64_internal_64(m));
return out;
}

View file

@ -68,7 +68,7 @@ public:
inline ComPtr<T> &operator=(ComPtr<T> &&c)
{
if (this != &c) {
if (&ptr != &c.ptr) {
Kill();
ptr = c.ptr;
c.ptr = nullptr;

View file

@ -17,7 +17,7 @@
#pragma once
class WinHandle {
HANDLE handle;
HANDLE handle = INVALID_HANDLE_VALUE;
inline void Clear()
{
@ -26,7 +26,7 @@ class WinHandle {
}
public:
inline WinHandle() : handle(NULL) {}
inline WinHandle() {}
inline WinHandle(HANDLE handle_) : handle(handle_) {}
inline ~WinHandle() {Clear();}

View file

@ -32,6 +32,7 @@ struct win_version_info {
EXPORT bool is_64_bit_windows(void);
EXPORT bool get_dll_ver(const wchar_t *lib, struct win_version_info *info);
EXPORT void get_win_ver(struct win_version_info *info);
EXPORT uint32_t get_win_ver_int(void);
#ifdef __cplusplus
}