44 */
55package aws.smithy.kotlin.runtime.client.util
66
7+ import aws.sdk.kotlin.runtime.util.SdkDispatchers // adjust import
78import kotlinx.cinterop.*
89import kotlinx.coroutines.withContext
9- import aws.sdk.kotlin.runtime.util.SdkDispatchers // adjust import
10- import platform.windows.*
1110import platform.posix._wunlink // to delete the temp file afterwards
11+ import platform.windows.*
1212
1313@OptIn(ExperimentalForeignApi ::class )
1414private fun String.wideCString (mem : MemScope ) = wcstr.getPointer(mem)
@@ -20,7 +20,8 @@ internal actual suspend fun executeCommand(
2020 maxOutputLengthBytes : Long ,
2121 timeoutMillis : Long ,
2222 clock : Clock ,
23- ): Pair <Int , String > = withContext(SdkDispatchers .IO ) { memScoped {
23+ ): Pair <Int , String > = withContext(SdkDispatchers .IO ) {
24+ memScoped {
2425 // 1) Make a temp file to capture stdout+stderr
2526 val tmpDirBuf = allocArray<UShortVar >(MAX_PATH )
2627 val tmpNameBuf = allocArray<UShortVar >(MAX_PATH )
@@ -44,7 +45,7 @@ internal actual suspend fun executeCommand(
4445 sa.ptr,
4546 CREATE_ALWAYS ,
4647 FILE_ATTRIBUTE_NORMAL .toUInt(),
47- null
48+ null ,
4849 )
4950 if (hOut == INVALID_HANDLE_VALUE ) error(" CreateFileW failed for temp output" )
5051
@@ -53,7 +54,11 @@ internal actual suspend fun executeCommand(
5354 // Prefer %ComSpec% if present, else fallback.
5455 val comspecBuf = allocArray<WCHARVar >(MAX_PATH )
5556 val comspecLen = GetEnvironmentVariableW (" ComSpec" .wideCString(this ), comspecBuf, MAX_PATH )
56- val cmdExe = if (comspecLen > 0u ) { comspecBuf } else { " C:\\ Windows\\ System32\\ cmd.exe" .wideCString(this ) }
57+ val cmdExe = if (comspecLen > 0u ) {
58+ comspecBuf
59+ } else {
60+ " C:\\ Windows\\ System32\\ cmd.exe" .wideCString(this )
61+ }
5762
5863 val cmdLine = (" /C " + command).wideCString(this )
5964
@@ -69,15 +74,15 @@ internal actual suspend fun executeCommand(
6974
7075 val created = CreateProcessW (
7176 cmdExe,
72- cmdLine, // mutable buffer OK; wcstr gives writable copy here
77+ cmdLine, // mutable buffer OK; wcstr gives writable copy here
7378 null ,
7479 null ,
75- TRUE , // inherit handles (so child gets hOut)
80+ TRUE , // inherit handles (so child gets hOut)
7681 CREATE_NO_WINDOW .toUInt(),
7782 null ,
7883 null ,
7984 si.ptr,
80- pi.ptr
85+ pi.ptr,
8186 )
8287 if (created == 0 ) error(" CreateProcessW failed: ${GetLastError ()} " )
8388
@@ -94,7 +99,6 @@ internal actual suspend fun executeCommand(
9499 error(" Process timed out after ${timeoutMillis} ms" )
95100 }
96101
97-
98102 // 5) Get exit code
99103 val exitCodeVar = alloc<DWORDVar >()
100104 GetExitCodeProcess (pi.hProcess, exitCodeVar.ptr)
@@ -109,7 +113,7 @@ internal actual suspend fun executeCommand(
109113 null ,
110114 OPEN_EXISTING ,
111115 FILE_ATTRIBUTE_NORMAL .toUInt(),
112- null
116+ null ,
113117 )
114118 if (hIn == INVALID_HANDLE_VALUE ) {
115119 // Clean up and bail
@@ -151,4 +155,5 @@ internal actual suspend fun executeCommand(
151155 } finally {
152156 CloseHandle (hOut)
153157 }
154- } }
158+ }
159+ }
0 commit comments