Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4a8b5fa5884436f92305593683e62a4cec7f2ab7 (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
package org.eclipse.cdt.internal.p2.touchpoint.natives.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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.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.internal.provisional.p2.artifact.repository.IFileArtifactRepository;
import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException;
import org.eclipse.equinox.internal.provisional.p2.engine.ProvisioningAction;
import org.eclipse.equinox.internal.provisional.p2.metadata.IArtifactKey;
import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit;
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) {
		return untar(parameters);
	}

	@Override
	public IStatus undo(Map parameters) {
		return CleanupUntarAction.cleanup(parameters);
	}

	public static IStatus untar(Map parameters) {
		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);

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

			IArtifactKey artifactKey = iu.getArtifacts()[0];

			IFileArtifactRepository downloadCache;
			try {
				downloadCache = Util.getDownloadCacheRepo();
			} 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) {
		File zipFile = new File(source);
		if (zipFile == null || !zipFile.exists()) {
			Util.log(UnzipAction.class.getName() + " the files to be unzipped is not here"); //$NON-NLS-1$
		}

		try {
			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(source, 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();
					long lastModified = tarEntry.getModTime().getTime();
					outFile.setLastModified(lastModified);
					fileList.add(outFile);
				}
			}
			tarIn.close();
			return fileList.toArray(new File[fileList.size()]);
		} catch (IOException e) {
			Util.log(UnzipAction.class.getName() + " error unzipping zipfile: " + zipFile.getAbsolutePath() + "destination: " + destination); //$NON-NLS-1$ //$NON-NLS-2$
		}
		return null;
	}
}

Back to the top