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

The following examples show how to use com.jcraft.jsch.ChannelSftp.LsEntry#getAttrs() . 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: 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 2
Source File: PooledSFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
public SFTPInfo(LsEntry file, String name, URI parent, URI self) throws UnsupportedEncodingException {
	this.file = file;
	this.parent = parent;
	this.name = name;
	this.attrs = file.getAttrs();
	if (file.getAttrs().isDir() && !name.endsWith(URIUtils.PATH_SEPARATOR)) {
		name = name + URIUtils.PATH_SEPARATOR;
	}
	if (self != null) {
		this.uri = self;
	} else {
		this.uri = URIUtils.getChildURI(parent, name);
	}
}
 
Example 3
Source File: SftpSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public StatInfo[] call() throws IOException, CancellationException, JSchException, ExecutionException, InterruptedException, SftpException {
    if (!path.startsWith("/")) { //NOI18N
        throw new FileNotFoundException("Path is not absolute: " + path); //NOI18N
    }
    List<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 lsLoadID = RemoteStatistics.startChannelActivity("lsload", path); // NOI18N
            try {
                List<LsEntry> entries = (List<LsEntry>) cftp.ls(path);
                result = new ArrayList<>(Math.max(1, entries.size() - 2));
                for (LsEntry entry : entries) {
                    String name = entry.getFilename();
                    if (!".".equals(name) && !"..".equals(name)) { //NOI18N
                        SftpATTRS attrs = entry.getAttrs();
                        //if (!(attrs.isDir() || attrs.isLink())) {
                        //    if ( (attrs.getPermissions() & S_IFMT) != S_IFREG) {
                        //        // skip not regular files
                        //        continue;
                        //    }
                        //}
                        result.add(createStatInfo(path, name, 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(lsLoadID);
                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 == null ? new StatInfo[0] : result.toArray(new StatInfo[result.size()]);
}