mod_UpgradeMgr.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. #include "stdafx.h"
  2. #include "mod_UpgradeMgr.h"
  3. #include "UpgradeRun_client_g.h"
  4. using namespace UpgradeRun;
  5. #include "Download_client_g.h"
  6. using namespace Download;
  7. #include "AccessAuthorization_client_g.h"
  8. using namespace AccessAuthorization;
  9. #include "LocalMediaPlay_client_g.h"
  10. //using namespace LocalMediaPlay;
  11. #include "EventCode.h"
  12. //#include "md5file.h"
  13. #include "fileutil.h"
  14. #include <regex>
  15. #include "RVCComm.h"
  16. #ifdef RVC_OS_WIN
  17. #include <io.h>
  18. #else
  19. #include <unistd.h>
  20. #include <dirent.h>
  21. #include <errno.h>
  22. #endif
  23. namespace Task
  24. {
  25. struct GetMD5Task : public ITaskSp
  26. {
  27. CUpgradeMgrEntity* Mgr;
  28. explicit GetMD5Task(CUpgradeMgrEntity* e) : Mgr(e) {}
  29. void Process(){
  30. LOG_FUNCTION();
  31. bool isSendHashList = false;
  32. for(int i=0;i<3;i++){
  33. Dbg("begin get MD5 list");
  34. //尝试三次机会获取,如失败则不再获取hash文件,计算耗时时间
  35. CSmallDateTime beginT = CSmallDateTime::GetNow();
  36. CSimpleStringA tempSM3ListStr = "";
  37. ErrorCodeEnum rc = Mgr->GetMD5List(tempSM3ListStr);
  38. CSmallDateTime endT = CSmallDateTime::GetNow();
  39. Dbg("get MD5 list 耗时%d秒",(DWORD)(endT-beginT));
  40. if (rc != Error_Succeed)
  41. {
  42. LogError(Severity_Middle, rc, 0, "get MD5 list fail");
  43. }
  44. else
  45. {
  46. CSimpleStringA tempSM3HashStr = "";
  47. if(Error_Succeed==Mgr->SM3DataToStr(tempSM3ListStr,tempSM3HashStr,true)){
  48. //通知发送传hash值给服务器,并置获取hash码成功状态位
  49. Mgr->m_strSM3Hash = tempSM3HashStr;
  50. Dbg("get MD5 list success, len=%d, SM3 hash=%s", Mgr->m_strMD5List.GetLength(),Mgr->m_strSM3Hash.GetData());
  51. isSendHashList = true;
  52. break;
  53. }else{
  54. LogError(Severity_Middle, rc, 0, "get MD5 list hash fail");
  55. }
  56. }
  57. }
  58. if(isSendHashList){
  59. Dbg("send MD5 list to ReportFSM");
  60. Mgr->m_IsSM3listSucc=1;//获取sm3hash成功
  61. Mgr->m_fsm.SendSM3ListEvent();
  62. }else{
  63. Mgr->m_IsSM3listSucc=-1;//获取sm3hash失败
  64. Dbg("get MD5 list fail,don't send MD5 list to ReportFSM");
  65. }
  66. }
  67. };
  68. }
  69. // 升级管理 UpgradeManager 0x506
  70. void CUpgradeMgrEntity::OnStarted()
  71. {
  72. #if 0 //v4.9.0内容,暂时不加上
  73. //判断install.ini文件是否存在,不存在则创建新的文件
  74. if (createInstallFile() != Error_Succeed) {
  75. LogWarn(Severity_Middle, Error_Exception, ERR_WRAN_CREATE_INSTALL, "create Install.ini error");
  76. m_testResult = Error_InvalidState;//install.ini文件创建失败,则让健康杀死重试
  77. return;
  78. }
  79. #endif
  80. // 初始化状态机
  81. m_fsm.Init(this);
  82. auto pFunc = GetFunction();
  83. CUUID subID;
  84. auto rc = pFunc->SubscribeBroadcast("UpgradeRun", NULL, this, subID);
  85. if (rc != Error_Succeed)
  86. {
  87. LogError(Severity_Middle, rc, 0, "subscribe entity [UpgradeRun] broadcast msg fail");
  88. //return;
  89. }
  90. //test
  91. //CSystemStaticInfo sysInfo;
  92. //ZeroMemory(&sysInfo, sizeof(sysInfo));
  93. //rc = GetFunction()->GetSystemStaticInfo(sysInfo);
  94. //CVersion CurMachineVersion(sysInfo.MachineVersion.GetMajor(), sysInfo.MachineVersion.GetMinor());
  95. //Dbg("current version is %s", CurMachineVersion.ToString().GetData());
  96. //CSimpleStringA testStr = "1.0-4.9";
  97. //DWORD dwMajor1(0), dwMinor1(0);
  98. //int n = sscanf(testStr, "%d.%d", &dwMajor1, &dwMinor1);
  99. //CVersion beginVersion(dwMajor1, dwMinor1);
  100. //Dbg("begin version is %s", beginVersion.ToString().GetData());
  101. //auto list = testStr.Split('-');
  102. //Dbg("teststr is split %d", list.GetCount());
  103. //if (list.GetCount() >= 2)
  104. //{
  105. // DWORD dwMajor2(0), dwMinor2(0);
  106. // n = sscanf(list[1], "%d.%d", &dwMajor2, &dwMinor2);
  107. // CVersion endVersion(dwMajor2, dwMinor2);
  108. // Dbg("end version is %s", endVersion.ToString().GetData());
  109. // Dbg("test list[0]=%s", list[0].GetData());
  110. // Dbg("test list[1]=%s", list[1].GetData());
  111. // bool bMatch = CurMachineVersion >= beginVersion && CurMachineVersion <= endVersion;
  112. // if (bMatch) {
  113. // Dbg("bMatch is true");
  114. // }
  115. // else {
  116. // Dbg("bMatch is false");
  117. // }
  118. //}
  119. //else
  120. //{
  121. // if (testStr.IsEndWith("-"))
  122. // {
  123. // Dbg("testStr.IsEndWith is >=");
  124. // bool bMatch = CurMachineVersion >= beginVersion;
  125. // if (bMatch) {
  126. // Dbg("bMatch is true");
  127. // }
  128. // else {
  129. // Dbg("bMatch is false");
  130. // }
  131. // }
  132. // else
  133. // {
  134. // Dbg("testStr.IsEndWith is =");
  135. // bool bMatch = CurMachineVersion == beginVersion;
  136. // if (bMatch) {
  137. // Dbg("bMatch is true");
  138. // }
  139. // else {
  140. // Dbg("bMatch is false");
  141. // }
  142. // }
  143. //}
  144. //// 监视准入状态
  145. //CSimpleStringA strValue;
  146. //if (pFunc->GetSysVar("EntryPermit", strValue) == Error_Succeed && strValue.Compare("L") ==0)
  147. //{
  148. // // 已经进入准入状态
  149. // m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
  150. //}
  151. //else
  152. //{
  153. // rc= pFunc->RegistSysVarEvent("EntryPermit", this);
  154. // assert(rc == Error_Succeed);
  155. //}
  156. //zl@20190311 每次启动时生成新的MD5List
  157. //rc = GetMD5List(m_strMD5List);
  158. //if (rc != Error_Succeed)
  159. //{
  160. // LogError(Severity_Middle, rc, 0, "GetMD5List fail");
  161. //}
  162. //else
  163. //{
  164. // Dbg("get MD5 list success, len=%d", m_strMD5List.GetLength());
  165. //}
  166. // 监视准入状态
  167. CSimpleStringA strValue;
  168. if (pFunc->GetSysVar("UIState", strValue) == Error_Succeed && strValue.Compare("M") ==0)
  169. {
  170. // 已经进入首页状态
  171. Dbg("system page isStartup 1");
  172. m_bStartUp =true;
  173. }
  174. else
  175. {
  176. // 否则启动监控
  177. rc= pFunc->RegistSysVarEvent("UIState", this);
  178. assert(rc == Error_Succeed);
  179. }
  180. //计算时间太长,放入工作线程中
  181. Task::GetMD5Task* task = new Task::GetMD5Task(this);
  182. rc = this->GetFunction()->PostThreadPoolTask(task);
  183. if (rc != Error_Succeed)
  184. {
  185. LogError(Severity_Middle, rc, 0, "Post GetMD5Task task to Thread is fail");
  186. }
  187. m_beginSM3HashTime = CSmallDateTime::GetNow();
  188. // 由于准入未上线或可能不稳定,为了保持升级服务的可用性,一直保持可升级状态
  189. m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
  190. }
  191. void CUpgradeMgrEntity::OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  192. {
  193. if (Test_ShakeHand == eTestType)
  194. {
  195. //根据不同情况返回握手情况,监控实体根据不同情况杀死实体进程
  196. pTransactionContext->SendAnswer(m_testResult);
  197. }
  198. }
  199. void CUpgradeMgrEntity::OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  200. {
  201. GetFunction()->UnsubscribeBroadcast("Download");
  202. GetFunction()->UnsubscribeBroadcast("UpgradeRun");
  203. pTransactionContext->SendAnswer(Error_Succeed);
  204. }
  205. void CUpgradeMgrEntity::OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  206. {
  207. if (strcmp(pszKey, "EntryPermit") ==0)
  208. {
  209. if (strcmp(pszValue, "L") ==0)
  210. {
  211. // 准入成功
  212. m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_EntryPermit));
  213. // 取消监视
  214. this->GetFunction()->UnregistSysVarEvent("EntryPermit");
  215. }
  216. }
  217. if(strcmp("UIState",pszKey)==0){
  218. //Dbg("rx sysvar %s from %s, %s to %s", pszKey, pszEntityName, pszOldValue, pszValue);
  219. if(!m_bStartUp){
  220. if (strcmp(pszValue, "M") ==0){
  221. Dbg("system page isStartup 2");
  222. m_bStartUp = true;
  223. }
  224. }
  225. }
  226. }
  227. void CUpgradeMgrEntity::OnDownloadEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, Download::DownloadResult &evt)
  228. {
  229. Dbg("OnDownloadEvent, file:%s, result:%d, errMsg:%s", (const char*)evt.strFileName, evt.errorCode, (const char*)evt.errorMsg);
  230. m_fsm.PostEventFIFO(new CUpgradeMgrFSM::DownloadedEvent(evt.strFileName, evt.errorCode, evt.errorMsg));
  231. }
  232. void CUpgradeMgrEntity::OnUpgradeCheckEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, UpgradeRun::UpgradeCheckEvent &evt)
  233. {
  234. Dbg("OnUpgradeCheckEvent, pack:%s, result:%d, errMsg:%s", (const char*)evt.strPackName, evt.error, (const char*)evt.strComment);
  235. auto pEvent = new CUpgradeMgrFSM::UpgradeRunCheckEvent(evt.strPackName, evt.error, evt.coverList, evt.strComment);
  236. m_fsm.PostEventFIFO(pEvent);
  237. }
  238. void CUpgradeMgrEntity::OnUpgradeDoneEvent(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, UpgradeRun::UpgradeDoneEvent &evt)
  239. {
  240. Dbg("OnUpgradeDoneEvent, pack:%s, result:%d, errMsg:%s", (const char*)evt.strPackName, evt.error, (const char*)evt.strComment);
  241. auto pEvent = new CUpgradeMgrFSM::UpgradeRunDoneEvent(evt.strPackName, evt.error, evt.bSysInstall, evt.bLightPack, evt.strNewVersion, evt.strFWID, evt.strSysPatchName,evt.strComment);
  242. m_fsm.PostEventFIFO(pEvent);
  243. //add by zl 20190221
  244. // 终端置为非升级状态。1:升级状态,0:非升级状态。处于升级状态时调用播放接口不生效
  245. auto rc = GetFunction()->SetSysVar("UpdateState", "0", true);
  246. assert(rc == Error_Succeed);
  247. }
  248. char CUpgradeMgrEntity::GetInstallStateVal(const InstallStateEnum enumVal)
  249. {
  250. const struct {
  251. int ch;
  252. InstallStateEnum val;
  253. } tbl[] = {
  254. {'A', Install_Active},
  255. {'I', Install_Pending},
  256. {'S', Install_SetToStart},
  257. {'F', Install_FailRun},
  258. {'R', Install_RollBack},
  259. {'U', Install_Upgraded},
  260. {'C',Install_Cancelled},
  261. {'W',Install_WaitConfirm},
  262. {'D', Install_Installed},
  263. };
  264. int i;
  265. for (i = 0; i < sizeof(tbl)/sizeof(tbl[0]); ++i) {
  266. if (tbl[i].val == enumVal) {
  267. return tbl[i].ch;
  268. }
  269. }
  270. return ' '; // error
  271. }
  272. ErrorCodeEnum CUpgradeMgrEntity::RegistLocalPack(const CSimpleStringA &strPackFile)
  273. {
  274. // 手动拷贝升级包,本地升级
  275. return m_fsm.RegistLocalPack(strPackFile);
  276. }
  277. DWORD CUpgradeMgrEntity::RegistManualPack(const CSimpleStringA &strPackFile)
  278. {
  279. // 用户桌面手动升级
  280. return m_fsm.RegistManualPack(strPackFile);
  281. }
  282. ErrorCodeEnum CUpgradeMgrEntity::DownloadPack(const CSimpleStringA &strPackFile)
  283. {
  284. // 通知下载实体准备下载
  285. auto pClient = new DownloadService_ClientBase(this);
  286. auto rc = pClient->Connect();
  287. if (rc == Error_Succeed)
  288. {
  289. DownloadService_DownloadFile_Req req = {};
  290. DownloadService_DownloadFile_Ans ans = {};
  291. req.strFileName = strPackFile;
  292. req.dwExpireTime = CSmallDateTime::GetNow() + 3600 * 24 * 14;
  293. rc = pClient->DownloadFile(req, ans, 5000);
  294. pClient->GetFunction()->CloseSession();
  295. }
  296. else
  297. {
  298. Dbg("connect to download entity fail: %d", rc);
  299. pClient->SafeDelete();
  300. }
  301. return rc;
  302. }
  303. ErrorCodeEnum CUpgradeMgrEntity::CancelDownloadPack(const CSimpleStringA &strPackFile)
  304. {
  305. // 通知下载实体准备下载
  306. auto pClient = new DownloadService_ClientBase(this);
  307. auto rc = pClient->Connect();
  308. if (rc == Error_Succeed)
  309. {
  310. DownloadService_CancelDownloadFile_Req req = {};
  311. DownloadService_CancelDownloadFile_Ans ans = {};
  312. req.strFileName = strPackFile;
  313. rc = pClient->CancelDownloadFile(req, ans, 5000);
  314. pClient->GetFunction()->CloseSession();
  315. }
  316. else
  317. {
  318. Dbg("connect to download entity fail: %d", rc);
  319. pClient->SafeDelete();
  320. }
  321. return rc;
  322. }
  323. ErrorCodeEnum CUpgradeMgrEntity::CancelUpdate(const CSimpleStringA &strPackFile)
  324. {
  325. // 取消安装
  326. m_fsm.PushCancelUpgradePack(strPackFile);
  327. return Error_Succeed;
  328. }
  329. ErrorCodeEnum CUpgradeMgrEntity::RollbackUpdate(const CSimpleStringA &strVersion)
  330. {
  331. // 调用框架接口回滚升级
  332. auto pFunc = GetFunction()->GetPrivilegeFunction();
  333. assert(pFunc != NULL);
  334. auto rc = Error_Succeed;
  335. if (strVersion.GetLength() == 0)
  336. {
  337. Dbg("try rollback to previous version");
  338. rc = pFunc->RollBackToPreviousVersion();
  339. }
  340. else
  341. {
  342. Dbg("try rollback to version: [%s]", (const char*)strVersion);
  343. int w1, w2, w3, w4;
  344. int ret = sscanf(strVersion, "%d.%d.%d.%d", &w1, &w2, &w3, &w4);
  345. if (ret <4)
  346. {
  347. Dbg("version [%s] parse fail", (const char*)strVersion);
  348. rc= Error_Param;
  349. }
  350. else
  351. {
  352. rc = pFunc->RollBackToHistoryVersion(CVersion(w1, w2, w3, w4));
  353. }
  354. }
  355. if (rc == Error_Succeed)
  356. {
  357. // 通过事件通知健康实体
  358. LogEvent(Severity_Middle, Event_Req_Framework_Rollback, "rollback upgrade succeed");
  359. ////更新安装失败包信息(包名和失败次数)
  360. //UpdatePackFailInfo();
  361. }
  362. else
  363. {
  364. LogError(Severity_Low, rc, 0, "rollback upgrade fail");
  365. }
  366. return rc;
  367. }
  368. DWORD CUpgradeMgrEntity::GetManualPacks(CSimpleStringA &strManualPacks)
  369. {
  370. return m_fsm.GetManualPacks(strManualPacks);
  371. }
  372. ErrorCodeEnum CUpgradeMgrEntity::SyncTime()
  373. {
  374. // 通知准入实体同步时间
  375. AccessAuthService_ClientBase *pClient = new AccessAuthService_ClientBase(this);
  376. auto rc = pClient->Connect();
  377. if (rc == Error_Succeed)
  378. {
  379. Dbg("connect to entity [AccessAuthorization] succeed, start syncTime now");
  380. rc = pClient->SyncTime();
  381. pClient->GetFunction()->CloseSession();
  382. }
  383. else
  384. {
  385. LogError(Severity_Low, rc, 0, "connect to entity [AccessAuthorization] fail");
  386. pClient->SafeDelete();
  387. }
  388. return rc;
  389. }
  390. // 将升级任务推送到运行实体
  391. ErrorCodeEnum CUpgradeMgrEntity::PushUpdateTask(const CSimpleStringA &strPackName, const int nPackType)
  392. //ErrorCodeEnum CUpgradeMgrEntity::PushUpdateTask(const CSimpleStringA &strPackName)
  393. {
  394. auto pClient = new UpgradeRunService_ClientBase(this);
  395. auto rc = pClient->Connect();
  396. if (rc == Error_Succeed)
  397. {
  398. UpgradeRunService_PushUpdateTask_Info info;
  399. info.strPackName = strPackName;
  400. info.nTaskType = nPackType;
  401. rc = pClient->PushUpdateTask(info);
  402. pClient->GetFunction()->CloseSession();
  403. }
  404. else
  405. {
  406. Dbg("connect to upgrade run entity fail: %d", rc);
  407. pClient->SafeDelete();
  408. }
  409. return rc;
  410. }
  411. CServerSessionBase* CUpgradeMgrEntity::OnNewSession(const char* /*pszRemoteEntityName*/, const char * /*pszParam*/)
  412. {
  413. return new CUpgradeMgrSession(this);
  414. }
  415. ErrorCodeEnum CUpgradeMgrEntity::SwitchUpgrade(const CSimpleStringA &strPack)
  416. {
  417. m_fsm.PostEventFIFO(new FSMEvent(CUpgradeMgrFSM::Event_SwitchNow));
  418. return Error_Succeed;
  419. }
  420. ErrorCodeEnum CUpgradeMgrEntity::GetUpgradeState(bool &bInstalling, CSimpleStringA &strPackFile, CSimpleStringA &strExecID,
  421. char &cInstallState, bool &bSysInstall, bool &bLightPack, CSimpleStringA &strNewVersion)
  422. {
  423. return m_fsm.GetUpgradeState(bInstalling, strPackFile, strExecID, cInstallState, bSysInstall, bLightPack, strNewVersion);
  424. }
  425. ErrorCodeEnum CUpgradeMgrEntity::MD5File(CSimpleStringA strFilePath, CSimpleStringA &strMD5)
  426. {//暂时不引用MD5生成
  427. //char* pMd5 = MD5FILE::MD5_file((char*)strFilePath.GetData(), 32);
  428. //if (NULL == pMd5)
  429. //{
  430. // Dbg("Get %s MD5 value fail");
  431. // return Error_Unexpect;
  432. //}
  433. //strMD5 = pMd5;
  434. return Error_Succeed;
  435. }
  436. ErrorCodeEnum CUpgradeMgrEntity::SM3_Str(CSimpleStringA &strSM3,BYTE * SM3Byte,bool isSub){
  437. if(SM3Byte == NULL){
  438. return Error_Exception;
  439. }
  440. int SM3_len=64;
  441. if(isSub){
  442. SM3_len=32;
  443. }
  444. int i;
  445. char* file_SM3 = (char*)malloc((SM3_len + 1) * sizeof(char));
  446. if(file_SM3 == NULL)
  447. {
  448. fprintf(stderr, "SM3 malloc failed.\n");
  449. return Error_Exception;
  450. }
  451. memset(file_SM3, 0, (SM3_len + 1));
  452. if(SM3_len == 32)
  453. {
  454. for(i=0; i<16; i++)
  455. { //SM3截取中间16位字节,8-24位字节数,小写
  456. sprintf(&file_SM3[i*2], "%02x", SM3Byte[i+8]);
  457. }
  458. }
  459. else if(SM3_len == 64)
  460. {
  461. for(i=0; i<32; i++)
  462. {
  463. sprintf(&file_SM3[i*2], "%02x", SM3Byte[i]);
  464. }
  465. }
  466. strSM3=file_SM3;
  467. free(file_SM3);
  468. return Error_Succeed;
  469. }
  470. #if 0 //v4.9.0内容,暂时不加上
  471. ErrorCodeEnum CUpgradeMgrEntity::createInstallFile()
  472. {
  473. CSimpleStringA strVerPath ;
  474. ErrorCodeEnum rc = this->GetFunction()->GetPath("Base", strVerPath);
  475. if(rc!=Error_Succeed){
  476. Dbg("get Base path is fail:%d",(int)rc);
  477. return rc;
  478. }
  479. CSimpleStringA strInstallPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "install.ini", strVerPath.GetData());
  480. if(ExistsFileA(strInstallPath.GetData())){
  481. Dbg("install.ini is exsit:%s",strInstallPath.GetData());
  482. return Error_Succeed;//已存在,框架已加载
  483. }else{
  484. Dbg("begin create new install.ini:%s",strInstallPath.GetData());
  485. //调用框架接口去创建文件
  486. auto pPrivilegeFunc = this->GetFunction()->GetPrivilegeFunction();
  487. if(pPrivilegeFunc==NULL){
  488. Dbg("get GetPrivilegeFunction is null");
  489. return Error_Null;
  490. }else{
  491. if(pPrivilegeFunc->GenerateNewInstallCfg()==Error_Succeed){
  492. Dbg("create new install.ini succ",strInstallPath.GetData());
  493. return Error_Succeed;
  494. }else{
  495. Dbg("create new install.ini fail ,exec GenerateNewInstallCfg is fail");
  496. return Error_Exception;
  497. }
  498. }
  499. }
  500. }
  501. #endif
  502. ErrorCodeEnum CUpgradeMgrEntity::SM3FileToStr(CSimpleStringA strFilePath, CSimpleStringA &strSM3,bool isSub)
  503. {
  504. if(strFilePath.IsNullOrEmpty()){
  505. return Error_Null;
  506. }
  507. int nlen = strlen(strFilePath.GetData());
  508. char* pchar = new char[nlen+1];
  509. strcpy(pchar,strFilePath.GetData());
  510. BYTE md5[32]={0};
  511. //Dbg("进行国密加密,file=%s",strFilePath.GetData());
  512. //if(SM3File(pchar,md5)){
  513. // delete pchar;
  514. // 获取16进制的字符串
  515. // if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
  516. // Dbg("sm3国密转16进制字符串失败,file=%s",strFilePath.GetData());
  517. // return Error_Unexpect;
  518. // }
  519. // return Error_Succeed;
  520. //}else{
  521. // delete pchar;
  522. // Dbg("sm3国密加密失败,file=%s",strFilePath.GetData());
  523. // return Error_Unexpect;
  524. //}
  525. try
  526. {
  527. if(SM3File(pchar,md5)){
  528. delete pchar;
  529. //获取16进制的字符串
  530. if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
  531. Dbg("sm3国密转16进制字符串失败,file=%s",strFilePath.GetData());
  532. return Error_Unexpect;
  533. }
  534. return Error_Succeed;
  535. }else{
  536. delete pchar;
  537. Dbg("sm3国密加密失败,file=%s",strFilePath.GetData());
  538. return Error_Unexpect;
  539. }
  540. }
  541. catch (...)
  542. {
  543. delete pchar;
  544. Dbg("sm3国密加密异常失败,file=%s",strFilePath.GetData());
  545. return Error_Exception;
  546. }
  547. }
  548. ErrorCodeEnum CUpgradeMgrEntity::MD5Data(CSimpleStringA strData, CSimpleStringA &strMD5)
  549. {//暂时不引用MD5生成
  550. //char* pMd5 = MD5FILE::MD5_data((char*)strData.GetData(), 32);
  551. //if (NULL == pMd5)
  552. //{
  553. // Dbg("Get %s MD5 value fail");
  554. // return Error_Unexpect;
  555. //}
  556. //strMD5 = pMd5;
  557. return Error_Succeed;
  558. }
  559. ErrorCodeEnum CUpgradeMgrEntity::SM3DataToStr(CSimpleStringA strData, CSimpleStringA &strSM3,bool isSub)
  560. {
  561. if(strData.IsNullOrEmpty()){
  562. return Error_Null;
  563. }
  564. BYTE md5[32]={0};
  565. try
  566. {
  567. if(SM3Hash((BYTE*)strData.GetData(),strlen(strData.GetData()),md5)){
  568. //获取16进制的字符串
  569. if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
  570. Dbg("sm3国密转16进制字符串失败");
  571. return Error_Unexpect;
  572. }
  573. return Error_Succeed;
  574. }else{
  575. Dbg("sm3国密加密失败");
  576. return Error_Unexpect;
  577. }
  578. }
  579. catch (...)
  580. {
  581. Dbg("sm3国密加密异常失败");
  582. return Error_Exception;
  583. }
  584. }
  585. //修改成sm3加密
  586. ErrorCodeEnum CUpgradeMgrEntity::MD5Folder(CSimpleStringA strFolderPath,bool isDepDIr)
  587. {
  588. if (strFolderPath.IsNullOrEmpty())
  589. {
  590. return Error_Null;
  591. }
  592. Dbg("Start to get file hash list, dir=[%s] ", strFolderPath.GetData());
  593. #ifdef RVC_OS_WIN
  594. _finddata_t FileInfo;
  595. CSimpleStringA strfind = strFolderPath + SPLIT_SLASH_STR+"*";
  596. long Handle = _findfirst(strfind, &FileInfo);
  597. if (-1L == Handle)
  598. {
  599. _findclose(Handle);
  600. Dbg("%s文件夹为空", strFolderPath);
  601. return Error_Succeed;
  602. }
  603. CSimpleStringA newPath;
  604. do{
  605. if (FileInfo.attrib & _A_SUBDIR)
  606. {
  607. if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
  608. {
  609. CSimpleStringA strFindName = FileInfo.name;
  610. //整个黑名单文件夹过滤
  611. bool isBlackDir = false;
  612. list<CSimpleStringA>::iterator itor = m_fsm.m_DirBlacklist.begin();
  613. while(itor!=m_fsm.m_DirBlacklist.end())
  614. {
  615. CSimpleStringA dirBlack = *itor;
  616. if(strcmp(dirBlack.GetData(),FileInfo.name)==0){
  617. isBlackDir=true;
  618. break;
  619. }
  620. itor++;
  621. }
  622. if(isBlackDir){
  623. CSimpleStringA dirPath = strFolderPath + SPLIT_SLASH_STR + FileInfo.name;
  624. Dbg("BlackDir Filter don't add to md5 list, dir=[%s]", dirPath.GetData());
  625. continue;//跳过文件夹
  626. }else{
  627. //判断是否是dep文件夹或者是dep文件夹下面的子文件夹
  628. if(strcmp(FileInfo.name, "dep") == 0||isDepDIr){
  629. newPath = strFolderPath + SPLIT_SLASH_STR + FileInfo.name;
  630. ErrorCodeEnum rc = MD5Folder(newPath,true);
  631. if(Error_Succeed!=rc){
  632. _findclose(Handle);
  633. return rc;
  634. }
  635. }else{
  636. newPath = strFolderPath + SPLIT_SLASH_STR + FileInfo.name;
  637. ErrorCodeEnum rc = MD5Folder(newPath);
  638. if(Error_Succeed!=rc){
  639. _findclose(Handle);
  640. return rc;
  641. }
  642. }
  643. }
  644. }
  645. }
  646. else
  647. {
  648. CSimpleStringA strFindName = FileInfo.name;
  649. {
  650. if (strFindName.IndexOf(".") == -1)
  651. {
  652. Dbg("%s file name is illegal", strFindName);
  653. continue;
  654. }
  655. //Dbg("计算hash码文件:%s",strFindName);
  656. //整体黑名单文件过滤
  657. bool isBlackFile = false;
  658. list<CSimpleStringA>::iterator itor = m_fsm.m_FileBlacklist.begin();
  659. while(itor!=m_fsm.m_FileBlacklist.end())
  660. {
  661. CSimpleStringA fileBlack = *itor;
  662. if(strFindName.IsEndWith(fileBlack.GetData(),true)){
  663. isBlackFile=true;
  664. break;
  665. }
  666. itor++;
  667. }
  668. if(isBlackFile){
  669. Dbg("BlackFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
  670. continue;
  671. }
  672. //对历史遗留的centerSetting错误缓存文件特殊处理,不纳入hash文件列表
  673. //对所有集中配置文件都过滤,规则是CenterSetting.*.ini*
  674. regex e("CenterSetting[.][^.]*[.]ini.*");
  675. if(std::regex_match(strFindName.GetData(),e,regex_constants::match_default)){
  676. continue;
  677. }
  678. // 过滤install.ini,集中配置,log文件和cfg\certs目录
  679. // TODO:改成配置
  680. //if (strFindName.IsEndWith("install.ini", true)
  681. // || strFindName.IsEndWith("CenterSetting.DMZ.ini", true)
  682. // || strFindName.IsEndWith("CenterSetting.LAN.ini", true)
  683. // || strFindName.IsEndWith(".log", true)
  684. // || strFindName.IsEndWith("RootCert.pem", true)
  685. // || strFindName.IsEndWith("CaCert.pem", true)
  686. // || strFindName.IsEndWith("userCert.pem", true)
  687. // || strFindName.IsEndWith("CertBlackList.txt", true)
  688. // || strFindName.IsEndWith("idfront.jpg", true)
  689. // || strFindName.IsEndWith("idback.jpg", true))
  690. //{
  691. // Dbg("don't add %s to md5 list", strFindName);
  692. // continue;
  693. //}
  694. //判断是否需要用dep白名单过滤
  695. if(isDepDIr){
  696. bool isWhiteFile = false;
  697. list<CSimpleStringA>::iterator itor = m_fsm.m_DepWhitelist.begin();
  698. while(itor!=m_fsm.m_DepWhitelist.end())
  699. {
  700. CSimpleStringA fileWhite = *itor;
  701. if(strFindName.IsEndWith(fileWhite.GetData(),true)){
  702. isWhiteFile=true;
  703. break;
  704. }
  705. itor++;
  706. }
  707. //不是白名单也不是默认dll文件,不需要加入hash计算
  708. if(!isWhiteFile&&!strFindName.IsEndWith(".dll", true)){
  709. Dbg("WhiteFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
  710. continue;
  711. }
  712. }
  713. CSimpleStringA strFilePath = strFolderPath + SPLIT_SLASH_STR + strFindName;
  714. CSimpleStringA strMD5Val;
  715. //ErrorCodeEnum rErrcode = MD5File(strFilePath, strMD5Val);
  716. //修改为SM3进行哈希
  717. ErrorCodeEnum rErrcode = SM3FileToStr(strFilePath, strMD5Val,false);
  718. if (Error_Succeed != rErrcode)
  719. {
  720. Dbg("%s获取MD5失败,错误码:%d", strFilePath.GetData(), (int)rErrcode);
  721. _findclose(Handle);
  722. return rErrcode;
  723. }
  724. m_FileHashMap[strFilePath] = strMD5Val;
  725. }
  726. }
  727. } while (_findnext(Handle, &FileInfo) == 0);
  728. _findclose(Handle);
  729. return Error_Succeed;
  730. #else
  731. //循环扫描文件算出文件hash值
  732. DIR* dir;
  733. struct dirent* entry;
  734. CSimpleStringA newPath;
  735. if ((dir = opendir(strFolderPath.GetData())) == NULL)
  736. {
  737. Dbg("open dir fail:%d", errno);
  738. return Error_Unexpect;
  739. }
  740. while ((entry = readdir(dir)) != NULL)
  741. {
  742. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  743. {//current dir OR parrent dir
  744. continue;
  745. }
  746. else if (entry->d_type == 4)
  747. {//dir
  748. CSimpleStringA strFindName = entry->d_name;
  749. //整个黑名单文件夹过滤
  750. bool isBlackDir = false;
  751. list<CSimpleStringA>::iterator itor = m_fsm.m_DirBlacklist.begin();
  752. while (itor != m_fsm.m_DirBlacklist.end())
  753. {
  754. CSimpleStringA dirBlack = *itor;
  755. if (strcmp(dirBlack.GetData(), entry->d_name) == 0) {
  756. isBlackDir = true;
  757. break;
  758. }
  759. itor++;
  760. }
  761. if (isBlackDir) {
  762. CSimpleStringA dirPath = strFolderPath + SPLIT_SLASH_STR + entry->d_name;
  763. Dbg("BlackDir Filter don't add to md5 list, dir=[%s]", dirPath.GetData());
  764. continue;//跳过文件夹
  765. }
  766. else {
  767. //判断是否是dep文件夹或者是dep文件夹下面的子文件夹
  768. if (strcmp(entry->d_name, "dep") == 0 || isDepDIr) {
  769. newPath = strFolderPath + SPLIT_SLASH_STR + entry->d_name;
  770. ErrorCodeEnum rc = MD5Folder(newPath, true);
  771. if (Error_Succeed != rc) {
  772. closedir(dir);
  773. return rc;
  774. }
  775. }
  776. else {
  777. newPath = strFolderPath + SPLIT_SLASH_STR + entry->d_name;
  778. ErrorCodeEnum rc = MD5Folder(newPath);
  779. if (Error_Succeed != rc) {
  780. closedir(dir);
  781. return rc;
  782. }
  783. }
  784. }
  785. }
  786. else if (entry->d_type == 8)
  787. {//file
  788. CSimpleStringA strFindName = entry->d_name;
  789. {
  790. //Linux下很多文件是没有后缀名,故放开。
  791. //if (strFindName.IndexOf(".") == -1)
  792. //{
  793. // Dbg("%s file name is illegal", strFindName.GetData());
  794. // continue;
  795. //}
  796. //整体黑名单文件过滤
  797. bool isBlackFile = false;
  798. list<CSimpleStringA>::iterator itor = m_fsm.m_FileBlacklist.begin();
  799. while (itor != m_fsm.m_FileBlacklist.end())
  800. {
  801. CSimpleStringA fileBlack = *itor;
  802. if (strFindName.IsEndWith(fileBlack.GetData(), true)) {
  803. isBlackFile = true;
  804. break;
  805. }
  806. itor++;
  807. }
  808. if (isBlackFile) {
  809. Dbg("BlackFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
  810. continue;
  811. }
  812. //对历史遗留的centerSetting错误缓存文件特殊处理,不纳入hash文件列表
  813. //对所有集中配置文件都过滤,规则是CenterSetting.*.ini*
  814. regex e("CenterSetting[.][^.]*[.]ini.*");
  815. if (std::regex_match(strFindName.GetData(), e, regex_constants::match_default)) {
  816. continue;
  817. }
  818. //判断是否需要用dep白名单过滤
  819. if (isDepDIr) {
  820. bool isWhiteFile = false;
  821. list<CSimpleStringA>::iterator itor = m_fsm.m_DepWhitelist.begin();
  822. while (itor != m_fsm.m_DepWhitelist.end())
  823. {
  824. CSimpleStringA fileWhite = *itor;
  825. if (strFindName.IsEndWith(fileWhite.GetData(), true)) {
  826. isWhiteFile = true;
  827. break;
  828. }
  829. itor++;
  830. }
  831. //不是白名单也不是默认so文件,不需要加入hash计算
  832. //if (!isWhiteFile && !strFindName.IsEndWith(".so", true)) {
  833. // Dbg("WhiteFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
  834. // continue;
  835. //}
  836. if (!isWhiteFile && strFindName.IndexOf(".so")==-1) {
  837. Dbg("WhiteFile Filter don't add to md5 list, file = [%s]", strFindName.GetData());
  838. continue;
  839. }
  840. }
  841. CSimpleStringA strFilePath = strFolderPath + SPLIT_SLASH_STR + strFindName;
  842. CSimpleStringA strMD5Val;
  843. //ErrorCodeEnum rErrcode = MD5File(strFilePath, strMD5Val);
  844. //修改为SM3进行哈希
  845. ErrorCodeEnum rErrcode = SM3FileToStr(strFilePath, strMD5Val, false);
  846. if (Error_Succeed != rErrcode)
  847. {
  848. Dbg("%s获取MD5失败,错误码:%d", strFilePath.GetData(), (int)rErrcode);
  849. closedir(dir);
  850. return rErrcode;
  851. }
  852. Sleep(200);//加入时间碎片
  853. m_FileHashMap[strFilePath] = strMD5Val;
  854. }
  855. }
  856. else if (entry->d_type == 10)
  857. {//link file,暂不处理
  858. continue;
  859. }
  860. }
  861. closedir(dir);
  862. return Error_Succeed;
  863. #endif
  864. }
  865. ErrorCodeEnum CUpgradeMgrEntity::GetMD5List(CSimpleStringA &strMD5List)
  866. {
  867. if (!m_strMD5List.IsNullOrEmpty())
  868. {
  869. strMD5List = m_strMD5List;
  870. return Error_Succeed;
  871. }
  872. m_FileHashMap.clear();//生成文件hash列表前先清空
  873. CSystemStaticInfo ssInfo;
  874. this->GetFunction()->GetSystemStaticInfo(ssInfo);
  875. CSimpleStringA curVer = ssInfo.InstallVersion.ToString();
  876. if (curVer.IsNullOrEmpty())
  877. {
  878. Dbg("InstallVersion is null");
  879. return Error_Null;
  880. }
  881. CSimpleStringA strRootVerPath;
  882. this->GetFunction()->GetPath("RootVer", strRootVerPath);
  883. Dbg("strRootVerPath %s,curVer:%s", strRootVerPath.GetData(),curVer.GetData());
  884. CSimpleStringA strCurVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (const char*)strRootVerPath, (const char*)curVer);
  885. if (!ExistsDirA(strCurVerPath))
  886. {
  887. Dbg("%d not exist", strCurVerPath);
  888. return Error_Unexpect;
  889. }
  890. ErrorCodeEnum rc = MD5Folder(strCurVerPath);
  891. if (Error_Succeed != rc)
  892. {
  893. return rc;
  894. }
  895. for(auto it=m_FileHashMap.begin(); it!=m_FileHashMap.end(); it++)
  896. {
  897. strMD5List += it->first;
  898. strMD5List += ",";
  899. strMD5List += it->second;
  900. strMD5List += ";";
  901. }
  902. m_strMD5List = strMD5List;
  903. return Error_Succeed;
  904. }
  905. ErrorCodeEnum CUpgradeMgrEntity::StopMediaPlay()
  906. {
  907. // 通知准入实体同步时间
  908. LocalMediaPlay::PlayService_ClientBase *pClient = new LocalMediaPlay::PlayService_ClientBase(this);
  909. auto rc = pClient->Connect();
  910. if (rc == Error_Succeed)
  911. {
  912. Dbg("connect to entity [LocalMediaPlay] succeed, start StopMediaPlay now");
  913. LocalMediaPlay::PlayService_StopPlayVideo_Req req1 = {};
  914. LocalMediaPlay::PlayService_StopPlayVideo_Ans ans1 = {};
  915. req1.CfgInx = 1;
  916. if (Error_Succeed == pClient->StopPlayVideo(req1, ans1, 5000))
  917. {
  918. Dbg("StopPlayVideo success");
  919. }
  920. LocalMediaPlay::PlayService_StopPlayAudio_Req req2 = {};
  921. LocalMediaPlay::PlayService_StopPlayAudio_Ans ans2 = {};
  922. if (Error_Succeed == pClient->StopPlayAudio(req2, ans2, 5000))
  923. {
  924. Dbg("StopPlayAudio success");
  925. }
  926. pClient->GetFunction()->CloseSession();
  927. }
  928. else
  929. {
  930. LogError(Severity_Low, rc, 0, "connect to entity [LocalMediaPlay] fail");
  931. pClient->SafeDelete();
  932. }
  933. return rc;
  934. }
  935. SP_BEGIN_ENTITY_MAP()
  936. SP_ENTITY(CUpgradeMgrEntity)
  937. SP_END_ENTITY_MAP()