Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SFTP helper functions to retrieve the current working directory and obtain the real absolute path of a relative path. #79

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
9 changes: 9 additions & 0 deletions Sources/Citadel/SFTP/Client/SFTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down