Skip to content

[5.4] Judge executables with API on Windows #2984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Sources/Foundation/FileManager+Win32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,10 @@ extension FileManager {
}

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

internal func _isDeletableFile(atPath path: String) -> Bool {
Expand Down
17 changes: 12 additions & 5 deletions Tests/Foundation/Tests/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ class TestFileManager : XCTestCase {
func test_isExecutableFile() {
let fm = FileManager.default
let path = NSTemporaryDirectory() + "test_isExecutableFile\(NSUUID().uuidString)"
let exePath = path + ".exe"
defer {
try? fm.removeItem(atPath: path)
try? fm.removeItem(atPath: exePath)
}

do {
Expand All @@ -275,16 +277,21 @@ class TestFileManager : XCTestCase {

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

#if os(Windows)
// test unExecutable even if file has an `exe` extension
try fm.copyItem(atPath: path, toPath: exePath)
XCTAssertFalse(fm.isExecutableFile(atPath: exePath))
#else
// test executable if file has execute permissions
try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0100))], ofItemAtPath: path)
XCTAssertTrue(fm.isExecutableFile(atPath: path))
#endif

// test against the test bundle itself
let testFoundationBinary = try XCTUnwrap(testBundle().path(forAuxiliaryExecutable: "TestFoundation"))
XCTAssertTrue(fm.isExecutableFile(atPath: testFoundationBinary))
} catch let e {
XCTFail("\(e)")
}
Expand Down