標籤:ubuntu 下實現產品自動更新功能
最近公司產品需要增加一個功能,就是版本自動更新,使用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!\n");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("\ndeb http://%s/apt %s %s\n", 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\n"); goto update_done;}if(strstr(buffer,source_ok)){trace_log(buffer); printf("scan sourceok and exit\n");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!\n");
初步測試沒有什麼問題。