Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cc5863d0060379e4711919d8ac110d085b8f8bb7 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
 * Copyright (c) 2011, 2015 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.core.internal.operations;

import static java.text.MessageFormat.format;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.services.IFileSystem.DoneOpen;
import org.eclipse.tcf.services.IFileSystem.DoneStat;
import org.eclipse.tcf.services.IFileSystem.FileAttrs;
import org.eclipse.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tcf.services.IFileSystem.IFileHandle;
import org.eclipse.tcf.te.tcf.core.concurrent.TCFOperationMonitor;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IConfirmCallback;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.FileState;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.PersistenceManager;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.utils.StatusHelper;
import org.eclipse.tcf.te.tcf.filesystem.core.nls.Messages;
import org.eclipse.tcf.util.TCFFileOutputStream;

/**
 * Upload multiple files from local system to a remote system.
 */
public class OpUpload extends AbstractOperation {
	private static class WorkItem {
		final File fSource;
		final FSTreeNode fDestination;
		final boolean fDropToDestination;
		WorkItem(File source, FSTreeNode destination, boolean isDrop) {
			fSource = source;
			fDestination = destination;
			fDropToDestination = isDrop;
		}
	}

	IConfirmCallback fConfirmCallback;

	LinkedList<WorkItem> fWork = new LinkedList<WorkItem>();
	private long fStartTime;

	public OpUpload(IConfirmCallback confirm) {
		fConfirmCallback = confirm;
	}

	public void addUpload(File source, FSTreeNode destinationFile) {
		fWork.add(new WorkItem(source, destinationFile, false));
	}

	public void addDrop(File source, FSTreeNode destiniationFolder) {
		fWork.add(new WorkItem(source, destiniationFolder, true));
	}

	@Override
	public IStatus doRun(IProgressMonitor monitor) {
		fStartTime = System.currentTimeMillis();
		monitor.beginTask(getName(), IProgressMonitor.UNKNOWN);
		while (!fWork.isEmpty()) {
			IStatus s = runWorkItem(fWork.remove(), monitor);
			if (!s.isOK())
				return s;
		}
		return Status.OK_STATUS;
	}

	protected IStatus runWorkItem(final WorkItem item, IProgressMonitor monitor) {
		final String path;
		final FSTreeNode destination = item.fDestination;
		FSTreeNode existing;
		final String name;
		final File source = item.fSource;
		if (item.fDropToDestination) {
			IStatus status = refresh(destination, fStartTime, monitor);
			if (!status.isOK())
				return status;

			name = item.fSource.getName();
			existing = destination.findChild(name);

			if (source.isDirectory()) {
				if (existing != null) {
					if (!existing.isDirectory()) {
						return StatusHelper.createStatus(format(Messages.OpCopy_error_noDirectory, existing.getLocation()), null);
					}
					int replace = confirmCallback(existing, fConfirmCallback);
					if (replace == IConfirmCallback.NO) {
						return Status.OK_STATUS;
					}
					if (replace != IConfirmCallback.YES) {
						return Status.CANCEL_STATUS;
					}
				} else {
					status = destination.operationNewFolder(name).run(new SubProgressMonitor(monitor, 0));
					if (!status.isOK())
						return status;
					existing = destination.findChild(name);
				}

				for (File child : source.listFiles()) {
					fWork.addFirst(new WorkItem(child, existing, true));
				}
				return Status.OK_STATUS;
			} else if (source.isFile()) {
				if (existing != null) {
					if (!existing.isFile()) {
						return StatusHelper.createStatus(format(Messages.OpCopy_error_noFile, existing.getLocation()), null);
					}
					int replace = confirmCallback(existing, fConfirmCallback);
					if (replace == IConfirmCallback.NO) {
						return Status.OK_STATUS;
					}
					if (replace != IConfirmCallback.YES) {
						return Status.CANCEL_STATUS;
					}
				}
				path = getPath(destination, name);
			} else {
				return Status.OK_STATUS;
			}
		} else {
			name = destination.getName();
			existing = destination;
			path = destination.getLocation(true);
		}

		final TCFOperationMonitor<OutputStream> result = new TCFOperationMonitor<OutputStream>();
		monitor.subTask(NLS.bind(Messages.OpUpload_UploadSingleFile, item.fSource));
		Protocol.invokeLater(new Runnable() {
			@Override
			public void run() {
				IFileSystem fs = destination.getRuntimeModel().getFileSystem();
				if (fs == null) {
					result.setCancelled();
				} else {
					tcfGetOutputStream(fs, path, result);
				}
			}
		});
		IStatus status = result.waitDone(monitor);
		if (!status.isOK())
			return status;

		OutputStream out = new BufferedOutputStream(result.getValue());
		try {
			IStatus s = uploadFile(item.fSource, existing, out, new SubProgressMonitor(monitor, 0));
			if (!s.isOK())
				return s;
		} finally {
			try {
				out.close();
			} catch (IOException e) {
			}
		}

		return updateNode(path, name, destination, existing, monitor);
	}

	private IStatus updateNode(final String path, final String name,
			final FSTreeNode destination, final FSTreeNode existing, IProgressMonitor monitor) {
		final TCFOperationMonitor<?> r2 = new TCFOperationMonitor<Object>();
		Protocol.invokeLater(new Runnable() {
			@Override
			public void run() {
				IFileSystem fs = destination.getRuntimeModel().getFileSystem();
				if (fs == null) {
					r2.setCancelled();
				} else if (!r2.checkCancelled()) {
					fs.stat(path, new DoneStat() {
						@Override
						public void doneStat(IToken token, FileSystemException error, FileAttrs attrs) {
							if (error != null) {
								r2.setError(format(Messages.OpUpload_error_upload, name), error);
							} else if (!r2.checkCancelled()) {
								if (existing != null) {
									existing.setAttributes(attrs, true);
								} else {
									destination.addNode(new FSTreeNode(destination, name, false, attrs), true);
								}
								r2.setDone(null);
							}
						}
					});
				}
			}
		});
		return r2.waitDone(monitor);
	}

	protected void tcfGetOutputStream(IFileSystem fileSystem, final String path, final TCFOperationMonitor<OutputStream> result) {
		int flags = IFileSystem.TCF_O_WRITE | IFileSystem.TCF_O_CREAT | IFileSystem.TCF_O_TRUNC;
		if (!result.checkCancelled()) {
			fileSystem.open(path, flags, null, new DoneOpen() {
				@Override
				public void doneOpen(IToken token, FileSystemException error, IFileHandle handle) {
					if (error != null) {
						result.setError(format(Messages.OpUpload_error_openFile, path), error);
					} else {
						result.setDone(new TCFFileOutputStream(handle));
					}
				}
			});
		}
	}

	private IStatus uploadFile(File source, FSTreeNode existing, OutputStream output, IProgressMonitor monitor) {
		byte[] data = new byte[DEFAULT_CHUNK_SIZE];
		// Calculate the total size.
		long totalSize = source.length();
		// Calculate the chunk size of one percent.
		int chunk_size = (int) totalSize / 100;
		// The current reading percentage.
		int percentRead = 0;
		// The current length of read bytes.
		long bytesRead = 0;
		MessageDigest digest = null;
		InputStream input = null;
		try {
			input = new BufferedInputStream(new FileInputStream(source));
			if (existing != null) {
				try {
					digest = MessageDigest.getInstance(MD_ALG);
					input = new DigestInputStream(input, digest);
				} catch (NoSuchAlgorithmException e) {
				}
			}

			// Total size displayed on the progress dialog.
			String fileLength = formatSize(totalSize);
			int length;
			while ((length = input.read(data)) >= 0) {
				output.write(data, 0, length);
				bytesRead += length;
				if (chunk_size != 0) {
					int percent = (int) bytesRead / chunk_size;
					if (percent != percentRead) { // Update the progress.
						monitor.worked(percent - percentRead);
						percentRead = percent; // Remember the percentage.
						// Report the progress.
						if (fWork.size() == 0)
							monitor.subTask(NLS.bind(Messages.OpUpload_UploadingProgress, new Object[]{source.getName(), formatSize(bytesRead), fileLength}));
					}
				}
				if (monitor.isCanceled())
					return Status.CANCEL_STATUS;
			}

			if (digest != null && existing != null) {
				statFile(existing, monitor);
				FileState filedigest = PersistenceManager.getInstance().getFileDigest(existing);
				filedigest.reset(digest.digest(), existing.getCacheFile().lastModified(), existing.getModificationTime());
			}
			return Status.OK_STATUS;
		} catch (IOException e) {
			return StatusHelper.createStatus(format(Messages.OpUpload_error_upload, source), e);
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (Exception e) {
				}
			}
		}
	}

	private void statFile(final FSTreeNode node, IProgressMonitor monitor) {
		final TCFOperationMonitor<?> result = new TCFOperationMonitor<Object>();
		Protocol.invokeLater(new Runnable() {
			@Override
			public void run() {
				tcfStat(node, result);
			}
		});
		result.waitDone(monitor);
	}

	protected void tcfStat(final FSTreeNode node, final TCFOperationMonitor<?> result) {
		if (!result.checkCancelled()) {
			final IFileSystem fs = node.getRuntimeModel().getFileSystem();
			if (fs == null) {
				result.setCancelled();
				return;
			}

			fs.stat(node.getLocation(true), new DoneStat() {
				@Override
				public void doneStat(IToken token, FileSystemException error, FileAttrs attrs) {
					if (error != null) {
						handleFSError(node, Messages.OpRefresh_errorReadAttributes, error, result);
					} else {
						node.setAttributes(attrs, false);
						result.setDone(null);
					}
				}
			});
		}
	}

	@Override
    public String getName() {
		String message;
		if(fWork.size()==1)
			message = NLS.bind(Messages.OpUpload_UploadSingleFile, fWork.element().fSource);
		else
			message = NLS.bind(Messages.OpUpload_UploadNFiles, Long.valueOf(fWork.size()));
		return message;
    }
}

Back to the top