From 6735660f1c119f06b2dad6244ac1fd100bd97e98 Mon Sep 17 00:00:00 2001 From: stevapple Date: Wed, 23 Dec 2020 00:53:27 +0800 Subject: [PATCH 1/5] Judge executables with API on Windows --- Sources/Foundation/FileManager+Win32.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/Foundation/FileManager+Win32.swift b/Sources/Foundation/FileManager+Win32.swift index 9069aa1925..c3b9bce396 100644 --- a/Sources/Foundation/FileManager+Win32.swift +++ b/Sources/Foundation/FileManager+Win32.swift @@ -711,7 +711,11 @@ 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) + guard !isDirectory.boolValue, _isReadableFile(atPath: path) else { return false } + return path.withCString(encodedAs: UTF16.self) { + var binaryType = DWORD(0) + return GetBinaryTypeW($0, &binaryType) + } } internal func _isDeletableFile(atPath path: String) -> Bool { From de1daf44692f66c23193a776c7b4d533ffdfe305 Mon Sep 17 00:00:00 2001 From: stevapple Date: Wed, 23 Dec 2020 02:53:53 +0800 Subject: [PATCH 2/5] Remove useless codes --- Sources/Foundation/FileManager+Win32.swift | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Sources/Foundation/FileManager+Win32.swift b/Sources/Foundation/FileManager+Win32.swift index c3b9bce396..4f46a2f847 100644 --- a/Sources/Foundation/FileManager+Win32.swift +++ b/Sources/Foundation/FileManager+Win32.swift @@ -709,12 +709,9 @@ extension FileManager { } internal func _isExecutableFile(atPath path: String) -> Bool { - var isDirectory: ObjCBool = false - guard fileExists(atPath: path, isDirectory: &isDirectory) else { return false } - guard !isDirectory.boolValue, _isReadableFile(atPath: path) else { return false } + var binaryType = DWORD(0) return path.withCString(encodedAs: UTF16.self) { - var binaryType = DWORD(0) - return GetBinaryTypeW($0, &binaryType) + GetBinaryTypeW($0, &binaryType) } } From cde744f019e350dbc956c14c084e6e9bdcd4fb3f Mon Sep 17 00:00:00 2001 From: stevapple Date: Wed, 23 Dec 2020 08:54:31 +0800 Subject: [PATCH 3/5] Fix test --- Tests/Foundation/Tests/TestFileManager.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/Foundation/Tests/TestFileManager.swift b/Tests/Foundation/Tests/TestFileManager.swift index b1b0208e61..c8087ffd29 100644 --- a/Tests/Foundation/Tests/TestFileManager.swift +++ b/Tests/Foundation/Tests/TestFileManager.swift @@ -275,16 +275,16 @@ 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 // test executable if file has execute permissions try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0100))], ofItemAtPath: path) +#if os(Windows) + // a Windows executable needs to be binary + XCTAssertFalse(fm.isExecutableFile(atPath: path)) +#else XCTAssertTrue(fm.isExecutableFile(atPath: path)) +#endif } catch let e { XCTFail("\(e)") } From 952998609687493c46c2184fcb200d99994113e5 Mon Sep 17 00:00:00 2001 From: stevapple Date: Wed, 23 Dec 2020 10:24:34 +0800 Subject: [PATCH 4/5] Test `isExecutableFile` against cmd.exe --- Tests/Foundation/Tests/TestFileManager.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Tests/Foundation/Tests/TestFileManager.swift b/Tests/Foundation/Tests/TestFileManager.swift index c8087ffd29..6c055f8de1 100644 --- a/Tests/Foundation/Tests/TestFileManager.swift +++ b/Tests/Foundation/Tests/TestFileManager.swift @@ -277,12 +277,17 @@ class TestFileManager : XCTestCase { try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0000))], ofItemAtPath: path) XCTAssertFalse(fm.isExecutableFile(atPath: path)) - // test executable if file has execute permissions - try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0100))], ofItemAtPath: path) #if os(Windows) - // a Windows executable needs to be binary - XCTAssertFalse(fm.isExecutableFile(atPath: path)) + // test against cmd.exe + let systemDir: String = { + var buffer = Array(repeating: 0, count: Int(MAX_PATH + 1)) + GetSystemDirectoryW(&buffer, .init(MAX_PATH + 1)) + return String(decodingCString: buffer, as: UTF16.self) + }() + XCTAssertTrue(fm.isExecutableFile(atPath: "\(systemDir)\\cmd.exe")) #else + // test executable if file has execute permissions + try fm.setAttributes([.posixPermissions : NSNumber(value: Int16(0o0100))], ofItemAtPath: path) XCTAssertTrue(fm.isExecutableFile(atPath: path)) #endif } catch let e { From fbf7f344d814517c4585f3b99503f03d627da756 Mon Sep 17 00:00:00 2001 From: YR Chen Date: Wed, 23 Dec 2020 16:58:51 +0800 Subject: [PATCH 5/5] Try to test against the bundle itself --- Tests/Foundation/Tests/TestFileManager.swift | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/Tests/Foundation/Tests/TestFileManager.swift b/Tests/Foundation/Tests/TestFileManager.swift index 6c055f8de1..c8d50061c9 100644 --- a/Tests/Foundation/Tests/TestFileManager.swift +++ b/Tests/Foundation/Tests/TestFileManager.swift @@ -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 { @@ -278,18 +280,18 @@ class TestFileManager : XCTestCase { XCTAssertFalse(fm.isExecutableFile(atPath: path)) #if os(Windows) - // test against cmd.exe - let systemDir: String = { - var buffer = Array(repeating: 0, count: Int(MAX_PATH + 1)) - GetSystemDirectoryW(&buffer, .init(MAX_PATH + 1)) - return String(decodingCString: buffer, as: UTF16.self) - }() - XCTAssertTrue(fm.isExecutableFile(atPath: "\(systemDir)\\cmd.exe")) + // 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)") }