@@ -843,20 +843,24 @@ pl_julia_setsrc (char *option)
843843#define ETC_APT_SOURCELIST "/etc/apt/sources.list"
844844#define ETC_OSRELEASE "/etc/os-release"
845845
846+ #define Debian_debian 1
847+ #define Debian_deriv_ubuntu 2
846848
847849/**
848- * 当不存在该文件时,我们只能拼凑一个假的出来,但该函数目前只适用于 Ubuntu
850+ * 当不存在该文件时,我们只能拼凑一个假的出来,但该函数目前只适用于 Ubuntu 和 Debian
849851 * 因为其它的 Debian 变体可能不使用 ETC_APT_SOURCELIST,也可能并不适用 `VERSION_CODENAME`
852+ *
853+ * @return 文件是否存在
850854 */
851- void
852- makeup_etc_apt_sourcelist ( )
855+ bool
856+ ensure_apt_sourcelist ( int debian_type )
853857{
854858 bool exist = xy_file_exist (ETC_APT_SOURCELIST );
855859
856860 if (exist )
857861 {
858862 chsrc_infolog_remarkably (ETC_APT_SOURCELIST " 存在" );
859- return ;
863+ return true ;
860864 }
861865 else
862866 {
@@ -866,16 +870,32 @@ makeup_etc_apt_sourcelist ()
866870 char * codename = xy_run ("sed -nr 's/VERSION_CODENAME=(.*)/\1/p' " ETC_OSRELEASE , 0 , NULL );
867871 codename = xy_str_delete_suffix (codename , "\n" );
868872
869- char * makeup = xy_strjoin (9 ,
870- "# Generated by " Chsrc_Version "\n"
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" );
873+ char * makeup = NULL ;
874+
875+ if (debian_type == Debian_deriv_ubuntu )
876+ {
877+ makeup = xy_strjoin (9 ,
878+ "# Generated by chsrc" Chsrc_Version "\n"
879+ "deb " Chsrc_Maintain_URL "/ubuntu" , codename , " main restricted universe multiverse\n"
880+ "deb " Chsrc_Maintain_URL "/ubuntu" , codename , "-updates main restricted universe multiverse\n"
881+ "deb " Chsrc_Maintain_URL "/ubuntu" , codename , "-backports main restricted universe multiverse\n"
882+ "deb " Chsrc_Maintain_URL "/ubuntu" , codename , "-security main restricted universe multiverse\n" );
883+ }
884+ else
885+ {
886+ // https://wiki.debian.org/SourcesList
887+ makeup = xy_strjoin (9 ,
888+ "# Generated by chsrc" Chsrc_Version "\n"
889+ "deb " Chsrc_Maintain_URL "/debian" , codename , " main contrib non-free\n"
890+ "deb " Chsrc_Maintain_URL "/debian" , codename , "-security main contrib non-free\n"
891+ "deb " Chsrc_Maintain_URL "/debian" , codename , "-updates main contrib non-free\n"
892+ "deb " Chsrc_Maintain_URL "/debian" , codename , "-backports main contrib non-free\n" );
893+ }
875894
876895 FILE * f = fopen (ETC_APT_SOURCELIST , "w" );
877- fwrite (makeup , strlen (makeup ), 1 , f );
896+ fwrite (makeup , strlen (makeup ), 1 , f );
878897 fclose (f );
898+ return false;
879899}
880900
881901
@@ -894,24 +914,28 @@ os_ubuntu_setsrc (char *option)
894914{
895915 chsrc_ensure_root ();
896916
897- makeup_etc_apt_sourcelist ( );
917+ bool sourcelist_exist = ensure_apt_sourcelist ( Debian_deriv_ubuntu );
898918
899919 int index = use_specific_mirror_or_auto_select (option , os_ubuntu );
900920
901921 SourceInfo source = os_ubuntu_sources [index ];
902922 chsrc_confirm_selection (& source );
903923
904- chsrc_backup (ETC_APT_SOURCELIST );
924+ // 不存在的时候,用的是我们生成的无效文件,不要备份
925+ if (sourcelist_exist )
926+ {
927+ chsrc_backup (ETC_APT_SOURCELIST );
928+ }
905929
906930 char * arch = xy_run ("arch" , 0 , NULL );
907931 char * cmd = NULL ;
908932 if (strncmp (arch , "x86_64" , 6 )== 0 )
909933 {
910- cmd = xy_strjoin (3 , "sed -E -i \'s@^ https?://.*$ @" , source .url , "@g\' " ETC_APT_SOURCELIST );
934+ cmd = xy_strjoin (3 , "sed -E -i \'s@https?://.*/ubuntu/? @" , source .url , "@g\' " ETC_APT_SOURCELIST );
911935 }
912936 else
913937 {
914- cmd = xy_strjoin (3 , "sed -E -i \'s@^ https?://.*$ @" , source .url , "-ports@g\' " ETC_APT_SOURCELIST );
938+ cmd = xy_strjoin (3 , "sed -E -i \'s@https?://.*/ubuntu-ports/? @" , source .url , "-ports@g\' " ETC_APT_SOURCELIST );
915939 }
916940
917941 chsrc_run (cmd );
@@ -968,6 +992,9 @@ os_debian_setsrc (char *option)
968992{
969993 chsrc_ensure_root ();
970994
995+ // Docker环境下,Debian镜像可能不存在该文件
996+ bool sourcelist_exist = ensure_apt_sourcelist (Debian_debian );
997+
971998 int index = use_specific_mirror_or_auto_select (option , os_debian );
972999
9731000 SourceInfo source = os_debian_sources [index ];
@@ -976,12 +1003,13 @@ os_debian_setsrc (char *option)
9761003 chsrc_note_remarkably ("如果遇到无法拉取 HTTPS 源的情况,我们会使用 HTTP 源并需要您运行:" );
9771004 puts ("apt install apt-transport-https ca-certificates" );
9781005
979- chsrc_backup (ETC_APT_SOURCELIST );
1006+ // 不存在的时候,用的是我们生成的无效文件,不要备份
1007+ if (sourcelist_exist )
1008+ {
1009+ chsrc_backup (ETC_APT_SOURCELIST );
1010+ }
9801011
981- char * cmd = xy_strjoin (3 ,
982- "sed -E -i \'s@https?://.*/debian/?@" ,
983- source .url ,
984- "@g\' /etc/apt/sources.list" );
1012+ char * cmd = xy_strjoin (3 , "sed -E -i \'s@https?://.*/debian/?@" , source .url , "@g\' " ETC_APT_SOURCELIST );
9851013
9861014 chsrc_run (cmd );
9871015 chsrc_run ("apt update" );
0 commit comments