Search in sources :

Example 6 with SftpATTRS

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

the class SFTPFileSystem method getFileStatus.

/**
   * Convert the file information in LsEntry to a {@link FileStatus} object. *
   *
   * @param sftpFile
   * @param parentPath
   * @return file status
   * @throws IOException
   */
private FileStatus getFileStatus(ChannelSftp channel, LsEntry sftpFile, Path parentPath) throws IOException {
    SftpATTRS attr = sftpFile.getAttrs();
    long length = attr.getSize();
    boolean isDir = attr.isDir();
    boolean isLink = attr.isLink();
    if (isLink) {
        String link = parentPath.toUri().getPath() + "/" + sftpFile.getFilename();
        try {
            link = channel.realpath(link);
            Path linkParent = new Path("/", link);
            FileStatus fstat = getFileStatus(channel, linkParent);
            isDir = fstat.isDirectory();
            length = fstat.getLen();
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
    int blockReplication = 1;
    // Using default block size since there is no way in SFTP channel to know of
    // block sizes on server. The assumption could be less than ideal.
    long blockSize = DEFAULT_BLOCK_SIZE;
    // convert to milliseconds
    long modTime = attr.getMTime() * 1000;
    long accessTime = 0;
    FsPermission permission = getPermissions(sftpFile);
    // not be able to get the real user group name, just use the user and group
    // id
    String user = Integer.toString(attr.getUId());
    String group = Integer.toString(attr.getGId());
    Path filePath = new Path(parentPath, sftpFile.getFilename());
    return new FileStatus(length, isDir, blockReplication, blockSize, modTime, accessTime, permission, user, group, filePath.makeQualified(this.getUri(), this.getWorkingDirectory()));
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpATTRS(com.jcraft.jsch.SftpATTRS) IOException(java.io.IOException) FsPermission(org.apache.hadoop.fs.permission.FsPermission) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SftpException(com.jcraft.jsch.SftpException)

Example 7 with SftpATTRS

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

Example 8 with SftpATTRS

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

the class SFTPFileSystem method getFileStatus.

/**
   * Convert the file information in LsEntry to a {@link FileStatus} object. *
   *
   * @param sftpFile
   * @param parentPath
   * @return file status
   * @throws IOException
   */
private FileStatus getFileStatus(ChannelSftp channel, LsEntry sftpFile, Path parentPath) throws IOException {
    SftpATTRS attr = sftpFile.getAttrs();
    long length = attr.getSize();
    boolean isDir = attr.isDir();
    boolean isLink = attr.isLink();
    if (isLink) {
        String link = parentPath.toUri().getPath() + "/" + sftpFile.getFilename();
        try {
            link = channel.realpath(link);
            Path linkParent = new Path("/", link);
            FileStatus fstat = getFileStatus(channel, linkParent);
            isDir = fstat.isDirectory();
            length = fstat.getLen();
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
    int blockReplication = 1;
    // Using default block size since there is no way in SFTP channel to know of
    // block sizes on server. The assumption could be less than ideal.
    long blockSize = DEFAULT_BLOCK_SIZE;
    // convert to milliseconds
    long modTime = attr.getMTime() * 1000;
    long accessTime = 0;
    FsPermission permission = getPermissions(sftpFile);
    // not be able to get the real user group name, just use the user and group
    // id
    String user = Integer.toString(attr.getUId());
    String group = Integer.toString(attr.getGId());
    Path filePath = new Path(parentPath, sftpFile.getFilename());
    return new FileStatus(length, isDir, blockReplication, blockSize, modTime, accessTime, permission, user, group, filePath.makeQualified(this.getUri(), this.getWorkingDirectory()));
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpATTRS(com.jcraft.jsch.SftpATTRS) IOException(java.io.IOException) FsPermission(org.apache.hadoop.fs.permission.FsPermission) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SftpException(com.jcraft.jsch.SftpException)

Aggregations

SftpATTRS (com.jcraft.jsch.SftpATTRS)8 SftpException (com.jcraft.jsch.SftpException)8 File (java.io.File)3 IOException (java.io.IOException)3 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 FileNotFoundException (java.io.FileNotFoundException)2 FileStatus (org.apache.hadoop.fs.FileStatus)2 Path (org.apache.hadoop.fs.Path)2 FsPermission (org.apache.hadoop.fs.permission.FsPermission)2 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)1 LsEntrySelector (com.jcraft.jsch.ChannelSftp.LsEntrySelector)1 JSch (com.jcraft.jsch.JSch)1 JSchException (com.jcraft.jsch.JSchException)1 Session (com.jcraft.jsch.Session)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Matcher (java.util.regex.Matcher)1 OpenSshConfig (org.spearce_voltpatches.jgit.transport.OpenSshConfig)1