Skip to content

Commit

Permalink
Fix releasing container file
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbar committed Mar 7, 2024
1 parent d9421ea commit 977c84f
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/PanelSwBackendExtension/PanelSwBackendExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ HRESULT CPanelSwBundleExtension::ReleaseContainer(IPanelSwContainer* pContainer)
ContainerIterator endIt = _containers.end();
for (ContainerIterator it = _containers.begin(); it != endIt; ++it)
{
if (*it == pContainer)
if (*it == (IPanelSwContainer*)pContainer)
{
hr = S_OK;
_containers.remove(pContainer);
Expand Down
2 changes: 1 addition & 1 deletion src/PanelSwBackendExtension/PanelSwBackendExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CPanelSwBundleExtension : public CBextBaseBundleExtension
public:
CPanelSwBundleExtension(IBundleExtensionEngine* pEngine);

~CPanelSwBundleExtension();
virtual ~CPanelSwBundleExtension();

STDMETHOD(Search)(LPCWSTR wzId, LPCWSTR wzVariable) override;

Expand Down
9 changes: 1 addition & 8 deletions src/PanelSwBackendExtension/PanelSwLzmaContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CPanelSwLzmaContainer::~CPanelSwLzmaContainer()

HRESULT CPanelSwLzmaContainer::Reset()
{
_archive.reset();
_codecs.reset();
_inStream.Release();
_extractIndices.reset();
Expand Down Expand Up @@ -107,7 +108,6 @@ HRESULT CPanelSwLzmaContainer::ContainerOpen(LPCWSTR wzContainerId, LPCWSTR wzFi

hr = Init(wzContainerId, hFile, 0, CPanelSwLzmaInStream::INFINITE_CONTAINER_SIZE);
BextExitOnFailure(hr, "Failed to open container '%ls'", wzFilePath);
hFile = INVALID_HANDLE_VALUE;

BextLog(BUNDLE_EXTENSION_LOG_LEVEL_STANDARD, "Openned 7Z container '%ls'", wzFilePath);

Expand Down Expand Up @@ -193,14 +193,9 @@ HRESULT CPanelSwLzmaContainer::ContainerStreamToFile(LPCWSTR wzFileName)
{
HRESULT hr = S_OK;
BOOL bRes = TRUE;
HANDLE hFile = INVALID_HANDLE_VALUE;

BextExitOnNull((_fileIndex < _entryCount), hr, E_INVALIDSTATE, "7z container is exhausted, can't extract '%ls'", wzFileName);

// Creating a file for placeholder
hFile = ::CreateFile(wzFileName, FILE_ALL_ACCESS, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
BextExitOnNullWithLastError((hFile != INVALID_HANDLE_VALUE), hr, "Failed to create file '%ls'", wzFileName);

_extractIndices[_extractCount] = _fileIndex;
_extractPaths[_extractCount] = wzFileName;
::InterlockedIncrement(&_extractCount);
Expand All @@ -209,8 +204,6 @@ HRESULT CPanelSwLzmaContainer::ContainerStreamToFile(LPCWSTR wzFileName)
BextExitOnNullWithLastError(bRes, hr, "Failed to set extract event");

LExit:
ReleaseFile(hFile);

return hr;
}

Expand Down
2 changes: 1 addition & 1 deletion src/PanelSwBackendExtension/PanelSwLzmaContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class CPanelSwLzmaContainer : public IPanelSwContainer
{
public:

~CPanelSwLzmaContainer();
virtual ~CPanelSwLzmaContainer();

HRESULT ContainerOpen(LPCWSTR wzContainerId, LPCWSTR wzFilePath) override;

Expand Down
1 change: 0 additions & 1 deletion src/PanelSwBackendExtension/PanelSwLzmaInStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ HRESULT CPanelSwLzmaInStream::InitContainer(HANDLE hBundle, DWORD64 qwContainerS
void CPanelSwLzmaInStream::ReleaseContainer()
{
ReleaseFileHandle(_hBundle);
_hBundle = INVALID_HANDLE_VALUE;
_qwContainerStartPos = 0;
_qwContainerSize = 0;
_qwBundleSize = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/PanelSwBackendExtension/PanelSwLzmaInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Z7_class_final(CPanelSwLzmaInStream) :
Z7_IFACE_COM7_IMP(IStreamGetSize)

public:
~CPanelSwLzmaInStream();
virtual ~CPanelSwLzmaInStream();

HRESULT InitContainer(HANDLE hBundle, DWORD64 qwContainerStartPos, DWORD64 qwContainerSize);

static const DWORD64 INFINITE_CONTAINER_SIZE = (~(DWORD64)0);

private:
Expand Down
19 changes: 12 additions & 7 deletions src/PanelSwBackendExtension/PanelSwLzmaOutStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ HRESULT CPanelSwLzmaOutStream::Close()
BOOL bRes = FALSE;
for (unsigned i = 0; !bRes && (i < MAX_RETRIES); ++i)
{
hr = S_OK;

bRes = ::SetFileTime(_hFile, pftCreate, pftAccess, pftWrite);
if (!bRes)
{
hr = HRESULT_FROM_WIN32(::GetLastError());
BextLogError(hr, "Failed to set file times for '%ls' on attempt %u/%u", _szPath, i, MAX_RETRIES);
continue;
}
hr = S_OK;
break;
}
hr = S_OK; // Ignoring failure to set file time
Expand Down Expand Up @@ -74,6 +75,8 @@ HRESULT CPanelSwLzmaOutStream::Create(LPCWSTR szPath, UInt64 ullSize, const FILE

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

::SetFileAttributesW(_szPath, FILE_ATTRIBUTE_NORMAL);
::DeleteFileW(_szPath);

Expand All @@ -84,7 +87,6 @@ HRESULT CPanelSwLzmaOutStream::Create(LPCWSTR szPath, UInt64 ullSize, const FILE
BextLogError(hr, "Failed to create file '%ls' on attempt %u/%u", _szPath, i, MAX_RETRIES);
continue;
}
hr = S_OK;
break;
}
BextExitOnFailure(hr, "Failed to create file");
Expand Down Expand Up @@ -133,14 +135,15 @@ HRESULT CPanelSwLzmaOutStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64* new
liPos.QuadPart = offset;
for (unsigned i = 0; !bRes && (i < MAX_RETRIES); ++i)
{
hr = S_OK;

bRes = ::SetFilePointerEx(_hFile, liPos, &liNewPos, seekOrigin);
if (!bRes)
{
hr = HRESULT_FROM_WIN32(::GetLastError());
BextLogError(hr, "Failed to seek in file '%ls' on attempt %u/%u", _szPath, i, MAX_RETRIES);
continue;
}
hr = S_OK;
break;
}
BextExitOnNullWithLastError(bRes, hr, "Failed to seek in file '%ls'", (LPCWSTR)_szPath);
Expand All @@ -164,28 +167,29 @@ Z7_COM7F_IMF(CPanelSwLzmaOutStream::SetSize(UInt64 newSize))
HRESULT hr = S_OK;
ULARGE_INTEGER liCurrPos = { 0 };

hr = Seek(0, ESzSeek::SZ_SEEK_CUR, &liCurrPos.QuadPart);
hr = Seek(0, ESzSeek::SZ_SEEK_CUR, &liCurrPos.QuadPart, false);
BextExitOnFailure(hr, "Failed to get current seek position in file '%ls'", _szPath);

hr = Seek(newSize, ESzSeek::SZ_SEEK_SET, nullptr);
hr = Seek(newSize, ESzSeek::SZ_SEEK_SET, nullptr, false);
BextExitOnFailure(hr, "Failed to set seek position in file '%ls' to %I64u", _szPath, newSize);

bRes = FALSE;
for (unsigned i = 0; !bRes && (i < MAX_RETRIES); ++i)
{
hr = S_OK;

bRes = ::SetEndOfFile(_hFile);
if (!bRes)
{
hr = HRESULT_FROM_WIN32(::GetLastError());
BextLogError(HRESULT_FROM_WIN32(::GetLastError()), "Failed to set EOF in file '%ls' on attempt %u/%u", _szPath, i, MAX_RETRIES);
continue;
}
hr = S_OK;
break;
}
BextExitOnNullWithLastError(bRes, hr, "Failed to set EOF in file '%ls'", (LPCWSTR)_szPath);

hr = Seek(liCurrPos.QuadPart, ESzSeek::SZ_SEEK_SET, nullptr);
hr = Seek(liCurrPos.QuadPart, ESzSeek::SZ_SEEK_SET, nullptr, false);
BextExitOnFailure(hr, "Failed to set current seek position in file '%ls'", _szPath);

LExit:
Expand Down Expand Up @@ -277,6 +281,7 @@ HRESULT CPanelSwLzmaOutStream::CompleteWrite()
DWORD dwRes = ERROR_SUCCESS;
DWORD dwWritten = 0;
ULARGE_INTEGER ullPos = { 0,0 };
hr = S_OK;

ullPos.QuadPart = ullStartPos.QuadPart + ullWritten;

Expand Down
2 changes: 1 addition & 1 deletion src/PanelSwBackendExtension/PanelSwLzmaOutStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Z7_CLASS_IMP_COM_1(
Z7_IFACE_COM7_IMP(ISequentialOutStream)
public:

~CPanelSwLzmaOutStream();
virtual ~CPanelSwLzmaOutStream();

HRESULT Create(LPCWSTR szPath, UInt64 ullSize, const FILETIME ftCreationTime, const FILETIME ftLastAccessTime, const FILETIME ftLastWriteTime);

Expand Down
2 changes: 1 addition & 1 deletion src/PanelSwBackendExtension/PanelSwZipContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CPanelSwZipContainer : public IPanelSwContainer
{
public:

~CPanelSwZipContainer();
virtual ~CPanelSwZipContainer();

HRESULT ContainerOpen(LPCWSTR wzContainerId, LPCWSTR wzFilePath) override;

Expand Down
2 changes: 1 addition & 1 deletion src/PanelSwWixExtension.sln
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Project("{B7DD6F7E-DEF8-4E67-B5B7-07EF123DB6F0}") = "RegexUT", "UnitTests\RegexU
EndProject
Project("{B7DD6F7E-DEF8-4E67-B5B7-07EF123DB6F0}") = "FileOperationsUT", "UnitTests\FileOperationsUT\FileOperationsUT.wixproj", "{2B87572F-9B38-4F39-A213-97C1AC64703E}"
EndProject
Project("{B7DD6F7E-DEF8-4E67-B5B7-07EF123DB6F0}") = "TaskScheulerUT", "UnitTests\TaskScheulerUT\TaskScheulerUT.wixproj", "{31F20547-2522-4643-AE23-B5B4E25A8852}"
Project("{B7DD6F7E-DEF8-4E67-B5B7-07EF123DB6F0}") = "TaskSchedulerUT", "UnitTests\TaskSchedulerUT\TaskSchedulerUT.wixproj", "{31F20547-2522-4643-AE23-B5B4E25A8852}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{096EB3BA-5DCA-4121-8407-C93F31997339}"
ProjectSection(SolutionItems) = preProject
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.5</FullVersion>
<FullVersion>3.18.6</FullVersion>
<FullVersion Condition=" '$(GITHUB_RUN_NUMBER)'!='' ">$(FullVersion).$(GITHUB_RUN_NUMBER)</FullVersion>
<ProductName>PanelSwWixExtension</ProductName>
<Manufacturer>Panel::Software</Manufacturer>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="WixToolset.Sdk">
<Project Sdk="WixToolset.Sdk">
<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>
Expand Down

0 comments on commit 977c84f

Please sign in to comment.