1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033 |
- #include "stdafx.h"
- #include "mod_UpgradeMgr.h"
- #include "UpgradeRun_client_g.h"
- using namespace UpgradeRun;
- #include "Download_client_g.h"
- using namespace Download;
- #include "AccessAuthorization_client_g.h"
- using namespace AccessAuthorization;
- #include "LocalMediaPlay_client_g.h"
- //using namespace LocalMediaPlay;
- #include "EventCode.h"
- //#include "md5file.h"
- #include "fileutil.h"
- #include <regex>
- #include "RVCComm.h"
- #ifdef RVC_OS_WIN
- #include <io.h>
- #else
- #include <unistd.h>
- #include <dirent.h>
- #include <errno.h>
- #endif
- namespace Task
- {
- struct GetMD5Task : public ITaskSp
- {
- CUpgradeMgrEntity* Mgr;
- explicit GetMD5Task(CUpgradeMgrEntity* e) : Mgr(e) {}
- void Process(){
- LOG_FUNCTION();
- bool isSendHashList = false;
- for(int i=0;i<3;i++){
- Dbg("begin get MD5 list");
- //尝试三次机会获取,如失败则不再获取hash文件,计算耗时时间
- CSmallDateTime beginT = CSmallDateTime::GetNow();
- CSimpleStringA tempSM3ListStr = "";
- ErrorCodeEnum rc = Mgr->GetMD5List(tempSM3ListStr);
- CSmallDateTime endT = CSmallDateTime::GetNow();
- Dbg("get MD5 list 耗时%d秒",(DWORD)(endT-beginT));
- if (rc != Error_Succeed)
- {
- LogError(Severity_Middle, rc, 0, "get MD5 list fail");
- }
- else
- {
- CSimpleStringA tempSM3HashStr = "";
- if(Error_Succeed==Mgr->SM3DataToStr(tempSM3ListStr,tempSM3HashStr,true)){
- //通知发送传hash值给服务器,并置获取hash码成功状态位
- Mgr->m_strSM3Hash = tempSM3HashStr;
- Dbg("get MD5 list success, len=%d, SM3 hash=%s", Mgr->m_strMD5List.GetLength(),Mgr->m_strSM3Hash.GetData());
- isSendHashList = true;
- break;
- }else{
- LogError(Severity_Middle, rc, 0, "get MD5 list hash fail");
- }
-
- }
- }
- if(isSendHashList){
- Dbg("send MD5 list to ReportFSM");
- Mgr->m_IsSM3listSucc=1;//获取sm3hash成功
- Mgr->m_fsm.SendSM3ListEvent();
- }else{
- Mgr->m_IsSM3listSucc=-1;//获取sm3hash失败
- Dbg("get MD5 list fail,don't send MD5 list to ReportFSM");
- }
- }
- };
- }
- // 升级管理 UpgradeManager 0x506
- void CUpgradeMgrEntity::OnStarted()
- {
- #if 0 //v4.9.0内容,暂时不加上
- //判断install.ini文件是否存在,不存在则创建新的文件
- if (createInstallFile() != Error_Succeed) {
- LogWarn(Severity_Middle, Error_Exception, ERR_WRAN_CREATE_INSTALL, "create Install.ini error");
- m_testResult = Error_InvalidState;//install.ini文件创建失败,则让健康杀死重试
- return;
- }
- #endif
- // 初始化状态机
- m_fsm.Init(this);
- auto pFunc = GetFunction();
- CUUID subID;
- auto rc = pFunc->SubscribeBroadcast("UpgradeRun", NULL, this, subID);
- if (rc != Error_Succeed)
- {
- LogError(Severity_Middle, rc, 0, "subscribe entity [UpgradeRun] broadcast msg fail");
- //return;
- }
- //test
- //CSystemStaticInfo sysInfo;
- //ZeroMemory(&sysInfo, sizeof(sysInfo));
- //rc = GetFunction()->GetSystemStaticInfo(sysInfo);
- //CVersion CurMachineVersion(sysInfo.MachineVersion.GetMajor(), sysInfo.MachineVersion.GetMinor());
- //Dbg("current version is %s", CurMachineVersion.ToString().GetData());
- //CSimpleStringA testStr = "1.0-4.9";
- //DWORD dwMajor1(0), dwMinor1(0);
- //int n = sscanf(testStr, "%d.%d", &dwMajor1, &dwMinor1);
- //CVersion beginVersion(dwMajor1, dwMinor1);
- //Dbg("begin version is %s", beginVersion.ToString().GetData());
- //auto list = testStr.Split('-');
- //Dbg("teststr is split %d", list.GetCount());
- //if (list.GetCount() >= 2)
- //{
- // DWORD dwMajor2(0), dwMinor2(0);
- // n = sscanf(list[1], "%d.%d", &dwMajor2, &dwMinor2);
- // CVersion endVersion(dwMajor2, dwMinor2);
- // Dbg("end version is %s", endVersion.ToString().GetData());
- // Dbg("test list[0]=%s", list[0].GetData());
- // Dbg("test list[1]=%s", list[1].GetData());
- // bool bMatch = CurMachineVersion >= beginVersion && CurMachineVersion <= endVersion;
- // if (bMatch) {
- // Dbg("bMatch is true");
- // }
- // else {
- // Dbg("bMatch is false");
- // }
- //}
- //else
- //{
- // if (testStr.IsEndWith("-"))
- // {
- // Dbg("testStr.IsEndWith is >=");
- // bool bMatch = CurMachineVersion >= beginVersion;
- // if (bMatch) {
- // Dbg("bMatch is true");
- // }
- // else {
- // Dbg("bMatch is false");
- // }
- // }
- // else
- // {
- // Dbg("testStr.IsEndWith is =");
- // bool bMatch = CurMachineVersion == beginVersion;
- // if (bMatch) {
- // Dbg("bMatch is true");
- // }
- // else {
- // Dbg("bMatch is false");
- // }
- // }
- //}
-
- //// 监视准入状态
- //CSimpleStringA strValue;
- //if (pFunc->GetSysVar("EntryPermit", strValue) == Error_Succeed && strValue.Compare("L") ==0)
- //{
- // // 已经进入准入状态
- // m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
- //}
- //else
- //{
- // rc= pFunc->RegistSysVarEvent("EntryPermit", this);
- // assert(rc == Error_Succeed);
- //}
- //zl@20190311 每次启动时生成新的MD5List
- //rc = GetMD5List(m_strMD5List);
- //if (rc != Error_Succeed)
- //{
- // LogError(Severity_Middle, rc, 0, "GetMD5List fail");
- //}
- //else
- //{
- // Dbg("get MD5 list success, len=%d", m_strMD5List.GetLength());
- //}
- // 监视准入状态
- CSimpleStringA strValue;
- if (pFunc->GetSysVar("UIState", strValue) == Error_Succeed && strValue.Compare("M") ==0)
- {
- // 已经进入首页状态
- Dbg("system page isStartup 1");
- m_bStartUp =true;
- }
- else
- {
- // 否则启动监控
- rc= pFunc->RegistSysVarEvent("UIState", this);
- assert(rc == Error_Succeed);
- }
- //计算时间太长,放入工作线程中
- Task::GetMD5Task* task = new Task::GetMD5Task(this);
- rc = this->GetFunction()->PostThreadPoolTask(task);
- if (rc != Error_Succeed)
- {
- LogError(Severity_Middle, rc, 0, "Post GetMD5Task task to Thread is fail");
- }
- m_beginSM3HashTime = CSmallDateTime::GetNow();
- // 由于准入未上线或可能不稳定,为了保持升级服务的可用性,一直保持可升级状态
- m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
- }
- void CUpgradeMgrEntity::OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- if (Test_ShakeHand == eTestType)
- {
- //根据不同情况返回握手情况,监控实体根据不同情况杀死实体进程
- pTransactionContext->SendAnswer(m_testResult);
- }
- }
- void CUpgradeMgrEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- GetFunction()->UnsubscribeBroadcast("Download");
- GetFunction()->UnsubscribeBroadcast("UpgradeRun");
- pTransactionContext->SendAnswer(Error_Succeed);
- }
- void CUpgradeMgrEntity::OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
- {
- if (strcmp(pszKey, "EntryPermit") ==0)
- {
- if (strcmp(pszValue, "L") ==0)
- {
- // 准入成功
- m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
- // 取消监视
- this->GetFunction()->UnregistSysVarEvent("EntryPermit");
- }
- }
- if(strcmp("UIState",pszKey)==0){
- //Dbg("rx sysvar %s from %s, %s to %s", pszKey, pszEntityName, pszOldValue, pszValue);
- if(!m_bStartUp){
- if (strcmp(pszValue, "M") ==0){
- Dbg("system page isStartup 2");
- m_bStartUp = true;
- }
- }
- }
- }
- void CUpgradeMgrEntity::OnDownloadEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, Download::DownloadResult &evt)
- {
- Dbg("OnDownloadEvent, file:%s, result:%d, errMsg:%s", (const char*)evt.strFileName, evt.errorCode, (const char*)evt.errorMsg);
- m_fsm.PostEventFIFO(new CUpgradeMgrFSM::DownloadedEvent(evt.strFileName, evt.errorCode, evt.errorMsg));
- }
- void CUpgradeMgrEntity::OnUpgradeCheckEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, UpgradeRun::UpgradeCheckEvent &evt)
- {
- Dbg("OnUpgradeCheckEvent, pack:%s, result:%d, errMsg:%s", (const char*)evt.strPackName, evt.error, (const char*)evt.strComment);
- auto pEvent = new CUpgradeMgrFSM::UpgradeRunCheckEvent(evt.strPackName, evt.error, evt.coverList, evt.strComment);
- m_fsm.PostEventFIFO(pEvent);
- }
- void CUpgradeMgrEntity::OnUpgradeDoneEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, UpgradeRun::UpgradeDoneEvent &evt)
- {
- Dbg("OnUpgradeDoneEvent, pack:%s, result:%d, errMsg:%s", (const char*)evt.strPackName, evt.error, (const char*)evt.strComment);
- auto pEvent = new CUpgradeMgrFSM::UpgradeRunDoneEvent(evt.strPackName, evt.error, evt.bSysInstall, evt.bLightPack, evt.strNewVersion, evt.strFWID, evt.strSysPatchName,evt.strComment);
- m_fsm.PostEventFIFO(pEvent);
- //add by zl 20190221
- // 终端置为非升级状态。1:升级状态,0:非升级状态。处于升级状态时调用播放接口不生效
- auto rc = GetFunction()->SetSysVar("UpdateState", "0", true);
- assert(rc == Error_Succeed);
- }
- char CUpgradeMgrEntity::GetInstallStateVal(const InstallStateEnum enumVal)
- {
- const struct {
- int ch;
- InstallStateEnum val;
- } tbl[] = {
- {'A', Install_Active},
- {'I', Install_Pending},
- {'S', Install_SetToStart},
- {'F', Install_FailRun},
- {'R', Install_RollBack},
- {'U', Install_Upgraded},
- {'C',Install_Cancelled},
- {'W',Install_WaitConfirm},
- {'D', Install_Installed},
- };
- int i;
- for (i = 0; i < sizeof(tbl)/sizeof(tbl[0]); ++i) {
- if (tbl[i].val == enumVal) {
- return tbl[i].ch;
- }
- }
- return ' '; // error
- }
- ErrorCodeEnum CUpgradeMgrEntity::RegistLocalPack(const CSimpleStringA &strPackFile)
- {
- // 手动拷贝升级包,本地升级
- return m_fsm.RegistLocalPack(strPackFile);
- }
- DWORD CUpgradeMgrEntity::RegistManualPack(const CSimpleStringA &strPackFile)
- {
- // 用户桌面手动升级
- return m_fsm.RegistManualPack(strPackFile);
- }
- ErrorCodeEnum CUpgradeMgrEntity::DownloadPack(const CSimpleStringA &strPackFile)
- {
- // 通知下载实体准备下载
- auto pClient = new DownloadService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- DownloadService_DownloadFile_Req req = {};
- DownloadService_DownloadFile_Ans ans = {};
- req.strFileName = strPackFile;
- req.dwExpireTime = CSmallDateTime::GetNow() + 3600 * 24 * 14;
- rc = pClient->DownloadFile(req, ans, 5000);
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- Dbg("connect to download entity fail: %d", rc);
- pClient->SafeDelete();
- }
- return rc;
- }
- ErrorCodeEnum CUpgradeMgrEntity::CancelDownloadPack(const CSimpleStringA &strPackFile)
- {
- // 通知下载实体准备下载
- auto pClient = new DownloadService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- DownloadService_CancelDownloadFile_Req req = {};
- DownloadService_CancelDownloadFile_Ans ans = {};
- req.strFileName = strPackFile;
- rc = pClient->CancelDownloadFile(req, ans, 5000);
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- Dbg("connect to download entity fail: %d", rc);
- pClient->SafeDelete();
- }
- return rc;
- }
- ErrorCodeEnum CUpgradeMgrEntity::CancelUpdate(const CSimpleStringA &strPackFile)
- {
- // 取消安装
- m_fsm.PushCancelUpgradePack(strPackFile);
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::RollbackUpdate(const CSimpleStringA &strVersion)
- {
- // 调用框架接口回滚升级
- auto pFunc = GetFunction()->GetPrivilegeFunction();
- assert(pFunc != NULL);
- auto rc = Error_Succeed;
- if (strVersion.GetLength() == 0)
- {
- Dbg("try rollback to previous version");
- rc = pFunc->RollBackToPreviousVersion();
- }
- else
- {
- Dbg("try rollback to version: [%s]", (const char*)strVersion);
- int w1, w2, w3, w4;
- int ret = sscanf(strVersion, "%d.%d.%d.%d", &w1, &w2, &w3, &w4);
- if (ret <4)
- {
- Dbg("version [%s] parse fail", (const char*)strVersion);
- rc= Error_Param;
- }
- else
- {
- rc = pFunc->RollBackToHistoryVersion(CVersion(w1, w2, w3, w4));
- }
- }
- if (rc == Error_Succeed)
- {
- // 通过事件通知健康实体
- LogEvent(Severity_Middle, Event_Req_Framework_Rollback, "rollback upgrade succeed");
- ////更新安装失败包信息(包名和失败次数)
- //UpdatePackFailInfo();
- }
- else
- {
- LogError(Severity_Low, rc, 0, "rollback upgrade fail");
- }
- return rc;
- }
- DWORD CUpgradeMgrEntity::GetManualPacks(CSimpleStringA &strManualPacks)
- {
- return m_fsm.GetManualPacks(strManualPacks);
- }
- ErrorCodeEnum CUpgradeMgrEntity::SyncTime()
- {
- // 通知准入实体同步时间
- AccessAuthService_ClientBase *pClient = new AccessAuthService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- Dbg("connect to entity [AccessAuthorization] succeed, start syncTime now");
- rc = pClient->SyncTime();
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- LogError(Severity_Low, rc, 0, "connect to entity [AccessAuthorization] fail");
- pClient->SafeDelete();
- }
- return rc;
- }
- // 将升级任务推送到运行实体
- ErrorCodeEnum CUpgradeMgrEntity::PushUpdateTask(const CSimpleStringA &strPackName, const int nPackType)
- //ErrorCodeEnum CUpgradeMgrEntity::PushUpdateTask(const CSimpleStringA &strPackName)
- {
- auto pClient = new UpgradeRunService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- UpgradeRunService_PushUpdateTask_Info info;
- info.strPackName = strPackName;
- info.nTaskType = nPackType;
- rc = pClient->PushUpdateTask(info);
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- Dbg("connect to upgrade run entity fail: %d", rc);
- pClient->SafeDelete();
- }
- return rc;
- }
- CServerSessionBase* CUpgradeMgrEntity::OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszParam*/)
- {
- return new CUpgradeMgrSession(this);
- }
- ErrorCodeEnum CUpgradeMgrEntity::SwitchUpgrade(const CSimpleStringA &strPack)
- {
- m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_SwitchNow));
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::GetUpgradeState(bool &bInstalling, CSimpleStringA &strPackFile, CSimpleStringA &strExecID,
- char &cInstallState, bool &bSysInstall, bool &bLightPack, CSimpleStringA &strNewVersion)
- {
- return m_fsm.GetUpgradeState(bInstalling, strPackFile, strExecID, cInstallState, bSysInstall, bLightPack, strNewVersion);
- }
- ErrorCodeEnum CUpgradeMgrEntity::MD5File(CSimpleStringA strFilePath, CSimpleStringA &strMD5)
- {//暂时不引用MD5生成
- //char* pMd5 = MD5FILE::MD5_file((char*)strFilePath.GetData(), 32);
- //if (NULL == pMd5)
- //{
- // Dbg("Get %s MD5 value fail");
- // return Error_Unexpect;
- //}
- //strMD5 = pMd5;
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::SM3_Str(CSimpleStringA &strSM3,BYTE * SM3Byte,bool isSub){
- if(SM3Byte == NULL){
- return Error_Exception;
- }
- int SM3_len=64;
- if(isSub){
- SM3_len=32;
- }
- int i;
- char* file_SM3 = (char*)malloc((SM3_len + 1) * sizeof(char));
- if(file_SM3 == NULL)
- {
- fprintf(stderr, "SM3 malloc failed.\n");
- return Error_Exception;
- }
- memset(file_SM3, 0, (SM3_len + 1));
- if(SM3_len == 32)
- {
- for(i=0; i<16; i++)
- { //SM3截取中间16位字节,8-24位字节数,小写
- sprintf(&file_SM3[i*2], "%02x", SM3Byte[i+8]);
- }
- }
- else if(SM3_len == 64)
- {
- for(i=0; i<32; i++)
- {
- sprintf(&file_SM3[i*2], "%02x", SM3Byte[i]);
- }
- }
- strSM3=file_SM3;
- free(file_SM3);
- return Error_Succeed;
- }
- #if 0 //v4.9.0内容,暂时不加上
- ErrorCodeEnum CUpgradeMgrEntity::createInstallFile()
- {
- CSimpleStringA strVerPath ;
- ErrorCodeEnum rc = this->GetFunction()->GetPath("Base", strVerPath);
- if(rc!=Error_Succeed){
- Dbg("get Base path is fail:%d",(int)rc);
- return rc;
- }
- CSimpleStringA strInstallPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "install.ini", strVerPath.GetData());
- if(ExistsFileA(strInstallPath.GetData())){
- Dbg("install.ini is exsit:%s",strInstallPath.GetData());
- return Error_Succeed;//已存在,框架已加载
- }else{
- Dbg("begin create new install.ini:%s",strInstallPath.GetData());
- //调用框架接口去创建文件
- auto pPrivilegeFunc = this->GetFunction()->GetPrivilegeFunction();
- if(pPrivilegeFunc==NULL){
- Dbg("get GetPrivilegeFunction is null");
- return Error_Null;
- }else{
- if(pPrivilegeFunc->GenerateNewInstallCfg()==Error_Succeed){
- Dbg("create new install.ini succ",strInstallPath.GetData());
- return Error_Succeed;
- }else{
- Dbg("create new install.ini fail ,exec GenerateNewInstallCfg is fail");
- return Error_Exception;
- }
- }
- }
- }
- #endif
- ErrorCodeEnum CUpgradeMgrEntity::SM3FileToStr(CSimpleStringA strFilePath, CSimpleStringA &strSM3,bool isSub)
- {
- if(strFilePath.IsNullOrEmpty()){
- return Error_Null;
- }
- int nlen = strlen(strFilePath.GetData());
- char* pchar = new char[nlen+1];
- strcpy(pchar,strFilePath.GetData());
- BYTE md5[32]={0};
- //Dbg("进行国密加密,file=%s",strFilePath.GetData());
- //if(SM3File(pchar,md5)){
- // delete pchar;
- // 获取16进制的字符串
- // if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
- // Dbg("sm3国密转16进制字符串失败,file=%s",strFilePath.GetData());
- // return Error_Unexpect;
- // }
- // return Error_Succeed;
- //}else{
- // delete pchar;
- // Dbg("sm3国密加密失败,file=%s",strFilePath.GetData());
- // return Error_Unexpect;
- //}
- try
- {
- if(SM3File(pchar,md5)){
- delete pchar;
- //获取16进制的字符串
- if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
- Dbg("sm3国密转16进制字符串失败,file=%s",strFilePath.GetData());
- return Error_Unexpect;
- }
- return Error_Succeed;
- }else{
- delete pchar;
- Dbg("sm3国密加密失败,file=%s",strFilePath.GetData());
- return Error_Unexpect;
- }
- }
- catch (...)
- {
- delete pchar;
- Dbg("sm3国密加密异常失败,file=%s",strFilePath.GetData());
- return Error_Exception;
- }
-
- }
- ErrorCodeEnum CUpgradeMgrEntity::MD5Data(CSimpleStringA strData, CSimpleStringA &strMD5)
- {//暂时不引用MD5生成
- //char* pMd5 = MD5FILE::MD5_data((char*)strData.GetData(), 32);
- //if (NULL == pMd5)
- //{
- // Dbg("Get %s MD5 value fail");
- // return Error_Unexpect;
- //}
- //strMD5 = pMd5;
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::SM3DataToStr(CSimpleStringA strData, CSimpleStringA &strSM3,bool isSub)
- {
- if(strData.IsNullOrEmpty()){
- return Error_Null;
- }
- BYTE md5[32]={0};
- try
- {
- if(SM3Hash((BYTE*)strData.GetData(),strlen(strData.GetData()),md5)){
- //获取16进制的字符串
- if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
- Dbg("sm3国密转16进制字符串失败");
- return Error_Unexpect;
- }
- return Error_Succeed;
- }else{
- Dbg("sm3国密加密失败");
- return Error_Unexpect;
- }
- }
- catch (...)
- {
- Dbg("sm3国密加密异常失败");
- return Error_Exception;
- }
-
- }
- //修改成sm3加密
- ErrorCodeEnum CUpgradeMgrEntity::MD5Folder(CSimpleStringA strFolderPath,bool isDepDIr)
- {
- if (strFolderPath.IsNullOrEmpty())
- {
- return Error_Null;
- }
- Dbg("Start to get file hash list, dir=[%s] ", strFolderPath.GetData());
- #ifdef RVC_OS_WIN
- _finddata_t FileInfo;
- CSimpleStringA strfind = strFolderPath + SPLIT_SLASH_STR+"*";
- long Handle = _findfirst(strfind, &FileInfo);
- if (-1L == Handle)
- {
- _findclose(Handle);
- Dbg("%s文件夹为空", strFolderPath);
- return Error_Succeed;
- }
- CSimpleStringA newPath;
- do{
- if (FileInfo.attrib & _A_SUBDIR)
- {
- if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
- {
-
- CSimpleStringA strFindName = FileInfo.name;
- //整个黑名单文件夹过滤
- bool isBlackDir = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_DirBlacklist.begin();
- while(itor!=m_fsm.m_DirBlacklist.end())
- {
- CSimpleStringA dirBlack = *itor;
- if(strcmp(dirBlack.GetData(),FileInfo.name)==0){
- isBlackDir=true;
- break;
- }
- itor++;
- }
-
- if(isBlackDir){
- CSimpleStringA dirPath = strFolderPath + SPLIT_SLASH_STR + FileInfo.name;
- Dbg("BlackDir Filter don't add to md5 list, dir=[%s]", dirPath.GetData());
- continue;//跳过文件夹
- }else{
- //判断是否是dep文件夹或者是dep文件夹下面的子文件夹
- if(strcmp(FileInfo.name, "dep") == 0||isDepDIr){
- newPath = strFolderPath + SPLIT_SLASH_STR + FileInfo.name;
- ErrorCodeEnum rc = MD5Folder(newPath,true);
- if(Error_Succeed!=rc){
- _findclose(Handle);
- return rc;
- }
- }else{
- newPath = strFolderPath + SPLIT_SLASH_STR + FileInfo.name;
- ErrorCodeEnum rc = MD5Folder(newPath);
- if(Error_Succeed!=rc){
- _findclose(Handle);
- return rc;
- }
- }
- }
- }
- }
- else
- {
- CSimpleStringA strFindName = FileInfo.name;
- {
- if (strFindName.IndexOf(".") == -1)
- {
- Dbg("%s file name is illegal", strFindName);
- continue;
- }
- //Dbg("计算hash码文件:%s",strFindName);
- //整体黑名单文件过滤
- bool isBlackFile = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_FileBlacklist.begin();
- while(itor!=m_fsm.m_FileBlacklist.end())
- {
- CSimpleStringA fileBlack = *itor;
- if(strFindName.IsEndWith(fileBlack.GetData(),true)){
- isBlackFile=true;
- break;
- }
- itor++;
- }
- if(isBlackFile){
- Dbg("BlackFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
- continue;
- }
- //对历史遗留的centerSetting错误缓存文件特殊处理,不纳入hash文件列表
- //对所有集中配置文件都过滤,规则是CenterSetting.*.ini*
- regex e("CenterSetting[.][^.]*[.]ini.*");
- if(std::regex_match(strFindName.GetData(),e,regex_constants::match_default)){
- continue;
- }
- // 过滤install.ini,集中配置,log文件和cfg\certs目录
- // TODO:改成配置
- //if (strFindName.IsEndWith("install.ini", true)
- // || strFindName.IsEndWith("CenterSetting.DMZ.ini", true)
- // || strFindName.IsEndWith("CenterSetting.LAN.ini", true)
- // || strFindName.IsEndWith(".log", true)
- // || strFindName.IsEndWith("RootCert.pem", true)
- // || strFindName.IsEndWith("CaCert.pem", true)
- // || strFindName.IsEndWith("userCert.pem", true)
- // || strFindName.IsEndWith("CertBlackList.txt", true)
- // || strFindName.IsEndWith("idfront.jpg", true)
- // || strFindName.IsEndWith("idback.jpg", true))
- //{
- // Dbg("don't add %s to md5 list", strFindName);
- // continue;
- //}
- //判断是否需要用dep白名单过滤
- if(isDepDIr){
- bool isWhiteFile = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_DepWhitelist.begin();
- while(itor!=m_fsm.m_DepWhitelist.end())
- {
- CSimpleStringA fileWhite = *itor;
- if(strFindName.IsEndWith(fileWhite.GetData(),true)){
- isWhiteFile=true;
- break;
- }
- itor++;
- }
- //不是白名单也不是默认dll文件,不需要加入hash计算
- if(!isWhiteFile&&!strFindName.IsEndWith(".dll", true)){
- Dbg("WhiteFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
- continue;
- }
- }
- CSimpleStringA strFilePath = strFolderPath + SPLIT_SLASH_STR + strFindName;
- CSimpleStringA strMD5Val;
- //ErrorCodeEnum rErrcode = MD5File(strFilePath, strMD5Val);
- //修改为SM3进行哈希
- ErrorCodeEnum rErrcode = SM3FileToStr(strFilePath, strMD5Val,false);
- if (Error_Succeed != rErrcode)
- {
- Dbg("%s获取MD5失败,错误码:%d", strFilePath.GetData(), (int)rErrcode);
- _findclose(Handle);
- return rErrcode;
- }
- m_FileHashMap[strFilePath] = strMD5Val;
- }
- }
- } while (_findnext(Handle, &FileInfo) == 0);
- _findclose(Handle);
- return Error_Succeed;
- #else
- //循环扫描文件算出文件hash值
- DIR* dir;
- struct dirent* entry;
- CSimpleStringA newPath;
- if ((dir = opendir(strFolderPath.GetData())) == NULL)
- {
- Dbg("open dir fail:%d", errno);
- return Error_Unexpect;
- }
- while ((entry = readdir(dir)) != NULL)
- {
- if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
- {//current dir OR parrent dir
- continue;
- }
- else if (entry->d_type == 4)
- {//dir
- CSimpleStringA strFindName = entry->d_name;
- //整个黑名单文件夹过滤
- bool isBlackDir = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_DirBlacklist.begin();
- while (itor != m_fsm.m_DirBlacklist.end())
- {
- CSimpleStringA dirBlack = *itor;
- if (strcmp(dirBlack.GetData(), entry->d_name) == 0) {
- isBlackDir = true;
- break;
- }
- itor++;
- }
- if (isBlackDir) {
- CSimpleStringA dirPath = strFolderPath + SPLIT_SLASH_STR + entry->d_name;
- Dbg("BlackDir Filter don't add to md5 list, dir=[%s]", dirPath.GetData());
- continue;//跳过文件夹
- }
- else {
- //判断是否是dep文件夹或者是dep文件夹下面的子文件夹
- if (strcmp(entry->d_name, "dep") == 0 || isDepDIr) {
- newPath = strFolderPath + SPLIT_SLASH_STR + entry->d_name;
- ErrorCodeEnum rc = MD5Folder(newPath, true);
- if (Error_Succeed != rc) {
- closedir(dir);
- return rc;
- }
- }
- else {
- newPath = strFolderPath + SPLIT_SLASH_STR + entry->d_name;
- ErrorCodeEnum rc = MD5Folder(newPath);
- if (Error_Succeed != rc) {
- closedir(dir);
- return rc;
- }
- }
- }
- }
- else if (entry->d_type == 8)
- {//file
- CSimpleStringA strFindName = entry->d_name;
- {
- //Linux下很多文件是没有后缀名,故放开。
- //if (strFindName.IndexOf(".") == -1)
- //{
- // Dbg("%s file name is illegal", strFindName.GetData());
- // continue;
- //}
- //整体黑名单文件过滤
- bool isBlackFile = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_FileBlacklist.begin();
- while (itor != m_fsm.m_FileBlacklist.end())
- {
- CSimpleStringA fileBlack = *itor;
- if (strFindName.IsEndWith(fileBlack.GetData(), true)) {
- isBlackFile = true;
- break;
- }
- itor++;
- }
- if (isBlackFile) {
- Dbg("BlackFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
- continue;
- }
- //对历史遗留的centerSetting错误缓存文件特殊处理,不纳入hash文件列表
- //对所有集中配置文件都过滤,规则是CenterSetting.*.ini*
- regex e("CenterSetting[.][^.]*[.]ini.*");
- if (std::regex_match(strFindName.GetData(), e, regex_constants::match_default)) {
- continue;
- }
- //判断是否需要用dep白名单过滤
- if (isDepDIr) {
- bool isWhiteFile = false;
- list<CSimpleStringA>::iterator itor = m_fsm.m_DepWhitelist.begin();
- while (itor != m_fsm.m_DepWhitelist.end())
- {
- CSimpleStringA fileWhite = *itor;
- if (strFindName.IsEndWith(fileWhite.GetData(), true)) {
- isWhiteFile = true;
- break;
- }
- itor++;
- }
- //不是白名单也不是默认so文件,不需要加入hash计算
- //if (!isWhiteFile && !strFindName.IsEndWith(".so", true)) {
- // Dbg("WhiteFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
- // continue;
- //}
- if (!isWhiteFile && strFindName.IndexOf(".so")==-1) {
- Dbg("WhiteFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
- continue;
- }
- }
- CSimpleStringA strFilePath = strFolderPath + SPLIT_SLASH_STR + strFindName;
- CSimpleStringA strMD5Val;
- //ErrorCodeEnum rErrcode = MD5File(strFilePath, strMD5Val);
- //修改为SM3进行哈希
- ErrorCodeEnum rErrcode = SM3FileToStr(strFilePath, strMD5Val, false);
- if (Error_Succeed != rErrcode)
- {
- Dbg("%s获取MD5失败,错误码:%d", strFilePath.GetData(), (int)rErrcode);
- closedir(dir);
- return rErrcode;
- }
- Sleep(200);//加入时间碎片
- m_FileHashMap[strFilePath] = strMD5Val;
- }
- }
- else if (entry->d_type == 10)
- {//link file,暂不处理
- continue;
- }
- }
- closedir(dir);
- return Error_Succeed;
- #endif
-
- }
- ErrorCodeEnum CUpgradeMgrEntity::GetMD5List(CSimpleStringA &strMD5List)
- {
- if (!m_strMD5List.IsNullOrEmpty())
- {
- strMD5List = m_strMD5List;
- return Error_Succeed;
- }
- m_FileHashMap.clear();//生成文件hash列表前先清空
- CSystemStaticInfo ssInfo;
- this->GetFunction()->GetSystemStaticInfo(ssInfo);
- CSimpleStringA curVer = ssInfo.InstallVersion.ToString();
- if (curVer.IsNullOrEmpty())
- {
- Dbg("InstallVersion is null");
- return Error_Null;
- }
- CSimpleStringA strRootVerPath;
- this->GetFunction()->GetPath("RootVer", strRootVerPath);
- Dbg("strRootVerPath %s,curVer:%s", strRootVerPath.GetData(),curVer.GetData());
- CSimpleStringA strCurVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (const char*)strRootVerPath, (const char*)curVer);
- if (!ExistsDirA(strCurVerPath))
- {
- Dbg("%d not exist", strCurVerPath);
- return Error_Unexpect;
- }
- ErrorCodeEnum rc = MD5Folder(strCurVerPath);
- if (Error_Succeed != rc)
- {
- return rc;
- }
- for(auto it=m_FileHashMap.begin(); it!=m_FileHashMap.end(); it++)
- {
- strMD5List += it->first;
- strMD5List += ",";
- strMD5List += it->second;
- strMD5List += ";";
- }
- m_strMD5List = strMD5List;
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrEntity::StopMediaPlay()
- {
- // 通知准入实体同步时间
- LocalMediaPlay::PlayService_ClientBase *pClient = new LocalMediaPlay::PlayService_ClientBase(this);
- auto rc = pClient->Connect();
- if (rc == Error_Succeed)
- {
- Dbg("connect to entity [LocalMediaPlay] succeed, start StopMediaPlay now");
- LocalMediaPlay::PlayService_StopPlayVideo_Req req1 = {};
- LocalMediaPlay::PlayService_StopPlayVideo_Ans ans1 = {};
- req1.CfgInx = 1;
- if (Error_Succeed == pClient->StopPlayVideo(req1, ans1, 5000))
- {
- Dbg("StopPlayVideo success");
- }
- LocalMediaPlay::PlayService_StopPlayAudio_Req req2 = {};
- LocalMediaPlay::PlayService_StopPlayAudio_Ans ans2 = {};
- if (Error_Succeed == pClient->StopPlayAudio(req2, ans2, 5000))
- {
- Dbg("StopPlayAudio success");
- }
- pClient->GetFunction()->CloseSession();
- }
- else
- {
- LogError(Severity_Low, rc, 0, "connect to entity [LocalMediaPlay] fail");
- pClient->SafeDelete();
- }
- return rc;
- }
- SP_BEGIN_ENTITY_MAP()
- SP_ENTITY(CUpgradeMgrEntity)
- SP_END_ENTITY_MAP()
|