Merge pull request #5 from RubyMetric/feature-os-archlinux

Add feature os-archlinux,os-gentoolinux,os-other-arch of ubuntu; Add getcmd to help.h
This commit is contained in:
ccmywish 2023-09-05 16:56:29 +08:00 committed by GitHub
commit d825a2c145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 179 additions and 27 deletions

124
chsrc.c
View File

@ -816,7 +816,7 @@ pl_julia_setsrc (char* option)
/**
* ubuntu不同架构下的换源是不一样的,x86架构
* ubuntu不同架构下的换源是不一样的,
*/
void
os_ubuntu_setsrc (char* option)
@ -834,12 +834,24 @@ os_ubuntu_setsrc (char* option)
xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak");
char* cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/ubuntu\\/@\\1",
source_url,
"@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list");
char * arch = xy_getcmd("arch");
char * cmd;
if(strncmp(arch,"x86_64",6)==0)
{
cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/ubuntu\\/@\\1",
source_url,
"/@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list");
}
else
{
cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/ubuntu\\/@\\1",
source_url,
"-ports/@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list");
}
system(cmd);
free(cmd);
free(arch);
// char* rm = "rm -rf /etc/apt/source.list.bak";
// system(rm);
@ -849,6 +861,7 @@ os_ubuntu_setsrc (char* option)
/**
* Debian Buster HTTPS HTTPS 使 HTTP
* sudo apt install apt-transport-https ca-certificates
*
*/
void
os_debian_setsrc (char* option)
@ -861,6 +874,9 @@ os_debian_setsrc (char* option)
const char* source_abbr = os_ubuntu_sources[selected].mirror->abbr;
const char* source_url = os_ubuntu_sources[selected].url;
xy_info("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并 需要您 安装");
xy_info("sudo apt install apt-transport-https ca-certificates");
char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak";
system(backup);
@ -892,6 +908,8 @@ os_fedora_setsrc (char* option)
const char* source_abbr = os_fedora_sources[selected].mirror->abbr;
const char* source_url = os_fedora_sources[selected].url;
xy_info("fedora29版本及以下暂不支持");
char* backup = "cp -rf /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.bak";
system(backup);
backup = "cp -rf /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.bak";
@ -1010,6 +1028,10 @@ os_mysys2_setsrc(char* option)
xy_info ("chsrc: 备份文件名:2. /etc/pacman.d/mirrorlist.mingw64.bak");
xy_info ("chsrc: 备份文件名:3. /etc/pacman.d/mirrorlist.msys.bak");
char* prev = xy_strjoin(3,"请针对你的架构下载安装此目录下的文件:",source_url,"distrib/<架构>/");
xy_info (prev);
free(prev);
char* cmd = xy_strjoin(3,"sed -i \"s#https\?://mirror.msys2.org/#",source_url,"#g\" /etc/pacman.d/mirrorlist* ");
system(cmd);
free(cmd);
@ -1026,6 +1048,68 @@ os_mysys2_setsrc(char* option)
}
void
os_archlinux_setsrc(char* option)
{
int selected = 0;
for (int i=0;i<sizeof(os_archlinux_sources);i++) {
// 循环测速
}
const char* source_name = os_archlinux_sources[selected].mirror->name;
const char* source_abbr = os_archlinux_sources[selected].mirror->abbr;
const char* source_url = os_archlinux_sources[selected].url;
char* backup = "cp -rf /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak";
system(backup);
xy_info ("chsrc: 备份文件名: /etc/pacman.d/mirrorlist.bak");
char* new_file = xy_strjoin("Server = ",source_url,"$repo/os/$arch");
char* cmd = xy_strjoin(3,"echo ",new_file," > /etc/pacman.d/mirrorlist");
system(cmd);
free(cmd);
cmd = "cat /etc/pacman.d/mirrorlist.bak >> /etc/pacman.d/mirrorlist";
system(cmd);
free(new_file);
// char* rm = "rm -rf /etc/pacman.d/mirrorlist.bak";
// system(rm);
xy_info ("chsrc: 为 archlinux 命令换源");
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
xy_info ("Please use \"pacman -Syyu \" to update your source");
}
void
os_gentoolinux_setsrc(char* option)
{
int selected = 0;
for (int i=0;i<sizeof(os_gentoolinux_sources);i++) {
// 循环测速
}
const char* source_name = os_gentoolinux_sources[selected].mirror->name;
const char* source_abbr = os_gentoolinux_sources[selected].mirror->abbr;
const char* source_url = os_gentoolinux_sources[selected].url;
char* backup = "cp -rf /etc/portage/repos.conf/gentoo.conf /etc/portage/repos.conf/gentoo.conf.bak";
system(backup);
xy_info ("chsrc: 备份文件名: /etc/portage/repos.conf/gentoo.conf.bak");
char* cmd = xy_strjoin(3,"sed -i \"s#rsync[:|\\.|\\/|a-z|A-Z]*/gentoo-portage#rsync://",source_url,"gentoo-portage#g");
system(cmd);
free(cmd);
char * yuan = xy_strjoin(3,"GENTOO_MIRRORS=\"https://",source_url,"gentoo\"");
cmd = xy_strjoin("cat ",yuan," >> /etc/portage/make.conf");
system(cmd);
free(cmd);
free(yuan);
// char* rm = "rm -rf /etc/portage/repos.conf/gentoo.conf.bak";
// system(rm);
xy_info ("chsrc: 为 gentoolinux 命令换源");
xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name));
}
/************************************** Begin Target Matrix ****************************************/
def_target_info(pl_ruby);
def_target_info(pl_python);
@ -1064,22 +1148,26 @@ static const char const
target_info
os_ubuntu_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_debian_target = {os_debian_setsrc, NULL, os_debian_sources, 7},
os_fedora_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_kali_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_openbsd_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_mysys2_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7};
os_ubuntu_target = {os_ubuntu_setsrc, NULL, os_ubuntu_sources, 7},
os_debian_target = {os_debian_setsrc, NULL, os_debian_sources, 7},
os_fedora_target = {os_fedora_setsrc, NULL, os_fedora_sources, 7},
os_kali_target = {os_kali_setsrc, NULL, os_kali_sources, 7},
os_openbsd_target = {os_openbsd_setsrc, NULL, os_openbsd_sources, 7},
os_mysys2_target = {os_mysys2_setsrc, NULL, os_mysys2_sources, 7},
os_archlinux_target = {os_archlinux_setsrc, NULL, os_archlinux_sources, 7},
os_gentoolinux_target = {os_gentoolinux_setsrc, NULL, os_gentoolinux_sources, 7};
static const char const
*os_ubuntu [] = {"ubuntu", NULL, targetinfo(&os_ubuntu_target)},
*os_debian [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_fedora [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_kali [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_openbsd [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_mysys2 [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_ubuntu [] = {"ubuntu", NULL, targetinfo(&os_ubuntu_target)},
*os_debian [] = {"debian", NULL, targetinfo(&os_debian_target)},
*os_fedora [] = {"fedora", NULL, targetinfo(&os_fedora_target)},
*os_kali [] = {"kali", NULL, targetinfo(&os_kali_target)},
*os_openbsd [] = {"openbsd", NULL, targetinfo(&os_openbsd_target)},
*os_mysys2 [] = {"mysys2", NULL, targetinfo(&os_mysys2_target)},
*os_archlinux [] = {"mysys2", NULL, targetinfo(&os_archlinux_target)},
*os_gentoolinux [] = {"mysys2", NULL, targetinfo(&os_gentoolinux_target)},
**os_systems[] =
{
os_ubuntu, os_debian
os_ubuntu, os_debian,os_fedora,os_kali,os_openbsd,os_mysys2,os_archlinux,os_gentoolinux
};

49
chsrc.h
View File

@ -284,13 +284,13 @@ pl_php_sources[] = {
*/
static source_info
os_ubuntu_sources[] = {
{&Ali, "https://mirrors.aliyun.com/ubuntu/"},
{&Bfsu, "https://mirrors.bfsu.edu.cn/ubuntu/"},
{&Ustc, "https://mirrors.ustc.edu.cn/ubuntu/"},
{&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"},
{&Tencent, "https://mirrors.tencent.com/ubuntu/"},
{&Netease, "https://mirrors.163.com/ubuntu/"},
{&Sohu, "https://mirrors.sohu.com/ubuntu/"}
{&Ali, "https://mirrors.aliyun.com/ubuntu"},
{&Bfsu, "https://mirrors.bfsu.edu.cn/ubuntu"},
{&Ustc, "https://mirrors.ustc.edu.cn/ubuntu"},
{&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/ubuntu"},
{&Tencent, "https://mirrors.tencent.com/ubuntu"},
{&Netease, "https://mirrors.163.com/ubuntu"},
{&Sohu, "https://mirrors.sohu.com/ubuntu"}
},
/**
@ -372,6 +372,36 @@ os_mysys2_sources[] = {
{&Tencent, "https://mirrors.tencent.com/msys2/"},
{&Netease, "https://mirrors.163.com/msys2/"},
{&Sohu, "https://mirrors.sohu.com/msys2/"}
},
/**
* 2023-09-05
*
* TODO: 1.
*/
os_archlinux_sources[] = {
{&Ali, "https://mirrors.aliyun.com/archlinux/"},
{&Bfsu, "https://mirrors.bfsu.edu.cn/archlinux/"},
{&Ustc, "https://mirrors.ustc.edu.cn/archlinux/"},
{&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/archlinux/"},
{&Tencent, "https://mirrors.tencent.com/archlinux/"},
{&Netease, "https://mirrors.163.com/archlinux/"},
{&Sohu, "https://mirrors.sohu.com/archlinux/"}
},
/**
* 2023-09-05
*
* TODO: 1.
*/
os_gentoolinux_sources[] = {
{&Ali, "mirrors.aliyun.com"},
{&Bfsu, "mirrors.bfsu.edu.cn"},
{&Ustc, "mirrors.ustc.edu.cn"},
{&Tuna, "mirrors.tuna.tsinghua.edu.cn"},
{&Tencent, "mirrors.tencent.com"},
{&Netease, "mirrors.163.com"},
{&Sohu, "mirrors.sohu.com"}
}
;
@ -423,8 +453,9 @@ def_target_sources_n(pl_perl); def_target_sources_n(pl_php); def_target_sourc
def_target_sources_n(pl_rust); def_target_sources_n(pl_java); def_target_sources_n(pl_dotnet);
def_target_sources_n(pl_r); def_target_sources_n(pl_julia);
def_target_sources_n(os_ubuntu); def_target_sources_n(os_debian); def_target_sources_n(os_fedora);
def_target_sources_n(os_kali); def_target_sources_n(os_openbsd); def_target_sources_n(os_mysys2);
def_target_sources_n(os_ubuntu); def_target_sources_n(os_debian); def_target_sources_n(os_fedora);
def_target_sources_n(os_kali); def_target_sources_n(os_openbsd); def_target_sources_n(os_mysys2);
def_target_sources_n(os_archlinux); def_target_sources_n(os_gentoolinux);
/* Target Info */
typedef struct {

33
xy.h
View File

@ -440,4 +440,37 @@ xy_str_strip (const char* str)
return new;
}
/* *
* cmd后拿到cmd的执行结果 free掉这段内存
*
*/
char *
xy_getcmd(const char * cmd)
{
const int BUFSIZE = 1024;
FILE *stream;
char* buf = (char*)malloc(sizeof(char)*BUFSIZE);
// 执行命令,并将输出保存到 stream 指针指向的文件中。
stream = popen(cmd, "r");
if (stream == NULL) {
printf("命令执行失败。\n");
return NULL;
}
// 从 stream 指针指向的文件中读取数据。
char *ret;
do {
ret = fgets(buf, sizeof(buf), stream);
if(ret==NULL)
break;
}while(1);
// 关闭 stream 指针。
pclose(stream);
return buf;
}
#endif