Skip to main content
summaryrefslogtreecommitdiffstats
blob: e77b45e9115236e95d76315d19538b7b99c00357 (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
185
186
187
/*******************************************************************************
 * Copyright (c) 2015, 2017 Rapicorp, Inc 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:
 *     Rapicorp, Inc - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.p2.internal.repository.tools;

import java.io.*;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import org.eclipse.equinox.internal.p2.metadata.repository.Messages;
import org.eclipse.osgi.util.NLS;
import org.tukaani.xz.*;

/**
 * A class taking care of creating the XZ'ed version of a repository.
 * It takes as input the folder where a p2 repository is stored and produces the new files in this folder.
 * 
 * The files expected in to be found as input are content.xml or content.jar, or artifacts.xml or artifacts.jar. 
 * Note that the tool does not require both the a metadata repository and an artifact repository.
 * The output will be <fileName>.xml.xz, and a p2.index
 *
 */

public class XZCompressor {
	private static final String CONTENT_XML_XZ = "content.xml.xz"; //$NON-NLS-1$
	private static final String ARTIFACTS_XML_XZ = "artifacts.xml.xz"; //$NON-NLS-1$
	private static final String ARTIFACTS2 = "artifacts"; //$NON-NLS-1$
	private static final String CONTENT = "content"; //$NON-NLS-1$
	private static final String JAR = ".jar"; //$NON-NLS-1$
	private static final String XML = ".xml"; //$NON-NLS-1$

	private String repoFolder;
	private boolean preserveOriginalFile = true;
	private ArrayList<File> filesToDelete = new ArrayList<>();

	public String getRepoFolder() {
		return repoFolder;
	}

	public void setRepoFolder(String repoFolder) {
		this.repoFolder = repoFolder;
	}

	public boolean isPreserveOriginalFile() {
		return preserveOriginalFile;
	}

	public void setPreserveOriginalFile(boolean preserveOriginalFile) {
		this.preserveOriginalFile = preserveOriginalFile;
	}

	private File uncompressJar(File jarFile, String fileToExtract) throws IOException {
		JarInputStream jarStream = new JarInputStream(new FileInputStream(jarFile));
		JarEntry jarEntry = jarStream.getNextJarEntry();
		while (jarEntry != null && (!fileToExtract.equals(jarEntry.getName()))) {
			jarEntry = jarStream.getNextJarEntry();
		}
		//if there is a jar but the entry is missing or invalid, treat this as an invalid repository
		if (jarEntry == null) {
			jarStream.close();
			throw new IOException(NLS.bind(Messages.repoMan_invalidLocation, jarFile.getAbsolutePath()));
		}

		BufferedInputStream input = null;
		OutputStream output = null;
		File extractedFile = new File(jarFile.getAbsoluteFile().getParent(), fileToExtract);
		try {
			input = new BufferedInputStream(jarStream);
			output = new BufferedOutputStream(new FileOutputStream(extractedFile));

			byte[] buffer = new byte[8192];
			int bytesRead = 0;
			while ((bytesRead = input.read(buffer)) != -1)
				output.write(buffer, 0, bytesRead);
		} finally {
			if (input != null) {
				input.close();
			}
			if (output != null) {
				output.close();
			}
		}
		filesToDelete.add(extractedFile);
		return extractedFile;
	}

	private File getMetadataFile(String prefix) throws IOException {
		File candidate = new File(repoFolder, prefix + XML);
		if (candidate.exists()) {
			if (!preserveOriginalFile) {
				filesToDelete.add(candidate);
			}
			return candidate;
		}

		candidate = new File(repoFolder, prefix + JAR);
		if (candidate.exists()) {
			if (!preserveOriginalFile) {
				filesToDelete.add(candidate);
			}
			return uncompressJar(candidate, prefix + XML);
		}
		return null;
	}

	public void compressRepo() throws IOException {
		File metadata = getMetadataFile(CONTENT);
		if (metadata != null)
			compressFile(metadata, new File(repoFolder, CONTENT_XML_XZ));

		File artifacts = getMetadataFile(ARTIFACTS2);
		if (artifacts != null)
			compressFile(artifacts, new File(repoFolder, ARTIFACTS_XML_XZ));

		createP2Index(metadata != null, artifacts != null);
		deleteFiles();
	}

	private void deleteFiles() {
		for (Iterator<File> iterator = filesToDelete.iterator(); iterator.hasNext();) {
			iterator.next().delete();
		}
	}

	private void compressFile(File input, File output) throws IOException {
		LZMA2Options options = new LZMA2Options();
		try {
			options.setDictSize(LZMA2Options.DICT_SIZE_DEFAULT);
			options.setLcLp(3, 0);
			options.setPb(LZMA2Options.PB_MAX);
			options.setMode(LZMA2Options.MODE_NORMAL);
			options.setNiceLen(LZMA2Options.NICE_LEN_MAX);
			options.setMatchFinder(LZMA2Options.MF_BT4);
			options.setDepthLimit(512);
		} catch (UnsupportedOptionsException e) {
			//Can't happen 
		}
		XZOutputStream out = null;
		java.io.FileInputStream is = null;
		try {
			out = new XZOutputStream(new FileOutputStream(output), options);
			is = new FileInputStream(input);
			byte[] buf = new byte[8192];
			int size;
			while ((size = is.read(buf)) != -1)
				out.write(buf, 0, size);
		} finally {
			if (out != null)
				out.close();
			if (is != null)
				is.close();
		}

	}

	private void createP2Index(boolean metadata, boolean artifacts) throws IOException {
		Properties p2Index = new Properties();
		if (metadata) {
			if (preserveOriginalFile)
				p2Index.setProperty("metadata.repository.factory.order", "content.xml.xz,content.xml,!"); //$NON-NLS-1$//$NON-NLS-2$
			else
				p2Index.setProperty("metadata.repository.factory.order", "content.xml.xz,!"); //$NON-NLS-1$//$NON-NLS-2$
		}
		if (artifacts) {
			if (preserveOriginalFile)
				p2Index.setProperty("artifact.repository.factory.order", "artifacts.xml.xz,artifacts.xml,!"); //$NON-NLS-1$//$NON-NLS-2$
			else
				p2Index.setProperty("artifact.repository.factory.order", "artifacts.xml.xz,!"); //$NON-NLS-1$//$NON-NLS-2$
		}
		p2Index.setProperty("version", "1"); //$NON-NLS-1$//$NON-NLS-2$
		OutputStream output = null;
		try {
			output = new FileOutputStream(new File(repoFolder, "p2.index")); //$NON-NLS-1$
			p2Index.store(output, null);
		} finally {
			if (output != null)
				output.close();
		}
	}
}

Back to the top