Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f2caa63424a7cae7645cc08caeeaa68be6ca389d (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
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc.
 * 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.remote.core.operation;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IFileSystem;
import org.eclipse.tcf.services.IFileSystem.DirEntry;
import org.eclipse.tcf.services.IFileSystem.DoneClose;
import org.eclipse.tcf.services.IFileSystem.DoneOpen;
import org.eclipse.tcf.services.IFileSystem.DoneReadDir;
import org.eclipse.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tcf.services.IFileSystem.IFileHandle;
import org.eclipse.tcf.te.tcf.remote.core.TCFFileStore;
import org.eclipse.tcf.te.tcf.remote.core.operation.PeerInfo.DoneGetFileSystem;

public class TCFOperationChildStores extends TCFFileStoreOperation<IFileStore[]> {

	private final List<IFileStore> fFileStores = new ArrayList<IFileStore>();
    private IFileSystem fFileSystem;
	private IFileHandle fFileHandle;

	public TCFOperationChildStores(TCFFileStore filestore) {
	    super(filestore);
    }

    protected void setResult() {
    	setResult(fFileStores.toArray(new IFileStore[fFileStores.size()]));
    }

    protected final void setFileSystem(IFileSystem fileSystem) {
	    fFileSystem = fileSystem;
    }

    protected final void setFileHandle(IFileHandle fileHandle) {
	    fFileHandle = fileHandle;
    }

    @Override
    protected IFileStore[] waitForResult(SubMonitor sm) throws CoreException, InterruptedException, OperationCanceledException {
        return super.waitForResult(sm);
    }

	@Override
    protected void doExecute() {
		getFileSystem(new DoneGetFileSystem() {
			@Override
			public void done(final IFileSystem fileSystem, IStatus status) {
				if (shallAbort(status))
					return;

				setFileSystem(fileSystem);
				fileSystem.opendir(getPath(), new DoneOpen() {
					@Override
					public void doneOpen(IToken token, FileSystemException error, IFileHandle handle) {
						if (shallAbort(error))
							return;

						setFileHandle(handle);
						readDir();
					}
				});
			}
		});
	}

	protected void readDir() {
		fFileSystem.readdir(fFileHandle, new DoneReadDir() {
			@Override
			public void doneReadDir(IToken token, FileSystemException error, DirEntry[] entries, boolean eof) {
				if (shallAbort(error)) {
					closeHandle();
					return;
				}

				for (DirEntry dirEntry : entries) {
					createFileStore(dirEntry);
				}
				if (eof) {
					closeHandle();
					setResult();
				} else {
					readDir();
				}
			}
		});
    }

	protected void createFileStore(DirEntry dirEntry) {
		IFileStore child = getFileStore().getChild(dirEntry.filename);
		if (child instanceof TCFFileStore) {
			((TCFFileStore) child).setAttributes(dirEntry.attrs);
		}
		fFileStores.add(child);
    }

	protected void closeHandle() {
		if (fFileSystem != null && fFileHandle != null) {
			fFileSystem.close(fFileHandle, new DoneClose() {
				@Override
				public void doneClose(IToken token, FileSystemException error) {
				}
			});
			fFileSystem = null;
			fFileHandle = null;
		}
	}
}

Back to the top