From: Petr Tesarik <ptesarik@suse.cz>
Date: Wed, 15 Apr 2015 10:27:25 +0200
Subject: Drop KSFTPErrorCode dependency on libssh2
References: FATE#318874, bsc#917747
Patch-mainline: v0.8.16
Git-commit: 170074b42a166ac3c0cc214c11b152e2c60fd82b Mon Sep 17 00:00:00 2001

This means kdump must maintain the list of error codes. However, this
was the case already, because the error code would have to be added
to the switch statement.

Also, move KSFTPErrorCode to sshtransfer.h If someone wants to handle
sftp-related exceptions, they can include sshtransfer.h explicitly.

Signed-off-by: Petr Tesarik <ptesarik@suse.cz>
---
 kdumptool/global.cc      |  105 ----------------------------------------
 kdumptool/global.h       |   41 ---------------
 kdumptool/sshtransfer.cc |  123 +++++++++++++++++++++++++++++++++++++++++++++++
 kdumptool/sshtransfer.h  |   69 ++++++++++++++++++++++++++
 4 files changed, 192 insertions(+), 146 deletions(-)

--- a/kdumptool/global.cc
+++ b/kdumptool/global.cc
@@ -24,10 +24,6 @@
 
 #include "global.h"
 
-#if HAVE_LIBSSH2
-#   include <libssh2.h>
-#   include <libssh2_sftp.h>
-#endif
 #if HAVE_LIBESMTP
 #   include <libesmtp.h>
 #endif
@@ -73,107 +69,6 @@ const char *KNetError::what() const
 }
 
 //}}}
-//{{{ KSFTPError ---------------------------------------------------------------
-
-#if HAVE_LIBSSH2
-/* -------------------------------------------------------------------------- */
-const char *KSFTPError::what() const
-    throw ()
-{
-    static char buffer[MAXERROR];
-
-    string errorstring = m_errorstring + " (" +
-            getStringForCode(m_errorcode) + ")";
-
-    strncpy(buffer, errorstring.c_str(), MAXERROR);
-    buffer[MAXERROR-1] = 0;
-
-    return buffer;
-}
-
-/* -------------------------------------------------------------------------- */
-string KSFTPError::getStringForCode(int code) const
-    throw ()
-{
-    const char *msg;
-
-    switch (code) {
-        case LIBSSH2_FX_OK:
-            msg = "OK";
-
-        case LIBSSH2_FX_EOF:
-            msg = "End of file";
-
-        case LIBSSH2_FX_NO_SUCH_FILE:
-            msg = "No such file";
-
-        case LIBSSH2_FX_PERMISSION_DENIED:
-            msg = "Permission denied";
-
-        case LIBSSH2_FX_FAILURE:
-            msg = "Failure";
-
-        case LIBSSH2_FX_BAD_MESSAGE:
-            msg = "Bad message";
-
-        case LIBSSH2_FX_NO_CONNECTION:
-            msg = "No connection";
-
-        case LIBSSH2_FX_CONNECTION_LOST:
-            msg = "Connection lost";
-
-        case LIBSSH2_FX_OP_UNSUPPORTED:
-            msg = "Operation unsupported";
-
-        case LIBSSH2_FX_INVALID_HANDLE:
-            msg = "Invalid handle";
-
-        case LIBSSH2_FX_NO_SUCH_PATH:
-            msg = "No such path";
-
-        case LIBSSH2_FX_FILE_ALREADY_EXISTS:
-            msg = "File already exists";
-
-        case LIBSSH2_FX_WRITE_PROTECT:
-            msg = "Write protect";
-
-        case LIBSSH2_FX_NO_MEDIA:
-            msg = "No media";
-
-        case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM:
-            msg = "No space on file system";
-
-        case LIBSSH2_FX_QUOTA_EXCEEDED:
-            msg = "Quote exceeded";
-
-        case LIBSSH2_FX_UNKNOWN_PRINCIPLE:
-            msg = "Unknown principle";
-
-        case LIBSSH2_FX_LOCK_CONFLICT:
-            msg = "Lock conflict";
-
-        case LIBSSH2_FX_DIR_NOT_EMPTY:
-            msg = "Directory not empty";
-
-        case LIBSSH2_FX_NOT_A_DIRECTORY:
-            msg = "Not a directory";
-
-        case LIBSSH2_FX_INVALID_FILENAME:
-            msg = "Invalid file name";
-
-        case LIBSSH2_FX_LINK_LOOP:
-            msg = "Link loop";
-
-        default:
-            msg = "Unknown error";
-    }
-
-    return string(msg);
-}
-
-#endif // HAVE_LIBSSH2
-
-//}}}
 //{{{ KELFError ----------------------------------------------------------------
 
 /* -------------------------------------------------------------------------- */
--- a/kdumptool/global.h
+++ b/kdumptool/global.h
@@ -147,47 +147,6 @@ class KNetError : public KError {
 };
 
 //}}}
-//{{{ KSFTPError ---------------------------------------------------------------
-
-/**
- * Standard error class for SFTP (libssh2) errors.
- */
-class KSFTPError : public KError {
-    public:
-
-        /**
-         * Creates a new object of KSFTPError with string as error message.
-         *
-         * @param[in] string the error message
-         * @param[in] errorcode the value of the sftp_sftp_get_last_error()
-         */
-        KSFTPError(const std::string &string, int errorcode)
-            : KError(string), m_errorcode(errorcode),
-              m_errorstring(string) {}
-
-        /**
-         * Returns a readable error message from the string and the error code.
-         *
-         * @return Error message in the format 'string (strerror(errorcode))'.
-         */
-        virtual const char *what() const
-        throw ();
-
-        /**
-         * Don't know why that is necessary to avoid compiler errors.
-         */
-        virtual ~KSFTPError() throw () {}
-
-    protected:
-        std::string getStringForCode(int code) const
-        throw ();
-
-    private:
-        int m_errorcode;
-        std::string m_errorstring;
-};
-
-//}}}
 //{{{ KELFError ----------------------------------------------------------------
 
 /**
--- a/kdumptool/sshtransfer.cc
+++ b/kdumptool/sshtransfer.cc
@@ -19,6 +19,7 @@
 #include <iostream>
 #include <string>
 #include <cstdlib>
+#include <cstring>
 #include <cerrno>
 #include <stdint.h>
 #include <unistd.h>
@@ -36,6 +37,128 @@ using std::string;
 using std::cerr;
 using std::endl;
 
+//{{{ KSFTPError ---------------------------------------------------------------
+
+#define MAXERROR 4096
+
+/* -------------------------------------------------------------------------- */
+const char *KSFTPError::what() const
+    throw ()
+{
+    static char buffer[MAXERROR];
+
+    string errorstring = m_errorstring + " (" +
+            getStringForCode(m_errorcode) + ")";
+
+    strncpy(buffer, errorstring.c_str(), MAXERROR);
+    buffer[MAXERROR-1] = 0;
+
+    return buffer;
+}
+
+/* -------------------------------------------------------------------------- */
+string KSFTPError::getStringForCode(int code) const
+    throw ()
+{
+    const char *msg;
+
+    switch (code) {
+        case SSH_FX_OK:
+            msg = "OK";
+            break;
+
+        case SSH_FX_EOF:
+            msg = "End of file";
+            break;
+
+        case SSH_FX_NO_SUCH_FILE:
+            msg = "No such file";
+            break;
+
+        case SSH_FX_PERMISSION_DENIED:
+            msg = "Permission denied";
+            break;
+
+        case SSH_FX_FAILURE:
+            msg = "Failure";
+            break;
+
+        case SSH_FX_BAD_MESSAGE:
+            msg = "Bad message";
+            break;
+
+        case SSH_FX_NO_CONNECTION:
+            msg = "No connection";
+            break;
+
+        case SSH_FX_CONNECTION_LOST:
+            msg = "Connection lost";
+            break;
+
+        case SSH_FX_OP_UNSUPPORTED:
+            msg = "Operation unsupported";
+            break;
+
+        case SSH_FX_INVALID_HANDLE:
+            msg = "Invalid handle";
+            break;
+
+        case SSH_FX_NO_SUCH_PATH:
+            msg = "No such path";
+            break;
+
+        case SSH_FX_FILE_ALREADY_EXISTS:
+            msg = "File already exists";
+            break;
+
+        case SSH_FX_WRITE_PROTECT:
+            msg = "Write protect";
+            break;
+
+        case SSH_FX_NO_MEDIA:
+            msg = "No media";
+            break;
+
+        case SSH_FX_NO_SPACE_ON_FILESYSTEM:
+            msg = "No space on file system";
+            break;
+
+        case SSH_FX_QUOTA_EXCEEDED:
+            msg = "Quote exceeded";
+            break;
+
+        case SSH_FX_UNKNOWN_PRINCIPAL:
+            msg = "Unknown principal";
+            break;
+
+        case SSH_FX_LOCK_CONFLICT:
+            msg = "Lock conflict";
+            break;
+
+        case SSH_FX_DIR_NOT_EMPTY:
+            msg = "Directory not empty";
+            break;
+
+        case SSH_FX_NOT_A_DIRECTORY:
+            msg = "Not a directory";
+            break;
+
+        case SSH_FX_INVALID_FILENAME:
+            msg = "Invalid file name";
+            break;
+
+        case SSH_FX_LINK_LOOP:
+            msg = "Link loop";
+            break;
+
+        default:
+            msg = "Unknown error";
+    }
+
+    return string(msg);
+}
+
+//}}}
 //{{{ SFTPPacket ---------------------------------------------------------------
 
 /* -------------------------------------------------------------------------- */
--- a/kdumptool/sshtransfer.h
+++ b/kdumptool/sshtransfer.h
@@ -27,6 +27,75 @@
 #include "socket.h"
 #include "transfer.h"
 
+//{{{ KSFTPError ---------------------------------------------------------------
+
+/**
+ * Error codes defined by the protocol
+ */
+enum {
+    SSH_FX_OK			  =  0,
+    SSH_FX_EOF			  =  1,
+    SSH_FX_NO_SUCH_FILE		  =  2,
+    SSH_FX_PERMISSION_DENIED	  =  3,
+    SSH_FX_FAILURE		  =  4,
+    SSH_FX_BAD_MESSAGE		  =  5,
+    SSH_FX_NO_CONNECTION	  =  6,
+    SSH_FX_CONNECTION_LOST	  =  7,
+    SSH_FX_OP_UNSUPPORTED	  =  8,
+    SSH_FX_INVALID_HANDLE	  =  9,
+    SSH_FX_NO_SUCH_PATH		  = 10,
+    SSH_FX_FILE_ALREADY_EXISTS	  = 11,
+    SSH_FX_WRITE_PROTECT	  = 12,
+    SSH_FX_NO_MEDIA		  = 13,
+    SSH_FX_NO_SPACE_ON_FILESYSTEM = 14,
+    SSH_FX_QUOTA_EXCEEDED	  = 15,
+    SSH_FX_UNKNOWN_PRINCIPAL	  = 16,
+    SSH_FX_LOCK_CONFLICT	  = 17,
+    SSH_FX_DIR_NOT_EMPTY	  = 18,
+    SSH_FX_NOT_A_DIRECTORY	  = 19,
+    SSH_FX_INVALID_FILENAME	  = 20,
+    SSH_FX_LINK_LOOP		  = 21,
+};
+
+/**
+ * Standard error class for SFTP (libssh2) errors.
+ */
+class KSFTPError : public KError {
+    public:
+
+        /**
+         * Creates a new object of KSFTPError with string as error message.
+         *
+         * @param[in] string the error message
+         * @param[in] errorcode the value of the sftp_sftp_get_last_error()
+         */
+        KSFTPError(const std::string &string, int errorcode)
+            : KError(string), m_errorcode(errorcode),
+              m_errorstring(string) {}
+
+        /**
+         * Returns a readable error message from the string and the error code.
+         *
+         * @return Error message in the format 'string (strerror(errorcode))'.
+         */
+        virtual const char *what() const
+        throw ();
+
+        /**
+         * Don't know why that is necessary to avoid compiler errors.
+         */
+        virtual ~KSFTPError() throw () {}
+
+    protected:
+        std::string getStringForCode(int code) const
+        throw ();
+
+    private:
+        int m_errorcode;
+        std::string m_errorstring;
+};
+
+//}}}
 //{{{ SFTPPacket ---------------------------------------------------------------
 
 /**
