SpEntityPrivilege.cpp 70 KB

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