Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4c31e1b9659f786f8aa77066a4692737129a9a81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/****************************************************************************
 * Copyright (c) 2014 CohesionForce Inc
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     CohesionForce Inc - initial API and implementation
 *
 * SPDX-License-Identifier: EPL-2.0
 *****************************************************************************/

package org.eclipse.ecf.internal.provider.filetransfer.scp;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.Session;
import java.io.*;
import java.net.*;
import java.util.*;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.core.util.Proxy;
import org.eclipse.ecf.filetransfer.IRemoteFile;
import org.eclipse.ecf.filetransfer.IRemoteFileSystemListener;
import org.eclipse.ecf.filetransfer.identity.*;
import org.eclipse.ecf.provider.filetransfer.browse.AbstractFileSystemBrowser;
import org.eclipse.ecf.provider.filetransfer.browse.URLRemoteFile;
import org.eclipse.ecf.provider.filetransfer.identity.FileTransferNamespace;

/**
 * The ScpFileSystemBrowser uses the JCraft JSch package to run remote commands
 * using SSH to list files in a directory.
 *
 */
public class ScpFileSystemBrowser extends AbstractFileSystemBrowser implements
		IScpFileTransfer {

	protected InputStream inputStream;

	protected OutputStream outputStream;

	protected ScpUtil scpUtil;

	protected ChannelExec channel;
	protected String username;

	protected static final String SCP_EXEC = System.getProperty(
			"org.eclipse.ecf.filetransfer.scp.filebrowse.exec", "exec"); //$NON-NLS-1$
	protected static final String LS_START_COMMAND = System.getProperty(
			"org.eclipse.ecf.filetransfer.scp.filebrowse.lscommand.start",
			"IFS=\"$(printf '\n\t\')\"; shopt -s dotglob; for file in "); //$NON-NLS-1$; //$NON-NLS-1$
	protected static final String LS_END_COMMAND = System.getProperty(
			"org.eclipse.ecf.filetransfer.scp.filebrowse.lscommand.end",
			"/*; do stat --format='%F|%s|%Y|%n' $file; done "); //$NON-NLS-1$; //$NON-NLS-1$

	/**
	 * Constructor for creating a ScpFileSystemBrowser.
	 * 
	 * @param directoryOrFileID
	 *            - ID of the remote location to browse
	 * @param listener
	 *            - will be called asynchronously with events resulting from
	 *            file browsing.
	 * @param url
	 *            - URL of the parent directory to browse.
	 * @param connectContext
	 *            - contains username/password to use for the ssh connection
	 * @param proxy
	 *            - proxy to be used if set.
	 */
	public ScpFileSystemBrowser(IFileID directoryOrFileID,
			IRemoteFileSystemListener listener, URL url,
			IConnectContext connectContext, Proxy proxy) {
		super(directoryOrFileID, listener, url, connectContext, proxy);
		username = directoryOrFile.getUserInfo();
	}

	/**
	 * Method called from super class to build the list of remote files.
	 */
	protected void runRequest() throws Exception {
		scpUtil = new ScpUtil(this);
		final Session s = scpUtil.getSession();
		s.connect();
		if (s.isConnected()) {
			final String targetFileName = scpUtil
					.trimTargetFile(directoryOrFile.getPath());
			final String command = LS_START_COMMAND + targetFileName
					+ LS_END_COMMAND;
			channel = (ChannelExec) s.openChannel(SCP_EXEC);
			channel.setCommand(command);
			final OutputStream outs = channel.getOutputStream();
			inputStream = channel.getInputStream();
			channel.connect();

			setOutputStream(outs);

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inputStream));
			String line = reader.readLine();
			ArrayList strings = new ArrayList();
			while (line != null) {
				strings.add(line);
				line = reader.readLine();
			}
			List remoteFilesList = new ArrayList();
			for (int i = 0; i < strings.size(); i++) {
				try {
					remoteFilesList.add(createRemoteFile((String) strings
							.get(i)));
				} catch (Exception e) {
					Activator
							.getDefault()
							.log(new Status(
									IStatus.ERROR,
									Activator.PLUGIN_ID,
									IStatus.ERROR,
									"SCPFileBrowser could not convert string '" + ((String) strings.get(i)) + "' to FileID", e)); //$NON-NLS-1$
				}
			}
			remoteFiles = (IRemoteFile[]) remoteFilesList
					.toArray(new IRemoteFile[remoteFilesList.size()]);
		}
	}

	private IRemoteFile createRemoteFile(String string)
			throws URISyntaxException, FileCreateException, SecurityException {
		URLRemoteFile file = null;
		IFileID id = null;
		String[] parts = string.split("\\|");

		// Check to see if this string can be parsed
		if (parts.length < 4) {
			id = FileIDFactory.getDefault().createFileID(
					IDFactory.getDefault().getNamespaceByName(
							FileTransferNamespace.PROTOCOL), "scp://unknown");
			file = new URLRemoteFile(0, 0, id);
		} else {

			// Build the filename back up, since the filename may also contain
			// "|"
			// characters
			StringBuilder builder = new StringBuilder("scp://");
			for (int i = 3; i < parts.length; i++) {
				builder.append(parts[i]);

				// Put the | back into the name
				if (i > 3 && i < parts.length - 1) {
					builder.append("|");
				}
			}

			// If it's a directory, then make sure it ends with /
			if (parts[0].equals("directory")
					&& !builder.toString().endsWith("/")) {
				builder.append("/");
			} else if (!parts[0].equals("directory")
					&& builder.toString().endsWith("/")) {
				builder.deleteCharAt(builder.length() - 1);
			}

			String originalName = builder.toString();
			String formatedName = originalName.replace(" ", "%20");
			URI uri = new URI(formatedName);

			// Create the FileID
			id = FileIDFactory.getDefault().createFileID(
					IDFactory.getDefault().getNamespaceByName(
							FileTransferNamespace.PROTOCOL),
					new Object[] { uri });
			long size = Long.parseLong(parts[1]);
			long modification = Long.parseLong(parts[2]);
			file = new URLRemoteFile(modification, size, id);
		}
		return file;
	}

	protected void cleanUp() {
		super.cleanUp();
		if (channel != null) {
			channel.disconnect();
			channel = null;
		}
		if (scpUtil != null) {
			scpUtil.dispose();
			scpUtil = null;
		}
		try {
			if (inputStream != null)
				inputStream.close();
		} catch (final IOException e) {
			Activator.getDefault().log(
					new Status(IStatus.ERROR, Activator.PLUGIN_ID,
							IStatus.ERROR, "cleanup", e)); //$NON-NLS-1$
		}
		try {
			if (outputStream != null)
				outputStream.close();
		} catch (final IOException e) {
			Activator.getDefault().log(
					new Status(IStatus.ERROR, Activator.PLUGIN_ID,
							IStatus.ERROR, "cleanup", e)); //$NON-NLS-1$
		}
		inputStream = null;
		outputStream = null;
	}

	protected void setupProxy(Proxy proxy) {
		this.proxy = proxy;
		this.setupProxies();
	}

	protected void setInputStream(InputStream ins) {
		inputStream = ins;
	}

	protected void setOutputStream(OutputStream outs) {
		outputStream = outs;
	}

	public IConnectContext getConnectContext() {
		return connectContext;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Proxy getProxy() {
		return proxy;
	}

	public URL getTargetURL() {
		return directoryOrFile;
	}

	public Map getOptions() {
		return null;
	}

}

Back to the top