Skip to content

Commit

Permalink
Replace custom path handling in MRT (#908)
Browse files Browse the repository at this point in the history
* Replace custom path handling

* use default size
  • Loading branch information
huichen123 committed Jun 4, 2021
1 parent 2af7b0f commit c1653e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 59 deletions.
3 changes: 1 addition & 2 deletions build/build-mrt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#name: $(BuildDefinitionName)-$(date:yyMM).$(date:dd)$(rev:rrr)
parameters:
appxPackageDir: '$(build.artifactStagingDirectory)\AppxPackages\\'
MRTSourcesDirectory: $(Build.SourcesDirectory)\dev\MRTCore
MRTBinariesDirectory: $(Build.SourcesDirectory)\BuildOutput

Expand Down Expand Up @@ -114,7 +113,7 @@ steps:
platform: '$(buildPlatform)'
solution: '${{ parameters.MRTSourcesDirectory }}\mrt\MrtCore.sln'
configuration: '$(buildConfiguration)'
msbuildArguments: '/restore /p:AppxBundlePlatforms="$(buildPlatform)" /p:AppxPackageDir="${{ parameters.appxPackageDir }}" /p:AppxBundle=Always /p:UapAppxPackageBuildMode=StoreUpload /binaryLogger:$(Build.SourcesDirectory)/mrtcore.$(buildPlatform).$(buildConfiguration).binlog'
msbuildArguments: '/restore /binaryLogger:$(Build.SourcesDirectory)/mrtcore.$(buildPlatform).$(buildConfiguration).binlog'

- task: PublishBuildArtifacts@1
displayName: 'Publish mrtcore binlog'
Expand Down
83 changes: 26 additions & 57 deletions dev/MRTCore/mrt/Core/src/MRM.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

#include <Windows.h>
#include <Pathcch.h>
#include "wil/win32_helpers.h"
#include "wil/filesystem.h"
#include "mrm/BaseInternal.h"
#include "mrm/common/platform.h"
#include "mrm/readers/MrmReaders.h"
Expand Down Expand Up @@ -874,49 +876,20 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
{
*filePath = nullptr;

wchar_t path[MAX_PATH];
DWORD size = ARRAYSIZE(path);
DWORD length = GetModuleFileName(nullptr, path, size);
DWORD lastError = GetLastError();
std::unique_ptr<wchar_t[]> pathAllocated;
wil::unique_cotaskmem_string path;
RETURN_IF_FAILED(wil::GetModuleFileNameW(nullptr, path));

while ((length == size) && (lastError == ERROR_INSUFFICIENT_BUFFER))
{
size *= 2;
RETURN_HR_IF(E_UNEXPECTED, size > 32 * 1024);

pathAllocated.reset(new (std::nothrow) wchar_t[size]);
RETURN_IF_NULL_ALLOC(pathAllocated);

length = GetModuleFileName(nullptr, pathAllocated.get(), size);
lastError = GetLastError();
}

RETURN_HR_IF(HRESULT_FROM_WIN32(lastError), lastError != 0);

// Remove module file name
PWSTR pointerToPath = pathAllocated ? pathAllocated.get() : path;
PWSTR lastSlash = nullptr;
PWSTR secondToLastSlash = nullptr;
while (*pointerToPath)
{
if (*pointerToPath == L'\\')
{
secondToLastSlash = lastSlash;
lastSlash = pointerToPath;
}
pointerToPath++;
}
PCWSTR name = wil::find_last_path_segment(path.get());
wchar_t moduleFilename[MAX_PATH];
RETURN_IF_FAILED(StringCchCopyW(moduleFilename, ARRAYSIZE(moduleFilename), *name ? name : path.get()));

pointerToPath = pathAllocated ? pathAllocated.get() : path;
size_t length;
RETURN_IF_FAILED(StringCchLengthW(path.get(), STRSAFE_MAX_CCH, &length));

wchar_t moduleFilename[MAX_PATH];
RETURN_IF_FAILED(StringCchCopyW(moduleFilename, ARRAYSIZE(moduleFilename), lastSlash != nullptr ? lastSlash + 1 : pointerToPath));
size_t bufferCount;
RETURN_IF_FAILED(SizeTAdd(length, 1, &bufferCount));

if (lastSlash != nullptr)
{
*(lastSlash + 1) = 0;
}
RETURN_IF_FAILED(PathCchRemoveFileSpec(path.get(), bufferCount));

std::unique_ptr<wchar_t, decltype(&MrmFreeResource)> finalPath(nullptr, MrmFreeResource);

Expand All @@ -935,24 +908,25 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
// - if not exist, search under same path with [modulename].pri
for (int i = 0; i < 2; i++)
{
size_t lengthInSizeT;
RETURN_IF_FAILED(StringCchLengthW(pointerToPath, STRSAFE_MAX_CCH, &lengthInSizeT));
length = static_cast<DWORD>(lengthInSizeT);
size_t lengthOfName;
RETURN_IF_FAILED(StringCchLengthW(filenameToUse, STRSAFE_MAX_CCH, &lengthOfName));

RETURN_IF_FAILED(StringCchLengthW(filenameToUse, STRSAFE_MAX_CCH, &lengthInSizeT));
DWORD lengthOfName = static_cast<DWORD>(lengthInSizeT);
// We over-allocate a little bit so that we don't have to calculate the path length each time.
RETURN_IF_FAILED(SizeTAdd(bufferCount, lengthOfName, &length));

RETURN_IF_FAILED(DWordAdd(length, lengthOfName, &length));
RETURN_IF_FAILED(DWordAdd(length, 1, &length));
RETURN_IF_FAILED(DWordMult(length, sizeof(wchar_t), &size));
size_t size;
RETURN_IF_FAILED(SizeTMult(length, sizeof(wchar_t), &size));

PWSTR rawOutputPath = reinterpret_cast<PWSTR>(MrmAllocateBuffer(size));
RETURN_IF_NULL_ALLOC(rawOutputPath);

std::unique_ptr<wchar_t, decltype(&MrmFreeResource)> outputPath(rawOutputPath, MrmFreeResource);

RETURN_IF_FAILED(StringCchCopyW(outputPath.get(), length, pointerToPath));
RETURN_IF_FAILED(StringCchCatW(outputPath.get(), length, filenameToUse));
RETURN_IF_FAILED(PathCchCombineEx(
outputPath.get(),
size,
path.get(),
filenameToUse,
PATHCCH_ALLOW_LONG_PATHS));

DWORD attributes = GetFileAttributes(outputPath.get());

Expand All @@ -979,12 +953,7 @@ STDAPI MrmGetFilePathFromName(_In_opt_ PCWSTR filename, _Outptr_ PWSTR* filePath
else
{
// move to parent folder
if (secondToLastSlash == nullptr)
{
break;
}

*(secondToLastSlash + 1) = 0;
RETURN_IF_FAILED(PathCchRemoveFileSpec(path.get(), bufferCount));
}
}
}
Expand Down

0 comments on commit c1653e8

Please sign in to comment.