Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37e75790308f8947407e1f523581ae5d3485a78b (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*******************************************************************************
 * Copyright (c) 2014, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Marc-Andre Laperle (Ericsson) - Fix MinGW and Cygwin build (Bug 438476)
 *******************************************************************************/
package org.eclipse.cdt.remote.core;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Map;
import java.util.Properties;

import org.eclipse.cdt.core.CommandLauncher;
import org.eclipse.cdt.core.ICommandLauncher;
import org.eclipse.cdt.remote.internal.core.Activator;
import org.eclipse.cdt.remote.internal.core.messages.Messages;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.remote.core.IRemoteConnection;
import org.eclipse.remote.core.IRemoteConnectionType;
import org.eclipse.remote.core.IRemoteFileService;
import org.eclipse.remote.core.IRemoteProcess;
import org.eclipse.remote.core.IRemoteProcessBuilder;
import org.eclipse.remote.core.IRemoteProcessService;
import org.eclipse.remote.core.IRemoteResource;
import org.eclipse.remote.core.IRemoteServicesManager;
import org.eclipse.remote.core.RemoteProcessAdapter;

public class RemoteCommandLauncher implements ICommandLauncher {
	
	private static final String CYGWIN_PREFIX = "cygdrive"; //$NON-NLS-1$

	/**
	 * Convert a local (workspace) path into the remote equivalent. If the local path is not
	 * absolute, then do nothing.
	 * 
	 * e.g. Suppose the local path is /u/local_user/workspace/local_project/subdir1/subdir2
	 *      Suppose the remote project location is /home/remote_user/remote_project
	 *      Then the resulting path will be /home/remote_user/remote_project/subdir1/subdir2
	 * 
	 * @param localPath absolute local path in the workspace
	 * @param remote remote project
	 * @return remote path that is the equivalent of the local path
	 */
	public static String makeRemote(String local, IRemoteResource remote) {
		return makeRemote(new Path(local), remote).toString();
	}
	
	/**
	 * Convert a local (workspace) path into the remote equivalent. If the local path is not
	 * absolute, then do nothing.
	 * 
	 * e.g. Suppose the local path is /u/local_user/workspace/local_project/subdir1/subdir2
	 *      Suppose the remote project location is /home/remote_user/remote_project
	 *      Then the resulting path will be /home/remote_user/remote_project/subdir1/subdir2
	 * 
	 * @param localPath absolute local path in the workspace
	 * @param remote remote project
	 * @return remote path that is the equivalent of the local path
	 */
	public static IPath makeRemote(IPath localPath, IRemoteResource remote) {
		if (!localPath.isAbsolute()) {
			return localPath;
		}
		
		IPath remoteLocation = remote.getResource().getLocation();
		IPath remotePath = new Path(remote.getActiveLocationURI().getPath());
		
		// Device mismatch, we might be in the presence of Cygwin or MinGW
		if (remoteLocation.getDevice() != null && localPath.getDevice() == null) {
			boolean isCygwin = localPath.segment(0).equals(CYGWIN_PREFIX);
			remoteLocation = new Path(getPathString(remoteLocation, isCygwin));
			remotePath = new Path(getPathString(remotePath, isCygwin));
		}

		IPath relativePath = localPath.makeRelativeTo(remoteLocation);
		if (!relativePath.isEmpty()) {
			remotePath = remotePath.append(relativePath);
		}
		return remotePath;
	}
	
	private static String getPathString(IPath path, boolean isCygwin) {
		String s = path.toString();
		if (isCygwin) {
			s = s.replaceAll("^([a-zA-Z]):", "/" + CYGWIN_PREFIX + "/$1");  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
		} else {
			s = s.replaceAll("^([a-zA-Z]):", "/$1"); //$NON-NLS-1$ //$NON-NLS-2$
		}

		return s;
	}
	
	private final ICommandLauncher fLocalLauncher = new CommandLauncher();
	private boolean fShowCommand;
	private String[] fCommandArgs;
	private IRemoteConnection fConnection;
	private IRemoteProcess fRemoteProcess;
	private final Properties fEnvironment = new Properties();

	/**
	 * The number of milliseconds to pause between polling.
	 */
	private static final long DELAY = 50L;

	/**
	 * Constructs a command array that will be passed to the process
	 */
	private String[] constructCommandArray(String command, String[] commandArgs, IRemoteResource remote) {
		String[] args = new String[1 + commandArgs.length];
		args[0] = makeRemote(command, remote);
		for (int i = 0; i < commandArgs.length; i++) {
			args[i + 1] = makeRemote(commandArgs[i], remote);
		}
		return args;
	}

	@Override
	public Process execute(IPath commandPath, String[] args, String[] env, IPath workingDirectory, IProgressMonitor monitor)
			throws CoreException {
		if (getProject() != null) {
			IRemoteResource remRes = (IRemoteResource) getProject().getAdapter(IRemoteResource.class);
			if (remRes != null) {
				URI uri = remRes.getActiveLocationURI();
				IRemoteServicesManager remoteServicesManager = Activator.getService(IRemoteServicesManager.class);
				IRemoteConnectionType connectionType = remoteServicesManager.getConnectionType(uri);
				if (connectionType != null) {
					fConnection = connectionType.getConnection(uri);
					if (fConnection != null) {
						parseEnvironment(env);
						fCommandArgs = constructCommandArray(commandPath.toString(), args, remRes);
						IRemoteProcessService processService = fConnection.getService(IRemoteProcessService.class);
						IRemoteProcessBuilder processBuilder = processService.getProcessBuilder(fCommandArgs);
						if (workingDirectory != null) {
							String remoteWorkingPath = makeRemote(workingDirectory.toString(), remRes);
							IRemoteFileService fileManager = fConnection.getService(IRemoteFileService.class);
			                IFileStore wd = fileManager.getResource(remoteWorkingPath);
							processBuilder.directory(wd);
						}
						Map<String, String> processEnv = processBuilder.environment();
						for (String key : fEnvironment.stringPropertyNames()) {
							processEnv.put(key, fEnvironment.getProperty(key));
						}
						try {
							fRemoteProcess = processBuilder.start();
							return new RemoteProcessAdapter(fRemoteProcess);
						} catch (IOException e) {
							fLocalLauncher.setErrorMessage(e.getMessage());
							return null;
						}
					}
				}
			}
		}
		return fLocalLauncher.execute(commandPath, args, env, workingDirectory, monitor);
	}

	@Override
	public String[] getCommandArgs() {
		return fCommandArgs;
	}

	@Override
	public String getCommandLine() {
		return getCommandLine(fCommandArgs);
	}

	protected String getCommandLine(String[] commandArgs) {
		return getCommandLineQuoted(commandArgs, false);
	}

	@SuppressWarnings("nls")
	private String getCommandLineQuoted(String[] commandArgs, boolean quote) {
		String nl = System.getProperty("line.separator", "\n");
		if (fConnection != null) {
			nl = fConnection.getProperty(IRemoteConnection.LINE_SEPARATOR_PROPERTY);
		}
		StringBuffer buf = new StringBuffer();
		if (commandArgs != null) {
			for (String commandArg : commandArgs) {
				if (quote && (commandArg.contains(" ") || commandArg.contains("\"") || commandArg.contains("\\"))) {
					commandArg = '"' + commandArg.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"") + '"';
				}
				buf.append(commandArg);
				buf.append(' ');
			}
			buf.append(nl);
		}
		return buf.toString();
	}

	@Override
	public Properties getEnvironment() {
		return fEnvironment;
	}

	@Override
	public String getErrorMessage() {
		return fLocalLauncher.getErrorMessage();
	}

	@Override
	public IProject getProject() {
		return fLocalLauncher.getProject();
	}

	/**
	 * Parse array of "ENV=value" pairs to Properties.
	 */
	private void parseEnvironment(String[] env) {
		if (env != null) {
			fEnvironment.clear();
			for (String envStr : env) {
				// Split "ENV=value" and put in Properties
				int pos = envStr.indexOf('=');
				if (pos < 0) {
					pos = envStr.length();
				}
				String key = envStr.substring(0, pos);
				String value = envStr.substring(pos + 1);
				fEnvironment.put(key, value);
			}
		}
	}

	private void printCommandLine(OutputStream os) {
		if (os != null) {
			try {
				os.write(getCommandLineQuoted(getCommandArgs(), true).getBytes());
				os.flush();
			} catch (IOException e) {
				// ignore;
			}
		}
	}

	@Override
	public void setErrorMessage(String error) {
		fLocalLauncher.setErrorMessage(error);
	}

	@Override
	public void setProject(IProject project) {
		fLocalLauncher.setProject(project);
	}

	@Override
	public void showCommand(boolean show) {
		fLocalLauncher.showCommand(show);
		fShowCommand = show;
	}

	@Override
	public int waitAndRead(OutputStream out, OutputStream err) {
		if (fShowCommand) {
			printCommandLine(out);
		}

		if (fRemoteProcess == null) {
			return ILLEGAL_COMMAND;
		}

		RemoteProcessClosure closure = new RemoteProcessClosure(fRemoteProcess, out, err);
		closure.runBlocking();
		return OK;
	}

	@Override
	public int waitAndRead(OutputStream out, OutputStream err, IProgressMonitor monitor) {
		if (fShowCommand) {
			printCommandLine(out);
		}
		if (fRemoteProcess == null) {
			return ILLEGAL_COMMAND;
		}

		RemoteProcessClosure closure = new RemoteProcessClosure(fRemoteProcess, out, err);
		closure.runNonBlocking();
		while (!monitor.isCanceled() && closure.isAlive()) {
			try {
				Thread.sleep(DELAY);
			} catch (InterruptedException ie) {
				// ignore
			}
		}

		int state = OK;

		// Operation canceled by the user, terminate abnormally.
		if (monitor.isCanceled()) {
			closure.terminate();
			state = COMMAND_CANCELED;
			setErrorMessage(Messages.RemoteCommandLauncher_Command_canceled);
		}

		try {
			fRemoteProcess.waitFor();
		} catch (InterruptedException e) {
			// ignore
		}
		return state;
	}
}

Back to the top