use of com.jcraft.jsch.ChannelSftp.LsEntry 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;
}
use of com.jcraft.jsch.ChannelSftp.LsEntry in project voltdb by VoltDB.
the class SFTPSession method deletePreviouslyInstalledArtifacts.
/**
* if found, it deletes artifacts held in the directories that
* contain the given list of absolute file paths
*
* @param files a collection of files specified as absolute paths
*
* @throws SFTPException when an error occurs during SFTP operations performed
* by this method
*/
public void deletePreviouslyInstalledArtifacts(final Collection<File> files) {
Preconditions.checkArgument(files != null, "null file collection");
Preconditions.checkState(m_channel != null, "stale session");
verifyAllAreAbsolutePaths(files);
// dedup directories containing files
TreeSet<File> directories = new TreeSet<File>();
for (File f : files) {
directories.add(f.getParentFile());
}
// look for file artifacts that end with .so, .jar, and .jnilib
for (File d : directories) {
final ArrayList<String> toBeDeleted = new ArrayList<String>();
LsEntrySelector selector = new LsEntrySelector() {
@Override
public int select(LsEntry entry) {
Matcher mtc = ARTIFACT_REGEXP.matcher(entry.getFilename());
SftpATTRS attr = entry.getAttrs();
if (mtc.find() && !attr.isDir() && !attr.isLink()) {
toBeDeleted.add(entry.getFilename());
}
return CONTINUE;
}
};
try {
m_channel.ls(d.getPath(), selector);
if (m_log.isDebugEnabled()) {
m_log.debug("SFTP: ls " + d.getPath());
}
} catch (SftpException sfex) {
throw new SFTPException("list directory " + d, sfex);
}
// delete found artifacts
for (String f : toBeDeleted) {
File artifact = new File(d, f);
try {
m_channel.rm(artifact.getPath());
if (m_log.isDebugEnabled()) {
m_log.debug("SFTP: rm " + artifact.getPath());
}
} catch (SftpException sfex) {
throw new SFTPException("remove artifact " + artifact, sfex);
}
}
}
}
use of com.jcraft.jsch.ChannelSftp.LsEntry in project voltdb by VoltDB.
the class ExportOnServerVerifier method checkForMoreFilesRemote.
@SuppressWarnings("unchecked")
private void checkForMoreFilesRemote(Comparator<String> comparator) throws Exception {
int onDoneRetries = 6;
long start_time = System.currentTimeMillis();
while (m_exportFiles.isEmpty()) {
/*
* Collect the list of remote files at each node
* Sort the list from each node
*/
int activeFound = 0;
List<Pair<ChannelSftp, List<String>>> pathsFromAllNodes = new ArrayList<Pair<ChannelSftp, List<String>>>();
for (RemoteHost rh : m_hosts) {
Vector<LsEntry> files = rh.channel.ls(rh.path);
List<String> paths = new ArrayList<String>();
final int trackerModifyTime = rh.channel.stat(rh.path + "/" + TRACKER_FILENAME).getMTime();
boolean activeInRemote = false;
boolean filesInRemote = false;
for (LsEntry entry : files) {
activeInRemote = activeInRemote || entry.getFilename().trim().toLowerCase().startsWith("active");
filesInRemote = filesInRemote || entry.getFilename().trim().toLowerCase().startsWith("active");
if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..") && !entry.getAttrs().isDir()) {
final String entryFileName = rh.path + "/" + entry.getFilename();
final int entryModifyTime = entry.getAttrs().getMTime();
if (!entry.getFilename().contains("active")) {
Matcher mtc = EXPORT_FILENAME_REGEXP.matcher(entry.getFilename());
if (mtc.matches()) {
paths.add(entryFileName);
activeInRemote = activeInRemote || entryModifyTime > trackerModifyTime;
filesInRemote = true;
} else {
System.err.println("ERROR: " + entryFileName + " does not match expected export file name pattern");
}
} else if (entry.getFilename().trim().toLowerCase().startsWith("active-")) {
if ((trackerModifyTime - entryModifyTime) > 120) {
final String renamed = rh.path + "/" + entry.getFilename().substring("active-".length());
rh.channel.rename(entryFileName, renamed);
paths.add(renamed);
}
}
}
}
touchActiveTracker(rh);
rh.activeSeen = rh.activeSeen || activeInRemote;
rh.fileSeen = rh.fileSeen || filesInRemote;
if (activeInRemote)
activeFound++;
Collections.sort(paths, comparator);
if (!paths.isEmpty())
pathsFromAllNodes.add(Pair.of(rh.channel, paths));
}
if (!m_clientComplete.isEmpty()) {
printExportFileSituation(pathsFromAllNodes, activeFound);
}
if (pathsFromAllNodes.isEmpty() && activeFound == 0 && allActiveSeen()) {
if (--onDoneRetries <= 0)
return;
Thread.sleep(5000);
}
// add them to m_exportFiles as ordered by the comparator
TreeMap<String, Pair<ChannelSftp, String>> hadPaths = new TreeMap<String, Pair<ChannelSftp, String>>(comparator);
for (Pair<ChannelSftp, List<String>> p : pathsFromAllNodes) {
final ChannelSftp c = p.getFirst();
for (String path : p.getSecond()) {
hadPaths.put(path, Pair.of(c, path));
}
}
boolean hadOne = !hadPaths.isEmpty();
Iterator<Map.Entry<String, Pair<ChannelSftp, String>>> itr = hadPaths.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, Pair<ChannelSftp, String>> entry = itr.next();
m_exportFiles.offer(entry.getValue());
itr.remove();
}
long now = System.currentTimeMillis();
if ((now - start_time) > FILE_TIMEOUT_MS) {
throw new ValidationErr("Timed out waiting on new files.\n" + "This indicates a mismatch in the transaction streams between the client logs and the export data or the death of something important.", null, null);
} else if (!hadOne) {
Thread.sleep(1200);
}
}
}
use of com.jcraft.jsch.ChannelSftp.LsEntry in project ats-framework by Axway.
the class JschSftpClient method ls.
/**
*
* @param directoryPath directory path
* @return {@link List} of {@link FileEntry} objects corresponding with the target directory
* file and folder entries.
*/
public List<FileEntry> ls(String directoryPath) {
try {
List<LsEntry> entries = new ArrayList<LsEntry>();
channel.ls(directoryPath, new LsEntrySelector(entries));
return getFileEntries(entries, directoryPath);
} catch (Exception e) {
throw new JschSftpClientException(e.getMessage(), e);
}
}
use of com.jcraft.jsch.ChannelSftp.LsEntry in project cdap by caskdata.
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;
}
Aggregations