Skip to content

Commit

Permalink
Fixed Swift 5 warnings. Changed Swift tools version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Jessup committed May 19, 2020
1 parent 54a6695 commit 135fb33
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.1
// swift-tools-version:5.1
//
// Package.swift
// PerfectLib
Expand Down Expand Up @@ -35,6 +35,9 @@ let package = Package(
#else
let package = Package(
name: "PerfectLib",
platforms: [
.macOS(.v10_15)
],
products: [
.library(name: "PerfectLib", targets: ["PerfectLib"])
],
Expand Down
10 changes: 6 additions & 4 deletions Sources/PerfectLib/Dir.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 14 additions & 10 deletions Sources/PerfectLib/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 9 additions & 3 deletions Sources/PerfectLib/SysProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions Sources/PerfectLib/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
17 changes: 13 additions & 4 deletions Tests/PerfectLibTests/PerfectLibTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 135fb33

Please sign in to comment.