Search in sources :

Example 61 with SftpException

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

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 62 with SftpException

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

the class SFTPFileSystem method create.

/**
   * A stream obtained via this call must be closed before using other APIs of
   * this class or else the invocation will block.
   */
@Override
public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
    final ChannelSftp client = connect();
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, f);
    if (exists(client, f)) {
        if (overwrite) {
            delete(client, f, false);
        } else {
            disconnect(client);
            throw new IOException(String.format(E_FILE_EXIST, f));
        }
    }
    Path parent = absolute.getParent();
    if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
        parent = (parent == null) ? new Path("/") : parent;
        disconnect(client);
        throw new IOException(String.format(E_CREATE_DIR, parent));
    }
    OutputStream os;
    try {
        client.cd(parent.toUri().getPath());
        os = client.put(f.getName());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    FSDataOutputStream fos = new FSDataOutputStream(os, statistics) {

        @Override
        public void close() throws IOException {
            super.close();
            disconnect(client);
        }
    };
    return fos;
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) OutputStream(java.io.OutputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream)

Example 63 with SftpException

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

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 64 with SftpException

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

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 65 with SftpException

use of com.jcraft.jsch.SftpException in project GNS by MobilityFirst.

the class SFTPUpload method localFileNewer.

/**
   *
   * @param user
   * @param host
   * @param keyFile
   * @param fileToTransfer
   * @param sftpWorkingDirectory
   * @return true if the local file is newer
   */
public static boolean localFileNewer(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
    if (verbose) {
        System.out.println("Local File Newer Check " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
    }
    try {
        ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
        Path paths = Paths.get(fileToTransfer);
        String localDir = paths.getParent().toString();
        channelSftp.cd(sftpWorkingDirectory);
        channelSftp.lcd(localDir);
        SftpATTRS remoteAttributes = channelSftp.stat(paths.getFileName().toString());
        long localTime = new File(fileToTransfer).lastModified();
        long remoteTime = remoteAttributes.getMTime() * 1000L;
        if (verbose) {
            System.out.println("L: " + localDir + " R: " + sftpWorkingDirectory + "\n" + "Local time = " + localTime + " Remote time = " + remoteTime);
        }
        if (verbose) {
            System.out.println("Result " + (localTime > remoteTime));
        }
        return localTime > remoteTime;
    } catch (JSchException | SftpException e) {
        System.out.println("Exception while checking for file newer:" + e);
        return false;
    }
}
Also used : Path(java.nio.file.Path) JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException) File(java.io.File)

Aggregations

SftpException (com.jcraft.jsch.SftpException)113 ChannelSftp (com.jcraft.jsch.ChannelSftp)67 IOException (java.io.IOException)49 JSchException (com.jcraft.jsch.JSchException)28 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)24 File (java.io.File)24 SftpATTRS (com.jcraft.jsch.SftpATTRS)16 Path (org.apache.hadoop.fs.Path)15 Session (com.jcraft.jsch.Session)13 OutputStream (java.io.OutputStream)11 JSch (com.jcraft.jsch.JSch)10 FileNotFoundException (java.io.FileNotFoundException)10 InputStream (java.io.InputStream)10 Channel (com.jcraft.jsch.Channel)9 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)9 FileStatus (org.apache.hadoop.fs.FileStatus)9 ArrayList (java.util.ArrayList)8 Vector (java.util.Vector)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 Test (org.junit.Test)8