Search in sources :

Example 6 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project cdap by caskdata.

the class SFTPFileSystem method listStatus.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
@SuppressWarnings("unchecked")
private FileStatus[] listStatus(ChannelSftp client, Path file) throws IOException {
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    FileStatus fileStat = getFileStatus(client, absolute);
    if (!fileStat.isDirectory()) {
        return new FileStatus[] { fileStat };
    }
    Vector<LsEntry> sftpFiles;
    try {
        sftpFiles = (Vector<LsEntry>) client.ls(absolute.toUri().getPath());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    ArrayList<FileStatus> fileStats = new ArrayList<FileStatus>();
    for (int i = 0; i < sftpFiles.size(); i++) {
        LsEntry entry = sftpFiles.get(i);
        String fname = entry.getFilename();
        // skip current and parent directory, ie. "." and ".."
        if (!".".equalsIgnoreCase(fname) && !"..".equalsIgnoreCase(fname)) {
            fileStats.add(getFileStatus(client, entry, absolute));
        }
    }
    return fileStats.toArray(new FileStatus[fileStats.size()]);
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 7 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project hadoop by apache.

the class SFTPFileSystem method listStatus.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
@SuppressWarnings("unchecked")
private FileStatus[] listStatus(ChannelSftp client, Path file) throws IOException {
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    FileStatus fileStat = getFileStatus(client, absolute);
    if (!fileStat.isDirectory()) {
        return new FileStatus[] { fileStat };
    }
    Vector<LsEntry> sftpFiles;
    try {
        sftpFiles = (Vector<LsEntry>) client.ls(absolute.toUri().getPath());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    ArrayList<FileStatus> fileStats = new ArrayList<FileStatus>();
    for (int i = 0; i < sftpFiles.size(); i++) {
        LsEntry entry = sftpFiles.get(i);
        String fname = entry.getFilename();
        // skip current and parent directory, ie. "." and ".."
        if (!".".equalsIgnoreCase(fname) && !"..".equalsIgnoreCase(fname)) {
            fileStats.add(getFileStatus(client, entry, absolute));
        }
    }
    return fileStats.toArray(new FileStatus[fileStats.size()]);
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 8 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project DataX by alibaba.

the class SftpHelper method getListFiles.

@Override
public HashSet<String> getListFiles(String directoryPath, int parentLevel, int maxTraversalLevel) {
    if (parentLevel < maxTraversalLevel) {
        // 父级目录,以'/'结尾
        String parentPath = null;
        int pathLen = directoryPath.length();
        if (directoryPath.contains("*") || directoryPath.contains("?")) {
            //*和?的限制
            // path是正则表达式
            String subPath = UnstructuredStorageReaderUtil.getRegexPathParentPath(directoryPath);
            if (isDirExist(subPath)) {
                parentPath = subPath;
            } else {
                String message = String.format("不能进入目录:[%s]," + "请确认您的配置项path:[%s]存在,且配置的用户有权限进入", subPath, directoryPath);
                LOG.error(message);
                throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
            }
        } else if (isDirExist(directoryPath)) {
            // path是目录
            if (directoryPath.charAt(pathLen - 1) == IOUtils.DIR_SEPARATOR) {
                parentPath = directoryPath;
            } else {
                parentPath = directoryPath + IOUtils.DIR_SEPARATOR;
            }
        } else if (isSymbolicLink(directoryPath)) {
            //path是链接文件
            String message = String.format("文件:[%s]是链接文件,当前不支持链接文件的读取", directoryPath);
            LOG.error(message);
            throw DataXException.asDataXException(FtpReaderErrorCode.LINK_FILE, message);
        } else if (isFileExist(directoryPath)) {
            // path指向具体文件
            sourceFiles.add(directoryPath);
            return sourceFiles;
        } else {
            String message = String.format("请确认您的配置项path:[%s]存在,且配置的用户有权限读取", directoryPath);
            LOG.error(message);
            throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
        }
        try {
            Vector vector = channelSftp.ls(directoryPath);
            for (int i = 0; i < vector.size(); i++) {
                LsEntry le = (LsEntry) vector.get(i);
                String strName = le.getFilename();
                String filePath = parentPath + strName;
                if (isDirExist(filePath)) {
                    // 是子目录
                    if (!(strName.equals(".") || strName.equals(".."))) {
                        //递归处理
                        getListFiles(filePath, parentLevel + 1, maxTraversalLevel);
                    }
                } else if (isSymbolicLink(filePath)) {
                    //是链接文件
                    String message = String.format("文件:[%s]是链接文件,当前不支持链接文件的读取", filePath);
                    LOG.error(message);
                    throw DataXException.asDataXException(FtpReaderErrorCode.LINK_FILE, message);
                } else if (isFileExist(filePath)) {
                    // 是文件
                    sourceFiles.add(filePath);
                } else {
                    String message = String.format("请确认path:[%s]存在,且配置的用户有权限读取", filePath);
                    LOG.error(message);
                    throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
                }
            }
        // end for vector
        } catch (SftpException e) {
            String message = String.format("获取path:[%s] 下文件列表时发生I/O异常,请确认与ftp服务器的连接正常", directoryPath);
            LOG.error(message);
            throw DataXException.asDataXException(FtpReaderErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
        }
        return sourceFiles;
    } else {
        //超出最大递归层数
        String message = String.format("获取path:[%s] 下文件列表时超出最大层数,请确认路径[%s]下不存在软连接文件", directoryPath, directoryPath);
        LOG.error(message);
        throw DataXException.asDataXException(FtpReaderErrorCode.OUT_MAX_DIRECTORY_LEVEL, message);
    }
}
Also used : SftpException(com.jcraft.jsch.SftpException) Vector(java.util.Vector) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 9 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project DataX by alibaba.

the class SftpHelperImpl method getAllFilesInDir.

@Override
public Set<String> getAllFilesInDir(String dir, String prefixFileName) {
    Set<String> allFilesWithPointedPrefix = new HashSet<String>();
    try {
        this.printWorkingDirectory();
        @SuppressWarnings("rawtypes") Vector allFiles = this.channelSftp.ls(dir);
        LOG.debug(String.format("ls: %s", JSON.toJSONString(allFiles, SerializerFeature.UseSingleQuotes)));
        for (int i = 0; i < allFiles.size(); i++) {
            LsEntry le = (LsEntry) allFiles.get(i);
            String strName = le.getFilename();
            if (strName.startsWith(prefixFileName)) {
                allFilesWithPointedPrefix.add(strName);
            }
        }
    } catch (SftpException e) {
        String message = String.format("获取path:[%s] 下文件列表时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录ls权限, errorMessage:%s", dir, e.getMessage());
        LOG.error(message);
        throw DataXException.asDataXException(FtpWriterErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
    }
    return allFilesWithPointedPrefix;
}
Also used : SftpException(com.jcraft.jsch.SftpException) Vector(java.util.Vector) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) HashSet(java.util.HashSet)

Example 10 with LsEntry

use of com.jcraft.jsch.ChannelSftp.LsEntry in project ats-framework by Axway.

the class JschSftpClient method isDirectoryEmpty.

/**
     *
     * @param directoryPath directory path
     * @return <code>true</code> if the directory is empty
     */
public boolean isDirectoryEmpty(String directoryPath) {
    try {
        List<LsEntry> entries = new ArrayList<LsEntry>();
        channel.ls(directoryPath, new LsEntrySelector(entries));
        return entries.size() == 0;
    } catch (Exception e) {
        throw new JschSftpClientException(e.getMessage(), e);
    }
}
Also used : JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException) ArrayList(java.util.ArrayList) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) JschSftpClientException(com.axway.ats.core.ssh.exceptions.JschSftpClientException)

Aggregations

LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)12 SftpException (com.jcraft.jsch.SftpException)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)4 FileStatus (org.apache.hadoop.fs.FileStatus)4 Path (org.apache.hadoop.fs.Path)4 JschSftpClientException (com.axway.ats.core.ssh.exceptions.JschSftpClientException)2 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 FileNotFoundException (java.io.FileNotFoundException)2 Vector (java.util.Vector)2 Matcher (java.util.regex.Matcher)2 Channel (com.jcraft.jsch.Channel)1 LsEntrySelector (com.jcraft.jsch.ChannelSftp.LsEntrySelector)1 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 SftpATTRS (com.jcraft.jsch.SftpATTRS)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1