近期公司产品须要添加一个功能。就是版本号自己主动更新,使用apt-get 实现。apt-get 软件源配置的方法,參见本人资源里的共享。以下是代码中作为升级的一部分。
FILE *fp;
char buffer[256];
char source_ok[] = "Reading package lists...";
if(0 > system("mv -f /etc/apt/sources.list /etc/apt/sources.list.upgrade"))
printf("cldUp backup old source list error!
");
fp=fopen("/etc/apt/sources.list","w+");
if(0 == fp)
goto update_done;
fprintf(fp,"deb http://%s/apt %s %s", source_addr, source_dir, source_ver);
printf("
deb http://%s/apt %s %s
", source_addr, source_dir, source_ver);
fclose(fp);
fp=popen("apt-get update 2>&1","r");
while(!feof(fp)){
if(0 == fgets(buffer,sizeof(buffer),fp))
break;
printf("%s",buffer);
fflush(stdout);
if(strstr(buffer,"Failed")){
trace_log(buffer);
printf("scan failed and exit
");
goto update_done;
}
if(strstr(buffer,source_ok)){
trace_log(buffer);
printf("scan sourceok and exit
");
break;
}
}
pclose(fp);
fp=popen("apt-get -y --force-yes install ssh 2>&1","r");
while(!feof(fp)){
if(0 == fgets(buffer,sizeof(buffer),fp))
break;
printf("%s",buffer);
if(strstr(buffer,"1 upgraded")){
//save the upgrade info
b_upgrade_success = true;
trace_log(buffer);
}
}
pclose(fp);
if(b_upgrade_success)
{
b_upgrade_success = 0;
}
update_done:
if(0 > system("mv -f /etc/apt/sources.list.upgrade /etc/apt/sources.list"))
printf("restore source list error!
");
初步測试没有什么问题。