Skip to content
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

Fix resync BSQ blocks from resources #5900

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
38 changes: 38 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,12 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand All @@ -33,9 +39,12 @@
import java.io.InputStream;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import lombok.extern.slf4j.Slf4j;

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

public static List<String> listResourceDirectory(String directoryName) throws IOException, ResourceNotFoundException {
URL url = Thread.currentThread().getContextClassLoader().getResource(directoryName);
if (url == null) {
throw new ResourceNotFoundException(directoryName);
}
URI uri;
try {
uri = url.toURI();
} catch (URISyntaxException e) {
throw new IOException(e);
}
if (url.getProtocol().equals("file")) {
File dir = new File(uri);
String[] filenames = dir.list();
if (filenames != null) {
return List.of(filenames);
}
} else if (url.getProtocol().equals("jar")) {
try (FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
Stream<Path> filePaths = java.nio.file.Files.walk(fileSystem.getPath(directoryName), 1)) { //NOPMD
return filePaths
.skip(1)
.map(path -> path.getFileName().toString())
.collect(Collectors.toUnmodifiableList());
}
}
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