Skip to content

Commit

Permalink
Fix BSQ blocks resync from resources on Windows & Linux
Browse files Browse the repository at this point in the history
Prevent a "URI is not hierarchical" IllegalArgumentException from the
expression, 'new File(dirUrl.toURI())', which occurs on Linux & Windows
when listing the resource directory of BSQ blocks. Adapt a solution from
StackOverflow which uses two separate code paths depending on the
environment, into the new method 'FileUtil::listResourceDirectory'.

The issue is caused by the resource URL taking one of the two forms:

  file:/Users/[USER]/Java/bisq/bisq/p2p/out/production/resources/BsqBlocks_BTC_MAINNET
  jar:file:...p2p.jar!/BsqBlocks_BTC_MAINNET

depending on whether the system is OSX or not.
  • Loading branch information
stejbac committed Dec 7, 2021
1 parent 9cdd4ae commit ec58b26
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
45 changes: 45 additions & 0 deletions common/src/main/java/bisq/common/file/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;

import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand All @@ -32,10 +37,14 @@
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -168,6 +177,42 @@ public static void resourceToFile(String resourcePath,
}
}

// adapted from https://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory/48190582#48190582
public static List<String> listResourceDirectory(String directoryName) throws IOException, ResourceNotFoundException {
URL url = Thread.currentThread().getContextClassLoader().getResource(directoryName);
if (url == null) {
throw new ResourceNotFoundException(directoryName);
}
if (url.getProtocol().equals("file")) {
try {
File dir = new File(url.toURI());
String[] filenames = dir.list();
if (filenames != null) {
return List.of(filenames);
}
} catch (URISyntaxException e) {
throw new IOException(e);
}
} else if (url.getProtocol().equals("jar")) {
List<String> filenames = new ArrayList<>();
String dirname = directoryName + "/";
String path = url.getPath();
String jarPath = path.substring(5, path.indexOf("!"));
try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.startsWith(dirname) && !dirname.equals(name)) {
filenames.add(name.substring(dirname.length()));
}
}
}
return filenames;
}
throw new IOException("Failed to list resource directory: " + directoryName);
}

public static void renameFile(File oldFile, File newFile) throws IOException {
if (Utilities.isWindows()) {
// Work around an issue on Windows whereby you can't rename over existing files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import bisq.core.dao.state.model.blockchain.Block;

import bisq.common.config.Config;
import bisq.common.file.FileUtil;
import bisq.common.proto.persistable.PersistenceProtoResolver;

import protobuf.BaseBlock;
Expand All @@ -29,10 +30,6 @@
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.io.FileUtils;

import java.net.URL;

import java.io.File;

import java.util.LinkedList;
Expand Down Expand Up @@ -116,27 +113,19 @@ void copyFromResources(String postFix) {
return;
}

URL dirUrl = getClass().getClassLoader().getResource(resourceDir);
if (dirUrl == null) {
log.info("Directory {} in resources does not exist.", resourceDir);
return;
}
File dir = new File(dirUrl.toURI());
String[] fileNames = dir.list();
if (fileNames == null) {
log.info("No files in directory. {}", dir.getAbsolutePath());
List<String> fileNames = FileUtil.listResourceDirectory(resourceDir);
if (fileNames.isEmpty()) {
log.info("No files in directory. {}", resourceDir);
return;
}
if (!storageDir.exists()) {
storageDir.mkdir();
}
for (String fileName : fileNames) {
URL url = getClass().getClassLoader().getResource(resourceDir + File.separator + fileName);
File resourceFile = new File(url.toURI());
File destinationFile = new File(storageDir, fileName);
FileUtils.copyFile(resourceFile, destinationFile);
FileUtil.resourceToFile(resourceDir + "/" + fileName, destinationFile);
}
log.info("Copying {} resource files took {} ms", fileNames.length, System.currentTimeMillis() - ts);
log.info("Copying {} resource files took {} ms", fileNames.size(), System.currentTimeMillis() - ts);
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down

0 comments on commit ec58b26

Please sign in to comment.