Commit | Line | Data |
---|---|---|
f17bb886 MAL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Marc-Andre Laperle - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.InputStream; | |
17 | import java.lang.reflect.InvocationTargetException; | |
18 | import java.util.ArrayList; | |
058f8b3f | 19 | import java.util.Arrays; |
f17bb886 MAL |
20 | import java.util.HashMap; |
21 | import java.util.Iterator; | |
22 | import java.util.LinkedList; | |
23 | import java.util.List; | |
24 | import java.util.ListIterator; | |
25 | import java.util.Map; | |
26 | ||
fc4a373f BH |
27 | import org.eclipse.core.filesystem.EFS; |
28 | import org.eclipse.core.filesystem.IFileInfo; | |
f17bb886 MAL |
29 | import org.eclipse.core.resources.IFile; |
30 | import org.eclipse.core.resources.IFolder; | |
31 | import org.eclipse.core.resources.IResource; | |
32 | import org.eclipse.core.resources.ResourcesPlugin; | |
33 | import org.eclipse.core.runtime.CoreException; | |
34 | import org.eclipse.core.runtime.IPath; | |
35 | import org.eclipse.core.runtime.IProgressMonitor; | |
36 | import org.eclipse.core.runtime.IStatus; | |
37 | import org.eclipse.core.runtime.Path; | |
38 | import org.eclipse.core.runtime.Status; | |
39 | import org.eclipse.core.runtime.SubMonitor; | |
40 | import org.eclipse.core.runtime.SubProgressMonitor; | |
41 | import org.eclipse.core.runtime.URIUtil; | |
42 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
43 | import org.eclipse.jface.operation.ModalContext; | |
44 | import org.eclipse.swt.widgets.Shell; | |
45 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; | |
46 | import org.eclipse.tracecompass.tmf.core.TmfCommonConstants; | |
47 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceImportException; | |
48 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType; | |
49 | import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper; | |
50 | import org.eclipse.tracecompass.tmf.core.util.Pair; | |
51 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder; | |
52 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceTypeUIUtils; | |
53 | import org.eclipse.tracecompass.tmf.ui.project.model.TraceUtils; | |
54 | import org.eclipse.ui.dialogs.FileSystemElement; | |
55 | import org.eclipse.ui.dialogs.IOverwriteQuery; | |
56 | import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; | |
57 | import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider; | |
58 | import org.eclipse.ui.wizards.datatransfer.ImportOperation; | |
59 | ||
60 | /** | |
61 | * An operation that performs validation and importing of traces. Its primary | |
ee9e8924 MK |
62 | * inputs are a collection of TraceFileSystemElement and several flags that |
63 | * control | |
f17bb886 MAL |
64 | * |
65 | */ | |
66 | public class TraceValidateAndImportOperation implements IRunnableWithProgress { | |
67 | ||
68 | private static final String TRACE_IMPORT_TEMP_FOLDER = ".traceImport"; //$NON-NLS-1$ | |
69 | ||
70 | private String fTraceType; | |
71 | private IPath fDestinationContainerPath; | |
72 | private IPath fBaseSourceContainerPath; | |
73 | private boolean fImportFromArchive; | |
74 | private int fImportOptionFlags; | |
75 | private Shell fShell; | |
76 | private TmfTraceFolder fTraceFolderElement; | |
77 | private List<TraceFileSystemElement> fSelectedFileSystemElements; | |
78 | ||
79 | private IStatus fStatus; | |
80 | private ImportConflictHandler fConflictHandler; | |
81 | private String fCurrentPath; | |
82 | ||
83 | private List<IResource> fImportedResources; | |
84 | ||
85 | /** | |
86 | * Constructs a new validate and import operation. | |
87 | * | |
88 | * @param shell | |
89 | * the parent shell to use for possible error messages | |
90 | * @param traceFileSystemElements | |
91 | * the trace file elements to import | |
92 | * @param traceId | |
93 | * the trace type to import the traces as (can be set to null for | |
94 | * automatic detection) | |
95 | * @param baseSourceContainerPath | |
96 | * the path to the container of the source. This is used as a | |
97 | * "base" to generate the folder structure for the | |
98 | * "preserve folder structure" option. | |
99 | * @param destinationContainerPath | |
100 | * the destination path of the import operation, typically a | |
101 | * trace folder path. | |
102 | * @param importFromArchive | |
103 | * whether or not the source is an archive | |
104 | * @param importOptionFlags | |
105 | * bit-wise 'or' of import option flag constants ( | |
106 | * {@link ImportTraceWizardPage#OPTION_PRESERVE_FOLDER_STRUCTURE} | |
107 | * , | |
108 | * {@link ImportTraceWizardPage#OPTION_CREATE_LINKS_IN_WORKSPACE} | |
109 | * , | |
110 | * {@link ImportTraceWizardPage#OPTION_IMPORT_UNRECOGNIZED_TRACES} | |
111 | * , and | |
112 | * {@link ImportTraceWizardPage#OPTION_OVERWRITE_EXISTING_RESOURCES} | |
113 | * ) | |
114 | * @param traceFolderElement | |
115 | * the destination trace folder of the import operation. | |
116 | */ | |
ee9e8924 MK |
117 | public TraceValidateAndImportOperation(Shell shell, List<TraceFileSystemElement> traceFileSystemElements, String traceId, IPath baseSourceContainerPath, IPath destinationContainerPath, boolean importFromArchive, int importOptionFlags, |
118 | TmfTraceFolder traceFolderElement) { | |
f17bb886 MAL |
119 | fTraceType = traceId; |
120 | fBaseSourceContainerPath = baseSourceContainerPath; | |
121 | fDestinationContainerPath = destinationContainerPath; | |
122 | fImportOptionFlags = importOptionFlags; | |
123 | fImportFromArchive = importFromArchive; | |
124 | fShell = shell; | |
125 | fTraceFolderElement = traceFolderElement; | |
126 | ||
127 | boolean overwriteExistingResources = (importOptionFlags & ImportTraceWizardPage.OPTION_OVERWRITE_EXISTING_RESOURCES) != 0; | |
128 | if (overwriteExistingResources) { | |
ee9e8924 | 129 | setConflictHandler(new ImportConflictHandler(fShell, fTraceFolderElement, ImportConfirmation.OVERWRITE_ALL)); |
f17bb886 | 130 | } else { |
ee9e8924 | 131 | setConflictHandler(new ImportConflictHandler(fShell, fTraceFolderElement, ImportConfirmation.SKIP)); |
f17bb886 MAL |
132 | } |
133 | fImportedResources = new ArrayList<>(); | |
134 | fSelectedFileSystemElements = traceFileSystemElements; | |
135 | } | |
136 | ||
137 | @Override | |
138 | public void run(IProgressMonitor progressMonitor) { | |
139 | try { | |
140 | ||
141 | final List<TraceFileSystemElement> selectedFileSystemElements = fSelectedFileSystemElements; | |
142 | ||
143 | // List fileSystemElements will be filled using the | |
144 | // passThroughFilter | |
145 | SubMonitor subMonitor = SubMonitor.convert(progressMonitor, 1); | |
146 | ||
147 | // Check if operation was cancelled. | |
148 | ModalContext.checkCanceled(subMonitor); | |
149 | ||
150 | // Temporary directory to contain any extracted files | |
151 | IFolder destTempFolder = fTraceFolderElement.getProject().getResource().getFolder(TRACE_IMPORT_TEMP_FOLDER); | |
152 | if (destTempFolder.exists()) { | |
153 | SubProgressMonitor monitor = new SubProgressMonitor(subMonitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK); | |
154 | destTempFolder.delete(true, monitor); | |
155 | } | |
156 | SubProgressMonitor monitor = new SubProgressMonitor(subMonitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK); | |
157 | destTempFolder.create(IResource.HIDDEN, true, monitor); | |
158 | ||
159 | subMonitor = SubMonitor.convert(progressMonitor, 2); | |
160 | String baseSourceLocation = null; | |
161 | if (fImportFromArchive) { | |
162 | // When importing from archive, we first extract the | |
163 | // *selected* files to a temporary folder then create new | |
164 | // TraceFileSystemElements | |
165 | ||
166 | SubMonitor archiveMonitor = SubMonitor.convert(subMonitor.newChild(1), 2); | |
167 | ||
168 | // Extract selected files from source archive to temporary | |
169 | // folder | |
170 | extractArchiveContent(selectedFileSystemElements.iterator(), destTempFolder, archiveMonitor.newChild(1)); | |
171 | ||
172 | if (!selectedFileSystemElements.isEmpty()) { | |
173 | // Even if the files were extracted to temporary folder, they | |
174 | // have to look like they originate from the source archive | |
175 | baseSourceLocation = getRootElement(selectedFileSystemElements.get(0)).getSourceLocation(); | |
176 | // Extract additional archives contained in the extracted files | |
177 | // (archives in archives) | |
178 | List<TraceFileSystemElement> tempFolderFileSystemElements = createElementsForFolder(destTempFolder); | |
179 | extractAllArchiveFiles(tempFolderFileSystemElements, destTempFolder, destTempFolder.getLocation(), archiveMonitor.newChild(1)); | |
180 | } | |
181 | } else { | |
182 | SubMonitor directoryMonitor = SubMonitor.convert(subMonitor.newChild(1), 2); | |
ee9e8924 | 183 | // Import selected files, excluding archives (done in a later step) |
f17bb886 MAL |
184 | importFileSystemElements(directoryMonitor.newChild(1), selectedFileSystemElements); |
185 | ||
ee9e8924 | 186 | // Extract archives in selected files (if any) to temporary folder |
f17bb886 MAL |
187 | extractAllArchiveFiles(selectedFileSystemElements, destTempFolder, fBaseSourceContainerPath, directoryMonitor.newChild(1)); |
188 | // Even if the files were extracted to temporary folder, they | |
189 | // have to look like they originate from the source folder | |
190 | baseSourceLocation = URIUtil.toUnencodedString(fBaseSourceContainerPath.toFile().getCanonicalFile().toURI()); | |
191 | } | |
192 | ||
193 | /* | |
ee9e8924 | 194 | * Import extracted files that are now in the temporary folder, if any |
f17bb886 MAL |
195 | */ |
196 | ||
197 | // We need to update the source container path because the | |
198 | // "preserve folder structure" option would create the | |
199 | // wrong trace folders otherwise. | |
200 | fBaseSourceContainerPath = destTempFolder.getLocation(); | |
201 | List<TraceFileSystemElement> tempFolderFileSystemElements = createElementsForFolder(destTempFolder); | |
202 | if (!tempFolderFileSystemElements.isEmpty()) { | |
203 | calculateSourceLocations(tempFolderFileSystemElements, baseSourceLocation); | |
204 | // Never import extracted files as links, they would link to the | |
205 | // temporary directory that will be deleted | |
206 | fImportOptionFlags = fImportOptionFlags & ~ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE; | |
207 | SubMonitor importTempMonitor = subMonitor.newChild(1); | |
208 | importFileSystemElements(importTempMonitor, tempFolderFileSystemElements); | |
209 | } | |
210 | ||
211 | if (destTempFolder.exists()) { | |
212 | destTempFolder.delete(true, progressMonitor); | |
213 | } | |
214 | ||
215 | setStatus(Status.OK_STATUS); | |
216 | } catch (InterruptedException e) { | |
217 | setStatus(Status.CANCEL_STATUS); | |
218 | } catch (Exception e) { | |
219 | String errorMessage = Messages.ImportTraceWizard_ImportProblem + ": " + //$NON-NLS-1$ | |
220 | (fCurrentPath != null ? fCurrentPath : ""); //$NON-NLS-1$ | |
221 | Activator.getDefault().logError(errorMessage, e); | |
222 | setStatus(new Status(IStatus.ERROR, Activator.PLUGIN_ID, errorMessage, e)); | |
223 | } | |
224 | } | |
225 | ||
226 | /** | |
227 | * Get the list of resources that were imported by this operation. An | |
228 | * example use case would be to use this to open traces that were imported | |
229 | * by this operation. | |
230 | * | |
231 | * @return the resources that were imported | |
232 | */ | |
233 | public List<IResource> getImportedResources() { | |
234 | return fImportedResources; | |
235 | } | |
236 | ||
237 | /** | |
238 | * Import a collection of file system elements into the workspace. | |
239 | */ | |
240 | private void importFileSystemElements(IProgressMonitor monitor, List<TraceFileSystemElement> fileSystemElements) | |
241 | throws InterruptedException, TmfTraceImportException, CoreException, InvocationTargetException { | |
242 | SubMonitor subMonitor = SubMonitor.convert(monitor, fileSystemElements.size()); | |
243 | ||
244 | ListIterator<TraceFileSystemElement> fileSystemElementsIter = fileSystemElements.listIterator(); | |
245 | ||
246 | // Map to remember already imported directory traces | |
247 | final Map<String, TraceFileSystemElement> directoryTraces = new HashMap<>(); | |
248 | while (fileSystemElementsIter.hasNext()) { | |
249 | ModalContext.checkCanceled(monitor); | |
250 | fCurrentPath = null; | |
251 | TraceFileSystemElement element = fileSystemElementsIter.next(); | |
252 | IFileSystemObject fileSystemObject = element.getFileSystemObject(); | |
253 | String resourcePath = element.getFileSystemObject().getAbsolutePath(); | |
254 | element.setDestinationContainerPath(computeDestinationContainerPath(new Path(resourcePath))); | |
255 | ||
256 | fCurrentPath = resourcePath; | |
257 | SubMonitor sub = subMonitor.newChild(1); | |
258 | if (element.isDirectory()) { | |
259 | if (!directoryTraces.containsKey(resourcePath) && isDirectoryTrace(element)) { | |
260 | directoryTraces.put(resourcePath, element); | |
261 | validateAndImportTrace(element, sub); | |
262 | } | |
263 | } else { | |
264 | TraceFileSystemElement parentElement = (TraceFileSystemElement) element.getParent(); | |
265 | String parentPath = parentElement.getFileSystemObject().getAbsolutePath(); | |
266 | parentElement.setDestinationContainerPath(computeDestinationContainerPath(new Path(parentPath))); | |
267 | fCurrentPath = parentPath; | |
268 | if (!directoryTraces.containsKey(parentPath)) { | |
269 | if (isDirectoryTrace(parentElement)) { | |
270 | directoryTraces.put(parentPath, parentElement); | |
271 | validateAndImportTrace(parentElement, sub); | |
272 | } else { | |
273 | boolean validateFile = true; | |
274 | TraceFileSystemElement grandParentElement = (TraceFileSystemElement) parentElement.getParent(); | |
275 | // Special case for LTTng trace that may contain index | |
276 | // directory and files | |
277 | if (grandParentElement != null) { | |
278 | String grandParentPath = grandParentElement.getFileSystemObject().getAbsolutePath(); | |
279 | grandParentElement.setDestinationContainerPath(computeDestinationContainerPath(new Path(parentPath))); | |
280 | fCurrentPath = grandParentPath; | |
281 | if (directoryTraces.containsKey(grandParentPath)) { | |
282 | validateFile = false; | |
283 | } else if (isDirectoryTrace(grandParentElement)) { | |
284 | directoryTraces.put(grandParentPath, grandParentElement); | |
285 | validateAndImportTrace(grandParentElement, sub); | |
286 | validateFile = false; | |
287 | } | |
288 | } | |
289 | if (validateFile && (fileSystemObject.exists())) { | |
290 | validateAndImportTrace(element, sub); | |
291 | } | |
292 | } | |
293 | } | |
294 | } | |
295 | } | |
296 | } | |
297 | ||
298 | /** | |
299 | * Generate a new list of file system elements for the specified folder. | |
300 | */ | |
301 | private static List<TraceFileSystemElement> createElementsForFolder(IFolder folder) { | |
302 | // Create the new import provider and root element based on the | |
303 | // specified folder | |
304 | FileSystemObjectImportStructureProvider importStructureProvider = new FileSystemObjectImportStructureProvider(FileSystemStructureProvider.INSTANCE, null); | |
305 | IFileSystemObject rootElement = importStructureProvider.getIFileSystemObject(new File(folder.getLocation().toOSString())); | |
306 | TraceFileSystemElement createRootElement = TraceFileSystemElement.createRootTraceFileElement(rootElement, importStructureProvider); | |
307 | List<TraceFileSystemElement> list = new LinkedList<>(); | |
308 | createRootElement.getAllChildren(list); | |
309 | return list; | |
310 | } | |
311 | ||
312 | /** | |
313 | * Extract all file system elements (File) to destination folder (typically | |
314 | * workspace/TraceProject/.traceImport) | |
315 | */ | |
316 | private void extractAllArchiveFiles(List<TraceFileSystemElement> fileSystemElements, IFolder destFolder, IPath baseSourceContainerPath, IProgressMonitor progressMonitor) throws InterruptedException, CoreException, InvocationTargetException { | |
317 | SubMonitor subMonitor = SubMonitor.convert(progressMonitor, fileSystemElements.size()); | |
318 | ListIterator<TraceFileSystemElement> fileSystemElementsIter = fileSystemElements.listIterator(); | |
319 | while (fileSystemElementsIter.hasNext()) { | |
320 | ModalContext.checkCanceled(subMonitor); | |
321 | ||
322 | SubMonitor elementProgress = subMonitor.newChild(1); | |
323 | TraceFileSystemElement element = fileSystemElementsIter.next(); | |
324 | File archiveFile = (File) element.getFileSystemObject().getRawFileSystemObject(); | |
325 | boolean isArchiveFileElement = element.getFileSystemObject() instanceof FileFileSystemObject && ArchiveUtil.isArchiveFile(archiveFile); | |
326 | if (isArchiveFileElement) { | |
327 | elementProgress = SubMonitor.convert(elementProgress, 4); | |
ee9e8924 MK |
328 | IPath makeAbsolute = baseSourceContainerPath.makeAbsolute(); |
329 | IPath relativeToSourceContainer = new Path(element.getFileSystemObject().getAbsolutePath()).makeRelativeTo(makeAbsolute); | |
f17bb886 MAL |
330 | IFolder folder = safeCreateExtractedFolder(destFolder, relativeToSourceContainer, elementProgress.newChild(1)); |
331 | extractArchiveToFolder(archiveFile, folder, elementProgress.newChild(1)); | |
332 | ||
333 | // Delete original archive, we don't want to import this, just | |
334 | // the extracted content | |
335 | IFile fileRes = destFolder.getFile(relativeToSourceContainer); | |
336 | fileRes.delete(true, elementProgress.newChild(1)); | |
337 | IPath newPath = destFolder.getFullPath().append(relativeToSourceContainer); | |
338 | // Rename extracted folder (.extract) to original archive name | |
339 | folder.move(newPath, true, elementProgress.newChild(1)); | |
340 | folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(newPath); | |
341 | ||
342 | // Create the new import provider and root element based on | |
343 | // the newly extracted temporary folder | |
344 | FileSystemObjectImportStructureProvider importStructureProvider = new FileSystemObjectImportStructureProvider(FileSystemStructureProvider.INSTANCE, null); | |
345 | IFileSystemObject rootElement = importStructureProvider.getIFileSystemObject(new File(folder.getLocation().toOSString())); | |
346 | TraceFileSystemElement newElement = TraceFileSystemElement.createRootTraceFileElement(rootElement, importStructureProvider); | |
347 | List<TraceFileSystemElement> extractedChildren = new ArrayList<>(); | |
348 | newElement.getAllChildren(extractedChildren); | |
349 | extractAllArchiveFiles(extractedChildren, folder, folder.getLocation(), progressMonitor); | |
350 | } | |
351 | } | |
352 | } | |
353 | ||
354 | /** | |
355 | * Extract a file (File) to a destination folder | |
356 | */ | |
357 | private void extractArchiveToFolder(File sourceFile, IFolder destinationFolder, IProgressMonitor progressMonitor) throws InvocationTargetException, InterruptedException { | |
358 | Pair<IFileSystemObject, FileSystemObjectImportStructureProvider> rootObjectAndProvider = ArchiveUtil.getRootObjectAndProvider(sourceFile, fShell); | |
359 | TraceFileSystemElement rootElement = TraceFileSystemElement.createRootTraceFileElement(rootObjectAndProvider.getFirst(), rootObjectAndProvider.getSecond()); | |
360 | List<TraceFileSystemElement> fileSystemElements = new ArrayList<>(); | |
361 | rootElement.getAllChildren(fileSystemElements); | |
362 | extractArchiveContent(fileSystemElements.listIterator(), destinationFolder, progressMonitor); | |
363 | rootObjectAndProvider.getSecond().dispose(); | |
364 | } | |
365 | ||
366 | /** | |
367 | * Safely create a folder meant to receive extracted content by making sure | |
368 | * there is no name clash. | |
369 | */ | |
370 | private static IFolder safeCreateExtractedFolder(IFolder destinationFolder, IPath relativeContainerRelativePath, IProgressMonitor monitor) throws CoreException { | |
371 | SubMonitor subMonitor = SubMonitor.convert(monitor, 2); | |
372 | IFolder extractedFolder; | |
373 | String suffix = ""; //$NON-NLS-1$ | |
374 | int i = 2; | |
375 | while (true) { | |
376 | IPath fullPath = destinationFolder.getFullPath().append(relativeContainerRelativePath + ".extract" + suffix); //$NON-NLS-1$ | |
377 | IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(fullPath); | |
378 | if (!folder.exists()) { | |
379 | extractedFolder = folder; | |
380 | break; | |
381 | } | |
382 | suffix = "(" + i + ")"; //$NON-NLS-1$//$NON-NLS-2$ | |
383 | i++; | |
384 | } | |
385 | subMonitor.worked(1); | |
386 | ||
387 | TraceUtils.createFolder(extractedFolder, subMonitor.newChild(1)); | |
388 | return extractedFolder; | |
389 | } | |
390 | ||
391 | private void calculateSourceLocations(List<TraceFileSystemElement> fileSystemElements, String baseSourceLocation) { | |
392 | for (TraceFileSystemElement element : fileSystemElements) { | |
393 | IPath tempRelative = new Path(element.getFileSystemObject().getAbsolutePath()).makeRelativeTo(fBaseSourceContainerPath); | |
394 | String sourceLocation = baseSourceLocation + tempRelative; | |
395 | element.setSourceLocation(sourceLocation); | |
396 | ||
397 | TraceFileSystemElement parentElement = (TraceFileSystemElement) element.getParent(); | |
398 | tempRelative = new Path(parentElement.getFileSystemObject().getAbsolutePath()).makeRelativeTo(fBaseSourceContainerPath); | |
399 | sourceLocation = baseSourceLocation + tempRelative + '/'; | |
400 | parentElement.setSourceLocation(sourceLocation); | |
401 | } | |
402 | } | |
403 | ||
404 | /** | |
405 | * Extract all file system elements (Tar, Zip elements) to destination | |
406 | * folder (typically workspace/TraceProject/.traceImport or a subfolder of | |
407 | * it) | |
408 | */ | |
409 | private void extractArchiveContent(Iterator<TraceFileSystemElement> fileSystemElementsIter, IFolder tempFolder, IProgressMonitor progressMonitor) throws InterruptedException, | |
410 | InvocationTargetException { | |
411 | List<TraceFileSystemElement> subList = new ArrayList<>(); | |
412 | // Collect all the elements | |
413 | while (fileSystemElementsIter.hasNext()) { | |
414 | ModalContext.checkCanceled(progressMonitor); | |
415 | TraceFileSystemElement element = fileSystemElementsIter.next(); | |
416 | if (element.isDirectory()) { | |
417 | Object[] array = element.getFiles().getChildren(); | |
418 | for (int i = 0; i < array.length; i++) { | |
419 | subList.add((TraceFileSystemElement) array[i]); | |
420 | } | |
421 | } | |
422 | subList.add(element); | |
423 | } | |
424 | ||
425 | if (subList.isEmpty()) { | |
426 | return; | |
427 | } | |
428 | ||
429 | TraceFileSystemElement root = getRootElement(subList.get(0)); | |
430 | ||
431 | ImportProvider fileSystemStructureProvider = new ImportProvider(); | |
432 | ||
433 | IOverwriteQuery myQueryImpl = new IOverwriteQuery() { | |
434 | @Override | |
435 | public String queryOverwrite(String file) { | |
436 | return IOverwriteQuery.NO_ALL; | |
437 | } | |
438 | }; | |
439 | ||
440 | progressMonitor.setTaskName(Messages.ImportTraceWizard_ExtractImportOperationTaskName); | |
441 | IPath containerPath = tempFolder.getFullPath(); | |
442 | ImportOperation operation = new ImportOperation(containerPath, root, fileSystemStructureProvider, myQueryImpl, subList); | |
443 | operation.setContext(fShell); | |
444 | ||
445 | operation.setCreateContainerStructure(true); | |
446 | operation.setOverwriteResources(false); | |
447 | operation.setVirtualFolders(false); | |
448 | ||
449 | operation.run(new SubProgressMonitor(progressMonitor, subList.size(), SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK)); | |
450 | } | |
451 | ||
452 | private static TraceFileSystemElement getRootElement(TraceFileSystemElement element) { | |
453 | TraceFileSystemElement root = element; | |
454 | while (root.getParent() != null) { | |
455 | root = (TraceFileSystemElement) root.getParent(); | |
456 | } | |
457 | return root; | |
458 | } | |
459 | ||
460 | private IPath computeDestinationContainerPath(Path resourcePath) { | |
461 | IPath destinationContainerPath = fDestinationContainerPath; | |
462 | ||
463 | // We need to figure out the new destination path relative to the | |
464 | // selected "base" source directory. | |
465 | // Here for example, the selected source directory is /home/user | |
466 | if ((fImportOptionFlags & ImportTraceWizardPage.OPTION_PRESERVE_FOLDER_STRUCTURE) != 0) { | |
467 | // /home/user/bar/foo/trace -> /home/user/bar/foo | |
468 | IPath sourceContainerPath = resourcePath.removeLastSegments(1); | |
469 | if (fBaseSourceContainerPath.equals(resourcePath)) { | |
470 | // Use resourcePath directory if fBaseSourceContainerPath | |
471 | // points to a directory trace | |
472 | sourceContainerPath = resourcePath; | |
473 | } | |
474 | // /home/user/bar/foo, /home/user -> bar/foo | |
475 | IPath relativeContainerPath = sourceContainerPath.makeRelativeTo(fBaseSourceContainerPath); | |
476 | // project/Traces + bar/foo -> project/Traces/bar/foo | |
477 | destinationContainerPath = fDestinationContainerPath.append(relativeContainerPath); | |
478 | } | |
479 | return destinationContainerPath; | |
480 | } | |
481 | ||
482 | /** | |
483 | * Import a single file system element into the workspace. | |
484 | */ | |
485 | private void validateAndImportTrace(TraceFileSystemElement fileSystemElement, IProgressMonitor monitor) | |
486 | throws TmfTraceImportException, CoreException, InvocationTargetException, InterruptedException { | |
487 | String path = fileSystemElement.getFileSystemObject().getAbsolutePath(); | |
488 | TraceTypeHelper traceTypeHelper = null; | |
489 | ||
490 | File file = (File) fileSystemElement.getFileSystemObject().getRawFileSystemObject(); | |
491 | boolean isArchiveFileElement = fileSystemElement.getFileSystemObject() instanceof FileFileSystemObject && ArchiveUtil.isArchiveFile(file); | |
492 | if (isArchiveFileElement) { | |
493 | // We'll be extracting this later, do not import as a trace | |
494 | return; | |
495 | } | |
496 | ||
497 | if (fTraceType == null) { | |
498 | // Auto Detection | |
499 | try { | |
500 | traceTypeHelper = TmfTraceTypeUIUtils.selectTraceType(path, null, null); | |
501 | } catch (TmfTraceImportException e) { | |
502 | // the trace did not match any trace type | |
503 | } | |
504 | if (traceTypeHelper == null) { | |
505 | if ((fImportOptionFlags & ImportTraceWizardPage.OPTION_IMPORT_UNRECOGNIZED_TRACES) != 0) { | |
506 | importResource(fileSystemElement, monitor); | |
507 | } | |
508 | return; | |
509 | } | |
510 | } else { | |
511 | boolean isDirectoryTraceType = TmfTraceType.isDirectoryTraceType(fTraceType); | |
512 | if (fileSystemElement.isDirectory() != isDirectoryTraceType) { | |
513 | return; | |
514 | } | |
515 | traceTypeHelper = TmfTraceType.getTraceType(fTraceType); | |
516 | ||
517 | if (traceTypeHelper == null) { | |
518 | // Trace type not found | |
519 | throw new TmfTraceImportException(Messages.ImportTraceWizard_TraceTypeNotFound); | |
520 | } | |
521 | ||
522 | if (!traceTypeHelper.validate(path).isOK()) { | |
523 | // Trace type exist but doesn't validate for given trace. | |
524 | return; | |
525 | } | |
526 | } | |
527 | ||
528 | // Finally import trace | |
529 | IResource importedResource = importResource(fileSystemElement, monitor); | |
530 | if (importedResource != null) { | |
531 | TmfTraceTypeUIUtils.setTraceType(importedResource, traceTypeHelper); | |
532 | fImportedResources.add(importedResource); | |
533 | } | |
534 | ||
535 | } | |
536 | ||
537 | /** | |
538 | * Imports a trace resource to project. In case of name collision the user | |
539 | * will be asked to confirm overwriting the existing trace, overwriting or | |
540 | * skipping the trace to be imported. | |
541 | * | |
542 | * @param fileSystemElement | |
543 | * trace file system object to import | |
544 | * @param monitor | |
545 | * a progress monitor | |
546 | * @return the imported resource or null if no resource was imported | |
547 | * | |
548 | * @throws InvocationTargetException | |
549 | * if problems during import operation | |
550 | * @throws InterruptedException | |
551 | * if cancelled | |
552 | * @throws CoreException | |
553 | * if problems with workspace | |
554 | */ | |
555 | private IResource importResource(TraceFileSystemElement fileSystemElement, IProgressMonitor monitor) | |
556 | throws InvocationTargetException, InterruptedException, CoreException { | |
557 | ||
558 | IPath tracePath = getInitialDestinationPath(fileSystemElement); | |
559 | String newName = fConflictHandler.checkAndHandleNameClash(tracePath, monitor); | |
fc4a373f | 560 | |
f17bb886 MAL |
561 | if (newName == null) { |
562 | return null; | |
563 | } | |
564 | fileSystemElement.setLabel(newName); | |
565 | ||
566 | List<TraceFileSystemElement> subList = new ArrayList<>(); | |
567 | ||
568 | FileSystemElement parentFolder = fileSystemElement.getParent(); | |
569 | ||
570 | IPath containerPath = fileSystemElement.getDestinationContainerPath(); | |
571 | tracePath = containerPath.addTrailingSeparator().append(fileSystemElement.getLabel()); | |
572 | boolean createLinksInWorkspace = (fImportOptionFlags & ImportTraceWizardPage.OPTION_CREATE_LINKS_IN_WORKSPACE) != 0; | |
573 | if (fileSystemElement.isDirectory() && !createLinksInWorkspace) { | |
574 | containerPath = tracePath; | |
575 | ||
576 | Object[] array = fileSystemElement.getFiles().getChildren(); | |
577 | for (int i = 0; i < array.length; i++) { | |
578 | subList.add((TraceFileSystemElement) array[i]); | |
579 | } | |
580 | parentFolder = fileSystemElement; | |
581 | ||
582 | } else { | |
fc4a373f BH |
583 | if (!fileSystemElement.isDirectory()) { |
584 | // File traces | |
585 | IFileInfo info = EFS.getStore(new File(fileSystemElement.getFileSystemObject().getAbsolutePath()).toURI()).fetchInfo(); | |
586 | if (info.getLength() == 0) { | |
587 | // Don't import empty traces | |
588 | return null; | |
589 | } | |
590 | } | |
f17bb886 MAL |
591 | subList.add(fileSystemElement); |
592 | } | |
593 | ||
594 | ImportProvider fileSystemStructureProvider = new ImportProvider(); | |
595 | ||
596 | IOverwriteQuery myQueryImpl = new IOverwriteQuery() { | |
597 | @Override | |
598 | public String queryOverwrite(String file) { | |
599 | return IOverwriteQuery.NO_ALL; | |
600 | } | |
601 | }; | |
602 | ||
603 | monitor.setTaskName(Messages.ImportTraceWizard_ImportOperationTaskName + " " + fileSystemElement.getFileSystemObject().getAbsolutePath()); //$NON-NLS-1$ | |
604 | ImportOperation operation = new ImportOperation(containerPath, parentFolder, fileSystemStructureProvider, myQueryImpl, subList); | |
605 | operation.setContext(fShell); | |
606 | ||
607 | operation.setCreateContainerStructure(false); | |
608 | operation.setOverwriteResources(false); | |
609 | operation.setCreateLinks(createLinksInWorkspace); | |
610 | operation.setVirtualFolders(false); | |
611 | ||
612 | operation.run(new SubProgressMonitor(monitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK)); | |
613 | String sourceLocation = fileSystemElement.getSourceLocation(); | |
614 | IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(tracePath); | |
615 | if (sourceLocation != null) { | |
616 | resource.setPersistentProperty(TmfCommonConstants.SOURCE_LOCATION, sourceLocation); | |
617 | } | |
618 | ||
619 | return resource; | |
620 | } | |
621 | ||
622 | private static boolean isDirectoryTrace(TraceFileSystemElement fileSystemElement) { | |
623 | String path = fileSystemElement.getFileSystemObject().getAbsolutePath(); | |
624 | if (TmfTraceType.isDirectoryTrace(path)) { | |
625 | return true; | |
626 | } | |
627 | return false; | |
628 | } | |
629 | ||
630 | /** | |
631 | * @return the initial destination path, before rename, if any | |
632 | */ | |
633 | private static IPath getInitialDestinationPath(TraceFileSystemElement fileSystemElement) { | |
634 | IPath traceFolderPath = fileSystemElement.getDestinationContainerPath(); | |
635 | return traceFolderPath.append(fileSystemElement.getFileSystemObject().getName()); | |
636 | } | |
637 | ||
638 | /** | |
639 | * Set the status for this operation | |
640 | * | |
641 | * @param status | |
642 | * the status | |
643 | */ | |
644 | private void setStatus(IStatus status) { | |
645 | fStatus = status; | |
646 | } | |
647 | ||
648 | /** | |
649 | * Get the resulting status of this operation. Clients can use this for | |
650 | * error reporting, etc. | |
651 | * | |
652 | * @return the resulting status | |
653 | */ | |
654 | public IStatus getStatus() { | |
655 | return fStatus; | |
656 | } | |
657 | ||
658 | private class ImportProvider implements IImportStructureProvider { | |
659 | ||
660 | ImportProvider() { | |
661 | } | |
662 | ||
663 | @Override | |
664 | public String getLabel(Object element) { | |
665 | TraceFileSystemElement resource = (TraceFileSystemElement) element; | |
666 | return resource.getLabel(); | |
667 | } | |
668 | ||
669 | @Override | |
670 | public List getChildren(Object element) { | |
671 | TraceFileSystemElement resource = (TraceFileSystemElement) element; | |
058f8b3f | 672 | return Arrays.asList(resource.getFiles().getChildren()); |
f17bb886 MAL |
673 | } |
674 | ||
675 | @Override | |
676 | public InputStream getContents(Object element) { | |
677 | TraceFileSystemElement resource = (TraceFileSystemElement) element; | |
678 | return resource.getProvider().getContents(resource.getFileSystemObject()); | |
679 | } | |
680 | ||
681 | @Override | |
682 | public String getFullPath(Object element) { | |
683 | TraceFileSystemElement resource = (TraceFileSystemElement) element; | |
684 | return resource.getProvider().getFullPath(resource.getFileSystemObject()); | |
685 | } | |
686 | ||
687 | @Override | |
688 | public boolean isFolder(Object element) { | |
689 | TraceFileSystemElement resource = (TraceFileSystemElement) element; | |
690 | return resource.isDirectory(); | |
691 | } | |
692 | } | |
ee9e8924 MK |
693 | |
694 | /** | |
695 | * Sets the conflict handler | |
696 | * | |
697 | * @param conflictHandler | |
698 | * the conflict handler | |
699 | */ | |
700 | public void setConflictHandler(ImportConflictHandler conflictHandler) { | |
701 | fConflictHandler = conflictHandler; | |
702 | } | |
f17bb886 | 703 | } |