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