Commit 6d22e8b0 authored by Drew's avatar Drew

Implement missing Foundation API on Linux

This workaround is PRed to upstream at
https://github.com/apple/swift-corelibs-foundation/pull/248
parent 0b8b3339
......@@ -32,4 +32,53 @@ extension NSString {
return (self as String)
#endif
}
}
// MARK: NSFileManager.copyItemAtPath
// https://github.com/apple/swift-corelibs-foundation/pull/248
enum CopyError: ErrorType {
case CantOpenSourceFile(Int32)
case CantOpenDestFile(Int32)
case CantReadSourceFile(Int32)
case CantWriteDestFile(Int32)
}
extension NSFileManager {
func copyItemAtPath_SWIFTBUG(srcPath: String, toPath dstPath: String) throws {
let fd_from = open(srcPath, O_RDONLY)
if fd_from < 0 {
throw CopyError.CantOpenSourceFile(errno)
}
defer { precondition(close(fd_from) >= 0) }
let permission = (try! attributesOfItemAtPath(srcPath)[NSFilePosixPermissions] as! NSNumber).unsignedShortValue
let fd_to = open(dstPath, O_WRONLY | O_CREAT | O_EXCL, permission)
if fd_to < 0 {
throw CopyError.CantOpenDestFile(errno)
}
defer { precondition(close(fd_to) >= 0) }
var buf = [UInt8](count: 4096, repeatedValue: 0)
while true {
let nread = read(fd_from, &buf, buf.count)
if nread < 0 { throw CopyError.CantReadSourceFile(errno) }
if nread == 0 { break }
var writeSlice = buf[0..<nread]
while true {
var nwritten: Int! = nil
writeSlice.withUnsafeBufferPointer({ (ptr) -> () in
nwritten = write(fd_to, ptr.baseAddress, ptr.count)
})
if nwritten < 0 {
throw CopyError.CantWriteDestFile(errno)
}
writeSlice = writeSlice[writeSlice.startIndex.advancedBy(nwritten)..<writeSlice.endIndex]
if writeSlice.count == 0 { break }
}
}
}
}
\ No newline at end of file
......@@ -343,5 +343,5 @@ private func copyByOverwriting(fromPath: String, toPath: String) throws {
if manager.fileExistsAtPath(toPath) {
try manager.removeItemAtPath(toPath)
}
try! manager.copyItemAtPath(fromPath, toPath: toPath)
try! manager.copyItemAtPath_SWIFTBUG(fromPath, toPath: toPath)
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment