Java Code Examples for com.jcraft.jsch.ChannelSftp#stat()

The following examples show how to use com.jcraft.jsch.ChannelSftp#stat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SftpBuildProcessAdapter.java    From teamcity-deployer-plugin with Apache License 2.0 6 votes vote down vote up
private void createRemotePath(@NotNull final ChannelSftp channel,
                              @NotNull final String destination) throws SftpException {
  final int endIndex = destination.lastIndexOf('/');
  if (endIndex > 0) {
    createRemotePath(channel, destination.substring(0, endIndex));
  }
  try {
    channel.stat(destination);
  } catch (SftpException e) {
    // dir does not exist.
    if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
      channel.mkdir(destination);
    }
  }

}
 
Example 2
Source File: SFtpUInfoTask.java    From Aria with Apache License 2.0 5 votes vote down vote up
@Override protected void getFileInfo(Session session)
    throws JSchException, UnsupportedEncodingException, SftpException {
  SFtpTaskOption option = (SFtpTaskOption) getWrapper().getTaskOption();
  ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
  channel.connect(1000);

  String remotePath = option.getUrlEntity().remotePath;
  String temp = CommonUtil.convertSFtpChar(getOption().getCharSet(), remotePath)
      + "/"
      + getWrapper().getEntity().getFileName();

  SftpATTRS attr = null;
  try {
    attr = channel.stat(temp);
  } catch (Exception e) {
    ALog.d(TAG, String.format("文件不存在,remotePath:%s", remotePath));
  }

  boolean isComplete = false;
  UploadEntity entity = getWrapper().getEntity();
  if (attr != null && attr.getSize() == entity.getFileSize()) {
    isComplete = true;
  }

  CompleteInfo info = new CompleteInfo();
  info.code = isComplete ? ISCOMPLETE : 200;
  info.obj = attr;
  channel.disconnect();
  callback.onSucceed(getWrapper().getKey(), info);
}
 
Example 3
Source File: SFtpDInfoTask.java    From Aria with Apache License 2.0 5 votes vote down vote up
@Override protected void getFileInfo(Session session) throws JSchException,
    UnsupportedEncodingException, SftpException {
  SFtpTaskOption option = (SFtpTaskOption) getWrapper().getTaskOption();
  ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
  channel.connect(1000);

  //channel.setFilenameEncoding(option.getCharSet());
  //channel.setFilenameEncoding("gbk");

  String remotePath = option.getUrlEntity().remotePath;
  String temp = CommonUtil.convertSFtpChar(option.getCharSet(), remotePath);
  SftpATTRS attr = null;
  try {
    attr = channel.stat(temp);
  } catch (Exception e) {
    ALog.e(TAG, String.format("文件不存在,remotePath:%s", remotePath));
  }

  if (attr != null) {
    getWrapper().getEntity().setFileSize(attr.getSize());
    CompleteInfo info = new CompleteInfo();
    info.code = 200;
    callback.onSucceed(getWrapper().getKey(), info);
  } else {
    callback.onFail(getWrapper().getEntity(),
        new AriaSFTPException(String.format("文件不存在,remotePath:%s", remotePath)), false);
  }
  channel.disconnect();
}
 
Example 4
Source File: SFTPRepository.java    From ant-ivy with Apache License 2.0 5 votes vote down vote up
private void mkdirs(String directory, ChannelSftp c) throws SftpException {
    try {
        SftpATTRS att = c.stat(directory);
        if (att != null && att.isDir()) {
            return;
        }
    } catch (SftpException ex) {
        if (directory.indexOf('/') != -1) {
            mkdirs(directory.substring(0, directory.lastIndexOf('/')), c);
        }
        c.mkdir(directory);
    }
}
 
Example 5
Source File: SftpLightWeightFileSystem.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Override
public FileStatus getFileStatus(Path path) throws IOException {
  ChannelSftp channelSftp = null;
  ChannelExec channelExec1 = null;
  ChannelExec channelExec2 = null;
  try {
    channelSftp = this.fsHelper.getSftpChannel();
    SftpATTRS sftpAttrs = channelSftp.stat(HadoopUtils.toUriPath(path));
    FsPermission permission = new FsPermission((short) sftpAttrs.getPermissions());

    channelExec1 = this.fsHelper.getExecChannel("id " + sftpAttrs.getUId());
    String userName = IOUtils.toString(channelExec1.getInputStream());

    channelExec2 = this.fsHelper.getExecChannel("id " + sftpAttrs.getGId());
    String groupName = IOUtils.toString(channelExec2.getInputStream());

    FileStatus fs =
        new FileStatus(sftpAttrs.getSize(), sftpAttrs.isDir(), 1, 0l, sftpAttrs.getMTime(), sftpAttrs.getATime(),
            permission, StringUtils.trimToEmpty(userName), StringUtils.trimToEmpty(groupName), path);

    return fs;
  } catch (SftpException e) {
    throw new IOException(e);
  } finally {
    safeDisconnect(channelSftp);
    safeDisconnect(channelExec1);
    safeDisconnect(channelExec2);
  }

}
 
Example 6
Source File: SftpSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public StatInfo call() throws IOException, CancellationException, JSchException, ExecutionException, InterruptedException, SftpException {
    if (!path.startsWith("/")) { //NOI18N
        throw new FileNotFoundException("Path is not absolute: " + path); //NOI18N
    }
    StatInfo result = null;
    SftpException exception = null;
    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} started", getTraceName());
    }
    String threadName = Thread.currentThread().getName();
    Thread.currentThread().setName(PREFIX + ": " + getTraceName()); // NOI18N
    int attempt = 1;
    try {
        for (; attempt <= LS_RETRY_COUNT; attempt++) {
            ChannelSftp cftp = getChannel();
            RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("statload", path); // NOI18N
            try {
                SftpATTRS attrs = lstat ? cftp.lstat(path) : cftp.stat(path);
                String dirName, baseName;
                int slashPos = path.lastIndexOf('/');
                if (slashPos == 0) {
                    dirName = "";
                    baseName = path.substring(1);
                } else {
                    dirName = path.substring(0, slashPos);
                    baseName = path.substring(slashPos + 1);
                }
                result = createStatInfo(dirName, baseName, attrs, cftp);
                exception = null;
                break;
            } catch (SftpException e) {
                exception = e;
                if (e.id == SftpIOException.SSH_FX_FAILURE) {
                    if (LOG.isLoggable(Level.FINE)) {
                        LOG.log(Level.FINE, "{0} - exception while attempt {1}", new Object[]{getTraceName(), attempt});
                    }
                    if (MiscUtils.mightBrokeSftpChannel(e)) {
                        cftp.quit();
                    }
                } else {
                    // re-try in case of failure only
                    // otherwise consider this exception as unrecoverable
                    break;
                }
            } finally {
                RemoteStatistics.stopChannelActivity(activityID);
                releaseChannel(cftp);
            }
        }
    } finally {
        Thread.currentThread().setName(threadName);
    }

    if (exception != null) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "{0} failed", getTraceName());
        }
        throw decorateSftpException(exception, path);
    }

    if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "{0} finished in {1} attempt(s)", new Object[]{getTraceName(), attempt});
    }
    return result;
}
 
Example 7
Source File: SFTPRepository.java    From ant-ivy with Apache License 2.0 3 votes vote down vote up
/**
 * Checks the existence for a remote file
 *
 * @param file
 *            to check
 * @param channel
 *            to use
 * @return true if file exists, false otherwise
 */
private boolean checkExistence(String file, ChannelSftp channel) {
    try {
        return channel.stat(file) != null;
    } catch (SftpException ex) {
        return false;
    }
}