#ifndef UPGRADE_H
#define UPGRADE_H
#include <string>
using namespace std;
class upgrade {
public:
string url; //升级程序路径
string version; //版本对比
upgrade()
{
}
~upgrade()
{
}
int main(void);
};
#endif
#include <iostream>
#include <string>
#include "upgrade.h"
using namespace std;
#define DEBUG 1
int upgrade::main(void)
{
// 执行upgrade.sh,进行更新
// 成功失败都重启
int maxline = 512;
char buf[maxline] = {0};
#ifdef DEBUG
snprintf(buf, maxline, "sh -x upgrade.sh %s", url.c_str());
#else
snprintf(buf, maxline, "sh upgrade.sh %s", url.c_str());
#endif
FILE *f = popen(buf, "r");
fread(buf, 1, maxline-1, f);
pclose(f);
if(strstr(buf, "OK") != NULL) {
cout<<"升级成功,重启中。。。"<<endl;
return 0;
}
cout<<buf<<endl;
cout<<"升级失败,重启中。。。"<<endl;
return -1;
}
upgrade.sh
#!/bin/bash
if [ -z "$1" ]
then
echo "Usage: ./upgrade.sh <url>"
exit 1
fi
rm -rf /tmp/update /tmp/update-*.tar /tmp/update-*.tar.xz
#wget -P /tmp -o /dev/null "$1"
wget -P /tmp "$1"
[ $? -ne 0 ] && echo "failed" && exit -1
cd /tmp
xz -d update-*.tar.xz
[ $? -ne 0 ] && echo "failed" && exit -1
tar -xf update-*.tar
[ $? -ne 0 ] && echo "failed" && exit -1
cd -
[ $? -ne 0 ] && echo "failed" && exit -1
cp -af /tmp/update/* `dirname $0`/ # 程序默认放在此目录下
[ $? -ne 0 ] && echo "failed" && exit -1
chmod a+x `dirname $0`/test
chmod a+x `dirname $0`/startauto.sh
chmod a+x `dirname $0`/upgrade.sh
[ $? -ne 0 ] && echo "failed" && exit -1
echo OK