Skip to content

Commit 2168950

Browse files
adityamandaleekapull[bot]
authored andcommitted
Clean up some code in ANCM (#49718)
Misc code cleanup in ANCM.
1 parent de2db9c commit 2168950

File tree

6 files changed

+56
-85
lines changed

6 files changed

+56
-85
lines changed

src/Servers/IIS/AspNetCoreModuleV2/AspNetCore/globalmodule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ASPNET_CORE_GLOBAL_MODULE::OnGlobalConfigurationChange(
6363
// We need this duplicate setting because the global module is unable to read the app settings disallowRotationOnConfigChange value.
6464
if (m_pApplicationManager && m_pApplicationManager->ShouldRecycleOnConfigChange())
6565
{
66-
m_pApplicationManager->RecycleApplicationFromManager(pwszChangePath);
66+
m_pApplicationManager->RecycleApplicationFromManager(pwszChangePath);
6767
}
6868
}
6969

src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HandleWrapper.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ struct NullHandleTraits
2020
static void Close(HANDLE handle) noexcept { CloseHandle(handle); }
2121
};
2222

23-
struct FindFileHandleTraits
24-
{
25-
using HandleType = HANDLE;
26-
static constexpr HANDLE DefaultHandle = nullptr;
27-
static void Close(HANDLE handle) noexcept { FindClose(handle); }
28-
};
29-
3023
struct ModuleHandleTraits
3124
{
3225
using HandleType = HMODULE;

src/Servers/IIS/AspNetCoreModuleV2/CommonLib/debugutil.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -357,21 +357,18 @@ DebugPrintW(
357357

358358
OutputDebugString( strOutput.QueryStr() );
359359

360-
if (IsEnabled(ASPNETCORE_DEBUG_FLAG_CONSOLE) || g_logFile != INVALID_HANDLE_VALUE)
360+
if (IsEnabled(ASPNETCORE_DEBUG_FLAG_CONSOLE))
361361
{
362-
if (IsEnabled(ASPNETCORE_DEBUG_FLAG_CONSOLE))
363-
{
364-
WriteFileEncoded(GetConsoleOutputCP(), g_stdOutHandle, strOutput.QueryStr());
365-
}
362+
WriteFileEncoded(GetConsoleOutputCP(), g_stdOutHandle, strOutput.QueryStr());
363+
}
366364

367-
if (g_logFile != INVALID_HANDLE_VALUE)
368-
{
369-
SRWExclusiveLock lock(g_logFileLock);
365+
if (g_logFile != INVALID_HANDLE_VALUE)
366+
{
367+
SRWExclusiveLock lock(g_logFileLock);
370368

371-
SetFilePointer(g_logFile, 0, nullptr, FILE_END);
372-
WriteFileEncoded(CP_UTF8, g_logFile, strOutput.QueryStr());
373-
FlushFileBuffers(g_logFile);
374-
}
369+
SetFilePointer(g_logFile, 0, nullptr, FILE_END);
370+
WriteFileEncoded(CP_UTF8, g_logFile, strOutput.QueryStr());
371+
FlushFileBuffers(g_logFile);
375372
}
376373

377374
if (IsEnabled(ASPNETCORE_DEBUG_FLAG_EVENTLOG))

src/Servers/IIS/AspNetCoreModuleV2/CommonLib/file_utility.cpp

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,23 @@
1111
HRESULT
1212
FILE_UTILITY::IsPathUnc(
1313
__in LPCWSTR pszPath,
14-
__out BOOL * pfIsUnc
14+
__out bool* pfIsUnc
1515
)
1616
{
17-
HRESULT hr = S_OK;
18-
STRU strTempPath;
19-
20-
if ( pszPath == NULL || pfIsUnc == NULL )
17+
if (pszPath == nullptr || pfIsUnc == nullptr)
2118
{
22-
hr = E_INVALIDARG;
23-
goto Finished;
19+
return E_INVALIDARG;
2420
}
2521

26-
hr = MakePathCanonicalizationProof( (LPWSTR) pszPath, &strTempPath );
27-
if ( FAILED(hr) )
22+
STRU strTempPath;
23+
HRESULT hr = MakePathCanonicalizationProof(pszPath, &strTempPath );
24+
if (FAILED(hr))
2825
{
29-
goto Finished;
26+
return hr;
3027
}
3128

32-
//
33-
// MakePathCanonicalizationProof will map \\?\UNC, \\.\UNC and \\ to \\?\UNC
34-
//
35-
(*pfIsUnc) = ( _wcsnicmp( strTempPath.QueryStr(), L"\\\\?\\UNC\\", 8 /* sizeof \\?\UNC\ */) == 0 );
36-
37-
Finished:
29+
// MakePathCanonicalizationProof will map \\?\UNC, \\.\UNC and \\ to \\?\UNC (which is 8 characters)
30+
*pfIsUnc = (_wcsnicmp(strTempPath.QueryStr(), L"\\\\?\\UNC\\", 8) == 0);
3831

3932
return hr;
4033
}
@@ -103,69 +96,57 @@ FILE_UTILITY::ConvertPathToFullPath(
10396
return hr;
10497
}
10598

99+
// Ensures that the specified directory path exists, creating it if necessary. If any part of the directory
100+
// creation fails and the directory does not already exist, it returns an error obtained from GetLastError.
101+
// If all directories are successfully created or already exist, it returns S_OK.
106102
HRESULT
107-
FILE_UTILITY::EnsureDirectoryPathExist(
108-
_In_ LPCWSTR pszPath
103+
FILE_UTILITY::EnsureDirectoryPathExists(
104+
_In_ const LPCWSTR pszPath
109105
)
110106
{
111-
HRESULT hr = S_OK;
112-
STRU struPath;
113-
DWORD dwPosition = 0;
114-
BOOL fDone = FALSE;
115-
BOOL fUnc = FALSE;
116-
107+
STRU struPath;
117108
struPath.Copy(pszPath);
118-
hr = IsPathUnc(pszPath, &fUnc);
109+
110+
bool isUnc = false;
111+
HRESULT hr = IsPathUnc(pszPath, &isUnc);
119112
if (FAILED(hr))
120113
{
121-
goto Finished;
122-
}
123-
if (fUnc)
124-
{
125-
// "\\?\UNC\"
126-
dwPosition = 8;
114+
return hr;
127115
}
128-
else if (struPath.IndexOf(L'?', 0) != -1)
129-
{
130-
// sceanrio "\\?\"
131-
dwPosition = 4;
132-
}
133-
while (!fDone)
116+
117+
// Initialize position based on the type of the path.
118+
DWORD position = isUnc ? 8 // Skip "\\?\UNC\"
119+
: (struPath.IndexOf(L'?', 0) != -1) ? 4 // Skip "\\?\"
120+
: 0; // Skip nothing
121+
122+
// Traverse the path string, creating directories as we go.
123+
while (true)
134124
{
135-
dwPosition = struPath.IndexOf(L'\\', dwPosition + 1);
136-
if (dwPosition == -1)
137-
{
138-
// not found '/'
139-
fDone = TRUE;
140-
goto Finished;
141-
}
142-
else if (dwPosition ==0)
125+
position = struPath.IndexOf(L'\\', position + 1);
126+
if (position == -1)
143127
{
144-
hr = ERROR_INTERNAL_ERROR;
145-
goto Finished;
128+
// Didn't find a path separator, so we're done.
129+
return S_OK;
146130
}
147-
else if (struPath.QueryStr()[dwPosition-1] == L':')
131+
132+
// position is >= 1 here since we started searching at position + 1
133+
if (struPath.QueryStr()[position - 1] == L':')
148134
{
149-
// skip volume case
135+
// Skip the volume case
150136
continue;
151137
}
152-
else
153-
{
154-
struPath.QueryStr()[dwPosition] = L'\0';
155-
}
156138

157-
if (!CreateDirectory(struPath.QueryStr(), NULL) &&
158-
ERROR_ALREADY_EXISTS != GetLastError())
139+
// Temporarily terminate the string at the current path separator
140+
struPath.QueryStr()[position] = L'\0';
141+
if (!CreateDirectory(struPath.QueryStr(), nullptr) && GetLastError() != ERROR_ALREADY_EXISTS)
159142
{
160-
hr = HRESULT_FROM_WIN32(GetLastError());
161-
fDone = TRUE;
162-
goto Finished;
143+
// Unable to create directory and it doesn't already exist
144+
return HRESULT_FROM_WIN32(GetLastError());
163145
}
164-
struPath.QueryStr()[dwPosition] = L'\\';
165-
}
166146

167-
Finished:
168-
return hr;
147+
// Restore the path separator
148+
struPath.QueryStr()[position] = L'\\';
149+
}
169150
}
170151

171152
std::string FILE_UTILITY::GetHtml(HMODULE module, int page, USHORT statusCode, USHORT subStatusCode, const std::string& specificReasonPhrase, const std::string& solution)

src/Servers/IIS/AspNetCoreModuleV2/CommonLib/file_utility.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class FILE_UTILITY
2222

2323
static
2424
HRESULT
25-
EnsureDirectoryPathExist(
25+
EnsureDirectoryPathExists(
2626
_In_ LPCWSTR pszPath
2727
);
2828

@@ -40,7 +40,7 @@ class FILE_UTILITY
4040
HRESULT
4141
IsPathUnc(
4242
__in LPCWSTR pszPath,
43-
__out BOOL * pfIsUnc
43+
__out bool * pfIsUnc
4444
);
4545
};
4646

src/Servers/IIS/AspNetCoreModuleV2/OutOfProcessRequestHandler/serverprocess.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ SERVER_PROCESS::SetupStdHandles(
10861086
goto Finished;
10871087
}
10881088

1089-
hr = FILE_UTILITY::EnsureDirectoryPathExist(struPath.QueryStr());
1089+
hr = FILE_UTILITY::EnsureDirectoryPathExists(struPath.QueryStr());
10901090
if (FAILED_LOG(hr))
10911091
{
10921092
goto Finished;

0 commit comments

Comments
 (0)