From 9c32e9a999c2a0d86133b1fca16f75fe10672136 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 5 Feb 2013 21:49:35 +0000 Subject: experimental version of PU that can handle UAC correctly git-svn-id: http://svn.miranda-ng.org/main/trunk@3445 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/PluginUpdater/src/unzipfile.cpp | 76 +++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 33 deletions(-) (limited to 'plugins/PluginUpdater/src/unzipfile.cpp') diff --git a/plugins/PluginUpdater/src/unzipfile.cpp b/plugins/PluginUpdater/src/unzipfile.cpp index b2b621d37a..2362debf44 100644 --- a/plugins/PluginUpdater/src/unzipfile.cpp +++ b/plugins/PluginUpdater/src/unzipfile.cpp @@ -37,12 +37,8 @@ static void PrepareFileName(TCHAR *dest, size_t destSize, const TCHAR *ptszPath, void BackupFile(TCHAR *ptszSrcFileName, TCHAR *ptszBackFileName) { - CreatePathToFileT(ptszBackFileName); - DeleteFile(ptszBackFileName); - if ( MoveFile(ptszSrcFileName, ptszBackFileName) == 0) { // use copy on error - CopyFile(ptszSrcFileName, ptszBackFileName, FALSE); - DeleteFile(ptszSrcFileName); - } + SafeCreateFilePath(ptszBackFileName); + SafeMoveFile(ptszSrcFileName, ptszBackFileName); } bool extractCurrentFile(unzFile uf, TCHAR *ptszDestPath, TCHAR *ptszBackPath) @@ -69,38 +65,52 @@ bool extractCurrentFile(unzFile uf, TCHAR *ptszDestPath, TCHAR *ptszBackPath) if (err != UNZ_OK) return false; - PrepareFileName(tszDestFile, SIZEOF(tszDestFile), ptszDestPath, ptszNewName); - PrepareFileName(tszBackFile, SIZEOF(tszBackFile), ptszBackPath, ptszNewName); - BackupFile(tszDestFile, tszBackFile); + if (ptszBackPath != NULL) { + PrepareFileName(tszDestFile, SIZEOF(tszDestFile), ptszDestPath, ptszNewName); + PrepareFileName(tszBackFile, SIZEOF(tszBackFile), ptszBackPath, ptszNewName); + BackupFile(tszDestFile, tszBackFile); + } PrepareFileName(tszDestFile, SIZEOF(tszDestFile), ptszDestPath, ptszNewName); - CreatePathToFileT(tszDestFile); - - HANDLE hFile = CreateFile(tszDestFile, GENERIC_WRITE, FILE_SHARE_WRITE, 0, - CREATE_ALWAYS, file_info.external_fa, 0); - - if (hFile != INVALID_HANDLE_VALUE) { - while (true) { - err = unzReadCurrentFile(uf, buf, sizeof(buf)); - if (err <= 0) - break; - - DWORD bytes; - if (!WriteFile(hFile, buf, err, &bytes, FALSE)) { - err = UNZ_ERRNO; - break; - } + SafeCreateFilePath(tszDestFile); + + TCHAR *ptszFile2unzip; + if (hPipe == NULL) // direct mode + ptszFile2unzip = tszDestFile; + else { + TCHAR tszTempPath[MAX_PATH]; + GetTempPath( SIZEOF(tszTempPath), tszTempPath); + GetTempFileName(tszTempPath, _T("PUtemp"), GetCurrentProcessId(), tszBackFile); + ptszFile2unzip = tszBackFile; + } + + HANDLE hFile = CreateFile(ptszFile2unzip, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, file_info.external_fa, 0); + if (hFile == INVALID_HANDLE_VALUE) + return false; + + while (true) { + err = unzReadCurrentFile(uf, buf, sizeof(buf)); + if (err <= 0) + break; + + DWORD bytes; + if (!WriteFile(hFile, buf, err, &bytes, FALSE)) { + err = UNZ_ERRNO; + break; } + } - FILETIME ftLocal, ftCreate, ftLastAcc, ftLastWrite; - GetFileTime(hFile, &ftCreate, &ftLastAcc, &ftLastWrite); - DosDateTimeToFileTime(HIWORD(file_info.dosDate), LOWORD(file_info.dosDate), &ftLocal); - LocalFileTimeToFileTime(&ftLocal, &ftLastWrite); - SetFileTime(hFile, &ftCreate, &ftLastAcc, &ftLastWrite); + FILETIME ftLocal, ftCreate, ftLastAcc, ftLastWrite; + GetFileTime(hFile, &ftCreate, &ftLastAcc, &ftLastWrite); + DosDateTimeToFileTime(HIWORD(file_info.dosDate), LOWORD(file_info.dosDate), &ftLocal); + LocalFileTimeToFileTime(&ftLocal, &ftLastWrite); + SetFileTime(hFile, &ftCreate, &ftLastAcc, &ftLastWrite); - CloseHandle(hFile); - unzCloseCurrentFile(uf); /* don't lose the error */ - } + CloseHandle(hFile); + unzCloseCurrentFile(uf); /* don't lose the error */ + + if (hPipe) + SafeMoveFile(ptszFile2unzip, tszDestFile); } mir_free(ptszNewName); return true; -- cgit v1.2.3