AUDataManage.cpp
13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include "stdafx.h"
#include "AUDataManage.h"
#include "afxinet.h"
#include "tlhelp32.h"
#include "tinyxml.h"
#include ".\zlib\zip.h"
#include ".\zlib\unzip.h"
CAUDataManage* CAUDataManage::m_pInstance = NULL;
void Log(CString sLog)
{
CStdioFile fileLog;
CString sPath;
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, MAX_PATH);
CString sMudolePath = szPath;
CString sMudoleName = AfxGetAppName();
sPath = sMudolePath.Left(sMudolePath.GetLength() - sMudoleName.GetLength() - 4);
CFileStatus statusFile;
statusFile.m_size = 0;
CString sLogPath = sPath + "AutoUpdateLog.txt";
CFile::GetStatus(sLogPath, statusFile);
CTime tmCurTime;
tmCurTime = CTime::GetCurrentTime();
CString sData = tmCurTime.Format(_T("%y-%m-%d-%H-%M-%S"));
CString sText = sData + " " + sLog;
if (statusFile.m_size > 1024 * 1024 * 10)
{
if (fileLog.Open(sLogPath, CFile::modeCreate | CFile::modeWrite))
{
fileLog.WriteString(sText);
fileLog.WriteString("\n");
fileLog.Close();
}
}
else
{
if (fileLog.Open(sLogPath, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate))
{
fileLog.SeekToEnd();
fileLog.WriteString(sText);
fileLog.WriteString("\n");
fileLog.Close();
}
}
}
CAUDataManage::CAUDataManage()
{
m_sConfigFileName = "Version.xml";
m_sConfigFilePath = GetModulePath() + m_sConfigFileName;
m_stuConfigData = ReadConfig(m_sConfigFilePath);
}
CAUDataManage::~CAUDataManage()
{
}
BOOL CAUDataManage::CheckVersion()
{
BOOL bIsLatest = TRUE;
CreateTempFolder();
Log("检查版本。");
CInternetSession *pInternetSession = NULL;
CFtpConnection *pFtpConnection = NULL;
pInternetSession = new CInternetSession();
pFtpConnection = pInternetSession->GetFtpConnection(m_stuConfigData.sServerIP, m_stuConfigData.sUserName, m_stuConfigData.sPassWord);
CString sFilePath;
pFtpConnection->GetCurrentDirectory(sFilePath);
//add for test by liuye 2017.7.17
Log("for test 获取到的路径是: " + sFilePath);
//
sFilePath += m_stuConfigData.sUpdateDir + "/" + m_sConfigFileName;
CString sTempConfigPath = m_stuConfigData.sTempDir + "\\" + m_sConfigFileName;
if (pFtpConnection)
{
Log("连接到服务器。");
if (pFtpConnection->GetFile(sFilePath, sTempConfigPath))
{
Log("下载到配置文件。");
APPConfig stuTempConfig = ReadConfig(sTempConfigPath);
if (stuTempConfig.sVersion.Compare(m_stuConfigData.sVersion) == 0)
{
bIsLatest = TRUE;
}
else
{
bIsLatest = FALSE;
}
}
if (!bIsLatest)
{
CString sAP;
pFtpConnection->GetCurrentDirectory(sAP);
CString sPackageName = sAP + m_stuConfigData.sUpdateDir + "/" + m_stuConfigData.sZipName;
CFtpFileFind finderFtp(pFtpConnection);
if (!finderFtp.FindFile(sPackageName))
{
Log(sPackageName + " " + "文件不存在!");
bIsLatest = TRUE;
}
}
if (NULL != pFtpConnection)
{
pFtpConnection->Close();
delete pFtpConnection;
pFtpConnection = NULL;
}
if (NULL != pInternetSession)
{
delete pInternetSession;
pInternetSession = NULL;
}
}
else
{
Log("未连接到服务器。");
}
DeleteDirectory(m_stuConfigData.sTempDir);
return bIsLatest;
}
void CAUDataManage::Update()
{
HWND hMainWnd = AfxGetApp()->GetMainWnd()->m_hWnd;
PostMessage(hMainWnd, WM_UPDATEPROCESS, 0, PROCESS_KILLCLIENT);
if (KillCilent(m_stuConfigData.sAppName))
{
PostMessage(hMainWnd, WM_UPDATEPROCESS, 0, PROCESS_DOWNLOADFILE);
if (DownloadClientFile())
{
PostMessage(hMainWnd, WM_UPDATEPROCESS, 0, PROCESS_DELETEOLDFILE);
DeleteDirectory(m_stuConfigData.sAppDir);
PostMessage(hMainWnd, WM_UPDATEPROCESS, 0, PROCESS_UNZIP);
if (Unzip(m_stuConfigData.sTempDir + "\\" + m_stuConfigData.sZipName, m_stuConfigData.sAppDir))
{
m_stuConfigData = ReadConfig(m_sConfigFilePath);
}
}
}
PostMessage(hMainWnd, WM_UPDATEPROCESS, 0, PROCESS_COMPLETE);
}
BOOL CAUDataManage::KillCilent(CString sClientName)
{
BOOL bResult = FALSE;
Log("关闭进程。");
PROCESSENTRY32 stuClient;
HANDLE hAllProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hAllProcess != INVALID_HANDLE_VALUE)
{
stuClient.dwSize = sizeof(stuClient);
if (Process32First(hAllProcess, &stuClient))
{
CString sPrcessName;
do
{
sPrcessName = stuClient.szExeFile;
if (sClientName.CompareNoCase(sPrcessName) == 0)
{
HANDLE hProcessHandle;
hProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, true, stuClient.th32ProcessID);
if (hProcessHandle != NULL)
{
Log("找到进程" + sClientName);
if (TerminateProcess(hProcessHandle, 0))
{
Log(sClientName + "进程关闭。");
bResult = TRUE;
}
else
{
Log(sClientName + "进程关闭失败。");
bResult = FALSE;
}
CloseHandle(hProcessHandle);
break;
}
}
} while (Process32Next(hAllProcess, &stuClient));
}
CloseHandle(hAllProcess);
}
return bResult;
}
void CAUDataManage::RestartClient()
{
ShellExecute(NULL, "open", m_stuConfigData.sAppDir + CString("\\") + m_stuConfigData.sAppName, NULL, m_stuConfigData.sAppDir, SW_SHOW);
}
BOOL CAUDataManage::DownloadClientFile()
{
BOOL bResult = FALSE;
CreateTempFolder();
CInternetSession *pInternetSession = NULL;
CFtpConnection *pFtpConnection = NULL;
pInternetSession = new CInternetSession();
pFtpConnection = pInternetSession->GetFtpConnection(m_stuConfigData.sServerIP, m_stuConfigData.sUserName, m_stuConfigData.sPassWord);
CString sFileDir;
pFtpConnection->GetCurrentDirectory(sFileDir);
CString sSrcPath = sFileDir + m_stuConfigData.sUpdateDir + "/" + m_stuConfigData.sZipName;
CString sDstPath = m_stuConfigData.sTempDir + "\\" + m_stuConfigData.sZipName;
if (pFtpConnection)
{
Log("连接到服务器。");
if (pFtpConnection->GetFile(sSrcPath, sDstPath))
{
Log(sDstPath + "文件下载成功");
bResult = TRUE;
}
else
{
Log(sDstPath + "文件下载失败");
bResult = FALSE;
}
sSrcPath = sFileDir + m_stuConfigData.sUpdateDir + "/" + m_sConfigFileName;
sDstPath = m_stuConfigData.sTempDir + "\\" + m_sConfigFileName;
if (pFtpConnection->GetFile(sSrcPath, sDstPath))
{
Log(sDstPath + "文件下载成功");
bResult = TRUE;
}
else
{
Log(sDstPath + "文件下载失败");
bResult = FALSE;
}
}
else
{
Log("未连接到服务器。");
}
//释放资源
if (NULL != pFtpConnection)
{
pFtpConnection->Close();
delete pFtpConnection;
pFtpConnection = NULL;
}
if (NULL != pInternetSession)
{
delete pInternetSession;
pInternetSession = NULL;
}
return bResult;
}
void CAUDataManage::CreateTempFolder()
{
CString sFolder = GetModulePath() + L"temp";
if (PathFileExists(sFolder))
{
DeleteDirectory(sFolder);
}
CreateDirectory(sFolder, NULL);
}
void CAUDataManage::DeleteDirectory(CString sDirectory)
{
Log("正在删除目录" + sDirectory);
CFileFind cFind;
CString sFile = sDirectory + L"\\*.*";
BOOL IsFinded = cFind.FindFile(sFile);
while (IsFinded)
{
IsFinded = cFind.FindNextFile();
CString sTest = cFind.GetFileName();
if (!cFind.IsDots())
{
CString sFilePath;
if (cFind.GetFileName().Compare(m_stuConfigData.sSelfName) != 0
&& cFind.GetFilePath().Compare(m_stuConfigData.sTempDir) != 0
&& cFind.GetFileName().Compare("AutoUpdateLog.txt") != 0)
{
if (cFind.IsDirectory())
{
sFilePath = cFind.GetFilePath();
DeleteDirectory(sFilePath);
}
else
{
sFilePath = cFind.GetFilePath();
if (DeleteFile(sFilePath))
{
Log(sFilePath + " " + "删除成功。");
}
else
{
Log(sFilePath + " " + "删除失败。");
}
}
}
}
}
cFind.Close();
RemoveDirectory(sDirectory);
}
CString CAUDataManage::GetModulePath()
{
CString sPath;
TCHAR szPath[MAX_PATH];
GetModuleFileName(NULL, szPath, MAX_PATH);
CString sMudolePath = szPath;
CString sMudoleName = AfxGetAppName();
sPath = sMudolePath.Left(sMudolePath.GetLength() - sMudoleName.GetLength() - 4);
return sPath;
}
APPConfig CAUDataManage::ReadConfig(CString sConfigName)
{
Log("开始读取配置文件。");
APPConfig stuConfig;
TiXmlDocument xmlDoc(sConfigName.GetBuffer());
if (xmlDoc.LoadFile())
{
TiXmlElement *pRootEle = xmlDoc.RootElement();
TiXmlElement *pPackageNameEle = pRootEle->FirstChildElement();
stuConfig.sZipName = pPackageNameEle->GetText();
TiXmlElement *pAppNameEle = pPackageNameEle->NextSiblingElement();
stuConfig.sAppName = pAppNameEle->GetText();
TiXmlElement *pVersionEle = pAppNameEle->NextSiblingElement();
stuConfig.sVersion = pVersionEle->GetText();
TiXmlElement *pUpdateDir = pVersionEle->NextSiblingElement();
stuConfig.sUpdateDir = pUpdateDir->GetText();
TiXmlElement *pServerIP = pUpdateDir->NextSiblingElement();
stuConfig.sServerIP = pServerIP->GetText();
TiXmlElement *pUserName = pServerIP->NextSiblingElement();
stuConfig.sUserName = pUserName->GetText();
TiXmlElement *pPassWord = pUserName->NextSiblingElement();
stuConfig.sPassWord = pPassWord->GetText();
stuConfig.sAppDir = GetModulePath();
stuConfig.sSelfName = AfxGetAppName() + CString(".exe");
stuConfig.sTempDir = stuConfig.sAppDir + CString("temp");
Log("读取配置文件完成。");
}
return stuConfig;
}
void CAUDataManage::ReplaceClientFile(CString sAppDir)
{
DeleteDirectory(m_stuConfigData.sAppDir);
Log("旧文件已删除。");
CopyClientFile(m_stuConfigData.sTempDir, m_stuConfigData.sAppDir);
DeleteDirectory(m_stuConfigData.sTempDir);
Log("临时文件已删除。");
}
void CAUDataManage::CopyClientFile(CString sSrcFile, CString sDstFile)
{
CreateDirectory(sDstFile, NULL);
CFileFind cFinder;
CString sPath;
sPath = sSrcFile + "\\*.*";
BOOL bWorking = cFinder.FindFile(sPath);
while (bWorking)
{
Log("找到文件目录,正在复制文件。");
bWorking = cFinder.FindNextFile();
CString sTest = cFinder.GetFileName();
if (!cFinder.IsDots())
{
if (cFinder.IsDirectory())
{
CopyClientFile(cFinder.GetFilePath(), sDstFile + ("\\") + cFinder.GetFileName());
}
else
{
CString stSrcFile = cFinder.GetFilePath();
BOOL bDir = (GetFileAttributes(stSrcFile) & FILE_ATTRIBUTE_DIRECTORY);
if (!bDir)
{
if (CopyFile(cFinder.GetFilePath(), sDstFile + ("\\") + cFinder.GetFileName(), FALSE))
{
Log(cFinder.GetFileName() + " " + "文件复制成功。");
}
else
{
Log(cFinder.GetFileName() + " " + "文件复制失败。");
}
}
}
}
}
}
void CAUDataManage::DownloadFile(CString sSrcFile, CString sDstFile, CFtpConnection *pFtpConnection)
{
if (pFtpConnection)
{
Log("连接服务器成功,开始查找文件。");
/*BOOL bResult =*/ pFtpConnection->SetCurrentDirectory(sSrcFile);
CFtpFileFind cFTPFind(pFtpConnection);
CreateDirectory(sDstFile, NULL);
CString sPath;
sPath =/* sCurPath + */sSrcFile + "/*.*";
BOOL bWorking = cFTPFind.FindFile(sSrcFile);
while (bWorking)
{
Log("找到文件目录,开始下载文件。");
bWorking = cFTPFind.FindNextFile();
if (!cFTPFind.IsDots())
{
if (cFTPFind.IsDirectory())
{
CInternetSession *pInternetSession = NULL;
CFtpConnection *pFtpConnection = NULL;
pInternetSession = new CInternetSession();
pFtpConnection = pInternetSession->GetFtpConnection(m_stuConfigData.sServerIP, m_stuConfigData.sUserName, m_stuConfigData.sPassWord);
DownloadFile(sSrcFile + "/" + cFTPFind.GetFileName(), sDstFile + ("\\") + cFTPFind.GetFileName(), pFtpConnection);
if (NULL != pFtpConnection)
{
pFtpConnection->Close();
delete pFtpConnection;
pFtpConnection = NULL;
}
if (NULL != pInternetSession)
{
delete pInternetSession;
pInternetSession = NULL;
}
}
else
{
CString sFilePath = cFTPFind.GetFilePath();
CString sNewFile = sDstFile + "\\" + cFTPFind.GetFileName();
BOOL bResult = pFtpConnection->GetFile(sFilePath, sNewFile);
}
}
}
}
}
CAUDataManage* CAUDataManage::Instantialize()
{
if (m_pInstance == NULL)
{ //double check
//Lock lock(m_csLocker); //用lock实现线程安全,用资源管理类,实现异常安全
//使用资源管理类,在抛出异常的时候,资源管理类对象会被析构,析构总是发生的无论是因为异常抛出还是语句块结束。
if (m_pInstance == NULL)
{
m_pInstance = new CAUDataManage();
}
}
return m_pInstance;
}
void CAUDataManage::DestoryInstance()
{
if (m_pInstance)
{
delete m_pInstance;
m_pInstance = NULL;
}
}
BOOL CAUDataManage::Unzip(CString sSrcDir, CString sDstDir)
{
BOOL bResult = FALSE;
Log("开始解压文件" + sSrcDir);
HZIP hZip = OpenZip(sSrcDir.GetBuffer(), NULL);
if (hZip)
{
SetUnzipBaseDir(hZip, sDstDir);
ZIPENTRY stuZipentry;
GetZipItem(hZip, -1, &stuZipentry);
int iTotalCount = stuZipentry.index;
for (int index = 0; index < iTotalCount; ++index)
{
Log("正在解压文件" + CString(stuZipentry.name));
GetZipItem(hZip, index, &stuZipentry);
UnzipItem(hZip, index, stuZipentry.name);
}
}
if (hZip != NULL)
{
CloseZip(hZip);
hZip = NULL;
}
Log("复制配置文件");
if (CopyFile(m_stuConfigData.sTempDir + ("\\") + m_sConfigFileName, m_stuConfigData.sAppDir + ("\\") + m_sConfigFileName, FALSE))
{
Log(m_sConfigFileName + " " + "文件复制成功。");
bResult = TRUE;
}
else
{
Log(m_sConfigFileName + " " + "文件复制失败。");
bResult = FALSE;
}
DeleteDirectory(m_stuConfigData.sTempDir);
return bResult;
}