diff --git a/src/storage/sftp/read.c b/src/storage/sftp/read.c index 6637be371..e10346fc7 100644 --- a/src/storage/sftp/read.c +++ b/src/storage/sftp/read.c @@ -55,16 +55,13 @@ storageReadSftpOpen(THIS_VOID) if (this->sftpHandle == NULL) { const int rc = libssh2_session_last_errno(this->session); + const uint64_t sftpErr = libssh2_sftp_last_error(this->sftpSession); - // Handle errors except missing, which is reported to the caller via the result - if (rc == LIBSSH2_ERROR_SFTP_PROTOCOL || rc == LIBSSH2_ERROR_EAGAIN) + // Throw on any error except missing, which is reported to the caller via the result + if (rc != LIBSSH2_ERROR_SFTP_PROTOCOL || sftpErr != LIBSSH2_FX_NO_SUCH_FILE) { - if (libssh2_sftp_last_error(this->sftpSession) != LIBSSH2_FX_NO_SUCH_FILE) - { - storageSftpEvalLibSsh2Error( - rc, libssh2_sftp_last_error(this->sftpSession), &FileOpenError, - strNewFmt(STORAGE_ERROR_READ_OPEN, strZ(this->name)), NULL); - } + storageSftpEvalLibSsh2Error( + rc, sftpErr, &FileOpenError, strNewFmt(STORAGE_ERROR_READ_OPEN, strZ(this->name)), NULL); } } // Else success diff --git a/test/src/module/storage/sftpTest.c b/test/src/module/storage/sftpTest.c index 6342f8c16..3a4a2bac1 100644 --- a/test/src/module/storage/sftpTest.c +++ b/test/src/module/storage/sftpTest.c @@ -2492,21 +2492,47 @@ testRun(void) HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_EAGAIN), HRN_LIBSSH2_BLOCK(.resultInt = SSH2_BLOCK_READING_WRITING), HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_EAGAIN), - HRN_LIBSSH2_SFTP_ERROR(LIBSSH2_FX_OK), - HRN_LIBSSH2_SFTP_ERROR(LIBSSH2_ERROR_NONE)); + HRN_LIBSSH2_SFTP_ERROR(LIBSSH2_FX_OK)); TEST_ERROR_FMT( ioReadOpen(storageReadIo(file)), FileOpenError, STORAGE_ERROR_READ_OPEN ": libssh2 error [-37]", strZ(fileName)); // ------------------------------------------------------------------------------------------------------------------------- - TEST_TITLE("read missing - not sftp, not EAGAIN"); + TEST_TITLE("read open error - not sftp, not EAGAIN"); HRN_LIBSSH2_SCRIPT_SET( HRN_LIBSSH2_OPEN_READ(TEST_PATH "/readtest.txt", .resultNull = true), HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_METHOD_NOT_SUPPORTED), - HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_METHOD_NOT_SUPPORTED)); + HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_METHOD_NOT_SUPPORTED), + HRN_LIBSSH2_SFTP_ERROR(LIBSSH2_FX_OK)); - TEST_ERROR_FMT(ioReadOpen(storageReadIo(file)), FileMissingError, STORAGE_ERROR_READ_MISSING, strZ(fileName)); + TEST_ERROR_FMT( + ioReadOpen(storageReadIo(file)), FileOpenError, STORAGE_ERROR_READ_OPEN ": libssh2 error [-33]", strZ(fileName)); + + // ------------------------------------------------------------------------------------------------------------------------- + TEST_TITLE("read open error - socket error is not masked as missing"); + + HRN_LIBSSH2_SCRIPT_SET( + HRN_LIBSSH2_OPEN_READ(TEST_PATH "/readtest.txt", .resultNull = true), + HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_SOCKET_RECV), + HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_SOCKET_RECV), + HRN_LIBSSH2_SFTP_ERROR(LIBSSH2_FX_OK)); + + TEST_ERROR_FMT( + ioReadOpen(storageReadIo(file)), FileOpenError, STORAGE_ERROR_READ_OPEN ": libssh2 error [-43]", strZ(fileName)); + + // ------------------------------------------------------------------------------------------------------------------------- + TEST_TITLE("read open error - sftp error other than no such file is not masked as missing"); + + HRN_LIBSSH2_SCRIPT_SET( + HRN_LIBSSH2_OPEN_READ(TEST_PATH "/readtest.txt", .resultNull = true), + HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_SFTP_PROTOCOL), + HRN_LIBSSH2_ERRNO(LIBSSH2_ERROR_SFTP_PROTOCOL), + HRN_LIBSSH2_SFTP_ERROR(LIBSSH2_FX_PERMISSION_DENIED)); + + TEST_ERROR_FMT( + ioReadOpen(storageReadIo(file)), FileOpenError, + STORAGE_ERROR_READ_OPEN ": libssh2 error [-31]: sftp error [3] permission denied", strZ(fileName)); // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("read success");