Skip to content

Commit f26a1a6

Browse files
committed
minor cleanup executeCommandMingw
1 parent 3e87556 commit f26a1a6

File tree

1 file changed

+31
-59
lines changed

1 file changed

+31
-59
lines changed

aws-runtime/aws-config/mingw/src/aws/sdk/kotlin/runtime/auth/credentials/executeCommandMingw.kt

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@ internal actual suspend fun executeCommand(
2727
memScoped {
2828
// 1) temp path + file
2929
val tmpDirBuf = allocArray<UShortVar>(MAX_PATH)
30-
val gotTmp = GetTempPathW(MAX_PATH.toUInt(), tmpDirBuf)
31-
if (gotTmp == 0u) error("GetTempPathW failed")
30+
check(GetTempPathW(MAX_PATH.toUInt(), tmpDirBuf) == 0u) { "GetTempPathW failed" }
3231

3332
val tmpNameBuf = allocArray<UShortVar>(MAX_PATH)
3433
val tmpDirStr = tmpDirBuf.toKString()
35-
val gotName = GetTempFileNameW(tmpDirStr, "KNR", 0u, tmpNameBuf)
36-
if (gotName == 0u) error("GetTempFileNameW failed")
34+
check(GetTempFileNameW(tmpDirStr, "KNR", 0u, tmpNameBuf) == 0u) { "GetTempFileNameW failed" }
3735
val outPath: String = tmpNameBuf.toKString()
3836

3937
// 2) create output (inherit)
@@ -44,24 +42,17 @@ internal actual suspend fun executeCommand(
4442
}
4543

4644
var hOut: HANDLE? = CreateFileW(
47-
/* lpFileName = */
48-
outPath,
49-
/* dwDesiredAccess = */
50-
GENERIC_WRITE.toUInt(),
51-
/* dwShareMode = */
52-
(FILE_SHARE_READ or FILE_SHARE_WRITE).toUInt(),
53-
/* lpSecurityAttributes = */
54-
sa.ptr,
55-
/* dwCreationDisposition = */
56-
CREATE_ALWAYS.toUInt(),
57-
/* dwFlagsAndAttributes = */
58-
FILE_ATTRIBUTE_NORMAL.toUInt(),
59-
/* hTemplateFile = */
60-
null,
45+
outPath, // lpFileName
46+
GENERIC_WRITE.toUInt(), // dwDesiredAccess
47+
(FILE_SHARE_READ or FILE_SHARE_WRITE).toUInt(), // dwShareMode
48+
sa.ptr, // lpSecurityAttributes
49+
CREATE_ALWAYS.toUInt(), // dwCreationDisposition
50+
FILE_ATTRIBUTE_NORMAL.toUInt(), // dwFlagsAndAttributes
51+
null, // hTemplateFile
6152
)
62-
if (hOut == INVALID_HANDLE_VALUE) error("CreateFileW failed for temp output (GetLastError=${GetLastError()})")
53+
check(hOut != INVALID_HANDLE_VALUE) { "CreateFileW failed for temp output (GetLastError=${GetLastError()})" }
6354

64-
// Ensure the handle is marked inheritable (some setups ignore SA if handle flags were flipped later)
55+
// Ensure the handle is marked inheritable
6556
if (hOut != null && hOut != INVALID_HANDLE_VALUE) {
6657
SetHandleInformation(hOut, HANDLE_FLAG_INHERIT.toUInt(), HANDLE_FLAG_INHERIT.toUInt())
6758
}
@@ -86,29 +77,18 @@ internal actual suspend fun executeCommand(
8677
}
8778
val pi = alloc<PROCESS_INFORMATION>()
8879

89-
val created = CreateProcessW(
90-
/* lpApplicationName = */
91-
cmdExe,
92-
/* lpCommandLine = */
93-
cmdLineBuf,
94-
/* lpProcessAttributes = */
95-
null,
96-
/* lpThreadAttributes = */
97-
null,
98-
/* bInheritHandles = */
99-
TRUE,
100-
/* dwCreationFlags = */
101-
CREATE_NO_WINDOW.toUInt(),
102-
/* lpEnvironment = */
103-
null,
104-
/* lpCurrentDirectory = */
105-
null,
106-
/* lpStartupInfo = */
107-
si.ptr,
108-
/* lpProcessInformation = */
109-
pi.ptr,
110-
)
111-
if (created == 0) error("CreateProcessW failed (GetLastError=${GetLastError()})")
80+
check(CreateProcessW(
81+
cmdExe, // lpApplicationName
82+
cmdLineBuf, // lpCommandLine
83+
null, // lpProcessAttributes
84+
null, // lpThreadAttributes
85+
TRUE, // bInheritHandles
86+
CREATE_NO_WINDOW.toUInt(), // dwCreationFlags
87+
null, // lpEnvironment
88+
null, // lpCurrentDirectory
89+
si.ptr, // lpStartupInfo
90+
pi.ptr, // lpProcessInformation
91+
) == 0) { "CreateProcessW failed (GetLastError=${GetLastError()})" }
11292

11393
try {
11494
// 5) wait + timeout
@@ -132,29 +112,21 @@ internal actual suspend fun executeCommand(
132112
}
133113
val exitCode = exitCodeVar.value.toInt()
134114

135-
// 🔑 Close writer before reading back
136115
if (hOut != null && hOut != INVALID_HANDLE_VALUE) {
137116
FlushFileBuffers(hOut)
138117
CloseHandle(hOut)
139118
hOut = null
140119
}
141120

142-
// 7) read back bounded
121+
// 7) read output, bounded
143122
val hIn: HANDLE? = CreateFileW(
144-
/* lpFileName = */
145-
outPath,
146-
/* dwDesiredAccess = */
147-
GENERIC_READ.toUInt(),
148-
/* dwShareMode = */
149-
(FILE_SHARE_READ or FILE_SHARE_WRITE).toUInt(),
150-
/* lpSecurityAttributes = */
151-
null,
152-
/* dwCreationDisposition = */
153-
OPEN_EXISTING.toUInt(),
154-
/* dwFlagsAndAttributes = */
155-
FILE_ATTRIBUTE_NORMAL.toUInt(),
156-
/* hTemplateFile = */
157-
null,
123+
outPath, // lpFileName
124+
GENERIC_READ.toUInt(), // dwDesiredAccess
125+
(FILE_SHARE_READ or FILE_SHARE_WRITE).toUInt(), // dwShareMode
126+
null, // lpSecurityAttributes
127+
OPEN_EXISTING.toUInt(), // dwCreationDisposition
128+
FILE_ATTRIBUTE_NORMAL.toUInt(), // dwFlagsAndAttributes
129+
null, // hTemplateFile
158130
)
159131
if (hIn == INVALID_HANDLE_VALUE) {
160132
_wunlink(outPath.wideCString(this))

0 commit comments

Comments
 (0)