Skip to content

Commit

Permalink
Merge pull request #79 from alien10086/feature/sftptoolfuntion
Browse files Browse the repository at this point in the history
Add SFTP helper functions to retrieve the current working directory and obtain the real absolute path of a relative path.
  • Loading branch information
Joannis authored Nov 8, 2024
2 parents afe3204 + d83c7a2 commit 819bb80
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
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 @@ -288,6 +288,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

0 comments on commit 819bb80

Please sign in to comment.