From cc240536956e3ef6981599dfff05aa5628e910ac Mon Sep 17 00:00:00 2001
From: Sean Hall <r.sean.hall@gmail.com>
Date: Fri, 16 Apr 2021 13:40:25 -0500
Subject: Perform all layout operations in the BA process.

---
 src/engine/apply.cpp     |  27 +++---
 src/engine/elevation.cpp | 214 +++++++----------------------------------------
 src/engine/elevation.h   |  21 ++---
 3 files changed, 45 insertions(+), 217 deletions(-)

(limited to 'src/engine')

diff --git a/src/engine/apply.cpp b/src/engine/apply.cpp
index ab7fa077..cf904405 100644
--- a/src/engine/apply.cpp
+++ b/src/engine/apply.cpp
@@ -1076,13 +1076,13 @@ static HRESULT ApplyCacheVerifyContainerOrPayload(
     hr = UserExperienceOnCacheContainerOrPayloadVerifyBegin(pContext->pUX, wzPackageOrContainerId, wzPayloadId);
     ExitOnRootFailure(hr, "BA aborted cache container or payload verify begin.");
 
-    if (INVALID_HANDLE_VALUE != pContext->hPipe)
+    if (pContainer)
     {
-        hr = ElevationCacheVerifyContainerOrPayload(pContext->hPipe, pContainer, pPackage, pPayloadGroupItem->pPayload, pContext->wzLayoutDirectory);
+        hr = CacheVerifyContainer(pContainer, pContext->wzLayoutDirectory);
     }
-    else if (pContainer)
+    else if (!pContext->wzLayoutDirectory && INVALID_HANDLE_VALUE != pContext->hPipe)
     {
-        hr = CacheVerifyContainer(pContainer, pContext->wzLayoutDirectory);
+        hr = ElevationCacheVerifyPayload(pContext->hPipe, pPackage, pPayloadGroupItem->pPayload);
     }
     else
     {
@@ -1289,14 +1289,7 @@ static HRESULT LayoutBundle(
             hr = UserExperienceOnCacheVerifyBegin(pContext->pUX, NULL, NULL);
             ExitOnRootFailure(hr, "BA aborted cache verify begin.");
 
-            if (INVALID_HANDLE_VALUE != pContext->hPipe)
-            {
-                hr = ElevationLayoutBundle(pContext->hPipe, pContext->wzLayoutDirectory, wzUnverifiedPath);
-            }
-            else
-            {
-                hr = CacheLayoutBundle(wzExecutableName, pContext->wzLayoutDirectory, wzUnverifiedPath);
-            }
+            hr = CacheLayoutBundle(wzExecutableName, pContext->wzLayoutDirectory, wzUnverifiedPath);
 
             if (SUCCEEDED(hr))
             {
@@ -1539,11 +1532,7 @@ static HRESULT LayoutOrCacheContainerOrPayload(
         hr = UserExperienceOnCacheVerifyBegin(pContext->pUX, wzPackageOrContainerId, wzPayloadId);
         ExitOnRootFailure(hr, "BA aborted cache verify begin.");
 
-        if (INVALID_HANDLE_VALUE != pContext->hPipe) // pass the decision off to the elevated process.
-        {
-            hr = ElevationCacheOrLayoutContainerOrPayload(pContext->hPipe, pContainer, pPackage, pPayload, pContext->wzLayoutDirectory, wzUnverifiedPath, fMove);
-        }
-        else if (pContext->wzLayoutDirectory) // layout the container or payload.
+        if (pContext->wzLayoutDirectory) // layout the container or payload.
         {
             if (pContainer)
             {
@@ -1554,6 +1543,10 @@ static HRESULT LayoutOrCacheContainerOrPayload(
                 hr = CacheLayoutPayload(pPayload, pContext->wzLayoutDirectory, wzUnverifiedPath, fMove);
             }
         }
+        else if (INVALID_HANDLE_VALUE != pContext->hPipe) // pass the decision off to the elevated process.
+        {
+            hr = ElevationCacheCompletePayload(pContext->hPipe, pPackage, pPayload, wzUnverifiedPath, fMove);
+        }
         else // complete the payload.
         {
             hr = CacheCompletePayload(pPackage->fPerMachine, pPayload, pPackage->sczCacheId, wzUnverifiedPath, fMove);
diff --git a/src/engine/elevation.cpp b/src/engine/elevation.cpp
index 2dd61a81..502c5462 100644
--- a/src/engine/elevation.cpp
+++ b/src/engine/elevation.cpp
@@ -14,9 +14,8 @@ typedef enum _BURN_ELEVATION_MESSAGE_TYPE
     BURN_ELEVATION_MESSAGE_TYPE_SESSION_RESUME,
     BURN_ELEVATION_MESSAGE_TYPE_SESSION_END,
     BURN_ELEVATION_MESSAGE_TYPE_SAVE_STATE,
-    BURN_ELEVATION_MESSAGE_TYPE_LAYOUT_BUNDLE,
-    BURN_ELEVATION_MESSAGE_TYPE_CACHE_OR_LAYOUT_CONTAINER_OR_PAYLOAD,
-    BURN_ELEVATION_MESSAGE_TYPE_CACHE_VERIFY_CONTAINER_OR_PAYLOAD,
+    BURN_ELEVATION_MESSAGE_TYPE_CACHE_COMPLETE_PAYLOAD,
+    BURN_ELEVATION_MESSAGE_TYPE_CACHE_VERIFY_PAYLOAD,
     BURN_ELEVATION_MESSAGE_TYPE_CACHE_CLEANUP,
     BURN_ELEVATION_MESSAGE_TYPE_PROCESS_DEPENDENT_REGISTRATION,
     BURN_ELEVATION_MESSAGE_TYPE_EXECUTE_EXE_PACKAGE,
@@ -165,20 +164,13 @@ static HRESULT OnSaveState(
     __in BYTE* pbData,
     __in DWORD cbData
     );
-static HRESULT OnLayoutBundle(
-    __in_z LPCWSTR wzExecutableName,
-    __in BYTE* pbData,
-    __in DWORD cbData
-    );
-static HRESULT OnCacheOrLayoutContainerOrPayload(
-    __in BURN_CONTAINERS* pContainers,
+static HRESULT OnCacheCompletePayload(
     __in BURN_PACKAGES* pPackages,
     __in BURN_PAYLOADS* pPayloads,
     __in BYTE* pbData,
     __in DWORD cbData
     );
-static HRESULT OnCacheVerifyContainerOrPayload(
-    __in BURN_CONTAINERS* pContainers,
+static HRESULT OnCacheVerifyPayload(
     __in BURN_PACKAGES* pPackages,
     __in BURN_PAYLOADS* pPayloads,
     __in BYTE* pbData,
@@ -582,49 +574,13 @@ LExit:
 }
 
 /*******************************************************************
- ElevationLayoutBundle - 
+ ElevationCacheCompletePayload - 
 
 *******************************************************************/
-extern "C" HRESULT ElevationLayoutBundle(
+extern "C" HRESULT ElevationCacheCompletePayload(
     __in HANDLE hPipe,
-    __in_z LPCWSTR wzLayoutDirectory,
-    __in_z LPCWSTR wzUnverifiedPath
-    )
-{
-    HRESULT hr = S_OK;
-    BYTE* pbData = NULL;
-    SIZE_T cbData = 0;
-    DWORD dwResult = 0;
-
-    // serialize message data
-    hr = BuffWriteString(&pbData, &cbData, wzLayoutDirectory);
-    ExitOnFailure(hr, "Failed to write layout directory to message buffer.");
-
-    hr = BuffWriteString(&pbData, &cbData, wzUnverifiedPath);
-    ExitOnFailure(hr, "Failed to write payload unverified path to message buffer.");
-
-    // send message
-    hr = PipeSendMessage(hPipe, BURN_ELEVATION_MESSAGE_TYPE_LAYOUT_BUNDLE, pbData, cbData, NULL, NULL, &dwResult);
-    ExitOnFailure(hr, "Failed to send BURN_ELEVATION_MESSAGE_TYPE_LAYOUT_BUNDLE message to per-machine process.");
-
-    hr = (HRESULT)dwResult;
-
-LExit:
-    ReleaseBuffer(pbData);
-
-    return hr;
-}
-
-/*******************************************************************
- ElevationCacheOrLayoutPayload - 
-
-*******************************************************************/
-extern "C" HRESULT ElevationCacheOrLayoutContainerOrPayload(
-    __in HANDLE hPipe,
-    __in_opt BURN_CONTAINER* pContainer,
-    __in_opt BURN_PACKAGE* pPackage,
-    __in_opt BURN_PAYLOAD* pPayload,
-    __in_z_opt LPCWSTR wzLayoutDirectory,
+    __in BURN_PACKAGE* pPackage,
+    __in BURN_PAYLOAD* pPayload,
     __in_z LPCWSTR wzUnverifiedPath,
     __in BOOL fMove
     )
@@ -635,18 +591,12 @@ extern "C" HRESULT ElevationCacheOrLayoutContainerOrPayload(
     DWORD dwResult = 0;
 
     // serialize message data
-    hr = BuffWriteString(&pbData, &cbData, pContainer ? pContainer->sczId : NULL);
-    ExitOnFailure(hr, "Failed to write container id to message buffer.");
-
-    hr = BuffWriteString(&pbData, &cbData, pPackage ? pPackage->sczId : NULL);
+    hr = BuffWriteString(&pbData, &cbData, pPackage->sczId);
     ExitOnFailure(hr, "Failed to write package id to message buffer.");
 
-    hr = BuffWriteString(&pbData, &cbData, pPayload ? pPayload->sczKey : NULL);
+    hr = BuffWriteString(&pbData, &cbData, pPayload->sczKey);
     ExitOnFailure(hr, "Failed to write payload id to message buffer.");
 
-    hr = BuffWriteString(&pbData, &cbData, wzLayoutDirectory);
-    ExitOnFailure(hr, "Failed to write layout directory to message buffer.");
-
     hr = BuffWriteString(&pbData, &cbData, wzUnverifiedPath);
     ExitOnFailure(hr, "Failed to write unverified path to message buffer.");
 
@@ -654,8 +604,8 @@ extern "C" HRESULT ElevationCacheOrLayoutContainerOrPayload(
     ExitOnFailure(hr, "Failed to write move flag to message buffer.");
 
     // send message
-    hr = PipeSendMessage(hPipe, BURN_ELEVATION_MESSAGE_TYPE_CACHE_OR_LAYOUT_CONTAINER_OR_PAYLOAD, pbData, cbData, NULL, NULL, &dwResult);
-    ExitOnFailure(hr, "Failed to send BURN_ELEVATION_MESSAGE_TYPE_CACHE_OR_LAYOUT_CONTAINER_OR_PAYLOAD message to per-machine process.");
+    hr = PipeSendMessage(hPipe, BURN_ELEVATION_MESSAGE_TYPE_CACHE_COMPLETE_PAYLOAD, pbData, cbData, NULL, NULL, &dwResult);
+    ExitOnFailure(hr, "Failed to send BURN_ELEVATION_MESSAGE_TYPE_CACHE_COMPLETE_PAYLOAD message to per-machine process.");
 
     hr = (HRESULT)dwResult;
 
@@ -665,12 +615,10 @@ LExit:
     return hr;
 }
 
-extern "C" HRESULT ElevationCacheVerifyContainerOrPayload(
+extern "C" HRESULT ElevationCacheVerifyPayload(
     __in HANDLE hPipe,
-    __in_opt BURN_CONTAINER* pContainer,
-    __in_opt BURN_PACKAGE* pPackage,
-    __in_opt BURN_PAYLOAD* pPayload,
-    __in_z_opt LPCWSTR wzLayoutDirectory
+    __in BURN_PACKAGE* pPackage,
+    __in BURN_PAYLOAD* pPayload
     )
 {
     HRESULT hr = S_OK;
@@ -679,21 +627,15 @@ extern "C" HRESULT ElevationCacheVerifyContainerOrPayload(
     DWORD dwResult = 0;
 
     // serialize message data
-    hr = BuffWriteString(&pbData, &cbData, pContainer ? pContainer->sczId : NULL);
-    ExitOnFailure(hr, "Failed to write container id to message buffer.");
-
-    hr = BuffWriteString(&pbData, &cbData, pPackage ? pPackage->sczId : NULL);
+    hr = BuffWriteString(&pbData, &cbData, pPackage->sczId);
     ExitOnFailure(hr, "Failed to write package id to message buffer.");
 
-    hr = BuffWriteString(&pbData, &cbData, pPayload ? pPayload->sczKey : NULL);
+    hr = BuffWriteString(&pbData, &cbData, pPayload->sczKey);
     ExitOnFailure(hr, "Failed to write payload id to message buffer.");
 
-    hr = BuffWriteString(&pbData, &cbData, wzLayoutDirectory);
-    ExitOnFailure(hr, "Failed to write layout directory to message buffer.");
-
     // send message
-    hr = PipeSendMessage(hPipe, BURN_ELEVATION_MESSAGE_TYPE_CACHE_VERIFY_CONTAINER_OR_PAYLOAD, pbData, cbData, NULL, NULL, &dwResult);
-    ExitOnFailure(hr, "Failed to send BURN_ELEVATION_MESSAGE_TYPE_CACHE_VERIFY_CONTAINER_OR_PAYLOAD message to per-machine process.");
+    hr = PipeSendMessage(hPipe, BURN_ELEVATION_MESSAGE_TYPE_CACHE_VERIFY_PAYLOAD, pbData, cbData, NULL, NULL, &dwResult);
+    ExitOnFailure(hr, "Failed to send BURN_ELEVATION_MESSAGE_TYPE_CACHE_VERIFY_PAYLOAD message to per-machine process.");
 
     hr = (HRESULT)dwResult;
 
@@ -1762,16 +1704,12 @@ static HRESULT ProcessElevatedChildCacheMessage(
 
     switch (pMsg->dwMessage)
     {
-    case BURN_ELEVATION_MESSAGE_TYPE_LAYOUT_BUNDLE:
-        hrResult = OnLayoutBundle(pContext->pRegistration->sczExecutableName, (BYTE*)pMsg->pvData, pMsg->cbData);
-        break;
-
-    case BURN_ELEVATION_MESSAGE_TYPE_CACHE_OR_LAYOUT_CONTAINER_OR_PAYLOAD:
-        hrResult = OnCacheOrLayoutContainerOrPayload(pContext->pContainers, pContext->pPackages, pContext->pPayloads, (BYTE*)pMsg->pvData, pMsg->cbData);
+    case BURN_ELEVATION_MESSAGE_TYPE_CACHE_COMPLETE_PAYLOAD:
+        hrResult = OnCacheCompletePayload(pContext->pPackages, pContext->pPayloads, (BYTE*)pMsg->pvData, pMsg->cbData);
         break;
 
-    case BURN_ELEVATION_MESSAGE_TYPE_CACHE_VERIFY_CONTAINER_OR_PAYLOAD:
-        hrResult = OnCacheVerifyContainerOrPayload(pContext->pContainers, pContext->pPackages, pContext->pPayloads, (BYTE*)pMsg->pvData, pMsg->cbData);
+    case BURN_ELEVATION_MESSAGE_TYPE_CACHE_VERIFY_PAYLOAD:
+        hrResult = OnCacheVerifyPayload(pContext->pPackages, pContext->pPayloads, (BYTE*)pMsg->pvData, pMsg->cbData);
         break;
 
     case BURN_ELEVATION_MESSAGE_TYPE_CACHE_CLEANUP:
@@ -2063,37 +2001,7 @@ LExit:
     return hr;
 }
 
-static HRESULT OnLayoutBundle(
-    __in_z LPCWSTR wzExecutableName,
-    __in BYTE* pbData,
-    __in DWORD cbData
-    )
-{
-    HRESULT hr = S_OK;
-    SIZE_T iData = 0;
-    LPWSTR sczLayoutDirectory = NULL;
-    LPWSTR sczUnverifiedPath = NULL;
-
-    // Deserialize message data.
-    hr = BuffReadString(pbData, cbData, &iData, &sczLayoutDirectory);
-    ExitOnFailure(hr, "Failed to read layout directory.");
-
-    hr = BuffReadString(pbData, cbData, &iData, &sczUnverifiedPath);
-    ExitOnFailure(hr, "Failed to read unverified bundle path.");
-
-    // Layout the bundle.
-    hr = CacheLayoutBundle(wzExecutableName, sczLayoutDirectory, sczUnverifiedPath);
-    ExitOnFailure(hr, "Failed to layout bundle from: %ls", sczUnverifiedPath);
-
-LExit:
-    ReleaseStr(sczUnverifiedPath);
-    ReleaseStr(sczLayoutDirectory);
-
-    return hr;
-}
-
-static HRESULT OnCacheOrLayoutContainerOrPayload(
-    __in BURN_CONTAINERS* pContainers,
+static HRESULT OnCacheCompletePayload(
     __in BURN_PACKAGES* pPackages,
     __in BURN_PAYLOADS* pPayloads,
     __in BYTE* pbData,
@@ -2103,23 +2011,12 @@ static HRESULT OnCacheOrLayoutContainerOrPayload(
     HRESULT hr = S_OK;
     SIZE_T iData = 0;
     LPWSTR scz = NULL;
-    BURN_CONTAINER* pContainer = NULL;
     BURN_PACKAGE* pPackage = NULL;
     BURN_PAYLOAD* pPayload = NULL;
-    LPWSTR sczLayoutDirectory = NULL;
     LPWSTR sczUnverifiedPath = NULL;
     BOOL fMove = FALSE;
 
     // Deserialize message data.
-    hr = BuffReadString(pbData, cbData, &iData, &scz);
-    ExitOnFailure(hr, "Failed to read container id.");
-
-    if (scz && *scz)
-    {
-        hr = ContainerFindById(pContainers, scz, &pContainer);
-        ExitOnFailure(hr, "Failed to find container: %ls", scz);
-    }
-
     hr = BuffReadString(pbData, cbData, &iData, &scz);
     ExitOnFailure(hr, "Failed to read package id.");
 
@@ -2138,55 +2035,31 @@ static HRESULT OnCacheOrLayoutContainerOrPayload(
         ExitOnFailure(hr, "Failed to find payload: %ls", scz);
     }
 
-    hr = BuffReadString(pbData, cbData, &iData, &sczLayoutDirectory);
-    ExitOnFailure(hr, "Failed to read layout directory.");
-
     hr = BuffReadString(pbData, cbData, &iData, &sczUnverifiedPath);
     ExitOnFailure(hr, "Failed to read unverified path.");
 
     hr = BuffReadNumber(pbData, cbData, &iData, (DWORD*)&fMove);
     ExitOnFailure(hr, "Failed to read move flag.");
 
-    // Layout payload.
-    if (sczLayoutDirectory && *sczLayoutDirectory)
+    if (pPackage && pPayload) // complete payload.
     {
-        if (pContainer)
-        {
-            Assert(!pPackage);
-            Assert(!pPayload);
-
-            hr = CacheLayoutContainer(pContainer, sczLayoutDirectory, sczUnverifiedPath, fMove);
-            ExitOnFailure(hr, "Failed to layout container from: %ls to %ls", sczUnverifiedPath, sczLayoutDirectory);
-        }
-        else
-        {
-            hr = CacheLayoutPayload(pPayload, sczLayoutDirectory, sczUnverifiedPath, fMove);
-            ExitOnFailure(hr, "Failed to layout payload from: %ls to %ls", sczUnverifiedPath, sczLayoutDirectory);
-        }
-    }
-    else if (pPackage) // complete payload.
-    {
-        Assert(!pContainer);
-
         hr = CacheCompletePayload(pPackage->fPerMachine, pPayload, pPackage->sczCacheId, sczUnverifiedPath, fMove);
         ExitOnFailure(hr, "Failed to cache payload: %ls", pPayload->sczKey);
     }
     else
     {
         hr = E_INVALIDARG;
-        ExitOnRootFailure(hr, "Invalid data passed to cache or layout payload.");
+        ExitOnRootFailure(hr, "Invalid data passed to cache complete payload.");
     }
 
 LExit:
     ReleaseStr(sczUnverifiedPath);
-    ReleaseStr(sczLayoutDirectory);
     ReleaseStr(scz);
 
     return hr;
 }
 
-static HRESULT OnCacheVerifyContainerOrPayload(
-    __in BURN_CONTAINERS* pContainers,
+static HRESULT OnCacheVerifyPayload(
     __in BURN_PACKAGES* pPackages,
     __in BURN_PAYLOADS* pPayloads,
     __in BYTE* pbData,
@@ -2196,21 +2069,11 @@ static HRESULT OnCacheVerifyContainerOrPayload(
     HRESULT hr = S_OK;
     SIZE_T iData = 0;
     LPWSTR scz = NULL;
-    BURN_CONTAINER* pContainer = NULL;
     BURN_PACKAGE* pPackage = NULL;
     BURN_PAYLOAD* pPayload = NULL;
     LPWSTR sczCacheDirectory = NULL;
 
     // Deserialize message data.
-    hr = BuffReadString(pbData, cbData, &iData, &scz);
-    ExitOnFailure(hr, "Failed to read container id.");
-
-    if (scz && *scz)
-    {
-        hr = ContainerFindById(pContainers, scz, &pContainer);
-        ExitOnFailure(hr, "Failed to find container: %ls", scz);
-    }
-
     hr = BuffReadString(pbData, cbData, &iData, &scz);
     ExitOnFailure(hr, "Failed to read package id.");
 
@@ -2229,36 +2092,17 @@ static HRESULT OnCacheVerifyContainerOrPayload(
         ExitOnFailure(hr, "Failed to find payload: %ls", scz);
     }
 
-    hr = BuffReadString(pbData, cbData, &iData, &sczCacheDirectory);
-    ExitOnFailure(hr, "Failed to read layout directory.");
-
-    if (!sczCacheDirectory || !*sczCacheDirectory)
+    if (pPackage && pPayload)
     {
-        if (!pPackage)
-        {
-            hr = E_INVALIDARG;
-            ExitOnRootFailure(hr, "Invalid data passed to cache verify payload.");
-        }
-
         hr = CacheGetCompletedPath(TRUE, pPackage->sczCacheId, &sczCacheDirectory);
         ExitOnFailure(hr, "Failed to get cached path for package with cache id: %ls", pPackage->sczCacheId);
-    }
-
-    if (pContainer)
-    {
-        Assert(!pPackage);
-        Assert(!pPayload);
 
-        hr = CacheVerifyContainer(pContainer, sczCacheDirectory);
-    }
-    else if (pPayload)
-    {
         hr = CacheVerifyPayload(pPayload, sczCacheDirectory);
     }
     else
     {
         hr = E_INVALIDARG;
-        ExitOnRootFailure(hr, "Invalid data passed to cache or layout payload.");
+        ExitOnRootFailure(hr, "Invalid data passed to cache verify payload.");
     }
     // Nothing should be logged on failure.
 
diff --git a/src/engine/elevation.h b/src/engine/elevation.h
index da67e3f3..fd7ee110 100644
--- a/src/engine/elevation.h
+++ b/src/engine/elevation.h
@@ -50,26 +50,17 @@ HRESULT ElevationSaveState(
     __in_bcount(cbBuffer) BYTE* pbBuffer,
     __in SIZE_T cbBuffer
     );
-HRESULT ElevationLayoutBundle(
+HRESULT ElevationCacheCompletePayload(
     __in HANDLE hPipe,
-    __in_z LPCWSTR wzLayoutDirectory,
-    __in_z LPCWSTR wzUnverifiedPath
-    );
-HRESULT ElevationCacheOrLayoutContainerOrPayload(
-    __in HANDLE hPipe,
-    __in_opt BURN_CONTAINER* pContainer,
-    __in_opt BURN_PACKAGE* pPackage,
-    __in_opt BURN_PAYLOAD* pPayload,
-    __in_z_opt LPCWSTR wzLayoutDirectory,
+    __in BURN_PACKAGE* pPackage,
+    __in BURN_PAYLOAD* pPayload,
     __in_z LPCWSTR wzUnverifiedPath,
     __in BOOL fMove
     );
-HRESULT ElevationCacheVerifyContainerOrPayload(
+HRESULT ElevationCacheVerifyPayload(
     __in HANDLE hPipe,
-    __in_opt BURN_CONTAINER* pContainer,
-    __in_opt BURN_PACKAGE* pPackage,
-    __in_opt BURN_PAYLOAD* pPayload,
-    __in_z_opt LPCWSTR wzLayoutDirectory
+    __in BURN_PACKAGE* pPackage,
+    __in BURN_PAYLOAD* pPayload
     );
 HRESULT ElevationCacheCleanup(
     __in HANDLE hPipe
-- 
cgit v1.2.3-55-g6feb