summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/model/YoctoHostFile.java
blob: 7113efb26b22fbc732a88ba348522fa632980600 (plain)
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
/*******************************************************************************
 * Copyright (c) 2013 Intel Corporation.
 * 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:
 * Ioana Grigoropol(Intel) - initial API and implementation
 *******************************************************************************/
package org.yocto.bc.ui.model;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
import org.eclipse.rse.services.files.IFileService;
import org.eclipse.rse.services.files.IHostFile;
import org.yocto.bc.ui.filesystem.Messages;
import org.yocto.bc.ui.filesystem.OEFile;
import org.yocto.bc.ui.filesystem.Policy;
import org.yocto.remote.utils.RemoteHelper;

public class YoctoHostFile implements IHostFile{
	private IHostFile file;
	private final URI fileURI;
	private ProjectInfo projectInfo;
	private IFileService fileService;

	public YoctoHostFile(ProjectInfo pInfo, URI fileURI, IProgressMonitor monitor) throws SystemMessageException {
		this.projectInfo = pInfo;
		this.fileURI = fileURI;
		String path = fileURI.getPath();
		fileService = projectInfo.getFileService(monitor);
		file = RemoteHelper.getRemoteHostFile(projectInfo.getConnection(), path, monitor);
	}

	public YoctoHostFile(ProjectInfo projectInfo, URI uri) {
		this.fileURI = uri;
		this.projectInfo = projectInfo;
	}

	public IHostFile getFile() {
		return file;
	}
	public void setFile(IHostFile file) {
		this.file = file;
	}
	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}
	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}
	@Override
	public String getAbsolutePath() {
		return file.getAbsolutePath();
	}
	@Override
	public String getName() {
		return file.getName();
	}
	public URI getProjectLocationURI() {
		return projectInfo.getOriginalURI();
	}
	public URI getLocationURI() {
		projectInfo.getOriginalURI().getPath().indexOf(file.getAbsolutePath());
		return projectInfo.getOriginalURI();
	}
	@Override
	public boolean isDirectory() {
		return file.isDirectory();
	}
	@Override
	public String getParentPath() {
		return file.getParentPath();
	}
	public boolean copy(IFileStore destFileStore, IProgressMonitor monitor) {
		IHostFile destFile;
		try {
			OEFile oeFile = (OEFile)destFileStore;
			String parentPath = oeFile.getParentPath();
			destFile = fileService.createFile(parentPath, destFileStore.getName(), monitor);
			fileService.copy(file.getParentPath(), file.getName(), destFile.getParentPath(), destFile.getName(), monitor);
		} catch (SystemMessageException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	@Override
	public boolean exists() {
		return file.exists();
	}
	@Override
	public boolean canRead() {
		return file.canRead();
	}
	@Override
	public boolean canWrite() {
		return file.canWrite();
	}
	@Override
	public long getModifiedDate() {
		return file.getModifiedDate();
	}
	@Override
	public long getSize() {
		return file.getSize();
	}
	@Override
	public boolean isArchive() {
		return file.isArchive();
	}
	@Override
	public boolean isFile() {
		return file.isFile();
	}
	@Override
	public boolean isHidden() {
		return file.isHidden();
	}
	@Override
	public boolean isRoot() {
		return file.isRoot();
	}
	@Override
	public void renameTo(String newName) {
		file.renameTo(newName);
	}
	public URI getParentFile() {
		if (file.getParentPath().isEmpty())
			return null;
		try {
			return new URI(fileURI.getScheme(), fileURI.getHost(), file.getParentPath(), fileURI.getFragment());
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return null;
		}
	}
	public boolean delete(IProgressMonitor monitor) {
		try {
			fileService.delete(file.getParentPath(), file.getName(), monitor);
		} catch (SystemMessageException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * This method is called after a failure to modify a file or directory.
	 * Check to see if the parent is read-only and if so then
	 * throw an exception with a more specific message and error code.
	 *
	 * @param target The file that we failed to modify
	 * @param exception The low level exception that occurred, or <code>null</code>
	 * @throws CoreException A more specific exception if the parent is read-only
	 */
	private void checkReadOnlyParent() throws CoreException {
		String parent = file.getParentPath();
		String parentOfParent = parent.substring(0, parent.lastIndexOf("/"));
		IHostFile parentFile;
		try {
			parentFile = fileService.getFile(parentOfParent, parent, new NullProgressMonitor());
			if (parentFile == null || !parentFile.canRead() || !parentFile.canWrite()) {
				String message = NLS.bind(Messages.readOnlyParent, parent);
				Policy.error(EFS.ERROR_PARENT_READ_ONLY, message, null);
			}
		} catch (SystemMessageException e) {
			e.printStackTrace();
		}

	}

	public void mkdir(int options) {
		try {

			if (!file.isDirectory()) {
				file = fileService.createFolder(file.getParentPath(), file.getName(), new NullProgressMonitor());
				if (!file.isDirectory()) {
					checkReadOnlyParent();
					String message = NLS.bind(Messages.failedCreateWrongType, file.getAbsolutePath());
					Policy.error(EFS.ERROR_WRONG_TYPE, message);
				}
			}
		} catch (SystemMessageException e1) {
			e1.printStackTrace();
		} catch (CoreException e) {
			e.printStackTrace();
		}

	}

	public String[] getChildNames(IProgressMonitor monitor) {
		if (file.isDirectory()) {
			IHostFile[] files;
			try {
				files = fileService.list(file.getAbsolutePath(), "*", IFileService.FILE_TYPE_FILES_AND_FOLDERS, monitor);
				ArrayList<String> names = new ArrayList<String>();

				for (IHostFile f : files) {
					names.add(f.getName());
				}

				String[] arrNames = new String[names.size()];
				names.toArray(arrNames);
				return arrNames;
			} catch (SystemMessageException e) {
				e.printStackTrace();
			}
		}
		return  new String[]{};
	}
	public IHost getConnection() {
		return projectInfo.getConnection();
	}

	public URI getChildURI(String name) {
		try {
			return new URI(fileURI.getScheme(), fileURI.getHost(), fileService.getFile(file.getAbsolutePath(), name, null).getAbsolutePath(), fileURI.getFragment());
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (SystemMessageException e) {
			e.printStackTrace();
		}
		return null;
	}
	public File toLocalFile() {
		//TODO
		//fileService.getFile(file.getParentPath(), file.getName(), null);
		return null;
	}
	public URI toURI() {
		return fileURI;
	}
	public YoctoHostFile getChildHostFile(String name) {
		try {
			return new YoctoHostFile(projectInfo, getChildURI(name), new NullProgressMonitor());
		} catch (SystemMessageException e) {
			e.printStackTrace();
			return null;
		}
	}

	public URI getChildURIformPath(IPath path) {
		try {
			String fileName =  path.lastSegment();
			path = path.removeLastSegments(1);
			String newPath = fileService.getFile(file.getAbsolutePath() + "/" + path.toPortableString(), fileName, null).getAbsolutePath();
			return new URI(fileURI.getScheme(), fileURI.getHost(), newPath, fileURI.getFragment());
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return null;
		} catch (SystemMessageException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void move(IFileStore destFile, IProgressMonitor monitor) {
		try {
			fileService.move(file.getParentPath(), file.getName(), destFile.getParent().toURI().getPath(), destFile.getName(), monitor);
		} catch (SystemMessageException e) {
			e.printStackTrace();
		}
	}

	public OutputStream getOutputStream(int options, IProgressMonitor monitor) {
		try {
			return fileService.getOutputStream(file.getParentPath(), file.getName(), options, monitor);
		} catch (SystemMessageException e) {
			e.printStackTrace();
			return null;
		}
	}

	public InputStream getInputStream(int options, IProgressMonitor monitor) {
		try {
			return fileService.getInputStream(file.getParentPath(), file.getName(), false, monitor);
		} catch (SystemMessageException e) {
			e.printStackTrace();
			return null;
		}
	}

	public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) {
		try {
			if ((options & EFS.SET_LAST_MODIFIED) != 0)
				fileService.setLastModified(file.getParentPath(), file.getName(), info.getLastModified(), monitor);
		} catch (SystemMessageException e) {
			e.printStackTrace();
		}
	}

	public IFileService getFileService() {
		return fileService;
	}

	public void setFileService(IFileService fileService) {
		this.fileService = fileService;
	}

	@Override
	public String toString() {
		URI uri = this.projectInfo.getOriginalURI();
		try {
			return new URI(uri.getScheme(), uri.getHost(), fileURI.getPath(), uri.getFragment()).toString();
		} catch (URISyntaxException e) {
			return fileURI.toString();
		}
	}
}