Search in sources :

Example 21 with SftpException

use of com.jcraft.jsch.SftpException 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 22 with SftpException

use of com.jcraft.jsch.SftpException in project cdap by caskdata.

the class SFTPFileSystem method mkdirs.

/**
   * 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.
   */
private boolean mkdirs(ChannelSftp client, Path file, FsPermission permission) throws IOException {
    boolean created = true;
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    String pathName = absolute.getName();
    if (!exists(client, absolute)) {
        Path parent = absolute.getParent();
        created = (parent == null || mkdirs(client, parent, FsPermission.getDefault()));
        if (created) {
            String parentDir = parent.toUri().getPath();
            boolean succeeded = true;
            try {
                client.cd(parentDir);
                client.mkdir(pathName);
            } catch (SftpException e) {
                throw new IOException(String.format(E_MAKE_DIR_FORPATH, pathName, parentDir));
            }
            created = created & succeeded;
        }
    } else if (isFile(client, absolute)) {
        throw new IOException(String.format(E_DIR_CREATE_FROMFILE, absolute));
    }
    return created;
}
Also used : Path(org.apache.hadoop.fs.Path) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException)

Example 23 with SftpException

use of com.jcraft.jsch.SftpException in project cdap by caskdata.

the class SFTPFileSystem method rename.

/**
   * 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.
   *
   * @param channel
   * @param src
   * @param dst
   * @return rename successful?
   * @throws IOException
   */
private boolean rename(ChannelSftp channel, Path src, Path dst) throws IOException {
    Path workDir;
    try {
        workDir = new Path(channel.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absoluteSrc = makeAbsolute(workDir, src);
    Path absoluteDst = makeAbsolute(workDir, dst);
    if (!exists(channel, absoluteSrc)) {
        throw new IOException(String.format(E_SPATH_NOTEXIST, src));
    }
    if (exists(channel, absoluteDst)) {
        throw new IOException(String.format(E_DPATH_EXIST, dst));
    }
    boolean renamed = true;
    try {
        channel.cd("/");
        channel.rename(src.toUri().getPath(), dst.toUri().getPath());
    } catch (SftpException e) {
        renamed = false;
    }
    return renamed;
}
Also used : Path(org.apache.hadoop.fs.Path) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException)

Example 24 with SftpException

use of com.jcraft.jsch.SftpException in project cdap by caskdata.

the class SFTPFileSystem method delete.

/**
   * 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.
   */
private boolean delete(ChannelSftp channel, Path file, boolean recursive) throws IOException {
    Path workDir;
    try {
        workDir = new Path(channel.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    String pathName = absolute.toUri().getPath();
    FileStatus fileStat = null;
    try {
        fileStat = getFileStatus(channel, absolute);
    } catch (FileNotFoundException e) {
        // file not found, no need to delete, return true
        return false;
    }
    if (!fileStat.isDirectory()) {
        boolean status = true;
        try {
            channel.rm(pathName);
        } catch (SftpException e) {
            status = false;
        }
        return status;
    } else {
        boolean status = true;
        FileStatus[] dirEntries = listStatus(channel, absolute);
        if (dirEntries != null && dirEntries.length > 0) {
            if (!recursive) {
                throw new IOException(String.format(E_DIR_NOTEMPTY, file));
            }
            for (int i = 0; i < dirEntries.length; ++i) {
                delete(channel, new Path(absolute, dirEntries[i].getPath()), recursive);
            }
        }
        try {
            channel.rmdir(pathName);
        } catch (SftpException e) {
            status = false;
        }
        return status;
    }
}
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)

Example 25 with SftpException

use of com.jcraft.jsch.SftpException 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)

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