Java Examples for com.jcraft.jsch.ChannelSftp.LsEntry
The following java examples will help you to understand the usage of com.jcraft.jsch.ChannelSftp.LsEntry. These source code samples are taken from different open source projects.
Example 1
| Project: batch-upload-tool-master File: SftpUtils.java View source code |
public static void landFile(File fileToLand, UploadProperties properties) throws Exception {
JSch jsch = new JSch();
String host = properties.getSftpServer();
String user = properties.getUser();
String password = properties.getPassword();
int port = properties.getPort();
Session session = jsch.getSession(user, host, port);
session.setOutputStream(System.out);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect(TIMEOUT);
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp c = (ChannelSftp) channel;
// delete any existing file with the target filename, so that rename will work
@SuppressWarnings("unchecked") Vector<LsEntry> files = c.ls(".");
for (LsEntry file : files) {
if (FINAL_REMOTE_FILENAME.equals(file.getFilename())) {
c.rm(FINAL_REMOTE_FILENAME);
}
}
// transmit file, using temp remote name, so ingestion won't process file until complete
c.put(new FileInputStream(fileToLand), TEMP_REMOTE_FILENAME, ChannelSftp.OVERWRITE);
// rename remote file so ingestion can begin
c.rename(TEMP_REMOTE_FILENAME, FINAL_REMOTE_FILENAME);
c.disconnect();
channel.disconnect();
session.disconnect();
}Example 2
| Project: spring-integration-master File: SftpOutboundTests.java View source code |
@Test
public void testHandleFileMessage() throws Exception {
File targetDir = new File("remote-target-dir");
assertTrue("target directory does not exist: " + targetDir.getName(), targetDir.exists());
SessionFactory<LsEntry> sessionFactory = new TestSftpSessionFactory();
FileTransferringMessageHandler<LsEntry> handler = new FileTransferringMessageHandler<LsEntry>(sessionFactory);
handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName()));
DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator();
fGenerator.setBeanFactory(mock(BeanFactory.class));
fGenerator.setExpression("payload + '.test'");
handler.setFileNameGenerator(fGenerator);
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
File srcFile = File.createTempFile("testHandleFileMessage", ".tmp", new File("."));
srcFile.deleteOnExit();
File destFile = new File(targetDir, srcFile.getName() + ".test");
destFile.deleteOnExit();
handler.handleMessage(new GenericMessage<>(srcFile));
assertTrue("destination file was not created", destFile.exists());
}Example 3
| Project: spring-integration-samples-master File: SftpTestUtils.java View source code |
public static void createTestFiles(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes()); template.execute((SessionCallback<LsEntry, Void>) session -> { try { session.mkdir("si.sftp.sample"); } catch (Exception e) { assertThat(e.getMessage(), containsString("failed to create")); } for (int i = 0; i < fileNames.length; i++) { stream.reset(); session.write(stream, "si.sftp.sample/" + fileNames[i]); } return null; }); } }
Example 4
| Project: citrus-tool-master File: SshResource.java View source code |
@Override
public List list() {
assertDirectory();
List entries;
try {
entries = channel.ls(getURI().getPath());
} catch (SftpException e) {
throw new ConfigException(e);
}
List result = new ArrayList(entries.size());
for (Iterator i = entries.iterator(); i.hasNext(); ) {
LsEntry entry = (LsEntry) i.next();
String name = entry.getFilename();
if (".".equals(name) || "..".equals(name)) {
continue;
}
result.add(new SshResource(getSession(), channel, getURI().getSubURI(name, entry.getAttrs().isDir()), entry.getAttrs()));
}
Collections.sort(result);
return result;
}Example 5
| Project: orion.server-master File: SftpFileStore.java View source code |
@Override
public IFileInfo[] childInfos(int options, IProgressMonitor monitor) throws CoreException {
SynchronizedChannel channel = getChannel();
try {
Vector<LsEntry> children = channel.ls(getPathString(channel));
List<IFileInfo> childInfos = new ArrayList<IFileInfo>(children.size());
for (LsEntry child : children) {
if (!shouldSkip(child.getFilename()))
childInfos.add(attrsToInfo(child.getFilename(), child.getAttrs()));
}
return childInfos.toArray(new IFileInfo[childInfos.size()]);
} catch (Exception e) {
ChannelCache.flush(host);
throw wrap(e);
}
}Example 6
| Project: pentaho-kettle-master File: SFTPClient.java View source code |
public String[] dir() throws KettleJobException {
String[] fileList = null;
try {
java.util.Vector<?> v = c.ls(".");
java.util.Vector<String> o = new java.util.Vector<String>();
if (v != null) {
for (int i = 0; i < v.size(); i++) {
Object obj = v.elementAt(i);
if (obj != null && obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
LsEntry lse = (com.jcraft.jsch.ChannelSftp.LsEntry) obj;
if (!lse.getAttrs().isDir()) {
o.add(lse.getFilename());
}
}
}
}
if (o.size() > 0) {
fileList = new String[o.size()];
o.copyInto(fileList);
}
} catch (SftpException e) {
throw new KettleJobException(e);
}
return fileList;
}Example 7
| Project: elasticsearch-repository-ssh-master File: JSchClient.java View source code |
public Vector<LsEntry> ls(final BlobPath blobPath) throws SftpException, JSchException { Session session = sshPool.getSession(); ChannelSftp channel = openSftpChannel(session); try { @SuppressWarnings("unchecked") final Vector<LsEntry> entities = channel.ls(config.getLocation() + "/" + blobPath.buildAsString("/")); return entities; } finally { closeChannel(channel); sshPool.returnSession(session); } }
Example 8
| Project: gft-master File: SshFileServerConnection.java View source code |
@Override
@SuppressWarnings("unchecked")
public LinkedHashMap<String, FileAttributes> getDirectoryEntries(String path) {
try {
path = fileserver.unixPath(path);
logger.info("getting remote diretory: {}", path);
Vector<LsEntry> vv = sftp.ls(path);
logger.info("found {} entries", vv.size());
LinkedHashMap<String, FileAttributes> result = new LinkedHashMap<String, FileAttributes>();
for (LsEntry entry : vv) {
logger.debug("found entry {} - {}", entry.getFilename(), entry.getLongname());
SftpATTRS attr = entry.getAttrs();
result.put(entry.getFilename(), new FileAttributes(attr.getATime(), attr.getMTime(), attr.isDir(), attr.getSize()));
}
return result;
} catch (SftpException e) {
throw new RuntimeException(e + " for directory " + path, e);
}
}Example 9
| Project: jucy-master File: UploadFile.java View source code |
@SuppressWarnings("unchecked")
private long rekUpload(File source, String path, ChannelSftp c) throws SftpException {
long total = 0;
List<LsEntry> existing = c.ls(path);
for (File f : source.listFiles()) {
if (f.isFile()) {
c.put(f.getPath(), path + "/" + f.getName(), new MyProgress());
total += f.length();
} else if (f.isDirectory()) {
String dirPath = path + "/" + f.getName();
boolean exists = false;
for (LsEntry lse : existing) {
if (lse.getFilename().equals(f.getName())) {
exists = true;
}
}
if (!exists) {
c.mkdir(dirPath);
}
total += rekUpload(f, dirPath, c);
}
}
return total;
}Example 10
| Project: org.nabucco.testautomation.engine.proxy.process-master File: SFTPClient.java View source code |
/**
* List all files in the given directory.
*
* @param path
* the path to the directory
*
* @throws SFTPException
* when the directory does not exist or access is restricted
*/
public List<LsEntry> ls(String path) throws SFTPException {
try {
@SuppressWarnings("unchecked") Vector<ChannelSftp.LsEntry> entries = this.channel.ls(path);
return new ArrayList<ChannelSftp.LsEntry>(entries);
} catch (SftpException se) {
logger.error(se, "Error listing files in SFTP directory '", path, "'.");
throw new SFTPException("Error listing files in SFTP directory '" + path + "'.", se);
}
}Example 11
| Project: smartly-master File: SFTPClient.java View source code |
/**
* List remote path
*
* @param opt_path
* @return
*/
public Set<String> list(final String opt_path) {
final Set<String> result = new HashSet<String>();
final String path = StringUtils.hasText(opt_path) ? opt_path : ".";
try {
final Vector vv = _channel.ls(path);
if (vv != null) {
for (int ii = 0; ii < vv.size(); ii++) {
Object obj = vv.elementAt(ii);
if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
result.add(((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getFilename());
}
}
}
} catch (Exception e) {
this.getLogger().log(Level.SEVERE, null, e);
}
return result;
}Example 12
| Project: AmazonEC2Matlab-master File: Sftp.java View source code |
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
String host = null;
if (arg.length > 0) {
host = arg[0];
} else {
host = JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name") + "@localhost");
}
String user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@') + 1);
int port = 22;
Session session = jsch.getSession(user, host, port);
// username and password will be given via UserInfo interface.
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp c = (ChannelSftp) channel;
java.io.InputStream in = System.in;
java.io.PrintStream out = System.out;
java.util.Vector cmds = new java.util.Vector();
byte[] buf = new byte[1024];
int i;
String str;
int level = 0;
while (true) {
out.print("sftp> ");
cmds.removeAllElements();
i = in.read(buf, 0, 1024);
if (i <= 0)
break;
i--;
if (i > 0 && buf[i - 1] == 0x0d)
i--;
//str=new String(buf, 0, i);
//System.out.println("|"+str+"|");
int s = 0;
for (int ii = 0; ii < i; ii++) {
if (buf[ii] == ' ') {
if (ii - s > 0) {
cmds.addElement(new String(buf, s, ii - s));
}
while (ii < i) {
if (buf[ii] != ' ')
break;
ii++;
}
s = ii;
}
}
if (s < i) {
cmds.addElement(new String(buf, s, i - s));
}
if (cmds.size() == 0)
continue;
String cmd = (String) cmds.elementAt(0);
if (cmd.equals("quit")) {
c.quit();
break;
}
if (cmd.equals("exit")) {
c.exit();
break;
}
if (cmd.equals("rekey")) {
session.rekey();
continue;
}
if (cmd.equals("compression")) {
if (cmds.size() < 2) {
out.println("compression level: " + level);
continue;
}
try {
level = Integer.parseInt((String) cmds.elementAt(1));
if (level == 0) {
session.setConfig("compression.s2c", "none");
session.setConfig("compression.c2s", "none");
} else {
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none");
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none");
}
} catch (Exception e) {
}
session.rekey();
continue;
}
if (cmd.equals("cd") || cmd.equals("lcd")) {
if (cmds.size() < 2)
continue;
String path = (String) cmds.elementAt(1);
try {
if (cmd.equals("cd"))
c.cd(path);
else
c.lcd(path);
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("rm") || cmd.equals("rmdir") || cmd.equals("mkdir")) {
if (cmds.size() < 2)
continue;
String path = (String) cmds.elementAt(1);
try {
if (cmd.equals("rm"))
c.rm(path);
else if (cmd.equals("rmdir"))
c.rmdir(path);
else
c.mkdir(path);
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("chgrp") || cmd.equals("chown") || cmd.equals("chmod")) {
if (cmds.size() != 3)
continue;
String path = (String) cmds.elementAt(2);
int foo = 0;
if (cmd.equals("chmod")) {
byte[] bar = ((String) cmds.elementAt(1)).getBytes();
int k;
for (int j = 0; j < bar.length; j++) {
k = bar[j];
if (k < '0' || k > '7') {
foo = -1;
break;
}
foo <<= 3;
foo |= (k - '0');
}
if (foo == -1)
continue;
} else {
try {
foo = Integer.parseInt((String) cmds.elementAt(1));
} catch (Exception e) {
continue;
}
}
try {
if (cmd.equals("chgrp")) {
c.chgrp(foo, path);
} else if (cmd.equals("chown")) {
c.chown(foo, path);
} else if (cmd.equals("chmod")) {
c.chmod(foo, path);
}
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("pwd") || cmd.equals("lpwd")) {
str = (cmd.equals("pwd") ? "Remote" : "Local");
str += " working directory: ";
if (cmd.equals("pwd"))
str += c.pwd();
else
str += c.lpwd();
out.println(str);
continue;
}
if (cmd.equals("ls") || cmd.equals("dir")) {
String path = ".";
if (cmds.size() == 2)
path = (String) cmds.elementAt(1);
try {
java.util.Vector vv = c.ls(path);
if (vv != null) {
for (int ii = 0; ii < vv.size(); ii++) {
// out.println(vv.elementAt(ii).toString());
Object obj = vv.elementAt(ii);
if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
out.println(((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getLongname());
}
}
}
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("lls") || cmd.equals("ldir")) {
String path = ".";
if (cmds.size() == 2)
path = (String) cmds.elementAt(1);
try {
java.io.File file = new java.io.File(path);
if (!file.exists()) {
out.println(path + ": No such file or directory");
continue;
}
if (file.isDirectory()) {
String[] list = file.list();
for (int ii = 0; ii < list.length; ii++) {
out.println(list[ii]);
}
continue;
}
out.println(path);
} catch (Exception e) {
System.out.println(e);
}
continue;
}
if (cmd.equals("get") || cmd.equals("get-resume") || cmd.equals("get-append") || cmd.equals("put") || cmd.equals("put-resume") || cmd.equals("put-append")) {
if (cmds.size() != 2 && cmds.size() != 3)
continue;
String p1 = (String) cmds.elementAt(1);
// String p2=p1;
String p2 = ".";
if (cmds.size() == 3)
p2 = (String) cmds.elementAt(2);
try {
SftpProgressMonitor monitor = new MyProgressMonitor();
if (cmd.startsWith("get")) {
int mode = ChannelSftp.OVERWRITE;
if (cmd.equals("get-resume")) {
mode = ChannelSftp.RESUME;
} else if (cmd.equals("get-append")) {
mode = ChannelSftp.APPEND;
}
c.get(p1, p2, monitor, mode);
} else {
int mode = ChannelSftp.OVERWRITE;
if (cmd.equals("put-resume")) {
mode = ChannelSftp.RESUME;
} else if (cmd.equals("put-append")) {
mode = ChannelSftp.APPEND;
}
c.put(p1, p2, monitor, mode);
}
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("ln") || cmd.equals("symlink") || cmd.equals("rename")) {
if (cmds.size() != 3)
continue;
String p1 = (String) cmds.elementAt(1);
String p2 = (String) cmds.elementAt(2);
try {
if (cmd.equals("rename"))
c.rename(p1, p2);
else
c.symlink(p1, p2);
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("stat") || cmd.equals("lstat")) {
if (cmds.size() != 2)
continue;
String p1 = (String) cmds.elementAt(1);
SftpATTRS attrs = null;
try {
if (cmd.equals("stat"))
attrs = c.stat(p1);
else
attrs = c.lstat(p1);
} catch (SftpException e) {
System.out.println(e.toString());
}
if (attrs != null) {
out.println(attrs);
} else {
}
continue;
}
if (cmd.equals("readlink")) {
if (cmds.size() != 2)
continue;
String p1 = (String) cmds.elementAt(1);
String filename = null;
try {
filename = c.readlink(p1);
out.println(filename);
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("realpath")) {
if (cmds.size() != 2)
continue;
String p1 = (String) cmds.elementAt(1);
String filename = null;
try {
filename = c.realpath(p1);
out.println(filename);
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("version")) {
out.println("SFTP protocol version " + c.version());
continue;
}
if (cmd.equals("help") || cmd.equals("?")) {
out.println(help);
continue;
}
out.println("unimplemented command: " + cmd);
}
session.disconnect();
} catch (Exception e) {
System.out.println(e);
}
System.exit(0);
}Example 13
| Project: bior_pipeline-master File: RSync.java View source code |
/** A slow way of getting the remote files (each file is a separate call to get its stats)
* This takes about 10 seconds per 1000 files.
* This uses recursion to get all of the files, starting with the main project path
* @param sftpChannel
* @param fullPathToDirectory
* @return
* @throws SftpException
* @throws ParseException
*/
private ArrayList<FileInfo> getRemoteFiles(ChannelSftp sftpChannel, String fullPathToDirectory) throws SftpException, ParseException {
ArrayList<FileInfo> fileList = new ArrayList<FileInfo>();
sftpChannel.cd(fullPathToDirectory);
Vector<LsEntry> files = sftpChannel.ls(".");
for (LsEntry file : files) {
// Don't add the "." and ".." directories
String filename = file.getFilename();
if (".".equals(filename) || "..".equals(filename))
continue;
FileInfo fileInfo = new FileInfo();
fileInfo.path = fullPathToDirectory + "/" + filename;
SftpATTRS attrs = file.getAttrs();
fileInfo.isDir = attrs.isDir();
fileInfo.isRemote = true;
fileInfo.size = attrs.getSize();
fileInfo.modDate = (Date) mDateFormat.parse(attrs.getMtimeString());
fileInfo.operation = FileOperation.noChange;
fileList.add(fileInfo);
if (fileInfo.isDir)
fileInfo.dirContents = getRemoteFiles(sftpChannel, fileInfo.path);
}
return fileList;
}Example 14
| Project: jeffaschenk-commons-master File: SecureNetworkPullServiceImpl.java View source code |
/**
* Perform the Import LifeCycle.
*/
@Override
public synchronized void performImportLifeCycle(boolean performArchive, boolean performWait) {
// *****************************************
// Initialize
TimeDuration td = new TimeDuration();
td.start();
// *****************************************
// Publish a Life Cycle Services Event
LifeCycleServicesEvent event = new LifeCycleServicesEvent(this, LifeCycleServiceType.IMPORT, LifeCycleServiceStateType.BEGIN, TimeUtils.now());
publisher.publishEvent(event);
// *****************************************
// Place existing current into Previous
// processing.
this.lastTotalFilesProcessed = this.currentTotalFilesProcessed;
this.lastTimeProcessingStarted = this.currentTimeProcessingStarted;
this.lastTimeProcessingEnded = this.currentTimeProcessingEnded;
this.lastProcessingStatistic = new ArrayList<ImportLoadStatistic>(this.currentProcessingStatistic);
this.currentTotalFilesProcessed = 0;
this.currentTimeProcessingStarted = TimeUtils.now();
this.currentTimeProcessingEnded = this.currentTimeProcessingStarted;
this.currentProcessingStatistic = new ArrayList<ImportLoadStatistic>();
int files_skipped = 0;
// ***********************************
// Secure Channel
ChannelSftp channelSftp = null;
// previous Processed Files.
try {
JSch jsch = new JSch();
int port = 22;
Session session = jsch.getSession(secureMBoxPrincipal, secureMBoxHostname, port);
session.setPassword(secureMBoxCredential);
//
// Setup Strict HostKeyChecking so that we dont get
// the unknown host key exception
//
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("./" + secureMBoxDirectory);
java.util.Vector vv = channelSftp.ls("./");
if (vv != null) {
for (int ii = 0; ii < vv.size(); ii++) {
Object obj = vv.elementAt(ii);
if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
// Cast
ChannelSftp.LsEntry ls_entry = (com.jcraft.jsch.ChannelSftp.LsEntry) obj;
if ((ls_entry.getAttrs().isDir()) || (ls_entry.getFilename().startsWith("."))) {
continue;
}
// TODO Interrogate File Name and Timestamp.
logger.info("FOUND Directory Entry:[" + ls_entry.getFilename() + "], File Size:" + ls_entry.getAttrs().getSize() + "B, " + " Mod Time:[" + ls_entry.getAttrs().getMtimeString() + "]");
}
}
}
// TODO Determine which file to obtain.....
// *********************************
// Obtain the Class Definition for this File.
Class<? extends RootElement> clazz = this.utilityService.getDefaultClassInstance();
// *********************************
// Instantiate our new Statistic
ImportLoadStatistic statistic = new ImportLoadStatistic(null, clazz, this.systemDAO.getRowCount(clazz));
statistic.setPerformArchive(performArchive);
statistic.setFileViaSecureNetworkChannel(true);
// TODO Fix Me
statistic.setNetworkFileName("sample.txt");
// ***********************************
// Obtain Our Secure InputStream
// and perform the Import.
InputStream inputStream = channelSftp.get(statistic.getNetworkFileName());
this.parseImportFile(statistic, inputStream);
this.currentTotalFilesProcessed++;
logger.info("Processed: " + statistic);
// *****************************************
// Publish a Life Cycle Services Event
event = new LifeCycleServicesEvent(this, LifeCycleServiceType.IMPORT, LifeCycleServiceStateType.DONE, TimeUtils.now());
publisher.publishEvent(event);
// ********************************************************
// All Spawned Tasks Completed.
this.currentTimeProcessingEnded = TimeUtils.now();
td.stop();
} catch (SftpException e) {
} catch (JSchException e) {
} finally {
if (channelSftp != null) {
channelSftp.disconnect();
}
// *******************************************
// Show Statistics.
logger.info("This Cycle Number of Processed Files:[" + this.currentTotalFilesProcessed + "], Skipped Files:[" + files_skipped + "], Duration:[" + td.getElapsedtoString() + "]");
}
}Example 15
| Project: dltk.core-master File: SshFileHandle.java View source code |
private void fetchChildren() {
Vector<LsEntry> list = connection.list(path);
if (list != null) {
children.clear();
long c = System.currentTimeMillis();
for (LsEntry entry : list) {
String filename = entry.getFilename();
if (//$NON-NLS-1$ //$NON-NLS-2$
filename.equals(".") || filename.equals("..")) {
continue;
}
final SftpATTRS childAttrs = entry.getAttrs();
final IPath childPath;
if (filename.indexOf(IPath.DEVICE_SEPARATOR) == -1) {
childPath = path.append(filename);
} else {
// this way DEVICE_SEPARATOR is kept in path segment
childPath = path.append(new Path(null, filename));
}
SshFileHandle childHandle = new SshFileHandle(connection, childPath, childAttrs);
synchronized (attrCache) {
attrCache.put(childHandle, new CacheEntry(childAttrs, c));
}
children.put(filename, childHandle);
}
childrenFetched = true;
}
}Example 16
| Project: opennms_dashboard-master File: Sftp3gppUrlConnection.java View source code |
/**
* Gets the file list (from the path defined on the URL).
*
* @return the file list
* @throws SftpException the SFTP exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@SuppressWarnings("unchecked")
public List<String> getFileList() throws SftpException, IOException {
List<String> files = new ArrayList<String>();
Vector<LsEntry> entries = getChannel().ls(url.getPath());
for (LsEntry entry : entries) {
if (entry.getFilename().startsWith("."))
continue;
files.add(entry.getFilename());
}
Collections.sort(files);
return files;
}Example 17
| Project: org.eclipse.dltk.core-master File: SshFileHandle.java View source code |
private void fetchChildren() {
Vector<LsEntry> list = connection.list(path);
if (list != null) {
children.clear();
long c = System.currentTimeMillis();
for (LsEntry entry : list) {
String filename = entry.getFilename();
if (//$NON-NLS-1$ //$NON-NLS-2$
filename.equals(".") || filename.equals("..")) {
continue;
}
final SftpATTRS childAttrs = entry.getAttrs();
final IPath childPath;
if (filename.indexOf(IPath.DEVICE_SEPARATOR) == -1) {
childPath = path.append(filename);
} else {
// this way DEVICE_SEPARATOR is kept in path segment
childPath = path.append(new Path(null, filename));
}
SshFileHandle childHandle = new SshFileHandle(connection, childPath, childAttrs);
synchronized (attrCache) {
attrCache.put(childHandle, new CacheEntry(childAttrs, c));
}
children.put(filename, childHandle);
}
childrenFetched = true;
}
}Example 18
| Project: universal-java-matrix-package-master File: JSchUtil.java View source code |
public static List<String> ls(String hostname, int port, String username, File keyFile, final String passphrase, String path) throws JSchException, IOException, SftpException {
Session session = createSession(hostname, port, username, keyFile, passphrase);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
@SuppressWarnings("unchecked") Vector<LsEntry> vector = (Vector<LsEntry>) channel.ls(path);
channel.disconnect();
session.disconnect();
List<String> files = new ArrayList<String>();
for (LsEntry lse : vector) {
files.add(lse.getFilename());
}
return files;
}Example 19
| Project: scisoft-icat-master File: Sftp.java View source code |
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
String host = null;
if (arg.length > 0) {
host = arg[0];
} else {
host = JOptionPane.showInputDialog("Enter username@hostname", System.getProperty("user.name") + "@localhost");
}
String user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@') + 1);
int port = 22;
Session session = jsch.getSession(user, host, port);
// username and password will be given via UserInfo interface.
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp c = (ChannelSftp) channel;
java.io.InputStream in = System.in;
java.io.PrintStream out = System.out;
java.util.Vector cmds = new java.util.Vector();
byte[] buf = new byte[1024];
int i;
String str;
int level = 0;
while (true) {
out.print("sftp> ");
cmds.removeAllElements();
i = in.read(buf, 0, 1024);
if (i <= 0)
break;
i--;
if (i > 0 && buf[i - 1] == 0x0d)
i--;
// str=new String(buf, 0, i);
// System.out.println("|"+str+"|");
int s = 0;
for (int ii = 0; ii < i; ii++) {
if (buf[ii] == ' ') {
if (ii - s > 0) {
cmds.addElement(new String(buf, s, ii - s));
}
while (ii < i) {
if (buf[ii] != ' ')
break;
ii++;
}
s = ii;
}
}
if (s < i) {
cmds.addElement(new String(buf, s, i - s));
}
if (cmds.size() == 0)
continue;
String cmd = (String) cmds.elementAt(0);
if (cmd.equals("quit")) {
c.quit();
break;
}
if (cmd.equals("exit")) {
c.exit();
break;
}
if (cmd.equals("rekey")) {
session.rekey();
continue;
}
if (cmd.equals("compression")) {
if (cmds.size() < 2) {
out.println("compression level: " + level);
continue;
}
try {
level = Integer.parseInt((String) cmds.elementAt(1));
if (level == 0) {
session.setConfig("compression.s2c", "none");
session.setConfig("compression.c2s", "none");
} else {
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none");
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none");
}
} catch (Exception e) {
}
session.rekey();
continue;
}
if (cmd.equals("cd") || cmd.equals("lcd")) {
if (cmds.size() < 2)
continue;
String path = (String) cmds.elementAt(1);
try {
if (cmd.equals("cd"))
c.cd(path);
else
c.lcd(path);
} catch (SftpException e) {
System.out.println(e.toString());
}
continue;
}
if (cmd.equals("rm") || cmd.equals("rmdir") || cmd.equals("mkdir")) {
if (cmds.size() < 2)
continue;
String path = (String) cmds.elementAt(1);
try {
if (cmd.equals("rm"))
c.rm(path);
else if (cmd.equals("rmdir"))
c.rmdir(path);
else
c.mkdir(path);
} catch (SftpException e) {
logger.error("sftp exception: ", e);
}
continue;
}
if (cmd.equals("chgrp") || cmd.equals("chown") || cmd.equals("chmod")) {
if (cmds.size() != 3)
continue;
String path = (String) cmds.elementAt(2);
int foo = 0;
if (cmd.equals("chmod")) {
byte[] bar = ((String) cmds.elementAt(1)).getBytes();
int k;
for (int j = 0; j < bar.length; j++) {
k = bar[j];
if (k < '0' || k > '7') {
foo = -1;
break;
}
foo <<= 3;
foo |= (k - '0');
}
if (foo == -1)
continue;
} else {
try {
foo = Integer.parseInt((String) cmds.elementAt(1));
} catch (Exception e) {
continue;
}
}
try {
if (cmd.equals("chgrp")) {
c.chgrp(foo, path);
} else if (cmd.equals("chown")) {
c.chown(foo, path);
} else if (cmd.equals("chmod")) {
c.chmod(foo, path);
}
} catch (SftpException e) {
logger.error("sftp exception: ", e);
}
continue;
}
if (cmd.equals("pwd") || cmd.equals("lpwd")) {
str = (cmd.equals("pwd") ? "Remote" : "Local");
str += " working directory: ";
if (cmd.equals("pwd"))
str += c.pwd();
else
str += c.lpwd();
out.println(str);
continue;
}
if (cmd.equals("ls") || cmd.equals("dir")) {
String path = ".";
if (cmds.size() == 2)
path = (String) cmds.elementAt(1);
try {
java.util.Vector vv = c.ls(path);
if (vv != null) {
for (int ii = 0; ii < vv.size(); ii++) {
// out.println(vv.elementAt(ii).toString());
Object obj = vv.elementAt(ii);
if (obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry) {
out.println(((com.jcraft.jsch.ChannelSftp.LsEntry) obj).getLongname());
}
}
}
} catch (SftpException e) {
logger.error("sftp exception: ", e);
}
continue;
}
if (cmd.equals("lls") || cmd.equals("ldir")) {
String path = ".";
if (cmds.size() == 2)
path = (String) cmds.elementAt(1);
try {
java.io.File file = new java.io.File(path);
if (!file.exists()) {
out.println(path + ": No such file or directory");
continue;
}
if (file.isDirectory()) {
String[] list = file.list();
for (int ii = 0; ii < list.length; ii++) {
out.println(list[ii]);
}
continue;
}
out.println(path);
} catch (Exception e) {
logger.error("sftp exception: ", e);
}
continue;
}
if (cmd.equals("get") || cmd.equals("get-resume") || cmd.equals("get-append") || cmd.equals("put") || cmd.equals("put-resume") || cmd.equals("put-append")) {
if (cmds.size() != 2 && cmds.size() != 3)
continue;
String p1 = (String) cmds.elementAt(1);
// String p2=p1;
String p2 = ".";
if (cmds.size() == 3)
p2 = (String) cmds.elementAt(2);
try {
SftpProgressMonitor monitor = new MyProgressMonitor();
if (cmd.startsWith("get")) {
int mode = ChannelSftp.OVERWRITE;
if (cmd.equals("get-resume")) {
mode = ChannelSftp.RESUME;
} else if (cmd.equals("get-append")) {
mode = ChannelSftp.APPEND;
}
c.get(p1, p2, monitor, mode);
} else {
int mode = ChannelSftp.OVERWRITE;
if (cmd.equals("put-resume")) {
mode = ChannelSftp.RESUME;
} else if (cmd.equals("put-append")) {
mode = ChannelSftp.APPEND;
}
c.put(p1, p2, monitor, mode);
}
} catch (SftpException e) {
logger.error("sftp exception: ", e);
}
continue;
}
if (cmd.equals("ln") || cmd.equals("symlink") || cmd.equals("rename")) {
if (cmds.size() != 3)
continue;
String p1 = (String) cmds.elementAt(1);
String p2 = (String) cmds.elementAt(2);
try {
if (cmd.equals("rename"))
c.rename(p1, p2);
else
c.symlink(p1, p2);
} catch (SftpException e) {
logger.error("sftp exception: ", e);
}
continue;
}
if (cmd.equals("stat") || cmd.equals("lstat")) {
if (cmds.size() != 2)
continue;
String p1 = (String) cmds.elementAt(1);
SftpATTRS attrs = null;
try {
if (cmd.equals("stat"))
attrs = c.stat(p1);
else
attrs = c.lstat(p1);
} catch (SftpException e) {
logger.error("sftp exception: ", e);
}
if (attrs != null) {
out.println(attrs);
} else {
}
continue;
}
if (cmd.equals("readlink")) {
if (cmds.size() != 2)
continue;
String p1 = (String) cmds.elementAt(1);
String filename = null;
try {
filename = c.readlink(p1);
out.println(filename);
} catch (SftpException e) {
logger.error("sftp exception: ", e);
}
continue;
}
if (cmd.equals("realpath")) {
if (cmds.size() != 2)
continue;
String p1 = (String) cmds.elementAt(1);
String filename = null;
try {
filename = c.realpath(p1);
out.println(filename);
} catch (SftpException e) {
logger.error("sftp exception: ", e);
}
continue;
}
if (cmd.equals("version")) {
out.println("SFTP protocol version " + c.version());
continue;
}
if (cmd.equals("help") || cmd.equals("?")) {
out.println(help);
continue;
}
out.println("unimplemented command: " + cmd);
}
session.disconnect();
} catch (Exception e) {
logger.error("sftp exception: ", e);
}
System.exit(0);
}Example 20
| Project: ant-ivy-master File: SFTPRepository.java View source code |
/**
* This method is similar to getResource, except that the returned resource is fully initialized
* (resolved in the sftp repository), and that the given string is a full remote path
*
* @param path
* the full remote path in the repository of the resource
* @return a fully initialized resource, able to answer to all its methods without needing any
* further connection
*/
public Resource resolveResource(String path) {
try {
ChannelSftp c = getSftpChannel(path);
Collection r = c.ls(getPath(path));
if (r != null) {
for (Iterator iter = r.iterator(); iter.hasNext(); ) {
Object obj = iter.next();
if (obj instanceof LsEntry) {
LsEntry entry = (LsEntry) obj;
SftpATTRS attrs = entry.getAttrs();
return new BasicResource(path, true, attrs.getSize(), attrs.getMTime() * MILLIS_PER_SECOND, false);
}
}
}
} catch (Exception e) {
Message.debug("Error while resolving resource " + path, e);
}
return new BasicResource(path, false, 0, 0, false);
}Example 21
| Project: DataX-master File: SftpHelper.java View source code |
@Override
public HashSet<String> getListFiles(String directoryPath, int parentLevel, int maxTraversalLevel) {
if (parentLevel < maxTraversalLevel) {
// 父级目录,以'/'结尾
String parentPath = null;
int pathLen = directoryPath.length();
if (//*和?的�制
directoryPath.contains("*") || directoryPath.contains("?")) {
// path是æ£åˆ™è¡¨è¾¾å¼?
String subPath = UnstructuredStorageReaderUtil.getRegexPathParentPath(directoryPath);
if (isDirExist(subPath)) {
parentPath = subPath;
} else {
String message = String.format("ä¸?能进入目录:[%s]," + "请确认您的é…?置项path:[%s]å˜åœ¨ï¼Œä¸”é…?置的用户有æ?ƒé™?进入", subPath, directoryPath);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
}
} else if (isDirExist(directoryPath)) {
// path是目录
if (directoryPath.charAt(pathLen - 1) == IOUtils.DIR_SEPARATOR) {
parentPath = directoryPath;
} else {
parentPath = directoryPath + IOUtils.DIR_SEPARATOR;
}
} else if (isSymbolicLink(directoryPath)) {
//path是链接文件
String message = String.format("文件:[%s]是链接文件,当��支�链接文件的读�", directoryPath);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.LINK_FILE, message);
} else if (isFileExist(directoryPath)) {
// path指�具体文件
sourceFiles.add(directoryPath);
return sourceFiles;
} else {
String message = String.format("请确认您的é…?置项path:[%s]å˜åœ¨ï¼Œä¸”é…?置的用户有æ?ƒé™?读å?–", directoryPath);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
}
try {
Vector vector = channelSftp.ls(directoryPath);
for (int i = 0; i < vector.size(); i++) {
LsEntry le = (LsEntry) vector.get(i);
String strName = le.getFilename();
String filePath = parentPath + strName;
if (isDirExist(filePath)) {
// 是å?目录
if (!(strName.equals(".") || strName.equals(".."))) {
//递归处�
getListFiles(filePath, parentLevel + 1, maxTraversalLevel);
}
} else if (isSymbolicLink(filePath)) {
//是链接文件
String message = String.format("文件:[%s]是链接文件,当��支�链接文件的读�", filePath);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.LINK_FILE, message);
} else if (isFileExist(filePath)) {
// 是文件
sourceFiles.add(filePath);
} else {
String message = String.format("请确认path:[%s]å˜åœ¨ï¼Œä¸”é…?置的用户有æ?ƒé™?读å?–", filePath);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
}
}
// end for vector
} catch (SftpException e) {
String message = String.format("获å?–path:[%s] 下文件列表时å?‘生I/O异常,请确认与ftpæœ?务器的连接æ£å¸¸", directoryPath);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
}
return sourceFiles;
} else {
//超出最大递归层数
String message = String.format("获å?–path:[%s] 下文件列表时超出最大层数,请确认路径[%s]下ä¸?å˜åœ¨è½¯è¿žæŽ¥æ–‡ä»¶", directoryPath, directoryPath);
LOG.error(message);
throw DataXException.asDataXException(FtpReaderErrorCode.OUT_MAX_DIRECTORY_LEVEL, message);
}
}Example 22
| Project: dcm4chee-storage2-master File: SftpStorageSystemProvider.java View source code |
@Override
public void deleteObject(StorageContext context, String name) throws IOException {
String path = resolvePath(name);
ChannelSftp channel = openChannel();
try {
try {
channel.rm(path);
} catch (SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE)
throw new ObjectNotFoundException(storageSystem.getStorageSystemPath(), name);
throw new IOException("Delete file failed for path " + path, e);
}
String dir = getParentDir(path);
try {
String basePath = storageSystem.getStorageSystemPath();
while (!basePath.equals(dir)) {
@SuppressWarnings("unchecked") Vector<LsEntry> v = channel.ls(dir);
if (v.size() > 2)
break;
channel.rmdir(dir);
dir = getParentDir(dir);
}
} catch (SftpException e) {
if (e.id != ChannelSftp.SSH_FX_FAILURE)
throw new IOException("Remove directory failed for path " + dir, e);
}
} finally {
channel.disconnect();
}
}Example 23
| Project: eu.geclipse.core-master File: SFTPFileStore.java View source code |
@SuppressWarnings("unchecked")
private void update() throws CoreException {
SFTPConnection connection = ConnectionManager.getInstance().acquireConnection(this.connectionKey);
ChannelSftp channel = connection.getChannel();
if (this.uri.getPath().length() == 0) {
try {
this.uri = new URI(this.uri.getScheme(), this.uri.getUserInfo(), this.uri.getHost(), this.uri.getPort(), channel.getHome(), this.uri.getQuery(), this.uri.getFragment());
this.path = new Path(this.uri.getPath());
} catch (URISyntaxException uriSyntaxException) {
Activator.logException(uriSyntaxException);
}// necessary for ganymede
catch (SftpException sftpException) {
Activator.logException(sftpException);
}
}
try {
String remotePath = this.path.toString();
if (remotePath.length() == 0) {
//$NON-NLS-1$
remotePath = "/";
}
Vector<LsEntry> vector = channel.ls(remotePath);
for (LsEntry lsEntry : vector) {
String filename = lsEntry.getFilename();
if (!"..".equals(filename) && filename.indexOf(':') == -1) {
//$NON-NLS-1$
SftpATTRS attributes = lsEntry.getAttrs();
FileInfo fileInfo = new FileInfo(filename);
fileInfo.setLength(attributes.getSize());
fileInfo.setDirectory(attributes.isDir());
fileInfo.setExists(true);
fileInfo.setLastModified((long) attributes.getMTime() * 1000);
// TODO think about a ways to get attribute
fileInfo.setAttribute(EFS.ATTRIBUTE_READ_ONLY, false);
if (".".equals(filename) || vector.size() == 1) {
//$NON-NLS-1$
if (".".equals(filename)) {
//$NON-NLS-1$
fileInfo.setName(remotePath);
} else {
fileInfo.setName(filename);
}
if (this.myFileInfo == null || this.myFileInfo.getLastModified() != fileInfo.getLastModified()) {
this.myFileInfo = fileInfo;
}
} else {
this.children.put(filename, new SFTPFileStore(this, fileInfo));
this.childNames.add(filename);
this.childInfos.add(fileInfo);
}
}
}
} catch (SftpException sftpException) {
this.myFileInfo = new FileInfo(this.path.lastSegment());
this.myFileInfo.setExists(false);
this.myFileInfo.setAttribute(EFS.ATTRIBUTE_READ_ONLY, false);
if (sftpException.id != 2) {
Activator.logException(sftpException);
}
} finally {
connection.unlock();
}
}Example 24
| Project: extreme-fishbowl-master File: SftpFileObject.java View source code |
/**
* Lists the children of this file.
*/
protected FileObject[] doListChildrenResolved() throws Exception {
// List the contents of the folder
final Vector vector;
final ChannelSftp channel = fileSystem.getChannel();
try {
vector = channel.ls(relPath);
} finally {
fileSystem.putChannel(channel);
}
if (vector == null) {
throw new FileSystemException("vfs.provider.sftp/list-children.error");
}
// Extract the child names
final ArrayList children = new ArrayList();
for (Iterator iterator = vector.iterator(); iterator.hasNext(); ) {
final LsEntry stat = (LsEntry) iterator.next();
String name = stat.getFilename();
if (VFS.isUriStyle()) {
if (stat.getAttrs().isDir() && name.charAt(name.length() - 1) != '/') {
name = name + "/";
}
}
if (name.equals(".") || name.equals("..") || name.equals("./") || name.equals("../")) {
continue;
}
FileObject fo = getFileSystem().resolveFile(getFileSystem().getFileSystemManager().resolveName(getName(), UriParser.encode(name), NameScope.CHILD));
((SftpFileObject) FileObjectUtils.getAbstractFileObject(fo)).setStat(stat.getAttrs());
children.add(fo);
}
return (FileObject[]) children.toArray(new FileObject[children.size()]);
}Example 25
| Project: kipeto-master File: SFTPRepositoryStrategy.java View source code |
@SuppressWarnings("unchecked")
public List<Reference> allReferences() throws IOException {
List<Reference> list = new ArrayList<Reference>();
try {
Vector<LsEntry> ls = channel.ls(refs);
for (LsEntry entry : ls) {
String name = entry.getFilename();
if (isSpecialFile(name)) {
continue;
} else {
String id = resolveReference(name);
list.add(new Reference(id, name));
}
}
return list;
} catch (SftpException e) {
throw new RuntimeException(e);
}
}Example 26
| Project: twister.github.io-master File: RunnerRepository.java View source code |
public static String[] getRemoteFolderContent(String folder) {
while (sftpoccupied) {
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
sftpoccupied = true;
int size;
Vector v = null;
try {
v = connection.ls(folder);
size = v.size();
} catch (Exception e) {
System.out.println("No files found in: " + folder + " directory");
size = 0;
}
ArrayList<String> files = new ArrayList<String>();
String name = null;
for (int i = 0; i < size; i++) {
try {
name = ((LsEntry) v.get(i)).getFilename();
if (name.split("\\.").length == 0)
continue;
files.add(name.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
sftpoccupied = false;
String[] resp = new String[files.size()];
files.toArray(resp);
return resp;
}Example 27
| Project: cloud-master File: SshContextImpl.java View source code |
@Override
public List<String> ls(File file) throws IOException {
try {
final List<String> names = Lists.newArrayList();
LsEntrySelector selector = new LsEntrySelector() {
@Override
public int select(LsEntry entry) {
// TODO: Filter out directory etc through
// attributes...
names.add(entry.getFilename());
return CONTINUE;
}
};
// TODO: The source code for the ls function is not
// confidence-inspiring
// TODO: Don't get attributes??
channel.ls(file.getPath(), selector);
return names;
} catch (SftpException e) {
throw new IOException("Error during sftp ls: " + file, e);
}
}Example 28
| Project: gobblin-master File: SftpFsHelper.java View source code |
@Override
public List<String> ls(String path) throws FileBasedHelperException {
try {
List<String> list = new ArrayList<>();
ChannelSftp channel = getSftpChannel();
Vector<LsEntry> vector = channel.ls(path);
for (LsEntry entry : vector) {
list.add(entry.getFilename());
}
channel.disconnect();
return list;
} catch (SftpException e) {
throw new FileBasedHelperException("Cannot execute ls command on sftp connection", e);
}
}Example 29
| Project: pdi-vfs-master File: SftpFileObject.java View source code |
/**
* Lists the children of this file.
*/
protected FileObject[] doListChildrenResolved() throws Exception {
// List the contents of the folder
final Vector vector;
final ChannelSftp channel = fileSystem.getChannel();
String workingDirectory = null;
try {
try {
if (relPath != null) {
workingDirectory = channel.pwd();
channel.cd(relPath);
}
} catch (SftpException e) {
return null;
}
try {
vector = channel.ls(".");
} catch (SftpException e) {
try {
if (relPath != null) {
channel.cd(workingDirectory);
}
} catch (SftpException e2) {
throw e;
}
throw e;
}
try {
if (relPath != null) {
channel.cd(workingDirectory);
}
} catch (SftpException e) {
throw new FileSystemException("vfs.provider.sftp/change-work-directory-back.error", workingDirectory);
}
} finally {
fileSystem.putChannel(channel);
}
if (vector == null) {
throw new FileSystemException("vfs.provider.sftp/list-children.error");
}
// Extract the child names
final ArrayList children = new ArrayList();
for (Iterator iterator = vector.iterator(); iterator.hasNext(); ) {
final LsEntry stat = (LsEntry) iterator.next();
String name = stat.getFilename();
if (VFS.isUriStyle()) {
if (stat.getAttrs().isDir() && name.charAt(name.length() - 1) != '/') {
name = name + "/";
}
}
if (name.equals(".") || name.equals("..") || name.equals("./") || name.equals("../")) {
continue;
}
FileObject fo = getFileSystem().resolveFile(getFileSystem().getFileSystemManager().resolveName(getName(), UriParser.encode(name), NameScope.CHILD));
((SftpFileObject) FileObjectUtils.getAbstractFileObject(fo)).setStat(stat.getAttrs());
children.add(fo);
}
return (FileObject[]) children.toArray(new FileObject[children.size()]);
}Example 30
| Project: syncany-plugin-sftp-master File: SftpTransferManager.java View source code |
@Override
public <T extends RemoteFile> Map<String, T> list(Class<T> remoteFileClass) throws StorageException {
connect();
try {
// List folder
String remoteFilePath = getRemoteFilePath(remoteFileClass);
List<LsEntry> entries = listEntries(remoteFilePath + "/");
// Create RemoteFile objects
Map<String, T> remoteFiles = new HashMap<String, T>();
for (LsEntry entry : entries) {
try {
T remoteFile = RemoteFile.createRemoteFile(entry.getFilename(), remoteFileClass);
remoteFiles.put(entry.getFilename(), remoteFile);
} catch (Exception e) {
logger.log(Level.INFO, "Cannot create instance of " + remoteFileClass.getSimpleName() + " for file " + entry.getFilename() + "; maybe invalid file name pattern. Ignoring file.");
}
}
return remoteFiles;
} catch (SftpException ex) {
disconnect();
logger.log(Level.SEVERE, "Unable to list FTP directory.", ex);
throw new StorageException(ex);
}
}Example 31
| Project: cdap-master File: SFTPFileSystem.java View source code |
/**
* 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;
}Example 32
| Project: hadoop-master File: SFTPFileSystem.java View source code |
/**
* 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;
}Example 33
| Project: JECommons-master File: DataSourceHelper.java View source code |
public static List<String> getSFTPMatchedFileNames(ChannelSftp _channel, DateTime lastReadout, String filePath) {
filePath = filePath.replace("\\", "/");
String[] pathStream = getPathTokens(filePath);
String startPath = "";
if (filePath.startsWith("/")) {
startPath = "/";
}
List<String> folderPathes = getSFTPMatchingPathes(startPath, pathStream, new ArrayList<String>(), _channel, lastReadout, new DateTimeFormatterBuilder());
// System.out.println("foldersize,"+folderPathes.size());
List<String> fileNames = new ArrayList<String>();
if (folderPathes.isEmpty()) {
org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR, "Cant find suitable folder on the device");
return fileNames;
}
if (folderPathes.isEmpty()) {
org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR, "Cant find suitable folder on the device");
return fileNames;
}
String fileNameScheme = pathStream[pathStream.length - 1];
String currentfolder = null;
try {
for (String folder : folderPathes) {
// fc.changeWorkingDirectory(folder);
// System.out.println("currentFolder,"+folder);
currentfolder = folder;
// Vector ls = _channel.ls(folder);
for (Object fileName : _channel.ls(folder)) {
LsEntry currentFile = (LsEntry) fileName;
String currentFileName = currentFile.getFilename();
currentFileName = removeFoler(currentFileName, folder);
boolean match = false;
System.out.println(currentFileName);
if (DataSourceHelper.containsTokens(fileNameScheme)) {
boolean matchDate = matchDateString(currentFileName, fileNameScheme);
DateTime folderTime = getFileTime(folder + currentFileName, pathStream);
boolean isLater = folderTime.isAfter(lastReadout);
if (matchDate && isLater) {
match = true;
}
} else {
Pattern p = Pattern.compile(fileNameScheme);
Matcher m = p.matcher(currentFileName);
match = m.matches();
}
if (match) {
fileNames.add(folder + currentFileName);
}
}
}
} catch (Exception ex) {
org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR, "Error while searching a matching file");
org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR, "Folder: " + currentfolder);
org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR, "FileName: " + fileNameScheme);
org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR, ex.getMessage());
}
if (folderPathes.isEmpty()) {
org.apache.log4j.Logger.getLogger(DataSourceHelper.class).log(org.apache.log4j.Level.ERROR, "Cant find suitable files on the device");
}
// System.out.println("filenamesize"+fileNames.size());
return fileNames;
}Example 34
| Project: structr-master File: SSHFilesTest.java View source code |
@Test
public void test00RootDirectoriesAndAttributes() {
final ChannelSftp sftp = setupSftpClient("ftpuser1", "ftpuserpw1");
try {
final Vector<LsEntry> entries = sftp.ls("/");
// listing contains "." => 5 entries
assertEquals("Invalid result size for SSH root directory", 5, entries.size());
final LsEntry currentDir = entries.get(0);
final LsEntry files = entries.get(1);
final LsEntry pages = entries.get(2);
final LsEntry schema = entries.get(3);
final LsEntry components = entries.get(4);
// check names
assertEquals("Invalid current directory name", ".", currentDir.getFilename());
assertEquals("Invalid files directory name", "files", files.getFilename());
assertEquals("Invalid pages directory name", "pages", pages.getFilename());
assertEquals("Invalid schema directory name", "schema", schema.getFilename());
assertEquals("Invalid components directory name", "components", components.getFilename());
// check permissions
assertEquals("Invalid permissions on . directory", "dr--------", currentDir.getAttrs().getPermissionsString());
assertEquals("Invalid permissions on files directory", "drwxrwxr-x", files.getAttrs().getPermissionsString());
assertEquals("Invalid permissions on pages directory", "drwxrwxr-x", pages.getAttrs().getPermissionsString());
assertEquals("Invalid permissions on schema directory", "drwxrwxr-x", schema.getAttrs().getPermissionsString());
assertEquals("Invalid permissions on components directory", "drwxrwxr-x", components.getAttrs().getPermissionsString());
// check flags (?)
assertEquals("Invalid flags on . directory", 12, currentDir.getAttrs().getFlags());
assertEquals("Invalid flags on files directory", 12, files.getAttrs().getFlags());
assertEquals("Invalid flags on pages directory", 12, pages.getAttrs().getFlags());
assertEquals("Invalid flags on schema directory", 12, schema.getAttrs().getFlags());
assertEquals("Invalid flags on components directory", 12, components.getAttrs().getFlags());
// check size
assertEquals("Invalid size on . directory", 0, currentDir.getAttrs().getSize());
assertEquals("Invalid size on files directory", 0, files.getAttrs().getSize());
assertEquals("Invalid size on pages directory", 0, pages.getAttrs().getSize());
assertEquals("Invalid size on schema directory", 0, schema.getAttrs().getSize());
assertEquals("Invalid size on components directory", 0, components.getAttrs().getSize());
final String date = getDateStringDependingOnCurrentDayOfMonth();
// check string representation
assertEquals("Invalid string representation of . directory", "dr-------- 1 superadmin superadmin 0 " + date + " .", currentDir.getLongname());
assertEquals("Invalid string representation of files directory", "drwxrwxr-x 1 superadmin superadmin 0 " + date + " files", files.getLongname());
assertEquals("Invalid string representation of pages directory", "drwxrwxr-x 1 superadmin superadmin 0 " + date + " pages", pages.getLongname());
assertEquals("Invalid string representation of schema directory", "drwxrwxr-x 1 superadmin superadmin 0 " + date + " schema", schema.getLongname());
assertEquals("Invalid string representation of components directory", "drwxrwxr-x 1 superadmin superadmin 0 " + date + " components", components.getLongname());
sftp.disconnect();
} catch (SftpException ex) {
logger.warn("", ex);
fail("Unexpected exception: " + ex.getMessage());
}
}Example 35
| Project: muCommander-master File: SFTPFile.java View source code |
@SuppressWarnings("unchecked")
@Override
public AbstractFile[] ls() throws IOException {
// Retrieve a ConnectionHandler and lock it
SFTPConnectionHandler connHandler = (SFTPConnectionHandler) ConnectionPool.getConnectionHandler(connHandlerFactory, fileURL, true);
List<LsEntry> files = new ArrayList<LsEntry>();
try {
// Makes sure the connection is started, if not starts it
connHandler.checkConnection();
files = connHandler.channelSftp.ls(absPath);
} catch (SftpException e) {
e.printStackTrace();
} finally {
// Release the lock on the ConnectionHandler
connHandler.releaseLock();
}
int nbFiles = files.size();
// File doesn't exist, return an empty file array
if (nbFiles == 0)
return new AbstractFile[] {};
AbstractFile children[] = new AbstractFile[nbFiles];
FileURL childURL;
String filename;
int fileCount = 0;
String parentPath = fileURL.getPath();
if (!parentPath.endsWith(SEPARATOR))
parentPath += SEPARATOR;
// Fill AbstractFile array and discard '.' and '..' files
for (LsEntry file : files) {
filename = file.getFilename();
// Discard '.' and '..' files, dunno why these are returned
if (filename.equals(".") || filename.equals(".."))
continue;
childURL = (FileURL) fileURL.clone();
childURL.setPath(parentPath + filename);
children[fileCount++] = FileFactory.getFile(childURL, this, new SFTPFileAttributes(childURL, file.getAttrs()));
}
// Create new array of the exact file count
if (fileCount < nbFiles) {
AbstractFile newChildren[] = new AbstractFile[fileCount];
System.arraycopy(children, 0, newChildren, 0, fileCount);
return newChildren;
}
return children;
}Example 36
| Project: nifi-master File: SFTPTransfer.java View source code |
@Override
public int select(final LsEntry entry) {
final String entryFilename = entry.getFilename();
// files regardless of ignoring dot files
if (entryFilename.equals(".") || entryFilename.equals("..")) {
return LsEntrySelector.CONTINUE;
}
// ignoring them
if (ignoreDottedFiles && entryFilename.startsWith(".")) {
return LsEntrySelector.CONTINUE;
}
// if is a directory and we're supposed to recurse
if (recurse && entry.getAttrs().isDir()) {
subDirs.add(entry);
return LsEntrySelector.CONTINUE;
}
// FILE_FILTER_REGEX - then let's add it
if (!entry.getAttrs().isDir() && !entry.getAttrs().isLink() && isPathMatch) {
if (pattern == null || pattern.matcher(entryFilename).matches()) {
listing.add(newFileInfo(entry, path));
}
}
if (listing.size() >= maxResults) {
return LsEntrySelector.BREAK;
}
return LsEntrySelector.CONTINUE;
}Example 37
| Project: PTP-master File: FileTools.java View source code |
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ptp.remotetools.core.IRemoteFileTools#listItems(java.lang
* .String)
*/
@SuppressWarnings("unchecked")
public IRemoteItem[] listItems(final String root, IProgressMonitor monitor) throws RemoteOperationException, RemoteConnectionException, CancelException {
final SubMonitor subMon = SubMonitor.convert(monitor, 10);
try {
validateRemotePath(root);
Vector<LsEntry> files;
try {
SftpCallable<Vector<LsEntry>> c = new SftpCallable<Vector<LsEntry>>() {
@Override
public Vector<LsEntry> call() throws SftpException {
return getChannel().ls(root);
}
};
files = c.syncCmdInThread(Messages.FileTools_10, subMon.newChild(10));
} catch (IOException e) {
throw new RemoteOperationException(e);
} catch (SftpException e) {
throw new RemoteOperationException(e);
}
cacheUserData();
List<RemoteItem> result = new ArrayList<RemoteItem>();
Enumeration<LsEntry> enumeration = files.elements();
while (enumeration.hasMoreElements()) {
LsEntry entry = enumeration.nextElement();
String fileName = entry.getFilename();
String pathName = concatenateRemotePath(root, fileName);
if (//$NON-NLS-1$ //$NON-NLS-2$
fileName.equals(".") || fileName.equals("..")) {
// Ignore parent and current dir entry.
continue;
}
result.add(new RemoteItem(this, pathName, entry.getAttrs()));
}
IRemoteItem[] resultArray = new IRemoteItem[result.size()];
result.toArray(resultArray);
return resultArray;
} finally {
if (monitor != null) {
monitor.done();
}
}
}Example 38
| Project: voltdb-master File: ExportOnServerVerifier.java View source code |
@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);
}
}
}