Skip to content

Commit

Permalink
Attempt once to create a hard link to containers before verifying the…
Browse files Browse the repository at this point in the history
…m, instead of copying
  • Loading branch information
nirbar committed Oct 30, 2024
1 parent ccb6cb3 commit 7a7ff66
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/burn/engine/apply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ static HRESULT CopyPayload(
__in BURN_CACHE_PROGRESS_CONTEXT* pProgress,
__in HANDLE hSourceFile,
__in_z LPCWSTR wzSourcePath,
__in_z LPCWSTR wzDestinationPath
__in_z LPCWSTR wzDestinationPath,
__in BOOL fAllowHardLink
);
static HRESULT DownloadPayload(
__in BURN_CACHE_PROGRESS_CONTEXT* pProgress,
Expand Down Expand Up @@ -1489,7 +1490,7 @@ static HRESULT LayoutBundle(
}
else
{
hr = CopyPayload(&progress, pContext->hSourceEngineFile, sczBundlePath, wzUnverifiedPath);
hr = CopyPayload(&progress, pContext->hSourceEngineFile, sczBundlePath, wzUnverifiedPath, FALSE);
// Error handling happens after sending complete message to BA.

// If succeeded, send 100% complete here to make sure progress was sent to the BA.
Expand Down Expand Up @@ -1624,6 +1625,7 @@ static HRESULT AcquireContainerOrPayload(
DWORD64 qwFileSize = 0;
BOOL fMinimumFileSize = FALSE;
BOOL fEqual = FALSE;
BOOL fAllowHardLink = pContainer && !*pfRetry; // Not allowing hard links on payloads to ensure it isn't modified after verification. We don't mind hard links on containers because the extracted files will not be suspectible to modifications

if (pContainer)
{
Expand Down Expand Up @@ -1776,7 +1778,7 @@ static HRESULT AcquireContainerOrPayload(

if (!fPathEqual)
{
hr = CopyPayload(pProgress, INVALID_HANDLE_VALUE, pContext->rgSearchPaths[dwChosenSearchPath], wzDestinationPath);
hr = CopyPayload(pProgress, INVALID_HANDLE_VALUE, pContext->rgSearchPaths[dwChosenSearchPath], wzDestinationPath, fAllowHardLink);
ExitOnFailure(hr, "Failed to copy payload: %ls", wzPayloadId);

// Store the source path so it can be used as the LastUsedFolder if it passes verification.
Expand Down Expand Up @@ -1979,7 +1981,8 @@ static HRESULT CopyPayload(
__in BURN_CACHE_PROGRESS_CONTEXT* pProgress,
__in HANDLE hSourceFile,
__in_z LPCWSTR wzSourcePath,
__in_z LPCWSTR wzDestinationPath
__in_z LPCWSTR wzDestinationPath,
__in BOOL fAllowHardLink
)
{
HRESULT hr = S_OK;
Expand All @@ -1994,6 +1997,25 @@ static HRESULT CopyPayload(
hr = PreparePayloadDestinationPath(wzDestinationPath);
ExitOnFailure(hr, "Failed to prepare payload destination path: %ls", wzDestinationPath);

// Hard links are sufficient because the payload will be verified before copying to the final destination
if (fAllowHardLink)
{
if (::CreateHardLinkW(wzDestinationPath, wzSourcePath, NULL))
{
LARGE_INTEGER liSourceSize = { };
LARGE_INTEGER liZero = { };

FileSize(wzSourcePath, &liSourceSize.QuadPart);
CacheProgressRoutine(liSourceSize, liSourceSize, liZero, liZero, 0, CALLBACK_CHUNK_FINISHED, INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, pProgress);
LogId(REPORT_STANDARD, MSG_PAYLOAD_HARD_LINK, wzSourcePath, wzDestinationPath);
ExitFunction();
}
else
{
LogId(REPORT_WARNING, MSG_PAYLOAD_HARD_LINK_FAILED, wzSourcePath, ::GetLastError());
}
}

if (INVALID_HANDLE_VALUE == hSourceFile)
{
hSourceOpenedFile = ::CreateFileW(wzSourcePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
Expand Down
14 changes: 14 additions & 0 deletions src/burn/engine/engine.mc
Original file line number Diff line number Diff line change
Expand Up @@ -1310,3 +1310,17 @@ SymbolicName=MSG_UX_PAYLOAD_MISSING
Language=English
UX payload deletion was detected, Id: '%1!ls!', path: '%2!ls!', action: %3!ls!.
.
MessageId=705
Severity=Success
SymbolicName=MSG_PAYLOAD_HARD_LINK
Language=English
Created a hard link from '%1!ls!' to '%2!ls!'.
.
MessageId=706
Severity=Warning
SymbolicName=MSG_PAYLOAD_HARD_LINK_FAILED
Language=English
Creating a hard link to '%1!ls!' failed with error code %2!u!. Resorting to plain copy.
.

0 comments on commit 7a7ff66

Please sign in to comment.