Examples of SftpException


Examples of org.vngx.jsch.exception.SftpException

  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

Examples of org.vngx.jsch.exception.SftpException

    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

Examples of org.vngx.jsch.exception.SftpException

      }
      _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

Examples of org.vngx.jsch.exception.SftpException

    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

Examples of org.vngx.jsch.exception.SftpException

      }

      sendOPENR(srcb);
      readResponse();
      if( _header.type != SSH_FXP_HANDLE ) {
        throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
      }
      byte[] handle = _buffer.getString();         // handle

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

Examples of org.vngx.jsch.exception.SftpException

      }

      sendOPENDIR(Util.str2byte(dir, _fileEncoding));
      readResponse();
      if( _header.type != SSH_FXP_HANDLE ) {
        throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
      }

      byte[] handle = _buffer.getString();         // handle
      List<LsEntry> lsEntries = new ArrayList<LsEntry>();
      while( true ) {
        sendREADDIR(handle);
        readHeader();
        if( _header.type != SSH_FXP_STATUS && _header.type != SSH_FXP_NAME ) {
          throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
        } else if( _header.type == SSH_FXP_STATUS ) {
          fill(_buffer, _header.length);
          int i = _buffer.getInt();
          if( i == SSH_FX_EOF ) {
            break;
          }
          throwStatusError(_buffer, i);
        }

        _buffer.rewind();
        fill(_buffer.buffer, 0, 4);
        _header.length -= 4;
        int count = _buffer.getInt();
        _buffer.reset();

        while( count > 0 ) {
          if( _header.length > 0 ) {
            _buffer.shift();
            int j = (_buffer.buffer.length > (_buffer.index + _header.length)) ? _header.length : (_buffer.buffer.length - _buffer.index);
            int i = fill(_buffer.buffer, _buffer.index, j);
            _buffer.index += i;
            _header.length -= i;
          }
          byte[] bFilename = _buffer.getString();
          String sFilename = Util.byte2str(bFilename, _fileEncoding);
          byte[] bLongname = _serverVersion <= 3 ? _buffer.getString() : null;
          SftpATTRS attrs = SftpATTRS.getATTR(_buffer);

          boolean found = false;
          if( bPattern == null ) {
            found = true;
          } else if( !wildcardPattern ) {
            found = Arrays.equals(bPattern, bFilename);
          } else {
            found = Util.glob(bPattern, _utf8 ? bFilename : Util.str2byte(sFilename, UTF8));
          }

          if( found ) {
            // TODO: need to generate long name from attrs for sftp protocol 4(and later)
            String sLongname = bLongname == null ?
              (attrs.toString() + " " + sFilename) :
              Util.byte2str(bLongname, _fileEncoding);
            lsEntries.add(new LsEntry(sFilename, sLongname, attrs));
          }
          count--;
        }
      }
      _sendCLOSE(handle);
      return lsEntries;
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to ls path: "+path, e);
    }
  }
View Full Code Here

Examples of org.vngx.jsch.exception.SftpException

    }
  }

  public String readlink(String path) throws SftpException {
    if( _serverVersion < 3 ) {
      throw new SftpException(SSH_FX_OP_UNSUPPORTED, "The remote SFTP server is too old to support readlink operation");
    }
    try {
      path = isUnique(remoteAbsolutePath(path));

      sendREADLINK(Util.str2byte(path, _fileEncoding));
      readResponse();
      if( _header.type != SSH_FXP_NAME ) {
        throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
      }

      int count = _buffer.getInt();       // count
      byte[] filename = null;
      for( int i = 0; i < count; i++ ) {
        filename = _buffer.getString()// absolute path
        if( _serverVersion <= 3 ) {
          _buffer.getString();    // long filename
        }
        SftpATTRS.getATTR(_buffer);    // dummy attribute
      }
      return Util.byte2str(filename, _fileEncoding);
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to readlink path: "+path, e);
    }
  }
View Full Code Here

Examples of org.vngx.jsch.exception.SftpException

    }
  }

  public void symlink(String oldpath, String newpath) throws SftpException {
    if( _serverVersion < 3 ) {
      throw new SftpException(SSH_FX_OP_UNSUPPORTED, "The remote SFTP server is too old to support symlink operation");
    }
    try {
      oldpath = isUnique(remoteAbsolutePath(oldpath));
      newpath = remoteAbsolutePath(newpath);

      if( isPattern(newpath) ) {
        throw new SftpException(SSH_FX_FAILURE, "Failed to symlink, new path is invalid: "+newpath);
      }
      newpath = Util.unquote(newpath);

      sendSYMLINK(Util.str2byte(oldpath, _fileEncoding), Util.str2byte(newpath, _fileEncoding));
      readResponseOk();
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to symlink path: "+oldpath, e);
    }
  }
View Full Code Here

Examples of org.vngx.jsch.exception.SftpException

    }
  }

  public void rename(String oldpath, String newpath) throws SftpException {
    if( _serverVersion < 2 ) {
      throw new SftpException(SSH_FX_OP_UNSUPPORTED, "The remote SFTP server is too old to support rename operation");
    }
    try {
      oldpath = isUnique(remoteAbsolutePath(oldpath));
      newpath = remoteAbsolutePath(newpath);

      List<String> matches = globRemote(newpath);
      if( matches.size() >= 2 ) {
        throw new SftpException(SSH_FX_FAILURE, "Failed to rename path, found too many matches: "+matches);
      } else if( matches.size() == 1 ) {
        newpath = matches.get(0);
      } else {
        if( isPattern(newpath) ) {
          throw new SftpException(SSH_FX_FAILURE, "Failed to rename path, new path is invalid: "+newpath);
        }
        newpath = Util.unquote(newpath);
      }

      sendRENAME(Util.str2byte(oldpath, _fileEncoding), Util.str2byte(newpath, _fileEncoding));
      readResponseOk();
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to rename path: "+oldpath, e);
    }
  }
View Full Code Here

Examples of org.vngx.jsch.exception.SftpException

        readResponseOk();
      }
    } catch(SftpException e) {
      throw e;
    } catch(Exception e) {
      throw new SftpException(SSH_FX_FAILURE, "Failed to rm path: "+path, e);
    }
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.