28
28
*/
29
29
package cc .arduino .contributions .packages ;
30
30
31
+ import cc .arduino .contributions .GPGDetachedSignatureVerifier ;
31
32
import cc .arduino .utils .ArchiveExtractor ;
32
33
import cc .arduino .utils .MultiStepProgress ;
33
34
import cc .arduino .utils .Progress ;
36
37
import org .apache .commons .exec .CommandLine ;
37
38
import org .apache .commons .exec .Executor ;
38
39
import processing .app .BaseNoGui ;
40
+ import processing .app .I18n ;
41
+ import processing .app .PreferencesData ;
39
42
import processing .app .helpers .FileUtils ;
40
43
import processing .app .helpers .filefilters .OnlyDirs ;
41
44
import processing .app .tools .CollectStdOutStdErrExecutor ;
44
47
import java .io .File ;
45
48
import java .io .IOException ;
46
49
import java .net .URL ;
47
- import java .util .Collection ;
48
- import java .util .Iterator ;
49
- import java .util .LinkedList ;
50
- import java .util .List ;
50
+ import java .util .*;
51
51
52
52
import static processing .app .I18n ._ ;
53
53
import static processing .app .I18n .format ;
54
54
55
55
public class ContributionInstaller {
56
56
57
- private static final String PACKAGE_INDEX_URL ;
58
-
59
- static {
60
- String extenalPackageIndexUrl = System .getProperty ("PACKAGE_INDEX_URL" );
61
- if (extenalPackageIndexUrl != null && !"" .equals (extenalPackageIndexUrl )) {
62
- PACKAGE_INDEX_URL = extenalPackageIndexUrl ;
63
- } else {
64
- PACKAGE_INDEX_URL = "http://downloads.arduino.cc/packages/package_index.json" ;
65
- }
66
- }
67
-
68
57
private final ContributionsIndexer indexer ;
69
58
private final DownloadableContributionsDownloader downloader ;
70
59
@@ -239,22 +228,46 @@ public List<String> remove(ContributedPlatform platform) {
239
228
}
240
229
241
230
public List <String > updateIndex () throws Exception {
242
- List <String > errors = new LinkedList <String >();
243
231
MultiStepProgress progress = new MultiStepProgress (1 );
244
232
245
- downloadIndex (progress , PACKAGE_INDEX_URL );
246
- try {
247
- downloadIndex (progress , PACKAGE_INDEX_URL + ".sig" );
248
- } catch (Exception e ) {
249
- //ignore errors
233
+ List <String > downloadedPackageIndexFilesAccumulator = new LinkedList <String >();
234
+ downloadIndexAndSignature (progress , downloadedPackageIndexFilesAccumulator , Constants .PACKAGE_INDEX_URL );
235
+
236
+ Set <String > packageIndexURLs = new HashSet <String >();
237
+ String additionalURLs = PreferencesData .get (Constants .PREFERENCES_BOARDS_MANAGER_ADDITIONAL_URLS , "" );
238
+ if (!"" .equals (additionalURLs )) {
239
+ packageIndexURLs .addAll (Arrays .asList (additionalURLs .split ("," )));
240
+ }
241
+
242
+ for (String packageIndexURL : packageIndexURLs ) {
243
+ downloadIndexAndSignature (progress , downloadedPackageIndexFilesAccumulator , packageIndexURL );
250
244
}
251
245
252
246
progress .stepDone ();
253
247
254
- return errors ;
248
+ return downloadedPackageIndexFilesAccumulator ;
249
+ }
250
+
251
+ private void downloadIndexAndSignature (MultiStepProgress progress , List <String > downloadedPackagedIndexFilesAccumulator , String packageIndexUrl ) throws Exception {
252
+ File packageIndex = downloadIndex (progress , packageIndexUrl );
253
+ downloadedPackagedIndexFilesAccumulator .add (packageIndex .getName ());
254
+ try {
255
+ File packageIndexSignature = downloadIndex (progress , packageIndexUrl + ".sig" );
256
+ boolean signatureVerified = new GPGDetachedSignatureVerifier ().verify (packageIndex , packageIndexSignature , new File (BaseNoGui .getContentFile ("lib" ), "public.gpg.key" ));
257
+ if (signatureVerified ) {
258
+ downloadedPackagedIndexFilesAccumulator .add (packageIndexSignature .getName ());
259
+ } else {
260
+ downloadedPackagedIndexFilesAccumulator .remove (packageIndex .getName ());
261
+ packageIndex .delete ();
262
+ packageIndexSignature .delete ();
263
+ System .err .println (I18n .format (_ ("{0} file signature verification failed. File ignored." ), packageIndexUrl ));
264
+ }
265
+ } catch (Exception e ) {
266
+ //ignore errors
267
+ }
255
268
}
256
269
257
- private void downloadIndex (MultiStepProgress progress , String packageIndexUrl ) throws Exception {
270
+ private File downloadIndex (MultiStepProgress progress , String packageIndexUrl ) throws Exception {
258
271
String statusText = _ ("Downloading platforms index..." );
259
272
URL url = new URL (packageIndexUrl );
260
273
String [] urlPathParts = url .getFile ().split ("/" );
@@ -269,9 +282,24 @@ private void downloadIndex(MultiStepProgress progress, String packageIndexUrl) t
269
282
if (!tmpFile .renameTo (outputFile )) {
270
283
throw new Exception ("An error occurred while updating platforms index!" );
271
284
}
285
+
286
+ return outputFile ;
272
287
}
273
288
274
289
protected void onProgress (Progress progress ) {
275
290
// Empty
276
291
}
292
+
293
+ public void deleteUnknownFiles (List <String > downloadedPackageIndexFiles ) {
294
+ File preferencesFolder = indexer .getIndexFile ("." ).getParentFile ();
295
+ File [] additionalPackageIndexFiles = preferencesFolder .listFiles (new PackageIndexFilenameFilter (Constants .DEFAULT_INDEX_FILE_NAME ));
296
+ if (additionalPackageIndexFiles == null ) {
297
+ return ;
298
+ }
299
+ for (File additionalPackageIndexFile : additionalPackageIndexFiles ) {
300
+ if (!downloadedPackageIndexFiles .contains (additionalPackageIndexFile .getName ())) {
301
+ additionalPackageIndexFile .delete ();
302
+ }
303
+ }
304
+ }
277
305
}
0 commit comments