Compare commits

...

2 commits

Author SHA1 Message Date
Gravita
fb2883d215 [FIX] Possible multithreading issue 2023-03-22 21:46:40 +07:00
Gravita
dc664c7ee2 [FIX] Forbidden modification info 2023-03-22 21:18:08 +07:00
2 changed files with 13 additions and 11 deletions

View file

@ -33,7 +33,6 @@
import pro.gravit.launcher.utils.DirWatcher; import pro.gravit.launcher.utils.DirWatcher;
import pro.gravit.utils.helper.*; import pro.gravit.utils.helper.*;
import javax.swing.*;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandle;
@ -46,6 +45,7 @@
import java.nio.file.SimpleFileVisitor; import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -241,20 +241,23 @@ public static void verifyHDir(Path dir, HashedDir hdir, FileNameMatcher matcher,
// Hash directory and compare (ignore update-only matcher entries, it will break offline-mode) // Hash directory and compare (ignore update-only matcher entries, it will break offline-mode)
HashedDir currentHDir = new HashedDir(dir, matcher, true, digest); HashedDir currentHDir = new HashedDir(dir, matcher, true, digest);
HashedDir.Diff diff = hdir.diff(currentHDir, matcher); HashedDir.Diff diff = hdir.diff(currentHDir, matcher);
AtomicReference<String> latestPath = new AtomicReference<>("unknown");
if (!diff.mismatch.isEmpty() || (checkExtra && !diff.extra.isEmpty())) { if (!diff.mismatch.isEmpty() || (checkExtra && !diff.extra.isEmpty())) {
diff.extra.walk(File.separator, (e, k, v) -> { diff.extra.walk(File.separator, (e, k, v) -> {
if (v.getType().equals(HashedEntry.Type.FILE)) { if (v.getType().equals(HashedEntry.Type.FILE)) {
LogHelper.error("Extra file %s", e); LogHelper.error("Extra file %s", e);
latestPath.set(e);
} else LogHelper.error("Extra %s", e); } else LogHelper.error("Extra %s", e);
return HashedDir.WalkAction.CONTINUE; return HashedDir.WalkAction.CONTINUE;
}); });
diff.mismatch.walk(File.separator, (e, k, v) -> { diff.mismatch.walk(File.separator, (e, k, v) -> {
if (v.getType().equals(HashedEntry.Type.FILE)) { if (v.getType().equals(HashedEntry.Type.FILE)) {
LogHelper.error("Mismatch file %s", e); LogHelper.error("Mismatch file %s", e);
latestPath.set(e);
} else LogHelper.error("Mismatch %s", e); } else LogHelper.error("Mismatch %s", e);
return HashedDir.WalkAction.CONTINUE; return HashedDir.WalkAction.CONTINUE;
}); });
throw new SecurityException(String.format("Forbidden modification: '%s'", IOHelper.getFileName(dir))); throw new SecurityException(String.format("Forbidden modification: '%s' file '%s'", IOHelper.getFileName(dir), latestPath.get()));
} }
} }

View file

@ -19,6 +19,7 @@
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer; import java.util.function.Consumer;
public class Downloader { public class Downloader {
@ -28,7 +29,7 @@ public class Downloader {
private static boolean isNoHttp2; private static boolean isNoHttp2;
protected final HttpClient client; protected final HttpClient client;
protected final ExecutorService executor; protected final ExecutorService executor;
protected final LinkedList<DownloadTask> tasks = new LinkedList<>(); protected final Queue<DownloadTask> tasks = new ConcurrentLinkedDeque<>();
protected CompletableFuture<Void> future; protected CompletableFuture<Void> future;
protected Downloader(HttpClient client, ExecutorService executor) { protected Downloader(HttpClient client, ExecutorService executor) {
this.client = client; this.client = client;
@ -128,15 +129,13 @@ protected DownloadTask sendAsync(AsyncDownloader.SizedFile file, URI baseUri, Pa
IOHelper.createParentDirs(targetDir.resolve(file.filePath)); IOHelper.createParentDirs(targetDir.resolve(file.filePath));
ProgressTrackingBodyHandler<Path> bodyHandler = makeBodyHandler(targetDir.resolve(file.filePath), callback); ProgressTrackingBodyHandler<Path> bodyHandler = makeBodyHandler(targetDir.resolve(file.filePath), callback);
CompletableFuture<HttpResponse<Path>> future = client.sendAsync(makeHttpRequest(baseUri, file.urlPath), bodyHandler); CompletableFuture<HttpResponse<Path>> future = client.sendAsync(makeHttpRequest(baseUri, file.urlPath), bodyHandler);
var ref = new Object() { AtomicReference<DownloadTask> task = new AtomicReference<>(null);
DownloadTask task = null; task.set(new DownloadTask(bodyHandler, future.thenApply((e) -> {
}; tasks.remove(task.get());
ref.task = new DownloadTask(bodyHandler, future.thenApply((e) -> {
tasks.remove(ref.task);
return e; return e;
})); })));
tasks.add(ref.task); tasks.add(task.get());
return ref.task; return task.get();
} }
protected HttpRequest makeHttpRequest(URI baseUri, String filePath) throws URISyntaxException { protected HttpRequest makeHttpRequest(URI baseUri, String filePath) throws URISyntaxException {