Skip to content

Commit b8c9b25

Browse files
authored
Merge pull request #2984 from stevapple/windows-executable-5.4
[5.4] Judge executables with API on Windows
2 parents 97667c0 + fd60b97 commit b8c9b25

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Sources/Foundation/FileManager+Win32.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,10 @@ extension FileManager {
709709
}
710710

711711
internal func _isExecutableFile(atPath path: String) -> Bool {
712-
var isDirectory: ObjCBool = false
713-
guard fileExists(atPath: path, isDirectory: &isDirectory) else { return false }
714-
return !isDirectory.boolValue && _isReadableFile(atPath: path)
712+
var binaryType = DWORD(0)
713+
return path.withCString(encodedAs: UTF16.self) {
714+
GetBinaryTypeW($0, &binaryType)
715+
}
715716
}
716717

717718
internal func _isDeletableFile(atPath path: String) -> Bool {

Tests/Foundation/Tests/TestFileManager.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ class TestFileManager : XCTestCase {
265265
func test_isExecutableFile() {
266266
let fm = FileManager.default
267267
let path = NSTemporaryDirectory() + "test_isExecutableFile\(NSUUID().uuidString)"
268+
let exePath = path + ".exe"
268269
defer {
269270
try? fm.removeItem(atPath: path)
271+
try? fm.removeItem(atPath: exePath)
270272
}
271273

272274
do {
@@ -275,16 +277,21 @@ class TestFileManager : XCTestCase {
275277

276278
// test unExecutable if file has no permissions
277279
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0000))], ofItemAtPath: path)
278-
#if os(Windows)
279-
// Files are always executable on Windows
280-
XCTAssertTrue(fm.isExecutableFile(atPath: path))
281-
#else
282280
XCTAssertFalse(fm.isExecutableFile(atPath: path))
283-
#endif
284281

282+
#if os(Windows)
283+
// test unExecutable even if file has an `exe` extension
284+
try fm.copyItem(atPath: path, toPath: exePath)
285+
XCTAssertFalse(fm.isExecutableFile(atPath: exePath))
286+
#else
285287
// test executable if file has execute permissions
286288
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0100))], ofItemAtPath: path)
287289
XCTAssertTrue(fm.isExecutableFile(atPath: path))
290+
#endif
291+
292+
// test against the test bundle itself
293+
let testFoundationBinary = try XCTUnwrap(testBundle().path(forAuxiliaryExecutable: "TestFoundation"))
294+
XCTAssertTrue(fm.isExecutableFile(atPath: testFoundationBinary))
288295
} catch let e {
289296
XCTFail("\(e)")
290297
}

0 commit comments

Comments
 (0)