Search in sources :

Example 6 with SftpException

use of com.jcraft.jsch.SftpException in project hadoop by apache.

the class SFTPFileSystem method open.

@Override
public FSDataInputStream open(Path f, int bufferSize) throws IOException {
    ChannelSftp channel = connect();
    Path workDir;
    try {
        workDir = new Path(channel.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, f);
    FileStatus fileStat = getFileStatus(channel, absolute);
    if (fileStat.isDirectory()) {
        disconnect(channel);
        throw new IOException(String.format(E_PATH_DIR, f));
    }
    InputStream is;
    try {
        // the path could be a symbolic link, so get the real path
        absolute = new Path("/", channel.realpath(absolute.toUri().getPath()));
        is = channel.get(absolute.toUri().getPath());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    FSDataInputStream fis = new FSDataInputStream(new SFTPInputStream(is, channel, statistics));
    return fis;
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) FileStatus(org.apache.hadoop.fs.FileStatus) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException)

Example 7 with SftpException

use of com.jcraft.jsch.SftpException in project hadoop by apache.

the class SFTPFileSystem method getFileStatus.

/**
   * 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 getFileStatus(ChannelSftp client, Path file) throws IOException {
    FileStatus fileStat = null;
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    Path parentPath = absolute.getParent();
    if (parentPath == null) {
        // root directory
        // Length of root directory on server not known
        long length = -1;
        boolean isDir = true;
        int blockReplication = 1;
        // Block Size not known.
        long blockSize = DEFAULT_BLOCK_SIZE;
        // Modification time of root directory not known.
        long modTime = -1;
        Path root = new Path("/");
        return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this.getUri(), this.getWorkingDirectory()));
    }
    String pathName = parentPath.toUri().getPath();
    Vector<LsEntry> sftpFiles;
    try {
        sftpFiles = (Vector<LsEntry>) client.ls(pathName);
    } catch (SftpException e) {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    if (sftpFiles != null) {
        for (LsEntry sftpFile : sftpFiles) {
            if (sftpFile.getFilename().equals(file.getName())) {
                // file found in directory
                fileStat = getFileStatus(client, sftpFile, parentPath);
                break;
            }
        }
        if (fileStat == null) {
            throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
        }
    } else {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    return fileStat;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 8 with SftpException

use of com.jcraft.jsch.SftpException in project che by eclipse.

the class JschSshClient method copyFile.

private void copyFile(String sourcePath, String absoluteTargetPath, ChannelSftp channelSftp) throws MachineException {
    try {
        channelSftp.put(sourcePath, absoluteTargetPath);
        // apply permissions
        File file = new File(sourcePath);
        // read
        int permissions = 256;
        // execute
        if (file.canExecute()) {
            permissions += 64;
        }
        // write
        if (file.canWrite()) {
            permissions += 128;
        }
        channelSftp.chmod(permissions, absoluteTargetPath);
    } catch (SftpException e) {
        throw new MachineException(format("Sftp copying of file %s failed. Error: %s", absoluteTargetPath, e.getLocalizedMessage()));
    }
}
Also used : SftpException(com.jcraft.jsch.SftpException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) File(java.io.File)

Example 9 with SftpException

use of com.jcraft.jsch.SftpException in project DataX by alibaba.

the class SftpHelperImpl method mkDirRecursive.

@Override
public void mkDirRecursive(String directoryPath) {
    boolean isDirExist = false;
    try {
        this.printWorkingDirectory();
        SftpATTRS sftpATTRS = this.channelSftp.lstat(directoryPath);
        isDirExist = sftpATTRS.isDir();
    } catch (SftpException e) {
        if (e.getMessage().toLowerCase().equals("no such file")) {
            LOG.warn(String.format("您的配置项path:[%s]不存在,将尝试进行目录创建, errorMessage:%s", directoryPath, e.getMessage()), e);
            isDirExist = false;
        }
    }
    if (!isDirExist) {
        StringBuilder dirPath = new StringBuilder();
        dirPath.append(IOUtils.DIR_SEPARATOR_UNIX);
        String[] dirSplit = StringUtils.split(directoryPath, IOUtils.DIR_SEPARATOR_UNIX);
        try {
            // ftp server不支持递归创建目录,只能一级一级创建
            for (String dirName : dirSplit) {
                dirPath.append(dirName);
                mkDirSingleHierarchy(dirPath.toString());
                dirPath.append(IOUtils.DIR_SEPARATOR_UNIX);
            }
        } catch (SftpException e) {
            String message = String.format("创建目录:%s时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录创建权限, errorMessage:%s", directoryPath, e.getMessage());
            LOG.error(message, e);
            throw DataXException.asDataXException(FtpWriterErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
        }
    }
}
Also used : SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 10 with SftpException

use of com.jcraft.jsch.SftpException in project DataX by alibaba.

the class SftpHelperImpl method mkdir.

@Override
public void mkdir(String directoryPath) {
    boolean isDirExist = false;
    try {
        this.printWorkingDirectory();
        SftpATTRS sftpATTRS = this.channelSftp.lstat(directoryPath);
        isDirExist = sftpATTRS.isDir();
    } catch (SftpException e) {
        if (e.getMessage().toLowerCase().equals("no such file")) {
            LOG.warn(String.format("您的配置项path:[%s]不存在,将尝试进行目录创建, errorMessage:%s", directoryPath, e.getMessage()), e);
            isDirExist = false;
        }
    }
    if (!isDirExist) {
        try {
            // warn 检查mkdir -p
            this.channelSftp.mkdir(directoryPath);
        } catch (SftpException e) {
            String message = String.format("创建目录:%s时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录创建权限, errorMessage:%s", directoryPath, e.getMessage());
            LOG.error(message, e);
            throw DataXException.asDataXException(FtpWriterErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
        }
    }
}
Also used : SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Aggregations

SftpException (com.jcraft.jsch.SftpException)45 IOException (java.io.IOException)18 ChannelSftp (com.jcraft.jsch.ChannelSftp)17 Path (org.apache.hadoop.fs.Path)14 File (java.io.File)13 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)9 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)8 JSchException (com.jcraft.jsch.JSchException)8 FileStatus (org.apache.hadoop.fs.FileStatus)8 Session (com.jcraft.jsch.Session)7 FileNotFoundException (java.io.FileNotFoundException)7 SftpATTRS (com.jcraft.jsch.SftpATTRS)6 Channel (com.jcraft.jsch.Channel)5 OutputStream (java.io.OutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 Vector (java.util.Vector)4 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3