diff options
Diffstat (limited to 'plugins/PluginUpdater/src/unzipfile.cpp')
-rw-r--r-- | plugins/PluginUpdater/src/unzipfile.cpp | 76 |
1 files changed, 43 insertions, 33 deletions
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;
|