#include "stdio.h"
#include "unistd.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include "common.h"
#ifndef path
#define version_path "."
#define path "./update"
#endif // !1
char new_version = 0; //0 no check //1 new version //2 download finished
int new_version_int = 0;
static int get_version(char * buf){
int ret = -1;
if(strstr(buf,"version <") != NULL) {
char * p = strstr(buf,"<");
char * d = strstr(buf,">");
if(p!=NULL&&d!=NULL){
*d = 0;
ret = atoi(p+1);
}
}
return ret;
}
void version_check(void){
printf("[-------------------------------------------------------------------------------------]version_check\n");
if(new_version == 0){
char ver1[20] = {0};
char ver2[20] = {0};
int ver_int1 = 0;
int ver_int2 = 0;
system("rm "path" -rf");
system("mkdir "path);
system("wget "download_version" -O "path"/version.txt");
FILE * f1 = fopen(path"/version.txt","r");
FILE * f2 = fopen(version_path"/version","r");
if(f1 == NULL||f2 == NULL) return;
fread(ver1,17,1,f1);
fread(ver2,17,1,f2);
ver_int1 = get_version(ver1);
ver_int2 = get_version(ver2);
printf("%d,%d\n",ver_int1,ver_int2);
if((ver_int1 >= 0)&&(ver_int2 >= 0)){
if(ver_int1 == ver_int2)
printf("no new version\n");
else{
new_version = 1;
new_version_int = ver_int1;
printf("new version\n");
}
}else printf("check fail\n");
fclose(f1);
fclose(f2);
system("rm "path"/version.txt");
}
}
void download_version_check(void){
printf("[-------------------------------------------------------------------------------------]download_version_check\n");
if(access(path"/download_finished", F_OK) == 0){
char ver1[20] = {0};
char ver2[20] = {0};
int ver_int1 = 0;
int ver_int2 = 0;
system("rm "path"/version.txt");
system("wget "download_version" -O "path"/version.txt");
FILE * f1 = fopen(path"/version.txt","r");
FILE * f2 = fopen(path"/download_finished","r");
if(f1 == NULL||f2 == NULL) return;
fread(ver1,16,1,f1);
fread(ver2,16,1,f2);
ver_int1 = get_version(ver1);
ver_int2 = get_version(ver2);
printf("[download_version_check]:%d,%d\n",ver_int1,ver_int2);
if((ver_int1 >= 0)&&(ver_int2 >= 0)){
if(ver_int1 == ver_int2){
new_version = 2;
new_version_int = ver_int1;
printf("download_finished\n");
}else{
new_version = 1;
new_version_int = ver_int1;
system("rm "path"/download_finished");
printf("new version\n");
}
}else printf("check fail\n");
fclose(f1);
fclose(f2);
system("rm "path"/version.txt");
}
}
void packet_download(void){
printf("[-------------------------------------------------------------------------------------]packet_download\n");
if(new_version == 1) {
int percent = 0;
char buf[100] = {0};
char bufj[20] ={0};
char bufsend[200] = {0};
struct stat file_stat;
sprintf(bufsend,"wget "download_packet_c" -O "path"/sha1Tab",new_version_int);
system(bufsend);
sprintf(bufsend,"wget "download_packet" -O "path"/update.zip &>"version_path"/download.log &",new_version_int);
system(bufsend);
sleep(1);
FILE * file = fopen(version_path"/download.log","r");
while(1){
sleep(1);
stat(version_path"/download.log",&file_stat);
if(file_stat.st_size > 200){
fseek(file, -100, SEEK_END);
fread(buf,100,1,file);
char *p = strstr(buf,"\n");
char *d = strstr(p+1,"\n");
if(p != NULL&&d != NULL){
*d = 0;
char * s = strstr(buf,"%");
if(s != NULL){
memcpy(bufj,s-3,3);
percent = atoi(bufj);
if(percent!=0)
printf("downloading ...%s,%d\n",bufj,percent);
}
s = strstr(buf,"100%");
if(s != NULL){
printf("download finished ... 100%%,100%%\n");
new_version = 2;
break;
}
}
}
}
fclose(file);
system("rm "version_path"/download.log");
system("touch "path"/download_finished");
char download_finished[200] = {0};
sprintf(download_finished,"echo \"version <%05d>\" > "path"/download_finished",new_version_int);
system(download_finished);
}
}
void packet_update(void){
printf("[-------------------------------------------------------------------------------------]packet_update\n");
if(new_version == 2){
printf("packet_update ...\n");
system("touch /tmp/update_prepare_ok");
#if auto_recovery
system("recovery &");
#endif
}
}
int main(void){
#if use_this_version
system("echo "YR_VERSION" > "version_path"/version");
#endif
download_version_check();
version_check();
packet_download();
packet_update();
}
没有合适的资源?快使用搜索试试~ 我知道了~
linux设备使用http服务自动更新检测
需积分: 5 0 下载量 44 浏览量
2022-07-26
11:16:21
上传
评论
收藏 7KB GZ 举报
温馨提示
共8个文件
recover:1个
mk:1个
sh:1个
【更新流程】 1. 执行update_checking程序,程序检查是否有现成的更新包,且更新包版本是否为最新,若存在则设置可以更新的标志,并跳过检查和下载过程 2. 程序下载版本文件version.txt,对比本地、temp/version文件中的版本号。如果存在新版本,设置可以下载标志位(可选用户交互设置开启下载,本处我们直接下载),启动下载 3. 下载固件包和校验文件,输出log和进度信息重定向到/temp中 4. 下载完成设置更新表完成标志,用户可以选择执行更新 5. 执行更新
资源详情
资源评论
资源推荐
收起资源包目录
update.tar.gz (8个子文件)
update
main.c 4KB
recover 13KB
common.h 794B
auto_build.sh 491B
packet
Makefile 605B
version 17B
Build.mk 518B
.vscode
settings.json 60B
共 8 条
- 1
荒草人生
- 粉丝: 24
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0