12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpTimer.h"
- #include "SpMisc.h"
- #include "SpEntity.h"
- #include "SpModule.h"
- #include "SpEntityPrivilege.h"
- #include "SpAsyncWait.h"
- #include "sp_iom.h"
- #include "sp_svc.h"
- #include "sp_dbg_export.h"
- #include "sp_log.h"
- #include "sp_var.h"
- #include "sp_def.h"
- #include "sp_cfg.h"
- #include "sp_env.h"
- #include "memutil.h"
- #include "strutil.h"
- #include "fileutil.h"
- #include "shm_array.h"
- #include "shm_mem.h"
- #include "iobuffer.h"
- #include "iniutil.h"
- //#include "sp_sal.h"
- #include "CodeSignVerify.h"
- #include "..\spshell\def.h"
- #include <io.h>
- static ErrorCodeEnum ControlEntity(SpEntity *pEntity, const char *pszEntityName, int call_type, const char *cmd_line, int param1, int param2, CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
- {
- if (!pszEntityName)
- return Error_Null;
- sp_env_t *env = sp_get_env();
- sp_entity_t *ent = sp_mod_mgr_find_entity_by_name(env->mod_mgr, pszEntityName);
- if (!ent)
- return Error_NotExist;
- iobuffer_t *req_pkt = iobuffer_create(-1, -1);
- int v = ent->cfg->idx;
- iobuffer_write(req_pkt, IOBUF_T_I4, &v, 0);
- if (call_type == SHELL_CMD_REQ_ENTITY_START)
- iobuffer_write(req_pkt, IOBUF_T_STR, cmd_line, -1);
- else if (call_type == SHELL_CMD_REQ_ENTITY_TEST)
- iobuffer_write(req_pkt, IOBUF_T_I4, ¶m1, 0);
- SpAsyncWaitRPC *pAsyncWait = new SpAsyncWaitRPC(pEntity, &req_pkt, call_type);
- ErrorCodeEnum Error = pAsyncWait->Begin();
- if (Error == Error_Succeed) {
- pAsynWaitSp.Attach(pAsyncWait, pAsyncWait->GetRefCountPtr());
- }
-
- pAsyncWait->DecrementRef(); // xkm@20150115
- if (req_pkt)
- iobuffer_dec_ref(req_pkt);
- return Error;
- }
- SpEntityPrivilege::SpEntityPrivilege(SpModule *pModule, sp_entity_t *ent, sp_cfg_shell_entity_t *cfg_ent, CEntityBase *pEntityBase)
- : SpEntity(pModule, ent, cfg_ent, pEntityBase), m_pEntityLifeListener(NULL)
- {
- spinlock_init(&m_maplock);
- m_pEntityStateListenerMap = stringmap_create(0);
- }
- SpEntityPrivilege::~SpEntityPrivilege()
- {
- UnregistEntityStateEvent(NULL);
- stringmap_destroy(m_pEntityStateListenerMap);
- }
- ErrorCodeEnum SpEntityPrivilege::Init()
- {
- ErrorCodeEnum Error = SpEntity::Init();
- return Error;
-
- }
- void SpEntityPrivilege::Term()
- {
- //....
- SpEntity::Term();
- UnregistEntityStateEvent(NULL); // unregister all
- }
- CSmartPointer<IEntityFunctionPrivilege> SpEntityPrivilege::GetPrivilegeFunction()
- {
- IEntityFunctionPrivilege *pPrivilege = dynamic_cast<IEntityFunctionPrivilege*>(this);
- return pPrivilege;
- }
- ErrorCodeEnum SpEntityPrivilege::StartEntity(const char *pszEntityName, const char *pszCmdLine,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
- {
- return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_START, pszCmdLine, 0, 0, pAsynWaitSp);
- }
- ErrorCodeEnum SpEntityPrivilege::StopEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
- {
- return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_STOP, NULL, 0, 0, pAsynWaitSp);
- }
- ErrorCodeEnum SpEntityPrivilege::CloseEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
- {
- return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_STOP, NULL, 0, 0, pAsynWaitSp);
- }
- ErrorCodeEnum SpEntityPrivilege::TerminateEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
- {
- return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_TERMINATE, NULL, 0, 0, pAsynWaitSp);
- }
- ErrorCodeEnum SpEntityPrivilege::PauseEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
- {
- return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_PAUSE, NULL, 0, 0, pAsynWaitSp);
- }
- ErrorCodeEnum SpEntityPrivilege::ContinueEntity(const char *pszEntityName,CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
- {
- return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_CONTINUE, NULL, 0, 0, pAsynWaitSp);
- }
- ErrorCodeEnum SpEntityPrivilege::TestEntity(const char *pszEntityName,EntityTestEnum eTestType, CSmartPointer<IAsynWaitSp> &pAsynWaitSp)
- {
- return ControlEntity(this, pszEntityName, SHELL_CMD_REQ_ENTITY_TEST, NULL, (int)eTestType, 0, pAsynWaitSp);
- }
- ErrorCodeEnum SpEntityPrivilege::RegistEntityLifeEvent(IEntityLifeListener *pListener)
- {
- if (!m_pEntityLifeListener) {
- sp_mod_entity_life_cb cb;
- cb.on_entity_close = &SpEntityPrivilege::__on_entity_close;
- cb.on_entity_create = &SpEntityPrivilege::__on_entity_create;
- cb.on_entity_exception = &SpEntityPrivilege::__on_entity_exception;
- cb.user_data = this;
- int rc = sp_mod_entity_life_listener_create(&cb, m_svc, pListener, &m_pEntityLifeListener);
- return SpTranslateError(rc);
- } else {
- return Error_Duplication;
- }
- }
- ErrorCodeEnum SpEntityPrivilege::UnregistLiftEvent()
- {
- if (!m_pEntityLifeListener) {
- return Error_NotInit;
- } else {
- sp_mod_entity_life_listener_destroy(m_pEntityLifeListener);
- m_pEntityLifeListener = NULL;
- return Error_Succeed;
- }
- }
- ErrorCodeEnum SpEntityPrivilege::RegistEntityStateEvent(const char *pszEntityName,IEntityStateListener *pListener)
- {
- sp_env_t *env = sp_get_env();
- int target_ent_id;
- if (!pListener)
- return Error_Null;
- if (pszEntityName) {
- sp_entity_t *ent;
- ent = sp_mod_mgr_find_entity_by_name(env->mod_mgr, pszEntityName);
- if (!ent)
- return Error_NotExist;
- target_ent_id = ent->cfg->idx;
- } else {
- LOG_TRACE("registered state event for all entities!");
- target_ent_id = -1; // regist for all entities
- pszEntityName = "*";
- }
- ErrorCodeEnum Error = Error_Succeed;
- spinlock_enter(&m_maplock, -1);
- if (stringmap_find(m_pEntityStateListenerMap, pszEntityName) ){
- Error = Error_AlreadyExist;
- } else {
- int rc;
- sp_mod_entity_state_listener_t *listener;
- sp_mod_entity_event_cb cb;
- cb.on_close_connection = &SpEntityPrivilege::__on_close_connection;
- cb.on_create_connection = &SpEntityPrivilege::__on_create_connection;
- cb.on_user_state = &SpEntityPrivilege::__on_user_state;;
- cb.on_entity_state = &SpEntityPrivilege::__on_entity_state;
- cb.user_data = this;
- rc = sp_mod_entity_listener_create(target_ent_id, &cb, m_svc, pListener, &listener);
- if (rc == 0) {
- stringmap_add(m_pEntityStateListenerMap, pszEntityName ? pszEntityName : "*", listener);
- } else {
- Error = SpTranslateError(rc);
- }
- }
- spinlock_leave(&m_maplock);
- return Error;
- }
- ErrorCodeEnum SpEntityPrivilege::UnregistEntityStateEvent(const char *pszEntityName)
- {
- ErrorCodeEnum Error = Error_Succeed;
- if (pszEntityName == NULL) {
- pszEntityName = "*";
- }
- spinlock_enter(&m_maplock, -1);
- if (pszEntityName) {
- stringmap_kv_pair *kvp;
- kvp = stringmap_find(m_pEntityStateListenerMap, pszEntityName);
- if (kvp) {
- sp_mod_entity_state_listener_t *listener;
- listener = (sp_mod_entity_state_listener_t *)stringmap_kv_pair_get_value(kvp);
- stringmap_remove(m_pEntityStateListenerMap, pszEntityName);
- sp_mod_entity_listener_destroy(listener);
- } else {
- Error = Error_NotExist;
- }
- } else { // unregist all
- stringmap_iterator *it = stringmap_iterator_create(m_pEntityStateListenerMap);
- while (stringmap_iterator_next(it) == 0) {
- const char *ent_name = (const char*)stringmap_iterator_get_key(it);
- sp_mod_entity_state_listener_t *listener = (sp_mod_entity_state_listener_t *)stringmap_iterator_get_value(it);
- stringmap_remove(m_pEntityStateListenerMap, ent_name);
- sp_mod_entity_listener_destroy(listener);
- }
- stringmap_iterator_destroy(it);
- }
- spinlock_leave(&m_maplock);
- return Error;
- }
- ErrorCodeEnum SpEntityPrivilege::DisplayBlueScreen(const char *pszTitle)
- {
- ErrorCodeEnum Error;
- iobuffer_t *pkt = iobuffer_create(-1, -1);
- iobuffer_write(pkt, IOBUF_T_STR, pszTitle, -1);
- //int nBlueScreen = 1;
- //iobuffer_write(pkt, IOBUF_T_I4, &nBlueScreen, 0);
- Error = PostInfoShell(SHELL_CMD_INFO_BLUESCREEN_DISPLAY, &pkt);
- if (pkt)
- iobuffer_dec_ref(pkt);
- return Error;
- }
- ErrorCodeEnum SpEntityPrivilege::UndisplayBlueScreen()
- {
- ErrorCodeEnum Error;
- iobuffer_t *pkt = iobuffer_create(-1, -1);
- Error = PostInfoShell(SHELL_CMD_INFO_BLUESCREEN_UNDISPLAY, &pkt);
- if (pkt)
- iobuffer_dec_ref(pkt);
- return Error;
- }
- ErrorCodeEnum SpEntityPrivilege::Reboot(RebootTriggerEnum eTriggerReason,RebootWayEnum eWay)
- {
- ErrorCodeEnum Error;
- int v;
- iobuffer_t *pkt = iobuffer_create(-1, -1);
- v = eTriggerReason;
- iobuffer_write(pkt, IOBUF_T_I4, &v, 0);
- v = eWay;
- iobuffer_write(pkt, IOBUF_T_I4, &v, 0);
- Error = PostInfoShell(SHELL_CMD_INFO_MACHINE_REBOOT, &pkt);
- if (pkt)
- iobuffer_dec_ref(pkt);
- return Error;
- }
- bool SpEntityPrivilege::WriteInstallLog(const char *pszPackName, const char *pszLogText)
- {
- auto env = sp_get_env();
- // 创建安装包日志文件
- CSimpleStringA strInstallLogDir = CSimpleStringA::Format("%s\\InstallLog", env->dir->root_runinfo_path);
- if (!ExistsDirA(strInstallLogDir))
- CreateDirA(strInstallLogDir, TRUE);
- // 写安装包日志
- CSimpleStringA strInstallLog = CSimpleStringA::Format("%s\\%s.log", (const char*)strInstallLogDir, pszPackName);
- FILE *pInstallLog = fopen(strInstallLog, "a+");
- if (pInstallLog == NULL)
- {
- LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("open install log file [%s] to write fail", pszPackName));
- return false;
- }
-
- fprintf_s(pInstallLog, "[%s] %s\r\n", (const char*)CSmallDateTime::GetNow().ToTimeString(), pszLogText);
- fflush(pInstallLog);
- fclose(pInstallLog);
- return true;
- }
- bool SpEntityPrivilege::RecursiveCopyDir(const char *pszSourceDir, const char *pszDestDir)
- {
- array_header_t *arr;
-
- if (!ExistsDirA(pszSourceDir))
- {
- Dbg("source dir [%s] not exists", pszSourceDir);
- return false;
- }
- if (!ExistsDirA(pszDestDir))
- {
- if (!CreateDirRecursiveA(pszDestDir))
- {
- Dbg("create dir [%s] fail: %d", pszDestDir, GetLastError());
- return false;
- }
- Dbg("create dir [%s] succ", pszDestDir);
- }
- arr = fileutil_get_sub_files_a(pszSourceDir);
- if (arr)
- {
- int i;
- for (i = 0; i < arr->nelts; ++i)
- {
- char szDestFile[256] = { 0 };
- char *file = ARRAY_IDX(arr, i, char*);
- strcpy(szDestFile, pszDestDir);
- if (szDestFile[strlen(szDestFile) - 1] != '\\')
- strcat(szDestFile, "\\");
- strcat(szDestFile, strrchr(file, '\\') + 1);
- if (!CopyFileA(file, szDestFile, FALSE))
- {
- Dbg("copy file [%s] fail: %d", file, GetLastError());
- toolkit_array_free2(arr);
- return false;
- }
- Dbg("copy file [%s] succ", file);
- }
- toolkit_array_free2(arr);
- }
- arr = fileutil_get_sub_dirs_a(pszSourceDir);
- if (arr)
- {
- int i;
- for (i = 0; i < arr->nelts; ++i)
- {
- char szDestSubDir[256] = { 0 };
- char *dir = ARRAY_IDX(arr, i, char*);
- strcpy(szDestSubDir, pszDestDir);
- if (szDestSubDir[strlen(szDestSubDir) - 1] != '\\')
- strcat(szDestSubDir, "\\");
- strcat(szDestSubDir, strrchr(dir, '\\') + 1);
- if (!RecursiveCopyDir(dir, szDestSubDir))
- {
- Dbg("copy dir [%s] fail: %d", dir, GetLastError());
- toolkit_array_free2(arr);
- return false;
- }
- Dbg("copy dir [%s] succ", dir);
- }
- toolkit_array_free2(arr);
- }
- return true;
- }
- // 创建新版本目录,拷贝当前版本bin、cfg、dep、mod、install.ini,并修改Shell.ini及install.ini中相关配置
- ErrorCodeEnum SpEntityPrivilege::CreateInstallNewVersion(CVersion NewSoftwareVersion,const char *pszPackageName)
- {
- auto env = sp_get_env();
- auto cfg = env->cfg;
- // 创建安装包日志文件
- WriteInstallLog(pszPackageName, CSimpleStringA::Format("Create new version: [%s], pack: [%s]",
- (const char*)NewSoftwareVersion.ToString(), (const char*)pszPackageName));
- CSimpleStringA strCurVerPath = env->dir->base_path;
- CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)NewSoftwareVersion.ToString());
- auto &v = cfg->install_ini->install_version;
- CVersion curVersion(v.major, v.minor, v.revision, v.build);
- assert(curVersion != NewSoftwareVersion);
- if (curVersion == NewSoftwareVersion)
- {
- LogError(Severity_Low, Error_Bug, 0, CSimpleStringA::Format("new ver [%s] equals current ver [%s]",
- (const char*)NewSoftwareVersion.ToString(), (const char*)curVersion.ToString()));
- return Error_Bug;
- }
- if (ExistsDirA(strNewVerPath))
- {
- Dbg("new version dir [%s] already exist, delete it", (const char*)strNewVerPath);
- RemoveDirRecursiveA(strNewVerPath);
- }
- if (!CreateDirA(strNewVerPath, TRUE))
- {
- Dbg("create new version dir fail: %d", GetLastError());
- return Error_Unexpect;
- }
- Dbg("create new version dir succ");
- CSimpleStringA strSource = CSimpleStringA::Format("%s\\bin", (const char*)strCurVerPath);
- CSimpleStringA strDest = CSimpleStringA::Format("%s\\bin", (const char*)strNewVerPath);
- if (!RecursiveCopyDir(strSource, strDest))
- {
- Dbg("copy bin dir fail");
- return Error_Unexpect;
- }
- Dbg("copy bin dir succ");
- strSource = CSimpleStringA::Format("%s\\cfg", (const char*)strCurVerPath);
- strDest = CSimpleStringA::Format("%s\\cfg", (const char*)strNewVerPath);
- if (!RecursiveCopyDir(strSource, strDest))
- {
- Dbg("copy cfg dir fail");
- return Error_Unexpect;
- }
- Dbg("copy cfg dir succ");
-
- strSource = CSimpleStringA::Format("%s\\dep", (const char*)strCurVerPath);
- strDest = CSimpleStringA::Format("%s\\dep", (const char*)strNewVerPath);
- if (!RecursiveCopyDir(strSource, strDest))
- {
- Dbg("copy dep dir fail");
- return Error_Unexpect;
- }
- Dbg("copy dep dir succ");
- strSource = CSimpleStringA::Format("%s\\mod", (const char*)strCurVerPath);
- strDest = CSimpleStringA::Format("%s\\mod", (const char*)strNewVerPath);
- if (!RecursiveCopyDir(strSource, strDest))
- {
- Dbg("copy mod dir fail");
- return Error_Unexpect;
- }
- Dbg("copy mod dir succ");
- strSource = CSimpleStringA::Format("%s\\res", (const char*)strCurVerPath);
- strDest = CSimpleStringA::Format("%s\\res", (const char*)strNewVerPath);
- // 检测res目录是否存在
- if (ExistsDirA(strSource))
- {
- if (!RecursiveCopyDir(strSource, strDest))
- {
- Dbg("copy res dir fail");
- return Error_Unexpect;
- }
- Dbg("copy res dir succ");
- }
- else
- {
- if (!CreateDirA(strDest, TRUE))
- {
- Dbg("create res dir fail");
- return Error_Unexpect;
- }
- Dbg("create res dir succ");
- }
- strSource = CSimpleStringA::Format("%s\\imdep", (const char*)strCurVerPath);
- strDest = CSimpleStringA::Format("%s\\imdep", (const char*)strNewVerPath);
- // 检测imdep目录是否存在
- if (ExistsDirA(strSource))
- {
- if (!RecursiveCopyDir(strSource, strDest))
- {
- Dbg("copy imdep dir fail");
- return Error_Unexpect;
- }
- Dbg("copy imdep dir succ");
- }
- else
- {
- if (!CreateDirA(strDest, TRUE))
- {
- Dbg("create imdep dir fail");
- return Error_Unexpect;
- }
- Dbg("create imdep dir succ");
- }
- strSource = CSimpleStringA::Format("%s\\install.ini", (const char*)strCurVerPath);
- strDest = CSimpleStringA::Format("%s\\install.ini", (const char*)strNewVerPath);
- if (!CopyFileA(strSource, strDest, FALSE))
- {
- Dbg("copy install.ini fail: %d", GetLastError());
- return Error_Unexpect;
- }
- Dbg("copy install.ini succ");
-
- // 修改新Shell.ini
- CSimpleStringA strNewShellIni = CSimpleStringA::Format("%s\\cfg\\shell.ini", (const char*) strNewVerPath);
- CSimpleStringA strShellVarIni = CSimpleStringA::Format("%s\\runcfg\\shellVar.ini", (const char*)env->dir->root_runinfo_path);
- Dbg("update SoftwareVersion to %s", strShellVarIni);
- int nRet = inifile_format_write((const char*)strShellVarIni, "Main", "SoftwareVersion", "%d.%d.%d", NewSoftwareVersion.GetMajor(),
- NewSoftwareVersion.GetMinor(), NewSoftwareVersion.GetRevision());
- //// 修改当前install.ini
- inifile_write_str(cfg->install_ini_path, "Main", "LatterInstallVersion", NewSoftwareVersion.ToString());
- inifile_write_str(cfg->install_ini_path, "Main", "LatterInstallPack", pszPackageName);
-
- //inifile_write_str(cfg->install_ini_path, CurVersion.ToString(), "InstallState", "U");
- // 修改新版本install.ini
- auto strNewInstallIni = CSimpleStringA::Format("%s\\install.ini", (const char*)strNewVerPath);
- assert(ExistsFileA(strNewInstallIni));
-
- char szNow[16] = {};
- sprintf(szNow, "0x%08X", y2k_time_now());
- // 缓存前版本轻量包记录
- char *packs = inifile_read_str(strNewInstallIni, "Main", "LightPack", "");
-
- nRet |= inifile_write_str(strNewInstallIni, "Main", "InstallVersion", NewSoftwareVersion.ToString());
- nRet |= inifile_write_str(strNewInstallIni, "Main", "CreateDate", szNow);
- nRet |= inifile_write_str(strNewInstallIni, "Main", "LatterInstallVersion", "");
- nRet |= inifile_write_str(strNewInstallIni, "Main", "LightPack", "");
- nRet |= inifile_write_str(strNewInstallIni, "Main", "TotalRunCount", "1");
- nRet |= inifile_write_str(strNewInstallIni, "Main", "TodayRunCount", "1");
- nRet |= inifile_write_str(strNewInstallIni, "Main", "CurrentTime", szNow);
-
- nRet |= inifile_write_str(strNewInstallIni, NewSoftwareVersion.ToString(), "PreviousInstallVersion", curVersion.ToString());
- nRet |= inifile_write_str(strNewInstallIni, NewSoftwareVersion.ToString(), "InstallPack", pszPackageName);
- nRet |= inifile_write_str(strNewInstallIni, NewSoftwareVersion.ToString(), "InstallState", "I"); // 正在安装
- // 去掉所有历史版本中轻量安装记录
- if (packs)
- {
- if (strlen(packs) >0)
- {
- char *p = NULL;
- while((p = strrchr(packs, ',')) != NULL)
- {
- *p = 0;
- WritePrivateProfileSectionA(p+1, NULL, strNewInstallIni);
- }
- WritePrivateProfileSectionA(packs, NULL, strNewInstallIni);
- }
- toolkit_free(packs);
- }
- Dbg("write version into install.ini done");
- // 修改内存配置项
- auto ver_info = (sp_cfg_version_info_t*)shm_malloc(sizeof(sp_cfg_version_info_t));
- memset(ver_info, 0, sizeof(sp_cfg_version_info_t));
- ver_info->version.major = NewSoftwareVersion.GetMajor();
- ver_info->version.minor = NewSoftwareVersion.GetMinor();
- ver_info->version.revision = NewSoftwareVersion.GetRevision();
- ver_info->version.build = NewSoftwareVersion.GetBuild();
- ver_info->previous_version.major = curVersion.GetMajor();
- ver_info->previous_version.minor = curVersion.GetMinor();
- ver_info->previous_version.revision = curVersion.GetRevision();
- ver_info->previous_version.build = curVersion.GetBuild();
- ver_info->install_pack = shm_strdup(pszPackageName);
- ver_info->install_state = Install_Pending;
- SHM_ARRAY_PUSH(cfg->install_ini->arr_version, sp_cfg_version_info_t*) = ver_info;
- cfg->install_ini->latter_install_version.major = NewSoftwareVersion.GetMajor();
- cfg->install_ini->latter_install_version.minor = NewSoftwareVersion.GetMinor();
- cfg->install_ini->latter_install_version.revision = NewSoftwareVersion.GetRevision();
- cfg->install_ini->latter_install_version.build = NewSoftwareVersion.GetBuild();
- Dbg("write version into share mem done");
- return nRet == 0 ? Error_Succeed : Error_Unexpect;
- }
- ErrorCodeEnum CopyFolder(CSimpleStringA strSourcePath, CSimpleStringA strDestPath)
- {
- if (strSourcePath.IsNullOrEmpty() || strDestPath.IsNullOrEmpty())
- {
- return Error_Null;
- }
- Dbg("Start to copy dir %s to dir %s", (const char*)strSourcePath,(const char*)strDestPath);
- _finddata_t FileInfo;
- CSimpleStringA strfind = strSourcePath + "\\*";
- long Handle = _findfirst(strfind, &FileInfo);
- if (-1L == Handle)
- {
- _findclose(Handle);
- Dbg("%s文件夹为空", strSourcePath);
- return Error_Succeed;
- }
- CSimpleStringA newPath;
- do{
- if (FileInfo.attrib & _A_SUBDIR)
- {
- if ((strcmp(FileInfo.name, ".") != 0) && (strcmp(FileInfo.name, "..") != 0))
- {
- newPath = strSourcePath + "\\" + FileInfo.name;
- CSimpleStringA newDestPath = strDestPath + "\\" + FileInfo.name;
-
- /*if (strcmp(FileInfo.name, "") == 0)
- {
- continue;
- }*/
- if (!ExistsDirA(newDestPath))
- {
- CreateDirA(newDestPath, TRUE);
- Dbg("Create Dir %s success", (const char*)newDestPath);
- }
-
- CopyFolder(newPath, newDestPath);
- }
- }
- else
- {
- CSimpleStringA strFindName = FileInfo.name;
- {
- CSimpleStringA strSourceFile = strSourcePath + "\\" + strFindName;
- CSimpleStringA strDestFile = strDestPath + "\\" + strFindName;
- if (ExistsFileA(strSourceFile))
- {
- // 先去除目标文件只读属性
- if (ExistsFileA(strDestFile))
- RemoveFileReadOnlyAttributeA(strDestFile);
- LOG_TRACE("copy file from [%s] to [%s]", (const char*)strSourceFile, (const char*)strDestFile);
- BOOL bRet = CopyFileA(strSourceFile, strDestFile, FALSE);
- // 去除只读属性
- if (bRet)
- RemoveFileReadOnlyAttributeA(strDestFile);
- else
- {
- // 覆盖失败,则生成.new副本
- Dbg("copy file [%s] to [%s] fail, now save as .new file", (const char*) strSourcePath, (const char*) strDestFile);
- strDestFile += ".new";
- bRet = CopyFileA(strSourceFile, strDestFile, FALSE);
- if (bRet)
- {
- RemoveFileReadOnlyAttributeA(strDestPath);
- Dbg("copy [%s] as [%s] succeed", (const char*)strSourceFile, (const char*) strDestFile);
- }
- else
- {
- LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("copy file [%s] as [%s] fail", (const char*) strSourceFile, (const char*) strDestFile));
- return Error_Block;
- }
- }
- }
- else
- {
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to copy not exists", (const char*)strSourceFile));
- return Error_NotExist;
- }
- }
- }
- } while (_findnext(Handle, &FileInfo) == 0);
- _findclose(Handle);
- return Error_Succeed;
- }
- CSimpleStringA GetFileDirectory(const char *pszFullPath)
- {
- int i=strlen(pszFullPath)-1;
- for( ; i>0 && pszFullPath[i]!='\\'; i--)NULL;
- return CSimpleStringA(pszFullPath, i);
- }
- ErrorCodeEnum SpEntityPrivilege::BeginLightInstall(const char *pszPackageName)
- {
- // 创建安装包日志文件
- WriteInstallLog(pszPackageName, CSimpleStringA::Format("Begin install light pack: [%s]", pszPackageName));
- CSimpleStringA strAdPath;
- auto rc = GetPath("Ad", strAdPath);
- assert(rc == Error_Succeed);
- CSimpleStringA strDownloadsPath;
- rc = GetPath("Downloads", strDownloadsPath);
- assert(rc == Error_Succeed);
- CSimpleStringA strUnzipPath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)pszPackageName);
- if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
- strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength()-4);
- auto strConfigPath = CSimpleStringA::Format("%s\\Run.ini", (const char*)strUnzipPath);
- // 检查文件是否存在
- DWORD attr = GetFileAttributesA(strConfigPath);
- assert((attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY));
- // ToCopy= Run\bin\SpShell.exe, Run\dep\CardIssuer.dll
- char *p = inifile_read_str(strConfigPath, "Action", "ToCopy", "");
- CSimpleStringA strToCopy = p;
- toolkit_free(p);
- //assert(!strToCopy.IsNullOrEmpty());
- if (strToCopy.IsNullOrEmpty())
- {
- LogError(Severity_Low, Error_Null, 0, "file to copy not define");
- return Error_Null;
- }
- auto list = strToCopy.Split(',');
- for(int i=0; i<list.GetCount(); i++)
- {
- auto &file = list[i];
- if (file.IsStartWith("Data\\"))
- {
- // 通配符检测
- if (file.IndexOf("*") >0)
- {
- CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)GetFileDirectory(file));
-
- CSimpleStringA StrDirToCopy = GetFileDirectory(file).SubString(5, file.GetLength()-5);
- CSimpleStringA strDestPath = "";
- if (StrDirToCopy.IsNullOrEmpty())
- {
- strDestPath = strAdPath;
- }
- else
- {
- strDestPath = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)GetFileDirectory(file).SubString(5, file.GetLength()-5));
- }
-
- rc = CopyFolder(strSourcePath, strDestPath);
- if (Error_Succeed != rc)
- {
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("copy dir [%s] to dir [%s] fail", (const char*)strSourcePath, (const char*)strDestPath));
- }
- }
- else
- {
- CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)file);
- CSimpleStringA strDestPath = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)file.SubString(5, file.GetLength()-5));
- if (ExistsFileA(strSourcePath))
- {
- // 先去除目标文件只读属性
- if (ExistsFileA(strDestPath))
- RemoveFileReadOnlyAttributeA(strDestPath);
- LOG_TRACE("copy file from [%s] to [%s]", (const char*)strSourcePath, (const char*)strDestPath);
- BOOL bRet = CopyFileA(strSourcePath, strDestPath, FALSE);
- // 去除只读属性
- if (bRet)
- RemoveFileReadOnlyAttributeA(strDestPath);
- else
- {
- // 覆盖失败,则生成.new副本
- Dbg("copy file [%s] to [%s] fail, now save as .new file", (const char*) strSourcePath, (const char*) strDestPath);
- strDestPath += ".new";
- bRet = CopyFileA(strSourcePath, strDestPath, FALSE);
- if (bRet)
- {
- RemoveFileReadOnlyAttributeA(strDestPath);
- Dbg("copy [%s] as [%s] succeed", (const char*)strSourcePath, (const char*) strDestPath);
- }
- else
- {
- LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("copy file [%s] as [%s] fail", (const char*) strSourcePath, (const char*) strDestPath));
- return Error_Block;
- }
- }
- }
- else
- {
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to copy not exists", (const char*)strSourcePath));
- return Error_NotExist;
- }
- }
- }
- else
- {
- LogError(Severity_Low, Error_NoPrivilege, 0, CSimpleStringA::Format("light pack [%s] has invalid copy item: [%s]",
- (const char*)pszPackageName, (const char*)file));
- return Error_NoPrivilege;
- }
- }
- // 登录到安装记录文件中
- sp_env_t *env = sp_get_env();
- auto cfg = env->cfg;
- CSimpleStringA strPackName = pszPackageName;
- CSimpleStringA strLightPacks = cfg->install_ini->light_packs;
- if (!strLightPacks.IsNullOrEmpty())
- strLightPacks += ",";
- strLightPacks += strPackName;
- inifile_write_str(cfg->install_ini_path, "Main", "LightPack", strLightPacks);
- shm_free(cfg->install_ini->light_packs);
- cfg->install_ini->light_packs = shm_strdup(strLightPacks);
- // 修改内存配置项
- sp_cfg_pack_info_t *pack_info = (sp_cfg_pack_info_t*)shm_malloc(sizeof(sp_cfg_pack_info_t));
- memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
- pack_info->name = shm_strdup(strPackName);
- pack_info->install_time = y2k_time_now();
- pack_info->state = Install_Installed;
- SHM_ARRAY_PUSH(cfg->install_ini->arr_light_pack, sp_cfg_pack_info_t*) = pack_info;
- inifile_format_write(cfg->install_ini_path, strPackName, "InstalledDate", "0x%08X", y2k_time_now());
- inifile_write_str(cfg->install_ini_path, strPackName, "PackState", "U");
- Dbg("install light pack [%s] succeed", (const char*)strPackName);
- return Error_Succeed;
- }
- //ErrorCodeEnum SpEntityPrivilege::BeginLightInstall(const char *pszPackageName)
- //{
- // // 创建安装包日志文件
- // WriteInstallLog(pszPackageName, CSimpleStringA::Format("Begin install light pack: [%s]", pszPackageName));
- //
- // CSimpleStringA strAdPath;
- // auto rc = GetPath("Ad", strAdPath);
- // assert(rc == Error_Succeed);
- //
- // CSimpleStringA strDownloadsPath;
- // rc = GetPath("Downloads", strDownloadsPath);
- // assert(rc == Error_Succeed);
- //
- // CSimpleStringA strUnzipPath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)pszPackageName);
- // if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
- // strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength()-4);
- //
- // auto strConfigPath = CSimpleStringA::Format("%s\\Run.ini", (const char*)strUnzipPath);
- //
- // // 检查文件是否存在
- // DWORD attr = GetFileAttributesA(strConfigPath);
- // assert((attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY));
- //
- // // ToCopy= Run\bin\SpShell.exe, Run\dep\CardIssuer.dll
- // char *p = inifile_read_str(strConfigPath, "Action", "ToCopy", "");
- // CSimpleStringA strToCopy = p;
- // toolkit_free(p);
- //
- // //assert(!strToCopy.IsNullOrEmpty());
- // if (strToCopy.IsNullOrEmpty())
- // {
- // LogError(Severity_Low, Error_Null, 0, "file to copy not define");
- // return Error_Null;
- // }
- //
- // auto list = strToCopy.Split(',');
- // for(int i=0; i<list.GetCount(); i++)
- // {
- // auto &file = list[i];
- // if (file.IsStartWith("Data\\"))
- // {
- // CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)file);
- // CSimpleStringA strDestPath = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)file.SubString(5, file.GetLength()-5));
- //
- // if (ExistsFileA(strSourcePath))
- // {
- // // 先去除目标文件只读属性
- // if (ExistsFileA(strDestPath))
- // RemoveFileReadOnlyAttributeA(strDestPath);
- //
- // LOG_TRACE("copy file from [%s] to [%s]", (const char*)strSourcePath, (const char*)strDestPath);
- // BOOL bRet = CopyFileA(strSourcePath, strDestPath, FALSE);
- //
- // // 去除只读属性
- // if (bRet)
- // RemoveFileReadOnlyAttributeA(strDestPath);
- // else
- // {
- // // 覆盖失败,则生成.new副本
- // Dbg("copy file [%s] to [%s] fail, now save as .new file", (const char*) strSourcePath, (const char*) strDestPath);
- //
- // strDestPath += ".new";
- // bRet = CopyFileA(strSourcePath, strDestPath, FALSE);
- // if (bRet)
- // {
- // RemoveFileReadOnlyAttributeA(strDestPath);
- // Dbg("copy [%s] as [%s] succeed", (const char*)strSourcePath, (const char*) strDestPath);
- // }
- // else
- // {
- // LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("copy file [%s] as [%s] fail", (const char*) strSourcePath, (const char*) strDestPath));
- // return Error_Block;
- // }
- // }
- // }
- // else
- // {
- // LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to copy not exists", (const char*)strSourcePath));
- // return Error_NotExist;
- // }
- // }
- // else
- // {
- // LogError(Severity_Low, Error_NoPrivilege, 0, CSimpleStringA::Format("light pack [%s] has invalid copy item: [%s]",
- // (const char*)pszPackageName, (const char*)file));
- // return Error_NoPrivilege;
- // }
- // }
- //
- // // 登录到安装记录文件中
- // sp_env_t *env = sp_get_env();
- // auto cfg = env->cfg;
- //
- // CSimpleStringA strPackName = pszPackageName;
- //
- // CSimpleStringA strLightPacks = cfg->install_ini->light_packs;
- // if (!strLightPacks.IsNullOrEmpty())
- // strLightPacks += ",";
- //
- // strLightPacks += strPackName;
- // inifile_write_str(cfg->install_ini_path, "Main", "LightPack", strLightPacks);
- //
- // shm_free(cfg->install_ini->light_packs);
- // cfg->install_ini->light_packs = shm_strdup(strLightPacks);
- //
- // // 修改内存配置项
- // sp_cfg_pack_info_t *pack_info = (sp_cfg_pack_info_t*)shm_malloc(sizeof(sp_cfg_pack_info_t));
- // memset(pack_info, 0, sizeof(sp_cfg_pack_info_t));
- // pack_info->name = shm_strdup(strPackName);
- // pack_info->install_time = y2k_time_now();
- // pack_info->state = Install_Installed;
- // SHM_ARRAY_PUSH(cfg->install_ini->arr_light_pack, sp_cfg_pack_info_t*) = pack_info;
- //
- // inifile_format_write(cfg->install_ini_path, strPackName, "InstalledDate", "0x%08X", y2k_time_now());
- // inifile_write_str(cfg->install_ini_path, strPackName, "PackState", "U");
- //
- // Dbg("install light pack [%s] succeed", (const char*)strPackName);
- //
- // return Error_Succeed;
- //}
- bool SpEntityPrivilege::IsWow64Process()
- {
- typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
- LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle("kernel32"), "IsWow64Process");
-
- BOOL bIsWow64 = FALSE;
- if (NULL != fnIsWow64Process)
- {
- if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
- {
- Dbg("call IsWow64Process error: %d", GetLastError());
- return false;
- }
- return bIsWow64 == TRUE;
- }
- else
- {
- Dbg("get IsWow64Process error: %d", GetLastError());
- return false;
- }
- }
- // result=0&msg=install ok
- bool SpEntityPrivilege::GetSysUpgradeResult(const char *pszResultLog, ErrorCodeEnum &eErrorCode, CSimpleStringA &strErrMsg)
- {
- // 设置初始值
- eErrorCode = Error_Succeed;
- strErrMsg = "";
- if (!ExistsFileA(pszResultLog))
- {
- Dbg(CSimpleStringA::Format("result log [%s] not exist", pszResultLog));
- strErrMsg = "result.log not exist";
- return false;
- }
- FILE *pResultLog = fopen(pszResultLog, "r");
- if (pResultLog == NULL)
- {
- LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("open result log [%s] fail", pszResultLog));
- strErrMsg = "open result.log fail";
- return false;
- }
- char szTmp[4096] = {};
- int nRead = fread_s(szTmp, sizeof(szTmp), 1, sizeof(szTmp), pResultLog);
- CSimpleStringA strResult = szTmp;
- fclose(pResultLog);
- pResultLog = NULL;
- auto arr = strResult.Split('&');
- for (int i = 0; i < arr.GetCount(); i++)
- {
- auto arr2 = arr[i].Split('=');
- if (arr2.GetCount() == 2)
- {
- if (stricmp(arr2[0], "result") == 0)
- eErrorCode = (ErrorCodeEnum)(DWORD)atoi(arr2[1]);
- else if (stricmp(arr2[0], "msg") == 0)
- strErrMsg = arr2[1];
- }
- }
- return true;
- }
- // 执行系统外升级
- ErrorCodeEnum SpEntityPrivilege::BeginSysPackInstall(const char *pszPackageName, CSimpleStringA &strErrMsg)
- {
- // 创建安装包日志文件
- strErrMsg = "";
- WriteInstallLog(pszPackageName, CSimpleStringA::Format("begin install os pack: [%s]", pszPackageName));
- CSimpleStringA strDownloadsPath;
- auto rc = GetPath("Downloads", strDownloadsPath);
- assert(rc == Error_Succeed);
- CSimpleStringA strUnzipPath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)pszPackageName);
- if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
- strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength() - 4);
- auto strConfigPath = CSimpleStringA::Format("%s\\Run.ini", (const char*)strUnzipPath);
- // 检查文件是否存在
- DWORD attr = GetFileAttributesA(strConfigPath);
- assert((attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY));
- // ToRun=install.bat
- char *p = inifile_read_str(strConfigPath, "Action", "ToRun", "");
- CSimpleStringA strToRun = p;
- toolkit_free(p);
- //assert(!strToCopy.IsNullOrEmpty());
- if (strToRun.IsNullOrEmpty())
- {
- strErrMsg = "未定义执行文件[ToRun]";
- LogError(Severity_Low, Error_Null, 0, "file to run not define");
- return Error_Null;
- }
- ErrorCodeEnum eErrorCode = Error_Succeed;
- auto list = strToRun.Split(',');
- for (int i = 0; i < list.GetCount(); i++)
- {
- auto &file = list[i];
- CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)file);
- if (ExistsFileA(strSourcePath))
- {
- // 如果是Wow64模式,需屏蔽System32重定向
- PVOID pRedirectOldValue(NULL);
- bool bIsWow64 = IsWow64Process();
- if (bIsWow64)
- {
- if (!Wow64DisableWow64FsRedirection(&pRedirectOldValue))
- Dbg("Wow64DisableWow64FsRedirection fail: %d", GetLastError());
- else
- Dbg("Wow64DisableWow64FsRedirection succ");
- }
-
- // xkm@20161114: 执行完成,从result.txt中读取安装结果
- system(strSourcePath);
- CSimpleStringA strResultLog = strUnzipPath + "\\result.log";
- if (!GetSysUpgradeResult(strResultLog, eErrorCode, strErrMsg))
- {
- Dbg("get upgrade result from result.log fail, %s", (const char*)strErrMsg);
- eErrorCode = Error_Succeed;
- strErrMsg = "";
- }
-
- // 如果是Wow64模式,恢复System32重定向
- if (bIsWow64)
- {
- if (!Wow64RevertWow64FsRedirection(pRedirectOldValue))
- Dbg("Wow64RevertWow64FsRedirection fail: %d", GetLastError());
- else
- Dbg("Wow64RevertWow64FsRedirection succ");
- }
- if (eErrorCode != Error_Succeed)
- {
- LogError(Severity_Low, Error_UpdateFailed, 0, CSimpleStringA::Format("run file [%s] error: %s(%d)", (const char*)file, (const char*)strErrMsg, eErrorCode));
- WriteInstallLog(pszPackageName, CSimpleStringA::Format("run file [%s] error: %s(%d)", (const char*)file, (const char*)strErrMsg, eErrorCode));
- return eErrorCode;
- }
- }
- else
- {
- strErrMsg = "指定执行文件不存在";
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to run not exists", (const char*)strSourcePath));
- WriteInstallLog(pszPackageName, CSimpleStringA::Format("file [%s] to run not exists", (const char*)strSourcePath));
- return Error_NotExist;
- }
- }
- WriteInstallLog(pszPackageName, "install sys pack succ");
- // 登录到安装记录文件中
- sp_env_t *env = sp_get_env();
- auto cfg = env->cfg;
- CSimpleStringA strPackName = pszPackageName;
- char *pszSysPacks = inifile_read_str(cfg->install_ini_path, "Main", "SysPack", "");
- CSimpleStringA strSysPacks = pszSysPacks;
- toolkit_free(pszSysPacks);
- if (!strSysPacks.IsNullOrEmpty())
- strSysPacks += ",";
- strSysPacks += pszPackageName;
- inifile_write_str(cfg->install_ini_path, "Main", "SysPack", strSysPacks);
- inifile_format_write(cfg->install_ini_path, strPackName, "InstalledDate", "0x%08X", y2k_time_now());
- inifile_write_str(cfg->install_ini_path, strPackName, "PackState", "U");
- Dbg("install sys pack [%s] succ", (const char*)strPackName);
- return Error_Succeed;
- }
- // 判断是否是已安装的系统外升级包
- ErrorCodeEnum SpEntityPrivilege::IsInstalledSysPack(const char *pszPackageName)
- {
- // 登录到安装记录文件中
- sp_env_t *env = sp_get_env();
- auto cfg = env->cfg;
- CSimpleStringA strPackName = pszPackageName;
- // 查询系统外升级包
- char *p = inifile_read_str(cfg->install_ini_path, "Main", "SysPack", "");
- CSimpleStringA strSysPackList = p;
- toolkit_free(p);
- if (!strSysPackList.IsNullOrEmpty() && (strSysPackList.IndexOf(strPackName)!= -1))
- {
- return Error_Succeed;
- }
- return Error_Unexpect;
- }
- ErrorCodeEnum SpEntityPrivilege::CopyFileToNewVersion(const char *pszRelativeFile, int nCopyMode)
- {
- auto env = sp_get_env();
- auto cfg = env->cfg;
-
- // 取到新版本安装包名称
- char *p = inifile_read_str(cfg->install_ini_path, "Main", "LatterInstallVersion", "");
- CSimpleStringA strNewVer = p;
- toolkit_free(p);
- assert(!strNewVer.IsNullOrEmpty());
- CSimpleStringA strNewInstallIni = CSimpleStringA::Format("%s\\%s\\install.ini", env->dir->root_ver_path, (const char*)strNewVer);
- assert(ExistsFileA(strNewInstallIni));
-
- p = inifile_read_str(strNewInstallIni, strNewVer, "InstallPack", "");
- CSimpleStringA strPackName = p;
- toolkit_free(p);
- assert(!strPackName.IsNullOrEmpty());
- // 取得临时解压目录
- CSimpleStringA strDownloadsPath;
- auto rc = GetPath("Downloads", strDownloadsPath);
- assert(rc == Error_Succeed);
- CSimpleStringA strUnzipPath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)strPackName);
- if (strUnzipPath.IsEndWith(".zip") || strUnzipPath.IsEndWith(".cab"))
- strUnzipPath = strUnzipPath.SubString(0, strUnzipPath.GetLength()-4);
-
- CSimpleStringA strCopyPath = pszRelativeFile;
- CSimpleStringA strSourceFile;
- CSimpleStringA strDestFile;
- if (strCopyPath.IsStartWith("Data\\"))
- {
- CSimpleStringA strAdPath;
- rc = GetPath("Ad", strAdPath);
- assert(rc == Error_Succeed);
- strSourceFile = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)strCopyPath);
- strDestFile = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)strCopyPath.SubString(5, strCopyPath.GetLength()-5));
- }
- else if (strCopyPath.IsStartWith("Run\\"))
- {
- strSourceFile = CSimpleStringA::Format("%s\\%s", (const char*)strUnzipPath, (const char*)strCopyPath);
- CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strNewVer);
- strDestFile = CSimpleStringA::Format("%s\\%s",(const char*) strNewVerPath, (const char*)strCopyPath.SubString(4, strCopyPath.GetLength()-4));
- }
- else
- {
- WriteInstallLog(strPackName, CSimpleStringA::Format("Copy file: [%s], result: [0x%X]", pszRelativeFile, Error_NoPrivilege));
- LogError(Severity_Low, Error_NoPrivilege, 0, CSimpleStringA::Format("invalid copy item: [%s], must start with \"Data\" or \"Run\"", (const char*)pszRelativeFile));
- return Error_NoPrivilege;
- }
-
- //assert(ExistsFileA(strSourceFile));
- if (!ExistsFileA(strSourceFile))
- {
- WriteInstallLog(strPackName, CSimpleStringA::Format("Copy file: [%s], result: [0x%X]", pszRelativeFile, Error_NotExist));
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("file [%s] to copy not exist", (const char*)strSourceFile));
- return Error_NotExist;
- }
- //#ifdef _DEBUG
- // bool bVerifySign = false;
- //#else
- // bool bVerifySign = true;
- //#endif
- //
- // if (bVerifySign && (strSourceFile.IsEndWith(".dll", true) || strSourceFile.IsEndWith(".exe", true)))
- // {
- // CSimpleStringA strErrMsg;
- // if (VerifySignature(strSourceFile, strErrMsg) != Error_Succeed)
- // {
- // WriteInstallLog(strPackName, CSimpleStringA::Format("file [%s] sign verify not pass: %s", pszRelativeFile, (const char*)strErrMsg));
- // LogError(Severity_Low, Error_FailVerify, 0, CSimpleStringA::Format("file [%s] sign verify not pass: %s", (const char*)strSourceFile, (const char*)strErrMsg));
- // return Error_FailVerify;
- // }
- // }
- // check dest dir if exists
- auto strDestDir = GetFileDirectory(strDestFile);
- if (!ExistsDirA(strDestDir))
- CreateDirA(strDestDir, TRUE);
-
- BOOL bDestFileExists = ExistsFileA(strDestFile);
- if (bDestFileExists)
- RemoveFileReadOnlyAttributeA(strDestFile);
- BOOL bRet = TRUE;
-
- // 1(copy only exisit); 2(copy only not exist); 3(copy always); 4(no copy if exist)
- switch (nCopyMode)
- {
- case 1:
- {
- if (bDestFileExists)
- bRet= CopyFileA(strSourceFile, strDestFile, FALSE);
- else
- LOG_TRACE("ignore file [%s] copy for dest file not exists", pszRelativeFile);
- }
- break;
- case 2:
- {
- if (!bDestFileExists)
- bRet= CopyFileA(strSourceFile, strDestFile, TRUE);
- else
- LOG_TRACE("ignore file [%s] copy for dest file exists", pszRelativeFile);
- }
- break;
- case 3:
- bRet= CopyFileA(strSourceFile, strDestFile, FALSE);
- break;
- case 4:
- {
- // 不发生拷贝,只进行判断
- if(bDestFileExists)
- LOG_TRACE("file [%s] exist", pszRelativeFile);
- else
- LOG_TRACE("file [%s] not exist", pszRelativeFile);
- }
- break;
- }
- if (bRet)
- RemoveFileReadOnlyAttributeA(strDestFile);
- rc = bRet ? Error_Succeed : Error_Unexpect;
- WriteInstallLog(strPackName, CSimpleStringA::Format("Copy file: [%s], result: [0x%X]", pszRelativeFile, rc));
- return rc;
- }
- // File*A.*
- bool SpEntityPrivilege::IsFileMatch(const char *pszFilter, const char *pszFileName)
- {
- if (stricmp(pszFilter, "*") == 0 || stricmp(pszFilter, "*.*") == 0)
- return true;
- const char *p1 = pszFilter;
- const char *p2 = pszFileName;
- if (p1 == NULL)
- return true;
- if (p2 == NULL)
- return false;
- if (*p1 ==NULL && *p2 ==NULL)
- return true;
- else if (*p1 == NULL)
- return false;
- else if (*p2 == NULL)
- {
- // 查找*p1是否全是*
- while(*p1 == '*')
- p1++;
- if (*p1 == NULL)
- return true;
- else
- return false;
- }
- if (*p1 != '*')
- {
- if (tolower(*p1) != tolower(*p2))
- return false;
- else
- return IsFileMatch(p1+1, p2+1);
- }
- else
- {
- while(*++p1 == '*');
- if (*p1 == NULL)
- return true;
- while (*p2)
- {
- while(tolower(*p1) != tolower(*p2) && *++p2);
- if (*p2 == NULL)
- return false;
- if(IsFileMatch(p1+1, p2+1))
- return true;
- p2++;
- }
- return false;
- }
- }
- CSimpleStringA SpEntityPrivilege::GetFileDirectory(const char *pszFullPath)
- {
- int i=strlen(pszFullPath);
- for( ; i>0 && pszFullPath[i-1]!='\\'; i--)NULL;
- return CSimpleStringA(pszFullPath, i);
- }
- ErrorCodeEnum SpEntityPrivilege::DeleteFileInNewVersion(const char *pszRelativeFile)
- {
- auto env = sp_get_env();
- auto cfg = env->cfg;
- // 取到新版本安装包名称
- char *p =inifile_read_str(cfg->install_ini_path, "Main", "LatterInstallVersion", "");
- CSimpleStringA strNewVer = p;
- toolkit_free(p);
- CSimpleStringA strNewInstallIni = CSimpleStringA::Format("%s\\%s\\install.ini", env->dir->root_ver_path, (const char*)strNewVer);
- assert(ExistsFileA(strNewInstallIni));
-
- p = inifile_read_str(strNewInstallIni, strNewVer, "InstallPack", "");
- CSimpleStringA strPackName = p;
- toolkit_free(p);
- assert(!strPackName.IsNullOrEmpty());
- CSimpleStringA strDelFile = pszRelativeFile;
- CSimpleStringA strDestFile;
- if (strDelFile.IsStartWith("Data\\"))
- {
- CSimpleStringA strAdPath;
- auto rc = GetPath("Ad", strAdPath);
- assert(rc == Error_Succeed);
- strDestFile = CSimpleStringA::Format("%s\\%s", (const char*)strAdPath, (const char*)strDelFile.SubString(5, strDelFile.GetLength()-5));
- }
- else if (strDelFile.IsStartWith("Run\\"))
- {
- CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strNewVer);
- assert(ExistsDirA(strNewVerPath));
- strDestFile = CSimpleStringA::Format("%s\\%s", (const char*)strNewVerPath, (const char*)strDelFile.SubString(4, strDelFile.GetLength()-4));
- }
- else
- {
- WriteInstallLog(strPackName, CSimpleStringA::Format("Delete file: [%s], result: [%d]", pszRelativeFile, Error_NoPrivilege));
- LogError(Severity_Low, Error_NoPrivilege, 0, CSimpleStringA::Format("invalid delete item: [%s], must start with \"Data\" or \"Run\"", (const char*)pszRelativeFile));
- return Error_NoPrivilege;
- }
- // 解析文件通配符*
- BOOL bRet = TRUE;
- int nIndex = strDestFile.IndexOf("*");
- if (nIndex > 0)
- {
- CSimpleStringA strFileFilter = _GetFileName(strDestFile);
- CSimpleStringA strDestDir = strDestFile.SubString(0, strDestFile.GetLength()-strFileFilter.GetLength()-1);
- Dbg("delete wildchar file [%s] in dir [%s]", (const char*)strFileFilter, (const char*)strDestDir);
- // 删除所有匹配文件
- auto arr = fileutil_get_sub_files(strDestDir);
- if (arr != NULL)
- {
- for (int i = 0; i < arr->nelts; ++i)
- {
- char *file = ARRAY_IDX(arr, i, char*);
-
- if (IsFileMatch(strFileFilter, _GetFileName(file)))
- {
- Dbg("delete match file: [%s]", file);
- bRet &= RemoveFileA(file);
- }
- else
- {
- Dbg("ignore not match file: [%s]", file);
- }
- }
- toolkit_array_free2(arr);
- }
- // xkm@20170302: 删除所有匹配子目录
- auto subdirs = fileutil_get_sub_dirs(strDestDir);
- if (subdirs != NULL)
- {
- for (int i = 0; i < subdirs->nelts; ++i)
- {
- const char *dir = ARRAY_IDX(subdirs, i, char*);
- if (IsFileMatch(strFileFilter, _GetFileName(dir)))
- {
- Dbg("delete match dir: [%s]", dir);
- bRet &= RemoveDirRecursive(dir);
- }
- else
- {
- Dbg("ignore not match dir: [%s]", dir);
- }
- }
- toolkit_array_free2(subdirs);
- }
- }
- else
- {
- if (ExistsFileA(strDestFile))
- {
- bRet = DeleteFileA(strDestFile);
- Dbg("delete file [%s] result: %d", strDestFile, bRet);
- }
- else if (ExistsDirA(strDestFile))
- {
- bRet = RemoveDirRecursive(strDestFile);
- Dbg("delete dir [%s] result: %d", strDestFile, bRet);
- }
- else
- {
- Dbg("delete file [%s] fail, not exists", pszRelativeFile);
- }
- }
- auto rc = bRet ? Error_Succeed : Error_Unexpect;
- WriteInstallLog(strPackName, CSimpleStringA::Format("Delete file: [%s], result: [%d]", pszRelativeFile, rc));
- return rc;
- }
- ErrorCodeEnum SpEntityPrivilege::RollBackToPreviousVersion()
- {
- auto env = sp_get_env();
- auto cfg = env->cfg;
- // 修改当前Install.ini配置
- auto &v = cfg->install_ini->install_version;
- CVersion curVersion(v.major, v.minor, v.revision, v.build);
- CInstallInfo info = {};
- auto rc = GetInstallInfo(curVersion, info);
- assert(rc == Error_Succeed);
- if (!info.PreviousInstallVersion.IsValid())
- {
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("current version [%s] has no previous version", (const char*)curVersion.ToString()));
- return Error_NotExist;
- }
- auto strPrevVersion = info.PreviousInstallVersion.ToString();
- // 检查对应文件夹是否存在
- CSimpleStringA strPreVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strPrevVersion);
- if (!ExistsDirA(strPreVerPath))
- {
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("previous version dir [%s] not exists", (const char*)strPreVerPath));
- return Error_NotExist;
- }
- // 修改当前版本状态为倒回R
- int nRet = inifile_write_str(cfg->install_ini_path, curVersion.ToString(), "InstallState", "R");
- // 更新回退次数
- auto nTotalRollbackCount = inifile_read_int(cfg->install_ini_path, curVersion.ToString(), "TotalRollbackCount", 0);
- nTotalRollbackCount++;
- nRet = inifile_write_int(cfg->install_ini_path, curVersion.ToString(), "TotalRollbackCount", nTotalRollbackCount);
- if (-1 == nRet)
- {
- LogError(Severity_Low, Error_Unexpect, 0, "inifile_write_int TotalRollbackCount");
- }
- LOG_TRACE("inifile_write_int, nTotalRollbackCount[%d]", nTotalRollbackCount);
- // 修改active.txt文件
- CSimpleStringA strActiveFile = CSimpleStringA::Format("%s\\active.txt", env->dir->root_ver_path);
- HANDLE hFile = ::CreateFileA(strActiveFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- BOOL bSetSucc = FALSE;
- if (hFile == INVALID_HANDLE_VALUE)
- {
- LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
- return Error_Unexpect;
- }
- DWORD dwWrittenLen(0);
- bSetSucc = WriteFile(hFile, (const char*)strPrevVersion, strPrevVersion.GetLength(), &dwWrittenLen, NULL);
- FlushFileBuffers(hFile);
- SetEndOfFile(hFile);
- CloseHandle(hFile);
- LOG_TRACE("set rollback to previous version [%s] succeed", (const char*)strPrevVersion);
- //出现active文件为空的问题,此处增加写文件件后再读一次,确认是否写成功,以此来确认是否是写文件导致
- if (1)
- {
- hFile = ::CreateFileA(strActiveFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
- return Error_Unexpect;
- }
- DWORD dwReadLen(0);
- char cVer[16] = { 0 };
- if (ReadFile(hFile, cVer, 16, &dwReadLen, NULL))
- {
- LOG_TRACE("read active.txt, version len[%d], version[%s]", dwReadLen, cVer);
- //如果为空,则重写
- if (strlen(cVer) == 0)
- {
- if (WriteFile(hFile, (const char*)strPrevVersion, strPrevVersion.GetLength(), &dwWrittenLen, NULL))
- {
- LOG_TRACE("rewrite active.txt success, strPrevVersion[%s]", strPrevVersion);
- }
- else
- {
- LogError(Severity_Low, Error_Unexpect, 0, "rewrite active.txt fail");
- }
- }
- }
- else
- {
- LogError(Severity_Low, Error_Unexpect, 0, "Read active.txt fail");
- }
- FlushFileBuffers(hFile);
- SetEndOfFile(hFile);
- CloseHandle(hFile);
- }
-
- return bSetSucc ? Error_Succeed : Error_Unexpect;
- }
- ErrorCodeEnum SpEntityPrivilege::RollBackToHistoryVersion(CVersion historyVersion)
- {
- auto env = sp_get_env();
- auto cfg = env->cfg;
- // 判断目标版本是否小于当前版本
- auto &v = cfg->install_ini->install_version;
- CVersion curVersion(v.major, v.minor, v.revision, v.build);
- if (curVersion <= historyVersion)
- {
- LogError(Severity_Low, Error_Param, 0, CSimpleStringA::Format("history version [%s] >= current version [%s]",
- (const char*)historyVersion.ToString(), (const char*)curVersion.ToString()));
- return Error_Param;
- }
- // 修改当前Install.ini配置
- CInstallInfo info= {};
- auto rc = GetInstallInfo(historyVersion, info);
- if (rc != Error_Succeed)
- {
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("history version [%s] to rollback not exist", (const char*)historyVersion.ToString()));
- return Error_NotExist;
- }
- // 检查对应文件夹是否存在
- CSimpleStringA strHisVersion = historyVersion.ToString();
- CSimpleStringA strHisVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strHisVersion);
- if (!ExistsDirA(strHisVerPath))
- {
- LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("history version dir [%s] not exists", (const char*)strHisVerPath));
- return Error_NotExist;
- }
- // 修改当前版本状态为倒回R
- int nRet = inifile_write_str(cfg->install_ini_path, curVersion.ToString(), "InstallState", "R");
- // 更新回退次数
- auto nTotalRollbackCount = inifile_read_int(cfg->install_ini_path, curVersion.ToString(), "TotalRollbackCount", 0);
- nTotalRollbackCount++;
- nRet = inifile_write_int(cfg->install_ini_path, curVersion.ToString(), "TotalRollbackCount", nTotalRollbackCount);
- if (-1 == nRet)
- {
- LogError(Severity_Low, Error_Unexpect, 0, "inifile_write_int TotalRollbackCount");
- }
- LOG_TRACE("inifile_write_int, nTotalRollbackCount[%d]", nTotalRollbackCount);
- // 修改active.txt文件
- CSimpleStringA strActiveFile = CSimpleStringA::Format("%s\\active.txt", env->dir->root_ver_path);
- HANDLE hFile = ::CreateFileA(strActiveFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- BOOL bSetSucc = FALSE;
- if (hFile == INVALID_HANDLE_VALUE)
- {
- LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
- return Error_Unexpect;
- }
-
- DWORD dwWrittenLen(0);
- bSetSucc = WriteFile(hFile, (const char*)strHisVersion, strHisVersion.GetLength(), &dwWrittenLen, NULL);
- FlushFileBuffers(hFile);
- SetEndOfFile(hFile);
- CloseHandle(hFile);
- //出现active文件为空的问题,此处增加写文件件后再读一次,确认是否写成功,以此来确认是否是写文件导致
- if (1)
- {
- hFile = ::CreateFileA(strActiveFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
- return Error_Unexpect;
- }
- DWORD dwReadLen(0);
- char cVer[16] = {0};
- if (ReadFile(hFile,cVer,16,&dwReadLen,NULL))
- {
- LOG_TRACE("read active.txt, version len[%d], version[%s]", dwReadLen, cVer);
- }
- else
- {
- LogError(Severity_Low, Error_Unexpect, 0, "Read active.txt fail");
- }
- CloseHandle(hFile);
- }
- LOG_TRACE("set rollback to history version [%s] succeed", (const char*)strHisVersion);
- return bSetSucc ? Error_Succeed : Error_Unexpect;
- }
- ErrorCodeEnum SpEntityPrivilege::UpgradeToNewVersion(bool bToSysInstall)
- {
- auto env = sp_get_env();
- auto cfg = env->cfg;
-
- // 修改当前Install.ini配置
- auto &v = cfg->install_ini->install_version;
- CVersion curVersion(v.major, v.minor, v.revision, v.build);
- int nRet = inifile_write_str(cfg->install_ini_path, curVersion.ToString(), "InstallState", "U"); // 已升级
- // 取到新版本
- char *p = inifile_read_str(cfg->install_ini_path, "Main", "LatterInstallVersion", "");
- CSimpleStringA strNewVer = p;
- toolkit_free(p);
- assert(!strNewVer.IsNullOrEmpty());
- if (strNewVer.IsNullOrEmpty())
- {
- LogError(Severity_Low, Error_Unexpect, 0, "install.ini Main LatterInstallVersion is null");
- return Error_Unexpect;
- }
- CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", env->dir->root_ver_path, (const char*)strNewVer);
-
- // 修改新版本install.ini
- auto strNewInstallIni = CSimpleStringA::Format("%s\\install.ini", (const char*)strNewVerPath);
- assert(ExistsFileA(strNewInstallIni));
- // 取新版本安装包名称
- p = inifile_read_str(strNewInstallIni, strNewVer, "InstallPack", "");
- CSimpleStringA strPackName = p;
- toolkit_free(p);
- assert(!strPackName.IsNullOrEmpty());
- WriteInstallLog(strPackName, "Upgrade to new version");
- nRet &= inifile_write_str(strNewInstallIni, curVersion.ToString(), "InstallState", "U"); // 已升级
- nRet &= inifile_write_str(strNewInstallIni, strNewVer, "InstallState", "S"); // 设置启动
- // 修改active.txt配置
- CSimpleStringA strActiveFile = CSimpleStringA::Format("%s\\active.txt", env->dir->root_ver_path);
- HANDLE hFile = ::CreateFileA(strActiveFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- BOOL bSetSucc = (nRet == 0);
- if (hFile != INVALID_HANDLE_VALUE)
- {
- DWORD dwWrittenLen(0);
- bSetSucc = bSetSucc && WriteFile(hFile, (const char*)strNewVer, strNewVer.GetLength(), &dwWrittenLen, NULL);
- FlushFileBuffers(hFile);
- SetEndOfFile(hFile);
- CloseHandle(hFile);
- LOG_TRACE("WriteFile [%s] to active.txt", (const char*)strNewVer);
- }
- else
- {
- bSetSucc = FALSE;
- LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
- }
- //出现active文件为空的问题,此处增加写文件件后再读一次,确认是否写成功,以此来确认是否是写文件导致
- if (1)
- {
- hFile = ::CreateFileA(strActiveFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- LogError(Severity_Low, Error_Unexpect, 0, "open active.txt fail");
- return Error_Unexpect;
- }
- DWORD dwReadLen(0);
- char cVer[16] = {0};
- if (ReadFile(hFile,cVer,16,&dwReadLen,NULL))
- {
- LOG_TRACE("read active.txt, version len[%d], version[%s]", dwReadLen, cVer);
- }
- else
- {
- LogError(Severity_Low, Error_Unexpect, 0, "Read active.txt fail");
- }
- CloseHandle(hFile);
- }
- // 修改内存配置
- if (bSetSucc)
- {
- sp_version_t newVersion = {};
- sscanf((const char*)strNewVer, "%d.%d.%d.%d", &newVersion.major, &newVersion.minor, &newVersion.revision, &newVersion.build);
- auto newVerInfo = sp_cfg_find_version_info(cfg, &newVersion);
- if (newVerInfo != NULL)
- newVerInfo->install_state = Install_SetToStart;
- LOG_TRACE("set new version [%s] active", (const char*)strNewVer);
- return Error_Succeed;
- }
- else
- {
- LogError(Severity_Low, Error_Unexpect, 0, "switch to new version fail");
- }
- return Error_Unexpect;
- }
- ErrorCodeEnum SpEntityPrivilege::SetRunSucceed()
- {
- sp_env_t *env = sp_get_env();
- auto cfg = env->cfg;
-
- // 修改内存状态
- auto pVerInfo = sp_cfg_find_version_info(cfg, &cfg->install_ini->install_version);
- pVerInfo->install_state = Install_Active; // 安装成功
- // 设置后一版本状态为倒回
- if (sp_version_is_valid(&cfg->install_ini->latter_install_version))
- {
- // 修改后一版本安装文件状态为倒回
- auto &latterVer = cfg->install_ini->latter_install_version;
- char tmp[MAX_PATH];
- auto rc = sp_dir_get_path_version(env->dir, latterVer.major, latterVer.minor, latterVer.revision, latterVer.build, SP_DIR_INSTALL_INI,
- NULL, tmp, MAX_PATH);
- if (rc != 0)
- {
- sp_dbg_warn("get install.ini path failed!");
- return (ErrorCodeEnum)rc;
- }
- if (ExistsFileA(tmp)) // 可能后续版本目录被删除
- {
- // 倒回状态
- CVersion lVer(latterVer.major, latterVer.minor, latterVer.revision, latterVer.build);
- inifile_write_str(tmp, (const char*)lVer.ToString(), "InstallState", "R");
- // 修改内存状态
- auto pLatterVerInfo = sp_cfg_find_version_info(cfg, &latterVer);
- if (pLatterVerInfo != NULL)
- pLatterVerInfo->install_state = Install_RollBack;
- // 修改对应安装包日志
- auto p = inifile_read_str(tmp, (const char*)lVer.ToString(), "InstallPack", "");
- CSimpleStringA strPackName = p;
- toolkit_free(p);
- assert(!strPackName.IsNullOrEmpty());
- auto &curVer = cfg->install_ini->install_version;
- WriteInstallLog(strPackName, CSimpleStringA::Format("Rollback to version: [%d.%d.%d.%d]",
- curVer.major, curVer.minor, curVer.revision, curVer.build));
- }
- }
- // 设置当前版本安装状态为活动
- char *pCurVersion = inifile_read_str(cfg->install_ini_path, "Main", "InstallVersion", "");
- inifile_write_str(cfg->install_ini_path, pCurVersion, "InstallState", "A");
- inifile_format_write(cfg->install_ini_path, pCurVersion, "SwitchOverDate", "0x%08X", y2k_time_now());
- auto pPackName = inifile_read_str(cfg->install_ini_path, pCurVersion, "InstallPack", "");
- CSimpleStringA strPackName = pPackName;
- toolkit_free(pCurVersion);
- toolkit_free(pPackName);
-
- // 修改安装包历史
- if (!strPackName.IsNullOrEmpty())
- WriteInstallLog(strPackName, "Set version state active");
-
- // 拷贝升级包到Upgraded目录
- const char *pPackFile = pVerInfo->install_pack;
- CSimpleStringA strDownloadPath, strUpgradedPath;
- GetPath("Downloads", strDownloadPath);
- GetPath("Upgraded", strUpgradedPath);
- CSimpleStringA strSourcePath = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadPath, pPackFile);
- CSimpleStringA strDestPath = CSimpleStringA::Format("%s\\%s", (const char*)strUpgradedPath, pPackFile);
- CopyFileA(strSourcePath, strDestPath, FALSE);
- return Error_Succeed;
- }
- ErrorCodeEnum SpEntityPrivilege::IsPackInstalled(const char *pszPackName, bool &bInstalled)
- {
- if (pszPackName == NULL)
- {
- bInstalled = false;
- return Error_Param;
- }
- // 先从内存查找
- auto env = sp_get_env();
- auto cfg = env->cfg;
- bInstalled = sp_cfg_is_pack_installed(cfg, pszPackName) == 0;
- if (bInstalled)
- return Error_Succeed;
- // 再从安装文件目录查找
- CSimpleStringA strPath;
- GetPath("RunInfo", strPath);
-
- CSimpleStringA strInstallLog = CSimpleStringA::Format("%s\\InstallLog\\%s.log", (const char*)strPath, pszPackName);
- bInstalled = ExistsFileA(strInstallLog) == TRUE;
- return Error_Succeed;
- }
- ErrorCodeEnum SpEntityPrivilege::GetSystemStaticInfoForVersion(CVersion verSoftware,CSystemStaticInfo &Info)
- {
- auto env = sp_get_env();
- auto cfg = env->cfg;
- auto root_ini = cfg->root_ini;
- memset(&Info, 0, sizeof(Info));
- Info.strTerminalID = CSimpleStringA(root_ini->terminal_no);
- Info.strMachineType = CSimpleStringA(root_ini->machine_type);
- //Info.strMachineModel = CSimpleStringA(root_ini->machine_model);
- Info.MachineVersion = CVersion(root_ini->machine_version.major, root_ini->machine_version.minor, root_ini->machine_version.revision, root_ini->machine_version.build);
- Info.eScreen = (ScreenEnum)root_ini->screen;
- Info.strSite = root_ini->site;
- Info.strEnrolAddr = root_ini->enroll_address;
- Info.EnrolGPS = CSphereVector(root_ini->enroll_gps_x, root_ini->enroll_gps_y);
- CSimpleStringA strInstallIni = CSimpleStringA::Format("%s\\%s\\install.ini", env->dir->root_ver_path, (const char*)verSoftware.ToString());
- if (!ExistsFileA(strInstallIni))
- return Error_NotExist;
- int n1(0), n2(0), n3(0), n4(0);
- auto p = inifile_read_str(strInstallIni, "Main", "LatterInstallVersion", "");
- int n = sscanf(p, "%d.%d.%d.%d", &n1, &n2, &n3, &n4);
- toolkit_free(p);
- if (n == 4)
- Info.LatterInstallVersion = CVersion(n1, n2, n3, n4);
- else
- Info.LatterInstallVersion = CVersion(0, 0, 0, 0);
-
- //Info.LatterInstallPack = inifile_read_int(strInstallIni, "Main", "LatterInstallPack", 0);
- Info.nTotalRunCount = inifile_read_int(strInstallIni, "Main", "TotalRunCount", 0);
- Info.nTodayRunCount = inifile_read_int(strInstallIni, "Main", "TodayRunCount", 0);
- Info.tmCreateDate = CSmallDateTime(inifile_read_int(strInstallIni, "Main", "CreateDate", 0));
- Info.tmCurrentTime = CSmallDateTime(inifile_read_int(strInstallIni, "Main", "CurrentTime", 0));
- p = inifile_read_str(strInstallIni, "Main", "InstallVersion", "");
- n = sscanf(p, "%d.%d.%d.%d", &n1, &n2, &n3, &n4);
- toolkit_free(p);
-
- if (n != 4)
- return Error_Unexpect;
-
- Info.InstallVersion = CVersion(n1, n2, n3, n4);
- auto strInstallVersion = Info.InstallVersion.ToString();
- p = inifile_read_str(strInstallIni, strInstallVersion, "PreviousInstallVersion", "");
- n = sscanf(p, "%d.%d.%d.%d", &n1, &n2, &n3, &n4);
- toolkit_free(p);
- if (n == 4)
- Info.PreviousInstallVersion = CVersion(n1, n2, n3, n4);
- else
- Info.PreviousInstallVersion = CVersion(0, 0, 0, 0);
- Info.tmSwithOverDate = CSmallDateTime(inifile_read_int(strInstallIni, strInstallVersion, "SwitchOverDate", 0));
-
- p = inifile_read_str(strInstallIni, strInstallVersion, "InstallPack", "");
- Info.InstallPack = p;
- toolkit_free(p);
- Info.InstallState = (InstallStateEnum)read_ini_install_state(strInstallIni, strInstallVersion, "InstallState");
- // light pack
- p = inifile_read_str(strInstallIni, "Main", "LightPack", "");
- CSimpleStringA strLightPacks = p;
- toolkit_free(p);
- if (!strLightPacks.IsNullOrEmpty())
- {
- auto list = strLightPacks.Split(',');
- Info.LightPackInfos.Init(list.GetCount());
- for (int i = 0; i < list.GetCount(); ++i)
- {
- auto &strPackName = list[i];
- Info.LightPackInfos[i].strPackName = strPackName;
- Info.LightPackInfos[i].State = (InstallStateEnum)read_ini_install_state(strInstallIni, strPackName, "PackState");
- Info.LightPackInfos[i].tmInstalledDate = CSmallDateTime(inifile_read_int(strInstallIni, strPackName, "InstalledDate", 0));
- }
- }
- return Error_Succeed;
- }
- ErrorCodeEnum SpEntityPrivilege::SetSysDebugLevel(const char *pszEntityName,DebugLevelEnum eDebugLevel,bool bPersist)
- {
- sp_env_t *env = sp_get_env();
- if (!pszEntityName)
- return Error_Null;
- if (stricmp(pszEntityName, "SpShell") == 0)
- env->cfg->shell_ini->shell_debug_level = (int)eDebugLevel;
- sp_cfg_shell_entity_t *cfg_ent = sp_cfg_get_entity_by_name(env->cfg, pszEntityName);
- if (cfg_ent) {
- int old = cfg_ent->debug_level;
- if (old != eDebugLevel) {
- cfg_ent->debug_level = (int)eDebugLevel;
- if (bPersist) {
- return SpTranslateError(sp_cfg_refresh_debug_level(env->cfg, cfg_ent));
- }
- }
- } else {
- return Error_NotExist;
- }
- return Error_Succeed;
- }
- ErrorCodeEnum SpEntityPrivilege::ShowOuputConsole()
- {
- ErrorCodeEnum Error;
- iobuffer_t *pkt = iobuffer_create(-1, -1);
- Error = PostInfoShell(SHELL_CMD_INFO_OUTPUT_CONSOLE_ON, &pkt);
- if (pkt)
- iobuffer_dec_ref(pkt);
- return Error;
- }
- ErrorCodeEnum SpEntityPrivilege::CloseOuputConsole()
- {
- ErrorCodeEnum Error;
- iobuffer_t *pkt = iobuffer_create(-1, -1);
- Error = PostInfoShell(SHELL_CMD_INFO_OUTPUT_CONSOLE_OFF, &pkt);
- if (pkt)
- iobuffer_dec_ref(pkt);
- return Error;
- }
- void SpEntityPrivilege::on_entity_create(int entity_id, int trigger_entity_id)
- {
- if (m_pEntityLifeListener) {
- sp_env_t *env = sp_get_env();
- IEntityLifeListener *pCallback = (IEntityLifeListener *)sp_mod_entity_life_listener_get_tag(m_pEntityLifeListener);
- sp_mod_mgr_t *mod_mgr = env->mod_mgr;
- sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, entity_id);
- sp_entity_t *trigger_ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, trigger_entity_id);
- pCallback->OnCreated(ent->cfg->name, Error_Succeed, trigger_ent->cfg->name);
- }
- }
- void SpEntityPrivilege::on_entity_close(int entity_id, int trigger_entity_id, int cause_code)
- {
- if (m_pEntityLifeListener) {
- sp_env_t *env = sp_get_env();
- IEntityLifeListener *pCallback = (IEntityLifeListener *)sp_mod_entity_life_listener_get_tag(m_pEntityLifeListener);
- sp_mod_mgr_t *mod_mgr = env->mod_mgr;
- sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, entity_id);
- sp_entity_t *trigger_ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, trigger_entity_id);
- pCallback->OnClosed(ent->cfg->name, (EntityCloseCauseEnum)cause_code, Error_Succeed, trigger_ent->cfg->name);
- }
- }
- void SpEntityPrivilege::on_entity_exception(int entity_id, int error, int entity_state)
- {
- if (m_pEntityLifeListener) {
- sp_env_t *env = sp_get_env();
- IEntityLifeListener *pCallback = (IEntityLifeListener *)sp_mod_entity_life_listener_get_tag(m_pEntityLifeListener);
- sp_mod_mgr_t *mod_mgr = env->mod_mgr;
- sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(mod_mgr, entity_id);
- pCallback->OnException(ent->cfg->name, "", (EntityStateEnum)entity_state, (EntityStateEnum)entity_state, (ErrorCodeEnum)error);
- }
- }
- void SpEntityPrivilege::__on_entity_create(sp_mod_entity_life_listener_t *listener, int entity_id, int trigger_entity_id, void *user_data)
- {
- SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
- pThis->on_entity_create(entity_id, trigger_entity_id);
- }
- 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)
- {
- SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
- pThis->on_entity_close(entity_id, trigger_entity_id, cause_code);
- }
- void SpEntityPrivilege::__on_entity_exception(sp_mod_entity_life_listener_t *listener, int entity_id, int error, int entity_state, void *user_data)
- {
- SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
- pThis->on_entity_exception(entity_id, error, entity_state);
- }
- void SpEntityPrivilege::on_user_state(sp_mod_entity_state_listener_t *listener, int entity_id, int last_state, int curr_state)
- {
- sp_env_t *env = sp_get_env();
- IEntityStateListener *pListener = (IEntityStateListener *)sp_mod_entity_listener_get_tag(listener);
- sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, entity_id);
- pListener->OnUserStateHook(ent->cfg->name, (DWORD)curr_state, (DWORD)last_state);
- }
- 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)
- {
- sp_env_t *env = sp_get_env();
- IEntityStateListener *pListener = (IEntityStateListener *)sp_mod_entity_listener_get_tag(listener);
- sp_entity_t *ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, entity_id);
- sp_entity_t *trigger_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, trigger_entity_id);
- pListener->OnEntityStateHook(ent->cfg->name, trigger_ent->cfg->name, (EntityStateEnum)curr_state, (EntityStateEnum)last_state);
- }
- void SpEntityPrivilege::on_create_connection(sp_mod_entity_state_listener_t *listener, int src_entity_id, int dst_entity_id)
- {
- sp_env_t *env = sp_get_env();
- IEntityStateListener *pListener = (IEntityStateListener *)sp_mod_entity_listener_get_tag(listener);
- sp_entity_t *src_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, src_entity_id);
- sp_entity_t *dst_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, dst_entity_id);
- pListener->OnCeateConnection(src_ent->cfg->name, dst_ent->cfg->name);
- }
- void SpEntityPrivilege::on_close_connection(sp_mod_entity_state_listener_t *listener, int src_entity_id, int dst_entity_id)
- {
- sp_env_t *env = sp_get_env();
- IEntityStateListener *pListener = (IEntityStateListener *)sp_mod_entity_listener_get_tag(listener);
- sp_entity_t *src_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, src_entity_id);
- sp_entity_t *dst_ent = sp_mod_mgr_find_entity_by_idx(env->mod_mgr, dst_entity_id);
- pListener->OnCloseConnection(src_ent->cfg->name, dst_ent->cfg->name);
- }
- void SpEntityPrivilege::__on_user_state(sp_mod_entity_state_listener_t *listener, int entity_id, int last_state, int curr_state, void *user_data)
- {
- SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
- pThis->on_user_state(listener, entity_id, last_state, curr_state);
- }
- 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)
- {
- SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
- pThis->on_entity_state(listener, entity_id, trigger_entity_id, last_state, curr_state);
- }
- void SpEntityPrivilege::__on_create_connection(sp_mod_entity_state_listener_t *listener, int src_entity_id, int dst_entity_id, void *user_data)
- {
- SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
- pThis->on_create_connection(listener, src_entity_id, dst_entity_id);
- }
- void SpEntityPrivilege::__on_close_connection(sp_mod_entity_state_listener_t *listener, int src_entity_id, int dst_entity_id, void *user_data)
- {
- SpEntityPrivilege *pThis = static_cast<SpEntityPrivilege*>(user_data);
- pThis->on_close_connection(listener, src_entity_id, dst_entity_id);
- }
|