Skip to content

Fix crash due to index json parse errors and permission problems #7964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ protected void onUpdatePressed() {
try {
setProgressVisible(true, "");
installer.updateIndex(this::setProgress);
((LibrariesIndexTableModel) contribModel).update();
onIndexesUpdated();
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@

import cc.arduino.Constants;
import cc.arduino.contributions.packages.ContributedPlatform;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.mrbean.MrBeanModule;
import org.apache.commons.compress.utils.IOUtils;
Expand Down Expand Up @@ -73,11 +76,14 @@ public LibrariesIndexer(File preferencesFolder) {
}

public void parseIndex() throws IOException {
index = new EmptyLibrariesIndex(); // Fallback

if (!indexFile.exists()) {
index = new EmptyLibrariesIndex();
} else {
parseIndex(indexFile);
return;
}

parseIndex(indexFile);

// TODO: resolve libraries inner references
}

Expand All @@ -98,8 +104,12 @@ private void parseIndex(File file) throws IOException {
.forEach(library -> library.setCategory("Uncategorized"));

index = newIndex;
} catch (JsonParseException | JsonMappingException e) {
System.err.println(
format(tr("Error parsing libraries index: {0}\nTry to open the Library Manager to update the libraries index."),
e.getMessage()));
} catch (Exception e) {
System.err.println("Error parsing library.index:" + e.getMessage());
System.err.println(format(tr("Error reading libraries index: {0}"), e.getMessage()));
} finally {
IOUtils.closeQuietly(indexIn);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public LibraryInstaller(Platform platform) {
}

public synchronized void updateIndex(ProgressListener progressListener) throws Exception {
final MultiStepProgress progress = new MultiStepProgress(2);
final MultiStepProgress progress = new MultiStepProgress(3);

DownloadableContributionsDownloader downloader = new DownloadableContributionsDownloader(BaseNoGui.librariesIndexer.getStagingFolder());
// Step 1: Download index
Expand All @@ -79,7 +79,10 @@ public synchronized void updateIndex(ProgressListener progressListener) throws E
if (!tmpFile.renameTo(outputFile))
throw new Exception(tr("An error occurred while updating libraries index!"));

// Step 2: Rescan index
// Step 2: Parse index
BaseNoGui.librariesIndexer.parseIndex();

// Step 3: Rescan index
rescanLibraryIndex(progress, progressListener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.mrbean.MrBeanModule;
import org.apache.commons.compress.utils.IOUtils;
import processing.app.I18n;
import processing.app.Platform;
import processing.app.PreferencesData;
import processing.app.debug.TargetPackage;
Expand All @@ -55,6 +54,7 @@
import java.util.*;
import java.util.stream.Collectors;

import static processing.app.I18n.format;
import static processing.app.I18n.tr;
import static processing.app.helpers.filefilters.OnlyDirs.ONLY_DIRS;

Expand Down Expand Up @@ -100,13 +100,17 @@ public void parseIndex() throws Exception {
// Overlay 3rd party indexes
File[] indexFiles = preferencesFolder.listFiles(new TestPackageIndexFilenameFilter(new PackageIndexFilenameFilter(Constants.DEFAULT_INDEX_FILE_NAME)));

for (File indexFile : indexFiles) {
try {
mergeContributions(indexFile);
} catch (JsonProcessingException e) {
System.err.println(I18n.format(tr("Skipping contributed index file {0}, parsing error occured:"), indexFile));
System.err.println(e);
if (indexFiles != null) {
for (File indexFile : indexFiles) {
try {
mergeContributions(indexFile);
} catch (JsonProcessingException e) {
System.err.println(format(tr("Skipping contributed index file {0}, parsing error occured:"), indexFile));
System.err.println(e);
}
}
} else {
System.err.println(format(tr("Error reading package indexes folder: {0}\n(maybe a permission problem?)"), preferencesFolder));
}

// Fill tools and toolsDependency cross references
Expand Down Expand Up @@ -156,7 +160,7 @@ private void mergeContributions(File indexFile) throws IOException {
} else {
if (contributedPackage.isTrusted() || !isPackageNameProtected(contributedPackage)) {
if (isPackageNameProtected(contributedPackage) && trustall) {
System.err.println(I18n.format(tr("Warning: forced trusting untrusted contributions")));
System.err.println(format(tr("Warning: forced trusting untrusted contributions")));
}
List<ContributedPlatform> platforms = contributedPackage.getPlatforms();
if (platforms == null) {
Expand Down