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

The following examples show how to use com.jcraft.jsch.SftpATTRS#isDir() . 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: SftpClient.java    From hop with Apache License 2.0 6 votes vote down vote up
public FileType getFileType( String filename ) throws HopWorkflowException {
  try {
    SftpATTRS attrs = c.stat( filename );
    if ( attrs == null ) {
      return FileType.IMAGINARY;
    }

    if ( ( attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS ) == 0 ) {
      throw new HopWorkflowException( "Unknown permissions error" );
    }

    if ( attrs.isDir() ) {
      return FileType.FOLDER;
    } else {
      return FileType.FILE;
    }
  } catch ( Exception e ) {
    throw new HopWorkflowException( e );
  }
}
 
Example 2
Source File: SftpClient.java    From hop with Apache License 2.0 6 votes vote down vote up
public boolean folderExists( String foldername ) {
  boolean retval = false;
  try {
    SftpATTRS attrs = c.stat( foldername );
    if ( attrs == null ) {
      return false;
    }

    if ( ( attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS ) == 0 ) {
      throw new HopWorkflowException( "Unknown permissions error" );
    }

    retval = attrs.isDir();
  } catch ( Exception e ) {
    // Folder can not be found!
  }
  return retval;
}
 
Example 3
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 4
Source File: SFTPUtil.java    From xnx3 with Apache License 2.0 6 votes vote down vote up
private List<FileBean> _buildFiles(Vector ls,String dir) throws Exception {  
    if (ls != null && ls.size() >= 0) {  
        List<FileBean> list = new ArrayList<FileBean>();
        for (int i = 0; i < ls.size(); i++) {  
            LsEntry f = (LsEntry) ls.get(i);  
            String nm = f.getFilename();  
            
            if (nm.equals(".") || nm.equals(".."))  
                continue;  
            SftpATTRS attr = f.getAttrs();  
            FileBean fileBean=new FileBean();
            if (attr.isDir()) {  
                fileBean.setDir(true);
            } else {  
                fileBean.setDir(false);
            }  
            fileBean.setAttrs(attr);
            fileBean.setFilePath(dir);
            fileBean.setFileName(nm);
            list.add(fileBean);  
        }  
        return list;  
    }  
    return null;  
}
 
Example 5
Source File: SftpConnection.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
     * Determines whether a directory exists or not
     * 如果是文件,也会抛出异常,返回 false
     *
     * @param remoteDirectoryPath
     * @return true if exists, false otherwise
     * @throws IOException thrown if any I/O error occurred.
     */
    @Override
    public boolean existsDirectory(String remoteDirectoryPath) {

        try {
            // System.out.println(channel.realpath(remoteFilePath));
            SftpATTRS attrs = channel.stat(remoteDirectoryPath);
            return attrs.isDir();
        } catch (SftpException e) {
            // e.printStackTrace();
            return false;
        }


//        String originalWorkingDirectory = getWorkingDirectory();
//        try {
//            changeDirectory(remoteDirectoryPath);
//        } catch (FtpException e) {
//            //文件夹不存在,会抛出异常。
//            return false;
//        }
//        //恢复 ftpClient 当前工作目录属性
//        changeDirectory(originalWorkingDirectory);
//        return true;
    }
 
Example 6
Source File: SFTPFileSystem.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("resource")
private SFTPAttributesAndOutputStreamPair newOutputStream(Channel channel, SFTPPath path, boolean requireAttributes, OpenOptions options)
        throws IOException {

    // retrieve the attributes unless create is true and createNew is false, because then the file can be created
    SftpATTRS attributes = null;
    if (!options.create || options.createNew) {
        attributes = findAttributes(channel, path, false);
        if (attributes != null && attributes.isDir()) {
            throw Messages.fileSystemProvider().isDirectory(path.path());
        }
        if (!options.createNew && attributes == null) {
            throw new NoSuchFileException(path.path());
        } else if (options.createNew && attributes != null) {
            throw new FileAlreadyExistsException(path.path());
        }
    }
    // else the file can be created if necessary

    if (attributes == null && requireAttributes) {
        attributes = findAttributes(channel, path, false);
    }

    OutputStream out = channel.newOutputStream(path.path(), options);
    return new SFTPAttributesAndOutputStreamPair(attributes, out);
}
 
Example 7
Source File: SFTPFileSystem.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
DirectoryStream<Path> newDirectoryStream(final SFTPPath path, Filter<? super Path> filter) throws IOException {
    try (Channel channel = channelPool.get()) {
        List<LsEntry> entries = channel.listFiles(path.path());
        boolean isDirectory = false;
        for (Iterator<LsEntry> i = entries.iterator(); i.hasNext(); ) {
            LsEntry entry = i.next();
            String filename = entry.getFilename();
            if (CURRENT_DIR.equals(filename)) {
                isDirectory = true;
                i.remove();
            } else if (PARENT_DIR.equals(filename)) {
                i.remove();
            }
        }

        if (!isDirectory) {
            // https://github.com/robtimus/sftp-fs/issues/4: don't fail immediately but check the attributes
            // Follow links to ensure the directory attribute can be read correctly
            SftpATTRS attributes = channel.readAttributes(path.path(), true);
            if (!attributes.isDir()) {
                throw new NotDirectoryException(path.path());
            }
        }
        return new SFTPPathDirectoryStream(path, entries, filter);
    }
}
 
Example 8
Source File: SFTPFileSystem.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
private void copyAcrossFileSystems(Channel sourceChannel, SFTPPath source, SftpATTRS sourceAttributes, SFTPPath target, CopyOptions options)
        throws IOException {

    @SuppressWarnings("resource")
    SFTPFileSystem targetFileSystem = target.getFileSystem();
    try (Channel targetChannel = targetFileSystem.channelPool.getOrCreate()) {

        SftpATTRS targetAttributes = findAttributes(targetChannel, target, false);

        if (targetAttributes != null) {
            if (options.replaceExisting) {
                targetChannel.delete(target.path(), targetAttributes.isDir());
            } else {
                throw new FileAlreadyExistsException(target.path());
            }
        }

        if (sourceAttributes.isDir()) {
            targetChannel.mkdir(target.path());
        } else {
            copyFile(sourceChannel, source, targetChannel, target, options);
        }
    }
}
 
Example 9
Source File: SFTPClient.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public FileType getFileType( String filename ) throws KettleJobException {
  try {
    SftpATTRS attrs = c.stat( filename );
    if ( attrs == null ) {
      return FileType.IMAGINARY;
    }

    if ( ( attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS ) == 0 ) {
      throw new KettleJobException( "Unknown permissions error" );
    }

    if ( attrs.isDir() ) {
      return FileType.FOLDER;
    } else {
      return FileType.FILE;
    }
  } catch ( Exception e ) {
    throw new KettleJobException( e );
  }
}
 
Example 10
Source File: SFTPClient.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public boolean folderExists( String foldername ) {
  boolean retval = false;
  try {
    SftpATTRS attrs = c.stat( foldername );
    if ( attrs == null ) {
      return false;
    }

    if ( ( attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS ) == 0 ) {
      throw new KettleJobException( "Unknown permissions error" );
    }

    retval = attrs.isDir();
  } catch ( Exception e ) {
    // Folder can not be found!
  }
  return retval;
}
 
Example 11
Source File: SFTPFileSystem.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
void delete(SFTPPath path) throws IOException {
    try (Channel channel = channelPool.get()) {
        SftpATTRS attributes = getAttributes(channel, path, false);
        boolean isDirectory = attributes.isDir();
        channel.delete(path.path(), isDirectory);
    }
}
 
Example 12
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 13
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);
  }

}