Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6cf6219b710cb760ebbac1dd38491d7fe50247f1 (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
/*******************************************************************************
 * Copyright (c) 2011 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 java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.services.IFileSystem.DoneCopy;
import org.eclipse.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tcf.te.tcf.core.Tcf;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IConfirmCallback;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.exceptions.TCFException;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.exceptions.TCFFileSystemException;
import org.eclipse.tcf.te.tcf.filesystem.core.model.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.nls.Messages;

/**
 * The operation class that copies selected FSTreeNodes to a specify destination folder.
 */
public class OpCopy extends Operation {
	// The nodes to be copied.
	List<FSTreeNode> nodes;
	// The destination folder to be copied to.
	FSTreeNode dest;
	// The callback invoked to confirm overwriting when there're files with same names.
	IConfirmCallback confirmCallback;
	// If it is required to copy the permissions.
	boolean cpPermission;
	// If it is required to copy the ownership.
	boolean cpOwnership;
	
	/**
	 * Create a copy operation using the specified nodes and destination folder.
	 * 
	 * @param nodes The file/folder nodes to be copied.
	 * @param dest The destination folder to be copied to.
	 */
	public OpCopy(List<FSTreeNode> nodes, FSTreeNode dest) {
		this(nodes, dest, false, false, null);
	}

	/**
	 * Create a copy operation using the specified nodes and destination folder,
	 * using the specified flags of copying permissions and ownership and a callback
	 * to confirm to overwrite existing files.
	 *
	 * @param nodes The file/folder nodes to be copied.
	 * @param dest The destination folder to be copied to.
	 */
	public OpCopy(List<FSTreeNode> nodes, FSTreeNode dest, boolean cpPerm, boolean cpOwn, IConfirmCallback confirmCallback) {
		super();
		this.nodes = getAncestors(nodes);
		this.dest = dest;
		this.cpOwnership = cpOwn;
		this.cpPermission = cpPerm;
		this.confirmCallback = confirmCallback;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.internal.operations.Operation#run(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
		super.run(monitor);
		FSTreeNode head = nodes.get(0);
		IChannel channel = null;
		try {
			channel = openChannel(head.peerNode.getPeer());
			if (channel != null) {
				IFileSystem service = getBlockingFileSystem(channel);
				if (service != null) {
					for (FSTreeNode node : nodes) {
						// Iterate the nodes and copy each of them to the destination folder.
						copyNode(service, node, dest);
					}
				}
				else {
					String message = NLS.bind(Messages.Operation_NoFileSystemError, head.peerNode.getPeerId());
					throw new TCFFileSystemException(message);
				}
			}
		}
		catch (TCFException e) {
			throw new InvocationTargetException(e, e.getMessage());
		}
		finally {
			if (channel != null) Tcf.getChannelManager().closeChannel(channel);
			monitor.done();
		}
	}

	/**
	 * Copy the file/folder represented by the specified node to the destination folder.
	 *
	 * @param service The file system service to do the remote copying.
	 * @param node The file/folder node to be copied.
	 * @param dest The destination folder.
	 * @throws TCFFileSystemException The exception thrown during copying
	 * @throws InterruptedException The exception thrown when the operation is canceled.
	 */
	void copyNode(IFileSystem service, FSTreeNode node, FSTreeNode dest) throws TCFFileSystemException, InterruptedException {
		if (node.isFile()) {
			copyFile(service, node, dest);
		}
		else if (node.isDirectory()) {
			copyFolder(service, node, dest);
		}
	}

	/**
	 * Copy the folder represented by the specified node to the destination folder.
	 *
	 * @param service The file system service to do the remote copying.
	 * @param node The folder node to be copied.
	 * @param dest The destination folder.
	 * @throws TCFFileSystemException The exception thrown during copying
	 * @throws InterruptedException The exception thrown when the operation is canceled.
	 */
	private void copyFolder(IFileSystem service, FSTreeNode node, FSTreeNode dest) throws TCFFileSystemException, InterruptedException {
		if (monitor.isCanceled()) throw new InterruptedException();
		FSTreeNode copy = findChild(service, dest, node.name);
		if (copy == null) {
			// If no existing directory with the same name, create it.
			copy = (FSTreeNode) node.clone();
			addChild(service, dest, copy);
			mkdir(service, copy);
			copyChildren(service, node, copy);
		}
		else if (node.equals(copy)) {
			copy = createCopyDestination(service, node, dest);
			mkdir(service, copy);
			copyChildren(service, node, copy);
		}
		else if (confirmReplace(node, confirmCallback)) {
			copyChildren(service, node, copy);
		}
		monitor.worked(1);
	}

	/**
	 * Copy the children of the node to the destination folder.
	 *
	 * @param service The file system service to do the remote copying.
	 * @param node The folder node to be copied.
	 * @param dest The destination folder.
	 * @throws TCFFileSystemException The exception thrown during copying
	 * @throws InterruptedException The exception thrown when the operation is canceled.
	 */
	private void copyChildren(IFileSystem service, FSTreeNode node, FSTreeNode dest) throws TCFFileSystemException, InterruptedException {
	    List<FSTreeNode> children = getChildren(node, service);
	    if (!children.isEmpty()) {
	    	for (FSTreeNode child : children) {
	    		// Iterate and copy its children nodes.
	    		copyNode(service, child, dest);
	    	}
	    }
    }

	/**
	 * Copy the file represented by the specified node to the destination folder.
	 *
	 * @param service The file system service to do the remote copying.
	 * @param node The file node to be copied.
	 * @param dest The destination folder.
	 * @throws TCFFileSystemException The exception thrown during copying
	 * @throws InterruptedException The exception thrown when the operation is canceled.
	 */
	private void copyFile(IFileSystem service, FSTreeNode node, FSTreeNode dest) throws TCFFileSystemException, InterruptedException {
		if (monitor.isCanceled()) throw new InterruptedException();
		monitor.subTask(NLS.bind(Messages.OpCopy_Copying, node.name));
		// Create the copy target file
		final FSTreeNode copy = createCopyDestination(service, node, dest);
		String src_path = node.getLocation(true);
		String dst_path = copy.getLocation(true);
		final TCFFileSystemException[] errors = new TCFFileSystemException[1];
		// Get the options of copy permission and ownership.
		service.copy(src_path, dst_path, cpPermission, cpOwnership, new DoneCopy() {
			@Override
			public void doneCopy(IToken token, FileSystemException error) {
				if (error != null) {
					String message = NLS.bind(Messages.OpCopy_CannotCopyFile, copy.name, error);
					errors[0] = new TCFFileSystemException(message, error);
				}
			}
		});
		if (errors[0] != null) {
			removeChild(service, dest, copy);
			throw errors[0];
		}
		monitor.worked(1);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#getName()
	 */
	@Override
    public String getName() {
	    return Messages.OpCopy_CopyingFile;
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.filesystem.core.interfaces.IOperation#getTotalWork()
	 */
	@Override
    public int getTotalWork() {
		if(nodes != null && !nodes.isEmpty()) {
			final AtomicReference<Integer> ref = new AtomicReference<Integer>();
			SafeRunner.run(new ISafeRunnable(){
				@Override
                public void handleException(Throwable exception) {
					// Ignore on purpose.
                }
				@Override
                public void run() throws Exception {
					FSTreeNode head = nodes.get(0);
					IChannel channel = null;
					try {
						channel = openChannel(head.peerNode.getPeer());
						if (channel != null) {
							IFileSystem service = getBlockingFileSystem(channel);
							if (service != null) {
								ref.set(Integer.valueOf(count(service, nodes)));
							}
							else {
								String message = NLS.bind(Messages.Operation_NoFileSystemError, head.peerNode.getPeerId());
								throw new TCFFileSystemException(message);
							}
						}
					}
					finally {
						if (channel != null) Tcf.getChannelManager().closeChannel(channel);
					}
                }});
			Integer value = ref.get();
			return value == null ? IProgressMonitor.UNKNOWN : value.intValue();
		}
		return IProgressMonitor.UNKNOWN;
    }
}

Back to the top