chsrc/include/chsrc.h

374 lines
9.2 KiB
C
Raw Normal View History

/** ------------------------------------------------------------
* File : chsrc.h
* License : GPLv3
* Authors : Aoran Zeng <ccmywish@qq.com>
* Created on : <2023-08-29>
2023-10-05 17:32:02 +08:00
* Last modified : <2023-10-05>
*
* chsrc:
*
* chsrc.c
* ------------------------------------------------------------*/
2023-08-29 23:04:54 +08:00
2023-09-03 14:56:49 +08:00
#include "xy.h"
2023-09-15 12:44:50 +08:00
#include "sources.h"
2023-09-03 14:48:53 +08:00
2023-09-15 12:50:56 +08:00
2023-09-27 15:20:44 +08:00
#define App_Prefix "chsrc: "
#define chsrc_success(str) xy_success(xy_2strjoin(App_Prefix, (str)))
#define chsrc_info(str) xy_info(xy_2strjoin(App_Prefix, (str)))
#define chsrc_warn(str) xy_warn(xy_2strjoin(App_Prefix, (str)))
#define chsrc_error(str) xy_error(xy_2strjoin(App_Prefix, (str)))
2023-09-15 12:50:56 +08:00
/**
*
*
* @param check_cmd `progname` `progname`
* 使 python Windows上
* Microsoft Store
*
* @param progname
*/
bool
2023-10-05 09:40:10 +08:00
query_program_exist (char* check_cmd, char* progname)
2023-09-15 12:50:56 +08:00
{
char* which = check_cmd;
int ret = system(which);
// char buf[32] = {0}; sprintf(buf, "错误码: %d", ret);
if (0!=ret) {
// xy_warn (xy_strjoin(4, "× 命令 ", progname, " 不存在,", buf));
xy_warn (xy_strjoin(3, "× 命令 ", progname, " 不存在"));
return false;
} else {
xy_success (xy_strjoin(3, "√ 命令 ", progname, " 存在"));
return true;
}
}
/**
* _setsrc codetarget可用源中
*
* @param target
* @param input default def
*/
2023-10-05 09:40:10 +08:00
#define find_mirror(s, input) query_mirror_exist(s##_sources, s##_sources_n, (char*)#s+3, input)
2023-09-15 12:50:56 +08:00
int
2023-10-05 09:40:10 +08:00
query_mirror_exist (SourceInfo* sources, size_t size, char* target, char* input)
2023-09-15 12:50:56 +08:00
{
if (0==size) {
xy_error(xy_strjoin(3, "chsrc: 当前 ", target, " 无任何可用源,请联系维护者"));
exit(1);
}
if (1==size) {
2023-09-27 15:20:44 +08:00
xy_success(xy_strjoin(5, "chsrc: ", sources[0].mirror->name, "", target, " 目前唯一可用镜像站,感谢他们的慷慨支持"));
2023-09-15 12:50:56 +08:00
}
if (xy_streql("default", input) || xy_streql("def", input)) {
xy_info ("chsrc: 默认使用维护团队测速第一的源");
return 0;
}
int idx = 0;
2023-10-05 09:28:34 +08:00
SourceInfo source = sources[0];
2023-09-15 12:50:56 +08:00
bool exist = false;
for (int i=0; i<size; i++)
{
source = sources[i];
if (xy_streql(source.mirror->code, input)) {
idx = i;
exist = true;
break;
}
}
if (!exist) {
xy_error (xy_strjoin(3, "chsrc: 镜像站 ", input, " 不存在"));
xy_error (xy_2strjoin("chsrc: 查看可使用源,请使用 chsrc list ", target));
exit(1);
}
return idx;
}
/**
2023-09-27 19:31:23 +08:00
* oh-my-mirrorz.py @ccmywish C语言
2023-09-15 12:50:56 +08:00
*/
char*
to_human_readable_speed (double speed)
{
char* scale[] = {"Byte/s", "KByte/s", "MByte/s", "GByte/s", "TByte/s"};
int i = 0;
while (speed > 1024.0)
{
i += 1;
speed /= 1024.0;
}
char* buf = xy_malloc0(64);
sprintf(buf, "%.2f %s", speed, scale[i]);
char* new = NULL;
if (i <= 1 ) new = xy_str_to_red(buf);
else
{
if (i == 2 && speed < 2.00) new = xy_str_to_yellow(buf);
else new = xy_str_to_green(buf);
}
return new;
}
/**
* https://github.com/mirrorz-org/oh-my-mirrorz/blob/master/oh-my-mirrorz.py
2023-09-27 19:31:23 +08:00
* @ccmywish C语言
2023-09-15 12:50:56 +08:00
*
* @return -1
*/
double
test_speed_url (const char* url)
{
char* time_sec = "6";
/* 现在我们切换至跳转后的链接来测速,不再使用下述判断
if (xy_str_start_with(url, "https://registry.npmmirror"))
{
// 这里 npmmirror 跳转非常慢需要1~3秒所以我们给它留够至少8秒测速时间否则非常不准
time_sec = "10";
}
*/
// 我们用 —L因为Ruby China源会跳转到其他地方
// npmmirror 也会跳转
char* curl_cmd = xy_strjoin(6, "curl -qsL -o ", xy_os_devnull,
" -w \"%{http_code} %{speed_download}\" -m", time_sec ,
" -A chsrc/" Chsrc_Version " ", url);
// xy_info (xy_2strjoin("chsrc: 测速 ", url));
2023-09-27 19:31:23 +08:00
char* buf = xy_getcmd (curl_cmd, 0, NULL);
2023-09-15 12:50:56 +08:00
// 如果尾部有换行,删除
2023-09-27 19:31:23 +08:00
buf = xy_str_strip (buf);
2023-09-15 12:50:56 +08:00
2023-09-27 19:31:23 +08:00
// 分隔两部分数据
2023-09-15 12:50:56 +08:00
char* split = strchr(buf, ' ');
if (split) *split = '\0';
// puts(buf); puts(split+1);
int http_code = atoi(buf);
double speed = atof(split+1);
char* speedstr = to_human_readable_speed(speed);
if (200!=http_code) {
2023-09-27 19:31:23 +08:00
char* httpcodestr = xy_str_to_yellow (xy_2strjoin("HTTP码 ", buf));
puts (xy_strjoin (3, speedstr, " | ", httpcodestr));
2023-09-15 12:50:56 +08:00
} else {
puts (speedstr);
}
return speed;
}
2023-09-03 14:48:53 +08:00
int
2023-10-05 09:40:10 +08:00
get_max_ele_idx_in_dbl_ary (double* array, int size)
2023-09-03 14:48:53 +08:00
{
double maxval = array[0];
2023-09-06 19:45:37 +08:00
int maxidx = 0;
2023-09-03 14:48:53 +08:00
for (int i=1; i<size; i++) {
if (array[i]>maxval) {
maxval = array[i];
maxidx = i;
}
}
return maxidx;
}
2023-09-27 09:40:31 +08:00
/**
*
*/
#define auto_select(s) auto_select_(s##_sources, s##_sources_n, (char*)#s+3)
2023-09-15 12:50:56 +08:00
int
2023-10-05 09:28:34 +08:00
auto_select_ (SourceInfo* sources, size_t size, const char* target)
2023-09-15 12:50:56 +08:00
{
if (0==size) {
xy_error(xy_strjoin(3, "chsrc: 当前 ", target, " 无任何可用源,请联系维护者"));
exit(1);
}
bool onlyone = false;
if (1==size) onlyone = true;
double speeds[size];
double speed = 0.0;
for (int i=0;i<size;i++)
{
2023-10-05 09:28:34 +08:00
SourceInfo src = sources[i];
2023-09-15 12:50:56 +08:00
const char* url = src.mirror->__bigfile_url;
if (NULL==url) {
2023-09-27 15:20:44 +08:00
chsrc_warn ( xy_strjoin(3, "开发者未提供 ", src.mirror->code, " 镜像站测速链接,跳过该站点"));
2023-09-15 12:50:56 +08:00
speed = 0;
} else {
printf ("%s",xy_strjoin(3, "chsrc: 测速 ", src.mirror->site , " ... "));
fflush(stdout);
speed = test_speed_url (url);
}
speeds[i] = speed;
}
2023-10-05 09:40:10 +08:00
int fastidx = get_max_ele_idx_in_dbl_ary (speeds, size);
2023-09-15 12:50:56 +08:00
if (onlyone)
2023-09-27 15:20:44 +08:00
xy_success(xy_strjoin(5, "chsrc: ", sources[fastidx].mirror->name, "", target, " 目前唯一可用镜像站,感谢他们的慷慨支持"));
2023-09-15 12:50:56 +08:00
else
xy_success (xy_2strjoin("chsrc: 最快镜像站: ", sources[fastidx].mirror->name));
return fastidx;
}
2023-09-27 09:40:31 +08:00
#define use_specific_mirror_or_auto_select(input, s) \
(NULL!=(input)) ? find_mirror(s, input) : auto_select(s)
2023-09-03 17:57:45 +08:00
/**
* _setsrc
*/
void
2023-10-05 09:28:34 +08:00
chsrc_say_selection (SourceInfo* source)
2023-09-03 17:57:45 +08:00
{
2023-09-05 15:29:53 +08:00
xy_info (xy_strjoin(5, "chsrc: 选中镜像站: ", source->mirror->abbr, " (", source->mirror->code, ")"));
2023-09-03 17:57:45 +08:00
}
2023-09-15 12:50:56 +08:00
2023-09-04 15:39:49 +08:00
void
2023-10-05 09:28:34 +08:00
chsrc_say_thanks (SourceInfo* source)
2023-09-04 15:39:49 +08:00
{
2023-09-05 15:29:53 +08:00
xy_success(xy_2strjoin("chsrc: 感谢镜像提供方: ", source->mirror->name));
2023-09-04 15:39:49 +08:00
}
2023-09-03 14:48:53 +08:00
2023-09-22 13:07:49 +08:00
void
2023-09-29 21:28:02 +08:00
chsrc_ensure_root ()
2023-09-22 13:07:49 +08:00
{
char* euid = getenv("$EUID");
if (NULL==euid) {
2023-09-27 19:31:23 +08:00
char* buf = xy_getcmd("id -u", 0, NULL);
2023-09-22 13:07:49 +08:00
if (0!=atoi(buf)) goto not_root;
else return;
} else {
if (0!=atoi(euid)) goto not_root;
else return;
}
not_root:
xy_error("chsrc: 请在命令前使用 sudo 来保证必要的权限");
exit(1);
}
2023-09-26 21:41:47 +08:00
static void
chsrc_run (const char* cmd)
{
xy_info (xy_2strjoin ("chsrc: 运行 ", cmd));
system(cmd);
}
2023-09-26 22:24:48 +08:00
static void
chsrc_check_file (const char* path)
{
char* cmd = NULL;
2023-09-27 18:31:45 +08:00
path = xy_uniform_path (path);
2023-09-26 22:24:48 +08:00
if(xy_on_windows) {
cmd = xy_2strjoin ("type ", path);
} else {
cmd = xy_2strjoin ("cat ", path);
}
chsrc_run (cmd);
}
2023-09-29 21:23:58 +08:00
static void
chsrc_ensure_dir (const char* dir)
{
2023-09-29 22:15:58 +08:00
dir = xy_uniform_path (dir);
2023-09-29 21:23:58 +08:00
char* mkdir_cmd = NULL;
if (xy_on_windows) {
mkdir_cmd = "md ";
} else {
mkdir_cmd = "mkdir -p ";
}
char* cmd = xy_2strjoin (mkdir_cmd, dir);
cmd = xy_str_to_quietcmd (cmd);
chsrc_run (cmd);
}
2023-09-26 21:41:47 +08:00
static void
2023-09-26 22:14:41 +08:00
chsrc_append_to_file (const char* str, const char* file)
2023-09-26 21:41:47 +08:00
{
2023-09-27 18:31:45 +08:00
file = xy_uniform_path (file);
2023-09-29 21:23:58 +08:00
char* dir = xy_parent_dir (file);
chsrc_ensure_dir (dir);
2023-09-26 21:41:47 +08:00
char* cmd = NULL;
if (xy_on_windows) {
cmd = xy_strjoin (4, "echo ", str, " >> ", file);
} else {
cmd = xy_strjoin (4, "echo '", str, "' >> ", file);
}
chsrc_run(cmd);
}
static void
2023-09-26 22:14:41 +08:00
chsrc_overwrite_file (const char* str, const char* file)
2023-09-26 21:41:47 +08:00
{
2023-09-27 18:31:45 +08:00
file = xy_uniform_path (file);
2023-09-29 21:23:58 +08:00
char* dir = xy_parent_dir (file);
chsrc_ensure_dir (dir);
2023-09-26 21:41:47 +08:00
char* cmd = NULL;
if (xy_on_windows) {
cmd = xy_strjoin (4, "echo ", str, " > ", file);
} else {
cmd = xy_strjoin (4, "echo '", str, "' > ", file);
}
chsrc_run(cmd);
}
2023-09-26 23:02:12 +08:00
static void
chsrc_backup (const char* path)
{
2023-10-05 17:32:02 +08:00
char* cmd = NULL;
if (xy_on_bsd)
{
// 似乎BSD的cp并没有 --backup='t' 选项
cmd = xy_strjoin(5, "cp -f ", path, " ", path, ".bak");
}
else if (xy_on_windows) {
// /Y 表示覆盖
cmd = xy_strjoin(5, "copy /Y ", path, " ", path, ".bak" );
}
else {
cmd = xy_strjoin(5, "cp ", path, " ", path, ".bak --backup='t'");
}
2023-09-26 23:02:12 +08:00
chsrc_run (cmd);
2023-09-26 23:04:42 +08:00
chsrc_info ( xy_strjoin (3, "备份文件名 ", path, ".bak"));
2023-09-26 23:02:12 +08:00
}
2023-09-26 21:41:47 +08:00
2023-09-02 19:07:30 +08:00
/* Target Info */
typedef struct {
void (*setfn)(char* option);
void (*getfn)(char* option);
2023-10-05 09:28:34 +08:00
SourceInfo* sources;
size_t sources_n;
} TargetInfo;
2023-09-02 19:07:30 +08:00
2023-10-05 09:28:34 +08:00
#define def_target(t) TargetInfo t##_target = {t##_setsrc, t##_getsrc, t##_sources, t##_sources_n}