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