summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/model/YoctoHostFile.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/model/YoctoHostFile.java')
-rw-r--r--plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/model/YoctoHostFile.java326
1 files changed, 326 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/model/YoctoHostFile.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/model/YoctoHostFile.java
new file mode 100644
index 0000000..7113efb
--- /dev/null
+++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/model/YoctoHostFile.java
@@ -0,0 +1,326 @@
1/*******************************************************************************
2 * Copyright (c) 2013 Intel Corporation.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Ioana Grigoropol(Intel) - initial API and implementation
10 *******************************************************************************/
11package org.yocto.bc.ui.model;
12
13import java.io.File;
14import java.io.InputStream;
15import java.io.OutputStream;
16import java.net.URI;
17import java.net.URISyntaxException;
18import java.util.ArrayList;
19
20import org.eclipse.core.filesystem.EFS;
21import org.eclipse.core.filesystem.IFileInfo;
22import org.eclipse.core.filesystem.IFileStore;
23import org.eclipse.core.runtime.CoreException;
24import org.eclipse.core.runtime.IPath;
25import org.eclipse.core.runtime.IProgressMonitor;
26import org.eclipse.core.runtime.NullProgressMonitor;
27import org.eclipse.osgi.util.NLS;
28import org.eclipse.rse.core.model.IHost;
29import org.eclipse.rse.services.clientserver.messages.SystemMessageException;
30import org.eclipse.rse.services.files.IFileService;
31import org.eclipse.rse.services.files.IHostFile;
32import org.yocto.bc.ui.filesystem.Messages;
33import org.yocto.bc.ui.filesystem.OEFile;
34import org.yocto.bc.ui.filesystem.Policy;
35import org.yocto.remote.utils.RemoteHelper;
36
37public class YoctoHostFile implements IHostFile{
38 private IHostFile file;
39 private final URI fileURI;
40 private ProjectInfo projectInfo;
41 private IFileService fileService;
42
43 public YoctoHostFile(ProjectInfo pInfo, URI fileURI, IProgressMonitor monitor) throws SystemMessageException {
44 this.projectInfo = pInfo;
45 this.fileURI = fileURI;
46 String path = fileURI.getPath();
47 fileService = projectInfo.getFileService(monitor);
48 file = RemoteHelper.getRemoteHostFile(projectInfo.getConnection(), path, monitor);
49 }
50
51 public YoctoHostFile(ProjectInfo projectInfo, URI uri) {
52 this.fileURI = uri;
53 this.projectInfo = projectInfo;
54 }
55
56 public IHostFile getFile() {
57 return file;
58 }
59 public void setFile(IHostFile file) {
60 this.file = file;
61 }
62 public ProjectInfo getProjectInfo() {
63 return projectInfo;
64 }
65 public void setProjectInfo(ProjectInfo projectInfo) {
66 this.projectInfo = projectInfo;
67 }
68 @Override
69 public String getAbsolutePath() {
70 return file.getAbsolutePath();
71 }
72 @Override
73 public String getName() {
74 return file.getName();
75 }
76 public URI getProjectLocationURI() {
77 return projectInfo.getOriginalURI();
78 }
79 public URI getLocationURI() {
80 projectInfo.getOriginalURI().getPath().indexOf(file.getAbsolutePath());
81 return projectInfo.getOriginalURI();
82 }
83 @Override
84 public boolean isDirectory() {
85 return file.isDirectory();
86 }
87 @Override
88 public String getParentPath() {
89 return file.getParentPath();
90 }
91 public boolean copy(IFileStore destFileStore, IProgressMonitor monitor) {
92 IHostFile destFile;
93 try {
94 OEFile oeFile = (OEFile)destFileStore;
95 String parentPath = oeFile.getParentPath();
96 destFile = fileService.createFile(parentPath, destFileStore.getName(), monitor);
97 fileService.copy(file.getParentPath(), file.getName(), destFile.getParentPath(), destFile.getName(), monitor);
98 } catch (SystemMessageException e) {
99 e.printStackTrace();
100 return false;
101 }
102 return true;
103 }
104 @Override
105 public boolean exists() {
106 return file.exists();
107 }
108 @Override
109 public boolean canRead() {
110 return file.canRead();
111 }
112 @Override
113 public boolean canWrite() {
114 return file.canWrite();
115 }
116 @Override
117 public long getModifiedDate() {
118 return file.getModifiedDate();
119 }
120 @Override
121 public long getSize() {
122 return file.getSize();
123 }
124 @Override
125 public boolean isArchive() {
126 return file.isArchive();
127 }
128 @Override
129 public boolean isFile() {
130 return file.isFile();
131 }
132 @Override
133 public boolean isHidden() {
134 return file.isHidden();
135 }
136 @Override
137 public boolean isRoot() {
138 return file.isRoot();
139 }
140 @Override
141 public void renameTo(String newName) {
142 file.renameTo(newName);
143 }
144 public URI getParentFile() {
145 if (file.getParentPath().isEmpty())
146 return null;
147 try {
148 return new URI(fileURI.getScheme(), fileURI.getHost(), file.getParentPath(), fileURI.getFragment());
149 } catch (URISyntaxException e) {
150 e.printStackTrace();
151 return null;
152 }
153 }
154 public boolean delete(IProgressMonitor monitor) {
155 try {
156 fileService.delete(file.getParentPath(), file.getName(), monitor);
157 } catch (SystemMessageException e) {
158 e.printStackTrace();
159 return false;
160 }
161 return true;
162 }
163
164 /**
165 * This method is called after a failure to modify a file or directory.
166 * Check to see if the parent is read-only and if so then
167 * throw an exception with a more specific message and error code.
168 *
169 * @param target The file that we failed to modify
170 * @param exception The low level exception that occurred, or <code>null</code>
171 * @throws CoreException A more specific exception if the parent is read-only
172 */
173 private void checkReadOnlyParent() throws CoreException {
174 String parent = file.getParentPath();
175 String parentOfParent = parent.substring(0, parent.lastIndexOf("/"));
176 IHostFile parentFile;
177 try {
178 parentFile = fileService.getFile(parentOfParent, parent, new NullProgressMonitor());
179 if (parentFile == null || !parentFile.canRead() || !parentFile.canWrite()) {
180 String message = NLS.bind(Messages.readOnlyParent, parent);
181 Policy.error(EFS.ERROR_PARENT_READ_ONLY, message, null);
182 }
183 } catch (SystemMessageException e) {
184 e.printStackTrace();
185 }
186
187 }
188
189 public void mkdir(int options) {
190 try {
191
192 if (!file.isDirectory()) {
193 file = fileService.createFolder(file.getParentPath(), file.getName(), new NullProgressMonitor());
194 if (!file.isDirectory()) {
195 checkReadOnlyParent();
196 String message = NLS.bind(Messages.failedCreateWrongType, file.getAbsolutePath());
197 Policy.error(EFS.ERROR_WRONG_TYPE, message);
198 }
199 }
200 } catch (SystemMessageException e1) {
201 e1.printStackTrace();
202 } catch (CoreException e) {
203 e.printStackTrace();
204 }
205
206 }
207
208 public String[] getChildNames(IProgressMonitor monitor) {
209 if (file.isDirectory()) {
210 IHostFile[] files;
211 try {
212 files = fileService.list(file.getAbsolutePath(), "*", IFileService.FILE_TYPE_FILES_AND_FOLDERS, monitor);
213 ArrayList<String> names = new ArrayList<String>();
214
215 for (IHostFile f : files) {
216 names.add(f.getName());
217 }
218
219 String[] arrNames = new String[names.size()];
220 names.toArray(arrNames);
221 return arrNames;
222 } catch (SystemMessageException e) {
223 e.printStackTrace();
224 }
225 }
226 return new String[]{};
227 }
228 public IHost getConnection() {
229 return projectInfo.getConnection();
230 }
231
232 public URI getChildURI(String name) {
233 try {
234 return new URI(fileURI.getScheme(), fileURI.getHost(), fileService.getFile(file.getAbsolutePath(), name, null).getAbsolutePath(), fileURI.getFragment());
235 } catch (URISyntaxException e) {
236 e.printStackTrace();
237 } catch (SystemMessageException e) {
238 e.printStackTrace();
239 }
240 return null;
241 }
242 public File toLocalFile() {
243 //TODO
244 //fileService.getFile(file.getParentPath(), file.getName(), null);
245 return null;
246 }
247 public URI toURI() {
248 return fileURI;
249 }
250 public YoctoHostFile getChildHostFile(String name) {
251 try {
252 return new YoctoHostFile(projectInfo, getChildURI(name), new NullProgressMonitor());
253 } catch (SystemMessageException e) {
254 e.printStackTrace();
255 return null;
256 }
257 }
258
259 public URI getChildURIformPath(IPath path) {
260 try {
261 String fileName = path.lastSegment();
262 path = path.removeLastSegments(1);
263 String newPath = fileService.getFile(file.getAbsolutePath() + "/" + path.toPortableString(), fileName, null).getAbsolutePath();
264 return new URI(fileURI.getScheme(), fileURI.getHost(), newPath, fileURI.getFragment());
265 } catch (URISyntaxException e) {
266 e.printStackTrace();
267 return null;
268 } catch (SystemMessageException e) {
269 e.printStackTrace();
270 return null;
271 }
272 }
273
274 public void move(IFileStore destFile, IProgressMonitor monitor) {
275 try {
276 fileService.move(file.getParentPath(), file.getName(), destFile.getParent().toURI().getPath(), destFile.getName(), monitor);
277 } catch (SystemMessageException e) {
278 e.printStackTrace();
279 }
280 }
281
282 public OutputStream getOutputStream(int options, IProgressMonitor monitor) {
283 try {
284 return fileService.getOutputStream(file.getParentPath(), file.getName(), options, monitor);
285 } catch (SystemMessageException e) {
286 e.printStackTrace();
287 return null;
288 }
289 }
290
291 public InputStream getInputStream(int options, IProgressMonitor monitor) {
292 try {
293 return fileService.getInputStream(file.getParentPath(), file.getName(), false, monitor);
294 } catch (SystemMessageException e) {
295 e.printStackTrace();
296 return null;
297 }
298 }
299
300 public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) {
301 try {
302 if ((options & EFS.SET_LAST_MODIFIED) != 0)
303 fileService.setLastModified(file.getParentPath(), file.getName(), info.getLastModified(), monitor);
304 } catch (SystemMessageException e) {
305 e.printStackTrace();
306 }
307 }
308
309 public IFileService getFileService() {
310 return fileService;
311 }
312
313 public void setFileService(IFileService fileService) {
314 this.fileService = fileService;
315 }
316
317 @Override
318 public String toString() {
319 URI uri = this.projectInfo.getOriginalURI();
320 try {
321 return new URI(uri.getScheme(), uri.getHost(), fileURI.getPath(), uri.getFragment()).toString();
322 } catch (URISyntaxException e) {
323 return fileURI.toString();
324 }
325 }
326}