Skip to content

Commit

Permalink
Fix deleting folders with locked files
Browse files Browse the repository at this point in the history
  • Loading branch information
nirbar committed Jan 31, 2024
1 parent 4da4a44 commit dfeb8ca
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/PanelSwCustomActions/DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason, LPVOID)
{
case DLL_PROCESS_ATTACH:
WcaGlobalInitialize(hInst);

_gFsRedirect.fDisabled = FALSE;

#if !defined(_WIN64)
hProc = ::GetCurrentProcess();
hr = ProcWow64(hProc, &bWow64);
if (SUCCEEDED(hr) && bWow64)
Expand All @@ -23,7 +24,7 @@ extern "C" BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ulReason, LPVOID)
_gFsRedirect.fDisabled = FALSE;
}
}

#endif
break;

case DLL_PROCESS_DETACH:
Expand Down
27 changes: 20 additions & 7 deletions src/PanelSwCustomActions/FileOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ HRESULT CFileOperations::DeletePath(LPCWSTR szFrom, bool bIgnoreMissing, bool bI
opInfo.fFlags = FOF_NO_UI;

LogUnformatted(LOGLEVEL::LOGMSG_STANDARD, true, L"Deleting '%ls'", szFrom);
if (!::PathIsDirectory(szFrom))
{
::SetFileAttributes(szFrom, FILE_ATTRIBUTE_NORMAL);
}
nRes = ::SHFileOperation(&opInfo);
if (bIgnoreMissing && (nRes == ERROR_FILE_NOT_FOUND) || (nRes == ERROR_PATH_NOT_FOUND))
{
Expand All @@ -341,13 +345,22 @@ HRESULT CFileOperations::DeletePath(LPCWSTR szFrom, bool bIgnoreMissing, bool bI
LogUnformatted(LOGLEVEL::LOGMSG_STANDARD, true, L"Failed deleting '%ls' due to a lock on file(s), so reboot will be required", szFromNull);

// MoveFileEx can delete empty folder only, so we must explictly delete files first
ReleaseStrArray(pszFiles, nFiles);
hr = ListFiles(szFrom, L"*", true, &pszFiles, &nFiles);
ExitOnFailure(hr, "Failed listing files in folder '%ls'", szFrom);

for (UINT i = 0; i < nFiles; ++i)
if (::PathIsDirectory(szFrom))
{
::MoveFileEx(pszFiles[i], nullptr, MOVEFILE_DELAY_UNTIL_REBOOT);
if (pszFiles)
{
ReleaseStrArray(pszFiles, nFiles);
pszFiles = nullptr;
nFiles = 0;
}

hr = ListFiles(szFrom, L"*", true, &pszFiles, &nFiles);
ExitOnFailure(hr, "Failed listing files in folder '%ls'", szFrom);

for (UINT i = 0; i < nFiles; ++i)
{
DeletePath(pszFiles[i], bIgnoreMissing, bIgnoreErrors, bOnlyIfEmpty, bAllowReboot);
}
}

::MoveFileEx(szFromNull, nullptr, MOVEFILE_DELAY_UNTIL_REBOOT);
Expand Down Expand Up @@ -517,7 +530,7 @@ HRESULT CFileOperations::ListFiles(LPCWSTR szFolder, LPCWSTR szPattern, bool bRe

if (IsSymbolicLinkOrMount(szFolder))
{
WcaLog(LOGLEVEL::LOGMSG_VERBOSE, "Folder '%ls' is a symbolic link or a mount point, so not enumerating its files");
WcaLog(LOGLEVEL::LOGMSG_VERBOSE, "Folder '%ls' is a symbolic link or a mount point, so not enumerating its files", szFolder);
ExitFunction();
}

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.17.2</FullVersion>
<FullVersion>3.17.3</FullVersion>
<FullVersion Condition=" '$(GITHUB_RUN_NUMBER)'!='' ">$(FullVersion).$(GITHUB_RUN_NUMBER)</FullVersion>
<ProductName>PanelSwWixExtension</ProductName>
<Manufacturer>Panel::Software</Manufacturer>
Expand Down

0 comments on commit dfeb8ca

Please sign in to comment.