Skip to content

Commit a71d1fa

Browse files
committed
Use SwiftSystem.Errno instead of calling strerror directly
This resolves some builds warnings on Windows, which helps log readability. (Yes I know we could alternatively just disable those warnings, but technically strerror does have thread safety issues and if we're going to fix that it would be nice to just do it in SwiftSystem in one place only)
1 parent 5bec990 commit a71d1fa

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

Sources/SWBUtil/POSIX.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ import SWBLibc
1414

1515
public import protocol Foundation.LocalizedError
1616

17-
#if os(Windows)
1817
#if canImport(System)
19-
import System
18+
public import System
2019
#else
21-
import SystemPackage
22-
#endif
20+
public import SystemPackage
2321
#endif
2422

2523
public enum POSIX: Sendable {
@@ -78,12 +76,16 @@ public enum POSIX: Sendable {
7876
}
7977

8078
public struct POSIXError: Error, LocalizedError, CustomStringConvertible, Equatable {
81-
public let code: Int32
79+
public let underlyingError: Errno
8280
public let context: String?
8381
public let arguments: [String]
8482

83+
public var code: Int32 {
84+
underlyingError.rawValue
85+
}
86+
8587
public init(_ code: Int32, context: String? = nil, _ arguments: [String]) {
86-
self.code = code
88+
self.underlyingError = Errno(rawValue: code)
8789
self.context = context
8890
self.arguments = arguments
8991
}
@@ -93,7 +95,7 @@ public struct POSIXError: Error, LocalizedError, CustomStringConvertible, Equata
9395
}
9496

9597
public var description: String {
96-
let end = "\(String(cString: strerror(code))) (\(code))"
98+
let end = "\(underlyingError.description) (\(code))"
9799
if let context {
98100
return "\(context)(\(arguments.joined(separator: ", "))): \(end)"
99101
}

Sources/SWBUtil/PbxCp.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ fileprivate func copyTree(_ srcPath: Path, _ dstPath: Path, options: CopyOptions
432432
do {
433433
_srcPath = try localFS.realpath(srcPath)
434434
} catch let error as POSIXError {
435-
outStream <<< "error: \(srcPath.str): \(String(cString: strerror(error.code)))\n"
435+
// TODO: Does this really need to print specially for POSIXError?
436+
outStream <<< "error: \(srcPath.str): \(error.underlyingError.description)\n"
436437
return false
437438
} catch {
438439
outStream <<< "error: \(srcPath.str): \(error.localizedDescription)\n"
@@ -441,7 +442,8 @@ fileprivate func copyTree(_ srcPath: Path, _ dstPath: Path, options: CopyOptions
441442
do {
442443
_dstPath = try localFS.realpath(dstPath)
443444
} catch let error as POSIXError {
444-
outStream <<< "error: \(dstPath.str): \(String(cString: strerror(error.code)))\n"
445+
// TODO: Does this really need to print specially for POSIXError?
446+
outStream <<< "error: \(dstPath.str): \(error.underlyingError.description)\n"
445447
return false
446448
} catch {
447449
outStream <<< "error: \(srcPath.str): \(error.localizedDescription)\n"

0 commit comments

Comments
 (0)