SpEntityPrivilege.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpTimer.h"
  4. #include "SpMisc.h"
  5. #include "SpEntity.h"
  6. #include "SpModule.h"
  7. #include "SpEntityPrivilege.h"
  8. #include "SpAsyncWait.h"
  9. #include "sp_iom.h"
  10. #include "sp_svc.h"
  11. #include "sp_dbg_export.h"
  12. #include "sp_log.h"
  13. #include "sp_var.h"
  14. #include "sp_def.h"
  15. #include "sp_cfg.h"
  16. #include "sp_env.h"
  17. #include "memutil.h"
  18. #include "strutil.h"
  19. #include "fileutil.h"
  20. #include "shm_array.h"
  21. #include "shm_mem.h"
  22. #include "iobuffer.h"
  23. #include "iniutil.h"
  24. //#include "sp_sal.h"
  25. #include "CodeSignVerify.h"
  26. #include "..\spshell\def.h"
  27. #include <io.h>
  28. static ErrorCodeEnum ControlEntity(SpEntity *pEntity, const char *pszEntityName, int call_type, const char *cmd_line, int param1, int param2, CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
  29. {
  30. if (!pszEntityName)
  31. return Error_Null;
  32. sp_env_t *env = sp_get_env();
  33. sp_entity_t *ent = sp_mod_mgr_find_entity_by_name(env->mod_mgr, pszEntityName);
  34. if (!ent)
  35. return Error_NotExist;
  36. iobuffer_t *req_pkt = iobuffer_create(-1, -1);
  37. int v = ent->cfg->idx;
  38. iobuffer_write(req_pkt, IOBUF_T_I4, &v, 0);
  39. if (call_type == SHELL_CMD_REQ_ENTITY_START)
  40. iobuffer_write(req_pkt, IOBUF_T_STR, cmd_line, -1);
  41. else if (call_type == SHELL_CMD_REQ_ENTITY_TEST)
  42. iobuffer_write(req_pkt, IOBUF_T_I4, &param1, 0);
  43. SpAsyncWaitRPC *pAsyncWait = new SpAsyncWaitRPC(pEntity, &req_pkt, call_type);
  44. ErrorCodeEnum Error = pAsyncWait->Begin();
  45. if (Error == Error_Succeed) {
  46. pAsynWaitSp.Attach(pAsyncWait, pAsyncWait->GetRefCountPtr());
  47. }
  48. pAsyncWait->DecrementRef(); // xkm@20150115
  49. if (req_pkt)
  50. iobuffer_dec_ref(req_pkt);
  51. return Error;
  52. }
  53. SpEntityPrivilege::SpEntityPrivilege(SpModule *pModule, sp_entity_t *ent, sp_cfg_shell_entity_t *cfg_ent, CEntityBase *pEntityBase)
  54. : SpEntity(pModule, ent, cfg_ent, pEntityBase), m_pEntityLifeListener(NULL)
  55. {
  56. spinlock_init(&m_maplock);
  57. m_pEntityStateListenerMap = stringmap_create(0);
  58. }
  59. SpEntityPrivilege::~SpEntityPrivilege()
  60. {
  61. UnregistEntityStateEvent(NULL);
  62. stringmap_destroy(m_pEntityStateListenerMap);
  63. }
  64. ErrorCodeEnum SpEntityPrivilege::Init()
  65. {
  66. ErrorCodeEnum Error = SpEntity::Init();
  67. return Error;
  68. }
  69. void SpEntityPrivilege::Term()
  70. {
  71. //....
  72. SpEntity::Term();
  73. UnregistEntityStateEvent(NULL); // unregister all
  74. }
  75. CSmartPointer<IEntityFunctionPrivilege> SpEntityPrivilege::GetPrivilegeFunction()
  76. {
  77. IEntityFunctionPrivilege *pPrivilege = dynamic_cast<IEntityFunctionPrivilege*>(this);
  78. return pPrivilege;
  79. }
  80. ErrorCodeEnum SpEntityPrivilege::StartEntity(const char *pszEntityName, const char *pszCmdLine,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
  81. {
  82. return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_START, pszCmdLine, 0, 0, pAsynWaitSp);
  83. }
  84. ErrorCodeEnum SpEntityPrivilege::StopEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
  85. {
  86. return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_STOP, NULL, 0, 0, pAsynWaitSp);
  87. }
  88. ErrorCodeEnum SpEntityPrivilege::CloseEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
  89. {
  90. return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_STOP, NULL, 0, 0, pAsynWaitSp);
  91. }
  92. ErrorCodeEnum SpEntityPrivilege::TerminateEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
  93. {
  94. return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_TERMINATE, NULL, 0, 0, pAsynWaitSp);
  95. }
  96. ErrorCodeEnum SpEntityPrivilege::PauseEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
  97. {
  98. return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_PAUSE, NULL, 0, 0, pAsynWaitSp);
  99. }
  100. ErrorCodeEnum SpEntityPrivilege::ContinueEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
  101. {
  102. return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_CONTINUE, NULL, 0, 0, pAsynWaitSp);
  103. }
  104. ErrorCodeEnum SpEntityPrivilege::TestEntity(const char *pszEntityName,EntityTestEnum eTestType, CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
  105. {
  106. return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_TEST, NULL, (int)eTestType, 0, pAsynWaitSp);
  107. }
  108. ErrorCodeEnum SpEntityPrivilege::RegistEntityLifeEvent(IEntityLifeListener *pListener)
  109. {
  110. if (!m_pEntityLifeListener) {
  111. sp_mod_entity_life_cb cb;
  112. cb.on_entity_close = &SpEntityPrivilege::__on_entity_close;
  113. cb.on_entity_create = &SpEntityPrivilege::__on_entity_create;
  114. cb.on_entity_exception = &SpEntityPrivilege::__on_entity_exception;
  115. cb.user_data = this;
  116. int rc = sp_mod_entity_life_listener_create(&cb, m_svc, pListener, &m_pEntityLifeListener);
  117. return SpTranslateError(rc);
  118. } else {
  119. return Error_Duplication;
  120. }
  121. }
  122. ErrorCodeEnum SpEntityPrivilege::UnregistLiftEvent()
  123. {
  124. if (!m_pEntityLifeListener) {
  125. return Error_NotInit;
  126. } else {
  127. sp_mod_entity_life_listener_destroy(m_pEntityLifeListener);
  128. m_pEntityLifeListener = NULL;
  129. return Error_Succeed;
  130. }
  131. }
  132. ErrorCodeEnum SpEntityPrivilege::RegistEntityStateEvent(const char *pszEntityName,IEntityStateListener *pListener)
  133. {
  134. sp_env_t *env = sp_get_env();
  135. int target_ent_id;
  136. if (!pListener)
  137. return Error_Null;
  138. if (pszEntityName) {
  139. sp_entity_t *ent;
  140. ent = sp_mod_mgr_find_entity_by_name(env->mod_mgr, pszEntityName);
  141. if (!ent)
  142. return Error_NotExist;
  143. target_ent_id = ent->cfg->idx;
  144. } else {
  145. LOG_TRACE("registered state event for all entities!");
  146. target_ent_id = -1; // regist for all entities
  147. pszEntityName = "*";
  148. }
  149. ErrorCodeEnum Error = Error_Succeed;
  150. spinlock_enter(&m_maplock, -1);
  151. if (stringmap_find(m_pEntityStateListenerMap, pszEntityName) ){
  152. Error = Error_AlreadyExist;
  153. } else {
  154. int rc;
  155. sp_mod_entity_state_listener_t *listener;
  156. sp_mod_entity_event_cb cb;
  157. cb.on_close_connection = &SpEntityPrivilege::__on_close_connection;
  158. cb.on_create_connection = &SpEntityPrivilege::__on_create_connection;
  159. cb.on_user_state = &SpEntityPrivilege::__on_user_state;;
  160. cb.on_entity_state = &SpEntityPrivilege::__on_entity_state;
  161. cb.user_data = this;
  162. rc = sp_mod_entity_listener_create(target_ent_id, &cb, m_svc, pListener, &listener);
  163. if (rc == 0) {
  164. stringmap_add(m_pEntityStateListenerMap, pszEntityName ? pszEntityName : "*", listener);
  165. } else {
  166. Error = SpTranslateError(rc);
  167. }
  168. }
  169. spinlock_leave(&m_maplock);
  170. return Error;
  171. }
  172. ErrorCodeEnum SpEntityPrivilege::UnregistEntityStateEvent(const char *pszEntityName)
  173. {
  174. ErrorCodeEnum Error = Error_Succeed;
  175. if (pszEntityName == NULL) {
  176. pszEntityName = "*";
  177. }
  178. spinlock_enter(&m_maplock, -1);
  179. if (pszEntityName) {
  180. stringmap_kv_pair *kvp;
  181. kvp = stringmap_find(m_pEntityStateListenerMap, pszEntityName);
  182. if (kvp) {
  183. sp_mod_entity_state_listener_t *listener;
  184. listener = (sp_mod_entity_state_listener_t *)stringmap_kv_pair_get_value(kvp);
  185. stringmap_remove(m_pEntityStateListenerMap, pszEntityName);
  186. sp_mod_entity_listener_destroy(listener);
  187. } else {
  188. Error = Error_NotExist;
  189. }
  190. } else { // unregist all
  191. stringmap_iterator *it = stringmap_iterator_create(m_pEntityStateListenerMap);
  192. while (stringmap_iterator_next(it) == 0) {
  193. const char *ent_name = (const char*)stringmap_iterator_get_key(it);
  194. sp_mod_entity_state_listener_t *listener = (sp_mod_entity_state_listener_t *)stringmap_iterator_get_value(it);
  195. stringmap_remove(m_pEntityStateListenerMap, ent_name);
  196. sp_mod_entity_listener_destroy(listener);
  197. }
  198. stringmap_iterator_destroy(it);
  199. }
  200. spinlock_leave(&m_maplock);
  201. return Error;
  202. }
  203. ErrorCodeEnum SpEntityPrivilege::DisplayBlueScreen(const char *pszTitle)
  204. {
  205. ErrorCodeEnum Error;
  206. iobuffer_t *pkt = iobuffer_create(-1, -1);
  207. iobuffer_write(pkt, IOBUF_T_STR, pszTitle, -1);
  208. //int nBlueScreen = 1;
  209. //iobuffer_write(pkt, IOBUF_T_I4, &nBlueScreen, 0);
  210. Error = PostInfoShell(SHELL_CMD_INFO_BLUESCREEN_DISPLAY, &pkt);
  211. if (pkt)
  212. iobuffer_dec_ref(pkt);
  213. return Error;
  214. }
  215. ErrorCodeEnum SpEntityPrivilege::UndisplayBlueScreen()
  216. {
  217. ErrorCodeEnum Error;
  218. iobuffer_t *pkt = iobuffer_create(-1, -1);
  219. Error = PostInfoShell(SHELL_CMD_INFO_BLUESCREEN_UNDISPLAY, &pkt);
  220. if (pkt)
  221. iobuffer_dec_ref(pkt);
  222. return Error;
  223. }
  224. ErrorCodeEnum SpEntityPrivilege::Reboot(RebootTriggerEnum eTriggerReason,RebootWayEnum eWay)
  225. {
  226. ErrorCodeEnum Error;
  227. int v;
  228. iobuffer_t *pkt = iobuffer_create(-1, -1);
  229. v = eTriggerReason;
  230. iobuffer_write(pkt, IOBUF_T_I4, &v, 0);
  231. v = eWay;
  232. iobuffer_write(pkt, IOBUF_T_I4, &v, 0);
  233. Error = PostInfoShell(SHELL_CMD_INFO_MACHINE_REBOOT, &pkt);
  234. if (pkt)
  235. iobuffer_dec_ref(pkt);
  236. return Error;
  237. }
  238. bool SpEntityPrivilege::WriteInstallLog(const char *pszPackName, const char *pszLogText)
  239. {
  240. auto env = sp_get_env();
  241. // 创建安装包日志文件
  242. CSimpleStringA strInstallLogDir = CSimpleStringA::Format("%s\\InstallLog", env->dir->root_runinfo_path);
  243. if (!ExistsDirA(strInstallLogDir))
  244. CreateDirA(strInstallLogDir, TRUE);
  245. // 写安装包日志
  246. CSimpleStringA strInstallLog = CSimpleStringA::Format("%s\\%s.log", (const char*)strInstallLogDir, pszPackName);
  247. FILE *pInstallLog = fopen(strInstallLog, "a+");
  248. if (pInstallLog == NULL)
  249. {
  250. LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("open install log file [%s] to write fail", pszPackName));
  251. return false;
  252. }
  253. fprintf_s(pInstallLog, "[%s] %s\r\n", (const char*)CSmallDateTime::GetNow().ToTimeString(), pszLogText);
  254. fflush(pInstallLog);
  255. fclose(pInstallLog);
  256. return true;
  257. }
  258. bool SpEntityPrivilege::RecursiveCopyDir(const char *pszSourceDir, const char *pszDestDir)
  259. {
  260. array_header_t *arr;
  261. if (!ExistsDirA(pszSourceDir))
  262. {
  263. Dbg("source dir [%s] not exists", pszSourceDir);
  264. return false;
  265. }
  266. if (!ExistsDirA(pszDestDir))
  267. {
  268. if (!CreateDirRecursiveA(pszDestDir))
  269. {
  270. Dbg("create dir [%s] fail: %d", pszDestDir, GetLastError());
  271. return false;
  272. }
  273. Dbg("create dir [%s] succ", pszDestDir);
  274. }
  275. arr = fileutil_get_sub_files_a(pszSourceDir);
  276. if (arr)
  277. {
  278. int i;
  279. for (i = 0; i < arr->nelts; ++i)
  280. {
  281. char szDestFile[256] = { 0 };
  282. char *file = ARRAY_IDX(arr, i, char*);
  283. strcpy(szDestFile, pszDestDir);
  284. if (szDestFile[strlen(szDestFile) - 1] != '\\')
  285. strcat(szDestFile, "\\");
  286. strcat(szDestFile, strrchr(file, '\\') + 1);
  287. if (!CopyFileA(file, szDestFile, FALSE))
  288. {
  289. Dbg("copy file [%s] fail: %d", file, GetLastError());
  290. toolkit_array_free2(arr);
  291. return false;
  292. }
  293. Dbg("copy file [%s] succ", file);
  294. }
  295. toolkit_array_free2(arr);
  296. }
  297. arr = fileutil_get_sub_dirs_a(pszSourceDir);
  298. if (arr)
  299. {
  300. int i;
  301. for (i = 0; i < arr->nelts; ++i)
  302. {
  303. char szDestSubDir[256] = { 0 };
  304. char *dir = ARRAY_IDX(arr, i, char*);
  305. strcpy(szDestSubDir, pszDestDir);
  306. if (szDestSubDir[strlen(szDestSubDir) - 1] != '\\')
  307. strcat(szDestSubDir, "\\");
  308. strcat(szDestSubDir, strrchr(dir, '\\') + 1);
  309. if (!RecursiveCopyDir(dir, szDestSubDir))
  310. {
  311. Dbg("copy dir [%s] fail: %d", dir, GetLastError());
  312. toolkit_array_free2(arr);
  313. return false;
  314. }
  315. Dbg("copy dir [%s] succ", dir);
  316. }
  317. toolkit_array_free2(arr);
  318. }
  319. return true;
  320. }
  321. // 创建新版本目录,拷贝当前版本bin、cfg、dep、mod、install.ini,并修改Shell.ini及install.ini中相关配置
  322. ErrorCodeEnum SpEntityPrivilege::CreateInstallNewVersion(CVersion NewSoftwareVersion,const char *pszPackageName)
  323. {
  324. auto env = sp_get_env();
  325. auto cfg = env->cfg;
  326. // 创建安装包日志文件
  327. WriteInstallLog(pszPackageName, CSimpleStringA::Format("Create new version: [%s], pack: [%s]",
  328. (const char*)NewSoftwareVersion.ToString(), (const char*)pszPackageName));
  329. CSimpleStringA strCurVerPath = env->dir->base_path;
  330. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)NewSoftwareVersion.ToString());
  331. auto &v = cfg->install_ini->install_version;
  332. CVersion curVersion(v.major, v.minor, v.revision, v.build);
  333. assert(curVersion != NewSoftwareVersion);
  334. if (curVersion == NewSoftwareVersion)
  335. {
  336. LogError(Severity_Low, Error_Bug, 0, CSimpleStringA::Format("new ver [%s] equals current ver [%s]",
  337. (const char*)NewSoftwareVersion.ToString(), (const char*)curVersion.ToString()));
  338. return Error_Bug;
  339. }
  340. if (ExistsDirA(strNewVerPath))
  341. {
  342. Dbg("new version dir [%s] already exist, delete it", (const char*)strNewVerPath);
  343. RemoveDirRecursiveA(strNewVerPath);
  344. }
  345. if (!CreateDirA(strNewVerPath, TRUE))
  346. {
  347. Dbg("create new version dir fail: %d", GetLastError());
  348. return Error_Unexpect;
  349. }
  350. Dbg("create new version dir succ");
  351. CSimpleStringA strSource = CSimpleStringA::Format("%s\\bin", (const char*)strCurVerPath);
  352. CSimpleStringA strDest = CSimpleStringA::Format("%s\\bin", (const char*)strNewVerPath);
  353. if (!RecursiveCopyDir(strSource, strDest))
  354. {
  355. Dbg("copy bin dir fail");
  356. return Error_Unexpect;
  357. }
  358. Dbg("copy bin dir succ");
  359. strSource = CSimpleStringA::Format("%s\\cfg", (const char*)strCurVerPath);
  360. strDest = CSimpleStringA::Format("%s\\cfg", (const char*)strNewVerPath);
  361. if (!RecursiveCopyDir(strSource, strDest))
  362. {
  363. Dbg("copy cfg dir fail");
  364. return Error_Unexpect;
  365. }
  366. Dbg("copy cfg dir succ");
  367. strSource = CSimpleStringA::Format("%s\\dep", (const char*)strCurVerPath);
  368. strDest = CSimpleStringA::Format("%s\\dep", (const char*)strNewVerPath);
  369. if (!RecursiveCopyDir(strSource, strDest))
  370. {
  371. Dbg("copy dep dir fail");
  372. return Error_Unexpect;
  373. }
  374. Dbg("copy dep dir succ");
  375. strSource = CSimpleStringA::Format("%s\\mod", (const char*)strCurVerPath);
  376. strDest = CSimpleStringA::Format("%s\\mod", (const char*)strNewVerPath);
  377. if (!RecursiveCopyDir(strSource, strDest))
  378. {
  379. Dbg("copy mod dir fail");
  380. return Error_Unexpect;
  381. }
  382. Dbg("copy mod dir succ");
  383. strSource = CSimpleStringA::Format("%s\\res", (const char*)strCurVerPath);
  384. strDest = CSimpleStringA::Format("%s\\res", (const char*)strNewVerPath);
  385. // 检测res目录是否存在
  386. if (ExistsDirA(strSource))
  387. {
  388. if (!RecursiveCopyDir(strSource, strDest))
  389. {
  390. Dbg("copy res dir fail");
  391. return Error_Unexpect;
  392. }
  393. Dbg("copy res dir succ");
  394. }
  395. else
  396. {
  397. if (!CreateDirA(strDest, TRUE))
  398. {
  399. Dbg("create res dir fail");
  400. return Error_Unexpect;
  401. }
  402. Dbg("create res dir succ");
  403. }
  404. strSource = CSimpleStringA::Format("%s\\imdep", (const char*)strCurVerPath);
  405. strDest = CSimpleStringA::Format("%s\\imdep", (const char*)strNewVerPath);
  406. // 检测imdep目录是否存在
  407. if (ExistsDirA(strSource))
  408. {
  409. if (!RecursiveCopyDir(strSource, strDest))
  410. {
  411. Dbg("copy imdep dir fail");
  412. return Error_Unexpect;
  413. }
  414. Dbg("copy imdep dir succ");
  415. }
  416. else
  417. {
  418. if (!CreateDirA(strDest, TRUE))
  419. {
  420. Dbg("create imdep dir fail");
  421. return Error_Unexpect;
  422. }
  423. Dbg("create imdep dir succ");
  424. }
  425. strSource = CSimpleStringA::Format("%s\\install.ini", (const char*)strCurVerPath);
  426. strDest = CSimpleStringA::Format("%s\\install.ini", (const char*)strNewVerPath);
  427. if (!CopyFileA(strSource, strDest, FALSE))
  428. {
  429. Dbg("copy install.ini fail: %d", GetLastError());
  430. return Error_Unexpect;
  431. }
  432. Dbg("copy install.ini succ");
  433. // 修改新Shell.ini
  434. CSimpleStringA strNewShellIni = CSimpleStringA::Format("%s\\cfg\\shell.ini", (const char*) strNewVerPath);
  435. CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s\\runcfg\\shellVar.ini", (const char*)env->dir->root_runinfo_path);
  436. Dbg("update SoftwareVersion to %s", strShellVarIni);
  437. int nRet = inifile_format_write((const char*)strShellVarIni, "Main", "SoftwareVersion", "%d.%d.%d", NewSoftwareVersion.GetMajor(),
  438. NewSoftwareVersion.GetMinor(), NewSoftwareVersion.GetRevision());
  439. //// 修改当前install.ini
  440. inifile_write_str(cfg->install_ini_path, "Main", "LatterInstallVersion", NewSoftwareVersion.ToString());
  441. inifile_write_str(cfg->install_ini_path, "Main", "LatterInstallPack", pszPackageName);
  442. //inifile_write_str(cfg->install_ini_path, CurVersion.ToString(), "InstallState", "U");
  443. // 修改新版本install.ini
  444. auto strNewInstallIni = CSimpleStringA::Format("%s\\install.ini", (const char*)strNewVerPath);
  445. assert(ExistsFileA(strNewInstallIni));
  446. char szNow[16] = {};
  447. sprintf(szNow, "0x%08X", y2k_time_now());
  448. // 缓存前版本轻量包记录
  449. char *packs = inifile_read_str(strNewInstallIni, "Main", "LightPack", "");
  450. nRet |= inifile_write_str(strNewInstallIni, "Main", "InstallVersion", NewSoftwareVersion.ToString());
  451. nRet |= inifile_write_str(strNewInstallIni, "Main", "CreateDate", szNow);
  452. nRet |= inifile_write_str(strNewInstallIni, "Main", "LatterInstallVersion", "");
  453. nRet |= inifile_write_str(strNewInstallIni, "Main", "LightPack", "");
  454. nRet |= inifile_write_str(strNewInstallIni, "Main", "TotalRunCount", "1");
  455. nRet |= inifile_write_str(strNewInstallIni, "Main", "TodayRunCount", "1");
  456. nRet |= inifile_write_str(strNewInstallIni, "Main", "CurrentTime", szNow);
  457. nRet |= inifile_write_str(strNewInstallIni, NewSoftwareVersion.ToString(), "PreviousInstallVersion", curVersion.ToString());
  458. nRet |= inifile_write_str(strNewInstallIni, NewSoftwareVersion.ToString(), "InstallPack", pszPackageName);
  459. nRet |= inifile_write_str(strNewInstallIni, NewSoftwareVersion.ToString(), "InstallState", "I"); // 正在安装
  460. // 去掉所有历史版本中轻量安装记录
  461. if (packs)
  462. {
  463. if (strlen(packs) >0)
  464. {
  465. char *p = NULL;
  466. while((p = strrchr(packs, ',')) != NULL)
  467. {
  468. *p = 0;
  469. WritePrivateProfileSectionA(p+1, NULL, strNewInstallIni);
  470. }
  471. WritePrivateProfileSectionA(packs, NULL, strNewInstallIni);
  472. }
  473. toolkit_free(packs);
  474. }
  475. Dbg("write version into install.ini done");
  476. // 修改内存配置项
  477. auto ver_info = (sp_cfg_version_info_t*)shm_malloc(sizeof(sp_cfg_version_info_t));
  478. memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
  479. ver_info->version.major = NewSoftwareVersion.GetMajor();
  480. ver_info->version.minor = NewSoftwareVersion.GetMinor();
  481. ver_info->version.revision = NewSoftwareVersion.GetRevision();
  482. ver_info->version.build = NewSoftwareVersion.GetBuild();
  483. ver_info->previous_version.major = curVersion.GetMajor();
  484. ver_info->previous_version.minor = curVersion.GetMinor();
  485. ver_info->previous_version.revision = curVersion.GetRevision();
  486. ver_info->previous_version.build = curVersion.GetBuild();
  487. ver_info->install_pack = shm_strdup(pszPackageName);
  488. ver_info->install_state = Install_Pending;
  489. SHM_ARRAY_PUSH(cfg->install_ini->arr_version, sp_cfg_version_info_t*) = ver_info;
  490. cfg->install_ini->latter_install_version.major = NewSoftwareVersion.GetMajor();
  491. cfg->install_ini->latter_install_version.minor = NewSoftwareVersion.GetMinor();
  492. cfg->install_ini->latter_install_version.revision = NewSoftwareVersion.GetRevision();
  493. cfg->install_ini->latter_install_version.build = NewSoftwareVersion.GetBuild();
  494. Dbg("write version into share mem done");
  495. return nRet == 0 ? Error_Succeed : Error_Unexpect;
  496. }
  497. ErrorCodeEnum CopyFolder(CSimpleStringA strSourcePath, CSimpleStringA strDestPath)
  498. {
  499. if (strSourcePath.IsNullOrEmpty() || strDestPath.IsNullOrEmpty())
  500. {
  501. return Error_Null;
  502. }
  503. Dbg("Start to copy dir %s to dir %s", (const char*)strSourcePath,(const char*)strDestPath);
  504. _finddata_t FileInfo;
  505. CSimpleStringA strfind = strSourcePath + "\\*";
  506. long Handle = _findfirst(strfind, &FileInfo);
  507. if (-1L == Handle)
  508. {
  509. _findclose(Handle);
  510. Dbg("%s文件夹为空", strSourcePath);
  511. return Error_Succeed;
  512. }
  513. CSimpleStringA newPath;
  514. do{
  515. if (FileInfo.attrib & _A_SUBDIR)
  516. {
  517. if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
  518. {
  519. newPath = strSourcePath + "\\" + FileInfo.name;
  520. CSimpleStringA newDestPath = strDestPath + "\\" + FileInfo.name;
  521. /*if (strcmp(FileInfo.name, "") == 0)
  522. {
  523. continue;
  524. }*/
  525. if (!ExistsDirA(newDestPath))
  526. {
  527. CreateDirA(newDestPath, TRUE);
  528. Dbg("Create Dir %s success", (const char*)newDestPath);
  529. }
  530. CopyFolder(newPath, newDestPath);
  531. }
  532. }
  533. else
  534. {
  535. CSimpleStringA strFindName = FileInfo.name;
  536. {
  537. CSimpleStringA strSourceFile = strSourcePath + "\\" + strFindName;
  538. CSimpleStringA strDestFile = strDestPath + "\\" + strFindName;
  539. if (ExistsFileA(strSourceFile))
  540. {
  541. // 先去除目标文件只读属性
  542. if (ExistsFileA(strDestFile))
  543. RemoveFileReadOnlyAttributeA(strDestFile);
  544. LOG_TRACE("copy file from [%s] to [%s]", (const char*)strSourceFile, (const char*)strDestFile);
  545. BOOL bRet = CopyFileA(strSourceFile, strDestFile, FALSE);
  546. // 去除只读属性
  547. if (bRet)
  548. RemoveFileReadOnlyAttributeA(strDestFile);
  549. else
  550. {
  551. // 覆盖失败,则生成.new副本
  552. Dbg("copy file [%s] to [%s] fail, now save as .new file", (const char*) strSourcePath, (const char*) strDestFile);
  553. strDestFile += ".new";
  554. bRet = CopyFileA(strSourceFile, strDestFile, FALSE);
  555. if (bRet)
  556. {
  557. RemoveFileReadOnlyAttributeA(strDestPath);
  558. Dbg("copy [%s] as [%s] succeed", (const char*)strSourceFile, (const char*) strDestFile);
  559. }
  560. else
  561. {
  562. LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("copy file [%s] as [%s] fail", (const char*) strSourceFile, (const char*) strDestFile));
  563. return Error_Block;
  564. }
  565. }
  566. }
  567. else
  568. {
  569. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to copy not exists", (const char*)strSourceFile));
  570. return Error_NotExist;
  571. }
  572. }
  573. }
  574. } while (_findnext(Handle, &FileInfo) == 0);
  575. _findclose(Handle);
  576. return Error_Succeed;
  577. }
  578. CSimpleStringA GetFileDirectory(const char *pszFullPath)
  579. {
  580. int i=strlen(pszFullPath)-1;
  581. for( ; i>0 && pszFullPath[i]!='\\'; i--)NULL;
  582. return CSimpleStringA(pszFullPath, i);
  583. }
  584. ErrorCodeEnum SpEntityPrivilege::BeginLightInstall(const char *pszPackageName)
  585. {
  586. // 创建安装包日志文件
  587. WriteInstallLog(pszPackageName, CSimpleStringA::Format("Begin install light pack: [%s]", pszPackageName));
  588. CSimpleStringA strAdPath;
  589. auto rc = GetPath("Ad", strAdPath);
  590. assert(rc == Error_Succeed);
  591. CSimpleStringA strDownloadsPath;
  592. rc = GetPath("Downloads", strDownloadsPath);
  593. assert(rc == Error_Succeed);
  594. CSimpleStringA strUnzipPath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)pszPackageName);
  595. if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
  596. strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength()-4);
  597. auto strConfigPath = CSimpleStringA::Format("%s\\Run.ini", (const char*)strUnzipPath);
  598. // 检查文件是否存在
  599. DWORD attr = GetFileAttributesA(strConfigPath);
  600. assert((attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY));
  601. // ToCopy= Run\bin\SpShell.exe, Run\dep\CardIssuer.dll
  602. char *p = inifile_read_str(strConfigPath, "Action", "ToCopy", "");
  603. CSimpleStringA strToCopy = p;
  604. toolkit_free(p);
  605. //assert(!strToCopy.IsNullOrEmpty());
  606. if (strToCopy.IsNullOrEmpty())
  607. {
  608. LogError(Severity_Low, Error_Null, 0, "file to copy not define");
  609. return Error_Null;
  610. }
  611. auto list = strToCopy.Split(',');
  612. for(int i=0; i<list.GetCount(); i++)
  613. {
  614. auto &file = list[i];
  615. if (file.IsStartWith("Data\\"))
  616. {
  617. // 通配符检测
  618. if (file.IndexOf("*") >0)
  619. {
  620. CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)GetFileDirectory(file));
  621. CSimpleStringA StrDirToCopy = GetFileDirectory(file).SubString(5, file.GetLength()-5);
  622. CSimpleStringA strDestPath = "";
  623. if (StrDirToCopy.IsNullOrEmpty())
  624. {
  625. strDestPath = strAdPath;
  626. }
  627. else
  628. {
  629. strDestPath = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)GetFileDirectory(file).SubString(5, file.GetLength()-5));
  630. }
  631. rc = CopyFolder(strSourcePath, strDestPath);
  632. if (Error_Succeed != rc)
  633. {
  634. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("copy dir [%s] to dir [%s] fail", (const char*)strSourcePath, (const char*)strDestPath));
  635. }
  636. }
  637. else
  638. {
  639. CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)file);
  640. CSimpleStringA strDestPath = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)file.SubString(5, file.GetLength()-5));
  641. if (ExistsFileA(strSourcePath))
  642. {
  643. // 先去除目标文件只读属性
  644. if (ExistsFileA(strDestPath))
  645. RemoveFileReadOnlyAttributeA(strDestPath);
  646. LOG_TRACE("copy file from [%s] to [%s]", (const char*)strSourcePath, (const char*)strDestPath);
  647. BOOL bRet = CopyFileA(strSourcePath, strDestPath, FALSE);
  648. // 去除只读属性
  649. if (bRet)
  650. RemoveFileReadOnlyAttributeA(strDestPath);
  651. else
  652. {
  653. // 覆盖失败,则生成.new副本
  654. Dbg("copy file [%s] to [%s] fail, now save as .new file", (const char*) strSourcePath, (const char*) strDestPath);
  655. strDestPath += ".new";
  656. bRet = CopyFileA(strSourcePath, strDestPath, FALSE);
  657. if (bRet)
  658. {
  659. RemoveFileReadOnlyAttributeA(strDestPath);
  660. Dbg("copy [%s] as [%s] succeed", (const char*)strSourcePath, (const char*) strDestPath);
  661. }
  662. else
  663. {
  664. LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("copy file [%s] as [%s] fail", (const char*) strSourcePath, (const char*) strDestPath));
  665. return Error_Block;
  666. }
  667. }
  668. }
  669. else
  670. {
  671. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to copy not exists", (const char*)strSourcePath));
  672. return Error_NotExist;
  673. }
  674. }
  675. }
  676. else
  677. {
  678. LogError(Severity_Low, Error_NoPrivilege, 0, CSimpleStringA::Format("light pack [%s] has invalid copy item: [%s]",
  679. (const char*)pszPackageName, (const char*)file));
  680. return Error_NoPrivilege;
  681. }
  682. }
  683. // 登录到安装记录文件中
  684. sp_env_t *env = sp_get_env();
  685. auto cfg = env->cfg;
  686. CSimpleStringA strPackName = pszPackageName;
  687. CSimpleStringA strLightPacks = cfg->install_ini->light_packs;
  688. if (!strLightPacks.IsNullOrEmpty())
  689. strLightPacks += ",";
  690. strLightPacks += strPackName;
  691. inifile_write_str(cfg->install_ini_path, "Main", "LightPack", strLightPacks);
  692. shm_free(cfg->install_ini->light_packs);
  693. cfg->install_ini->light_packs = shm_strdup(strLightPacks);
  694. // 修改内存配置项
  695. sp_cfg_pack_info_t *pack_info = (sp_cfg_pack_info_t*)shm_malloc(sizeof(sp_cfg_pack_info_t));
  696. memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
  697. pack_info->name = shm_strdup(strPackName);
  698. pack_info->install_time = y2k_time_now();
  699. pack_info->state = Install_Installed;
  700. SHM_ARRAY_PUSH(cfg->install_ini->arr_light_pack, sp_cfg_pack_info_t*) = pack_info;
  701. inifile_format_write(cfg->install_ini_path, strPackName, "InstalledDate", "0x%08X", y2k_time_now());
  702. inifile_write_str(cfg->install_ini_path, strPackName, "PackState", "U");
  703. Dbg("install light pack [%s] succeed", (const char*)strPackName);
  704. return Error_Succeed;
  705. }
  706. //ErrorCodeEnum SpEntityPrivilege::BeginLightInstall(const char *pszPackageName)
  707. //{
  708. // // 创建安装包日志文件
  709. // WriteInstallLog(pszPackageName, CSimpleStringA::Format("Begin install light pack: [%s]", pszPackageName));
  710. //
  711. // CSimpleStringA strAdPath;
  712. // auto rc = GetPath("Ad", strAdPath);
  713. // assert(rc == Error_Succeed);
  714. //
  715. // CSimpleStringA strDownloadsPath;
  716. // rc = GetPath("Downloads", strDownloadsPath);
  717. // assert(rc == Error_Succeed);
  718. //
  719. // CSimpleStringA strUnzipPath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)pszPackageName);
  720. // if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
  721. // strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength()-4);
  722. //
  723. // auto strConfigPath = CSimpleStringA::Format("%s\\Run.ini", (const char*)strUnzipPath);
  724. //
  725. // // 检查文件是否存在
  726. // DWORD attr = GetFileAttributesA(strConfigPath);
  727. // assert((attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY));
  728. //
  729. // // ToCopy= Run\bin\SpShell.exe, Run\dep\CardIssuer.dll
  730. // char *p = inifile_read_str(strConfigPath, "Action", "ToCopy", "");
  731. // CSimpleStringA strToCopy = p;
  732. // toolkit_free(p);
  733. //
  734. // //assert(!strToCopy.IsNullOrEmpty());
  735. // if (strToCopy.IsNullOrEmpty())
  736. // {
  737. // LogError(Severity_Low, Error_Null, 0, "file to copy not define");
  738. // return Error_Null;
  739. // }
  740. //
  741. // auto list = strToCopy.Split(',');
  742. // for(int i=0; i<list.GetCount(); i++)
  743. // {
  744. // auto &file = list[i];
  745. // if (file.IsStartWith("Data\\"))
  746. // {
  747. // CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)file);
  748. // CSimpleStringA strDestPath = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)file.SubString(5, file.GetLength()-5));
  749. //
  750. // if (ExistsFileA(strSourcePath))
  751. // {
  752. // // 先去除目标文件只读属性
  753. // if (ExistsFileA(strDestPath))
  754. // RemoveFileReadOnlyAttributeA(strDestPath);
  755. //
  756. // LOG_TRACE("copy file from [%s] to [%s]", (const char*)strSourcePath, (const char*)strDestPath);
  757. // BOOL bRet = CopyFileA(strSourcePath, strDestPath, FALSE);
  758. //
  759. // // 去除只读属性
  760. // if (bRet)
  761. // RemoveFileReadOnlyAttributeA(strDestPath);
  762. // else
  763. // {
  764. // // 覆盖失败,则生成.new副本
  765. // Dbg("copy file [%s] to [%s] fail, now save as .new file", (const char*) strSourcePath, (const char*) strDestPath);
  766. //
  767. // strDestPath += ".new";
  768. // bRet = CopyFileA(strSourcePath, strDestPath, FALSE);
  769. // if (bRet)
  770. // {
  771. // RemoveFileReadOnlyAttributeA(strDestPath);
  772. // Dbg("copy [%s] as [%s] succeed", (const char*)strSourcePath, (const char*) strDestPath);
  773. // }
  774. // else
  775. // {
  776. // LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("copy file [%s] as [%s] fail", (const char*) strSourcePath, (const char*) strDestPath));
  777. // return Error_Block;
  778. // }
  779. // }
  780. // }
  781. // else
  782. // {
  783. // LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to copy not exists", (const char*)strSourcePath));
  784. // return Error_NotExist;
  785. // }
  786. // }
  787. // else
  788. // {
  789. // LogError(Severity_Low, Error_NoPrivilege, 0, CSimpleStringA::Format("light pack [%s] has invalid copy item: [%s]",
  790. // (const char*)pszPackageName, (const char*)file));
  791. // return Error_NoPrivilege;
  792. // }
  793. // }
  794. //
  795. // // 登录到安装记录文件中
  796. // sp_env_t *env = sp_get_env();
  797. // auto cfg = env->cfg;
  798. //
  799. // CSimpleStringA strPackName = pszPackageName;
  800. //
  801. // CSimpleStringA strLightPacks = cfg->install_ini->light_packs;
  802. // if (!strLightPacks.IsNullOrEmpty())
  803. // strLightPacks += ",";
  804. //
  805. // strLightPacks += strPackName;
  806. // inifile_write_str(cfg->install_ini_path, "Main", "LightPack", strLightPacks);
  807. //
  808. // shm_free(cfg->install_ini->light_packs);
  809. // cfg->install_ini->light_packs = shm_strdup(strLightPacks);
  810. //
  811. // // 修改内存配置项
  812. // sp_cfg_pack_info_t *pack_info = (sp_cfg_pack_info_t*)shm_malloc(sizeof(sp_cfg_pack_info_t));
  813. // memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
  814. // pack_info->name = shm_strdup(strPackName);
  815. // pack_info->install_time = y2k_time_now();
  816. // pack_info->state = Install_Installed;
  817. // SHM_ARRAY_PUSH(cfg->install_ini->arr_light_pack, sp_cfg_pack_info_t*) = pack_info;
  818. //
  819. // inifile_format_write(cfg->install_ini_path, strPackName, "InstalledDate", "0x%08X", y2k_time_now());
  820. // inifile_write_str(cfg->install_ini_path, strPackName, "PackState", "U");
  821. //
  822. // Dbg("install light pack [%s] succeed", (const char*)strPackName);
  823. //
  824. // return Error_Succeed;
  825. //}
  826. bool SpEntityPrivilege::IsWow64Process()
  827. {
  828. typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
  829. LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
  830. BOOL bIsWow64 = FALSE;
  831. if (NULL != fnIsWow64Process)
  832. {
  833. if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
  834. {
  835. Dbg("call IsWow64Process error: %d", GetLastError());
  836. return false;
  837. }
  838. return bIsWow64 == TRUE;
  839. }
  840. else
  841. {
  842. Dbg("get IsWow64Process error: %d", GetLastError());
  843. return false;
  844. }
  845. }
  846. // result=0&msg=install ok
  847. bool SpEntityPrivilege::GetSysUpgradeResult(const char *pszResultLog, ErrorCodeEnum &eErrorCode, CSimpleStringA &strErrMsg)
  848. {
  849. // 设置初始值
  850. eErrorCode = Error_Succeed;
  851. strErrMsg = "";
  852. if (!ExistsFileA(pszResultLog))
  853. {
  854. Dbg(CSimpleStringA::Format("result log [%s] not exist", pszResultLog));
  855. strErrMsg = "result.log not exist";
  856. return false;
  857. }
  858. FILE *pResultLog = fopen(pszResultLog, "r");
  859. if (pResultLog == NULL)
  860. {
  861. LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("open result log [%s] fail", pszResultLog));
  862. strErrMsg = "open result.log fail";
  863. return false;
  864. }
  865. char szTmp[4096] = {};
  866. int nRead = fread_s(szTmp, sizeof(szTmp), 1, sizeof(szTmp), pResultLog);
  867. CSimpleStringA strResult = szTmp;
  868. fclose(pResultLog);
  869. pResultLog = NULL;
  870. auto arr = strResult.Split('&');
  871. for (int i = 0; i < arr.GetCount(); i++)
  872. {
  873. auto arr2 = arr[i].Split('=');
  874. if (arr2.GetCount() == 2)
  875. {
  876. if (stricmp(arr2[0], "result") == 0)
  877. eErrorCode = (ErrorCodeEnum)(DWORD)atoi(arr2[1]);
  878. else if (stricmp(arr2[0], "msg") == 0)
  879. strErrMsg = arr2[1];
  880. }
  881. }
  882. return true;
  883. }
  884. // 执行系统外升级
  885. ErrorCodeEnum SpEntityPrivilege::BeginSysPackInstall(const char *pszPackageName, CSimpleStringA &strErrMsg)
  886. {
  887. // 创建安装包日志文件
  888. strErrMsg = "";
  889. WriteInstallLog(pszPackageName, CSimpleStringA::Format("begin install os pack: [%s]", pszPackageName));
  890. CSimpleStringA strDownloadsPath;
  891. auto rc = GetPath("Downloads", strDownloadsPath);
  892. assert(rc == Error_Succeed);
  893. CSimpleStringA strUnzipPath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)pszPackageName);
  894. if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
  895. strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength() - 4);
  896. auto strConfigPath = CSimpleStringA::Format("%s\\Run.ini", (const char*)strUnzipPath);
  897. // 检查文件是否存在
  898. DWORD attr = GetFileAttributesA(strConfigPath);
  899. assert((attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY));
  900. // ToRun=install.bat
  901. char *p = inifile_read_str(strConfigPath, "Action", "ToRun", "");
  902. CSimpleStringA strToRun = p;
  903. toolkit_free(p);
  904. //assert(!strToCopy.IsNullOrEmpty());
  905. if (strToRun.IsNullOrEmpty())
  906. {
  907. strErrMsg = "未定义执行文件[ToRun]";
  908. LogError(Severity_Low, Error_Null, 0, "file to run not define");
  909. return Error_Null;
  910. }
  911. ErrorCodeEnum eErrorCode = Error_Succeed;
  912. auto list = strToRun.Split(',');
  913. for (int i = 0; i < list.GetCount(); i++)
  914. {
  915. auto &file = list[i];
  916. CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)file);
  917. if (ExistsFileA(strSourcePath))
  918. {
  919. // 如果是Wow64模式,需屏蔽System32重定向
  920. PVOID pRedirectOldValue(NULL);
  921. bool bIsWow64 = IsWow64Process();
  922. if (bIsWow64)
  923. {
  924. if (!Wow64DisableWow64FsRedirection(&pRedirectOldValue))
  925. Dbg("Wow64DisableWow64FsRedirection fail: %d", GetLastError());
  926. else
  927. Dbg("Wow64DisableWow64FsRedirection succ");
  928. }
  929. // xkm@20161114: 执行完成,从result.txt中读取安装结果
  930. system(strSourcePath);
  931. CSimpleStringA strResultLog = strUnzipPath + "\\result.log";
  932. if (!GetSysUpgradeResult(strResultLog, eErrorCode, strErrMsg))
  933. {
  934. Dbg("get upgrade result from result.log fail, %s", (const char*)strErrMsg);
  935. eErrorCode = Error_Succeed;
  936. strErrMsg = "";
  937. }
  938. // 如果是Wow64模式,恢复System32重定向
  939. if (bIsWow64)
  940. {
  941. if (!Wow64RevertWow64FsRedirection(pRedirectOldValue))
  942. Dbg("Wow64RevertWow64FsRedirection fail: %d", GetLastError());
  943. else
  944. Dbg("Wow64RevertWow64FsRedirection succ");
  945. }
  946. if (eErrorCode != Error_Succeed)
  947. {
  948. LogError(Severity_Low, Error_UpdateFailed, 0, CSimpleStringA::Format("run file [%s] error: %s(%d)", (const char*)file, (const char*)strErrMsg, eErrorCode));
  949. WriteInstallLog(pszPackageName, CSimpleStringA::Format("run file [%s] error: %s(%d)", (const char*)file, (const char*)strErrMsg, eErrorCode));
  950. return eErrorCode;
  951. }
  952. }
  953. else
  954. {
  955. strErrMsg = "指定执行文件不存在";
  956. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to run not exists", (const char*)strSourcePath));
  957. WriteInstallLog(pszPackageName, CSimpleStringA::Format("file [%s] to run not exists", (const char*)strSourcePath));
  958. return Error_NotExist;
  959. }
  960. }
  961. WriteInstallLog(pszPackageName, "install sys pack succ");
  962. // 登录到安装记录文件中
  963. sp_env_t *env = sp_get_env();
  964. auto cfg = env->cfg;
  965. CSimpleStringA strPackName = pszPackageName;
  966. char *pszSysPacks = inifile_read_str(cfg->install_ini_path, "Main", "SysPack", "");
  967. CSimpleStringA strSysPacks = pszSysPacks;
  968. toolkit_free(pszSysPacks);
  969. if (!strSysPacks.IsNullOrEmpty())
  970. strSysPacks += ",";
  971. strSysPacks += pszPackageName;
  972. inifile_write_str(cfg->install_ini_path, "Main", "SysPack", strSysPacks);
  973. inifile_format_write(cfg->install_ini_path, strPackName, "InstalledDate", "0x%08X", y2k_time_now());
  974. inifile_write_str(cfg->install_ini_path, strPackName, "PackState", "U");
  975. Dbg("install sys pack [%s] succ", (const char*)strPackName);
  976. return Error_Succeed;
  977. }
  978. // 判断是否是已安装的系统外升级包
  979. ErrorCodeEnum SpEntityPrivilege::IsInstalledSysPack(const char *pszPackageName)
  980. {
  981. // 登录到安装记录文件中
  982. sp_env_t *env = sp_get_env();
  983. auto cfg = env->cfg;
  984. CSimpleStringA strPackName = pszPackageName;
  985. // 查询系统外升级包
  986. char *p = inifile_read_str(cfg->install_ini_path, "Main", "SysPack", "");
  987. CSimpleStringA strSysPackList = p;
  988. toolkit_free(p);
  989. if (!strSysPackList.IsNullOrEmpty() && (strSysPackList.IndexOf(strPackName)!= -1))
  990. {
  991. return Error_Succeed;
  992. }
  993. return Error_Unexpect;
  994. }
  995. ErrorCodeEnum SpEntityPrivilege::CopyFileToNewVersion(const char *pszRelativeFile, int nCopyMode)
  996. {
  997. auto env = sp_get_env();
  998. auto cfg = env->cfg;
  999. // 取到新版本安装包名称
  1000. char *p = inifile_read_str(cfg->install_ini_path, "Main", "LatterInstallVersion", "");
  1001. CSimpleStringA strNewVer = p;
  1002. toolkit_free(p);
  1003. assert(!strNewVer.IsNullOrEmpty());
  1004. CSimpleStringA strNewInstallIni = CSimpleStringA::Format("%s\\%s\\install.ini", env->dir->root_ver_path, (const char*)strNewVer);
  1005. assert(ExistsFileA(strNewInstallIni));
  1006. p = inifile_read_str(strNewInstallIni, strNewVer, "InstallPack", "");
  1007. CSimpleStringA strPackName = p;
  1008. toolkit_free(p);
  1009. assert(!strPackName.IsNullOrEmpty());
  1010. // 取得临时解压目录
  1011. CSimpleStringA strDownloadsPath;
  1012. auto rc = GetPath("Downloads", strDownloadsPath);
  1013. assert(rc == Error_Succeed);
  1014. CSimpleStringA strUnzipPath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)strPackName);
  1015. if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
  1016. strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength()-4);
  1017. CSimpleStringA strCopyPath = pszRelativeFile;
  1018. CSimpleStringA strSourceFile;
  1019. CSimpleStringA strDestFile;
  1020. if (strCopyPath.IsStartWith("Data\\"))
  1021. {
  1022. CSimpleStringA strAdPath;
  1023. rc = GetPath("Ad", strAdPath);
  1024. assert(rc == Error_Succeed);
  1025. strSourceFile = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)strCopyPath);
  1026. strDestFile = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)strCopyPath.SubString(5, strCopyPath.GetLength()-5));
  1027. }
  1028. else if (strCopyPath.IsStartWith("Run\\"))
  1029. {
  1030. strSourceFile = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)strCopyPath);
  1031. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strNewVer);
  1032. strDestFile = CSimpleStringA::Format("%s\\%s",(const char*) strNewVerPath, (const char*)strCopyPath.SubString(4, strCopyPath.GetLength()-4));
  1033. }
  1034. else
  1035. {
  1036. WriteInstallLog(strPackName, CSimpleStringA::Format("Copy file: [%s], result: [0x%X]", pszRelativeFile, Error_NoPrivilege));
  1037. LogError(Severity_Low, Error_NoPrivilege, 0, CSimpleStringA::Format("invalid copy item: [%s], must start with \"Data\" or \"Run\"", (const char*)pszRelativeFile));
  1038. return Error_NoPrivilege;
  1039. }
  1040. //assert(ExistsFileA(strSourceFile));
  1041. if (!ExistsFileA(strSourceFile))
  1042. {
  1043. WriteInstallLog(strPackName, CSimpleStringA::Format("Copy file: [%s], result: [0x%X]", pszRelativeFile, Error_NotExist));
  1044. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to copy not exist", (const char*)strSourceFile));
  1045. return Error_NotExist;
  1046. }
  1047. //#ifdef _DEBUG
  1048. // bool bVerifySign = false;
  1049. //#else
  1050. // bool bVerifySign = true;
  1051. //#endif
  1052. //
  1053. // if (bVerifySign && (strSourceFile.IsEndWith(".dll", true) || strSourceFile.IsEndWith(".exe", true)))
  1054. // {
  1055. // CSimpleStringA strErrMsg;
  1056. // if (VerifySignature(strSourceFile, strErrMsg) != Error_Succeed)
  1057. // {
  1058. // WriteInstallLog(strPackName, CSimpleStringA::Format("file [%s] sign verify not pass: %s", pszRelativeFile, (const char*)strErrMsg));
  1059. // LogError(Severity_Low, Error_FailVerify, 0, CSimpleStringA::Format("file [%s] sign verify not pass: %s", (const char*)strSourceFile, (const char*)strErrMsg));
  1060. // return Error_FailVerify;
  1061. // }
  1062. // }
  1063. // check dest dir if exists
  1064. auto strDestDir = GetFileDirectory(strDestFile);
  1065. if (!ExistsDirA(strDestDir))
  1066. CreateDirA(strDestDir, TRUE);
  1067. BOOL bDestFileExists = ExistsFileA(strDestFile);
  1068. if (bDestFileExists)
  1069. RemoveFileReadOnlyAttributeA(strDestFile);
  1070. BOOL bRet = TRUE;
  1071. // 1(copy only exisit); 2(copy only not exist); 3(copy always); 4(no copy if exist)
  1072. switch (nCopyMode)
  1073. {
  1074. case 1:
  1075. {
  1076. if (bDestFileExists)
  1077. bRet= CopyFileA(strSourceFile, strDestFile, FALSE);
  1078. else
  1079. LOG_TRACE("ignore file [%s] copy for dest file not exists", pszRelativeFile);
  1080. }
  1081. break;
  1082. case 2:
  1083. {
  1084. if (!bDestFileExists)
  1085. bRet= CopyFileA(strSourceFile, strDestFile, TRUE);
  1086. else
  1087. LOG_TRACE("ignore file [%s] copy for dest file exists", pszRelativeFile);
  1088. }
  1089. break;
  1090. case 3:
  1091. bRet= CopyFileA(strSourceFile, strDestFile, FALSE);
  1092. break;
  1093. case 4:
  1094. {
  1095. // 不发生拷贝,只进行判断
  1096. if(bDestFileExists)
  1097. LOG_TRACE("file [%s] exist", pszRelativeFile);
  1098. else
  1099. LOG_TRACE("file [%s] not exist", pszRelativeFile);
  1100. }
  1101. break;
  1102. }
  1103. if (bRet)
  1104. RemoveFileReadOnlyAttributeA(strDestFile);
  1105. rc = bRet ? Error_Succeed : Error_Unexpect;
  1106. WriteInstallLog(strPackName, CSimpleStringA::Format("Copy file: [%s], result: [0x%X]", pszRelativeFile, rc));
  1107. return rc;
  1108. }
  1109. // File*A.*
  1110. bool SpEntityPrivilege::IsFileMatch(const char *pszFilter, const char *pszFileName)
  1111. {
  1112. if (stricmp(pszFilter, "*") == 0 || stricmp(pszFilter, "*.*") == 0)
  1113. return true;
  1114. const char *p1 = pszFilter;
  1115. const char *p2 = pszFileName;
  1116. if (p1 == NULL)
  1117. return true;
  1118. if (p2 == NULL)
  1119. return false;
  1120. if (*p1 ==NULL && *p2 ==NULL)
  1121. return true;
  1122. else if (*p1 == NULL)
  1123. return false;
  1124. else if (*p2 == NULL)
  1125. {
  1126. // 查找*p1是否全是*
  1127. while(*p1 == '*')
  1128. p1++;
  1129. if (*p1 == NULL)
  1130. return true;
  1131. else
  1132. return false;
  1133. }
  1134. if (*p1 != '*')
  1135. {
  1136. if (tolower(*p1) != tolower(*p2))
  1137. return false;
  1138. else
  1139. return IsFileMatch(p1+1, p2+1);
  1140. }
  1141. else
  1142. {
  1143. while(*++p1 == '*');
  1144. if (*p1 == NULL)
  1145. return true;
  1146. while (*p2)
  1147. {
  1148. while(tolower(*p1) != tolower(*p2) && *++p2);
  1149. if (*p2 == NULL)
  1150. return false;
  1151. if(IsFileMatch(p1+1, p2+1))
  1152. return true;
  1153. p2++;
  1154. }
  1155. return false;
  1156. }
  1157. }
  1158. CSimpleStringA SpEntityPrivilege::GetFileDirectory(const char *pszFullPath)
  1159. {
  1160. int i=strlen(pszFullPath);
  1161. for( ; i>0 && pszFullPath[i-1]!='\\'; i--)NULL;
  1162. return CSimpleStringA(pszFullPath, i);
  1163. }
  1164. ErrorCodeEnum SpEntityPrivilege::DeleteFileInNewVersion(const char *pszRelativeFile)
  1165. {
  1166. auto env = sp_get_env();
  1167. auto cfg = env->cfg;
  1168. // 取到新版本安装包名称
  1169. char *p =inifile_read_str(cfg->install_ini_path, "Main", "LatterInstallVersion", "");
  1170. CSimpleStringA strNewVer = p;
  1171. toolkit_free(p);
  1172. CSimpleStringA strNewInstallIni = CSimpleStringA::Format("%s\\%s\\install.ini", env->dir->root_ver_path, (const char*)strNewVer);
  1173. assert(ExistsFileA(strNewInstallIni));
  1174. p = inifile_read_str(strNewInstallIni, strNewVer, "InstallPack", "");
  1175. CSimpleStringA strPackName = p;
  1176. toolkit_free(p);
  1177. assert(!strPackName.IsNullOrEmpty());
  1178. CSimpleStringA strDelFile = pszRelativeFile;
  1179. CSimpleStringA strDestFile;
  1180. if (strDelFile.IsStartWith("Data\\"))
  1181. {
  1182. CSimpleStringA strAdPath;
  1183. auto rc = GetPath("Ad", strAdPath);
  1184. assert(rc == Error_Succeed);
  1185. strDestFile = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)strDelFile.SubString(5, strDelFile.GetLength()-5));
  1186. }
  1187. else if (strDelFile.IsStartWith("Run\\"))
  1188. {
  1189. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strNewVer);
  1190. assert(ExistsDirA(strNewVerPath));
  1191. strDestFile = CSimpleStringA::Format("%s\\%s", (const char*)strNewVerPath, (const char*)strDelFile.SubString(4, strDelFile.GetLength()-4));
  1192. }
  1193. else
  1194. {
  1195. WriteInstallLog(strPackName, CSimpleStringA::Format("Delete file: [%s], result: [%d]", pszRelativeFile, Error_NoPrivilege));
  1196. LogError(Severity_Low, Error_NoPrivilege, 0, CSimpleStringA::Format("invalid delete item: [%s], must start with \"Data\" or \"Run\"", (const char*)pszRelativeFile));
  1197. return Error_NoPrivilege;
  1198. }
  1199. // 解析文件通配符*
  1200. BOOL bRet = TRUE;
  1201. int nIndex = strDestFile.IndexOf("*");
  1202. if (nIndex > 0)
  1203. {
  1204. CSimpleStringA strFileFilter = _GetFileName(strDestFile);
  1205. CSimpleStringA strDestDir = strDestFile.SubString(0, strDestFile.GetLength()-strFileFilter.GetLength()-1);
  1206. Dbg("delete wildchar file [%s] in dir [%s]", (const char*)strFileFilter, (const char*)strDestDir);
  1207. // 删除所有匹配文件
  1208. auto arr = fileutil_get_sub_files(strDestDir);
  1209. if (arr != NULL)
  1210. {
  1211. for (int i = 0; i < arr->nelts; ++i)
  1212. {
  1213. char *file = ARRAY_IDX(arr, i, char*);
  1214. if (IsFileMatch(strFileFilter, _GetFileName(file)))
  1215. {
  1216. Dbg("delete match file: [%s]", file);
  1217. bRet &= RemoveFileA(file);
  1218. }
  1219. else
  1220. {
  1221. Dbg("ignore not match file: [%s]", file);
  1222. }
  1223. }
  1224. toolkit_array_free2(arr);
  1225. }
  1226. // xkm@20170302: 删除所有匹配子目录
  1227. auto subdirs = fileutil_get_sub_dirs(strDestDir);
  1228. if (subdirs != NULL)
  1229. {
  1230. for (int i = 0; i < subdirs->nelts; ++i)
  1231. {
  1232. const char *dir = ARRAY_IDX(subdirs, i, char*);
  1233. if (IsFileMatch(strFileFilter, _GetFileName(dir)))
  1234. {
  1235. Dbg("delete match dir: [%s]", dir);
  1236. bRet &= RemoveDirRecursive(dir);
  1237. }
  1238. else
  1239. {
  1240. Dbg("ignore not match dir: [%s]", dir);
  1241. }
  1242. }
  1243. toolkit_array_free2(subdirs);
  1244. }
  1245. }
  1246. else
  1247. {
  1248. if (ExistsFileA(strDestFile))
  1249. {
  1250. bRet = DeleteFileA(strDestFile);
  1251. Dbg("delete file [%s] result: %d", strDestFile, bRet);
  1252. }
  1253. else if (ExistsDirA(strDestFile))
  1254. {
  1255. bRet = RemoveDirRecursive(strDestFile);
  1256. Dbg("delete dir [%s] result: %d", strDestFile, bRet);
  1257. }
  1258. else
  1259. {
  1260. Dbg("delete file [%s] fail, not exists", pszRelativeFile);
  1261. }
  1262. }
  1263. auto rc = bRet ? Error_Succeed : Error_Unexpect;
  1264. WriteInstallLog(strPackName, CSimpleStringA::Format("Delete file: [%s], result: [%d]", pszRelativeFile, rc));
  1265. return rc;
  1266. }
  1267. ErrorCodeEnum SpEntityPrivilege::RollBackToPreviousVersion()
  1268. {
  1269. auto env = sp_get_env();
  1270. auto cfg = env->cfg;
  1271. // 修改当前Install.ini配置
  1272. auto &v = cfg->install_ini->install_version;
  1273. CVersion curVersion(v.major, v.minor, v.revision, v.build);
  1274. CInstallInfo info = {};
  1275. auto rc = GetInstallInfo(curVersion, info);
  1276. assert(rc == Error_Succeed);
  1277. if (!info.PreviousInstallVersion.IsValid())
  1278. {
  1279. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("current version [%s] has no previous version", (const char*)curVersion.ToString()));
  1280. return Error_NotExist;
  1281. }
  1282. auto strPrevVersion = info.PreviousInstallVersion.ToString();
  1283. // 检查对应文件夹是否存在
  1284. CSimpleStringA strPreVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strPrevVersion);
  1285. if (!ExistsDirA(strPreVerPath))
  1286. {
  1287. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("previous version dir [%s] not exists", (const char*)strPreVerPath));
  1288. return Error_NotExist;
  1289. }
  1290. // 修改当前版本状态为倒回R
  1291. int nRet = inifile_write_str(cfg->install_ini_path, curVersion.ToString(), "InstallState", "R");
  1292. // 更新回退次数
  1293. auto nTotalRollbackCount = inifile_read_int(cfg->install_ini_path, curVersion.ToString(), "TotalRollbackCount", 0);
  1294. nTotalRollbackCount++;
  1295. nRet = inifile_write_int(cfg->install_ini_path, curVersion.ToString(), "TotalRollbackCount", nTotalRollbackCount);
  1296. if (-1 == nRet)
  1297. {
  1298. LogError(Severity_Low, Error_Unexpect, 0, "inifile_write_int TotalRollbackCount");
  1299. }
  1300. LOG_TRACE("inifile_write_int, nTotalRollbackCount[%d]", nTotalRollbackCount);
  1301. // 修改active.txt文件
  1302. CSimpleStringA strActiveFile = CSimpleStringA::Format("%s\\active.txt", env->dir->root_ver_path);
  1303. HANDLE hFile = ::CreateFileA(strActiveFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1304. BOOL bSetSucc = FALSE;
  1305. if (hFile == INVALID_HANDLE_VALUE)
  1306. {
  1307. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  1308. return Error_Unexpect;
  1309. }
  1310. DWORD dwWrittenLen(0);
  1311. bSetSucc = WriteFile(hFile, (const char*)strPrevVersion, strPrevVersion.GetLength(), &dwWrittenLen, NULL);
  1312. FlushFileBuffers(hFile);
  1313. SetEndOfFile(hFile);
  1314. CloseHandle(hFile);
  1315. LOG_TRACE("set rollback to previous version [%s] succeed", (const char*)strPrevVersion);
  1316. //出现active文件为空的问题,此处增加写文件件后再读一次,确认是否写成功,以此来确认是否是写文件导致
  1317. if (1)
  1318. {
  1319. hFile = ::CreateFileA(strActiveFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1320. if (hFile == INVALID_HANDLE_VALUE)
  1321. {
  1322. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  1323. return Error_Unexpect;
  1324. }
  1325. DWORD dwReadLen(0);
  1326. char cVer[16] = { 0 };
  1327. if (ReadFile(hFile, cVer, 16, &dwReadLen, NULL))
  1328. {
  1329. LOG_TRACE("read active.txt, version len[%d], version[%s]", dwReadLen, cVer);
  1330. //如果为空,则重写
  1331. if (strlen(cVer) == 0)
  1332. {
  1333. if (WriteFile(hFile, (const char*)strPrevVersion, strPrevVersion.GetLength(), &dwWrittenLen, NULL))
  1334. {
  1335. LOG_TRACE("rewrite active.txt success, strPrevVersion[%s]", strPrevVersion);
  1336. }
  1337. else
  1338. {
  1339. LogError(Severity_Low, Error_Unexpect, 0, "rewrite active.txt fail");
  1340. }
  1341. }
  1342. }
  1343. else
  1344. {
  1345. LogError(Severity_Low, Error_Unexpect, 0, "Read active.txt fail");
  1346. }
  1347. FlushFileBuffers(hFile);
  1348. SetEndOfFile(hFile);
  1349. CloseHandle(hFile);
  1350. }
  1351. return bSetSucc ? Error_Succeed : Error_Unexpect;
  1352. }
  1353. ErrorCodeEnum SpEntityPrivilege::RollBackToHistoryVersion(CVersion historyVersion)
  1354. {
  1355. auto env = sp_get_env();
  1356. auto cfg = env->cfg;
  1357. // 判断目标版本是否小于当前版本
  1358. auto &v = cfg->install_ini->install_version;
  1359. CVersion curVersion(v.major, v.minor, v.revision, v.build);
  1360. if (curVersion <= historyVersion)
  1361. {
  1362. LogError(Severity_Low, Error_Param, 0, CSimpleStringA::Format("history version [%s] >= current version [%s]",
  1363. (const char*)historyVersion.ToString(), (const char*)curVersion.ToString()));
  1364. return Error_Param;
  1365. }
  1366. // 修改当前Install.ini配置
  1367. CInstallInfo info= {};
  1368. auto rc = GetInstallInfo(historyVersion, info);
  1369. if (rc != Error_Succeed)
  1370. {
  1371. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("history version [%s] to rollback not exist", (const char*)historyVersion.ToString()));
  1372. return Error_NotExist;
  1373. }
  1374. // 检查对应文件夹是否存在
  1375. CSimpleStringA strHisVersion = historyVersion.ToString();
  1376. CSimpleStringA strHisVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strHisVersion);
  1377. if (!ExistsDirA(strHisVerPath))
  1378. {
  1379. LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("history version dir [%s] not exists", (const char*)strHisVerPath));
  1380. return Error_NotExist;
  1381. }
  1382. // 修改当前版本状态为倒回R
  1383. int nRet = inifile_write_str(cfg->install_ini_path, curVersion.ToString(), "InstallState", "R");
  1384. // 更新回退次数
  1385. auto nTotalRollbackCount = inifile_read_int(cfg->install_ini_path, curVersion.ToString(), "TotalRollbackCount", 0);
  1386. nTotalRollbackCount++;
  1387. nRet = inifile_write_int(cfg->install_ini_path, curVersion.ToString(), "TotalRollbackCount", nTotalRollbackCount);
  1388. if (-1 == nRet)
  1389. {
  1390. LogError(Severity_Low, Error_Unexpect, 0, "inifile_write_int TotalRollbackCount");
  1391. }
  1392. LOG_TRACE("inifile_write_int, nTotalRollbackCount[%d]", nTotalRollbackCount);
  1393. // 修改active.txt文件
  1394. CSimpleStringA strActiveFile = CSimpleStringA::Format("%s\\active.txt", env->dir->root_ver_path);
  1395. HANDLE hFile = ::CreateFileA(strActiveFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1396. BOOL bSetSucc = FALSE;
  1397. if (hFile == INVALID_HANDLE_VALUE)
  1398. {
  1399. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  1400. return Error_Unexpect;
  1401. }
  1402. DWORD dwWrittenLen(0);
  1403. bSetSucc = WriteFile(hFile, (const char*)strHisVersion, strHisVersion.GetLength(), &dwWrittenLen, NULL);
  1404. FlushFileBuffers(hFile);
  1405. SetEndOfFile(hFile);
  1406. CloseHandle(hFile);
  1407. //出现active文件为空的问题,此处增加写文件件后再读一次,确认是否写成功,以此来确认是否是写文件导致
  1408. if (1)
  1409. {
  1410. hFile = ::CreateFileA(strActiveFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1411. if (hFile == INVALID_HANDLE_VALUE)
  1412. {
  1413. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  1414. return Error_Unexpect;
  1415. }
  1416. DWORD dwReadLen(0);
  1417. char cVer[16] = {0};
  1418. if (ReadFile(hFile,cVer,16,&dwReadLen,NULL))
  1419. {
  1420. LOG_TRACE("read active.txt, version len[%d], version[%s]", dwReadLen, cVer);
  1421. }
  1422. else
  1423. {
  1424. LogError(Severity_Low, Error_Unexpect, 0, "Read active.txt fail");
  1425. }
  1426. CloseHandle(hFile);
  1427. }
  1428. LOG_TRACE("set rollback to history version [%s] succeed", (const char*)strHisVersion);
  1429. return bSetSucc ? Error_Succeed : Error_Unexpect;
  1430. }
  1431. ErrorCodeEnum SpEntityPrivilege::UpgradeToNewVersion(bool bToSysInstall)
  1432. {
  1433. auto env = sp_get_env();
  1434. auto cfg = env->cfg;
  1435. // 修改当前Install.ini配置
  1436. auto &v = cfg->install_ini->install_version;
  1437. CVersion curVersion(v.major, v.minor, v.revision, v.build);
  1438. int nRet = inifile_write_str(cfg->install_ini_path, curVersion.ToString(), "InstallState", "U"); // 已升级
  1439. // 取到新版本
  1440. char *p = inifile_read_str(cfg->install_ini_path, "Main", "LatterInstallVersion", "");
  1441. CSimpleStringA strNewVer = p;
  1442. toolkit_free(p);
  1443. assert(!strNewVer.IsNullOrEmpty());
  1444. if (strNewVer.IsNullOrEmpty())
  1445. {
  1446. LogError(Severity_Low, Error_Unexpect, 0, "install.ini Main LatterInstallVersion is null");
  1447. return Error_Unexpect;
  1448. }
  1449. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strNewVer);
  1450. // 修改新版本install.ini
  1451. auto strNewInstallIni = CSimpleStringA::Format("%s\\install.ini", (const char*)strNewVerPath);
  1452. assert(ExistsFileA(strNewInstallIni));
  1453. // 取新版本安装包名称
  1454. p = inifile_read_str(strNewInstallIni, strNewVer, "InstallPack", "");
  1455. CSimpleStringA strPackName = p;
  1456. toolkit_free(p);
  1457. assert(!strPackName.IsNullOrEmpty());
  1458. WriteInstallLog(strPackName, "Upgrade to new version");
  1459. nRet &= inifile_write_str(strNewInstallIni, curVersion.ToString(), "InstallState", "U"); // 已升级
  1460. nRet &= inifile_write_str(strNewInstallIni, strNewVer, "InstallState", "S"); // 设置启动
  1461. // 修改active.txt配置
  1462. CSimpleStringA strActiveFile = CSimpleStringA::Format("%s\\active.txt", env->dir->root_ver_path);
  1463. HANDLE hFile = ::CreateFileA(strActiveFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1464. BOOL bSetSucc = (nRet == 0);
  1465. if (hFile != INVALID_HANDLE_VALUE)
  1466. {
  1467. DWORD dwWrittenLen(0);
  1468. bSetSucc = bSetSucc && WriteFile(hFile, (const char*)strNewVer, strNewVer.GetLength(), &dwWrittenLen, NULL);
  1469. FlushFileBuffers(hFile);
  1470. SetEndOfFile(hFile);
  1471. CloseHandle(hFile);
  1472. LOG_TRACE("WriteFile [%s] to active.txt", (const char*)strNewVer);
  1473. }
  1474. else
  1475. {
  1476. bSetSucc = FALSE;
  1477. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  1478. }
  1479. //出现active文件为空的问题,此处增加写文件件后再读一次,确认是否写成功,以此来确认是否是写文件导致
  1480. if (1)
  1481. {
  1482. hFile = ::CreateFileA(strActiveFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1483. if (hFile == INVALID_HANDLE_VALUE)
  1484. {
  1485. LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
  1486. return Error_Unexpect;
  1487. }
  1488. DWORD dwReadLen(0);
  1489. char cVer[16] = {0};
  1490. if (ReadFile(hFile,cVer,16,&dwReadLen,NULL))
  1491. {
  1492. LOG_TRACE("read active.txt, version len[%d], version[%s]", dwReadLen, cVer);
  1493. }
  1494. else
  1495. {
  1496. LogError(Severity_Low, Error_Unexpect, 0, "Read active.txt fail");
  1497. }
  1498. CloseHandle(hFile);
  1499. }
  1500. // 修改内存配置
  1501. if (bSetSucc)
  1502. {
  1503. sp_version_t newVersion = {};
  1504. sscanf((const char*)strNewVer, "%d.%d.%d.%d", &newVersion.major, &newVersion.minor, &newVersion.revision, &newVersion.build);
  1505. auto newVerInfo = sp_cfg_find_version_info(cfg, &newVersion);
  1506. if (newVerInfo != NULL)
  1507. newVerInfo->install_state = Install_SetToStart;
  1508. LOG_TRACE("set new version [%s] active", (const char*)strNewVer);
  1509. return Error_Succeed;
  1510. }
  1511. else
  1512. {
  1513. LogError(Severity_Low, Error_Unexpect, 0, "switch to new version fail");
  1514. }
  1515. return Error_Unexpect;
  1516. }
  1517. ErrorCodeEnum SpEntityPrivilege::SetRunSucceed()
  1518. {
  1519. sp_env_t *env = sp_get_env();
  1520. auto cfg = env->cfg;
  1521. // 修改内存状态
  1522. auto pVerInfo = sp_cfg_find_version_info(cfg, &cfg->install_ini->install_version);
  1523. pVerInfo->install_state = Install_Active; // 安装成功
  1524. // 设置后一版本状态为倒回
  1525. if (sp_version_is_valid(&cfg->install_ini->latter_install_version))
  1526. {
  1527. // 修改后一版本安装文件状态为倒回
  1528. auto &latterVer = cfg->install_ini->latter_install_version;
  1529. char tmp[MAX_PATH];
  1530. auto rc = sp_dir_get_path_version(env->dir, latterVer.major, latterVer.minor, latterVer.revision, latterVer.build, SP_DIR_INSTALL_INI,
  1531. NULL, tmp, MAX_PATH);
  1532. if (rc != 0)
  1533. {
  1534. sp_dbg_warn("get install.ini path failed!");
  1535. return (ErrorCodeEnum)rc;
  1536. }
  1537. if (ExistsFileA(tmp)) // 可能后续版本目录被删除
  1538. {
  1539. // 倒回状态
  1540. CVersion lVer(latterVer.major, latterVer.minor, latterVer.revision, latterVer.build);
  1541. inifile_write_str(tmp, (const char*)lVer.ToString(), "InstallState", "R");
  1542. // 修改内存状态
  1543. auto pLatterVerInfo = sp_cfg_find_version_info(cfg, &latterVer);
  1544. if (pLatterVerInfo != NULL)
  1545. pLatterVerInfo->install_state = Install_RollBack;
  1546. // 修改对应安装包日志
  1547. auto p = inifile_read_str(tmp, (const char*)lVer.ToString(), "InstallPack", "");
  1548. CSimpleStringA strPackName = p;
  1549. toolkit_free(p);
  1550. assert(!strPackName.IsNullOrEmpty());
  1551. auto &curVer = cfg->install_ini->install_version;
  1552. WriteInstallLog(strPackName, CSimpleStringA::Format("Rollback to version: [%d.%d.%d.%d]",
  1553. curVer.major, curVer.minor, curVer.revision, curVer.build));
  1554. }
  1555. }
  1556. // 设置当前版本安装状态为活动
  1557. char *pCurVersion = inifile_read_str(cfg->install_ini_path, "Main", "InstallVersion", "");
  1558. inifile_write_str(cfg->install_ini_path, pCurVersion, "InstallState", "A");
  1559. inifile_format_write(cfg->install_ini_path, pCurVersion, "SwitchOverDate", "0x%08X", y2k_time_now());
  1560. auto pPackName = inifile_read_str(cfg->install_ini_path, pCurVersion, "InstallPack", "");
  1561. CSimpleStringA strPackName = pPackName;
  1562. toolkit_free(pCurVersion);
  1563. toolkit_free(pPackName);
  1564. // 修改安装包历史
  1565. if (!strPackName.IsNullOrEmpty())
  1566. WriteInstallLog(strPackName, "Set version state active");
  1567. // 拷贝升级包到Upgraded目录
  1568. const char *pPackFile = pVerInfo->install_pack;
  1569. CSimpleStringA strDownloadPath, strUpgradedPath;
  1570. GetPath("Downloads", strDownloadPath);
  1571. GetPath("Upgraded", strUpgradedPath);
  1572. CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadPath, pPackFile);
  1573. CSimpleStringA strDestPath = CSimpleStringA::Format("%s\\%s", (const char*)strUpgradedPath, pPackFile);
  1574. CopyFileA(strSourcePath, strDestPath, FALSE);
  1575. return Error_Succeed;
  1576. }
  1577. ErrorCodeEnum SpEntityPrivilege::IsPackInstalled(const char *pszPackName, bool &bInstalled)
  1578. {
  1579. if (pszPackName == NULL)
  1580. {
  1581. bInstalled = false;
  1582. return Error_Param;
  1583. }
  1584. // 先从内存查找
  1585. auto env = sp_get_env();
  1586. auto cfg = env->cfg;
  1587. bInstalled = sp_cfg_is_pack_installed(cfg, pszPackName) == 0;
  1588. if (bInstalled)
  1589. return Error_Succeed;
  1590. // 再从安装文件目录查找
  1591. CSimpleStringA strPath;
  1592. GetPath("RunInfo", strPath);
  1593. CSimpleStringA strInstallLog = CSimpleStringA::Format("%s\\InstallLog\\%s.log", (const char*)strPath, pszPackName);
  1594. bInstalled = ExistsFileA(strInstallLog) == TRUE;
  1595. return Error_Succeed;
  1596. }
  1597. ErrorCodeEnum SpEntityPrivilege::GetSystemStaticInfoForVersion(CVersion verSoftware,CSystemStaticInfo &Info)
  1598. {
  1599. auto env = sp_get_env();
  1600. auto cfg = env->cfg;
  1601. auto root_ini = cfg->root_ini;
  1602. memset(&Info, 0, sizeof(Info));
  1603. Info.strTerminalID = CSimpleStringA(root_ini->terminal_no);
  1604. Info.strMachineType = CSimpleStringA(root_ini->machine_type);
  1605. //Info.strMachineModel = CSimpleStringA(root_ini->machine_model);
  1606. Info.MachineVersion = CVersion(root_ini->machine_version.major, root_ini->machine_version.minor, root_ini->machine_version.revision, root_ini->machine_version.build);
  1607. Info.eScreen = (ScreenEnum)root_ini->screen;
  1608. Info.strSite = root_ini->site;
  1609. Info.strEnrolAddr = root_ini->enroll_address;
  1610. Info.EnrolGPS = CSphereVector(root_ini->enroll_gps_x, root_ini->enroll_gps_y);
  1611. CSimpleStringA strInstallIni = CSimpleStringA::Format("%s\\%s\\install.ini", env->dir->root_ver_path, (const char*)verSoftware.ToString());
  1612. if (!ExistsFileA(strInstallIni))
  1613. return Error_NotExist;
  1614. int n1(0), n2(0), n3(0), n4(0);
  1615. auto p = inifile_read_str(strInstallIni, "Main", "LatterInstallVersion", "");
  1616. int n = sscanf(p, "%d.%d.%d.%d", &n1, &n2, &n3, &n4);
  1617. toolkit_free(p);
  1618. if (n == 4)
  1619. Info.LatterInstallVersion = CVersion(n1, n2, n3, n4);
  1620. else
  1621. Info.LatterInstallVersion = CVersion(0, 0, 0, 0);
  1622. //Info.LatterInstallPack = inifile_read_int(strInstallIni, "Main", "LatterInstallPack", 0);
  1623. Info.nTotalRunCount = inifile_read_int(strInstallIni, "Main", "TotalRunCount", 0);
  1624. Info.nTodayRunCount = inifile_read_int(strInstallIni, "Main", "TodayRunCount", 0);
  1625. Info.tmCreateDate = CSmallDateTime(inifile_read_int(strInstallIni, "Main", "CreateDate", 0));
  1626. Info.tmCurrentTime = CSmallDateTime(inifile_read_int(strInstallIni, "Main", "CurrentTime", 0));
  1627. p = inifile_read_str(strInstallIni, "Main", "InstallVersion", "");
  1628. n = sscanf(p, "%d.%d.%d.%d", &n1, &n2, &n3, &n4);
  1629. toolkit_free(p);
  1630. if (n != 4)
  1631. return Error_Unexpect;
  1632. Info.InstallVersion = CVersion(n1, n2, n3, n4);
  1633. auto strInstallVersion = Info.InstallVersion.ToString();
  1634. p = inifile_read_str(strInstallIni, strInstallVersion, "PreviousInstallVersion", "");
  1635. n = sscanf(p, "%d.%d.%d.%d", &n1, &n2, &n3, &n4);
  1636. toolkit_free(p);
  1637. if (n == 4)
  1638. Info.PreviousInstallVersion = CVersion(n1, n2, n3, n4);
  1639. else
  1640. Info.PreviousInstallVersion = CVersion(0, 0, 0, 0);
  1641. Info.tmSwithOverDate = CSmallDateTime(inifile_read_int(strInstallIni, strInstallVersion, "SwitchOverDate", 0));
  1642. p = inifile_read_str(strInstallIni, strInstallVersion, "InstallPack", "");
  1643. Info.InstallPack = p;
  1644. toolkit_free(p);
  1645. Info.InstallState = (InstallStateEnum)read_ini_install_state(strInstallIni, strInstallVersion, "InstallState");
  1646. // light pack
  1647. p = inifile_read_str(strInstallIni, "Main", "LightPack", "");
  1648. CSimpleStringA strLightPacks = p;
  1649. toolkit_free(p);
  1650. if (!strLightPacks.IsNullOrEmpty())
  1651. {
  1652. auto list = strLightPacks.Split(',');
  1653. Info.LightPackInfos.Init(list.GetCount());
  1654. for (int i = 0; i < list.GetCount(); ++i)
  1655. {
  1656. auto &strPackName = list[i];
  1657. Info.LightPackInfos[i].strPackName = strPackName;
  1658. Info.LightPackInfos[i].State = (InstallStateEnum)read_ini_install_state(strInstallIni, strPackName, "PackState");
  1659. Info.LightPackInfos[i].tmInstalledDate = CSmallDateTime(inifile_read_int(strInstallIni, strPackName, "InstalledDate", 0));
  1660. }
  1661. }
  1662. return Error_Succeed;
  1663. }
  1664. ErrorCodeEnum SpEntityPrivilege::SetSysDebugLevel(const char *pszEntityName,DebugLevelEnum eDebugLevel,bool bPersist)
  1665. {
  1666. sp_env_t *env = sp_get_env();
  1667. if (!pszEntityName)
  1668. return Error_Null;
  1669. if (stricmp(pszEntityName, "SpShell") == 0)
  1670. env->cfg->shell_ini->shell_debug_level = (int)eDebugLevel;
  1671. sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_name(env->cfg, pszEntityName);
  1672. if (cfg_ent) {
  1673. int old = cfg_ent->debug_level;
  1674. if (old != eDebugLevel) {
  1675. cfg_ent->debug_level = (int)eDebugLevel;
  1676. if (bPersist) {
  1677. return SpTranslateError(sp_cfg_refresh_debug_level(env->cfg, cfg_ent));
  1678. }
  1679. }
  1680. } else {
  1681. return Error_NotExist;
  1682. }
  1683. return Error_Succeed;
  1684. }
  1685. ErrorCodeEnum SpEntityPrivilege::ShowOuputConsole()
  1686. {
  1687. ErrorCodeEnum Error;
  1688. iobuffer_t *pkt = iobuffer_create(-1, -1);
  1689. Error = PostInfoShell(SHELL_CMD_INFO_OUTPUT_CONSOLE_ON, &pkt);
  1690. if (pkt)
  1691. iobuffer_dec_ref(pkt);
  1692. return Error;
  1693. }
  1694. ErrorCodeEnum SpEntityPrivilege::CloseOuputConsole()
  1695. {
  1696. ErrorCodeEnum Error;
  1697. iobuffer_t *pkt = iobuffer_create(-1, -1);
  1698. Error = PostInfoShell(SHELL_CMD_INFO_OUTPUT_CONSOLE_OFF, &pkt);
  1699. if (pkt)
  1700. iobuffer_dec_ref(pkt);
  1701. return Error;
  1702. }
  1703. void SpEntityPrivilege::on_entity_create(int entity_id, int trigger_entity_id)
  1704. {
  1705. if (m_pEntityLifeListener) {
  1706. sp_env_t *env = sp_get_env();
  1707. IEntityLifeListener *pCallback = (IEntityLifeListener *)sp_mod_entity_life_listener_get_tag(m_pEntityLifeListener);
  1708. sp_mod_mgr_t *mod_mgr = env->mod_mgr;
  1709. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, entity_id);
  1710. sp_entity_t *trigger_ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, trigger_entity_id);
  1711. pCallback->OnCreated(ent->cfg->name, Error_Succeed, trigger_ent->cfg->name);
  1712. }
  1713. }
  1714. void SpEntityPrivilege::on_entity_close(int entity_id, int trigger_entity_id, int cause_code)
  1715. {
  1716. if (m_pEntityLifeListener) {
  1717. sp_env_t *env = sp_get_env();
  1718. IEntityLifeListener *pCallback = (IEntityLifeListener *)sp_mod_entity_life_listener_get_tag(m_pEntityLifeListener);
  1719. sp_mod_mgr_t *mod_mgr = env->mod_mgr;
  1720. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, entity_id);
  1721. sp_entity_t *trigger_ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, trigger_entity_id);
  1722. pCallback->OnClosed(ent->cfg->name, (EntityCloseCauseEnum)cause_code, Error_Succeed, trigger_ent->cfg->name);
  1723. }
  1724. }
  1725. void SpEntityPrivilege::on_entity_exception(int entity_id, int error, int entity_state)
  1726. {
  1727. if (m_pEntityLifeListener) {
  1728. sp_env_t *env = sp_get_env();
  1729. IEntityLifeListener *pCallback = (IEntityLifeListener *)sp_mod_entity_life_listener_get_tag(m_pEntityLifeListener);
  1730. sp_mod_mgr_t *mod_mgr = env->mod_mgr;
  1731. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, entity_id);
  1732. pCallback->OnException(ent->cfg->name, "", (EntityStateEnum)entity_state, (EntityStateEnum)entity_state, (ErrorCodeEnum)error);
  1733. }
  1734. }
  1735. void SpEntityPrivilege::__on_entity_create(sp_mod_entity_life_listener_t *listener, int entity_id, int trigger_entity_id, void *user_data)
  1736. {
  1737. SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
  1738. pThis->on_entity_create(entity_id, trigger_entity_id);
  1739. }
  1740. void SpEntityPrivilege::__on_entity_close(sp_mod_entity_life_listener_t *listener, int entity_id, int trigger_entity_id, int cause_code, void *user_data)
  1741. {
  1742. SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
  1743. pThis->on_entity_close(entity_id, trigger_entity_id, cause_code);
  1744. }
  1745. void SpEntityPrivilege::__on_entity_exception(sp_mod_entity_life_listener_t *listener, int entity_id, int error, int entity_state, void *user_data)
  1746. {
  1747. SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
  1748. pThis->on_entity_exception(entity_id, error, entity_state);
  1749. }
  1750. void SpEntityPrivilege::on_user_state(sp_mod_entity_state_listener_t *listener, int entity_id, int last_state, int curr_state)
  1751. {
  1752. sp_env_t *env = sp_get_env();
  1753. IEntityStateListener *pListener = (IEntityStateListener *)sp_mod_entity_listener_get_tag(listener);
  1754. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, entity_id);
  1755. pListener->OnUserStateHook(ent->cfg->name, (DWORD)curr_state, (DWORD)last_state);
  1756. }
  1757. void SpEntityPrivilege::on_entity_state(sp_mod_entity_state_listener_t *listener, int entity_id, int trigger_entity_id, int last_state, int curr_state)
  1758. {
  1759. sp_env_t *env = sp_get_env();
  1760. IEntityStateListener *pListener = (IEntityStateListener *)sp_mod_entity_listener_get_tag(listener);
  1761. sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, entity_id);
  1762. sp_entity_t *trigger_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, trigger_entity_id);
  1763. pListener->OnEntityStateHook(ent->cfg->name, trigger_ent->cfg->name, (EntityStateEnum)curr_state, (EntityStateEnum)last_state);
  1764. }
  1765. void SpEntityPrivilege::on_create_connection(sp_mod_entity_state_listener_t *listener, int src_entity_id, int dst_entity_id)
  1766. {
  1767. sp_env_t *env = sp_get_env();
  1768. IEntityStateListener *pListener = (IEntityStateListener *)sp_mod_entity_listener_get_tag(listener);
  1769. sp_entity_t *src_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, src_entity_id);
  1770. sp_entity_t *dst_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, dst_entity_id);
  1771. pListener->OnCeateConnection(src_ent->cfg->name, dst_ent->cfg->name);
  1772. }
  1773. void SpEntityPrivilege::on_close_connection(sp_mod_entity_state_listener_t *listener, int src_entity_id, int dst_entity_id)
  1774. {
  1775. sp_env_t *env = sp_get_env();
  1776. IEntityStateListener *pListener = (IEntityStateListener *)sp_mod_entity_listener_get_tag(listener);
  1777. sp_entity_t *src_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, src_entity_id);
  1778. sp_entity_t *dst_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, dst_entity_id);
  1779. pListener->OnCloseConnection(src_ent->cfg->name, dst_ent->cfg->name);
  1780. }
  1781. void SpEntityPrivilege::__on_user_state(sp_mod_entity_state_listener_t *listener, int entity_id, int last_state, int curr_state, void *user_data)
  1782. {
  1783. SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
  1784. pThis->on_user_state(listener, entity_id, last_state, curr_state);
  1785. }
  1786. void SpEntityPrivilege::__on_entity_state(sp_mod_entity_state_listener_t *listener, int entity_id, int trigger_entity_id, int last_state, int curr_state, void *user_data)
  1787. {
  1788. SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
  1789. pThis->on_entity_state(listener, entity_id, trigger_entity_id, last_state, curr_state);
  1790. }
  1791. void SpEntityPrivilege::__on_create_connection(sp_mod_entity_state_listener_t *listener, int src_entity_id, int dst_entity_id, void *user_data)
  1792. {
  1793. SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
  1794. pThis->on_create_connection(listener, src_entity_id, dst_entity_id);
  1795. }
  1796. void SpEntityPrivilege::__on_close_connection(sp_mod_entity_state_listener_t *listener, int src_entity_id, int dst_entity_id, void *user_data)
  1797. {
  1798. SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
  1799. pThis->on_close_connection(listener, src_entity_id, dst_entity_id);
  1800. }