Package ch.ethz.ssh2

Examples of ch.ethz.ssh2.SFTPv3Client


        srcoff += writeRequestLen;
        len -= writeRequestLen;

        byte[] resp = receiveMessage(34000);

        TypesReader tr = new TypesReader(resp);

        int t = tr.readByte();

        int rep_id = tr.readUINT32();
        if (rep_id != req_id)
          throw new IOException("The server sent an invalid id field.");

        if (t != Packet.SSH_FXP_STATUS)
          throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");

        int errorCode = tr.readUINT32();

        if (errorCode == ErrorCodes.SSH_FX_OK)
          continue;

        String errorMessage = tr.readString();

        throw new SFTPException(errorMessage, errorCode);
      }
  }
View Full Code Here


  private final void closeHandle(byte[] handle) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(handle, 0, handle.length);

    sendMessage(Packet.SSH_FXP_CLOSE, req_id, tw.getBytes());

    expectStatusOKMessage(req_id);
  }
View Full Code Here

  {
    checkHandleValidAndOpen(handle);

    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_FSTAT...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    if (debug != null)
    {
View Full Code Here

  private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_STAT/SSH_FXP_LSTAT...");
      debug.flush();
    }

    sendMessage(statMethod, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    if (debug != null)
    {
View Full Code Here

   */
  public String readLink(String path) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_READLINK...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_READLINK, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    if (debug != null)
    {
View Full Code Here

   */
  public void setstat(String path, SFTPv3FileAttributes attr) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    tw.writeBytes(createAttrs(attr));

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_SETSTAT...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_SETSTAT, req_id, tw.getBytes());

    expectStatusOKMessage(req_id);
  }
View Full Code Here

  {
    checkHandleValidAndOpen(handle);

    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
    tw.writeBytes(createAttrs(attr));

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_FSETSTAT...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_FSETSTAT, req_id, tw.getBytes());

    expectStatusOKMessage(req_id);
  }
View Full Code Here

    /* Either I am too stupid to understand the SFTP draft
     * or the OpenSSH guys changed the semantics of src and target.
     */

    TypesWriter tw = new TypesWriter();
    tw.writeString(target, charsetName);
    tw.writeString(src, charsetName);

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_SYMLINK...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_SYMLINK, req_id, tw.getBytes());

    expectStatusOKMessage(req_id);
  }
View Full Code Here

   */
  public String canonicalPath(String path) throws IOException
  {
    int req_id = generateNextRequestID();

    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);

    if (debug != null)
    {
      debug.println("Sending SSH_FXP_REALPATH...");
      debug.flush();
    }

    sendMessage(Packet.SSH_FXP_REALPATH, req_id, tw.getBytes());

    byte[] resp = receiveMessage(34000);

    if (debug != null)
    {
View Full Code Here

  }

  public File transferCommandScript(File commandFile, boolean isWindows) throws Exception {
    try {
      SFTPv3Client sftpClient = new SFTPv3Client(this.getSshConnection());
      SFTPv3FileAttributes attr = new SFTPv3FileAttributes();
      attr.permissions = new Integer(0700);

      boolean exists = true;
      // check if File already exists
      while (exists) {
        try {
          SFTPv3FileAttributes attribs = sftpClient.stat(commandFile.getName());
        }
        catch (SFTPException e) {
          getLogger().debug9("Error code when checking file existence: " + e.getServerErrorCode());
          exists = false;
        }
        if (exists) {
          getLogger().debug1("file with that name already exists, trying new name...");
          String suffix = (isWindows ? ".cmd" : ".sh");
          File resultFile = File.createTempFile("sos", suffix);
          resultFile.delete();
          commandFile.renameTo(resultFile);
          commandFile = resultFile;
        }
      }

      // set execute permissions for owner
      SFTPv3FileHandle fileHandle = sftpClient.createFileTruncate(commandFile.getName(), attr);

      FileInputStream fis = null;
      long offset = 0;
      try {
        fis = new FileInputStream(commandFile);
        byte[] buffer = new byte[1024];
        while (true) {
          int len = fis.read(buffer, 0, buffer.length);
          if (len <= 0)
            break;
          sftpClient.write(fileHandle, offset, buffer, 0, len);
          offset += len;
        }
        fis.close();
        fis = null;

      }
      catch (Exception e) {
        throw new Exception("error occurred writing file [" + commandFile.getName() + "]: " + e.getMessage());
      }
      finally {
        if (fis != null)
          try {
            fis.close();
            fis = null;
          }
          catch (Exception ex) {
          } // gracefully ignore this error
      }
      sftpClient.closeFile(fileHandle);

      fileHandle = null;
      sftpClient.close();
      return commandFile;
    }
    catch (Exception e) {
      throw new Exception("Error transferring command script: " + e, e);
    }
View Full Code Here

TOP

Related Classes of ch.ethz.ssh2.SFTPv3Client

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.