Package org.vngx.jsch.exception

Examples of org.vngx.jsch.exception.SftpException


      sendINIT();

      // receive SSH_FXP_VERSION
      readHeader();
      if( _header.length > MAX_MSG_LENGTH ) {
        throw new SftpException(SSH_FX_FAILURE, "Received message is too long: " + _header.length);
      }
      _serverVersion = _header.rid;  // Retrieve version from header

      if( _header.length > 0 ) {
        _extensions = new HashMap<String,String>();
View Full Code Here


   * @throws SftpException if any errors occur or path is not valid
   */
  public void lcd(String path) throws SftpException {
    path = localAbsolutePath(path);
    if( !new File(path).isDirectory() ) {
      throw new SftpException(SSH_FX_NO_SUCH_FILE, "Failed to lcd, directory does not exist: "+path);
    }
    try {
      path = new File(path).getCanonicalPath();
    } catch(Exception e) { /* Ignore error. */ }
    _lcwd = path;
View Full Code Here

      path = isUnique(remoteAbsolutePath(path));

      byte[] realPath = _realpath(path);
      SftpATTRS attr = _stat(realPath);
      if( (attr.getFlags() & SSH_FILEXFER_ATTR_PERMISSIONS) == 0 ) {
        throw new SftpException(SSH_FX_FAILURE, "Failed to cd (permission denied): " + path);
      } else if( !attr.isDir() ) {
        throw new SftpException(SSH_FX_FAILURE, "Failed to cd (not a directory): " + path);
      }
      _cwd = Util.byte2str(realPath, _fileEncoding);
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to cd path: "+path, e);
    }
  }
View Full Code Here

    try {
      List<String> matches = globRemote(dst);
      if( matches.size() != 1 ) {
        if( matches.isEmpty() ) {
          if( isPattern(dst) ) {
            throw new SftpException(SSH_FX_FAILURE, "Invalid destination for put: "+dst);
          } else {
            dst = Util.unquote(dst);
          }
        }
        throw new SftpException(SSH_FX_FAILURE, "Destination is not unique: "+matches);
      } else {
        dst = matches.get(0);
      }

      boolean isRemoteDir = isRemoteDir(dst);
      matches = globLocal(src);

      StringBuffer dstsb = null;
      if( isRemoteDir ) {
        if( !dst.endsWith("/") ) {
          dst += "/";
        }
        dstsb = new StringBuffer(dst);
      } else if( matches.size() > 1 ) {
        throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but the destination is missing or a file.");
      }

      String _dst;
      for( String _src : matches ) {
        if( isRemoteDir ) {
          int i = _src.lastIndexOf(File.separatorChar);
          if( FS_IS_BS ) {
            int ii = _src.lastIndexOf('/');
            if( ii != -1 && ii > i ) {
              i = ii;
            }
          }
          if( i == -1 ) {
            dstsb.append(_src);
          } else {
            dstsb.append(_src.substring(i + 1));
          }
          _dst = dstsb.toString();
          dstsb.delete(dst.length(), _dst.length());
        } else {
          _dst = dst;
        }

        long sizeOfDest = 0;
        if( mode == RESUME ) {
          try {
            sizeOfDest = _stat(_dst).getSize();
          } catch(Exception eee) {
            // TODO Error handling?
          }
          long sizeOfSrc = new File(_src).length();
          if( sizeOfSrc < sizeOfDest ) {
            throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + _dst);
          } else if( sizeOfSrc == sizeOfDest ) {
            return;
          }
        }

        if( monitor != null ) {
          monitor.init(SftpProgressMonitor.PUT, _src, _dst, (new File(_src)).length());
          if( mode == RESUME ) {
            monitor.count(sizeOfDest);
          }
        }
        FileInputStream fis = null;
        try {
          _put(fis = new FileInputStream(_src), _dst, monitor, mode);
        } finally {
          if( fis != null ) {
            try { fis.close(); } catch(IOException ie) { /* Ignore error. */ }
          }
        }
      }
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to put: "+src, e);
    }
  }
View Full Code Here

      List<String> matches = globRemote(dst);
      if( matches.size() != 1 ) {
        if( matches.isEmpty() ) {
          if( isPattern(dst) ) {
            throw new SftpException(SSH_FX_FAILURE, "Invalid destination for put: "+dst);
          } else {
            dst = Util.unquote(dst);
          }
        }
        throw new SftpException(SSH_FX_FAILURE, "Destination is not unique: "+matches);
      } else {
        dst = matches.get(0);
      }
      if( isRemoteDir(dst) ) {
        throw new SftpException(SSH_FX_FAILURE, dst + " is a directory");
      }

      _put(src, dst, monitor, mode);
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to put: "+e, e);
    }
  }
View Full Code Here

        }
      }
      if( mode == RESUME && skip > 0 ) {
        long skipped = src.skip(skip);
        if( skipped < skip ) {
          throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + dst);
        }
      }

      if( mode == OVERWRITE ) {
        sendOPENW(dstb);
      } else {
        sendOPENA(dstb);
      }
      readResponse();
      if( _header.type != SSH_FXP_HANDLE ) {
        throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
      }

      byte[] handle = _buffer.getString();         // handle
      byte[] data = null;

      boolean dontcopy = true// WHA?!!
      if( !dontcopy ) {
        data = new byte[_buffer.buffer.length - (5 + 13 + 21 + handle.length + (32 + 20 /* padding and mac */))];
      }

      long offset = 0;
      if( mode == RESUME || mode == APPEND ) {
        offset += skip;
      }

      int startid = _seq;
      int ackid = _seq;
      int ackcount = 0;
      while( true ) {
        int nread = 0;
        int s = 0;
        int datalen = 0;
        int count = 0;

        if( !dontcopy ) {
          datalen = data.length - s;
        } else {
          data = _buffer.buffer;
          s = 5 + 13 + 21 + handle.length;
          datalen = _buffer.buffer.length - s
              - 32 - 20; // padding and mac
        }

        do {
          nread = src.read(data, s, datalen);
          if( nread > 0 ) {
            s += nread;
            datalen -= nread;
            count += nread;
          }
        } while( datalen > 0 && nread > 0 );
        if( count <= 0 ) {
          break;
        }

        int _i = count;
        while( _i > 0 ) {
          _i -= sendWRITE(handle, offset, data, 0, _i);
          if( (_seq - 1) == startid || _io_in.available() >= 1024 ) {
            while( _io_in.available() > 0 ) {
              ackid = readResponseOk();
              if( startid > ackid || ackid > _seq - 1 ) {
                if( ackid == _seq ) {
                  System.err.println("ack error: startid=" + startid + " seq=" + _seq + " _ackid=" + ackid);
                } else {
                  throw new SftpException(SSH_FX_FAILURE, "ack error: startid=" + startid + " seq=" + _seq + " _ackid=" + ackid);
                }
              }
              ackcount++;
            }
          }
        }
        offset += count;
        if( monitor != null && !monitor.count(count) ) {
          break;
        }
      }
      int _ackcount = _seq - startid;
      while( _ackcount > ackcount ) {
        readResponseOk();
        ackcount++;
      }
      if( monitor != null ) {
        monitor.end();
      }
      _sendCLOSE(handle);
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to put: "+dst, e);
    }
  }
View Full Code Here

  public OutputStream put(String dst, SftpProgressMonitor monitor, int mode, long offset) throws SftpException {
    try {
      dst = isUnique(remoteAbsolutePath(dst));
      if( isRemoteDir(dst) ) {
        throw new SftpException(SSH_FX_FAILURE, dst + " is a directory");
      }
      byte[] dstb = Util.str2byte(dst, _fileEncoding);

      long skip = 0;
      if( mode == RESUME || mode == APPEND ) {
        try {
          skip = _stat(dstb).getSize();
        } catch(Exception eee) {
          //System.err.println(eee);
        }
      }

      if( mode == OVERWRITE ) {
        sendOPENW(dstb);
      } else {
        sendOPENA(dstb);
      }

      readResponse();
      if( _header.type != SSH_FXP_HANDLE ) {
        throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
      }
      if( mode == RESUME || mode == APPEND ) {
        offset += skip;
      }
      byte[] handle = _buffer.getString();         // handle
      return new PutOutputStream(handle, offset, monitor);
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to put: "+dst, e);
    }
  }
View Full Code Here

    boolean dstExists = false, error = false;
    String _dst = null;
    try {
      List<String> matches = globRemote(src);
      if( matches.isEmpty() ) {
        throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such file: "+src);
      }

      File dstFile = new File(dst);
      boolean isDstDir = dstFile.isDirectory();
      StringBuffer dstsb = null;
      if( isDstDir ) {
        if( !dst.endsWith(File.separator) ) {
          dst += File.separator;
        }
        dstsb = new StringBuffer(dst);
      } else if( matches.size() > 1 ) {
        throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but destination is missing or a file.");
      }

      for( String _src : matches ) {
        SftpATTRS attr = _stat(_src);
        if( attr.isDir() ) {
          throw new SftpException(SSH_FX_FAILURE, "Not supported to get directory " + _src);
        }

        _dst = null;
        if( isDstDir ) {
          int i = _src.lastIndexOf('/');
          if( i == -1 ) {
            dstsb.append(_src);
          } else {
            dstsb.append(_src.substring(i + 1));
          }
          _dst = dstsb.toString();
          dstsb.delete(dst.length(), _dst.length());
        } else {
          _dst = dst;
        }

        File _dstFile = new File(_dst);
        if( mode == RESUME ) {
          long sizeOfSrc = attr.getSize();
          long sizeOfDst = _dstFile.length();
          if( sizeOfDst > sizeOfSrc ) {
            throw new SftpException(SSH_FX_FAILURE, "Failed to resume for " + _dst);
          } else if( sizeOfDst == sizeOfSrc ) {
            return// Nothing to resume, already have full file
          }
        }

        if( monitor != null ) {
          monitor.init(SftpProgressMonitor.GET, _src, _dst, attr.getSize());
          if( mode == RESUME ) {
            monitor.count(_dstFile.length());
          }
        }

        FileOutputStream fos = null;
        dstExists = _dstFile.exists();
        try {
          fos = new FileOutputStream(_dst, mode != OVERWRITE);
          _get(_src, fos, monitor, mode, new File(_dst).length());
        } finally {
          if( fos != null ) {
            try { fos.close(); } catch(IOException ie) { /* Ignore error. */ }
          }
        }
      }
    } catch(SftpException e) {
      error = true;
      throw e;
    } catch(Exception e) {
      error = true;
      throw new SftpException(SSH_FX_FAILURE, "Failed to get src: "+src, e);
    } finally {
      if( error && !dstExists && _dst != null ) {
        File _dstFile = new File(_dst);
        if( _dstFile.exists() && _dstFile.length() == 0 ) {
          _dstFile.delete();
View Full Code Here

      }
      _get(src, dst, monitor, mode, skip);
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to get src: "+src, e);
    }
  }
View Full Code Here

    byte[] srcb = Util.str2byte(src, _fileEncoding);
    try {
      sendOPENR(srcb);
      readResponse();
      if( _header.type != SSH_FXP_HANDLE ) {
        throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
      }

      byte[] handle = _buffer.getString();         // filename
      long offset = mode == RESUME ? skip : 0;
      int requestLen = 0;
      loop:
      while( true ) {
        requestLen = _buffer.buffer.length - 13;
        if( _serverVersion == 0 ) {
          requestLen = 1024;
        }
        sendREAD(handle, offset, requestLen);
        readHeader();

        if( _header.type == SSH_FXP_STATUS ) {
          fill(_buffer, _header.length);
          int i = _buffer.getInt();
          if( i == SSH_FX_EOF ) {
            break loop;
          }
          throwStatusError(_buffer, i);
        }
        if( _header.type != SSH_FXP_DATA ) {
          break loop;
        }

        _buffer.rewind();
        fill(_buffer.buffer, 0, 4);
        _header.length -= 4;
        int i = _buffer.getInt();   // length of data
        int foo = i;

        while( foo > 0 ) {
          int bar = foo > _buffer.buffer.length ? _buffer.buffer.length : foo;
          if( (i = _io_in.read(_buffer.buffer, 0, bar)) < 0 ) {
            break loop;
          }
          int bytesRead = i;
          dst.write(_buffer.buffer, 0, bytesRead);

          offset += bytesRead;
          foo -= bytesRead;

          if( monitor != null ) {
            if( !monitor.count(bytesRead) ) {
              while( foo > 0 ) {
                i = _io_in.read(_buffer.buffer, 0, (_buffer.buffer.length < foo ? _buffer.buffer.length : foo));
                if( i <= 0 ) {
                  break;
                }
                foo -= i;
              }
              break loop;
            }
          }

        }
      }
      dst.flush();

      if( monitor != null ) {
        monitor.end();
      }
      _sendCLOSE(handle);
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to get src: "+src, e);
    }
  }
View Full Code Here

TOP

Related Classes of org.vngx.jsch.exception.SftpException

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.