Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f5f9bb3ed9c12b5361e9f6396364100700ef1054 (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
/*******************************************************************************
 * 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.arduino.core.internal.board;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.eclipse.cdt.arduino.core.internal.Activator;
import org.eclipse.cdt.arduino.core.internal.ArduinoPreferences;
import org.eclipse.cdt.arduino.core.internal.Messages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import com.google.gson.Gson;

// Closeable isn't API yet but it's recommended.
public class ArduinoBoardManager {

	public static final ArduinoBoardManager instance = new ArduinoBoardManager();

	// Build tool ids
	public static final String BOARD_OPTION_ID = "org.eclipse.cdt.arduino.option.board"; //$NON-NLS-1$
	public static final String PLATFORM_OPTION_ID = "org.eclipse.cdt.arduino.option.platform"; //$NON-NLS-1$
	public static final String PACKAGE_OPTION_ID = "org.eclipse.cdt.arduino.option.package"; //$NON-NLS-1$
	public static final String AVR_TOOLCHAIN_ID = "org.eclipse.cdt.arduino.toolChain.avr"; //$NON-NLS-1$

	private Path packageIndexPath = ArduinoPreferences.getArduinoHome().resolve("package_index.json"); //$NON-NLS-1$
	private PackageIndex packageIndex;

	public ArduinoBoardManager() {
		new Job(Messages.ArduinoBoardManager_0) {
			protected IStatus run(IProgressMonitor monitor) {
				try {
					URL url = new URL("http://downloads.arduino.cc/packages/package_index.json"); //$NON-NLS-1$
					Files.createDirectories(packageIndexPath.getParent());
					Files.copy(url.openStream(), packageIndexPath, StandardCopyOption.REPLACE_EXISTING);
				} catch (IOException e) {
					return new Status(IStatus.ERROR, Activator.getId(), e.getLocalizedMessage(), e);
				}
				return Status.OK_STATUS;
			}
		}.schedule();
	}

	public PackageIndex getPackageIndex() throws CoreException {
		if (packageIndex == null) {
			try (FileReader reader = new FileReader(packageIndexPath.toFile())) {
				packageIndex = new Gson().fromJson(reader, PackageIndex.class);
				packageIndex.setOwners(this);
			} catch (IOException e) {
				throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Reading package index", e));
			}
		}
		return packageIndex;
	}

	public ArduinoBoard getBoard(String boardName, String platformName, String packageName) throws CoreException {
		return getPackageIndex().getPackage(packageName).getPlatform(platformName).getBoard(boardName);
	}

	public List<ArduinoBoard> getBoards() throws CoreException {
		List<ArduinoBoard> boards = new ArrayList<>();
		for (ArduinoPackage pkg : getPackageIndex().getPackages()) {
			for (ArduinoPlatform platform : pkg.getLatestPlatforms()) {
				boards.addAll(platform.getBoards());
			}
		}
		return boards;
	}

	public List<ArduinoBoard> getInstalledBoards() throws CoreException {
		List<ArduinoBoard> boards = new ArrayList<>();
		for (ArduinoPackage pkg : getPackageIndex().getPackages()) {
			for (ArduinoPlatform platform : pkg.getInstalledPlatforms()) {
				boards.addAll(platform.getBoards());
			}
		}
		return boards;
	}

	public ArduinoTool getTool(String packageName, String toolName, String version) {
		ArduinoPackage pkg = packageIndex.getPackage(packageName);
		return pkg != null ? pkg.getTool(toolName, version) : null;
	}

	public static IStatus downloadAndInstall(String url, String archiveFileName, Path installPath,
			IProgressMonitor monitor) {
		try {
			URL dl = new URL(url);
			Path dlDir = ArduinoPreferences.getArduinoHome().resolve("downloads");
			Files.createDirectories(dlDir);
			Path archivePath = dlDir.resolve(archiveFileName);
			Files.copy(dl.openStream(), archivePath, StandardCopyOption.REPLACE_EXISTING);

			boolean isWin = Platform.getOS().equals(Platform.OS_WIN32);

			// extract
			ArchiveInputStream archiveIn = null;
			try {
				String compressor = null;
				String archiver = null;
				if (archiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$
					compressor = CompressorStreamFactory.BZIP2;
					archiver = ArchiveStreamFactory.TAR;
				} else if (archiveFileName.endsWith(".tar.gz") || archiveFileName.endsWith(".tgz")) { //$NON-NLS-1$ //$NON-NLS-2$
					compressor = CompressorStreamFactory.GZIP;
					archiver = ArchiveStreamFactory.TAR;
				} else if (archiveFileName.endsWith(".tar.xz")) { //$NON-NLS-1$
					compressor = CompressorStreamFactory.XZ;
					archiver = ArchiveStreamFactory.TAR;
				} else if (archiveFileName.endsWith(".zip")) { //$NON-NLS-1$
					archiver = ArchiveStreamFactory.ZIP;
				}

				InputStream in = new BufferedInputStream(new FileInputStream(archivePath.toFile()));
				if (compressor != null) {
					in = new CompressorStreamFactory().createCompressorInputStream(compressor, in);
				}
				archiveIn = new ArchiveStreamFactory().createArchiveInputStream(archiver, in);

				for (ArchiveEntry entry = archiveIn.getNextEntry(); entry != null; entry = archiveIn.getNextEntry()) {
					if (entry.isDirectory()) {
						continue;
					}

					Path entryPath = installPath.resolve(entry.getName());
					Files.createDirectories(entryPath.getParent());

					if (entry instanceof TarArchiveEntry) {
						TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
						if (tarEntry.isLink()) {
							Path linkPath = installPath.resolve(tarEntry.getLinkName());
							Files.createSymbolicLink(entryPath, entryPath.getParent().relativize(linkPath));
						} else {
							Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
						}
						if (!isWin) {
							int mode = tarEntry.getMode();
							Files.setPosixFilePermissions(entryPath, toPerms(mode));
						}
					} else {
						Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
					}
				}
			} finally {
				if (archiveIn != null) {
					archiveIn.close();
				}
			}

			// Fix up directory
			File[] children = installPath.toFile().listFiles();
			if (children.length == 1 && children[0].isDirectory()) {
				// make that directory the install path
				Path childPath = children[0].toPath();
				Path tmpPath = installPath.getParent().resolve("_t"); //$NON-NLS-1$
				Files.move(childPath, tmpPath);
				Files.delete(installPath);
				Files.move(tmpPath, installPath);
			}
			return Status.OK_STATUS;
		} catch (IOException | CompressorException | ArchiveException e) {
			return new Status(IStatus.ERROR, Activator.getId(), "Installing Platform", e);
		}
	}

	private static Set<PosixFilePermission> toPerms(int mode) {
		Set<PosixFilePermission> perms = new HashSet<>();
		if ((mode & 0400) != 0) {
			perms.add(PosixFilePermission.OWNER_READ);
		}
		if ((mode & 0200) != 0) {
			perms.add(PosixFilePermission.OWNER_WRITE);
		}
		if ((mode & 0100) != 0) {
			perms.add(PosixFilePermission.OWNER_EXECUTE);
		}
		if ((mode & 0040) != 0) {
			perms.add(PosixFilePermission.GROUP_READ);
		}
		if ((mode & 0020) != 0) {
			perms.add(PosixFilePermission.GROUP_WRITE);
		}
		if ((mode & 0010) != 0) {
			perms.add(PosixFilePermission.GROUP_EXECUTE);
		}
		if ((mode & 0004) != 0) {
			perms.add(PosixFilePermission.OTHERS_READ);
		}
		if ((mode & 0002) != 0) {
			perms.add(PosixFilePermission.OTHERS_WRITE);
		}
		if ((mode & 0001) != 0) {
			perms.add(PosixFilePermission.OTHERS_EXECUTE);
		}
		return perms;
	}

}

Back to the top