2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-09-25 19:37:28 +00:00
|
|
|
#include <algorithm> // std::sort
|
2021-09-13 13:45:01 +00:00
|
|
|
#include <array> // std::array
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cerrno>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <cfloat> // DBL_DIG
|
2022-02-01 01:02:59 +00:00
|
|
|
#include <chrono>
|
2022-01-31 23:05:35 +00:00
|
|
|
#include <clocale> // localeconv()
|
|
|
|
#include <cstdint> // SIZE_MAX
|
|
|
|
#include <cstdlib> // getenv()
|
|
|
|
#include <cstring> /* strerror() */
|
|
|
|
#include <ctime> // nanosleep()
|
2021-09-25 19:37:28 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string_view>
|
2021-09-25 19:37:28 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-12-13 15:22:39 +00:00
|
|
|
#ifdef _WIN32
|
2022-02-01 01:02:59 +00:00
|
|
|
#include <windows.h> /* Sleep(), GetEnvironmentVariable() */
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <shellapi.h> /* CommandLineToArgv() */
|
2022-02-01 01:02:59 +00:00
|
|
|
#include <ws2tcpip.h> /* WSAStartup() */
|
2014-12-13 15:22:39 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-07 11:54:01 +00:00
|
|
|
#ifdef HAVE_ICONV
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <iconv.h>
|
2010-07-07 16:48:23 +00:00
|
|
|
#endif
|
2007-07-18 17:19:14 +00:00
|
|
|
|
2022-02-14 17:54:32 +00:00
|
|
|
#define UTF_CPP_CPLUSPLUS 201703L
|
2021-11-30 21:13:56 +00:00
|
|
|
#include <utf8.h>
|
2022-02-14 17:54:32 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/event.h>
|
2007-11-06 21:14:30 +00:00
|
|
|
|
2022-04-01 00:13:37 +00:00
|
|
|
#include <fmt/format.h>
|
2022-03-14 04:43:35 +00:00
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2022-01-31 23:05:35 +00:00
|
|
|
|
2014-12-10 18:37:58 +00:00
|
|
|
#include "error-types.h"
|
2022-01-31 23:05:35 +00:00
|
|
|
#include "error.h"
|
2022-03-11 21:09:22 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2020-10-13 15:33:56 +00:00
|
|
|
#include "mime-types.h"
|
2022-02-19 18:10:43 +00:00
|
|
|
#include "net.h" // ntohl()
|
2021-10-14 17:07:16 +00:00
|
|
|
#include "platform-quota.h" /* tr_device_info_create(), tr_device_info_get_disk_space(), tr_device_info_free() */
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2022-03-26 00:31:27 +00:00
|
|
|
#include "tr-strbuf.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
time_t __tr_current_time = 0;
|
2008-10-19 17:43:04 +00:00
|
|
|
|
2009-11-26 18:47:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-02-01 01:02:59 +00:00
|
|
|
struct timeval tr_gettimeofday()
|
2013-09-08 18:27:27 +00:00
|
|
|
{
|
2022-02-01 01:02:59 +00:00
|
|
|
auto const d = std::chrono::system_clock::now().time_since_epoch();
|
|
|
|
auto const s = std::chrono::duration_cast<std::chrono::seconds>(d);
|
|
|
|
auto ret = timeval{};
|
|
|
|
ret.tv_sec = s.count();
|
|
|
|
ret.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d - s).count();
|
|
|
|
return ret;
|
2013-09-08 18:27:27 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_malloc(size_t size)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return size != 0 ? malloc(size) : nullptr;
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_malloc0(size_t size)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return size != 0 ? calloc(1, size) : nullptr;
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_realloc(void* p, size_t size)
|
2015-10-25 17:13:14 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
void* result = size != 0 ? realloc(p, size) : nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (result == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2015-10-25 17:13:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_free(void* p)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (p != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
free(p);
|
|
|
|
}
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void* tr_memdup(void const* src, size_t byteCount)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return memcpy(tr_malloc(byteCount), src, byteCount);
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-04-08 00:20:29 +00:00
|
|
|
void tr_timerAdd(struct event& timer, int seconds, int microseconds)
|
2009-06-15 03:24:40 +00:00
|
|
|
{
|
2022-01-28 04:17:11 +00:00
|
|
|
auto tv = timeval{};
|
2017-04-19 12:04:45 +00:00
|
|
|
tv.tv_sec = seconds;
|
|
|
|
tv.tv_usec = microseconds;
|
2009-12-12 01:05:29 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(tv.tv_sec >= 0);
|
|
|
|
TR_ASSERT(tv.tv_usec >= 0);
|
|
|
|
TR_ASSERT(tv.tv_usec < 1000000);
|
2009-12-12 01:05:29 +00:00
|
|
|
|
2022-04-08 00:20:29 +00:00
|
|
|
evtimer_add(&timer, &tv);
|
2009-06-15 03:24:40 +00:00
|
|
|
}
|
2009-06-12 23:01:35 +00:00
|
|
|
|
2022-04-08 00:20:29 +00:00
|
|
|
void tr_timerAddMsec(struct event& timer, int msec)
|
2009-12-10 19:04:10 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const seconds = msec / 1000;
|
|
|
|
int const usec = (msec % 1000) * 1000;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_timerAdd(timer, seconds, usec);
|
2009-12-10 19:04:10 +00:00
|
|
|
}
|
|
|
|
|
2009-06-12 23:01:35 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2022-03-26 00:31:27 +00:00
|
|
|
uint8_t* tr_loadFile(std::string_view path_in, size_t* size, tr_error** error)
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2022-03-26 00:31:27 +00:00
|
|
|
auto const path = tr_pathbuf{ path_in };
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* try to stat the file */
|
2021-10-19 16:09:38 +00:00
|
|
|
auto info = tr_sys_path_info{};
|
|
|
|
tr_error* my_error = nullptr;
|
2022-03-26 00:31:27 +00:00
|
|
|
if (!tr_sys_path_get_info(path.c_str(), 0, &info, &my_error))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-03-28 02:55:30 +00:00
|
|
|
fmt::arg("path", path),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", my_error->message),
|
|
|
|
fmt::arg("error_code", my_error->code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_propagate(error, &my_error);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2007-10-15 20:15:34 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (info.type != TR_SYS_PATH_IS_FILE)
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2022-03-28 02:55:30 +00:00
|
|
|
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path)));
|
2021-12-28 02:32:22 +00:00
|
|
|
tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2007-10-15 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* file size should be able to fit into size_t */
|
2021-11-16 19:25:30 +00:00
|
|
|
if constexpr (sizeof(info.size) > sizeof(*size))
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(info.size <= SIZE_MAX);
|
2007-10-15 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Load the torrent file into our buffer */
|
2022-03-26 00:31:27 +00:00
|
|
|
auto const fd = tr_sys_file_open(path.c_str(), TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-03-28 02:55:30 +00:00
|
|
|
fmt::arg("path", path),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", my_error->message),
|
|
|
|
fmt::arg("error_code", my_error->code)));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_propagate(error, &my_error);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2007-10-15 20:15:34 +00:00
|
|
|
}
|
2015-04-11 10:51:59 +00:00
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* buf = static_cast<uint8_t*>(tr_malloc(info.size + 1));
|
2021-09-15 00:18:09 +00:00
|
|
|
if (!tr_sys_file_read(fd, buf, info.size, nullptr, &my_error))
|
2007-10-15 20:15:34 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-03-28 02:55:30 +00:00
|
|
|
fmt::arg("path", path),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", my_error->message),
|
|
|
|
fmt::arg("error_code", my_error->code)));
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2022-01-24 00:53:35 +00:00
|
|
|
tr_free(buf);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_propagate(error, &my_error);
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2007-10-15 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2017-04-19 12:04:45 +00:00
|
|
|
buf[info.size] = '\0';
|
|
|
|
*size = info.size;
|
|
|
|
return buf;
|
2007-10-15 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 00:31:27 +00:00
|
|
|
bool tr_loadFile(std::string_view path_in, std::vector<char>& setme, tr_error** error)
|
2021-11-16 06:15:13 +00:00
|
|
|
{
|
2022-03-26 00:31:27 +00:00
|
|
|
auto const path = tr_pathbuf{ path_in };
|
2021-11-16 06:15:13 +00:00
|
|
|
|
|
|
|
/* try to stat the file */
|
|
|
|
auto info = tr_sys_path_info{};
|
|
|
|
tr_error* my_error = nullptr;
|
2022-03-26 00:31:27 +00:00
|
|
|
if (!tr_sys_path_get_info(path.c_str(), 0, &info, &my_error))
|
2021-11-16 06:15:13 +00:00
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-03-28 02:55:30 +00:00
|
|
|
fmt::arg("path", path),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", my_error->message),
|
|
|
|
fmt::arg("error_code", my_error->code)));
|
2021-11-16 06:15:13 +00:00
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.type != TR_SYS_PATH_IS_FILE)
|
|
|
|
{
|
2022-03-28 02:55:30 +00:00
|
|
|
tr_logAddError(fmt::format(_("Couldn't read '{path}': Not a regular file"), fmt::arg("path", path)));
|
2021-12-28 02:32:22 +00:00
|
|
|
tr_error_set(error, TR_ERROR_EISDIR, "Not a regular file"sv);
|
2021-11-16 06:15:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the torrent file into our buffer */
|
2022-03-26 00:31:27 +00:00
|
|
|
auto const fd = tr_sys_file_open(path.c_str(), TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
|
2021-11-16 06:15:13 +00:00
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-03-28 02:55:30 +00:00
|
|
|
fmt::arg("path", path),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", my_error->message),
|
|
|
|
fmt::arg("error_code", my_error->code)));
|
2021-11-16 06:15:13 +00:00
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setme.resize(info.size);
|
|
|
|
if (!tr_sys_file_read(fd, std::data(setme), info.size, nullptr, &my_error))
|
|
|
|
{
|
2022-03-14 04:43:35 +00:00
|
|
|
tr_logAddError(fmt::format(
|
2022-03-16 00:51:36 +00:00
|
|
|
_("Couldn't read '{path}': {error} ({error_code})"),
|
2022-03-28 02:55:30 +00:00
|
|
|
fmt::arg("path", path),
|
2022-03-16 00:51:36 +00:00
|
|
|
fmt::arg("error", my_error->message),
|
|
|
|
fmt::arg("error_code", my_error->code)));
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2021-11-16 06:15:13 +00:00
|
|
|
tr_error_propagate(error, &my_error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-27 17:37:29 +00:00
|
|
|
tr_sys_file_close(fd);
|
2021-11-16 06:15:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-26 00:31:27 +00:00
|
|
|
bool tr_saveFile(std::string_view filename_in, std::string_view contents, tr_error** error)
|
2021-12-05 01:32:35 +00:00
|
|
|
{
|
2022-03-26 00:31:27 +00:00
|
|
|
auto const filename = tr_pathbuf{ filename_in };
|
2021-12-05 01:32:35 +00:00
|
|
|
// follow symlinks to find the "real" file, to make sure the temporary
|
|
|
|
// we build with tr_sys_file_open_temp() is created on the right partition
|
2022-03-26 00:31:27 +00:00
|
|
|
if (char* const real_filename = tr_sys_path_resolve(filename.c_str()); real_filename != nullptr)
|
2021-12-05 01:32:35 +00:00
|
|
|
{
|
2022-03-26 00:31:27 +00:00
|
|
|
if (filename_in != real_filename)
|
2022-01-15 19:33:57 +00:00
|
|
|
{
|
2022-03-26 00:31:27 +00:00
|
|
|
auto const saved = tr_saveFile(real_filename, contents, error);
|
|
|
|
tr_free(real_filename);
|
|
|
|
return saved;
|
2022-01-15 19:33:57 +00:00
|
|
|
}
|
2022-03-26 00:31:27 +00:00
|
|
|
|
|
|
|
tr_free(real_filename);
|
2021-12-05 01:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write it to a temp file first.
|
|
|
|
// This is a safeguard against edge cases, e.g. disk full, crash while writing, etc.
|
2022-03-26 00:31:27 +00:00
|
|
|
auto tmp = tr_pathbuf{ filename.sv(), ".tmp.XXXXXX"sv };
|
2021-12-05 01:32:35 +00:00
|
|
|
auto const fd = tr_sys_file_open_temp(std::data(tmp), error);
|
|
|
|
if (fd == TR_BAD_SYS_FILE)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the contents. This might take >1 pass.
|
|
|
|
auto ok = bool{ true };
|
|
|
|
while (!std::empty(contents))
|
|
|
|
{
|
|
|
|
auto n_written = uint64_t{};
|
|
|
|
if (!tr_sys_file_write(fd, std::data(contents), std::size(contents), &n_written, error))
|
|
|
|
{
|
|
|
|
ok = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
contents.remove_prefix(n_written);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we saved it to disk successfully, move it from '.tmp' to the correct filename
|
|
|
|
if (!tr_sys_file_close(fd, error) || !ok || !tr_sys_path_rename(tmp.c_str(), filename.c_str(), error))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-28 02:55:30 +00:00
|
|
|
tr_logAddTrace(fmt::format("Saved '{}'", filename));
|
2021-12-05 01:32:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
tr_disk_space tr_dirSpace(std::string_view dir)
|
2013-02-09 04:05:03 +00:00
|
|
|
{
|
2021-11-13 18:09:14 +00:00
|
|
|
if (std::empty(dir))
|
2013-02-09 04:05:03 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
errno = EINVAL;
|
2021-10-19 16:09:38 +00:00
|
|
|
return { -1, -1 };
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
return tr_device_info_get_disk_space(tr_device_info_create(dir));
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
char* tr_strvDup(std::string_view in)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-11-05 06:29:19 +00:00
|
|
|
auto const n = std::size(in);
|
|
|
|
auto* const ret = tr_new(char, n + 1);
|
|
|
|
std::copy(std::begin(in), std::end(in), ret);
|
|
|
|
ret[n] = '\0';
|
|
|
|
return ret;
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 06:29:19 +00:00
|
|
|
char* tr_strdup(void const* in)
|
|
|
|
{
|
2021-12-16 02:09:46 +00:00
|
|
|
return in == nullptr ? nullptr : tr_strvDup(static_cast<char const*>(in));
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 16:07:22 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
int DoMatch(char const* text, char const* p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* User-level routine. returns whether or not 'text' and 'p' matched */
|
2022-04-08 00:20:29 +00:00
|
|
|
bool tr_wildmat(std::string_view text, std::string_view pattern)
|
2021-09-25 16:07:22 +00:00
|
|
|
{
|
2022-04-08 00:20:29 +00:00
|
|
|
// TODO(ckerr): replace wildmat with base/strings/pattern.cc
|
|
|
|
// wildmat wants these to be zero-terminated.
|
|
|
|
return pattern == "*"sv || DoMatch(std::string{ text }.c_str(), std::string{ pattern }.c_str()) != 0;
|
2021-09-25 16:07:22 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* tr_strerror(int i)
|
2008-02-19 04:16:04 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* ret = strerror(i);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (ret == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = "Unknown Error";
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2008-02-19 04:16:04 +00:00
|
|
|
}
|
|
|
|
|
2008-09-05 19:11:30 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
std::string_view tr_strvStrip(std::string_view str)
|
2021-11-01 18:50:53 +00:00
|
|
|
{
|
|
|
|
auto constexpr test = [](auto ch)
|
|
|
|
{
|
|
|
|
return isspace(ch);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto const it = std::find_if_not(std::begin(str), std::end(str), test);
|
|
|
|
str.remove_prefix(std::distance(std::begin(str), it));
|
|
|
|
|
|
|
|
auto const rit = std::find_if_not(std::rbegin(str), std::rend(str), test);
|
|
|
|
str.remove_suffix(std::distance(std::rbegin(str), rit));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_str_has_suffix(char const* str, char const* suffix)
|
2010-06-07 14:25:31 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (str == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-07 14:25:31 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (suffix == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 16:09:38 +00:00
|
|
|
auto const str_len = strlen(str);
|
|
|
|
auto const suffix_len = strlen(suffix);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (str_len < suffix_len)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2010-06-07 14:25:31 +00:00
|
|
|
|
2019-07-14 12:40:41 +00:00
|
|
|
return evutil_ascii_strncasecmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
|
2010-06-07 14:25:31 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
uint64_t tr_time_msec()
|
2007-07-12 17:51:45 +00:00
|
|
|
{
|
2022-02-01 01:02:59 +00:00
|
|
|
auto const tv = tr_gettimeofday();
|
|
|
|
return uint64_t(tv.tv_sec) * 1000 + (tv.tv_usec / 1000);
|
2007-07-12 17:51:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_wait_msec(long int msec)
|
2007-07-12 17:51:45 +00:00
|
|
|
{
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
Sleep((DWORD)msec);
|
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = msec / 1000;
|
|
|
|
ts.tv_nsec = (msec % 1000) * 1000000;
|
2021-09-15 00:18:09 +00:00
|
|
|
nanosleep(&ts, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2007-07-12 17:51:45 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/*
|
2010-12-27 19:18:17 +00:00
|
|
|
* Copy src to string dst of size siz. At most siz-1 characters
|
|
|
|
* will be copied. Always NUL terminates (unless siz == 0).
|
2012-12-05 17:29:46 +00:00
|
|
|
* Returns strlen (src); if retval >= siz, truncation occurred.
|
2007-11-08 04:11:09 +00:00
|
|
|
*/
|
2021-09-12 17:41:49 +00:00
|
|
|
size_t tr_strlcpy(void* vdst, void const* vsrc, size_t siz)
|
2007-11-08 04:11:09 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* dst = static_cast<char*>(vdst);
|
|
|
|
auto const* const src = static_cast<char const*>(vsrc);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(dst != nullptr);
|
|
|
|
TR_ASSERT(src != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#ifdef HAVE_STRLCPY
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
return strlcpy(dst, src, siz);
|
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#else
|
|
|
|
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* d = dst;
|
2022-01-24 19:07:55 +00:00
|
|
|
auto const* s = src;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t n = siz;
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Copy as many bytes as will fit */
|
|
|
|
if (n != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
while (--n != 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((*d++ = *s++) == '\0')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
|
|
|
if (n == 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (siz != 0)
|
|
|
|
{
|
|
|
|
*d = '\0'; /* NUL-terminate dst */
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (*s++ != '\0')
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2007-11-08 04:11:09 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
return s - (char const*)src - 1; /* count does not include NUL */
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2008-05-18 16:44:30 +00:00
|
|
|
#endif
|
2007-11-08 04:11:09 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 19:13:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
double tr_getRatio(uint64_t numerator, uint64_t denominator)
|
2008-01-04 19:13:30 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (denominator > 0)
|
|
|
|
{
|
2021-10-19 16:09:38 +00:00
|
|
|
return numerator / (double)denominator;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-19 16:09:38 +00:00
|
|
|
|
|
|
|
if (numerator > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-19 16:09:38 +00:00
|
|
|
return TR_RATIO_INF;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-01-04 19:13:30 +00:00
|
|
|
|
2021-10-19 16:09:38 +00:00
|
|
|
return TR_RATIO_NA;
|
2008-01-04 19:13:30 +00:00
|
|
|
}
|
2008-02-25 20:21:22 +00:00
|
|
|
|
2008-03-24 15:58:06 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
void tr_removeElementFromArray(void* array, size_t index_to_remove, size_t sizeof_element, size_t nmemb)
|
2010-10-11 15:41:27 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* a = static_cast<char*>(array);
|
2010-10-11 15:41:27 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
memmove(
|
|
|
|
a + sizeof_element * index_to_remove,
|
|
|
|
a + sizeof_element * (index_to_remove + 1),
|
2017-04-19 12:04:45 +00:00
|
|
|
sizeof_element * (--nmemb - index_to_remove));
|
2010-10-11 15:41:27 +00:00
|
|
|
}
|
|
|
|
|
2009-01-10 22:48:58 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-11-30 21:13:56 +00:00
|
|
|
bool tr_utf8_validate(std::string_view sv, char const** good_end)
|
2009-01-10 22:48:58 +00:00
|
|
|
{
|
2021-11-30 21:13:56 +00:00
|
|
|
auto const* begin = std::data(sv);
|
|
|
|
auto const* const end = begin + std::size(sv);
|
|
|
|
auto const* walk = begin;
|
2021-12-01 23:11:57 +00:00
|
|
|
auto all_good = false;
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2021-11-30 21:13:56 +00:00
|
|
|
try
|
2010-07-07 16:48:23 +00:00
|
|
|
{
|
2021-11-30 21:13:56 +00:00
|
|
|
while (walk < end)
|
|
|
|
{
|
|
|
|
utf8::next(walk, end);
|
|
|
|
}
|
2021-12-01 23:11:57 +00:00
|
|
|
|
|
|
|
all_good = true;
|
2021-11-30 21:13:56 +00:00
|
|
|
}
|
2021-12-01 23:11:57 +00:00
|
|
|
catch (utf8::exception const&)
|
2021-11-30 21:13:56 +00:00
|
|
|
{
|
2021-12-01 23:11:57 +00:00
|
|
|
all_good = false;
|
2021-11-30 21:13:56 +00:00
|
|
|
}
|
2009-01-10 22:48:58 +00:00
|
|
|
|
2021-11-30 21:13:56 +00:00
|
|
|
if (good_end != nullptr)
|
|
|
|
{
|
|
|
|
*good_end = walk;
|
2010-07-07 16:48:23 +00:00
|
|
|
}
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2021-12-01 23:11:57 +00:00
|
|
|
return all_good;
|
2010-07-07 16:48:23 +00:00
|
|
|
}
|
2009-01-10 22:48:58 +00:00
|
|
|
|
2021-11-30 21:13:56 +00:00
|
|
|
static char* strip_non_utf8(std::string_view sv)
|
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* const ret = tr_new(char, std::size(sv) + 1);
|
2021-12-04 00:07:17 +00:00
|
|
|
if (ret != nullptr)
|
|
|
|
{
|
|
|
|
auto const it = utf8::unchecked::replace_invalid(std::data(sv), std::data(sv) + std::size(sv), ret, '?');
|
|
|
|
*it = '\0';
|
|
|
|
}
|
2021-11-30 21:13:56 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* to_utf8(std::string_view sv)
|
2010-07-07 16:48:23 +00:00
|
|
|
{
|
2016-02-07 11:54:01 +00:00
|
|
|
#ifdef HAVE_ICONV
|
2021-11-30 21:13:56 +00:00
|
|
|
size_t const buflen = std::size(sv) * 4 + 10;
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* const out = tr_new(char, buflen);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-10 00:13:47 +00:00
|
|
|
auto constexpr Encodings = std::array<char const*, 2>{ "CURRENT", "ISO-8859-15" };
|
|
|
|
for (auto const* test_encoding : Encodings)
|
2009-01-10 22:48:58 +00:00
|
|
|
{
|
2021-11-10 00:13:47 +00:00
|
|
|
iconv_t cd = iconv_open("UTF-8", test_encoding);
|
|
|
|
if (cd == (iconv_t)-1) // NOLINT(performance-no-int-to-ptr)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-23 05:07:43 +00:00
|
|
|
#ifdef ICONV_SECOND_ARGUMENT_IS_CONST
|
2021-11-30 21:13:56 +00:00
|
|
|
auto const* inbuf = std::data(sv);
|
2016-02-23 05:07:43 +00:00
|
|
|
#else
|
2021-11-30 21:13:56 +00:00
|
|
|
auto* inbuf = const_cast<char*>(std::data(sv));
|
2016-02-23 05:07:43 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
char* outbuf = out;
|
2021-11-30 21:13:56 +00:00
|
|
|
size_t inbytesleft = std::size(sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t outbytesleft = buflen;
|
2021-11-10 00:13:47 +00:00
|
|
|
auto const rv = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
|
|
|
|
iconv_close(cd);
|
|
|
|
if (rv != size_t(-1))
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2022-04-03 18:23:00 +00:00
|
|
|
char* const ret = tr_strvDup({ out, buflen - outbytesleft });
|
2021-11-10 00:13:47 +00:00
|
|
|
tr_free(out);
|
|
|
|
return ret;
|
2010-07-07 16:48:23 +00:00
|
|
|
}
|
2009-04-16 21:00:48 +00:00
|
|
|
}
|
2011-03-28 03:45:31 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(out);
|
|
|
|
|
2010-07-07 16:48:23 +00:00
|
|
|
#endif
|
2009-04-16 21:00:48 +00:00
|
|
|
|
2021-11-30 21:13:56 +00:00
|
|
|
return strip_non_utf8(sv);
|
2010-07-07 16:48:23 +00:00
|
|
|
}
|
2009-01-10 22:48:58 +00:00
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
std::string& tr_strvUtf8Clean(std::string_view cleanme, std::string& setme)
|
2021-11-10 00:13:47 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
if (tr_utf8_validate(cleanme, nullptr))
|
|
|
|
{
|
|
|
|
setme = cleanme;
|
|
|
|
}
|
|
|
|
else
|
2021-11-10 00:13:47 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
auto* const tmp = to_utf8(cleanme);
|
2022-02-07 04:28:36 +00:00
|
|
|
setme.assign(tmp != nullptr ? tmp : "");
|
2022-01-15 19:33:57 +00:00
|
|
|
tr_free(tmp);
|
2021-11-10 00:13:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 19:33:57 +00:00
|
|
|
return setme;
|
2021-11-10 00:13:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-30 20:08:20 +00:00
|
|
|
#ifdef _WIN32
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_win32_native_to_utf8(wchar_t const* text, int text_size)
|
2018-01-24 20:10:21 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_win32_native_to_utf8_ex(text, text_size, 0, 0, nullptr);
|
2018-01-24 20:10:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
char* tr_win32_native_to_utf8_ex(
|
|
|
|
wchar_t const* text,
|
|
|
|
int text_size,
|
|
|
|
int extra_chars_before,
|
|
|
|
int extra_chars_after,
|
2018-01-24 20:10:21 +00:00
|
|
|
int* real_result_size)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char* ret = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
int size;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2018-01-24 20:10:21 +00:00
|
|
|
if (text_size == -1)
|
|
|
|
{
|
|
|
|
text_size = wcslen(text);
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
size = WideCharToMultiByte(CP_UTF8, 0, text, text_size, nullptr, 0, nullptr, nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-01-24 20:10:21 +00:00
|
|
|
ret = tr_new(char, size + extra_chars_before + extra_chars_after + 1);
|
2021-09-15 00:18:09 +00:00
|
|
|
size = WideCharToMultiByte(CP_UTF8, 0, text, text_size, ret + extra_chars_before, size, nullptr, nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2018-01-24 20:10:21 +00:00
|
|
|
ret[size + extra_chars_before + extra_chars_after] = '\0';
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (real_result_size != nullptr)
|
2018-01-24 20:10:21 +00:00
|
|
|
{
|
|
|
|
*real_result_size = size;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
fail:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(ret);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
wchar_t* tr_win32_utf8_to_native(char const* text, int text_size)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return tr_win32_utf8_to_native_ex(text, text_size, 0, 0, nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
wchar_t* tr_win32_utf8_to_native_ex(
|
|
|
|
char const* text,
|
|
|
|
int text_size,
|
|
|
|
int extra_chars_before,
|
|
|
|
int extra_chars_after,
|
2017-04-19 12:04:45 +00:00
|
|
|
int* real_result_size)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
wchar_t* ret = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
int size;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (text_size == -1)
|
|
|
|
{
|
|
|
|
text_size = strlen(text);
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
size = MultiByteToWideChar(CP_UTF8, 0, text, text_size, nullptr, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = tr_new(wchar_t, size + extra_chars_before + extra_chars_after + 1);
|
|
|
|
size = MultiByteToWideChar(CP_UTF8, 0, text, text_size, ret + extra_chars_before, size);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ret[size + extra_chars_before + extra_chars_after] = L'\0';
|
2015-10-23 05:29:47 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (real_result_size != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*real_result_size = size;
|
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
|
|
|
fail:
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(ret);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
return nullptr;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* tr_win32_format_message(uint32_t code)
|
2014-07-03 21:58:39 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
wchar_t* wide_text = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
DWORD wide_size;
|
2021-09-15 00:18:09 +00:00
|
|
|
char* text = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t text_size;
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
wide_size = FormatMessageW(
|
|
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr,
|
2021-08-15 09:41:48 +00:00
|
|
|
code,
|
|
|
|
0,
|
|
|
|
(LPWSTR)&wide_text,
|
|
|
|
0,
|
2021-09-15 00:18:09 +00:00
|
|
|
nullptr);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (wide_size == 0)
|
|
|
|
{
|
2022-04-01 00:13:37 +00:00
|
|
|
return tr_strvDup(fmt::format(FMT_STRING("Unknown error ({:#08x})"), code));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (wide_size != 0 && wide_text != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
text = tr_win32_native_to_utf8(wide_text, wide_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalFree(wide_text);
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (text != nullptr)
|
2015-10-11 16:15:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Most (all?) messages contain "\r\n" in the end, chop it */
|
|
|
|
text_size = strlen(text);
|
|
|
|
|
|
|
|
while (text_size > 0 && isspace((uint8_t)text[text_size - 1]))
|
|
|
|
{
|
|
|
|
text[--text_size] = '\0';
|
|
|
|
}
|
2015-10-11 16:15:19 +00:00
|
|
|
}
|
2014-07-03 21:58:39 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return text;
|
2014-07-03 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_win32_make_args_utf8(int* argc, char*** argv)
|
2014-09-21 18:06:28 +00:00
|
|
|
{
|
2017-05-01 15:46:41 +00:00
|
|
|
int my_argc;
|
2017-04-19 12:04:45 +00:00
|
|
|
wchar_t** my_wide_argv;
|
2014-09-21 18:06:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
my_wide_argv = CommandLineToArgvW(GetCommandLineW(), &my_argc);
|
2014-09-21 18:06:28 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (my_wide_argv == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(*argc == my_argc);
|
2014-09-21 18:06:28 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
char** my_argv = tr_new(char*, my_argc + 1);
|
|
|
|
int processed_argc = 0;
|
2014-09-21 18:06:28 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < my_argc; ++i, ++processed_argc)
|
2014-09-21 18:06:28 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
my_argv[i] = tr_win32_native_to_utf8(my_wide_argv[i], -1);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (my_argv[i] == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
if (processed_argc < my_argc)
|
2014-09-21 18:06:28 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < processed_argc; ++i)
|
2014-09-21 18:06:28 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
tr_free(my_argv[i]);
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(my_argv);
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2014-09-21 18:06:28 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
my_argv[my_argc] = nullptr;
|
2014-09-21 18:06:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*argc = my_argc;
|
|
|
|
*argv = my_argv;
|
2014-09-21 18:06:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* TODO: Add atexit handler to cleanup? */
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
LocalFree(my_wide_argv);
|
2014-09-21 18:06:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
int tr_main_win32(int argc, char** argv, int (*real_main)(int, char**))
|
2015-04-21 10:07:57 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_win32_make_args_utf8(&argc, &argv);
|
|
|
|
SetConsoleCP(CP_UTF8);
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
2017-04-30 16:30:03 +00:00
|
|
|
return (*real_main)(argc, argv);
|
2015-04-21 10:07:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 21:58:39 +00:00
|
|
|
#endif
|
|
|
|
|
2009-02-09 17:25:48 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct number_range
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int low;
|
|
|
|
int high;
|
2009-02-09 17:25:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should be a single number (ex. "6") or a range (ex. "6-9").
|
|
|
|
* Anything else is an error and will return failure.
|
|
|
|
*/
|
2021-10-15 22:15:33 +00:00
|
|
|
static bool parseNumberSection(std::string_view str, number_range& range)
|
2009-02-09 17:25:48 +00:00
|
|
|
{
|
2021-12-29 08:28:12 +00:00
|
|
|
auto constexpr Delimiter = "-"sv;
|
|
|
|
|
|
|
|
auto const first = tr_parseNum<size_t>(str);
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
return false;
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
2021-12-29 08:28:12 +00:00
|
|
|
|
|
|
|
range.low = range.high = *first;
|
|
|
|
if (std::empty(str))
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-12-29 08:28:12 +00:00
|
|
|
return true;
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
2021-12-29 08:28:12 +00:00
|
|
|
|
|
|
|
if (!tr_strvStartsWith(str, Delimiter))
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-12-29 08:28:12 +00:00
|
|
|
return false;
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 08:28:12 +00:00
|
|
|
str.remove_prefix(std::size(Delimiter));
|
|
|
|
auto const second = tr_parseNum<size_t>(str);
|
|
|
|
if (!second)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
range.high = *second;
|
|
|
|
return true;
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a string like "1-4" or "1-4,6,9,14-51", this allocates and returns an
|
|
|
|
* array of setmeCount ints of all the values in the array.
|
|
|
|
* For example, "5-8" will return [ 5, 6, 7, 8 ] and setmeCount will be 4.
|
2012-12-05 17:29:46 +00:00
|
|
|
* It's the caller's responsibility to call tr_free () on the returned array.
|
2021-10-06 16:32:17 +00:00
|
|
|
* If a fragment of the string can't be parsed, nullptr is returned.
|
2009-02-09 17:25:48 +00:00
|
|
|
*/
|
2021-10-15 22:15:33 +00:00
|
|
|
std::vector<int> tr_parseNumberRange(std::string_view str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
auto values = std::set<int>{};
|
2021-11-10 00:13:47 +00:00
|
|
|
auto token = std::string_view{};
|
|
|
|
auto range = number_range{};
|
|
|
|
while (tr_strvSep(&str, &token, ',') && parseNumberSection(token, range))
|
2009-02-09 17:25:48 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
for (auto i = range.low; i <= range.high; ++i)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-09-25 19:37:28 +00:00
|
|
|
values.insert(i);
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 19:37:28 +00:00
|
|
|
return { std::begin(values), std::end(values) };
|
2009-02-09 17:25:48 +00:00
|
|
|
}
|
2009-03-04 16:16:02 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
double tr_truncd(double x, int precision)
|
2009-03-04 16:16:02 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
auto buf = std::array<char, 128>{};
|
|
|
|
auto const [out, len] = fmt::format_to_n(std::data(buf), std::size(buf) - 1, "{:.{}f}", x, DBL_DIG);
|
|
|
|
*out = '\0';
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
if (auto* const pt = strstr(std::data(buf), localeconv()->decimal_point); pt != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
pt[precision != 0 ? precision + 1 : 0] = '\0';
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
return atof(std::data(buf));
|
2010-06-24 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string tr_strpercent(double x)
|
2010-06-22 22:30:58 +00:00
|
|
|
{
|
2022-01-25 16:52:22 +00:00
|
|
|
if (x < 5.0)
|
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
return fmt::format("{:.2f}", tr_truncd(x, 2));
|
2022-01-25 16:52:22 +00:00
|
|
|
}
|
2022-04-04 18:36:48 +00:00
|
|
|
|
|
|
|
if (x < 100.0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
return fmt::format("{:.1f}", tr_truncd(x, 1));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
return fmt::format("{:.0f}", x);
|
2010-06-22 22:30:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string tr_strratio(double ratio, char const* infinity)
|
2009-03-04 16:16:02 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if ((int)ratio == TR_RATIO_NA)
|
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
return _("None");
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-12-31 00:30:21 +00:00
|
|
|
|
|
|
|
if ((int)ratio == TR_RATIO_INF)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
tr_strlcpy(std::data(buf), infinity, std::size(buf));
|
|
|
|
return std::data(buf);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
return tr_strpercent(ratio);
|
2010-06-22 22:30:58 +00:00
|
|
|
}
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-04-08 00:20:29 +00:00
|
|
|
bool tr_moveFile(std::string_view oldpath_in, std::string_view newpath_in, tr_error** error)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2022-04-08 00:20:29 +00:00
|
|
|
auto const oldpath = tr_pathbuf{ oldpath_in };
|
|
|
|
auto const newpath = tr_pathbuf{ newpath_in };
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2022-04-08 00:20:29 +00:00
|
|
|
// make sure the old file exists
|
|
|
|
auto info = tr_sys_path_info{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_sys_path_get_info(oldpath, 0, &info, error))
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_error_prefix(error, "Unable to get information on old file: ");
|
|
|
|
return false;
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
if (info.type != TR_SYS_PATH_IS_FILE)
|
2009-10-27 23:00:34 +00:00
|
|
|
{
|
2021-12-28 02:32:22 +00:00
|
|
|
tr_error_set(error, TR_ERROR_EINVAL, "Old path does not point to a file."sv);
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2009-10-27 23:00:34 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-03-21 20:22:50 +00:00
|
|
|
// ensure the target directory exists
|
2022-04-08 00:20:29 +00:00
|
|
|
if (auto const newdir = tr_sys_path_dirname(newpath, error);
|
|
|
|
std::empty(newdir) || !tr_sys_dir_create(newdir.c_str(), TR_SYS_DIR_CREATE_PARENTS, 0777, error))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-08 00:20:29 +00:00
|
|
|
tr_error_prefix(error, "Unable to create directory for new file: ");
|
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* they might be on the same filesystem... */
|
2022-03-27 17:37:29 +00:00
|
|
|
if (tr_sys_path_rename(oldpath, newpath))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
/* Otherwise, copy the file. */
|
|
|
|
if (!tr_sys_path_copy(oldpath, newpath, error))
|
2014-12-10 18:37:58 +00:00
|
|
|
{
|
Add in-kernel file copying for several platforms. (#1092)
* Add in-kernel copying support for Linux (sendfile64(2), copy_file_range(2)), FreeBSD 13 (copy_file_range(2)), MacOS (copyfile(2)), and Windows (CopyFileExA).
* Fix macro name USE_COPY_FILE_RANGE.
* Minor bugfixes for userspace fallback.
* Fix linux sendfile64 bugs.
* Remove some overzealous asserts.
* Allow transmission-test-copy to take an optional argument for an external reference file.
* Fix return value error of tr_sys_path_copy.
* Use COPYFILE_ALL for Macs without COPYFILE_CLONE.
* Add in-kernel file copying for several platforms.
Numerous operating systems now have support for copying files directly in the
kernel, which is generally more efficient than copying in a userspace read(2)/
write(2) loop. (This becomes particularly relevant for 4th gen PCI-E storage,
which approaches the latency of DRAM.) For Linux I use sendfile64(2), and, for
later kernels, copy_file_range(2). FreeBSD 13 will also support
copy_file_range(2). MacOS has copyfile(2), and Windows has CopyFileExA.
Operating systems lacking such a syscall continue to use the existing
read(2)/write(2) loop.
* Appease uncrustify.
* Appease uncrustify.
* copy-test: generate random content at run time.
* copy-test: Stylistic changes and more check()s.
* copy-test: files_are_identical should follow test idioms
* tr_sys_path_copy: numerous tweaks as requested by review.
* s/old file/source file; s/new file/destination file.
* tr_sys_path_copy: handle win32 wide characters in paths.
* Uncrustify.
* test-copy: Use non-string create_file_with_contents.
* tr_sys_path_copy: numerous fixes.
Per review: generate test file content at runtime; tidy use of check();
fix style; re-measure file sizes in the copy; define a macro when the
system does not provide it; use Unicode APIs on Windows; and fix
documentation.
* Updated as per comments.
* Rebase kernel-copy changes onto 3.0 with gtest.
* Undo irrelevant comment change.
* Fix syntax error.
* Use tr_malloc() instead of tr_valloc().
* Use EXPECT instead of TR_ASSERT in gtest.
* Add error handling.
* Acceptable coding style has changed again.
Now it's camelCase. Also use nullptr instead of NULL, etc.
* Fix east/west const.
Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
2021-08-07 09:04:03 +00:00
|
|
|
tr_error_prefix(error, "Unable to copy: ");
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2014-12-10 18:37:58 +00:00
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2022-04-02 22:42:51 +00:00
|
|
|
if (tr_error* my_error = nullptr; !tr_sys_path_remove(oldpath, &my_error))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-02 22:42:51 +00:00
|
|
|
tr_logAddError(fmt::format(
|
|
|
|
_("Couldn't remove '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", oldpath),
|
|
|
|
fmt::arg("error", my_error->message),
|
|
|
|
fmt::arg("error_code", my_error->code)));
|
|
|
|
tr_error_free(my_error);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2014-12-10 18:37:58 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
2010-02-23 06:06:45 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t tr_htonll(uint64_t x)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2011-03-13 20:23:23 +00:00
|
|
|
#ifdef HAVE_HTONLL
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
return htonll(x);
|
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
/* fallback code by bdonlan at https://stackoverflow.com/questions/809902/64-bit-ntohl-in-c/875505#875505 */
|
2017-04-19 12:04:45 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
uint32_t lx[2];
|
|
|
|
uint64_t llx;
|
2021-08-15 09:41:48 +00:00
|
|
|
} u;
|
2017-04-19 12:04:45 +00:00
|
|
|
u.lx[0] = htonl(x >> 32);
|
|
|
|
u.lx[1] = htonl(x & 0xFFFFFFFFULL);
|
|
|
|
return u.llx;
|
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
uint64_t tr_ntohll(uint64_t x)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2011-03-13 20:23:23 +00:00
|
|
|
#ifdef HAVE_NTOHLL
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
return ntohll(x);
|
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
#else
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
/* fallback code by bdonlan at https://stackoverflow.com/questions/809902/64-bit-ntohl-in-c/875505#875505 */
|
2017-04-19 12:04:45 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
uint32_t lx[2];
|
|
|
|
uint64_t llx;
|
2021-08-15 09:41:48 +00:00
|
|
|
} u;
|
2017-04-19 12:04:45 +00:00
|
|
|
u.llx = x;
|
|
|
|
return ((uint64_t)ntohl(u.lx[0]) << 32) | (uint64_t)ntohl(u.lx[1]);
|
|
|
|
|
2011-03-13 00:18:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-06-22 04:34:16 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
****
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-06-22 13:34:46 +00:00
|
|
|
struct formatter_unit
|
|
|
|
{
|
2022-02-01 05:59:43 +00:00
|
|
|
std::array<char, 16> name;
|
2021-10-26 21:06:47 +00:00
|
|
|
uint64_t value;
|
2010-06-22 13:34:46 +00:00
|
|
|
};
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
using formatter_units = std::array<formatter_unit, 4>;
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
TR_FMT_KB,
|
|
|
|
TR_FMT_MB,
|
|
|
|
TR_FMT_GB,
|
|
|
|
TR_FMT_TB
|
|
|
|
};
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2021-10-26 21:06:47 +00:00
|
|
|
static void formatter_init(
|
|
|
|
formatter_units& units,
|
|
|
|
uint64_t kilo,
|
|
|
|
char const* kb,
|
|
|
|
char const* mb,
|
|
|
|
char const* gb,
|
|
|
|
char const* tb)
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
2021-10-26 21:06:47 +00:00
|
|
|
uint64_t value = kilo;
|
2022-02-01 05:59:43 +00:00
|
|
|
tr_strlcpy(std::data(units[TR_FMT_KB].name), kb, std::size(units[TR_FMT_KB].name));
|
2021-10-14 15:36:49 +00:00
|
|
|
units[TR_FMT_KB].value = value;
|
2010-06-22 13:34:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
value *= kilo;
|
2022-02-01 05:59:43 +00:00
|
|
|
tr_strlcpy(std::data(units[TR_FMT_MB].name), mb, std::size(units[TR_FMT_MB].name));
|
2021-10-14 15:36:49 +00:00
|
|
|
units[TR_FMT_MB].value = value;
|
2010-06-22 13:34:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
value *= kilo;
|
2022-02-01 05:59:43 +00:00
|
|
|
tr_strlcpy(std::data(units[TR_FMT_GB].name), gb, std::size(units[TR_FMT_GB].name));
|
2021-10-14 15:36:49 +00:00
|
|
|
units[TR_FMT_GB].value = value;
|
2010-07-06 20:25:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
value *= kilo;
|
2022-02-01 05:59:43 +00:00
|
|
|
tr_strlcpy(std::data(units[TR_FMT_TB].name), tb, std::size(units[TR_FMT_TB].name));
|
2021-10-14 15:36:49 +00:00
|
|
|
units[TR_FMT_TB].value = value;
|
2010-06-22 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 21:06:47 +00:00
|
|
|
static char* formatter_get_size_str(formatter_units const& u, char* buf, uint64_t bytes, size_t buflen)
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
2021-10-19 16:09:38 +00:00
|
|
|
formatter_unit const* unit = nullptr;
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
if (bytes < u[1].value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-24 02:44:14 +00:00
|
|
|
unit = std::data(u);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 15:36:49 +00:00
|
|
|
else if (bytes < u[2].value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 15:36:49 +00:00
|
|
|
unit = &u[1];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-14 15:36:49 +00:00
|
|
|
else if (bytes < u[3].value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-14 15:36:49 +00:00
|
|
|
unit = &u[2];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-14 15:36:49 +00:00
|
|
|
unit = &u[3];
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2022-02-01 05:59:43 +00:00
|
|
|
double value = double(bytes) / unit->value;
|
|
|
|
auto const* const units = std::data(unit->name);
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2021-10-19 16:09:38 +00:00
|
|
|
auto precision = int{};
|
2017-04-19 12:04:45 +00:00
|
|
|
if (unit->value == 1)
|
|
|
|
{
|
|
|
|
precision = 0;
|
|
|
|
}
|
|
|
|
else if (value < 100)
|
|
|
|
{
|
|
|
|
precision = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
precision = 1;
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
auto const [out, len] = fmt::format_to_n(buf, buflen - 1, "{:.{}f} {:s}", value, precision, units);
|
|
|
|
*out = '\0';
|
2017-04-19 12:04:45 +00:00
|
|
|
return buf;
|
2010-06-22 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
static formatter_units size_units;
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2021-10-26 21:06:47 +00:00
|
|
|
void tr_formatter_size_init(uint64_t kilo, char const* kb, char const* mb, char const* gb, char const* tb)
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
2021-10-14 15:36:49 +00:00
|
|
|
formatter_init(size_units, kilo, kb, mb, gb, tb);
|
2010-06-22 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string tr_formatter_size_B(uint64_t bytes)
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
return formatter_get_size_str(size_units, std::data(buf), bytes, std::size(buf));
|
2010-06-22 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
static formatter_units speed_units;
|
2010-06-22 04:34:16 +00:00
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
size_t tr_speed_K = 0;
|
2010-07-04 06:07:21 +00:00
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
void tr_formatter_speed_init(size_t kilo, char const* kb, char const* mb, char const* gb, char const* tb)
|
2010-06-22 04:34:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_speed_K = kilo;
|
2021-10-14 15:36:49 +00:00
|
|
|
formatter_init(speed_units, kilo, kb, mb, gb, tb);
|
2010-06-22 04:34:16 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string tr_formatter_speed_KBps(double KBps)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
auto speed = KBps;
|
2021-12-31 00:30:21 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
if (speed <= 999.95) // 0.0 KB to 999.9 KB
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2022-04-04 18:36:48 +00:00
|
|
|
return fmt::format("{:d} {:s}", int(speed), std::data(speed_units[TR_FMT_KB].name));
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
2021-12-17 05:47:51 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
double const K = speed_units[TR_FMT_KB].value;
|
|
|
|
speed /= K;
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
if (speed <= 99.995) // 0.98 MB to 99.99 MB
|
|
|
|
{
|
|
|
|
return fmt::format("{:.2f} {:s}", speed, std::data(speed_units[TR_FMT_MB].name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (speed <= 999.95) // 100.0 MB to 999.9 MB
|
|
|
|
{
|
|
|
|
return fmt::format("{:.1f} {:s}", speed, std::data(speed_units[TR_FMT_MB].name));
|
2010-07-08 03:20:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 18:36:48 +00:00
|
|
|
return fmt::format("{:.1f} {:s}", speed / K, std::data(speed_units[TR_FMT_GB].name));
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
static formatter_units mem_units;
|
2010-07-03 00:25:22 +00:00
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
size_t tr_mem_K = 0;
|
2010-11-11 15:31:11 +00:00
|
|
|
|
2020-09-13 21:43:29 +00:00
|
|
|
void tr_formatter_mem_init(size_t kilo, char const* kb, char const* mb, char const* gb, char const* tb)
|
2010-07-03 00:25:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_mem_K = kilo;
|
2021-10-14 15:36:49 +00:00
|
|
|
formatter_init(mem_units, kilo, kb, mb, gb, tb);
|
2010-07-03 00:25:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 00:30:21 +00:00
|
|
|
std::string tr_formatter_mem_B(size_t bytes_per_second)
|
2010-06-22 13:34:46 +00:00
|
|
|
{
|
2021-12-31 00:30:21 +00:00
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
return formatter_get_size_str(mem_units, std::data(buf), bytes_per_second, std::size(buf));
|
2010-06-22 13:34:46 +00:00
|
|
|
}
|
2010-09-18 23:06:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_formatter_get_units(void* vdict)
|
2010-09-18 23:06:03 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* dict = static_cast<tr_variant*>(vdict);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_variantDictReserve(dict, 6);
|
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
tr_variantDictAddInt(dict, TR_KEY_memory_bytes, mem_units[TR_FMT_KB].value);
|
2021-10-19 16:09:38 +00:00
|
|
|
tr_variant* l = tr_variantDictAddList(dict, TR_KEY_memory_units, std::size(mem_units));
|
2021-10-14 15:36:49 +00:00
|
|
|
for (auto const& unit : mem_units)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-01 05:59:43 +00:00
|
|
|
tr_variantListAddStr(l, std::data(unit.name));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-09-18 23:06:03 +00:00
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
tr_variantDictAddInt(dict, TR_KEY_size_bytes, size_units[TR_FMT_KB].value);
|
|
|
|
l = tr_variantDictAddList(dict, TR_KEY_size_units, std::size(size_units));
|
|
|
|
for (auto const& unit : size_units)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-01 05:59:43 +00:00
|
|
|
tr_variantListAddStr(l, std::data(unit.name));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-09-18 23:06:03 +00:00
|
|
|
|
2021-10-14 15:36:49 +00:00
|
|
|
tr_variantDictAddInt(dict, TR_KEY_speed_bytes, speed_units[TR_FMT_KB].value);
|
|
|
|
l = tr_variantDictAddList(dict, TR_KEY_speed_units, std::size(speed_units));
|
|
|
|
for (auto const& unit : speed_units)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-01 05:59:43 +00:00
|
|
|
tr_variantListAddStr(l, std::data(unit.name));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-09-18 23:06:03 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 18:05:14 +00:00
|
|
|
/***
|
|
|
|
**** ENVIRONMENT
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool tr_env_key_exists(char const* key)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(key != nullptr);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-09-15 00:18:09 +00:00
|
|
|
return GetEnvironmentVariableA(key, nullptr, 0) != 0;
|
2014-09-21 18:05:14 +00:00
|
|
|
#else
|
2021-09-15 00:18:09 +00:00
|
|
|
return getenv(key) != nullptr;
|
2014-09-21 18:05:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_env_get_int(char const* key, int default_value)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(key != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2014-09-21 18:05:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char value[16];
|
2014-09-21 18:05:14 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
if (GetEnvironmentVariableA(key, value, TR_N_ELEMENTS(value)) > 1)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return atoi(value);
|
|
|
|
}
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
if (char const* value = getenv(key); !tr_str_is_empty(value))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return atoi(value);
|
|
|
|
}
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return default_value;
|
2014-09-21 18:05:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char* tr_env_get_string(char const* key, char const* default_value)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(key != nullptr);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2014-09-21 18:05:14 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2017-06-13 02:24:09 +00:00
|
|
|
wchar_t* wide_key = tr_win32_utf8_to_native(key, -1);
|
2021-09-15 00:18:09 +00:00
|
|
|
char* value = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (wide_key != nullptr)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
DWORD const size = GetEnvironmentVariableW(wide_key, nullptr, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (size != 0)
|
2014-09-21 18:05:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
wchar_t* const wide_value = tr_new(wchar_t, size);
|
|
|
|
|
|
|
|
if (GetEnvironmentVariableW(wide_key, wide_value, size) == size - 1)
|
|
|
|
{
|
|
|
|
value = tr_win32_native_to_utf8(wide_value, size);
|
|
|
|
}
|
2014-09-21 18:05:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(wide_value);
|
2014-09-21 18:05:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(wide_key);
|
2014-09-21 18:05:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (value == nullptr && default_value != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
value = tr_strdup(default_value);
|
|
|
|
}
|
2014-09-21 18:05:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return value;
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-11-05 22:46:21 +00:00
|
|
|
char const* value = getenv(key);
|
2014-09-21 18:05:14 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (value == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-11-05 22:46:21 +00:00
|
|
|
value = default_value;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 05:16:40 +00:00
|
|
|
return value != nullptr ? tr_strvDup(value) : nullptr;
|
2014-09-21 18:05:14 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-11 16:35:51 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-01-24 19:07:55 +00:00
|
|
|
void tr_net_init()
|
2015-10-11 16:35:51 +00:00
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsaData;
|
2017-04-19 12:04:45 +00:00
|
|
|
WSAStartup(MAKEWORD(2, 2), &wsaData);
|
2015-10-11 16:35:51 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2015-10-11 16:35:51 +00:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 15:33:56 +00:00
|
|
|
|
|
|
|
/// mime-type
|
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
std::string_view tr_get_mime_type_for_filename(std::string_view filename)
|
2020-10-13 15:33:56 +00:00
|
|
|
{
|
2021-10-01 22:55:19 +00:00
|
|
|
auto constexpr compare = [](mime_type_suffix const& entry, auto const& suffix)
|
2020-10-13 15:33:56 +00:00
|
|
|
{
|
2021-10-01 22:55:19 +00:00
|
|
|
return entry.suffix < suffix;
|
|
|
|
};
|
2020-10-13 15:33:56 +00:00
|
|
|
|
2021-12-17 05:47:51 +00:00
|
|
|
if (auto const pos = filename.rfind('.'); pos != std::string_view::npos)
|
2021-10-01 22:55:19 +00:00
|
|
|
{
|
2022-01-15 19:33:57 +00:00
|
|
|
auto const suffix_lc = tr_strlower(filename.substr(pos + 1));
|
2021-10-01 22:55:19 +00:00
|
|
|
auto const it = std::lower_bound(std::begin(mime_type_suffixes), std::end(mime_type_suffixes), suffix_lc, compare);
|
|
|
|
if (it != std::end(mime_type_suffixes) && suffix_lc == it->suffix)
|
|
|
|
{
|
2021-10-20 02:30:50 +00:00
|
|
|
return it->mime_type;
|
2020-10-25 21:13:48 +00:00
|
|
|
}
|
2020-10-13 15:33:56 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 02:30:50 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
|
|
|
// application/octet-stream is the default value.
|
|
|
|
// An unknown file type should use this type.
|
|
|
|
auto constexpr Fallback = "application/octet-stream"sv;
|
|
|
|
return Fallback;
|
2020-10-13 15:33:56 +00:00
|
|
|
}
|