Java Code Examples for com.jcraft.jsch.SftpATTRS#getSize()

The following examples show how to use com.jcraft.jsch.SftpATTRS#getSize() . 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: SftpSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private StatInfo createStatInfo(String dirName, String baseName, SftpATTRS attrs, ChannelSftp cftp) throws SftpException {
    String linkTarget = null;
    if (attrs.isLink()) {
        String path = dirName + '/' + baseName;
        RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("readlink", path); // NOI18N
        try {
            if (LOG.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "performing readlink {0}", path);
            }
            linkTarget = cftp.readlink(path);
        } finally {
            RemoteStatistics.stopChannelActivity(activityID);
        }
    }
    Date lastModified = new Date(attrs.getMTime() * 1000L);
    StatInfo result = new FileInfoProvider.StatInfo(baseName, attrs.getUId(), attrs.getGId(), attrs.getSize(),
            attrs.isDir(), attrs.isLink(), linkTarget, attrs.getPermissions(), lastModified);
    return result;
}
 
Example 2
Source File: SFTPFileSystem.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("resource")
SeekableByteChannel newByteChannel(SFTPPath path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
    if (attrs.length > 0) {
        throw Messages.fileSystemProvider().unsupportedCreateFileAttribute(attrs[0].name());
    }

    OpenOptions openOptions = OpenOptions.forNewByteChannel(options);

    try (Channel channel = channelPool.get()) {
        if (openOptions.read) {
            // use findAttributes instead of getAttributes, to let the opening of the stream provide the correct error message
            SftpATTRS attributes = findAttributes(channel, path, false);
            InputStream in = newInputStream(channel, path, openOptions);
            long size = attributes == null ? 0 : attributes.getSize();
            return FileSystemProviderSupport.createSeekableByteChannel(in, size);
        }

        // if append then we need the attributes, to find the initial position of the channel
        boolean requireAttributes = openOptions.append;
        SFTPAttributesAndOutputStreamPair outPair = newOutputStream(channel, path, requireAttributes, openOptions);
        long initialPosition = outPair.attributes == null ? 0 : outPair.attributes.getSize();
        return FileSystemProviderSupport.createSeekableByteChannel(outPair.out, initialPosition);
    }
}
 
Example 3
Source File: SFTPRepository.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
/**
 * This method is similar to getResource, except that the returned resource is fully initialized
 * (resolved in the sftp repository), and that the given string is a full remote path
 *
 * @param path
 *            the full remote path in the repository of the resource
 * @return a fully initialized resource, able to answer to all its methods without needing any
 *         further connection
 */
@SuppressWarnings("unchecked")
public Resource resolveResource(String path) {
    try {
        List<LsEntry> r = getSftpChannel(path).ls(getPath(path));
        if (r != null) {
            SftpATTRS attrs = r.get(0).getAttrs();
            return new BasicResource(path, true, attrs.getSize(),
                        attrs.getMTime() * MILLIS_PER_SECOND, false);
        }
    } catch (Exception e) {
        Message.debug("Error while resolving resource " + path, e);
        // silent fail, return nonexistent resource
    }

    return new BasicResource(path, false, 0, 0, false);
}
 
Example 4
Source File: SftpResourceAccessor.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ExternalResourceMetaData toMetaData(URI uri, SftpATTRS attributes) {
    long lastModified = -1;
    long contentLength = -1;

    if ((attributes.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) != 0) {
        lastModified = attributes.getMTime() * 1000;
    }
    if ((attributes.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) != 0) {
        contentLength = attributes.getSize();
    }

    return new DefaultExternalResourceMetaData(uri, lastModified, contentLength, null, null);
}
 
Example 5
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 6
Source File: SftpResourceAccessor.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ExternalResourceMetaData toMetaData(URI uri, SftpATTRS attributes) {
    long lastModified = -1;
    long contentLength = -1;

    if ((attributes.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) != 0) {
        lastModified = attributes.getMTime() * 1000;
    }
    if ((attributes.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) != 0) {
        contentLength = attributes.getSize();
    }

    return new DefaultExternalResourceMetaData(uri, lastModified, contentLength, null, null);
}
 
Example 7
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);
  }

}