package jetbrains.buildServer.deployer.agent.ssh.scp;

import java.io.IOException;
import java.io.InputStream;

public class ScpExecUtil {

  public static int checkScpAck(InputStream in) throws IOException {
    int b = in.read();
    // b may be 0 for success,
    //          1 for error,
    //          2 for fatal error,
    //          -1
    if (b == 0) return b;
    if (b == -1) return b;

    if (b == 1 || b == 2) {
      final StringBuilder sb = new StringBuilder();
      int c;
      do {
        c = in.read();
        sb.append((char) c);
      }
      while (c != '\n');
      throw new IOException(sb.toString());
    }
    return b;
  }
}
@@ -278,21 +278,23 @@
   1 for 
-error
+warning
 ,%0A    //
@@ -558,22 +558,16 @@
 %0A      %7D
-%0A     
  while (
@@ -610,43 +610,414 @@
 ion(
-sb.toString());%0A    %7D%0A    return b;
+%22Remote system responded with error: %22 + sb.toString());%0A    %7D else %7B%0A      final int available = in.available();%0A      byte%5B%5D content = new byte%5Bavailable + 1%5D;%0A      content%5B0%5D = (byte) b;%0A      final int read = in.read(content, 1, available);%0A      final String message = new String(content, 0, read + 1, %22UTF-8%22);%0A      throw new IOException(%22Unexpected response from remote system: %22 + message);%0A    %7D
 %0A  %7D

fix handling of error responses during SSH upload using SCP TW-45962