From d83c7a20ce99be79edf011ead037604f44c82c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=9B=BD=E6=94=BF?= Date: Fri, 8 Nov 2024 16:35:54 +0800 Subject: [PATCH] Add SFTP helper functions to retrieve the current working directory and obtain the real absolute path of a relative path. --- README.md | 4 ++++ Sources/Citadel/SFTP/Client/SFTPClient.swift | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 16678fd..b34244b 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,10 @@ To begin with SFTP, you must instantiate an SFTPClient based on your SSHClient: // Open an SFTP session on the SSH client let sftp = try await client.openSFTP() +// Get the current working directory +let cwd = try await sftp.getRealPath(atPath: ".") +//Obtain the real path of the directory eg "/opt/vulscan/.. -> /opt" +let truePath = try await sftp.getRealPath(atPath: "/opt/vulscan/..") // List the contents of the /etc directory let directoryContents = try await sftp.listDirectory(atPath: "/etc") diff --git a/Sources/Citadel/SFTP/Client/SFTPClient.swift b/Sources/Citadel/SFTP/Client/SFTPClient.swift index 1f13d61..6253d88 100644 --- a/Sources/Citadel/SFTP/Client/SFTPClient.swift +++ b/Sources/Citadel/SFTP/Client/SFTPClient.swift @@ -284,6 +284,15 @@ public final class SFTPClient: Sendable { self.logger.debug("SFTP renamed file at \(oldPath) to \(newPath)") } + // Obtain the real path of the directory eg "/opt/vulscan/.. -> /opt" and Pass in ". "on initialization You can get the current working directory + public func getRealPath(atPath path: String) async throws -> String { + guard case let .name(realpath) = try await sendRequest(.realpath(.init(requestId: self.allocateRequestId(), path: path))) else { + self.logger.warning("SFTP server returned bad response to open file request, this is a protocol error") + throw SFTPError.invalidResponse + } + return realpath.path + } + } extension SSHClient {