diff options
author | Adrian Dudau <adrian.dudau@enea.com> | 2014-06-26 13:23:09 +0200 |
---|---|---|
committer | Adrian Dudau <adrian.dudau@enea.com> | 2014-06-26 13:24:09 +0200 |
commit | c7da892cb23d50d4d85746c9a0b6b14bf570989d (patch) | |
tree | e7136073f386d6156f51766c498c52c30c8df85f /plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/filesystem/OEFile.java | |
download | eclipse-poky-kepler-c7da892cb23d50d4d85746c9a0b6b14bf570989d.tar.gz |
initial commit for Enea Linux 4.0daisy
Migrated from the internal git server on the daisy-enea branch
Signed-off-by: Adrian Dudau <adrian.dudau@enea.com>
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/filesystem/OEFile.java')
-rw-r--r-- | plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/filesystem/OEFile.java | 375 |
1 files changed, 375 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/filesystem/OEFile.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/filesystem/OEFile.java new file mode 100644 index 0000000..afcd372 --- /dev/null +++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/ui/filesystem/OEFile.java | |||
@@ -0,0 +1,375 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2005, 2006 IBM Corporation, 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 | * IBM Corporation - initial API and implementation | ||
10 | * Ken Gilmer - adaptation from internal class. | ||
11 | * Ioana Grigoropol (Intel) - adapt class for remote support | ||
12 | *******************************************************************************/ | ||
13 | package org.yocto.bc.ui.filesystem; | ||
14 | |||
15 | import java.io.File; | ||
16 | import java.io.FileInputStream; | ||
17 | import java.io.FileNotFoundException; | ||
18 | import java.io.FileOutputStream; | ||
19 | import java.io.IOException; | ||
20 | import java.io.InputStream; | ||
21 | import java.io.OutputStream; | ||
22 | import java.net.URI; | ||
23 | import java.util.List; | ||
24 | |||
25 | import org.eclipse.core.filesystem.EFS; | ||
26 | import org.eclipse.core.filesystem.IFileInfo; | ||
27 | import org.eclipse.core.filesystem.IFileStore; | ||
28 | import org.eclipse.core.filesystem.IFileSystem; | ||
29 | import org.eclipse.core.filesystem.URIUtil; | ||
30 | import org.eclipse.core.filesystem.provider.FileInfo; | ||
31 | import org.eclipse.core.filesystem.provider.FileStore; | ||
32 | import org.eclipse.core.runtime.CoreException; | ||
33 | import org.eclipse.core.runtime.IPath; | ||
34 | import org.eclipse.core.runtime.IProgressMonitor; | ||
35 | import org.eclipse.core.runtime.IStatus; | ||
36 | import org.eclipse.core.runtime.MultiStatus; | ||
37 | import org.eclipse.core.runtime.NullProgressMonitor; | ||
38 | import org.eclipse.core.runtime.Status; | ||
39 | import org.eclipse.osgi.util.NLS; | ||
40 | import org.eclipse.rse.services.clientserver.messages.SystemMessageException; | ||
41 | import org.eclipse.rse.services.files.IFileService; | ||
42 | import org.eclipse.rse.services.files.IHostFile; | ||
43 | import org.yocto.bc.bitbake.BBSession; | ||
44 | import org.yocto.bc.bitbake.ProjectInfoHelper; | ||
45 | import org.yocto.bc.bitbake.ShellSession; | ||
46 | import org.yocto.bc.ui.Activator; | ||
47 | import org.yocto.bc.ui.model.ProjectInfo; | ||
48 | import org.yocto.bc.ui.model.YoctoHostFile; | ||
49 | |||
50 | /** | ||
51 | * File system implementation based on storage of files in the local | ||
52 | * operating system's file system. | ||
53 | */ | ||
54 | public class OEFile extends FileStore { | ||
55 | private static int attributes(File aFile) { | ||
56 | if (!aFile.exists() || aFile.canWrite()) | ||
57 | return EFS.NONE; | ||
58 | return EFS.ATTRIBUTE_READ_ONLY; | ||
59 | } | ||
60 | |||
61 | |||
62 | protected final YoctoHostFile file; | ||
63 | |||
64 | private List<Object> ignoredPaths; | ||
65 | |||
66 | /** | ||
67 | * The absolute file system path of the file represented by this store. | ||
68 | */ | ||
69 | protected final String filePath; | ||
70 | |||
71 | private final URI root; | ||
72 | |||
73 | /** | ||
74 | * Creates a new local file. | ||
75 | * | ||
76 | * @param file The file this local file represents | ||
77 | * @param root | ||
78 | */ | ||
79 | public OEFile(URI fileURI, List<Object> ignoredPaths, URI root, ProjectInfo projInfo, IProgressMonitor monitor) throws SystemMessageException { | ||
80 | this.ignoredPaths = ignoredPaths; | ||
81 | this.root = root; | ||
82 | this.file = new YoctoHostFile(projInfo, fileURI, monitor); | ||
83 | this.filePath = file.getAbsolutePath(); | ||
84 | } | ||
85 | |||
86 | @Override | ||
87 | public String[] childNames(int options, IProgressMonitor monitor) { | ||
88 | return file.getChildNames(monitor); | ||
89 | } | ||
90 | |||
91 | /* | ||
92 | * detect if the path is potential builddir | ||
93 | */ | ||
94 | private boolean isPotentialBuildDir(String path) { | ||
95 | String parentPath = path.substring(0, path.lastIndexOf("/")); | ||
96 | String name = path.substring(path.lastIndexOf("/") + 1); | ||
97 | boolean ret = true; | ||
98 | try { | ||
99 | IFileService fs = file.getFileService(); | ||
100 | IHostFile hostFile = fs.getFile(parentPath, name, new NullProgressMonitor()); | ||
101 | if (!hostFile.isDirectory()) | ||
102 | return false; | ||
103 | IHostFile confDir = fs.getFile(path, BBSession.CONF_DIR, new NullProgressMonitor()); | ||
104 | if (!confDir.exists() || !confDir.isDirectory()) | ||
105 | return false; | ||
106 | for (int i = 0; i < BBSession.BUILDDIR_INDICATORS.length && ret == true; i++) { | ||
107 | IHostFile child = fs.getFile(path + "/" + BBSession.CONF_DIR, BBSession.BUILDDIR_INDICATORS[i], new NullProgressMonitor()); | ||
108 | if(!child.exists() || !child.isFile()) { | ||
109 | ret = false; | ||
110 | break; | ||
111 | } | ||
112 | } | ||
113 | |||
114 | } catch (SystemMessageException e) { | ||
115 | e.printStackTrace(); | ||
116 | } | ||
117 | return ret; | ||
118 | } | ||
119 | |||
120 | /* | ||
121 | * try to find items for ignoreList | ||
122 | */ | ||
123 | private void updateIgnorePaths(String path, List<Object> list, IProgressMonitor monitor) { | ||
124 | if(isPotentialBuildDir(path)) { | ||
125 | BBSession config = null; | ||
126 | try { | ||
127 | config = Activator.getBBSession(Activator.getProjInfo(root), monitor); | ||
128 | config.initialize(); | ||
129 | } catch(Exception e) { | ||
130 | e.printStackTrace(); | ||
131 | return; | ||
132 | } | ||
133 | if (config.get("TMPDIR") == null || config.get("DL_DIR") == null || config.get("SSTATE_DIR") == null) { | ||
134 | //wrong guess about the buildDir | ||
135 | return; | ||
136 | }else { | ||
137 | if(!list.contains(config.get("TMPDIR"))) { | ||
138 | list.add(config.get("TMPDIR")); | ||
139 | } | ||
140 | if(!list.contains(config.get("DL_DIR"))) { | ||
141 | list.add(config.get("DL_DIR")); | ||
142 | } | ||
143 | if(!list.contains(config.get("SSTATE_DIR"))) { | ||
144 | list.add(config.get("SSTATE_DIR")); | ||
145 | } | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | |||
150 | @Override | ||
151 | public IFileStore[] childStores(int options, IProgressMonitor monitor) throws CoreException { | ||
152 | String[] children = childNames(options, monitor); | ||
153 | IFileStore[] wrapped = new IFileStore[children.length]; | ||
154 | |||
155 | for (int i = 0; i < wrapped.length; i++) { | ||
156 | String fullPath = file.getAbsolutePath() + "/" + children[i]; | ||
157 | updateIgnorePaths(fullPath, ignoredPaths, monitor); | ||
158 | if (ignoredPaths.contains(fullPath)) { | ||
159 | wrapped[i] = getDeadChild(children[i]); | ||
160 | } else { | ||
161 | wrapped[i] = getChild(children[i]); | ||
162 | } | ||
163 | } | ||
164 | |||
165 | return wrapped; | ||
166 | } | ||
167 | |||
168 | @Override | ||
169 | public void copy(IFileStore destFileStore, int options, IProgressMonitor monitor) throws CoreException { | ||
170 | if (destFileStore instanceof OEFile) { | ||
171 | file.copy(destFileStore, monitor); | ||
172 | } | ||
173 | } | ||
174 | |||
175 | @Override | ||
176 | public void delete(int options, IProgressMonitor monitor) throws CoreException { | ||
177 | if (monitor == null) | ||
178 | monitor = new NullProgressMonitor(); | ||
179 | else | ||
180 | monitor = new NullProgressMonitor(); | ||
181 | try { | ||
182 | monitor.beginTask(NLS.bind(Messages.deleting, this), 200); | ||
183 | String message = Messages.deleteProblem; | ||
184 | MultiStatus result = new MultiStatus(Policy.PI_FILE_SYSTEM, EFS.ERROR_DELETE, message, null); | ||
185 | |||
186 | //don't allow Eclipse to delete entire OE directory | ||
187 | |||
188 | if (!isProject()) { | ||
189 | internalDelete(file, filePath, result, monitor); | ||
190 | } | ||
191 | |||
192 | if (!result.isOK()) | ||
193 | throw new CoreException(result); | ||
194 | } finally { | ||
195 | monitor.done(); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | @Override | ||
200 | public boolean equals(Object obj) { | ||
201 | if (!(obj instanceof OEFile)) | ||
202 | return false; | ||
203 | |||
204 | OEFile otherFile = (OEFile) obj; | ||
205 | |||
206 | return file.equals(otherFile.file); | ||
207 | } | ||
208 | |||
209 | @Override | ||
210 | public IFileInfo fetchInfo(int options, IProgressMonitor monitor) { | ||
211 | //in-lined non-native implementation | ||
212 | FileInfo info = new FileInfo(file.getName()); | ||
213 | final long lastModified = file.getModifiedDate(); | ||
214 | if (lastModified <= 0) { | ||
215 | //if the file doesn't exist, all other attributes should be default values | ||
216 | info.setExists(false); | ||
217 | return info; | ||
218 | } | ||
219 | info.setLastModified(lastModified); | ||
220 | info.setExists(true); | ||
221 | info.setLength(file.getSize()); | ||
222 | info.setDirectory(file.isDirectory()); | ||
223 | info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, file.exists() && !file.canWrite()); | ||
224 | info.setAttribute(EFS.ATTRIBUTE_HIDDEN, file.isHidden()); | ||
225 | return info; | ||
226 | } | ||
227 | |||
228 | @Override | ||
229 | public IFileStore getChild(IPath path) { | ||
230 | try { | ||
231 | return new OEFile(file.getChildURIformPath(path), ignoredPaths, root, file.getProjectInfo(), new NullProgressMonitor()); | ||
232 | } catch (SystemMessageException e) { | ||
233 | e.printStackTrace(); | ||
234 | return null; | ||
235 | } | ||
236 | } | ||
237 | |||
238 | @Override | ||
239 | public IFileStore getChild(String name) { | ||
240 | |||
241 | try { | ||
242 | return new OEFile(file.getChildURI(name), ignoredPaths, root, file.getProjectInfo(), new NullProgressMonitor()); | ||
243 | } catch (SystemMessageException e) { | ||
244 | e.printStackTrace(); | ||
245 | } | ||
246 | return null; | ||
247 | |||
248 | } | ||
249 | |||
250 | private IFileStore getDeadChild(String name) { | ||
251 | return new OEIgnoreFile(file.getChildHostFile(name)); | ||
252 | } | ||
253 | |||
254 | /* | ||
255 | * (non-Javadoc) | ||
256 | * @see org.eclipse.core.filesystem.IFileStore#getFileSystem() | ||
257 | */ | ||
258 | @Override | ||
259 | public IFileSystem getFileSystem() { | ||
260 | return OEFileSystem.getInstance(); | ||
261 | } | ||
262 | |||
263 | @Override | ||
264 | public String getName() { | ||
265 | return file.getName(); | ||
266 | } | ||
267 | |||
268 | @Override | ||
269 | public IFileStore getParent() { | ||
270 | URI parentURI = file.getParentFile(); | ||
271 | try { | ||
272 | return parentURI == null ? null : new OEFile(parentURI, ignoredPaths, root, file.getProjectInfo(), new NullProgressMonitor()); | ||
273 | } catch (SystemMessageException e) { | ||
274 | e.printStackTrace(); | ||
275 | return null; | ||
276 | } | ||
277 | } | ||
278 | |||
279 | @Override | ||
280 | public int hashCode() { | ||
281 | return file.hashCode(); | ||
282 | } | ||
283 | |||
284 | /** | ||
285 | * Deletes the given file recursively, adding failure info to | ||
286 | * the provided status object. The filePath is passed as a parameter | ||
287 | * to optimize java.io.File object creation. | ||
288 | */ | ||
289 | private boolean internalDelete(YoctoHostFile target, String pathToDelete, MultiStatus status, IProgressMonitor monitor) { | ||
290 | target.delete(monitor); | ||
291 | return false; | ||
292 | } | ||
293 | |||
294 | @Override | ||
295 | public boolean isParentOf(IFileStore other) { | ||
296 | if (!(other instanceof OEFile)) | ||
297 | return false; | ||
298 | String thisPath = filePath; | ||
299 | String thatPath = ((OEFile) other).filePath; | ||
300 | int thisLength = thisPath.length(); | ||
301 | int thatLength = thatPath.length(); | ||
302 | //if equal then not a parent | ||
303 | if (thisLength >= thatLength) | ||
304 | return false; | ||
305 | if (getFileSystem().isCaseSensitive()) { | ||
306 | if (thatPath.indexOf(thisPath) != 0) | ||
307 | return false; | ||
308 | } else { | ||
309 | if (thatPath.toLowerCase().indexOf(thisPath.toLowerCase()) != 0) | ||
310 | return false; | ||
311 | } | ||
312 | //The common portion must end with a separator character for this to be a parent of that | ||
313 | return thisPath.charAt(thisLength - 1) == File.separatorChar || thatPath.charAt(thisLength) == File.separatorChar; | ||
314 | } | ||
315 | |||
316 | /** | ||
317 | * @return | ||
318 | */ | ||
319 | private boolean isProject() { | ||
320 | return this.file.toString().equals(root); | ||
321 | } | ||
322 | |||
323 | @Override | ||
324 | public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreException { | ||
325 | file.mkdir(options); | ||
326 | return this; | ||
327 | } | ||
328 | |||
329 | @Override | ||
330 | public void move(IFileStore destFile, int options, IProgressMonitor monitor) throws CoreException { | ||
331 | file.move(destFile, monitor); | ||
332 | } | ||
333 | |||
334 | @Override | ||
335 | public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException { | ||
336 | return file.getInputStream(options, monitor); | ||
337 | } | ||
338 | |||
339 | @Override | ||
340 | public OutputStream openOutputStream(int options, IProgressMonitor monitor) throws CoreException { | ||
341 | return file.getOutputStream(options, monitor); | ||
342 | } | ||
343 | |||
344 | @Override | ||
345 | public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) throws CoreException { | ||
346 | file.putInfo(info, options, monitor); | ||
347 | } | ||
348 | |||
349 | /* (non-Javadoc) | ||
350 | * @see org.eclipse.core.filesystem.provider.FileStore#toLocalFile(int, org.eclipse.core.runtime.IProgressMonitor) | ||
351 | */ | ||
352 | @Override | ||
353 | public File toLocalFile(int options, IProgressMonitor monitor) throws CoreException { | ||
354 | return file.toLocalFile(); | ||
355 | } | ||
356 | |||
357 | /* (non-Javadoc) | ||
358 | * @see org.eclipse.core.filesystem.IFileStore#toString() | ||
359 | */ | ||
360 | @Override | ||
361 | public String toString() { | ||
362 | return file.toString(); | ||
363 | } | ||
364 | |||
365 | /* (non-Javadoc) | ||
366 | * @see org.eclipse.core.filesystem.IFileStore#toURI() | ||
367 | */ | ||
368 | @Override | ||
369 | public URI toURI() { | ||
370 | return URIUtil.toURI(filePath); | ||
371 | } | ||
372 | public String getParentPath() { | ||
373 | return filePath.substring(0, filePath.lastIndexOf("/")); | ||
374 | } | ||
375 | } | ||