Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5dea3e520f5db13a5fe1292afe5aa8737d71f9bc (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.IStatus;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.protocol.IChannel;
import org.eclipse.tcf.services.IFileSystem;
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;

/**
 * FSDelete deletes the selected FSTreeNode list.
 */
public class OpDelete extends Operation {
	private static final int RETRY_TIMES = 3;
	//The nodes to be deleted.
	List<FSTreeNode> nodes;
	//The callback invoked to confirm deleting read-only files.
	IConfirmCallback confirmCallback;

	/**
	 * Create a delete operation using the specified nodes.
	 *
	 * @param nodes The nodes to be deleted.
	 */
	public OpDelete(List<FSTreeNode> nodes, IConfirmCallback confirmCallback) {
		this.nodes = getAncestors(nodes);
		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) {
						remove(node, service);
					}
				}
				else {
					String message = NLS.bind(Messages.Operation_NoFileSystemError, head.peerNode.getPeerId());
					throw new TCFFileSystemException(IStatus.ERROR, message);
				}
			}
		}
		catch (TCFException e) {
			throw new InvocationTargetException(e, e.getMessage());
		}
		finally {
			if (channel != null) Tcf.getChannelManager().closeChannel(channel);
			monitor.done();
		}
	}

	/**
	 * Delete the file/folder node using the file system service.
	 *
	 * @param monitor The monitor to report the progress.
	 * @param node The file/folder node to be deleted.
	 * @param service The file system service.
	 * @throws TCFFileSystemException The exception thrown during deleting.
	 * @throws InterruptedException Thrown when the operation is canceled.
	 */
	void remove(FSTreeNode node, IFileSystem service) throws TCFFileSystemException, InterruptedException {
		if (node.isFile()) {
			removeFile(node, service);
		}
		else if (node.isDirectory()) {
			removeFolder(node, service);
		}
	}

	/**
	 * Delete the folder node and its children using the file system service.
	 *
	 * @param monitor The monitor to report the progress.
	 * @param node The folder node to be deleted.
	 * @param service The file system service.
	 * @throws TCFFileSystemException The exception thrown during deleting.
	 * @throws InterruptedException Thrown when the operation is canceled.
	 */
	@Override
	protected void removeFolder(final FSTreeNode node, IFileSystem service) throws TCFFileSystemException, InterruptedException {
		List<FSTreeNode> children = getChildren(node, service);
		if (!children.isEmpty()) {
			for (FSTreeNode child : children) {
				// Delete each child node.
				remove(child, service);
			}
		}
		monitor.subTask(NLS.bind(Messages.OpDelete_RemovingFileFolder, node.name));
		super.removeFolder(node, service);
		monitor.worked(1);
	}

	/**
	 * Delete the file node using the file system service.
	 *
	 * @param node The file node to be deleted.
	 * @param service The file system service.
	 * @throws TCFFileSystemException The exception thrown during deleting.
	 * @throws InterruptedException Thrown when the operation is canceled.
	 */
	protected void removeFile(final FSTreeNode node, IFileSystem service) throws TCFFileSystemException, InterruptedException {
		if (monitor.isCanceled()) throw new InterruptedException();
		monitor.subTask(NLS.bind(Messages.OpDelete_RemovingFileFolder, node.name));
		// If the file is read only on windows or not writable on unix, then make it deletable.
		if (confirmCallback != null && confirmCallback.requires(node)) {
			if (!yes2All) {
				int result = confirmCallback.confirms(node);
				if (result == 1) {
					yes2All = true;
				}
				else if (result == 2) {
					monitor.worked(1);
					return;
				}
				else if (result == 3) {
					// Cancel the whole operation
					monitor.setCanceled(true);
					throw new InterruptedException();
				}
			}
			IStatus status = mkWritable(node);
			if (!status.isOK()) return;
		}
		super.removeFile(node, service);
		monitor.worked(1);
	}
	
	/**
	 * Make the file/folder writable by changing its properties.
	 * It will try several times before return.
	 * 
	 * @param node the file/folder node.
	 */
	private IStatus mkWritable(FSTreeNode node) {
		final FSTreeNode clone = (FSTreeNode) node.clone();
		if (node.isWindowsNode()) {
			clone.setReadOnly(false);
		}
		else {
			clone.setWritable(true);
		}
		// Make the file writable.
		OpCommitAttr op = new OpCommitAttr(node, clone.attr);
		IOpExecutor executor = new NullOpExecutor();
		IStatus status = null;
		for (int i = 0; i < RETRY_TIMES; i++) {
			status = executor.execute(op);
			if (status.isOK()) return status;
		}
		return status;
	}

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

	/*
	 * (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(IStatus.ERROR, 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