Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2caadb6c83e7b6804b8bca13fdb37e4bffa5a9a8 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*******************************************************************************
 * Copyright (c) 2013, 2017 Ericsson AB and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Ericsson AB - initial API and implementation
 *     Ericsson AB (Hamdan Msheik) - bug- 432167
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.ui.sdk.scheduler;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.internal.p2.touchpoint.eclipse.AgentFromInstall;
import org.eclipse.equinox.p2.core.IProvisioningAgent;

public class PreviousConfigurationFinder {

	private static final Pattern path = Pattern.compile("(.+?)_{1}?([0-9\\.]+)_{1}?(\\d+)(_*?([^_].*)|$)"); //$NON-NLS-1$

	public static class Identifier implements Comparable<Identifier> {
		private static final String DELIM = ". _-"; //$NON-NLS-1$
		private int major, minor, service;

		public Identifier(int major, int minor, int service) {
			super();
			this.major = major;
			this.minor = minor;
			this.service = service;
		}

		/**
		 * @throws NumberFormatException if cannot parse the major and minor version components
		 */
		Identifier(String versionString) {
			super();
			StringTokenizer tokenizer = new StringTokenizer(versionString, DELIM);

			// major
			if (tokenizer.hasMoreTokens())
				major = Integer.parseInt(tokenizer.nextToken());

			// minor
			if (tokenizer.hasMoreTokens())
				minor = Integer.parseInt(tokenizer.nextToken());

			try {
				// service
				if (tokenizer.hasMoreTokens())
					service = Integer.parseInt(tokenizer.nextToken());
			} catch (NumberFormatException nfe) {
				// ignore the service qualifier in that case and default to 0
				// this will allow us to tolerate other non-conventional version numbers 
			}
		}

		/**
		 * Returns true if this id is considered to be greater than or equal to the given baseline.
		 * e.g. 
		 * 1.2.9 >= 1.3.1 -> false
		 * 1.3.0 >= 1.3.1 -> false
		 * 1.3.1 >= 1.3.1 -> true
		 * 1.3.2 >= 1.3.1 -> true
		 * 2.0.0 >= 1.3.1 -> true
		 */
		boolean isGreaterEqualTo(Identifier minimum) {
			if (major < minimum.major)
				return false;
			if (major > minimum.major)
				return true;
			// major numbers are equivalent so check minor
			if (minor < minimum.minor)
				return false;
			if (minor > minimum.minor)
				return true;
			// minor numbers are equivalent so check service
			return service >= minimum.service;
		}

		@Override
		public boolean equals(Object other) {
			if (!(other instanceof Identifier))
				return false;
			Identifier o = (Identifier) other;
			if (major == o.major && minor == o.minor && service == o.service)
				return true;
			return false;
		}

		@Override
		public String toString() {
			return "" + major + '.' + minor + '.' + service; //$NON-NLS-1$
		}

		@Override
		public int compareTo(Identifier o) {

			if (o != null) {
				if (major < o.major) {
					return -1;
				} else if (major > o.major) {
					return 1;
				} else if (minor < o.minor) {
					return -1;
				} else if (minor > o.minor) {
					return 1;
				} else if (service < o.service) {
					return -1;
				} else if (service > o.service) {
					return 1;
				} else {
					return 0;
				}
			}

			return 1;
		}
	}

	public static class ConfigurationDescriptor {
		String productId;
		Identifier version;
		String installPathHashcode;
		File configFolder;
		String os_ws_arch;

		public ConfigurationDescriptor(String productId, Identifier version, String installPathHashcode, String platformConfig, File configFolder) {
			this.productId = productId;
			this.version = version;
			this.installPathHashcode = installPathHashcode;
			this.configFolder = configFolder;
			this.os_ws_arch = platformConfig;
		}

		public String getProductId() {
			return productId;
		}

		public Identifier getVersion() {
			return version;
		}

		public String getInstallPathHashcode() {
			return installPathHashcode;
		}

		public File getConfig() {
			return configFolder;
		}

		public String getPlatformConfig() {
			return os_ws_arch;
		}
	}

	public static class ConfigurationDescriptorComparator implements Comparator<ConfigurationDescriptor> {

		// compare ConfigurationDescriptor according to their versions and when equals according to their lastModified field
		@Override
		public int compare(ConfigurationDescriptor o1, ConfigurationDescriptor o2) {
			int result = -1;
			if (o1 != null && o2 != null) {
				if (o1.getVersion().compareTo(o2.getVersion()) != 0) {
					result = o1.getVersion().compareTo(o2.getVersion());
				} else {
					if (o1.getConfig().lastModified() > o2.getConfig().lastModified()) {
						result = 1;
					} else if (o1.getConfig().lastModified() < o2.getConfig().lastModified()) {
						result = -1;
					} else
						result = 0;
				}
			} else if (o1 == null) {
				result = -1;
			} else if (o2 == null) {
				result = 1;
			}
			return result;
		}

	}

	private File currentConfig;

	public PreviousConfigurationFinder(File currentConfiguration) {
		currentConfig = currentConfiguration;
	}

	public ConfigurationDescriptor extractConfigurationData(File candidate) {
		Matcher m = path.matcher(candidate.getName());
		if (!m.matches())
			return null;
		return new ConfigurationDescriptor(m.group(1), new Identifier(m.group(2)), m.group(3), m.group(5), candidate.getAbsoluteFile());
	}

	public IProvisioningAgent findPreviousInstalls(File searchRoot, File installFolder) {
		List<ConfigurationDescriptor> potentialConfigurations = readPreviousConfigurations(searchRoot);
		ConfigurationDescriptor runningConfiguration = getConfigdataFromProductFile(installFolder);
		if (runningConfiguration == null)
			return null;
		ConfigurationDescriptor match = findMostRelevantConfigurationFromInstallHashDir(potentialConfigurations, runningConfiguration);
		if (match == null)
			match = findMostRelevantConfigurationFromProductId(potentialConfigurations, runningConfiguration);
		if (match == null)
			match = findSpecifiedConfiguration(searchRoot);
		if (match == null)
			return null;
		return AgentFromInstall.createAgentFrom(AutomaticUpdatePlugin.getDefault().getAgentProvider(), null, new File(match.getConfig(), "configuration"), null); //$NON-NLS-1$

	}

	public ConfigurationDescriptor findSpecifiedConfiguration(File searchRoot) {
		final String prefixesAsString = System.getProperty("p2.forcedMigrationLocation"); //$NON-NLS-1$
		if (prefixesAsString == null)
			return null;

		String[] prefixes = prefixesAsString.split(","); //$NON-NLS-1$
		for (String prefix : prefixes) {
			final String p = prefix;
			File[] match = searchRoot.listFiles((FileFilter) candidate -> {
				if (!candidate.isDirectory())
					return false;
				if (currentConfig.equals(candidate))
					return false;
				return candidate.getName().contains(p);
			});
			if (match.length != 0)
				return new ConfigurationDescriptor("unknown", new Identifier("0.0.0"), "unknown", "unknown", match[0]); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
		}
		return null;
	}

	private ConfigurationDescriptor getConfigdataFromProductFile(File installFolder) {
		Object[] productFileInfo = loadEclipseProductFile(installFolder);
		//Contrarily  to the runtime, when the .eclipseproduct can't be found, we don't fallback to org.eclipse.platform. 
		if (productFileInfo.length == 0)
			return null;
		return new ConfigurationDescriptor((String) productFileInfo[0], (Identifier) productFileInfo[1], getInstallDirHash(installFolder), Platform.getOS() + '_' + Platform.getWS() + '_' + Platform.getOSArch(), null);
	}

	public ConfigurationDescriptor findMostRelevantConfigurationFromInstallHashDir(List<ConfigurationDescriptor> configurations, ConfigurationDescriptor configToMatch) {
		ConfigurationDescriptor bestMatch = null;
		int numberOfcriteriaMet = 0;
		for (ConfigurationDescriptor candidate : configurations) {
			int criteriaMet = 0;
			if (!candidate.getInstallPathHashcode().equals(configToMatch.getInstallPathHashcode())) {
				continue;
			}
			criteriaMet++;

			if (configToMatch.getProductId().equals(candidate.getProductId()) && //
					configToMatch.getPlatformConfig().equals(candidate.getPlatformConfig()) && //
					(!candidate.getVersion().isGreaterEqualTo(configToMatch.getVersion()))) {
				//We have a match
				criteriaMet++;
			}

			if (criteriaMet == 0)
				continue;
			if (criteriaMet > numberOfcriteriaMet) {
				bestMatch = candidate;
				numberOfcriteriaMet = criteriaMet;
			} else if (criteriaMet == numberOfcriteriaMet) {
				if (bestMatch.getVersion().equals(candidate.getVersion())) {
					if (bestMatch.getConfig().lastModified() < candidate.getConfig().lastModified()) {
						bestMatch = candidate;
					}
				} else {
					if (candidate.getVersion().isGreaterEqualTo(bestMatch.getVersion()))
						bestMatch = candidate;
				}
			}
		}
		return bestMatch;
	}

	//Out of a set of configuration, find the one with the most similar product info.
	public ConfigurationDescriptor findMostRelevantConfigurationFromProductId(List<ConfigurationDescriptor> configurations, ConfigurationDescriptor configToMatch) {
		ConfigurationDescriptor bestMatch = null;

		List<ConfigurationDescriptor> candidates = new ArrayList<>();
		List<ConfigurationDescriptor> candidatesWithUnkonwArchitecture = new ArrayList<>();
		for (ConfigurationDescriptor candidate : configurations) {
			if (configToMatch.getProductId().equals(candidate.getProductId()) && configToMatch.getVersion().isGreaterEqualTo(candidate.getVersion())) {
				if (configToMatch.getPlatformConfig().equals(candidate.getPlatformConfig())) {
					candidates.add(candidate);
				} else { //candidate.getPlatformConfig() returns null in legacy installation prior to 4.x.x releases
					candidatesWithUnkonwArchitecture.add(candidate);
				}
			}
		}

		if (!candidates.isEmpty()) {
			Collections.sort(candidates, new ConfigurationDescriptorComparator());
			bestMatch = candidates.get(candidates.size() - 1);
		}

		if (bestMatch == null) {
			if (!candidatesWithUnkonwArchitecture.isEmpty()) {
				Collections.sort(candidatesWithUnkonwArchitecture, new ConfigurationDescriptorComparator());
				bestMatch = candidatesWithUnkonwArchitecture.get(candidatesWithUnkonwArchitecture.size() - 1);
			}
		}

		return bestMatch;
	}

	//Load the .eclipseproduct file in the base of the installation. This logic is very similar to the one found in the launcher
	private Object[] loadEclipseProductFile(File installDir) {
		final String ECLIPSE = "eclipse"; //$NON-NLS-1$
		final String PRODUCT_SITE_MARKER = ".eclipseproduct"; //$NON-NLS-1$
		final String PRODUCT_SITE_ID = "id"; //$NON-NLS-1$
		final String PRODUCT_SITE_VERSION = "version"; //$NON-NLS-1$

		File eclipseProduct = new File(installDir, PRODUCT_SITE_MARKER);
		String appId = null;
		Identifier appVersion = null;
		if (eclipseProduct.exists()) {
			Properties props = new Properties();
			try (FileInputStream is = new FileInputStream(eclipseProduct)) {
				props.load(is);
				appId = props.getProperty(PRODUCT_SITE_ID);
				if (appId == null || appId.trim().length() == 0)
					appId = ECLIPSE;
				String version = props.getProperty(PRODUCT_SITE_VERSION);
				if (version == null || version.trim().length() == 0)
					appVersion = new Identifier(0, 0, 0);
				else
					appVersion = new Identifier(version);
			} catch (IOException e) {
				return new String[0];
			}
		} else {
			return new String[0];
		}
		return new Object[] {appId, appVersion};
	}

	//Iterate through a folder to look for potential configuration folders and reify them.
	public List<ConfigurationDescriptor> readPreviousConfigurations(File configurationFolder) {
		File[] candidates = configurationFolder.listFiles();
		List<ConfigurationDescriptor> configurations = new ArrayList<>(candidates.length);
		for (File candidate : candidates) {
			if (!candidate.isDirectory())
				continue;
			if (candidate.equals(currentConfig))
				continue;
			ConfigurationDescriptor tmp = extractConfigurationData(candidate);
			if (tmp != null)
				configurations.add(tmp);
		}
		return configurations;
	}

	//This code computes the hashCode of the install location. 
	//This is a simplified version of the code that the launcher executes.
	private String getInstallDirHash(File installFolder) {
		try {
			return Integer.toString(installFolder.getCanonicalPath().hashCode());
		} catch (IOException e) {
			return ""; //$NON-NLS-1$
		}
	}
}

Back to the top