1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-19 06:55:23 +02:00

Bug 461438 - Start of unpack native touchpoint action.

This can be used for downloading and extracting toolchains into the
Eclipse install folder. It will be used for the Arduino CDT.

Change-Id: Iada25cff0fb2b2d9e5a55bedcaa8dbf9074058d8
This commit is contained in:
Doug Schaefer 2015-03-06 12:03:28 -05:00 committed by Gerrit Code Review @ Eclipse.org
parent f9e7d5d156
commit b2460aaf53
7 changed files with 183 additions and 19 deletions

View file

@ -4,7 +4,6 @@
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins">
<accessrules>
<accessrule kind="accessible" pattern="**/internal/provisional/**"/>
<accessrule kind="accessible" pattern="**/internal/p2/**"/>
</accessrules>
</classpathentry>
<classpathentry kind="src" path="src"/>

View file

@ -13,7 +13,8 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.equinox.p2.engine;bundle-version="1.0.1",
org.apache.ant;bundle-version="1.7.0",
org.eclipse.equinox.p2.touchpoint.natives;bundle-version="1.0.0",
org.eclipse.equinox.p2.repository;bundle-version="1.0.0"
org.eclipse.equinox.p2.repository;bundle-version="1.0.0",
org.apache.commons.compress;bundle-version="1.6.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.cdt.internal.p2,

View file

@ -10,8 +10,8 @@
*******************************************************************************/
package org.eclipse.cdt.internal.p2;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.BundleContext;
@ -23,7 +23,7 @@ import org.osgi.framework.ServiceReference;
public class Activator extends Plugin {
// The plug-in ID
public static final String PLUGIN_ID = "org.eclipse.cdt.p2";
public static final String PLUGIN_ID = "org.eclipse.cdt.p2"; //$NON-NLS-1$
// The shared instance
private static Activator plugin;
@ -71,24 +71,30 @@ public class Activator extends Plugin {
* @param name name of the service
* @return the service
*/
@SuppressWarnings("unchecked")
public static <T> T getService(Class<T> clazz) {
BundleContext context = plugin.getBundle().getBundleContext();
ServiceReference ref = context.getServiceReference(clazz.getName());
return (ref != null) ? (T)context.getService(ref) : null;
ServiceReference<T> ref = context.getServiceReference(clazz);
return (ref != null) ? context.getService(ref) : null;
}
/**
* Spit out the log.
*
* @param status
*/
public static void log(int severity, String message, Throwable exception) {
Platform.getLog(plugin.getBundle()).log(new Status(severity, PLUGIN_ID, message, exception));
public static IStatus getStatus(int severity, String message) {
return new Status(severity, PLUGIN_ID, message);
}
public static IStatus getStatus(int severity, Throwable e) {
return new Status(severity, PLUGIN_ID, e.getLocalizedMessage(), e);
}
public static void log(IStatus status) {
Platform.getLog(plugin.getBundle()).log(status);
plugin.getLog().log(status);
}
public static void log(CoreException e) {
plugin.getLog().log(e.getStatus());
}
public static void log(Throwable e) {
plugin.getLog().log(getStatus(IStatus.ERROR, e));
}
}

View file

@ -0,0 +1,5 @@
package org.eclipse.cdt.internal.p2.touchpoint.natives.actions;
public class CleanupUnpackAction {
}

View file

@ -0,0 +1,16 @@
package org.eclipse.cdt.internal.p2.touchpoint.natives.actions;
import org.eclipse.osgi.util.NLS;
public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.cdt.internal.p2.touchpoint.natives.actions.messages"; //$NON-NLS-1$
public static String UnpackAction_ParmNotPresent;
public static String UnpackAction_TargetDirExists;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}
private Messages() {
}
}

View file

@ -0,0 +1,135 @@
/*******************************************************************************
* Copyright (c) 2015 QNX Software Systems and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* QNX Software Systems - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.p2.touchpoint.natives.actions;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
import org.eclipse.cdt.internal.p2.Activator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
/**
* Unpack the artifact with a choice of compression
*
* syntax: unpack(source:<url>, targetDir:${installFolder}/<subdir>, compression:[gz|bz2])
*/
public class UnpackAction extends ProvisioningAction {
private static final String ACTION_NAME = "unpack"; //$NON-NLS-1$
private static final String PARM_SOURCE = "source"; //$NON-NLS-1$
private static final String PARM_TARGET_DIR = "targetDir"; //$NON-NLS-1$
private static final String PARM_FORMAT = "format"; //$NON-NLS-1$
@Override
public IStatus execute(Map<String, Object> parameters) {
try {
String source = (String)parameters.get(PARM_SOURCE);
if (source == null) {
return Activator.getStatus(IStatus.ERROR, String.format(Messages.UnpackAction_ParmNotPresent, PARM_SOURCE, ACTION_NAME));
}
String targetDir = (String)parameters.get(PARM_TARGET_DIR);
if (targetDir == null) {
return Activator.getStatus(IStatus.ERROR, String.format(Messages.UnpackAction_ParmNotPresent, PARM_TARGET_DIR, ACTION_NAME));
}
String format = (String)parameters.get(PARM_FORMAT);
if (format == null) {
return Activator.getStatus(IStatus.ERROR, String.format(Messages.UnpackAction_ParmNotPresent, PARM_FORMAT, ACTION_NAME));
}
IProfile profile = (IProfile) parameters.get("profile"); //$NON-NLS-1$
File installFolder = new File(profile.getProperty(IProfile.PROP_INSTALL_FOLDER));
File destDir = new File(installFolder, targetDir);
if (destDir.exists()) {
return Activator.getStatus(IStatus.ERROR, String.format(org.eclipse.cdt.internal.p2.touchpoint.natives.actions.Messages.UnpackAction_TargetDirExists, destDir.getAbsolutePath()));
}
URL url = new URL(source);
InputStream fileIn = new BufferedInputStream(url.openStream());
switch (format) {
case "tar.gz": //$NON-NLS-1$
InputStream gzIn = new GzipCompressorInputStream(fileIn);
untar(gzIn, destDir);
break;
case "tar.bz2": //$NON-NLS-1$
InputStream bzIn = new BZip2CompressorInputStream(fileIn);
untar(bzIn, destDir);
break;
case "tar.xz": //$NON-NLS-1$
InputStream xzIn = new XZCompressorInputStream(fileIn);
untar(xzIn, destDir);
break;
case "zip": //$NON-NLS-1$
}
return Status.OK_STATUS;
} catch (Throwable e) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getLocalizedMessage(), e);
}
}
private void untar(InputStream in, File destDir) throws IOException {
byte[] buff = new byte[4096];
try (TarArchiveInputStream tarIn = new TarArchiveInputStream(in)) {
for (TarArchiveEntry entry = tarIn.getNextTarEntry(); entry != null; entry = tarIn.getNextTarEntry()) {
String name = entry.getName();
File destFile = new File(destDir, name);
if (entry.isSymbolicLink()) {
Files.createSymbolicLink(destFile.toPath(), Paths.get(name));
} else {
try (FileOutputStream out = new FileOutputStream(destFile)) {
long size = entry.getSize();
while (size > 0) {
int n = tarIn.read(buff, 0, (int)Math.min(size, buff.length));
out.write(buff, 0, n);
size -= n;
}
}
chmod(destFile, entry.getMode());
}
}
}
}
private void chmod(File file, int mode) {
file.setExecutable((mode & 0111) != 0, (mode & 0110) == 0);
file.setWritable((mode & 0222) != 0, (mode & 0220) == 0);
file.setReadable((mode & 0444) != 0, (mode & 0440) == 0);
}
@Override
public IStatus undo(Map<String, Object> parameters) {
try {
return CleanupUntarAction.cleanup(parameters);
} catch (Exception e) {
return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getLocalizedMessage(), e);
}
}
}

View file

@ -0,0 +1,2 @@
UnpackAction_ParmNotPresent=The "{0}" parameter was not set in the "{1}" action.
UnpackAction_TargetDirExists=The target directory exists: {0}