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