Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2ff0bb69d95eef3c5dc43512496892324fb51607 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*******************************************************************************
 * Copyright (c) 2009, 2010 Wind River 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:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.p2.touchpoint.natives.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.eclipse.cdt.internal.p2.Activator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.engine.Profile;
import org.eclipse.equinox.internal.p2.touchpoint.natives.Messages;
import org.eclipse.equinox.internal.p2.touchpoint.natives.Util;
import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.ActionConstants;
import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.UnzipAction;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.repository.artifact.IFileArtifactRepository;
import org.eclipse.osgi.util.NLS;

/**
 * Untar the artifact with a choice of compression
 * 
 * syntax: untar(source:@artifact, target:${installFolder}/<subdir>, compression:[gz|bz2])
 * 
 * @author DSchaefe
 *
 */
public class UntarAction extends ProvisioningAction {

	private static final String ACTION_NAME = "untar";
	private static final String PARM_COMPRESSION = "compression";
	private static final String VALUE_GZ = "gz";
	private static final String VALUE_BZ2 = "bz2";
	
	private enum Compression {
		none,
		gz,
		bz2
	}
	
	@Override
	public IStatus execute(Map parameters) {
		try {
			return untar(parameters);
		} catch (Exception e) {
			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getLocalizedMessage(), e);
		}
	}

	@Override
	public IStatus undo(Map parameters) {
		try {
			return CleanupUntarAction.cleanup(parameters);
		} catch (Exception e) {
			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getLocalizedMessage(), e);
		}
	}

	public static IStatus untar(Map parameters) throws Exception {
		String source = (String)parameters.get(ActionConstants.PARM_SOURCE);
		if (source == null)
			return Util.createError(NLS.bind(Messages.param_not_set, ActionConstants.PARM_SOURCE, ACTION_NAME));

		String originalSource = source;
		String target = (String)parameters.get(ActionConstants.PARM_TARGET);
		if (target == null)
			return Util.createError(NLS.bind(Messages.param_not_set, ActionConstants.PARM_TARGET, ACTION_NAME));

		String compressionStr = (String)parameters.get(PARM_COMPRESSION);
		Compression compression;
		if (compressionStr == null)
			return Util.createError(NLS.bind(Messages.param_not_set, PARM_COMPRESSION, ACTION_NAME));
		else if (compressionStr.equals(VALUE_GZ))
			compression = Compression.gz;
		else if (compressionStr.equals(VALUE_BZ2))
			compression = Compression.bz2;
		else 
			// TODO Should put out a log if compression is unknown
			compression = Compression.none;
		
		IInstallableUnit iu = (IInstallableUnit) parameters.get(ActionConstants.PARM_IU);
		Profile profile = (Profile) parameters.get(ActionConstants.PARM_PROFILE);
		IProvisioningAgent agent = (IProvisioningAgent) parameters.get(ActionConstants.PARM_AGENT);

		if (source.equals(ActionConstants.PARM_AT_ARTIFACT)) {
			//TODO: fix wherever this occurs -- investigate as this is probably not desired
			if (iu.getArtifacts() == null || iu.getArtifacts().size() == 0)
				return Status.OK_STATUS;

			IArtifactKey artifactKey = iu.getArtifacts().iterator().next();

			IFileArtifactRepository downloadCache;
			try {
				downloadCache = Util.getDownloadCacheRepo(agent);
			} catch (ProvisionException e) {
				return e.getStatus();
			}
			File fileLocation = downloadCache.getArtifactFile(artifactKey);
			if ((fileLocation == null) || !fileLocation.exists())
				return Util.createError(NLS.bind(Messages.artifact_not_available, artifactKey));
			source = fileLocation.getAbsolutePath();
		}

		File[] unzippedFiles = untar(source, target, compression);
		StringBuffer unzippedFileNameBuffer = new StringBuffer();
		for (int i = 0; i < unzippedFiles.length; i++)
			unzippedFileNameBuffer.append(unzippedFiles[i].getAbsolutePath()).append(ActionConstants.PIPE);

		profile.setInstallableUnitProperty(iu, "unzipped" + ActionConstants.PIPE + originalSource + ActionConstants.PIPE + target, unzippedFileNameBuffer.toString()); //$NON-NLS-1$
		return Status.OK_STATUS;
	}
	
	private static File[] untar(String source, String destination, Compression compression) throws Exception {
		File zipFile = new File(source);
		if (!zipFile.exists()) {
			Util.log(UnzipAction.class.getName() + " the files to be unzipped is not here"); //$NON-NLS-1$
		}

		File target = new File(destination);
		
		FileInputStream fileIn = new FileInputStream(zipFile);
		InputStream compIn = fileIn;
		if (compression.equals(Compression.gz))
			compIn = new GZIPInputStream(fileIn);
		else if (compression.equals(Compression.bz2)) {
			// Skip the magic bytes first
			fileIn.read(new byte[2]);
			compIn = new CBZip2InputStream(fileIn);
		}

		ArrayList<File> fileList = new ArrayList<File>();
		TarInputStream tarIn = new TarInputStream(compIn);
		for (TarEntry tarEntry = tarIn.getNextEntry(); tarEntry != null; tarEntry = tarIn.getNextEntry()) {
			File outFile = new File(target, tarEntry.getName());
			if (tarEntry.isDirectory()) {
				outFile.mkdirs();
			} else {
				if (outFile.exists())
					outFile.delete();
				else
					outFile.getParentFile().mkdirs();
				FileOutputStream outStream = new FileOutputStream(outFile);
				tarIn.copyEntryContents(outStream);
				outStream.close();
					
				// Set last modified time from the tar entry
				long lastModified = tarEntry.getModTime().getTime();
				outFile.setLastModified(lastModified);
					
				// Set the executable bits from the tar entry
				// we let the umask determine the r/w
				int mode = tarEntry.getMode();
				boolean exec = (mode & 0x111) != 0;
				boolean execOwner = (mode & 0x11) == 0;
//				outFile.setExecutable(exec, execOwner);
					
				fileList.add(outFile);
			}
		}
		tarIn.close();
		return fileList.toArray(new File[fileList.size()]);
	}
	
}

Back to the top