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()));
}
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;
}
}
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()));
}
Aggregations