UpdatePublic.cpp
Upload User: dzyhzl
Upload Date: 2019-04-29
Package Size: 56270k
Code Size: 35k
Development Platform:

C/C++

  1. //////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FileName    :   UpdatePublic.cpp
  4. //  Version     :   1.0
  5. //  Creater     :   Cheng Bitao
  6. //  Date        :   2002-1-17 11:15:44
  7. //  Comment     :   Inplement the comment interface of update system
  8. //
  9. //////////////////////////////////////////////////////////////////////////////////////
  10.                         
  11. #include "Stdafx.h"    
  12. #include "Global.h"
  13. #include "UpdatePublic.h"
  14. #include "SaveLog.h"
  15. #include "UpdateSelf.h"
  16. #include "KAVStrTranslate.h"
  17. #include "GenKAVMoveProgram.h"
  18. #include "GetVersion.h"
  19. #include "GetFilesVersion.h"
  20. #include "PublicFun.h"
  21. #include "ProcessIndex.h"
  22. #include "UpdateData.h"
  23. #include "Resource.h"
  24. #include "SourceDef.h"
  25. CProcessIndex g_ProcessIndex;
  26. extern KUPDATE_DATA g_UpdateData;
  27. ///////////////////////////////////////////////////////////////////////////////
  28. //更新包合并 Add By Fellow 2003.08.11
  29. #define MAX_FILE 2004800 //最多20万个文件
  30. char temp_buffer[MAX_FILE] = {0};
  31. int getIndex(t_index_info* IndexList,int Count, unsigned long id)
  32. {//二分法找到指定的索引
  33. int nBegin, nEnd, nMid;
  34. nBegin = 0;
  35. nEnd = Count;
  36. while (nBegin <= nEnd) {
  37. nMid = (nBegin + nEnd) / 2;
  38. if (id == IndexList[nMid].id) break;
  39. if (id < IndexList[nMid].id) nEnd = nMid - 1;
  40. else nBegin = nMid + 1;
  41. }
  42. if(id != IndexList[nMid].id) return -1; //数据文件里面也没有
  43. return nMid;
  44. }
  45. void GetHeader(t_pack_header* aHeader, FILE* aFile)
  46. {
  47. fseek(aFile,0,SEEK_SET);
  48. fread(aHeader,1,sizeof(t_pack_header),aFile);
  49. }
  50. void GetIndexInfo(t_index_info* aIndexInfo, const t_pack_header* aHeader, FILE* aFile)
  51. {
  52. fseek(aFile,aHeader->index_offset,SEEK_SET);
  53. fread(aIndexInfo, sizeof(t_index_info), aHeader->count, aFile);
  54. }
  55. bool PackComb(const char* stdFilePath,const char* addFilePath)
  56. {//合并文件
  57. t_pack_header stdHeader;
  58. t_pack_header addHeader;
  59. unsigned long offset;
  60. t_index_info* stdIndex;
  61. t_index_info* addIndex;
  62. //打开资源源文件
  63. FILE* stdFile =NULL;
  64. stdFile = fopen(stdFilePath, "r+b");
  65. if(stdFile == NULL)
  66. {
  67. return false;
  68. }
  69. GetHeader(&stdHeader, stdFile);
  70. //打开资源新增加文件
  71. FILE* addFile =NULL;
  72. addFile = fopen(addFilePath, "rb");
  73. if(addFile == NULL)
  74. {
  75. return false;
  76. }
  77. GetHeader(&addHeader, addFile);
  78. stdIndex =  new t_index_info[stdHeader.count + addHeader.count];
  79. memset(stdIndex,0,sizeof(t_index_info) * (stdHeader.count + addHeader.count));
  80. GetIndexInfo(stdIndex, &stdHeader, stdFile);
  81. addIndex =  new t_index_info[addHeader.count];
  82. memset(addIndex,0,sizeof(t_index_info) * addHeader.count);
  83. //如果addFile里面没有需要增加的数据就直接完成
  84. if (addHeader.count == 0)
  85. {//关闭文件
  86. fclose(stdFile);
  87. fclose(addFile);
  88. return true;
  89. }
  90. GetIndexInfo(addIndex, &addHeader, addFile);
  91. //copy增加的数据到源文件中
  92. int DataSize = addHeader.index_offset - addHeader.data_offset;
  93. char* DataBuffer = new char[DataSize];
  94. fseek(addFile,addHeader.data_offset,SEEK_SET);
  95. fread(DataBuffer, 1, DataSize, addFile);
  96. fseek(stdFile,0,SEEK_END);
  97. offset = ftell(stdFile);
  98. char *ptr = DataBuffer;
  99. while(DataSize > 0) {
  100. int result;
  101. if(DataSize > 0x100000) result = fwrite(ptr ,1,0x100000,stdFile);
  102. else result = fwrite(ptr ,1, DataSize,stdFile);
  103. DataSize -= result;
  104. ptr += result;
  105. }
  106. //重新建立index
  107. int i,j;
  108. for(i = 0; i < addHeader.count; i++)
  109. {
  110. t_index_info* index = &addIndex[i];
  111. for(j = 0; j < stdHeader.count; j++) {
  112. if(index->id < stdIndex[j].id)
  113. { //索引排序
  114. memcpy(temp_buffer, (char *)&stdIndex[j], (stdHeader.count - j) * sizeof(t_index_info));
  115. memcpy((char *)&stdIndex[j + 1], temp_buffer, (stdHeader.count - j) * sizeof(t_index_info));
  116. //添加新索引
  117. memcpy(&stdIndex[j], index, sizeof(t_index_info));
  118. stdIndex[j].offset = offset + index->offset - sizeof(t_pack_header);
  119. ++stdHeader.count;
  120. break;
  121. }
  122. else if(index->id == stdIndex[j].id)
  123. { //新索引替换旧索引
  124. stdIndex[j].offset = offset + index->offset - sizeof(t_pack_header);
  125. stdIndex[j].size = index->size;
  126. stdIndex[j].compress_size = index->compress_size;
  127. break;
  128. }
  129. }
  130. if(j == stdHeader.count) {
  131. memcpy(&stdIndex[j], index, sizeof(t_index_info));
  132. stdIndex[j].offset = offset + index->offset - sizeof(t_pack_header);//Add By Fellow 09.04
  133. stdHeader.count++;
  134. }
  135. }
  136. //写入新的index
  137. fseek(stdFile,0,SEEK_END);
  138. offset = ftell(stdFile);
  139. fwrite(stdIndex,sizeof(t_index_info),stdHeader.count,stdFile);
  140. //写入header
  141. stdHeader.index_offset = offset;
  142. fseek(stdFile,0,SEEK_SET);
  143. fwrite(&stdHeader,sizeof(t_pack_header),1,stdFile);
  144. //关闭文件
  145. delete [] DataBuffer;
  146. delete [] stdIndex;
  147. delete [] addIndex;
  148. fclose(stdFile);
  149. fclose(addFile);
  150. return true;
  151. }
  152. ////////////////////////////////////////////////////////////////////////////////
  153. static int _SetRebootProcess_WINME()
  154. {
  155.     char szFileName[MAX_PATH]       = {0};
  156.     char szTempName[MAX_PATH]       = {0};
  157.     char szShortFileName[MAX_PATH]  = {0};
  158.     char szWinINIFileName[MAX_PATH] = {0};
  159.     
  160.     ::GetShortPathName(g_PathTable.szTempPath, szShortFileName, MAX_PATH);
  161.     strcat(szShortFileName, defUPDATE_FILE_EXE_FILE_NAME_ME);
  162.     strcpy(szFileName, szShortFileName);
  163.     if (!GenCopyFileProgram(szFileName))
  164.         return false;
  165.     
  166.     strcpy(szWinINIFileName, g_PathTable.szWindowsPath);
  167.     strcat(szWinINIFileName, "Win.ini");
  168.     ::WritePrivateProfileString("windows", "Run", szFileName, szWinINIFileName);
  169.     return true;
  170. }
  171. //--------------------------------------------------------------------------------------
  172. int ProcessIndexFile(const char cszFileName[])
  173. {
  174.     int nResult                     = false;
  175.     int nRetCode                    = 0;
  176.     char szIndexTxtFile[MAX_PATH]   = {0};
  177.     CString sLog                    = "";
  178.     if (g_UpdateData.nMethod == defUPDATE_METHOD_LAN)
  179.         strcpy(szIndexTxtFile, g_PathTable.szTempPath);
  180.     else
  181.         strcpy(szIndexTxtFile, g_PathTable.szDownloadDestPath);
  182.     strcat(szIndexTxtFile, defINDEX_TXT_FILE_NAME);
  183.     nRetCode = g_ProcessIndex.Init(cszFileName, szIndexTxtFile);
  184.     if (!nRetCode)
  185.     {
  186.         if (g_UpdateData.nMethod == defUPDATE_METHOD_LAN)
  187.             sLog = defIDS_ERROR_INDEX;
  188.         else 
  189.             sLog = defIDS_INDEX_ERROR;
  190.         g_UpdateData.SaveLog.WriteLogString(sLog, true);
  191.         goto Exit0;
  192.     }
  193.     
  194.     nRetCode = g_ProcessIndex.AnalysisUpdateFiles(        
  195.         g_UpdateData.nMethod,                  
  196.         g_UpdateData.szLocalPath, 
  197.         g_UpdateData.nMainVersion
  198.     );
  199.     if (!nRetCode)
  200.     {
  201.          if (g_UpdateData.nMethod == defUPDATE_METHOD_LAN)
  202.             sLog = defIDS_ERROR_INDEX;
  203.         else 
  204.             sLog = defIDS_INDEX_ERROR;
  205.         g_UpdateData.SaveLog.WriteLogString(sLog, true);
  206.         goto Exit0;
  207.     }
  208.   
  209. if(nRetCode ==defVersionNotenough || nRetCode ==defVersionMore)
  210. {
  211. return nRetCode;
  212. }
  213.     g_UpdateData.bNeedUpdateSelfFirst   = g_ProcessIndex.IsNeedUpdateSelf();
  214.     g_UpdateData.bVersionError      = g_ProcessIndex.IsVersionError();
  215.     g_UpdateData.sAnnounce      = g_ProcessIndex.GetAnnounce();
  216.     g_UpdateData.sReadme        = g_ProcessIndex.GetReadme();
  217.     // Count file size of Program or engie and sign or tools and help         
  218.     nResult = true;
  219. Exit0:    
  220.     return nResult;
  221. }
  222. //////////////////////////////////////////////////////////////////////////////////
  223. // Update file to local
  224. //////////////////////////////////////////////////////////////////////////////////
  225. int inline _GetTempFileName(char szFileName[])
  226. {
  227.     int i               = 0;
  228.     int nFileNameLen    = 0;
  229.     int nStrSize        = 0;
  230.     char *pszStr        = NULL;
  231.     int nRetCode        = 0;
  232.     
  233.     nFileNameLen    = strlen(szFileName);
  234.     pszStr          = szFileName + nFileNameLen;
  235.     nStrSize        = MAX_PATH - nFileNameLen;   
  236.     while (i < INT_MAX)
  237.     {
  238.         _IntToStr(i, nStrSize, pszStr);
  239.         nRetCode = FileExists(szFileName);
  240.         if (!nRetCode)
  241.             break;
  242.         i++;
  243.     }
  244.     
  245.     return true;
  246. }
  247. static int inline _GetOSPlatformVersion()
  248. {
  249.     OSVERSIONINFOEX     osVersionInfo;  
  250.     int nRetCode = 0;
  251.     int nResult  = -1;
  252.     ZeroMemory(&osVersionInfo, sizeof(OSVERSIONINFOEX));
  253.     osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  254.     nRetCode = ::GetVersionEx((OSVERSIONINFO *)&osVersionInfo);
  255.     if (!nRetCode)
  256.     {
  257.         osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  258.         nRetCode = ::GetVersionEx((OSVERSIONINFO *)&osVersionInfo);
  259.         KAV_PROCESS_ERROR(nRetCode);
  260.     }
  261.   
  262.     switch(osVersionInfo.dwPlatformId)
  263.     {
  264.     case VER_PLATFORM_WIN32_NT:
  265.         nResult = defOSPLATFORM_WINNT;
  266.         break;
  267.     case VER_PLATFORM_WIN32_WINDOWS:
  268.         if (((osVersionInfo.dwBuildNumber >> 16) & 0x0000FFFF) < 0x045A)
  269.             nResult = defOSPLATFORM_WIN9598;
  270.         else 
  271.             nResult = defOSPLATFORM_WINME;
  272.         break;
  273.     default:
  274.         nResult = 0;
  275.         break;
  276.     }
  277.     
  278. Exit0:
  279.     return nResult;
  280. }
  281. int CloseProgram(const char cszFileName[])
  282. {
  283.     int nResult = false;
  284.     int nRetCode = 0;
  285.     KModuleExitInfo ModuleExitInfo;
  286.     KBaseClosedProgram *pClosedProgram = NULL;
  287.     KProcessInfo ProcessInfo;
  288.     ModuleExitInfo.nExitType = -1;
  289.     strcpy(ModuleExitInfo.szFileName, cszFileName);
  290.     ModuleExitInfo.uID = 0;
  291.     ModuleExitInfo.szStartParam[0] = '';
  292.     ModuleExitInfo.szStopParam[0] = '';
  293. ModuleExitInfo.szReleaseFileName[0] = '';
  294.     nRetCode = g_ProcessIndex.GetExitType(&ModuleExitInfo);    
  295.     KAV_PROCESS_ERROR(nRetCode);
  296.     
  297.     ProcessInfo.uID = ModuleExitInfo.uID;
  298.     strcpy(ProcessInfo.szFileName, ModuleExitInfo.szReleaseFileName);
  299.     strcpy(ProcessInfo.szStartParam, ModuleExitInfo.szStartParam);
  300.     strcpy(ProcessInfo.szCloseParam, ModuleExitInfo.szStopParam);
  301.     switch (ModuleExitInfo.nExitType)
  302.     {
  303.     case defEXIT_TYPE_DIRECTORY:
  304.         pClosedProgram = new KClosedProgram(ProcessInfo);
  305.         KAV_PROCESS_ERROR(pClosedProgram);   
  306.         break;
  307.     default:
  308.         goto Exit0;
  309.     }
  310.     
  311.     nRetCode = g_UpdateData.CloseProgramMgr.AddClosedProgram(pClosedProgram);
  312.     if (!nRetCode)
  313.     {
  314.         delete pClosedProgram;
  315.         pClosedProgram = NULL;
  316.     }
  317.     nResult = pClosedProgram->CloseProgram();
  318.     
  319. Exit0:
  320.     return nResult;
  321. }
  322. int CloseCalledPrograms(const char cszCallByNames[])
  323. {
  324.     char szCallBy[MAX_PATH] = {0};
  325.     char *pszCallBy = NULL;
  326.     char *pszTemp = NULL;
  327.     char *pszPointer = NULL;
  328.     strcpy(szCallBy, cszCallByNames);
  329.     pszTemp = szCallBy;
  330.     while (*pszTemp != '')
  331.     {
  332.         pszCallBy = pszTemp;
  333.         pszPointer = strchr(pszTemp, ',');
  334.         if (pszPointer)
  335.         {
  336.             pszTemp = pszPointer + 1;
  337.             *pszPointer = '';
  338.             CloseProgram(pszCallBy);  
  339.         }
  340.         else
  341.         {
  342.             CloseProgram(pszCallBy);
  343.             break;
  344.         }         
  345.     }   
  346.     return true;
  347. }
  348. BOOL AddRenameSectionContent(CString sPathName, CString sContent)
  349. {
  350.     CFile file;
  351.     CString sOld, sNew, sPart1, sPart2;
  352.     if (FileExists(sPathName))
  353.     {
  354.         if (file.Open(sPathName, CFile::modeRead | CFile::shareDenyNone))
  355.         {
  356.             int nLen = file.GetLength();
  357.             char *pBuf = new char [nLen + 1];
  358.             file.Read(pBuf, nLen);
  359.             pBuf[nLen] = '';
  360.             sOld = pBuf;
  361.             KAV_DELETE_ARRAY(pBuf);
  362.             file.Close();
  363.         }
  364.         else
  365.             return FALSE;
  366.     }
  367.     else
  368.     {
  369.         sOld = "";
  370.     }
  371.     CString sTemp = sOld;
  372.     CString sTemp2 = sContent;
  373.     
  374.     sTemp.MakeLower();
  375.     sTemp2.MakeLower();
  376.     
  377.     if (sTemp.Find(sTemp2) != -1)
  378.         return true;
  379.     
  380.     int nPos = sTemp.Find("[rename]rn");
  381.     if (nPos != -1)
  382.     {
  383.         sPart1 = sOld.Left(nPos + 10);
  384.         sPart2 = sOld.Mid(nPos + 10);
  385.         if (sContent.Right(2) != "rn")
  386.             sContent += "rn";
  387.         sNew = sPart1 + sContent + sPart2;
  388.     }
  389.     else
  390.     {
  391.         sPart1 = sOld;
  392.         sPart2 = "rn[rename]rn";
  393.         
  394.         sNew = sPart1 + sPart2 + sContent;
  395.     }
  396.     if (file.Open(sPathName, CFile::modeCreate | CFile::modeWrite))
  397.     {
  398.         file.Write(sNew, sNew.GetLength());
  399.         file.Close();
  400.     }
  401.     else
  402.         return FALSE;
  403.     return TRUE;
  404. }
  405. int KAVSetupInstallFile(const char cszCleanedFileName[], const char cszLockedFileName[])
  406. {
  407.     int nResult = false;
  408.     int nRetCode = false;
  409.     char szTempFileName[MAX_PATH] = {0};
  410.     char szTemp[MAX_PATH] = {0};
  411.     CString szWININITFileName;
  412.     char *pszPos = NULL;
  413.     CString szBuf;
  414.     char szCleanedFileShortName[MAX_PATH] = {0};
  415.     char szLockedFileShortName[MAX_PATH] = {0};
  416.     ::GetShortPathName(cszCleanedFileName, szCleanedFileShortName, MAX_PATH);
  417.     ::GetShortPathName(cszLockedFileName, szLockedFileShortName, MAX_PATH);
  418.     strcpy(szTempFileName, szLockedFileShortName);
  419.     pszPos = strrchr(szTempFileName, '.');
  420.     pszPos++;
  421.     *pszPos = '';
  422.     strcat(pszPos, "TMP");
  423.     
  424.     nRetCode = ::CopyFile(szCleanedFileShortName, szTempFileName, FALSE);
  425.     KAV_PROCESS_ERROR(nRetCode);
  426.     
  427.     szBuf = "";
  428.     szBuf += "NUL=" + (CString)szLockedFileShortName + "rn";
  429.     szBuf += (CString)szLockedFileShortName + "=" + (CString)szTempFileName + "rn";
  430.     ::GetShortPathName(g_PathTable.szWindowsPath, szTemp, MAX_PATH);
  431.     szWININITFileName = szTemp;
  432.     szWININITFileName += "Wininit.ini";
  433.     ::AddRenameSectionContent(szWININITFileName, szBuf);
  434.     
  435.     nResult = true;
  436. Exit0:
  437.     return nResult;
  438. }
  439. // cszDestFileName : Dest file name, need update
  440. // cszTempFileName : temp file
  441. int ProcessSharingFile(const char cszDestFileName[], const char cszTempFileName[])
  442. {
  443.     int nResult                         = false;
  444.     int nRetCode                        = 0;
  445.     char szLocalTempFileName[MAX_PATH]  = {0};
  446.     char szLockedFileShortName[MAX_PATH]= {0};
  447.     HANDLE hFile                        = INVALID_HANDLE_VALUE;
  448.     int nWritten                        = 0;
  449.     char szBuf[MAX_PATH]                = {0};
  450.     CString szCopyIniFileName;    
  451.     CFile File;
  452.     CString sAttrib_FileName;
  453.     CString sKAVMOVE_BAT_FileName;
  454.     CString sKAVMove_EXE_FileName;
  455.     CString sCleanedFileShortName;
  456.     CString sLockedFileShortName;
  457.     CString sExeFilePathShort;
  458.     CString sBuffer;
  459.     int nCount = 0;
  460.     if (g_UpdateData.nOSPlatVersion == -1)        // Get the operation system platform ID
  461.         g_UpdateData.nOSPlatVersion = _GetOSPlatformVersion();
  462.     switch (g_UpdateData.nOSPlatVersion)
  463.     {
  464.     case defOSPLATFORM_WINNT:                 // WINNT/Windows2000
  465.         nRetCode = ::SetFileAttributes(cszDestFileName, FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_NORMAL);
  466.         KAV_PROCESS_ERROR(nRetCode);
  467.         
  468.         strcpy(szLocalTempFileName, cszDestFileName);
  469.         _GetTempFileName(szLocalTempFileName); 
  470.         nRetCode = ::CopyFile(cszTempFileName, szLocalTempFileName, false);
  471.         KAV_PROCESS_ERROR(nRetCode);
  472.             
  473.         //::DeleteFile(cszTempFileName);
  474.         nRetCode = ::MoveFileEx(
  475.             szLocalTempFileName, cszDestFileName, 
  476.             MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT
  477.         );
  478.         KAV_PROCESS_ERROR(nRetCode);
  479.         break;
  480.     case defOSPLATFORM_WIN9598:        // Windows 95/98/ME  
  481.     case defOSPLATFORM_WINME:          // Windows ME
  482.         if (strlen(strrchr(cszTempFileName, '\')) <= 13)
  483.         {
  484.             nRetCode = KAVSetupInstallFile(cszTempFileName, cszDestFileName);
  485.             KAV_PROCESS_ERROR(nRetCode);
  486.             break;
  487.         }
  488.         if (g_UpdateData.nOSPlatVersion == defOSPLATFORM_WIN9598)
  489.         {
  490.             ::SetFileAttributes(cszDestFileName, FILE_ATTRIBUTE_NORMAL);
  491.             ::GetShortPathName((CString)g_PathTable.szWindowsPath, szBuf, MAX_PATH);
  492.             sExeFilePathShort = szBuf;
  493.             sExeFilePathShort += "Command\";
  494.             sAttrib_FileName = sExeFilePathShort + "Attrib";
  495.             ::GetShortPathName(g_PathTable.szTempPath, szBuf, MAX_PATH);
  496.             AddPathChar(szBuf);
  497.             sExeFilePathShort = szBuf;
  498.             sKAVMOVE_BAT_FileName = sExeFilePathShort + defUPDATE_FILE_BAT_FILE_NAME;
  499.             sKAVMove_EXE_FileName = sExeFilePathShort + defUPDATE_FILE_EXE_FILE_NAME;             
  500.             if (!FileExists(sKAVMove_EXE_FileName))
  501.                 GenKAVMoveProgram(sKAVMove_EXE_FileName);
  502.             GetShortPathName(cszDestFileName, szBuf, MAX_PATH);
  503.             sLockedFileShortName = szBuf;
  504.             nRetCode = File.Open(sKAVMOVE_BAT_FileName, CFile::modeCreate | CFile::modeReadWrite | CFile::modeNoTruncate, NULL);
  505.             KAV_PROCESS_ERROR(nRetCode);
  506.             File.SeekToEnd();
  507.             ::GetShortPathName(cszTempFileName, szBuf, MAX_PATH);
  508.             sCleanedFileShortName = szBuf;
  509.             if (FileExists(sKAVMove_EXE_FileName))
  510.             {
  511.                 if (cszTempFileName[0] != '')
  512.                     sBuffer = "@if exist " + sKAVMove_EXE_FileName + " " + sKAVMove_EXE_FileName + " " + sCleanedFileShortName + " " + sLockedFileShortName + "rn";
  513.                 else 
  514.                     sBuffer = "@if exist " + sKAVMove_EXE_FileName + " " + sKAVMove_EXE_FileName + " " + sLockedFileShortName + "rn";
  515.             }
  516.             else
  517.             {
  518.                 if (cszTempFileName[0] != '')
  519.                 {
  520.                     sBuffer = "@if exist " + sLockedFileShortName + " " + sAttrib_FileName + " -s -h -r " + sLockedFileShortName + "rn";
  521.                     sBuffer += "@if exist " + sCleanedFileShortName + " copy /y " + sCleanedFileShortName + " " + sLockedFileShortName + "rn";
  522.                     sBuffer += "@if exist " + sCleanedFileShortName + " del " + sCleanedFileShortName + "rn";
  523.                 }
  524.                 else 
  525.                 {
  526.                     sBuffer = "if exist " + sLockedFileShortName + " " + sAttrib_FileName + " -s -h -r " + sLockedFileShortName + "rn";
  527.                     sBuffer += "@if exist " + sLockedFileShortName + " del " + sLockedFileShortName + "rn";
  528.                 }
  529.             }
  530.             nCount = sBuffer.GetLength();
  531.             File.Write(sBuffer, nCount);
  532.             File.Close();
  533.         }
  534.         else
  535.         {
  536.             ::SetFileAttributes(cszDestFileName, FILE_ATTRIBUTE_NORMAL);
  537.             szCopyIniFileName = (CString)g_PathTable.szTempPath + defUPDATE_FILES_INI_FILE_NAME;
  538.             strcpy(szLockedFileShortName, strrchr(cszDestFileName, '\') + 1);                
  539.             if (cszTempFileName)
  540.             {
  541.                 ::WritePrivateProfileString(szLockedFileShortName, "src", cszTempFileName, szCopyIniFileName);
  542.                 ::WritePrivateProfileString(szLockedFileShortName, "dst", cszDestFileName, szCopyIniFileName);
  543.             }
  544.             else 
  545.             {
  546.                 ::WritePrivateProfileString(szLockedFileShortName, "src", cszDestFileName, szCopyIniFileName);
  547.             }
  548.         }
  549.         
  550.         break;
  551.     }       // End of switch
  552.     
  553.     nResult = true;
  554. Exit0:    
  555.     return nResult;
  556. }
  557. int UpdateSelf()
  558. {
  559.     int nResult                         = false;
  560.     int nRetCode                        = 0;
  561.     char szUpdateSelfFile[MAX_PATH]     = {0};
  562.     char szUpdateSelfInfoFile[MAX_PATH] = {0};
  563.     char szParameter[MAX_PATH]          = {0};
  564.     char szDstFile[MAX_PATH]            = {0};
  565.     char szKeyName[30]                  = {0};
  566.     CString sLog                        = "";
  567.     PKUPDATE_ITEM pUpdateItem           = NULL;
  568.     int nNum                            = 0;
  569.     if (!g_UpdateData.bNeedUpdateSelfFirst)
  570.         goto Exit0;
  571.     // Get full path name for the Update Self information file
  572.     strcpy(szUpdateSelfInfoFile, g_PathTable.szDownloadDestPath);  
  573. MkDirEx(szUpdateSelfInfoFile);
  574.     strcat(szUpdateSelfInfoFile, defUPDATE_SELF_INFO_FILE);
  575.     // Delete the old information file
  576.     ::DeleteFile(szUpdateSelfInfoFile);
  577.     
  578.     nNum = 0;
  579.     pUpdateItem = g_ProcessIndex.m_pUpdateItemList;
  580.     while (pUpdateItem)
  581.     {
  582.         // Download temp file (srcfile)
  583.         sprintf(szKeyName, "SrcFile%d", nNum);
  584.         WritePrivateProfileString(
  585.             "UpdateSelf",
  586.         szKeyName,
  587.         pUpdateItem->szDownloadTmpFileName,
  588.         szUpdateSelfInfoFile
  589.         );
  590.         
  591.         //  DestFile
  592.         sprintf(szKeyName, "DesFile%d", nNum);
  593.         strcpy(szDstFile, pUpdateItem->szLocalPath);
  594.         strcat(szDstFile, pUpdateItem->szFileName);
  595.         WritePrivateProfileString(
  596.         "UpdateSelf",
  597.         szKeyName,
  598.         szDstFile,
  599.         szUpdateSelfInfoFile
  600.         );
  601.         pUpdateItem = pUpdateItem->pNext;
  602.         nNum++;
  603.     }
  604.     // Parameter
  605.     strcpy(szParameter, "/CONTINUE");
  606.     strcat(szParameter, " ");
  607.     strcat(szParameter, g_UpdateData.szParameter);
  608.     WritePrivateProfileString(
  609. "UpdateSelf",
  610. "Parameter",
  611. szParameter,
  612. szUpdateSelfInfoFile
  613.     );
  614.     strcpy(szDstFile, g_PathTable.szUpdateDestPath);
  615.     strcat(szDstFile, g_UpdateData.szExecuteProgram);
  616.     WritePrivateProfileString(
  617. "UpdateSelf",
  618. "FullPathName",
  619. szDstFile,
  620. szUpdateSelfInfoFile
  621.     );
  622.     // Create UpdateSelf.DAT file for Update self
  623.     strcpy(szUpdateSelfFile, g_PathTable.szDownloadDestPath);
  624.     strcat(szUpdateSelfFile, defUPDATE_SELF_FILE);
  625.     nRetCode = CreateUpdateSelfFile(szUpdateSelfFile);        
  626.     if (nRetCode)
  627. {
  628. sLog = defIDS_UPDATE_SELF_SUCCESS;
  629. nResult = true;
  630. }
  631.     else
  632. {
  633. sLog = defIDS_UPDATE_SELF_FAILED;
  634. }
  635.     g_UpdateData.SaveLog.WriteLogString(sLog, true);
  636. Exit0:
  637.     return nResult;
  638. }
  639. int UpdateFile_Copy(const KUPDATE_ITEM& UpdateItem)
  640. {
  641.     int nResult                         = false;
  642.     int nRetCode                        = false;
  643.     char szLocalFileName[MAX_PATH]      = {0};
  644.     char szLocalTempFileName[MAX_PATH]  = {0};
  645.     CString sLog;
  646.     CString sFormat;
  647.     DWORD dwFileAttribute               = 0;
  648.     strcpy(szLocalFileName, UpdateItem.szLocalPath);
  649.     strcat(szLocalFileName, UpdateItem.szFileName); 
  650.     MkDirEx(UpdateItem.szLocalPath);
  651. TRACE1("%s is copiedn", szLocalFileName);
  652.     dwFileAttribute = GetFileAttributes(szLocalFileName);
  653.     dwFileAttribute &= ~dwFileAttribute;
  654.     SetFileAttributes(szLocalFileName, dwFileAttribute);
  655. dwFileAttribute = GetFileAttributes(UpdateItem.szDownloadTmpFileName);
  656. dwFileAttribute &= ~dwFileAttribute;
  657. SetFileAttributes(UpdateItem.szDownloadTmpFileName, dwFileAttribute);
  658.     nRetCode = ::CopyFile(UpdateItem.szDownloadTmpFileName, szLocalFileName, false);
  659.     if (nRetCode)
  660.     {
  661.         nResult = CheckFileCRC(atoi(UpdateItem.szCRC), szLocalFileName);        
  662.         if (!nResult)
  663.         {
  664.             g_UpdateData.bUpdateFailed = true;
  665.             sFormat = defIDS_UPDATE_FAILED;
  666.             goto Exit0;
  667.         }
  668.         if (UpdateItem.nReboot)
  669.         {
  670.             g_UpdateData.bNeedRebootFalg = true;
  671.             sFormat = defIDS_NEED_RESET;
  672.         }
  673.         else
  674.         {
  675.             sFormat = defIDS_UPDATE_SUCCESS;
  676.         }     
  677. ::DeleteFile(UpdateItem.szDownloadTmpFileName);
  678.         goto Exit0;
  679.     }
  680.     // Update after reset computer 
  681.     strcpy(szLocalTempFileName, g_PathTable.szTempPath);
  682.     strcat(szLocalTempFileName, UpdateItem.szFileName);
  683.     _GetTempFileName(szLocalTempFileName);
  684.     nRetCode = ::CopyFile(UpdateItem.szDownloadTmpFileName, szLocalTempFileName, false);
  685.     if (!nRetCode)
  686.     {
  687.         sFormat = defIDS_COPY_TEMPFILE_FAILED;        
  688.         g_UpdateData.bUpdateFailed = true;
  689.         goto Exit0;
  690.     }
  691.     
  692.     nRetCode = ProcessSharingFile(szLocalFileName, szLocalTempFileName);
  693.     if (!nRetCode)
  694.     {
  695.         sFormat = defIDS_PROCESS_SHARING_FAILED;
  696.         
  697.         g_UpdateData.bUpdateFailed = true;
  698.         goto Exit0;
  699.     }
  700.     
  701.     nResult = CheckFileCRC(atoi(UpdateItem.szCRC), szLocalTempFileName);
  702.     if (!nResult)
  703.     {
  704.         g_UpdateData.bUpdateFailed = true;
  705.         sFormat = defIDS_UPDATE_FAILED;
  706.         goto Exit0;
  707.     }
  708.     
  709.     g_UpdateData.bRebootFinishUpdateFlag = true;
  710.     if (UpdateItem.nSharedNeedReboot || UpdateItem.nReboot)
  711.     {
  712.         sFormat = defIDS_UPDATE_SUCCESS_NEED_RESET;
  713.         g_UpdateData.bNeedRebootFalg = true;
  714.     }
  715.     else
  716.     {
  717.         sFormat = defIDS_UPDATE_SHARING_SUCCESS;
  718.     }    
  719.     
  720. Exit0:
  721.     sLog.Format(sFormat, UpdateItem.szFileName, UpdateItem.szLocalPath);
  722.     g_UpdateData.SaveLog.WriteLogString(sLog, true);
  723.     return nResult;
  724. }
  725. int UpdateFile_Package(const KUPDATE_ITEM& UpdateItem)
  726. {
  727. int nResult = false;
  728. char szLocalFileName[MAX_PATH]      = {0};
  729. DWORD dwFileAttribute               = 0;
  730. strcpy(szLocalFileName, UpdateItem.szLocalPath);
  731. strcat(szLocalFileName, UpdateItem.szFileName); 
  732. MkDirEx(UpdateItem.szLocalPath);
  733. TRACE1("%s is packagedn", szLocalFileName);
  734. dwFileAttribute = GetFileAttributes(szLocalFileName);
  735. dwFileAttribute &= ~dwFileAttribute;
  736. SetFileAttributes(szLocalFileName, dwFileAttribute);
  737. dwFileAttribute = GetFileAttributes(UpdateItem.szDownloadTmpFileName);
  738. dwFileAttribute &= ~dwFileAttribute;
  739. SetFileAttributes(UpdateItem.szDownloadTmpFileName, dwFileAttribute);
  740. if (FileExists(szLocalFileName))
  741. {
  742. typedef int (__stdcall *COMBINAPACKAGE)(const char*, char*);
  743. HMODULE hModule = LoadLibrary("ExpandPackage.dll");
  744. CString sFormat = defIDS_UPDATE_FAILED;
  745. if (hModule)
  746. {
  747. COMBINAPACKAGE pFunCombinaPackage = (COMBINAPACKAGE)GetProcAddress(hModule, "CombinatPackage");
  748. if (pFunCombinaPackage)
  749. {
  750. //nResult = pFunCombinaPackage(UpdateItem.szDownloadTmpFileName, szLocalFileName);
  751. nResult = PackComb(static_cast<const char *>(szLocalFileName),
  752. static_cast<const char *>(UpdateItem.szDownloadTmpFileName));
  753. if (!nResult)
  754. {
  755. g_UpdateData.bUpdateFailed = true;
  756. sFormat = defIDS_UPDATE_FAILED;
  757. }
  758. else
  759. {
  760. if (UpdateItem.nReboot)
  761. {
  762. g_UpdateData.bNeedRebootFalg = true;
  763. sFormat = defIDS_NEED_RESET;
  764. }
  765. else
  766. {
  767. sFormat = defIDS_UPDATE_SUCCESS;
  768. }
  769. ::DeleteFile(UpdateItem.szDownloadTmpFileName);
  770. }
  771. }
  772. ::FreeLibrary(hModule);
  773. }
  774. else
  775. {
  776. g_UpdateData.bUpdateFailed = true;
  777. sFormat = defIDS_UPDATE_FAILED;
  778. }
  779. CString sLog;
  780. sLog.Format(sFormat, UpdateItem.szDownloadTmpFileName, szLocalFileName);
  781. g_UpdateData.SaveLog.WriteLogString(sLog, true);
  782. }
  783. else
  784. {
  785. nResult = UpdateFile_Copy(UpdateItem);
  786. }
  787. return nResult;
  788. }
  789. int UpdateFile(KUPDATE_ITEM& UpdateItem)
  790. {
  791. char szMethod[MAX_PATH];
  792. strncpy(szMethod, UpdateItem.szUpdateFileMethod, MAX_PATH);
  793. char *pMethod = szMethod;
  794. int i = 0;
  795. int l = strlen(szMethod);
  796. while (i <= l)
  797. {
  798. if (szMethod[i] == ';' || szMethod[i] == 0)
  799. {
  800. *(pMethod + i) = 0;
  801. if (*pMethod == 0)
  802. break;
  803. if (strnicmp(pMethod, "Copy", 4) == 0)
  804. UpdateFile_Copy(UpdateItem);
  805. else if (strnicmp(pMethod, "Package", 7) == 0)
  806. {
  807. if (*(pMethod + 7) == '(')
  808. {
  809. int x = 8;
  810. while(*(pMethod + x) != ')')
  811. {
  812. UpdateItem.szFileName[x - 8] = *(pMethod + x);
  813. x++;
  814. }
  815. UpdateItem.szFileName[x - 8] = 0;
  816. }
  817. UpdateFile_Package(UpdateItem);
  818. }
  819. else
  820. ASSERT(0);
  821. pMethod = szMethod + i + 1;
  822. }
  823. i++;
  824. }
  825. return true;
  826. }
  827. int _DeleteSignFileInWin9X()
  828. {
  829.     int nRetCode = 0;
  830.     int nMaxSignFileNum = 0;
  831.     char szFileName[MAX_PATH];
  832.     char szShortFileName[MAX_PATH];
  833.     int nFileNameLen = 0;
  834.     CString sBuf;
  835.     int i;
  836.     // Delete redundent sign file
  837.     nMaxSignFileNum = GetLocalDatFileNumber();     // Get Max sign file number
  838.     strcpy(szFileName, g_PathTable.szUpdateDestPath);
  839.     strcat(szFileName, defBASE_SIGN_FILENAME);
  840.     nFileNameLen = strlen(szFileName);
  841.     nFileNameLen -= 9;
  842.     sBuf = "";
  843.     for (i = 0; i < nMaxSignFileNum; i++)
  844.     {
  845.         _IntToNStr(i, szFileName + nFileNameLen, 5);
  846.         nRetCode = FileExists(szFileName);
  847.         if (nRetCode)
  848.         {
  849.             ::GetShortPathName(szFileName, szShortFileName, MAX_PATH);
  850.             sBuf += "NUL=" + (CString)szShortFileName + "rn";            
  851.         } 
  852.     }
  853.     strcpy(szFileName, g_PathTable.szUpdateDestPath);
  854.     strcat(szFileName, defBASE_SIGN_UPDNAME);
  855.     nFileNameLen = strlen(szFileName);
  856.     nFileNameLen -= 9;
  857.     for (i = 0; i <= nMaxSignFileNum; i++)
  858.     {
  859.         _IntToNStr(i, szFileName + nFileNameLen, 5);
  860.         nRetCode = FileExists(szFileName);
  861.         if (nRetCode)
  862.         {
  863.             ::GetShortPathName(szFileName, szShortFileName, MAX_PATH);
  864.             sBuf += "NUL=" + (CString)szShortFileName + "rn"; 
  865.         }               
  866.     } 
  867.     strcpy(szFileName, g_PathTable.szWindowsPath);
  868.     strcat(szFileName, "Wininit.ini");
  869.     ::GetShortPathName(szFileName, szShortFileName, MAX_PATH);
  870.     
  871.     VERIFY(AddRenameSectionContent(szShortFileName, sBuf));  
  872.     return true;
  873. }
  874. static int _SetRebootProcess_WIN9X()
  875. {
  876.     int nRetCode = 0;
  877.     DWORD dwFileLen = 0;
  878.     CString sProcessFileArray[2];
  879.     CString sExeFilePathShort;
  880.     CString sKAVMOVE_BAT_FileName;
  881.     CString sKAVMOVE_EXE_FileName;
  882.     CString sCommandString;
  883.     CString sStartFileStrings;
  884.     CFile File;
  885.     char szBuf[MAX_PATH];
  886.     char *pszBuffer = NULL;
  887.     HANDLE Handle = INVALID_HANDLE_VALUE;
  888.     sProcessFileArray[0] = "C:\Autoexec.bat";
  889.     sProcessFileArray[1] = (CString)g_PathTable.szWindowsPath + "WinStart.bat";
  890.      
  891.     ::GetShortPathName((CString)g_PathTable.szTempPath, szBuf, MAX_PATH);
  892.     AddPathChar(szBuf);
  893.     sExeFilePathShort       = szBuf;
  894.     sKAVMOVE_BAT_FileName   = sExeFilePathShort + defUPDATE_FILE_BAT_FILE_NAME;
  895.     sKAVMOVE_EXE_FileName   = sExeFilePathShort + defUPDATE_FILE_EXE_FILE_NAME;
  896.     sCommandString = "rn@if exist " + sKAVMOVE_BAT_FileName + " call " + sKAVMOVE_BAT_FileName + "rn";
  897.     sCommandString += "if exist " + sKAVMOVE_BAT_FileName + " del " + sKAVMOVE_BAT_FileName + "rn";
  898.     sCommandString += "if exist " + sKAVMOVE_EXE_FileName + " del " + sKAVMOVE_EXE_FileName + "rn";
  899.     if (!FileExists(sKAVMOVE_EXE_FileName))
  900.         GenKAVMoveProgram(sKAVMOVE_EXE_FileName);
  901.     for (int i = 0; i < 2; i++)
  902.     {
  903.         nRetCode = File.Open(sProcessFileArray[i], CFile::modeNoTruncate | CFile::modeCreate | CFile::modeReadWrite, NULL);
  904.         if (nRetCode)
  905.         {
  906.             sStartFileStrings.Empty();
  907.             dwFileLen = File.GetLength();
  908.             if (dwFileLen > 0)
  909.             {
  910.                 pszBuffer = new char[dwFileLen + 1];
  911.                 if (pszBuffer)
  912.                 {
  913.                     nRetCode = File.Read(pszBuffer, dwFileLen);
  914.                     if ((DWORD)nRetCode == dwFileLen)
  915.                         sStartFileStrings = pszBuffer;
  916.                     KAV_DELETE_ARRAY(pszBuffer);
  917.                 }
  918.             }
  919.             nRetCode = sStartFileStrings.Find(sCommandString, 0);
  920.             if (nRetCode == -1)
  921.             {
  922.                 File.SeekToEnd();
  923.                 File.Write(sCommandString, sCommandString.GetLength());
  924.             }
  925.             File.Close();
  926.         }
  927.     }   // End of for    
  928.     
  929.     return true;
  930. }
  931. void _DeleteSignFileInWinNT()
  932. {
  933.     int nRetCode                = 0;
  934.     int nMaxSignFileNum         = 0;
  935.     int i                       = 0;
  936.     int nSignFileNum            = 0;
  937.     int nFileNameLen            = 0;
  938.     char szFileName[MAX_PATH]   = {0};
  939.     char szTemp[MAX_PATH]       = {0};
  940.     KUPDATE_ITEM *pUpdateItem   = NULL;
  941.     CString szPathName;
  942.     nMaxSignFileNum = GetLocalDatFileNumber();     // Get Max sign file number
  943.     strcpy(szTemp, g_PathTable.szUpdateDestPath);
  944.     ::GetShortPathName(szTemp, szFileName, MAX_PATH);
  945.     strcat(szFileName, defBASE_SIGN_FILENAME);
  946.     nFileNameLen = strlen(szFileName);
  947.     nFileNameLen -= 9;
  948.     for (i = 0; i < nMaxSignFileNum; i++)
  949.     {
  950.         _IntToNStr(i, szFileName + nFileNameLen, 5);
  951.         nRetCode = FileExists(szFileName);
  952.         if (nRetCode)
  953.             ::MoveFileEx(szFileName, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
  954.     }
  955.     ::GetShortPathName(szTemp, szFileName, MAX_PATH);
  956.     strcat(szFileName, defBASE_SIGN_UPDNAME);
  957.     nFileNameLen = strlen(szFileName);
  958.     nFileNameLen -= 9;
  959.     for (i = 0; i <= nMaxSignFileNum; i++)
  960.     {
  961.         _IntToNStr(i, szFileName + nFileNameLen, 5);
  962.         nRetCode = FileExists(szFileName);
  963.         if (nRetCode)
  964.             ::MoveFileEx(szFileName, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
  965.     }
  966. }
  967. int UpdateFiles()
  968. {
  969.     KUPDATE_ITEM *pUpdateItem   = NULL;
  970.     int nRetCode                = 1;
  971.     if (g_UpdateData.bNeedUpdateSelfFirst)
  972.     {
  973.         nRetCode = UpdateSelf();         
  974.         return nRetCode;
  975.     }
  976.     pUpdateItem = g_ProcessIndex.m_pUpdateItemList;
  977. if (pUpdateItem) // Close program before update file
  978. CloseCalledPrograms(g_ProcessIndex.m_szCallBy);
  979. KUPDATE_ITEM *pLastUpdateItem  = NULL;
  980.     while (pUpdateItem)
  981.     {
  982.         if (
  983.             (pUpdateItem->DownloadStatus == enumDOWNLOADSTATUS_DOWNLOADED) &&
  984.             pUpdateItem->bNeedUpdate
  985.         )
  986. {
  987. if (strnicmp(pUpdateItem->szUpdateFileMethod, "Last;", 5) == 0 &&
  988. pLastUpdateItem == NULL) //the first last copy
  989. {
  990. pLastUpdateItem = pUpdateItem;
  991. memmove(pLastUpdateItem->szUpdateFileMethod, pLastUpdateItem->szUpdateFileMethod + 5, MAX_PATH - 5);
  992. }
  993.             else if (!UpdateFile(*pUpdateItem))
  994. nRetCode = 0;
  995. }
  996.         pUpdateItem = pUpdateItem->pNext;
  997.     }
  998. if (pLastUpdateItem && !g_UpdateData.bUpdateFailed)
  999. {
  1000. if (!UpdateFile(*pLastUpdateItem))
  1001. nRetCode = 0;
  1002. }
  1003.     if (g_UpdateData.bRebootFinishUpdateFlag)
  1004.     {
  1005.         if ((g_UpdateData.nOSPlatVersion == defOSPLATFORM_WIN9598) || 
  1006.             (g_UpdateData.nOSPlatVersion == defOSPLATFORM_WINME)
  1007.         )
  1008.         {
  1009.             if (g_UpdateData.nOSPlatVersion == defOSPLATFORM_WIN9598)
  1010.                 _SetRebootProcess_WIN9X();
  1011.             else
  1012.                 _SetRebootProcess_WINME();
  1013.         }
  1014.     }
  1015.     return nRetCode;
  1016. }
  1017. int GetHostURL(const char cszFileName[], const char cszHostName[], char szHostURL[])
  1018. {
  1019. CStdioFile file;
  1020. int nResult = false;
  1021. if (file.Open(cszFileName, CFile::modeRead | CFile::typeText))
  1022. {
  1023. CString sName, sAddress;
  1024. while (file.ReadString(sName))
  1025. {
  1026. sName.TrimLeft();
  1027. sName.TrimRight();
  1028. if (sName == "")
  1029. continue;
  1030.             while (file.ReadString(sAddress))
  1031. {
  1032. sAddress.TrimLeft();
  1033. sAddress.TrimRight();
  1034. if (sAddress == "")
  1035. continue;
  1036. if (stricmp(cszHostName, sName) == 0)
  1037. {
  1038. strcpy(szHostURL, sAddress);
  1039. nResult = true;
  1040. break;
  1041. }
  1042.                 break;
  1043. }
  1044. }
  1045. file.Close();
  1046. return nResult;
  1047. }