33 * License : GPLv3
44 * Authors : Aoran Zeng <[email protected] > 55 * Created on : <2023-08-28>
6- * Last modified : <2024-06-08 >
6+ * Last modified : <2024-06-11 >
77 *
88 * chsrc:
99 *
1212 * SPDX-License-Identifier: GPL-3.0-or-later
1313 * ------------------------------------------------------------*/
1414
15- #define Chsrc_Version "v0.1.6 -2024/06/08 "
15+ #define Chsrc_Version "v0.1.7.pre -2024/06/11 "
1616#define Chsrc_Maintain_URL "https://gitee.com/RubyMetric/chsrc"
1717
1818#include "chsrc.h"
@@ -843,6 +843,41 @@ pl_julia_setsrc (char *option)
843843
844844
845845#define ETC_APT_SOURCELIST "/etc/apt/sources.list"
846+ #define ETC_OSRELEASE "/etc/os-release"
847+
848+
849+ /**
850+ * 当不存在该文件时,我们只能拼凑一个假的出来,但该函数目前只适用于 Ubuntu
851+ * 因为其它的 Debian 变体可能不使用 ETC_APT_SOURCELIST,也可能并不适用 `VERSION_CODENAME`
852+ */
853+ void
854+ makeup_etc_apt_sourcelist ()
855+ {
856+ bool exist = xy_file_exist (ETC_APT_SOURCELIST );
857+
858+ if (exist )
859+ {
860+ chsrc_infolog_remarkably (ETC_APT_SOURCELIST "存在" );
861+ return ;
862+ }
863+ else
864+ {
865+ chsrc_error_remarkably (ETC_APT_SOURCELIST "缺失,将替补" )
866+ }
867+
868+ char * codename = xy_run ("sed -nr 's/VERSION_CODENAME=(.*)/\1/p' " ETC_OSRELEASE , 0 , NULL );
869+
870+ char * makeup = xy_strjoin (9 ,
871+ "deb " Chsrc_Maintain_URL , codename , " main restricted universe multiverse\n"
872+ "deb " Chsrc_Maintain_URL , codename , "-updates main restricted universe multiverse\n"
873+ "deb " Chsrc_Maintain_URL , codename , "-backports main restricted universe multiverse\n"
874+ "deb " Chsrc_Maintain_URL , codename , "-security main restricted universe multiverse\n" );
875+
876+ FILE * f = fopen (ETC_APT_SOURCELIST , "w" );
877+ fwrite (makeup , strlen (makeup ), 1 , f );
878+ fclose (f );
879+ }
880+
846881
847882
848883void
@@ -859,6 +894,8 @@ os_ubuntu_setsrc (char *option)
859894{
860895 chsrc_ensure_root ();
861896
897+ makeup_etc_apt_sourcelist ();
898+
862899 int index = use_specific_mirror_or_auto_select (option , os_ubuntu );
863900
864901 SourceInfo source = os_ubuntu_sources [index ];
@@ -870,12 +907,11 @@ os_ubuntu_setsrc (char *option)
870907 char * cmd = NULL ;
871908 if (strncmp (arch , "x86_64" , 6 )== 0 )
872909 {
873- cmd = xy_strjoin (3 , "sed -E -i \'s@https?://.*/ubuntu/? @" , source .url , "@g\' /etc/apt/sources.list" );
910+ cmd = xy_strjoin (3 , "sed -E -i \'s@^ https?://.*$ @" , source .url , "@g\' " ETC_APT_SOURCELIST );
874911 }
875912 else
876913 {
877- cmd = xy_strjoin (3 , "sed -E -i \'s@https?://.*/ubuntu-ports/?@" , source .url ,
878- "-ports@g\' /etc/apt/sources.list" );
914+ cmd = xy_strjoin (3 , "sed -E -i \'s@^https?://.*$@" , source .url , "-ports@g\' " ETC_APT_SOURCELIST );
879915 }
880916
881917 chsrc_run (cmd );
0 commit comments