From 17e2a834adf4f5520779e3e1a56fa23bdf597817 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Thu, 19 Jan 2023 16:14:44 -0800 Subject: [PATCH] handle full path used with SFTP Get-Put commands --- examples/sftpclient/sftpclient.c | 19 +++++++++++++------ scripts/get-put.test | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index 00b3a49e..fa7bc153 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -1502,17 +1502,24 @@ static int doAutopilot(int cmd, char* local, char* remote) int err; int ret = WS_SUCCESS; char fullpath[128] = "."; - WS_SFTPNAME* name; + WS_SFTPNAME* name = NULL; - do { - name = wolfSSH_SFTP_RealPath(ssh, fullpath); - err = wolfSSH_get_error(ssh); - } while ((err == WS_WANT_READ || err == WS_WANT_WRITE) && + if (remote != NULL && remote[0] == '/') { + /* use remote absolute path if provided */ + WMEMSET(fullpath, 0, sizeof(fullpath)); + WSTRNCPY(fullpath, remote, sizeof(fullpath) - 1); + } + else { + do { + name = wolfSSH_SFTP_RealPath(ssh, fullpath); + err = wolfSSH_get_error(ssh); + } while ((err == WS_WANT_READ || err == WS_WANT_WRITE) && ret != WS_SUCCESS); - snprintf(fullpath, sizeof(fullpath), "%s/%s", + snprintf(fullpath, sizeof(fullpath), "%s/%s", name == NULL ? "." : name->fName, remote); + } do { if (cmd == AUTOPILOT_PUT) { diff --git a/scripts/get-put.test b/scripts/get-put.test index 9be26740..e1ca2cc3 100755 --- a/scripts/get-put.test +++ b/scripts/get-put.test @@ -109,4 +109,22 @@ then exit 1 fi +# using full path test. +rm -rf sample2-copy.txt +PWD=`pwd` +if ! ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$PORT" \ + -g -l sample2.txt -r $PWD/sample2-copy.txt +then + echo "Unable to put file using full path." + do_cleanup + exit 1 +fi + +if ! diff sample2.txt sample2-copy.txt >/dev/null +then + echo "Put test files do not match after using full path." + do_cleanup + exit 1 +fi + do_cleanup