1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
;
16 import java
.io
.IOException
;
17 import java
.util
.zip
.ZipException
;
18 import java
.util
.zip
.ZipFile
;
20 import org
.eclipse
.core
.runtime
.IPath
;
21 import org
.eclipse
.core
.runtime
.Path
;
22 import org
.eclipse
.swt
.widgets
.Shell
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.TmfTraceCoreUtils
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.util
.Pair
;
25 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.ArchiveFileManipulations
;
26 import org
.eclipse
.ui
.wizards
.datatransfer
.FileSystemStructureProvider
;
29 * Various utilities for dealing with archives in the context of importing
32 @SuppressWarnings({"restriction" })
33 public class ArchiveUtil
{
36 * Returns whether or not the source file is an archive file (Zip, tar,
41 * @return whether or not the source file is an archive file
43 public static boolean isArchiveFile(File sourceFile
) {
44 String absolutePath
= sourceFile
.getAbsolutePath();
45 return isTarFile(absolutePath
) || ArchiveFileManipulations
.isZipFile(absolutePath
) || isGzipFile(absolutePath
);
48 private static boolean isTarFile(String fileName
) {
49 TarFile specifiedTarSourceFile
= getSpecifiedTarSourceFile(fileName
);
50 if (specifiedTarSourceFile
!= null) {
52 specifiedTarSourceFile
.close();
54 } catch (IOException e
) {
61 private static boolean isGzipFile(String fileName
) {
62 if (!fileName
.isEmpty()) {
63 try (GzipFile specifiedTarSourceFile
= new GzipFile(fileName
);) {
65 } catch (IOException e
) {
71 private static ZipFile
getSpecifiedZipSourceFile(String fileName
) {
72 if (fileName
.length() == 0) {
77 return new ZipFile(fileName
);
78 } catch (ZipException e
) {
80 } catch (IOException e
) {
87 private static TarFile
getSpecifiedTarSourceFile(String fileName
) {
88 if (fileName
.length() == 0) {
92 // FIXME: Work around Bug 463633. Remove this block once we move to Eclipse 4.5.
93 if (new File(fileName
).length() < 512) {
98 return new TarFile(fileName
);
99 } catch (TarException
| IOException e
) {
106 @SuppressWarnings("resource")
107 static boolean ensureZipSourceIsValid(String archivePath
, Shell shell
) {
108 ZipFile specifiedFile
= getSpecifiedZipSourceFile(archivePath
);
109 if (specifiedFile
== null) {
112 return ArchiveFileManipulations
.closeZipFile(specifiedFile
, shell
);
115 static boolean ensureTarSourceIsValid(String archivePath
) {
116 TarFile specifiedFile
= getSpecifiedTarSourceFile(archivePath
);
117 if (specifiedFile
== null) {
120 return closeTarFile(specifiedFile
);
123 static boolean ensureGzipSourceIsValid(String archivePath
) {
124 return isGzipFile(archivePath
);
127 static boolean closeTarFile(TarFile file
) {
130 } catch (IOException e
) {
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
145 * the parent shell to use to display error messages
146 * @return the root file system object and it's associated import provider
148 @SuppressWarnings("resource")
149 public static Pair
<IFileSystemObject
, FileSystemObjectImportStructureProvider
> getRootObjectAndProvider(File sourceFile
, Shell shell
) {
150 if (sourceFile
== null) {
154 IFileSystemObject rootElement
= null;
155 FileSystemObjectImportStructureProvider importStructureProvider
= null;
157 // Import from directory
158 if (!isArchiveFile(sourceFile
)) {
159 importStructureProvider
= new FileSystemObjectImportStructureProvider(FileSystemStructureProvider
.INSTANCE
, null);
160 rootElement
= importStructureProvider
.getIFileSystemObject(sourceFile
);
162 // Import from archive
163 FileSystemObjectLeveledImportStructureProvider leveledImportStructureProvider
= null;
164 String archivePath
= sourceFile
.getAbsolutePath();
165 if (isTarFile(archivePath
)) {
166 if (ensureTarSourceIsValid(archivePath
)) {
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
);
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
);
176 leveledImportStructureProvider
= new FileSystemObjectLeveledImportStructureProvider(new SafePathZipLeveledStructureProvider(zipFile
), archivePath
);
177 } else if (ensureGzipSourceIsValid(archivePath
)) {
178 // We close the file when we dispose the import provider, see
179 // disposeSelectionGroupRoot
180 GzipFile zipFile
= null;
182 zipFile
= new GzipFile(archivePath
);
183 leveledImportStructureProvider
= new FileSystemObjectLeveledImportStructureProvider(new GzipLeveledStructureProvider(zipFile
), archivePath
);
184 } catch (IOException e
) {
188 if (leveledImportStructureProvider
== null) {
191 rootElement
= leveledImportStructureProvider
.getRoot();
192 importStructureProvider
= leveledImportStructureProvider
;
195 if (rootElement
== null) {
199 return new Pair
<>(rootElement
, importStructureProvider
);
203 * Convert a string path to a path containing valid names. See
204 * {@link TmfTraceCoreUtils#validateName(String)}.
207 * the string path to convert
208 * @return the path contains valid segment names
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
]));
217 newFullPath
= new Path(segment
);
219 newFullPath
= newFullPath
.append(segment
);