Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f4402aa5046feeac556a020a23825b7f2166a336 (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
/*******************************************************************************
 * 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 org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.services.IFileSystem.DoneClose;
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.filesystem.core.internal.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.nls.Messages;

/**
 * The file operation class to create a file in the file system of Target Explorer.
 */
public class OpCreateFile extends OpCreate {

	public OpCreateFile(FSTreeNode folder, String name) {
		super(folder, name);
	}

	@Override
	protected void tcfCreate(final FSTreeNode destination, final String name, final TCFResult<FSTreeNode> result) {
		Assert.isTrue(Protocol.isDispatchThread());
		if (result.checkCancelled())
			return;

		final String path = getPath(destination, name);
		final IFileSystem fileSystem = destination.getRuntimeModel().getFileSystem();
		if (fileSystem == null) {
			result.setCancelled();
			return;
		}

		fileSystem.open(path, IFileSystem.TCF_O_WRITE | IFileSystem.TCF_O_CREAT | IFileSystem.TCF_O_TRUNC, null, new DoneOpen() {
			@Override
			public void doneOpen(IToken token, FileSystemException error, IFileHandle hdl) {
				if (error != null) {
					result.setError(format(Messages.OpCreateFile_error_create, path), error);
				} else if (!result.checkCancelled()) {
					fileSystem.close(hdl, new DoneClose() {
						@Override
						public void doneClose(IToken token, FileSystemException error) {
							if (error != null) {
								result.setError(format(Messages.OpCreateFile_error_create, path), error);
							} else if (!result.checkCancelled()) {
								fileSystem.stat(path, new DoneStat() {
									@Override
									public void doneStat(IToken token, FileSystemException error, FileAttrs attrs) {
										if (error != null) {
											result.setError(format(Messages.OpCreateFile_error_create, path), error);
										} else if (!result.checkCancelled()) {
											FSTreeNode node = new FSTreeNode(destination, name, false, attrs);
											destination.addNode(node, true);
											result.setDone(node);
										}
									}
								});
							}
						}
					});
				}
			}
		});
	}
}

Back to the top