2a247a60dc1494fded402213b6731bf3ce161fd4
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / importtrace / ArchiveUtil.java
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.IOException;
17 import java.util.zip.ZipException;
18 import java.util.zip.ZipFile;
19
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.tracecompass.tmf.core.util.Pair;
22 import org.eclipse.ui.internal.wizards.datatransfer.ArchiveFileManipulations;
23 import org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider;
24 import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
25
26 /**
27 * Various utilities for dealing with archives in the context of importing
28 * traces.
29 */
30 @SuppressWarnings({"restriction" })
31 public class ArchiveUtil {
32
33 /**
34 * Returns whether or not the source file is an archive file (Zip, tar,
35 * tar.gz, gz).
36 *
37 * @param sourceFile
38 * the source file
39 * @return whether or not the source file is an archive file
40 */
41 public static boolean isArchiveFile(File sourceFile) {
42 String absolutePath = sourceFile.getAbsolutePath();
43 return isTarFile(absolutePath) || ArchiveFileManipulations.isZipFile(absolutePath) || isGzipFile(absolutePath);
44 }
45
46 private static boolean isTarFile(String fileName) {
47 TarFile specifiedTarSourceFile = getSpecifiedTarSourceFile(fileName);
48 if (specifiedTarSourceFile != null) {
49 try {
50 specifiedTarSourceFile.close();
51 return true;
52 } catch (IOException e) {
53 // ignore
54 }
55 }
56 return false;
57 }
58
59 private static boolean isGzipFile(String fileName) {
60 if (!fileName.isEmpty()) {
61 try (GzipFile specifiedTarSourceFile = new GzipFile(fileName);) {
62 return true;
63 } catch (IOException e) {
64 }
65 }
66 return false;
67 }
68
69 private static ZipFile getSpecifiedZipSourceFile(String fileName) {
70 if (fileName.length() == 0) {
71 return null;
72 }
73
74 try {
75 return new ZipFile(fileName);
76 } catch (ZipException e) {
77 // ignore
78 } catch (IOException e) {
79 // ignore
80 }
81
82 return null;
83 }
84
85 private static TarFile getSpecifiedTarSourceFile(String fileName) {
86 if (fileName.length() == 0) {
87 return null;
88 }
89
90 // FIXME: Work around Bug 463633. Remove this block once we move to Eclipse 4.5.
91 if (new File(fileName).length() < 512) {
92 return null;
93 }
94
95 try {
96 return new TarFile(fileName);
97 } catch (TarException | IOException e) {
98 // ignore
99 }
100
101 return null;
102 }
103
104 @SuppressWarnings("resource")
105 static boolean ensureZipSourceIsValid(String archivePath, Shell shell) {
106 ZipFile specifiedFile = getSpecifiedZipSourceFile(archivePath);
107 if (specifiedFile == null) {
108 return false;
109 }
110 return ArchiveFileManipulations.closeZipFile(specifiedFile, shell);
111 }
112
113 static boolean ensureTarSourceIsValid(String archivePath) {
114 TarFile specifiedFile = getSpecifiedTarSourceFile(archivePath);
115 if (specifiedFile == null) {
116 return false;
117 }
118 return closeTarFile(specifiedFile);
119 }
120
121 static boolean ensureGzipSourceIsValid(String archivePath) {
122 return isGzipFile(archivePath);
123 }
124
125 static boolean closeTarFile(TarFile file) {
126 try {
127 file.close();
128 } catch (IOException e) {
129 return false;
130 }
131
132 return true;
133 }
134
135 /**
136 * Get the root file system object and it's associated import provider for
137 * the specified source file. A shell is used to display messages in case of
138 * errors.
139 *
140 * @param sourceFile
141 * the source file
142 * @param shell
143 * the parent shell to use to display error messages
144 * @return the root file system object and it's associated import provider
145 */
146 @SuppressWarnings("resource")
147 public static Pair<IFileSystemObject, FileSystemObjectImportStructureProvider> getRootObjectAndProvider(File sourceFile, Shell shell) {
148 if (sourceFile == null) {
149 return null;
150 }
151
152 IFileSystemObject rootElement = null;
153 FileSystemObjectImportStructureProvider importStructureProvider = null;
154
155 // Import from directory
156 if (!isArchiveFile(sourceFile)) {
157 importStructureProvider = new FileSystemObjectImportStructureProvider(FileSystemStructureProvider.INSTANCE, null);
158 rootElement = importStructureProvider.getIFileSystemObject(sourceFile);
159 } else {
160 // Import from archive
161 FileSystemObjectLeveledImportStructureProvider leveledImportStructureProvider = null;
162 String archivePath = sourceFile.getAbsolutePath();
163 if (isTarFile(archivePath)) {
164 if (ensureTarSourceIsValid(archivePath)) {
165 // We close the file when we dispose the import provider,
166 // see disposeSelectionGroupRoot
167 TarFile tarFile = getSpecifiedTarSourceFile(archivePath);
168 leveledImportStructureProvider = new FileSystemObjectLeveledImportStructureProvider(new TarLeveledStructureProvider(tarFile), archivePath);
169 }
170 } else if (ensureZipSourceIsValid(archivePath, shell)) {
171 // We close the file when we dispose the import provider, see
172 // disposeSelectionGroupRoot
173 ZipFile zipFile = getSpecifiedZipSourceFile(archivePath);
174 leveledImportStructureProvider = new FileSystemObjectLeveledImportStructureProvider(new ZipLeveledStructureProvider(zipFile), archivePath);
175 } else if (ensureGzipSourceIsValid(archivePath)) {
176 // We close the file when we dispose the import provider, see
177 // disposeSelectionGroupRoot
178 GzipFile zipFile = null;
179 try {
180 zipFile = new GzipFile(archivePath);
181 leveledImportStructureProvider = new FileSystemObjectLeveledImportStructureProvider(new GzipLeveledStructureProvider(zipFile), archivePath);
182 } catch (IOException e) {
183 // do nothing
184 }
185 }
186 if (leveledImportStructureProvider == null) {
187 return null;
188 }
189 rootElement = leveledImportStructureProvider.getRoot();
190 importStructureProvider = leveledImportStructureProvider;
191 }
192
193 if (rootElement == null) {
194 return null;
195 }
196
197 return new Pair<>(rootElement, importStructureProvider);
198 }
199 }
This page took 0.042035 seconds and 4 git commands to generate.