Files
Termind/Frameworks/TermindRemoteInfrastructure/SSH/SFTPClient.swift
T

1490 lines
64 KiB
Swift

import AgentKit
import CSSH2
import CryptoKit
import Darwin
import Foundation
import TermindApplication
import TermindDomain
public nonisolated final class TransferCancellationToken: @unchecked Sendable {
public init() {}
private let lock = NSLock()
private var cancelled = false
private var observers: [UUID: @Sendable () -> Void] = [:]
public var isCancelled: Bool { lock.withLock { cancelled } }
public func cancel() {
let callbacks = lock.withLock {
cancelled = true
let callbacks = Array(observers.values)
observers.removeAll()
return callbacks
}
for callback in callbacks { callback() }
}
public func observeCancellation(_ callback: @escaping @Sendable () -> Void) -> UUID {
let id = UUID()
let alreadyCancelled = lock.withLock {
if cancelled { return true }
observers[id] = callback
return false
}
if alreadyCancelled { callback() }
return id
}
public func removeObserver(_ id: UUID) { _ = lock.withLock { observers.removeValue(forKey: id) } }
}
public nonisolated struct SFTPProgress: Sendable {
public var completedBytes: Int64
public var totalBytes: Int64
public var bytesPerSecond: Int64
public init(completedBytes: Int64, totalBytes: Int64, bytesPerSecond: Int64) {
self.completedBytes = completedBytes
self.totalBytes = totalBytes
self.bytesPerSecond = bytesPerSecond
}
}
nonisolated private struct SFTPProgressMeter {
public let total: Int64
public let report: @Sendable (SFTPProgress) -> Void
public private(set) var completed: Int64 = 0
private var sampledBytes: Int64 = 0
private var sampledAt = ContinuousClock.now
public nonisolated init(
total: Int64, offset: Int64 = 0, report: @escaping @Sendable (SFTPProgress) -> Void
) {
self.total = total
self.completed = offset
self.report = report
}
public nonisolated mutating func add(_ bytes: Int) {
completed += Int64(bytes)
sampledBytes += Int64(bytes)
let elapsed = sampledAt.duration(to: .now)
guard elapsed >= .milliseconds(250) else { return }
let components = elapsed.components
let seconds = max(
0.001,
Double(components.seconds) + Double(components.attoseconds) / 1e18
)
report(
SFTPProgress(
completedBytes: completed, totalBytes: total,
bytesPerSecond: Int64(Double(sampledBytes) / seconds)
))
sampledBytes = 0
sampledAt = .now
}
public nonisolated func finish() {
report(
SFTPProgress(
completedBytes: completed, totalBytes: total, bytesPerSecond: 0
))
}
}
public nonisolated protocol SFTPClientProtocol: RemoteFileSystem, Sendable {
func uploadFile(
from localURL: URL, to remotePath: String, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws
func downloadFile(
from remotePath: String, to localURL: URL, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws
func uploadStream(
fromSocket socket: Int32, to remotePath: String, byteCount: Int64, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void,
sourceSucceeded: @escaping @Sendable () -> Bool
) async throws
func openDownloadStream(
at path: String, maximumBytes: Int64, cancellation: TransferCancellationToken
) async throws -> any SFTPDownloadStream
func connect(
using configuration: SSHConnectionConfiguration,
progress: SSHRouteProgressHandler?
) async throws
func connect(
using configuration: SSHConnectionConfiguration,
progress: SSHRouteProgressHandler?,
connectionFailure: (@Sendable (String) -> Void)?
) async throws
func connect(
using request: SSHConnectionRequest,
progress: SSHRouteProgressHandler?,
connectionFailure: (@Sendable (String) -> Void)?
) async throws
func readTextPage(at path: String, offset: Int, limit: Int) async throws -> AgentTextPage
func disconnect() async
func homeDirectory() async throws -> String
func uploadFile(
from localURL: URL, to remotePath: String, overwrite: Bool,
cancellation: TransferCancellationToken,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws
func downloadFile(
from remotePath: String, to localURL: URL, overwrite: Bool,
cancellation: TransferCancellationToken,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws
func copyFile(
from sourcePath: String,
to destinationPath: String,
on destination: any SFTPClientProtocol,
byteCount: Int64,
overwrite: Bool,
cancellation: TransferCancellationToken,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws
func uploadStream(
fromSocket socket: Int32, to remotePath: String, byteCount: Int64, overwrite: Bool,
cancellation: TransferCancellationToken, progress: @escaping @Sendable (SFTPProgress) -> Void,
sourceSucceeded: @escaping @Sendable () -> Bool
) async throws
}
nonisolated extension SFTPClientProtocol {
public func uploadFile(
from localURL: URL, to remotePath: String, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
guard resume.snapshot.editPolicy == nil else { throw SFTPEditFailure.unsupported }
guard resume.snapshot.offset == 0 else { throw SFTPResumeFailure.unsupported }
try await uploadFile(
from: localURL, to: remotePath, overwrite: overwrite,
cancellation: cancellation, progress: progress)
}
public func downloadFile(
from remotePath: String, to localURL: URL, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
guard resume.snapshot.offset == 0 else { throw SFTPResumeFailure.unsupported }
try await downloadFile(
from: remotePath, to: localURL, overwrite: overwrite,
cancellation: cancellation, progress: progress)
}
public func uploadStream(
fromSocket socket: Int32, to remotePath: String, byteCount: Int64, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void,
sourceSucceeded: @escaping @Sendable () -> Bool
) async throws {
guard resume.snapshot.offset == 0 else { throw SFTPResumeFailure.unsupported }
try await uploadStream(
fromSocket: socket, to: remotePath, byteCount: byteCount, overwrite: overwrite,
cancellation: cancellation, progress: progress, sourceSucceeded: sourceSucceeded)
}
public func openDownloadStream(
at path: String, maximumBytes: Int64, cancellation: TransferCancellationToken
) async throws -> any SFTPDownloadStream {
throw SFTPError.operation("This SFTP client does not support streamed downloads.")
}
public func uploadStream(
fromSocket socket: Int32, to remotePath: String, byteCount: Int64, overwrite: Bool,
cancellation: TransferCancellationToken, progress: @escaping @Sendable (SFTPProgress) -> Void,
sourceSucceeded: @escaping @Sendable () -> Bool
) async throws {
throw SFTPError.operation("This SFTP client does not support streamed uploads.")
}
public func connect(
using request: SSHConnectionRequest,
progress: SSHRouteProgressHandler?,
connectionFailure: (@Sendable (String) -> Void)?
) async throws {
try await connect(
using: request.configuration, progress: progress,
connectionFailure: connectionFailure
)
}
public func connect(
using configuration: SSHConnectionConfiguration,
progress: SSHRouteProgressHandler?,
connectionFailure: (@Sendable (String) -> Void)?
) async throws {
try await connect(using: configuration, progress: progress)
}
public func readTextPage(at path: String, offset: Int, limit: Int) async throws -> AgentTextPage {
let data = try await readFile(at: path, maximumBytes: AgentScratchWorkspace.Limits.fileBytes)
var reader = try AgentTextPageReader(offset: offset, limit: limit)
try reader.append(data)
return try reader.finish()
}
public func readFile(at path: String, maximumBytes: Int) async throws -> Data {
throw SFTPError.operation("This SFTP client does not support inline reads.")
}
public func writeFile(_ data: Data, at path: String, overwrite: Bool) async throws {
throw SFTPError.operation("This SFTP client does not support inline writes.")
}
public func copyFile(
from sourcePath: String,
to destinationPath: String,
on destination: any SFTPClientProtocol,
byteCount: Int64,
overwrite: Bool,
cancellation: TransferCancellationToken,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
throw SFTPError.operation("This SFTP client does not support streamed remote copying.")
}
}
public nonisolated enum SFTPError: LocalizedError, Sendable {
case notConnected
case connectionLost(String)
case cancelled
case localFile(String)
case operation(String)
public var errorDescription: String? {
switch self {
case .notConnected: String(localized: "The SFTP connection is not available.")
case .connectionLost(let message): message
case .cancelled: String(localized: "Transfer cancelled.")
case .localFile(let message), .operation(let message): message
}
}
}
/// A failed transfer can leave its temporary remote file when cleanup also fails.
public nonisolated struct SFTPTemporaryFileCleanupError: LocalizedError, Sendable {
public var errorDescription: String? { "\(cause.localizedDescription) (\(path))" }
public let path: String
public let cause: any Error
public init(path: String, cause: any Error) {
self.path = path
self.cause = cause
}
}
public actor SFTPClient: SFTPClientProtocol {
public func uploadFile(
from localURL: URL, to remotePath: String, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
try await worker.uploadFile(
from: localURL, to: remotePath, overwrite: overwrite,
cancellation: cancellation, resume: resume, progress: progress)
}
public func downloadFile(
from remotePath: String, to localURL: URL, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
try await worker.downloadFile(
from: remotePath, to: localURL, overwrite: overwrite,
cancellation: cancellation, resume: resume, progress: progress)
}
public func uploadStream(
fromSocket socket: Int32, to remotePath: String, byteCount: Int64, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState,
progress: @escaping @Sendable (SFTPProgress) -> Void,
sourceSucceeded: @escaping @Sendable () -> Bool
) async throws {
try await worker.uploadFile(
fromSocket: socket, to: remotePath, byteCount: byteCount, overwrite: overwrite,
cancellation: cancellation, resume: resume, progress: progress, sourceSucceeded: sourceSucceeded)
}
public func openDownloadStream(
at path: String, maximumBytes: Int64, cancellation: TransferCancellationToken
) async throws -> any SFTPDownloadStream {
let (id, identity) = try await worker.openDownloadStream(
at: path, maximumBytes: maximumBytes, cancellation: cancellation)
return NativeSFTPDownloadStream(worker: worker, id: id, identity: identity)
}
private let keyboardInteractive: SSHKeyboardInteractiveHandler?
public init(keyboardInteractive: SSHKeyboardInteractiveHandler? = nil) {
self.keyboardInteractive = keyboardInteractive
}
private nonisolated let worker = SFTPWorker()
public func connect(
using configuration: SSHConnectionConfiguration,
progress: SSHRouteProgressHandler? = nil
) async throws {
try await worker.connect(
using: SSHConnectionRequest(configuration: configuration, keyboardInteractive: keyboardInteractive),
progress: progress
)
}
public func connect(
using configuration: SSHConnectionConfiguration,
progress: SSHRouteProgressHandler?,
connectionFailure: (@Sendable (String) -> Void)?
) async throws {
try await connect(
using: SSHConnectionRequest(configuration: configuration),
progress: progress, connectionFailure: connectionFailure
)
}
public func connect(
using request: SSHConnectionRequest,
progress: SSHRouteProgressHandler?,
connectionFailure: (@Sendable (String) -> Void)?
) async throws {
try await worker.connect(
using: request.withKeyboardInteractiveFallback(keyboardInteractive), progress: progress,
connectionFailure: connectionFailure
)
}
public func disconnect() async { await worker.disconnect() }
public func homeDirectory() async throws -> String {
try await worker.homeDirectory()
}
public func listDirectory(at path: String) async throws -> [RemoteFile] {
try await worker.listDirectory(at: path)
}
public func attributes(at path: String) async throws -> RemoteFile? {
try await worker.attributes(at: path)
}
public func readFile(at path: String, maximumBytes: Int) async throws -> Data {
try await worker.readFile(at: path, maximumBytes: maximumBytes)
}
public func readTextPage(at path: String, offset: Int, limit: Int) async throws -> AgentTextPage {
try await worker.readTextPage(at: path, offset: offset, limit: limit)
}
public func writeFile(_ data: Data, at path: String, overwrite: Bool) async throws {
try await worker.writeFile(data, at: path, overwrite: overwrite)
}
public func setPermissions(at path: String, to permissions: UInt32) async throws {
try await worker.setPermissions(at: path, to: permissions)
}
public func createDirectory(at path: String) async throws {
try await worker.createDirectory(at: path)
}
public func rename(from source: String, to destination: String, overwrite: Bool) async throws {
try await worker.rename(from: source, to: destination, overwrite: overwrite)
}
public func removeFile(at path: String) async throws { try await worker.removeFile(at: path) }
public func removeDirectory(at path: String) async throws { try await worker.removeDirectory(at: path) }
public func uploadFile(
from localURL: URL, to remotePath: String, overwrite: Bool,
cancellation: TransferCancellationToken,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
try await worker.uploadFile(
from: localURL, to: remotePath, overwrite: overwrite,
cancellation: cancellation, progress: progress
)
}
public func uploadStream(
fromSocket socket: Int32, to remotePath: String, byteCount: Int64, overwrite: Bool,
cancellation: TransferCancellationToken, progress: @escaping @Sendable (SFTPProgress) -> Void,
sourceSucceeded: @escaping @Sendable () -> Bool
) async throws {
try await worker.uploadFile(
fromSocket: socket, to: remotePath, byteCount: byteCount, overwrite: overwrite,
cancellation: cancellation, progress: progress, sourceSucceeded: sourceSucceeded
)
}
public func downloadFile(
from remotePath: String, to localURL: URL, overwrite: Bool,
cancellation: TransferCancellationToken,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
try await worker.downloadFile(
from: remotePath, to: localURL, overwrite: overwrite,
cancellation: cancellation, progress: progress
)
}
public func copyFile(
from sourcePath: String,
to destinationPath: String,
on destination: any SFTPClientProtocol,
byteCount: Int64,
overwrite: Bool,
cancellation: TransferCancellationToken,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
guard let destination = destination as? SFTPClient else {
throw SFTPError.operation("The destination cannot receive a streamed SFTP copy.")
}
let relay = try SFTPSocketRelay()
let observer = cancellation.observeCancellation { relay.stop() }
defer {
cancellation.removeObserver(observer)
relay.close()
}
try await withTaskCancellationHandler {
try Task.checkCancellation()
try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
do {
try await self.worker.downloadFile(
from: sourcePath, toSocket: relay.writer,
cancellation: cancellation
)
relay.finishWriting()
} catch {
cancellation.cancel()
throw error
}
}
group.addTask {
try await destination.worker.uploadFile(
fromSocket: relay.reader, to: destinationPath,
byteCount: byteCount, overwrite: overwrite,
cancellation: cancellation, progress: progress,
sourceSucceeded: { relay.sourceSucceeded }
)
}
do {
while try await group.next() != nil {}
} catch {
cancellation.cancel()
relay.stop()
group.cancelAll()
throw error
}
}
} onCancel: {
cancellation.cancel()
}
}
}
nonisolated final class SFTPSocketRelay: @unchecked Sendable {
public let writer: Int32
public let reader: Int32
private let lock = NSLock()
private var closed = false
private var readSucceeded = false
public var sourceSucceeded: Bool { lock.withLock { readSucceeded } }
public init() throws {
var pair: [Int32] = [-1, -1]
guard Darwin.socketpair(AF_UNIX, SOCK_STREAM, 0, &pair) == 0 else {
throw SFTPError.operation("Unable to create the SFTP copy relay: \(String(cString: strerror(errno)))")
}
writer = pair[0]
reader = pair[1]
var yes: Int32 = 1
for descriptor in pair {
_ = setsockopt(
descriptor, SOL_SOCKET, SO_NOSIGPIPE, &yes,
socklen_t(MemoryLayout<Int32>.size)
)
}
}
public func finishWriting() {
lock.withLock {
guard !closed else { return }
readSucceeded = true
_ = Darwin.shutdown(writer, SHUT_WR)
}
}
public func stop() {
lock.withLock {
guard !closed else { return }
_ = Darwin.shutdown(writer, SHUT_RDWR)
_ = Darwin.shutdown(reader, SHUT_RDWR)
}
}
public func close() {
lock.withLock {
guard !closed else { return }
closed = true
Darwin.close(writer)
Darwin.close(reader)
}
}
}
private nonisolated final class NativeSFTPDownloadStream: SFTPDownloadStream {
let worker: SFTPWorker
let id: UUID
let sourceIdentity: SFTPSourceIdentity?
var byteCount: Int64 { sourceIdentity!.byteCount }
init(worker: SFTPWorker, id: UUID, identity: SFTPSourceIdentity) {
self.worker = worker
self.id = id
self.sourceIdentity = identity
}
func seek(to offset: Int64) async throws { try await worker.seekDownloadStream(id: id, offset: offset) }
func readChunk() async throws -> Data? { try await worker.readDownloadChunk(id: id) }
func close() async throws { try await worker.closeDownloadStream(id: id) }
}
private nonisolated final class SFTPWorker: @unchecked Sendable {
private struct DownloadHandle {
var handle: OpaquePointer
var byteCount: Int64
var modificationTime: UInt?
var received: Int64 = 0
var eof = false
var cancellation: TransferCancellationToken
}
private var downloadHandles: [UUID: DownloadHandle] = [:]
private let queue = DispatchQueue(label: "com.akinokaede.termind.ssh.sftp")
private var routeTransport: SSHRoutedSocket?
private var session: OpaquePointer?
private var sftp: OpaquePointer?
private var keepaliveTimer: (any DispatchSourceTimer)?
private var connectionFailure: (@Sendable (String) -> Void)?
private var didReportConnectionFailure = false
deinit { cleanup() }
public func connect(
using request: SSHConnectionRequest,
progress: SSHRouteProgressHandler?,
connectionFailure: (@Sendable (String) -> Void)? = nil
) async throws {
await queue.perform { self.cleanup() }
let opened = try await SSHSessionWorker.openAuthenticatedSession(
using: request, timeout: 20, progress: progress
)
do {
try await queue.perform {
self.routeTransport = opened.transport
self.session = opened.session
let session = opened.session
progress?(.initializing(request.configuration.hostID, String(localized: "Starting SFTP subsystem")))
guard let sftp = libssh2_sftp_init(session) else {
throw SSHSessionWorker.sessionError(session, prefix: "Unable to start SFTP")
}
self.sftp = sftp
self.connectionFailure = connectionFailure
self.didReportConnectionFailure = false
self.startKeepalive(for: session)
}
try Task.checkCancellation()
} catch {
await queue.perform { self.cleanup() }
throw error
}
}
public func disconnect() async { await queue.perform { self.cleanup() } }
public func homeDirectory() async throws -> String {
try await queue.perform {
let sftp = try self.requireSFTP()
let source = "."
var target = [CChar](repeating: 0, count: 4 * 1024)
let count = source.withCString { sourceBuffer in
libssh2_sftp_symlink_ex(
sftp, sourceBuffer, UInt32(source.utf8.count),
&target, UInt32(target.count), Int32(LIBSSH2_SFTP_REALPATH)
)
}
guard count > 0 else { throw self.error("Unable to resolve the remote home directory") }
let bytes = target.prefix(Int(count)).map { UInt8(bitPattern: $0) }
return RemotePath.normalized(String(decoding: bytes, as: UTF8.self))
}
}
public func listDirectory(at path: String) async throws -> [RemoteFile] {
try await queue.perform {
let sftp = try self.requireSFTP()
guard
let handle = path.withCString({
libssh2_sftp_open_ex(
sftp, $0, UInt32(path.utf8.count), 0, 0,
Int32(LIBSSH2_SFTP_OPENDIR)
)
})
else {
throw self.error("Unable to open \(path)")
}
defer { _ = libssh2_sftp_close_handle(handle) }
var result: [RemoteFile] = []
var name = [CChar](repeating: 0, count: 4 * 1024)
while true {
var attributes = LIBSSH2_SFTP_ATTRIBUTES()
let count = libssh2_sftp_readdir_ex(
handle, &name, name.count, nil, 0, &attributes
)
if count > 0 {
let bytes = name.prefix(Int(count)).map { UInt8(bitPattern: $0) }
let filename = String(decoding: bytes, as: UTF8.self)
guard filename != ".", filename != ".." else { continue }
result.append(
Self.remoteFile(path: Self.join(path, filename), name: filename, attributes: attributes))
} else if count == 0 {
break
} else {
throw self.error("Unable to read \(path)")
}
}
return result.sorted {
if $0.isDirectory != $1.isDirectory { return $0.isDirectory }
return $0.name.localizedStandardCompare($1.name) == .orderedAscending
}
}
}
public func attributes(at path: String) async throws -> RemoteFile? {
try await queue.perform {
let sftp = try self.requireSFTP()
var attributes = LIBSSH2_SFTP_ATTRIBUTES()
let result = path.withCString {
libssh2_sftp_stat_ex(sftp, $0, UInt32(path.utf8.count), Int32(LIBSSH2_SFTP_LSTAT), &attributes)
}
if result == 0 {
return Self.remoteFile(
path: path, name: URL(fileURLWithPath: path).lastPathComponent,
attributes: attributes
)
}
if libssh2_sftp_last_error(sftp) == LIBSSH2_FX_NO_SUCH_FILE { return nil }
throw self.error("Unable to inspect \(path)")
}
}
func openDownloadStream(
at path: String, maximumBytes: Int64, cancellation: TransferCancellationToken
) async throws -> (UUID, SFTPSourceIdentity) {
try await queue.perform {
guard maximumBytes >= 0 else { throw SFTPDownloadFailure.tooLarge }
guard !cancellation.isCancelled else { throw SFTPError.cancelled }
let sftp = try self.requireSFTP()
var attributes = LIBSSH2_SFTP_ATTRIBUTES()
let inspected = path.withCString {
libssh2_sftp_stat_ex(sftp, $0, UInt32(path.utf8.count), Int32(LIBSSH2_SFTP_LSTAT), &attributes)
}
if inspected != 0 {
if libssh2_sftp_last_error(sftp) == LIBSSH2_FX_NO_SUCH_FILE { throw SFTPDownloadFailure.missingFile }
throw self.error("Unable to inspect download source")
}
guard attributes.flags & UInt(LIBSSH2_SFTP_ATTR_PERMISSIONS) != 0,
attributes.permissions & 0o170000 == 0o100000
else { throw SFTPDownloadFailure.unsupportedType }
guard
let handle = path.withCString({
libssh2_sftp_open_ex(
sftp, $0, UInt32(path.utf8.count), UInt(LIBSSH2_FXF_READ), 0, Int32(LIBSSH2_SFTP_OPENFILE))
})
else { throw self.error("Unable to open download source") }
do {
guard libssh2_sftp_fstat_ex(handle, &attributes, 0) == 0 else {
throw self.error("Unable to inspect download handle")
}
guard attributes.flags & UInt(LIBSSH2_SFTP_ATTR_PERMISSIONS) != 0,
attributes.permissions & 0o170000 == 0o100000
else { throw SFTPDownloadFailure.unsupportedType }
guard attributes.flags & UInt(LIBSSH2_SFTP_ATTR_SIZE) != 0 else {
throw self.error("Unable to determine download size")
}
guard attributes.filesize <= UInt64(maximumBytes), attributes.filesize <= UInt64(Int64.max)
else { throw SFTPDownloadFailure.tooLarge }
guard !cancellation.isCancelled else { throw SFTPError.cancelled }
let id = UUID()
let count = Int64(attributes.filesize)
self.downloadHandles[id] = DownloadHandle(
handle: handle, byteCount: count,
modificationTime: attributes.flags & UInt(LIBSSH2_SFTP_ATTR_ACMODTIME) != 0
? attributes.mtime : nil,
cancellation: cancellation
)
return (id, try self.remoteIdentity(handle))
} catch {
_ = libssh2_sftp_close_handle(handle)
throw error
}
}
}
func readDownloadChunk(id: UUID) async throws -> Data? {
try await queue.perform {
guard var entry = self.downloadHandles[id] else { throw SFTPError.notConnected }
guard !entry.cancellation.isCancelled else { throw SFTPError.cancelled }
if entry.eof { return nil }
var buffer = [UInt8](repeating: 0, count: 64 * 1_024)
let count = libssh2_sftp_read(entry.handle, &buffer, buffer.count)
if count > 0 {
guard Int64(count) <= entry.byteCount - entry.received else { throw SFTPDownloadFailure.sourceChanged }
entry.received += Int64(count)
self.downloadHandles[id] = entry
return Data(buffer.prefix(count))
}
guard count == 0 else { throw self.error("Unable to read download source") }
var final = LIBSSH2_SFTP_ATTRIBUTES()
guard libssh2_sftp_fstat_ex(entry.handle, &final, 0) == 0 else {
throw self.error("Unable to verify download source")
}
guard final.flags & UInt(LIBSSH2_SFTP_ATTR_SIZE) != 0,
final.filesize == UInt64(entry.byteCount), entry.received == entry.byteCount
else { throw SFTPDownloadFailure.sourceChanged }
if let initialTime = entry.modificationTime {
guard final.flags & UInt(LIBSSH2_SFTP_ATTR_ACMODTIME) != 0, final.mtime == initialTime else {
throw SFTPDownloadFailure.sourceChanged
}
}
entry.eof = true
self.downloadHandles[id] = entry
return nil
}
}
func seekDownloadStream(id: UUID, offset: Int64) async throws {
try await queue.perform {
guard var entry = self.downloadHandles[id] else { throw SFTPError.notConnected }
guard offset >= 0, offset <= entry.byteCount else { throw SFTPResumeFailure.partialChanged }
guard !entry.cancellation.isCancelled else { throw SFTPError.cancelled }
libssh2_sftp_seek64(entry.handle, UInt64(offset))
entry.received = offset
entry.eof = false
self.downloadHandles[id] = entry
}
}
func closeDownloadStream(id: UUID) async throws {
try await queue.perform {
guard let entry = self.downloadHandles.removeValue(forKey: id) else { return }
guard libssh2_sftp_close_handle(entry.handle) == 0 else { throw SFTPDownloadFailure.closeFailed }
}
}
public func readTextPage(at path: String, offset: Int, limit: Int) async throws -> AgentTextPage {
try Task.checkCancellation()
let cancellation = TransferCancellationToken()
return try await withTaskCancellationHandler {
try await queue.perform {
var reader = try AgentTextPageReader(offset: offset, limit: limit)
return try self.withRemoteReadHandle(at: path) { handle in
var buffer = [CChar](repeating: 0, count: 16 * 1_024)
while !reader.isComplete {
guard !cancellation.isCancelled else { throw CancellationError() }
let count = libssh2_sftp_read(handle, &buffer, buffer.count)
if count > 0 {
try reader.append(Data(bytes: buffer, count: count))
} else if count == 0 {
break
} else {
throw self.error("Unable to read \(path)")
}
}
guard !cancellation.isCancelled else { throw CancellationError() }
return try reader.finish()
}
}
} onCancel: {
cancellation.cancel()
}
}
public func readFile(at path: String, maximumBytes: Int) async throws -> Data {
try await queue.perform {
guard maximumBytes > 0 else { return Data() }
return try self.withRemoteReadHandle(at: path) { handle in
var data = Data()
var buffer = [CChar](repeating: 0, count: min(64 * 1024, maximumBytes + 1))
while data.count <= maximumBytes {
let wanted = min(buffer.count, maximumBytes + 1 - data.count)
let count = libssh2_sftp_read(handle, &buffer, wanted)
if count > 0 {
data.append(Data(bytes: buffer, count: count))
} else if count == 0 {
break
} else {
throw self.error("Unable to read \(path)")
}
}
guard data.count <= maximumBytes else {
throw SFTPError.operation("\(path) exceeds the inline read limit of \(maximumBytes) bytes.")
}
return data
}
}
}
public func writeFile(_ data: Data, at path: String, overwrite: Bool) async throws {
try await queue.perform {
let sftp = try self.requireSFTP()
if !overwrite {
var attributes = LIBSSH2_SFTP_ATTRIBUTES()
let exists = path.withCString {
libssh2_sftp_stat_ex(
sftp, $0, UInt32(path.utf8.count),
Int32(LIBSSH2_SFTP_LSTAT), &attributes
) == 0
}
if exists { throw SFTPError.operation("\(path) already exists.") }
}
try self.withAtomicRemoteWrite(
sftp: sftp, destination: path, overwrite: overwrite,
temporaryTag: ".termind-agent-part-"
) { handle in
try data.withUnsafeBytes { bytes in
guard let base = bytes.baseAddress?.assumingMemoryBound(to: CChar.self) else {
return
}
var offset = 0
while offset < bytes.count {
let written = libssh2_sftp_write(
handle, base.advanced(by: offset), bytes.count - offset
)
guard written > 0 else { throw self.error("Unable to write \(path)") }
offset += written
}
}
}
}
}
public func setPermissions(at path: String, to permissions: UInt32) async throws {
try await queue.perform {
let sftp = try self.requireSFTP()
var attributes = LIBSSH2_SFTP_ATTRIBUTES()
attributes.flags = UInt(LIBSSH2_SFTP_ATTR_PERMISSIONS)
attributes.permissions = UInt(permissions & 0o7777)
let result = path.withCString {
libssh2_sftp_stat_ex(
sftp, $0, UInt32(path.utf8.count),
Int32(LIBSSH2_SFTP_SETSTAT), &attributes
)
}
guard result == 0 else {
throw self.error("Unable to change permissions for \(path)")
}
}
}
public func createDirectory(at path: String) async throws {
try await queue.perform {
let sftp = try self.requireSFTP()
let result = path.withCString {
libssh2_sftp_mkdir_ex(sftp, $0, UInt32(path.utf8.count), 0o755)
}
guard result == 0 else { throw self.error("Unable to create \(path)") }
}
}
public func rename(from source: String, to destination: String, overwrite: Bool) async throws {
try await queue.perform {
let sftp = try self.requireSFTP()
let flags = overwrite ? Int(LIBSSH2_SFTP_RENAME_OVERWRITE) : 0
let result = source.withCString { sourceBuffer in
destination.withCString { destinationBuffer in
libssh2_sftp_rename_ex(
sftp, sourceBuffer, UInt32(source.utf8.count),
destinationBuffer, UInt32(destination.utf8.count), flags
)
}
}
guard result == 0 else { throw self.error("Unable to rename \(source)") }
}
}
public func removeFile(at path: String) async throws {
try await queue.perform {
let sftp = try self.requireSFTP()
guard
path.withCString({
libssh2_sftp_unlink_ex(sftp, $0, UInt32(path.utf8.count))
}) == 0
else {
throw self.error("Unable to delete \(path)")
}
}
}
public func removeDirectory(at path: String) async throws {
try await queue.perform {
let sftp = try self.requireSFTP()
guard
path.withCString({
libssh2_sftp_rmdir_ex(sftp, $0, UInt32(path.utf8.count))
}) == 0
else {
throw self.error("Unable to delete \(path)")
}
}
}
public func uploadFile(
from localURL: URL, to remotePath: String, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState? = nil,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
try await queue.perform {
let sftp = try self.requireSFTP()
let file = try FileHandle(forReadingFrom: localURL)
defer { try? file.close() }
let identity = try Self.localIdentity(localURL)
try resume?.validateSource(identity)
let offset = resume?.snapshot.offset ?? 0
if let resume { try Self.verifyLocalPrefix(file, snapshot: resume.snapshot, cancellation: cancellation) }
try self.withAtomicRemoteWrite(
sftp: sftp, destination: remotePath, overwrite: overwrite, resume: resume,
cancellation: cancellation,
validateCommit: {
guard !cancellation.isCancelled else { throw SFTPError.cancelled }
guard try Self.localIdentity(localURL) == identity else { throw SFTPResumeFailure.sourceChanged }
}
) { handle in
var meter = SFTPProgressMeter(total: identity.byteCount, offset: offset, report: progress)
while true {
if cancellation.isCancelled { throw SFTPError.cancelled }
let data = try file.read(upToCount: 64 * 1024) ?? Data()
if data.isEmpty { break }
guard Int64(data.count) <= identity.byteCount - meter.completed else {
throw SFTPResumeFailure.sourceChanged
}
try data.withUnsafeBytes { bytes in
guard let base = bytes.baseAddress?.assumingMemoryBound(to: CChar.self) else { return }
var position = 0
while position < bytes.count {
if cancellation.isCancelled { throw SFTPError.cancelled }
let written = libssh2_sftp_write(
handle, base.advanced(by: position), bytes.count - position)
guard written > 0 else { throw self.error("Unable to upload \(remotePath)") }
resume?.acknowledge(Data(bytes: base.advanced(by: position), count: written))
position += written
meter.add(written)
}
}
}
guard meter.completed == identity.byteCount else { throw SFTPResumeFailure.sourceChanged }
meter.finish()
}
}
}
public func downloadFile(
from remotePath: String, to localURL: URL, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState? = nil,
progress: @escaping @Sendable (SFTPProgress) -> Void
) async throws {
try await queue.perform {
try self.withRemoteReadHandle(at: remotePath) { handle in
let fm = FileManager.default
let temporary = URL(
fileURLWithPath: resume?.temporaryPath(for: localURL.path)
?? (localURL.path + ".termind-part-" + UUID().uuidString))
let snapshot = resume?.snapshot
let offset = snapshot?.offset ?? 0
if snapshot?.source == nil {
guard fm.createFile(atPath: temporary.path, contents: nil) else {
throw SFTPError.localFile("Unable to create \(temporary.path)")
}
}
var succeeded = false
defer { if !succeeded && resume == nil { try? fm.removeItem(at: temporary) } }
let file = try FileHandle(forUpdating: temporary)
defer { try? file.close() }
let identity = try self.remoteIdentity(handle)
try resume?.validateSource(identity)
if let snapshot {
guard try file.seekToEnd() == UInt64(offset) else { throw SFTPResumeFailure.partialChanged }
try file.seek(toOffset: 0)
try Self.verifyLocalPrefix(file, snapshot: snapshot, cancellation: cancellation)
try self.verifyRemotePrefix(handle, snapshot: snapshot, cancellation: cancellation)
}
var meter = SFTPProgressMeter(total: identity.byteCount, offset: offset, report: progress)
var buffer = [CChar](repeating: 0, count: 64 * 1024)
while true {
if cancellation.isCancelled { throw SFTPError.cancelled }
let count = libssh2_sftp_read(handle, &buffer, buffer.count)
if count > 0 {
guard Int64(count) <= identity.byteCount - meter.completed else {
throw SFTPResumeFailure.sourceChanged
}
let data = Data(bytes: buffer, count: count)
try file.write(contentsOf: data)
resume?.acknowledge(data)
meter.add(count)
} else if count == 0 {
break
} else {
throw self.error("Unable to download \(remotePath)")
}
}
guard meter.completed == identity.byteCount, try self.remoteIdentity(handle) == identity else {
throw SFTPResumeFailure.sourceChanged
}
if resume?.snapshot.capturesFileVersion == true {
var downloaded = LIBSSH2_SFTP_ATTRIBUTES()
guard libssh2_sftp_fstat_ex(handle, &downloaded, 0) == 0 else {
throw self.error(String(localized: "Unable to inspect edited file"))
}
resume?.recordCommittedVersion(
SFTPFileVersion(
Self.remoteFile(
path: remotePath, name: (remotePath as NSString).lastPathComponent,
attributes: downloaded)))
}
guard !cancellation.isCancelled else { throw SFTPError.cancelled }
try file.synchronize()
meter.finish()
if fm.fileExists(atPath: localURL.path) {
guard overwrite else { throw SFTPError.localFile("\(localURL.lastPathComponent) already exists.") }
_ = try fm.replaceItemAt(localURL, withItemAt: temporary)
} else {
try fm.moveItem(at: temporary, to: localURL)
}
resume?.didCommit()
succeeded = true
}
}
}
private static func localIdentity(_ url: URL) throws -> SFTPSourceIdentity {
let values = try url.resourceValues(forKeys: [.fileSizeKey, .contentModificationDateKey, .isRegularFileKey])
guard values.isRegularFile == true, let size = values.fileSize else { throw SFTPResumeFailure.sourceChanged }
return SFTPSourceIdentity(byteCount: Int64(size), modifiedAt: values.contentModificationDate)
}
private func remoteIdentity(_ handle: OpaquePointer) throws -> SFTPSourceIdentity {
var attrs = LIBSSH2_SFTP_ATTRIBUTES()
guard libssh2_sftp_fstat_ex(handle, &attrs, 0) == 0 else { throw error("Unable to inspect transfer file") }
guard attrs.flags & UInt(LIBSSH2_SFTP_ATTR_SIZE) != 0, attrs.filesize <= UInt64(Int64.max),
attrs.flags & UInt(LIBSSH2_SFTP_ATTR_PERMISSIONS) != 0, attrs.permissions & 0o170000 == 0o100000
else { throw SFTPResumeFailure.sourceChanged }
return SFTPSourceIdentity(
byteCount: Int64(attrs.filesize),
modifiedAt:
attrs.flags & UInt(LIBSSH2_SFTP_ATTR_ACMODTIME) != 0
? Date(timeIntervalSince1970: Double(attrs.mtime)) : nil)
}
private static func verifyLocalPrefix(
_ file: FileHandle, snapshot: SFTPResumeState.Snapshot, cancellation: TransferCancellationToken
) throws {
var remaining = snapshot.offset
var digest = SHA256()
while remaining > 0 {
guard !cancellation.isCancelled else { throw SFTPError.cancelled }
let data = try file.read(upToCount: Int(min(remaining, 64 * 1024))) ?? Data()
guard !data.isEmpty else { throw SFTPResumeFailure.partialChanged }
digest.update(data: data)
remaining -= Int64(data.count)
}
guard digest.finalize() == snapshot.digest.finalize() else { throw SFTPResumeFailure.partialChanged }
}
private func verifyRemotePrefix(
_ handle: OpaquePointer, snapshot: SFTPResumeState.Snapshot, cancellation: TransferCancellationToken
) throws {
libssh2_sftp_seek64(handle, 0)
var remaining = snapshot.offset
var digest = SHA256()
var buffer = [UInt8](repeating: 0, count: 64 * 1024)
while remaining > 0 {
guard !cancellation.isCancelled else { throw SFTPError.cancelled }
let count = libssh2_sftp_read(handle, &buffer, Int(min(remaining, Int64(buffer.count))))
guard count > 0 else {
if count < 0 { throw error("Unable to verify incomplete file") }
throw SFTPResumeFailure.partialChanged
}
digest.update(data: Data(buffer.prefix(count)))
remaining -= Int64(count)
}
guard digest.finalize() == snapshot.digest.finalize() else { throw SFTPResumeFailure.partialChanged }
libssh2_sftp_seek64(handle, UInt64(snapshot.offset))
}
public func downloadFile(
from remotePath: String, toSocket socket: Int32,
cancellation: TransferCancellationToken
) async throws {
try await queue.perform {
try self.withRemoteReadHandle(at: remotePath) { handle in
var buffer = [CChar](repeating: 0, count: 64 * 1024)
while true {
if cancellation.isCancelled { throw SFTPError.cancelled }
let count = libssh2_sftp_read(handle, &buffer, buffer.count)
if count > 0 {
var offset = 0
while offset < count {
if cancellation.isCancelled { throw SFTPError.cancelled }
let sent = buffer.withUnsafeBytes { bytes in
Darwin.send(
socket, bytes.baseAddress!.advanced(by: offset),
count - offset, 0
)
}
if sent > 0 {
offset += sent
} else if sent < 0, errno == EINTR {
continue
} else {
throw SFTPError.operation("The SFTP copy relay was interrupted.")
}
}
} else if count == 0 {
break
} else {
throw self.error("Unable to read \(remotePath)")
}
}
}
}
}
public func uploadFile(
fromSocket socket: Int32, to remotePath: String,
byteCount: Int64, overwrite: Bool,
cancellation: TransferCancellationToken, resume: SFTPResumeState? = nil,
progress: @escaping @Sendable (SFTPProgress) -> Void,
sourceSucceeded: @escaping @Sendable () -> Bool
) async throws {
try await queue.perform {
let sftp = try self.requireSFTP()
try self.withAtomicRemoteWrite(
sftp: sftp, destination: remotePath, overwrite: overwrite, resume: resume, cancellation: cancellation,
validateCommit: { guard !cancellation.isCancelled else { throw SFTPError.cancelled } }
) { handle in
var meter = SFTPProgressMeter(total: byteCount, offset: resume?.snapshot.offset ?? 0, report: progress)
var buffer = [CChar](repeating: 0, count: 64 * 1024)
while true {
if cancellation.isCancelled { throw SFTPError.cancelled }
let count = Darwin.recv(socket, &buffer, buffer.count, 0)
if count > 0 {
guard Int64(count) <= byteCount - meter.completed else {
throw SFTPError.operation(String(localized: "The SFTP copy source grew during transfer."))
}
try buffer.withUnsafeBytes { bytes in
guard let base = bytes.baseAddress?.assumingMemoryBound(to: CChar.self)
else { return }
var offset = 0
while offset < count {
if cancellation.isCancelled { throw SFTPError.cancelled }
let written = libssh2_sftp_write(
handle, base.advanced(by: offset), count - offset
)
guard written > 0 else {
throw self.error("Unable to copy to \(remotePath)")
}
resume?.acknowledge(Data(bytes: base.advanced(by: offset), count: written))
offset += written
meter.add(written)
}
}
} else if count == 0 {
break
} else if errno != EINTR {
throw SFTPError.operation("The SFTP copy relay was interrupted.")
}
}
guard !cancellation.isCancelled, sourceSucceeded() else { throw SFTPError.cancelled }
guard meter.completed == byteCount else {
throw SFTPError.operation(
"The source changed while it was being copied "
+ "(expected \(byteCount) bytes, received \(meter.completed))."
)
}
meter.finish()
}
}
}
private func withRemoteReadHandle<T>(
at path: String, read: (OpaquePointer) throws -> T
) throws -> T {
let sftp = try requireSFTP()
guard
let handle = path.withCString({
libssh2_sftp_open_ex(
sftp, $0, UInt32(path.utf8.count), UInt(LIBSSH2_FXF_READ), 0,
Int32(LIBSSH2_SFTP_OPENFILE)
)
})
else {
throw error("Unable to open \(path)")
}
defer { _ = libssh2_sftp_close_handle(handle) }
return try read(handle)
}
private func withAtomicRemoteWrite(
sftp: OpaquePointer,
destination: String,
overwrite: Bool,
temporaryTag: String = ".termind-part-",
resume: SFTPResumeState? = nil,
cancellation: TransferCancellationToken = TransferCancellationToken(),
validateCommit: () throws -> Void = {},
write: (OpaquePointer) throws -> Void
) throws {
if resume?.snapshot.committed == true { return }
let temporary = resume?.temporaryPath(for: destination) ?? (destination + temporaryTag + UUID().uuidString)
let continuing = resume?.snapshot.hasTemporaryFile == true
if continuing {
var attributes = LIBSSH2_SFTP_ATTRIBUTES()
let result = temporary.withCString {
libssh2_sftp_stat_ex(sftp, $0, UInt32(temporary.utf8.count), Int32(LIBSSH2_SFTP_LSTAT), &attributes)
}
if result != 0 {
guard libssh2_sftp_last_error(sftp) == LIBSSH2_FX_NO_SUCH_FILE else {
throw error("Unable to inspect incomplete file")
}
guard let resume, resume.snapshot.committing else { throw SFTPResumeFailure.partialChanged }
try withRemoteReadHandle(at: destination) { handle in
guard try remoteIdentity(handle).byteCount == resume.snapshot.offset else {
throw SFTPResumeFailure.partialChanged
}
try verifyRemotePrefix(handle, snapshot: resume.snapshot, cancellation: cancellation)
}
if let file = try editTarget(sftp: sftp, path: destination) {
resume.recordCommittedVersion(SFTPFileVersion(file))
}
resume.didCommit()
return
}
guard attributes.flags & UInt(LIBSSH2_SFTP_ATTR_PERMISSIONS) != 0,
attributes.permissions & 0o170000 == 0o100000
else { throw SFTPResumeFailure.partialChanged }
}
let editPolicy = resume?.snapshot.editPolicy
if let editPolicy { try editPolicy.validate(try editTarget(sftp: sftp, path: destination)) }
let flags =
resume == nil
? UInt(LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC)
: UInt(LIBSSH2_FXF_READ | LIBSSH2_FXF_WRITE | (continuing ? 0 : (LIBSSH2_FXF_CREAT | LIBSSH2_FXF_EXCL)))
guard
let handle = temporary.withCString({
libssh2_sftp_open_ex(
sftp, $0, UInt32(temporary.utf8.count), flags, 0o600,
Int32(LIBSSH2_SFTP_OPENFILE)
)
})
else {
throw error("Unable to create \(destination)")
}
resume?.createdTemporaryFile()
var handleOpen = true
do {
if let resume {
let snapshot = resume.snapshot
let identity = try remoteIdentity(handle)
guard identity.byteCount >= snapshot.offset else { throw SFTPResumeFailure.partialChanged }
try verifyRemotePrefix(handle, snapshot: snapshot, cancellation: cancellation)
if identity.byteCount != snapshot.offset {
var size = LIBSSH2_SFTP_ATTRIBUTES()
size.flags = UInt(LIBSSH2_SFTP_ATTR_SIZE)
size.filesize = UInt64(snapshot.offset)
guard libssh2_sftp_fstat_ex(handle, &size, 1) == 0 else {
throw error("Unable to trim incomplete file")
}
}
}
try write(handle)
if let resume {
guard try remoteIdentity(handle).byteCount == resume.snapshot.offset else {
throw SFTPResumeFailure.partialChanged
}
}
if let editPolicy {
var mode = LIBSSH2_SFTP_ATTRIBUTES()
mode.flags = UInt(LIBSSH2_SFTP_ATTR_PERMISSIONS)
mode.permissions = UInt(editPolicy.permissions & 0o7777)
guard libssh2_sftp_fstat_ex(handle, &mode, 1) == 0 else {
throw self.error(String(localized: "Unable to preserve remote file permissions"))
}
var committed = LIBSSH2_SFTP_ATTRIBUTES()
guard libssh2_sftp_fstat_ex(handle, &committed, 0) == 0 else {
throw self.error(String(localized: "Unable to inspect edited file"))
}
resume?.recordCommittedVersion(
SFTPFileVersion(
Self.remoteFile(
path: destination, name: (destination as NSString).lastPathComponent, attributes: committed)
))
}
let closed = libssh2_sftp_close_handle(handle)
handleOpen = false
guard closed == 0 else { throw error("Unable to close \(temporary)") }
try validateCommit()
if let editPolicy { try editPolicy.validate(try editTarget(sftp: sftp, path: destination)) }
// Do not turn an upload into destructive directory replacement.
var attributes = LIBSSH2_SFTP_ATTRIBUTES()
let exists =
destination.withCString {
libssh2_sftp_stat_ex(
sftp, $0, UInt32(destination.utf8.count), Int32(LIBSSH2_SFTP_LSTAT), &attributes)
} == 0
if exists && (!overwrite || (attributes.permissions & 0o170000) == 0o040000) {
throw SFTPStreamUploadOperation.Failure.targetConflict
}
resume?.willCommit()
try renameOnQueue(sftp: sftp, from: temporary, to: destination, overwrite: overwrite)
resume?.didCommit()
} catch {
if handleOpen { _ = libssh2_sftp_close_handle(handle) }
if resume != nil { throw error }
let removed = temporary.withCString {
libssh2_sftp_unlink_ex(sftp, $0, UInt32(temporary.utf8.count))
}
if removed != 0 { throw SFTPTemporaryFileCleanupError(path: temporary, cause: error) }
throw error
}
}
private func editTarget(sftp: OpaquePointer, path: String) throws -> RemoteFile? {
var attributes = LIBSSH2_SFTP_ATTRIBUTES()
let result = path.withCString {
libssh2_sftp_stat_ex(sftp, $0, UInt32(path.utf8.count), Int32(LIBSSH2_SFTP_LSTAT), &attributes)
}
if result != 0 {
if libssh2_sftp_last_error(sftp) == LIBSSH2_FX_NO_SUCH_FILE { return nil }
throw error(String(localized: "Unable to inspect remote edit destination"))
}
return Self.remoteFile(path: path, name: (path as NSString).lastPathComponent, attributes: attributes)
}
private func renameOnQueue(sftp: OpaquePointer, from source: String, to destination: String, overwrite: Bool) throws
{
let flags = overwrite ? Int(LIBSSH2_SFTP_RENAME_OVERWRITE) : 0
let result = SFTPRenameOperation.perform(
overwrite: overwrite,
posixRename: {
source.withCString { sourceBuffer in
destination.withCString { destinationBuffer in
libssh2_sftp_posix_rename_ex(
sftp, sourceBuffer, source.utf8.count, destinationBuffer, destination.utf8.count)
}
}
},
standardRename: {
source.withCString { sourceBuffer in
destination.withCString { destinationBuffer in
libssh2_sftp_rename_ex(
sftp, sourceBuffer, UInt32(source.utf8.count),
destinationBuffer, UInt32(destination.utf8.count), flags)
}
}
},
lastStatus: { libssh2_sftp_last_error(sftp) })
guard result == 0 else { throw error("Unable to finish \(destination)") }
}
private func requireSFTP() throws -> OpaquePointer {
guard let sftp else { throw SFTPError.notConnected }
return sftp
}
private func error(_ prefix: String) -> SFTPError {
guard let sftp else { return .operation(prefix) }
let sftpCode = libssh2_sftp_last_error(sftp)
let sessionCode = session.map(libssh2_session_last_errno) ?? 0
let message = "\(prefix): SFTP error \(sftpCode)"
let lostSFTPCodes = [
UInt(LIBSSH2_FX_NO_CONNECTION), UInt(LIBSSH2_FX_CONNECTION_LOST),
]
let lostSessionCodes = [
LIBSSH2_ERROR_SOCKET_SEND, LIBSSH2_ERROR_SOCKET_RECV,
LIBSSH2_ERROR_TIMEOUT, LIBSSH2_ERROR_CHANNEL_CLOSED,
]
if lostSFTPCodes.contains(sftpCode) || lostSessionCodes.contains(sessionCode) {
reportConnectionFailure(message)
return .connectionLost(message)
}
return .operation(message)
}
private func cleanup() {
keepaliveTimer?.cancel()
keepaliveTimer = nil
for entry in downloadHandles.values { _ = libssh2_sftp_close_handle(entry.handle) }
downloadHandles.removeAll()
if let sftp {
_ = libssh2_sftp_shutdown(sftp)
self.sftp = nil
}
if let session {
_ = libssh2_session_disconnect_ex(session, SSH_DISCONNECT_BY_APPLICATION, "Termind closed SFTP", "")
_ = libssh2_session_free(session)
self.session = nil
}
routeTransport?.close()
routeTransport = nil
connectionFailure = nil
didReportConnectionFailure = false
}
private func startKeepalive(for session: OpaquePointer) {
libssh2_keepalive_config(session, 1, 15)
let timer = DispatchSource.makeTimerSource(queue: queue)
timer.schedule(deadline: .now() + 15, repeating: 15)
timer.setEventHandler { [weak self] in
guard let self, self.session == session else { return }
var secondsToNext: Int32 = 0
let result = libssh2_keepalive_send(session, &secondsToNext)
if result < 0, result != LIBSSH2_ERROR_EAGAIN {
let error = SSHSessionWorker.sessionError(
session, prefix: "SFTP keepalive failed"
)
self.reportConnectionFailure(error.localizedDescription)
}
}
keepaliveTimer = timer
timer.resume()
}
private func reportConnectionFailure(_ message: String) {
guard !didReportConnectionFailure else { return }
didReportConnectionFailure = true
connectionFailure?(message)
}
private static func join(_ base: String, _ component: String) -> String {
base == "/" ? "/\(component)" : "\(base)/\(component)"
}
private static func remoteFile(
path: String, name: String, attributes: LIBSSH2_SFTP_ATTRIBUTES
) -> RemoteFile {
let mode = UInt32(attributes.permissions)
let kind: RemoteFile.Kind
if mode & UInt32(LIBSSH2_SFTP_S_IFMT) == UInt32(LIBSSH2_SFTP_S_IFDIR) {
kind = .directory
} else if mode & UInt32(LIBSSH2_SFTP_S_IFMT) == UInt32(LIBSSH2_SFTP_S_IFREG) {
kind = .file
} else if mode & UInt32(LIBSSH2_SFTP_S_IFMT) == UInt32(LIBSSH2_SFTP_S_IFLNK) {
kind = .symbolicLink
} else if mode == 0 {
kind = .unknown
} else {
kind = .special
}
return RemoteFile(
path: path, name: name, kind: kind, size: Int64(attributes.filesize),
modifiedAt: attributes.mtime == 0 ? nil : Date(timeIntervalSince1970: TimeInterval(attributes.mtime)),
permissions: mode, ownerUID: UInt32(attributes.uid), ownerGID: UInt32(attributes.gid)
)
}
}