diff --git a/Package.swift b/Package.swift index 229352ae..b6623ef2 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:4.1 +// swift-tools-version:5.1 // // Package.swift // PerfectLib @@ -35,6 +35,9 @@ let package = Package( #else let package = Package( name: "PerfectLib", + platforms: [ + .macOS(.v10_15) + ], products: [ .library(name: "PerfectLib", targets: ["PerfectLib"]) ], diff --git a/Sources/PerfectLib/Dir.swift b/Sources/PerfectLib/Dir.swift index 82371866..3245476e 100644 --- a/Sources/PerfectLib/Dir.swift +++ b/Sources/PerfectLib/Dir.swift @@ -52,9 +52,11 @@ public struct Dir { /// Return the process' current working directory. public static var workingDir: Dir { - let buffer = Array(repeating: 0 as UInt8, count: 2049) - let _ = getcwd(UnsafeMutableRawPointer(mutating: buffer).assumingMemoryBound(to: Int8.self), 2048) - let path = String(validatingUTF8: UnsafeMutableRawPointer(mutating: buffer).assumingMemoryBound(to: Int8.self)) ?? "." + var buffer = Array(repeating: 0 as UInt8, count: 2049) + buffer.withUnsafeMutableBytes { + _ = getcwd($0.bindMemory(to: Int8.self).baseAddress, 2048) + } + let path = String(cString: buffer) return Dir(path) } @@ -83,7 +85,7 @@ public struct Dir { } } - /// Deletes the directory. The directory must be empty in order to be successfuly deleted. + /// Deletes the directory. The directory must be empty in order to be successfully deleted. /// - throws: `PerfectError.FileError` public func delete() throws { let res = rmdir(realPath) diff --git a/Sources/PerfectLib/File.swift b/Sources/PerfectLib/File.swift index cf394196..7a55d1fb 100644 --- a/Sources/PerfectLib/File.swift +++ b/Sources/PerfectLib/File.swift @@ -81,13 +81,13 @@ public class File { return internalPath } var ary = [UInt8](repeating: 0, count: maxPath) - let buffer = UnsafeMutableRawPointer(mutating: ary).assumingMemoryBound(to: Int8.self) - let res = readlink(internalPath, buffer, maxPath) + let res = ary.withUnsafeMutableBytes { + readlink(internalPath, $0.bindMemory(to: Int8.self).baseAddress, maxPath) + } guard res != -1 else { return internalPath } - ary.removeLast(maxPath - res) - let trailPath = UTF8Encoding.encode(bytes: ary) + let trailPath = String(cString: ary) let lastChar = trailPath[trailPath.startIndex] guard lastChar != "/" && lastChar != "." else { return trailPath @@ -437,9 +437,10 @@ public extension File { let bSize = min(count, sizeOr(count)) var ary = [UInt8](repeating: 0, count: bSize) - let ptr = UnsafeMutableRawPointer(mutating: ary).assumingMemoryBound(to: Int8.self) - - let readCount = read(CInt(fd), ptr, bSize) + + let readCount = ary.withUnsafeMutableBytes { + read(CInt(fd), $0.bindMemory(to: Int8.self).baseAddress, bSize) + } guard readCount >= 0 else { try ThrowFileError() } @@ -475,11 +476,14 @@ public extension File { @discardableResult func write(bytes: [UInt8], dataPosition: Int = 0, length: Int = Int.max) throws -> Int { let len = min(bytes.count - dataPosition, length) - let ptr = UnsafeMutableRawPointer(mutating: bytes).assumingMemoryBound(to: UInt8.self).advanced(by: dataPosition) #if os(Linux) - let wrote = SwiftGlibc.write(Int32(fd), ptr, len) + let wrote = bytes.withUnsafeBytes { + SwiftGlibc.write(Int32(fd), $0.bindMemory(to: Int8.self).baseAddress?.advanced(by: dataPosition), len) + } #else - let wrote = Darwin.write(Int32(fd), ptr, len) + let wrote = bytes.withUnsafeBytes { + Darwin.write(Int32(fd), $0.bindMemory(to: Int8.self).baseAddress?.advanced(by: dataPosition), len) + } #endif guard wrote == len else { try ThrowFileError() diff --git a/Sources/PerfectLib/SysProcess.swift b/Sources/PerfectLib/SysProcess.swift index 8028f075..db55b852 100644 --- a/Sources/PerfectLib/SysProcess.swift +++ b/Sources/PerfectLib/SysProcess.swift @@ -82,9 +82,15 @@ public class SysProcess { var fSTDOUT: [Int32] = [0, 0] var fSTDERR: [Int32] = [0, 0] - pipe(UnsafeMutableRawPointer(mutating: fSTDIN).assumingMemoryBound(to: Int32.self)) - pipe(UnsafeMutableRawPointer(mutating: fSTDOUT).assumingMemoryBound(to: Int32.self)) - pipe(UnsafeMutableRawPointer(mutating: fSTDERR).assumingMemoryBound(to: Int32.self)) + fSTDIN.withUnsafeMutableBytes { + _ = pipe($0.bindMemory(to: Int32.self).baseAddress) + } + fSTDOUT.withUnsafeMutableBytes { + _ = pipe($0.bindMemory(to: Int32.self).baseAddress) + } + fSTDERR.withUnsafeMutableBytes { + _ = pipe($0.bindMemory(to: Int32.self).baseAddress) + } #if os(Linux) var action = posix_spawn_file_actions_t() var attr = posix_spawnattr_t() diff --git a/Sources/PerfectLib/Utilities.swift b/Sources/PerfectLib/Utilities.swift index 4bf06492..d29a7eec 100644 --- a/Sources/PerfectLib/Utilities.swift +++ b/Sources/PerfectLib/Utilities.swift @@ -247,8 +247,11 @@ extension String { } } bytesArray.append(0) - return UnsafePointer(bytesArray).withMemoryRebound(to: Int8.self, capacity: bytesArray.count) { - return String(validatingUTF8: $0) + return bytesArray.withUnsafeBytes { + guard let p = $0.bindMemory(to: Int8.self).baseAddress else { + return nil + } + return String(validatingUTF8: p) } } diff --git a/Tests/PerfectLibTests/PerfectLibTests.swift b/Tests/PerfectLibTests/PerfectLibTests.swift index 03b14edd..449ba853 100644 --- a/Tests/PerfectLibTests/PerfectLibTests.swift +++ b/Tests/PerfectLibTests/PerfectLibTests.swift @@ -102,12 +102,9 @@ class PerfectLibTests: XCTestCase { do { let encoded = try user.jsonEncodedString() - print(encoded) - guard let user2 = try encoded.jsonDecode() as? User else { return XCTAssert(false, "Invalid object \(encoded)") - } - + } XCTAssert(user.firstName == user2.firstName) XCTAssert(user.lastName == user2.lastName) XCTAssert(user.age == user2.age) @@ -584,6 +581,18 @@ class PerfectLibTests: XCTestCase { } } + func testDirWorkDir() { + let workDir = File("/tmp").realPath.replacingOccurrences(of: "//", with: "/") + "/" + let dir = Dir(workDir) + do { + try dir.setAsWorkingDir() + let fetch = Dir.workingDir.path + XCTAssertEqual(workDir, fetch) + } catch { + XCTAssert(false, "Error testing file perms: \(error)") + } + } + func testBytesIO() { let i8 = 254 as UInt8 let i16 = 54045 as UInt16