Skip to content

Commit

Permalink
Retry mechanism on opening and reading 7z containers
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbar committed Mar 12, 2024
1 parent 977c84f commit 542628c
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 45 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/github-actions-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
psw_wix_version:
description: 'PanelSwWix4 version'
required: true
default: 5.0.0-psw-wix.0256-45
default: 5.0.0-psw-wix.0262-48
type: string

jobs:
Expand All @@ -37,7 +37,7 @@ jobs:
Add-Content -Path ${{ github.env }} -Value "PSW_WIX_VERSION=${{ env.DEFAULT_PSW_WIX_VERSION }}"
}
env:
DEFAULT_PSW_WIX_VERSION: '5.0.0-psw-wix.0256-45'
DEFAULT_PSW_WIX_VERSION: '5.0.0-psw-wix.0262-48'

- name: Prepare for build
run: |
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<Wix4Version Condition=" '$(Wix4Version)' == '' ">4.0.4</Wix4Version>
<PanelSwWix4Version Condition=" '$(PanelSwWix4Version)' == '' ">5.0.0-psw-wix.0256-45</PanelSwWix4Version>
<PanelSwWix4Version Condition=" '$(PanelSwWix4Version)' == '' ">5.0.0-psw-wix.0262-48</PanelSwWix4Version>
<SevenZapVersion Condition=" '$(SevenZapVersion)' == '' ">23.1.33</SevenZapVersion>
</PropertyGroup>
<ItemGroup>
Expand Down
27 changes: 8 additions & 19 deletions src/PanelSwBackendExtension/PanelSwBackendExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ HRESULT CPanelSwBundleExtension::CreateContainer(LPCWSTR wzContainerId, IPanelSw
if (::wcsicmp(compression.bstrVal, L"Zip") == 0)
{
pContainer = new CPanelSwZipContainer();
BextExitOnNull(pContainer, hr, E_FAIL, "Failed to get allocate zip container");
BextExitOnNull(pContainer, hr, E_FAIL, "Failed to allocate zip container");
}
else if (::wcsicmp(compression.bstrVal, L"SevenZip") == 0)
{
pContainer = new CPanelSwLzmaContainer();
BextExitOnNull(pContainer, hr, E_FAIL, "Failed to get allocate 7z container");
BextExitOnNull(pContainer, hr, E_FAIL, "Failed to allocate 7z container");
}
else
{
Expand Down Expand Up @@ -117,19 +117,13 @@ STDMETHODIMP CPanelSwBundleExtension::ContainerOpen(LPCWSTR wzContainerId, LPCWS
hr = CreateContainer(wzContainerId, &pContainer);
BextExitOnFailure(hr, "Failed to create container");

hr = pContainer->ContainerOpen(wzContainerId, wzFilePath);
BextExitOnFailure(hr, "Failed to open container");

_containers.push_back(pContainer);
*ppContext = pContainer;
pContainer = nullptr;

LExit:
if (pContainer)
{
delete pContainer;
}
hr = pContainer->ContainerOpen(wzContainerId, wzFilePath);
BextExitOnFailure(hr, "Failed to open container");

LExit:
return hr;
}

Expand All @@ -141,19 +135,14 @@ STDMETHODIMP CPanelSwBundleExtension::ContainerOpenAttached(LPCWSTR wzContainerI
hr = CreateContainer(wzContainerId, &pContainer);
BextExitOnFailure(hr, "Failed to create container");

hr = pContainer->ContainerOpenAttached(wzContainerId, hBundle, qwContainerStartPos, qwContainerSize);
BextExitOnFailure(hr, "Failed to open attached container");

_containers.push_back(pContainer);
*ppContext = pContainer;
pContainer = nullptr;

LExit:
if (pContainer)
{
delete pContainer;
}
hr = pContainer->ContainerOpenAttached(wzContainerId, hBundle, qwContainerStartPos, qwContainerSize);
BextExitOnFailure(hr, "Failed to open attached container");

LExit:
return hr;
}

Expand Down
86 changes: 67 additions & 19 deletions src/PanelSwBackendExtension/PanelSwLzmaContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,37 @@ HRESULT CPanelSwLzmaContainer::Init(LPCWSTR wzContainerId, HANDLE hBundle, DWORD
_inStream = inStream;
BextExitOnNull(!!_inStream, hr, E_OUTOFMEMORY, "Failed to allocate stream");

inStream->InitContainer(hBundle, qwContainerStartPos, qwContainerSize);
BextExitOnFailure(hr, "Failed to open container stream");

openOpts.codecs = _codecs.get();
openOpts.stream = _inStream;
openOpts.excludedFormats = new CIntVector();
openOpts.props = new CObjectVector<CProperty>();

hr = _archive->OpenStream(openOpts);

for (unsigned i = 0; i < MAX_RETRIES; ++i)
{
hr = S_OK;

hr = inStream->InitContainer(hBundle, qwContainerStartPos, qwContainerSize);
if (FAILED(hr))
{
BextLogError(hr, "Failed to open container stream on attempt %u/%u", i, MAX_RETRIES);
continue;
}

hr = _archive->OpenStream(openOpts);
if (FAILED(hr))
{
BextLogError(hr, "Failed to open container on attempt %u/%u", i, MAX_RETRIES);
continue;
}
if (_archive->Archive == nullptr)
{
hr = E_FAIL;
BextLogError(hr, "Failed to initialize container on attempt %u/%u", i, MAX_RETRIES);
continue;
}

break;
}
BextExitOnFailure(hr, "Failed to open container");
BextExitOnNull(_archive->Archive, hr, E_FAIL, "Failed to initialize container");

Expand Down Expand Up @@ -220,12 +242,24 @@ HRESULT CPanelSwLzmaContainer::ContainerStreamToFileNow(UInt32 nFileIndex, LPCWS
BextExitOnNull(pExtractCallback, hr, E_OUTOFMEMORY, "Failed to allocate extract callback object");
extractClbk = pExtractCallback;

hr = pExtractCallback->Init(_archive->Archive, 1, &nFileIndex, &targetFile);
BextExitOnFailure(hr, "Failed to initialize extract callbck");
for (unsigned i = 0; i < MAX_RETRIES; ++i)
{
hr = S_OK;

hr = _archive->Archive->Extract(&nFileIndex, 1, 0, extractClbk);
BextExitOnFailure(hr, "Failed to extract '%ls'", wzFileName);
BextExitOnNull(!pExtractCallback->HasErrors(), hr, E_FAIL, "Failed to extract '%ls'", wzFileName);
hr = pExtractCallback->Init(_archive->Archive, 1, &nFileIndex, &targetFile);
BextExitOnFailure(hr, "Failed to initialize extract callbck");

hr = _archive->Archive->Extract(&nFileIndex, 1, 0, extractClbk);
if (FAILED(hr))
{
BextLogError(hr, "Failed to extract files on attempt %u/%u", i, MAX_RETRIES);
continue;
}

break;
}
BextExitOnFailure(hr, "Failed to extract files");
BextExitOnNull(!pExtractCallback->HasErrors(), hr, E_FAIL, "Failed to extract files");

LExit:
return S_OK;
Expand All @@ -247,11 +281,14 @@ HRESULT CPanelSwLzmaContainer::ContainerClose()
BOOL bRes = TRUE;
DWORD dwWait = ERROR_SUCCESS;

bRes = ::SetEvent(_hEndExtract);
BextExitOnNullWithLastError(bRes, hr, "Failed to set event");
if (_hEndExtract && _hExtractThread)
{
bRes = ::SetEvent(_hEndExtract);
BextExitOnNullWithLastError(bRes, hr, "Failed to set event");

dwWait = ::WaitForSingleObject(_hExtractThread, INFINITE);
BextExitOnNullWithLastError((dwWait == WAIT_OBJECT_0), hr, "Failed to wait for extract thread to terminate");
dwWait = ::WaitForSingleObject(_hExtractThread, INFINITE);
BextExitOnNullWithLastError((dwWait == WAIT_OBJECT_0), hr, "Failed to wait for extract thread to terminate");
}

LExit:
Reset();
Expand Down Expand Up @@ -411,14 +448,25 @@ HRESULT CPanelSwLzmaContainer::GetNextMapping(BSTR* psczStreamName)
UInt32* pIndices = pThis->_extractIndices.get() + dwPrevExtractCount;
FString* pPaths = pThis->_extractPaths.get() + dwPrevExtractCount;

hr = pExtractCallback->Init(pThis->_archive->Archive, dwExtractCount, pIndices, pPaths);
BextExitOnFailure(hr, "Failed to initialize extract callbck");
for (unsigned i = 0; i < MAX_RETRIES; ++i)
{
hr = S_OK;

hr = pThis->_archive->Archive->Extract(pIndices, dwExtractCount, 0, extractClbk);
hr = pExtractCallback->Init(pThis->_archive->Archive, dwExtractCount, pIndices, pPaths);
BextExitOnFailure(hr, "Failed to initialize extract callbck");

hr = pThis->_archive->Archive->Extract(pIndices, dwExtractCount, 0, extractClbk);
if (FAILED(hr))
{
BextLogError(hr, "Failed to extract files on attempt %u/%u", i, MAX_RETRIES);
continue;
}

dwPrevExtractCount = dwOverallExtractCount;
break;
}
BextExitOnFailure(hr, "Failed to extract files");
BextExitOnNull(!pExtractCallback->HasErrors(), hr, E_FAIL, "Failed to extract files");

dwPrevExtractCount = dwOverallExtractCount;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/PanelSwBackendExtension/PanelSwLzmaContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class CPanelSwLzmaContainer : public IPanelSwContainer
UInt32 _fileCount = 0; // File count including mappings
UInt32 _extractCount = 0;

static const unsigned MAX_RETRIES = 5;

// Extraction thread
static DWORD WINAPI ExtractThreadProc(LPVOID lpParameter);

Expand Down
2 changes: 1 addition & 1 deletion src/PanelSwBackendExtension/PanelSwLzmaExtractCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "lzma-sdk/CPP/7zip/Common/FileStreams.h"
#include "lzma-sdk/CPP/7zip/Archive/IArchive.h"

HRESULT CPanelSwLzmaExtractCallback::Init(IInArchive* archive, UInt32 extractCount, UInt32* extractIndices, FString* extractPaths)
HRESULT CPanelSwLzmaExtractCallback::Init(IInArchive* archive, UInt32 extractCount, const UInt32* extractIndices, const FString* extractPaths)
{
HRESULT hr = S_OK;

Expand Down
2 changes: 1 addition & 1 deletion src/PanelSwBackendExtension/PanelSwLzmaExtractCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CPanelSwLzmaExtractCallback Z7_final :
Z7_IFACE_COM7_IMP(IArchiveExtractCallbackMessage2)

public:
HRESULT Init(IInArchive* archive, UInt32 extractCount, UInt32* extractIndices, FString* extractPaths);
HRESULT Init(IInArchive* archive, UInt32 extractCount, const UInt32* extractIndices, const FString* extractPaths);

BOOL HasErrors() const;

Expand Down
2 changes: 1 addition & 1 deletion src/TidyBuild.custom.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="16.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildThisFileDirectory)TidyBuild.user.props" Condition="Exists('$(MSBuildThisFileDirectory)TidyBuild.user.props')"/>
<PropertyGroup>
<FullVersion>3.18.6</FullVersion>
<FullVersion>3.18.7</FullVersion>
<FullVersion Condition=" '$(GITHUB_RUN_NUMBER)'!='' ">$(FullVersion).$(GITHUB_RUN_NUMBER)</FullVersion>
<ProductName>PanelSwWixExtension</ProductName>
<Manufacturer>Panel::Software</Manufacturer>
Expand Down
2 changes: 1 addition & 1 deletion src/global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"msbuild-sdks": {
"WixToolset.Sdk": "4.0.4",
"PanelSwWix4.Sdk": "5.0.0-psw-wix.0256-45",
"PanelSwWix4.Sdk": "5.0.0-psw-wix.0262-48",
"Microsoft.Build.Traversal": "4.0.0"
},
"sdk": {
Expand Down

0 comments on commit 542628c

Please sign in to comment.