Repository navigation
fix(config): close resource streams #55
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |||||
| import java.nio.file.StandardCopyOption; | ||||||
| import java.util.List; | ||||||
| import java.util.Properties; | ||||||
| import java.util.stream.Stream; | ||||||
| import lombok.extern.slf4j.Slf4j; | ||||||
|
|
||||||
| @Slf4j | ||||||
|
|
@@ -137,39 +138,44 @@ public static boolean isSymbolicLink(File file) throws IOException { | |||||
| */ | ||||||
| public static void copyDatabases(Path src, Path dest, List<String> subDirs) | ||||||
| throws IOException { | ||||||
| // create subdirs, as using parallel() to run, so should create dirs first. | ||||||
| subDirs.forEach(dir -> { | ||||||
| if (isExists(Paths.get(src.toString(), dir).toString())) { | ||||||
| try { | ||||||
| Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS) | ||||||
| .forEach(source -> copy(source, dest.resolve(src.relativize(source)))); | ||||||
| } catch (IOException e) { | ||||||
| logger.error("copy database failed, src: {}, dest: {}, error: {}", | ||||||
| Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage()); | ||||||
| throw new RuntimeException(e); | ||||||
| } | ||||||
| for (String dir : subDirs) { | ||||||
| if (!isExists(Paths.get(src.toString(), dir).toString())) { | ||||||
| continue; | ||||||
| } | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| public static void copyDir(Path src, Path dest, String dir) { | ||||||
| if (isExists(Paths.get(src.toString(), dir).toString())) { | ||||||
| try { | ||||||
| if (createDirIfNotExists(Paths.get(dest.toString(), dir).toString())) { | ||||||
| Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS) | ||||||
| .forEach(source -> copy(source, dest.resolve(src.relativize(source)))); | ||||||
| } else { | ||||||
| throw new IOException(String.format("dest %s create fail ", | ||||||
| Paths.get(dest.toString(), dir))); | ||||||
| } | ||||||
| copyTree(src, dest, dir); | ||||||
| } catch (IOException e) { | ||||||
| logger.error("copy dir failed, src: {}, dest: {}, error: {}", | ||||||
| logger.error("copy database failed, src: {}, dest: {}, error: {}", | ||||||
| Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage()); | ||||||
| throw new RuntimeException(e); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public static void copyDir(Path src, Path dest, String dir) { | ||||||
| if (!isExists(Paths.get(src.toString(), dir).toString())) { | ||||||
| return; | ||||||
| } | ||||||
| try { | ||||||
| if (!createDirIfNotExists(Paths.get(dest.toString(), dir).toString())) { | ||||||
| throw new IOException(String.format("dest %s create fail ", | ||||||
| Paths.get(dest.toString(), dir))); | ||||||
| } | ||||||
| copyTree(src, dest, dir); | ||||||
| } catch (IOException e) { | ||||||
| logger.error("copy dir failed, src: {}, dest: {}, error: {}", | ||||||
| Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage()); | ||||||
| throw new RuntimeException(e); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private static void copyTree(Path src, Path dest, String dir) throws IOException { | ||||||
| try (Stream<Path> paths = Files.walk( | ||||||
| Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents
Suggested change
|
||||||
| paths.forEach(source -> copy(source, dest.resolve(src.relativize(source)))); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| public static void copy(Path source, Path dest) { | ||||||
| try { | ||||||
| // create hard link when file is .sst | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package org.tron.plugins.utils; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import org.junit.Assert; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
|
|
||
| public class FileUtilsTest { | ||
|
|
||
| @Rule | ||
| public final TemporaryFolder folder = new TemporaryFolder(); | ||
|
|
||
| @Test | ||
| public void testCopyDatabasesCopiesTreeAfterMissingDirectory() throws IOException { | ||
| Path src = createSourceTree(); | ||
| Path dest = folder.newFolder().toPath(); | ||
|
|
||
| FileUtils.copyDatabases(src, dest, Arrays.asList("missing", "database")); | ||
|
|
||
| Assert.assertFalse(Files.exists(dest.resolve("missing"))); | ||
| Assert.assertArrayEquals(new byte[]{1, 2, 3}, | ||
| Files.readAllBytes(dest.resolve("database/nested/data"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyMethodsSkipMissingSource() throws IOException { | ||
| Path src = folder.newFolder().toPath(); | ||
| Path dest = folder.getRoot().toPath().resolve("missing-dest"); | ||
|
|
||
| FileUtils.copyDatabases(src, dest, Collections.singletonList("missing")); | ||
| FileUtils.copyDir(src, dest, "missing"); | ||
|
|
||
| Assert.assertFalse(Files.exists(dest)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyDatabasesDoesNotCreateDestinationParents() throws IOException { | ||
| Path src = createSourceTree(); | ||
| Path dest = folder.getRoot().toPath().resolve("missing-parent/dest"); | ||
|
|
||
| RuntimeException error = Assert.assertThrows(RuntimeException.class, | ||
| () -> FileUtils.copyDatabases(src, dest, Collections.singletonList("database"))); | ||
|
|
||
| Assert.assertTrue(error.getCause() instanceof IOException); | ||
| Assert.assertFalse(Files.exists(dest.getParent())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyDirCreatesDestinationParents() throws IOException { | ||
| Path src = createSourceTree(); | ||
| Path dest = folder.getRoot().toPath().resolve("missing-parent/dest"); | ||
|
|
||
| FileUtils.copyDir(src, dest, "database"); | ||
|
|
||
| Assert.assertArrayEquals(new byte[]{1, 2, 3}, | ||
| Files.readAllBytes(dest.resolve("database/nested/data"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyMethodsPreserveLeadingSeparatorInDirectory() throws IOException { | ||
| Path src = createSourceTree(); | ||
| Path databasesDest = folder.newFolder().toPath(); | ||
| Path directoryDest = folder.newFolder().toPath(); | ||
| String dir = File.separator + "database"; | ||
|
|
||
| FileUtils.copyDatabases(src, databasesDest, Collections.singletonList(dir)); | ||
| FileUtils.copyDir(src, directoryDest, dir); | ||
|
|
||
| Assert.assertArrayEquals(new byte[]{1, 2, 3}, | ||
| Files.readAllBytes(databasesDest.resolve("database/nested/data"))); | ||
| Assert.assertArrayEquals(new byte[]{1, 2, 3}, | ||
| Files.readAllBytes(directoryDest.resolve("database/nested/data"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyDirWrapsDestinationCreationFailure() throws IOException { | ||
| Path src = createSourceTree(); | ||
| Path dest = folder.newFile().toPath(); | ||
|
|
||
| RuntimeException error = Assert.assertThrows(RuntimeException.class, | ||
| () -> FileUtils.copyDir(src, dest, "database")); | ||
|
|
||
| Assert.assertTrue(error.getCause() instanceof IOException); | ||
| Assert.assertTrue(error.getCause().getMessage().contains("create fail")); | ||
| } | ||
|
|
||
| private Path createSourceTree() throws IOException { | ||
| Path src = folder.newFolder().toPath(); | ||
| Path nested = Files.createDirectories(src.resolve("database/nested")); | ||
| Files.write(nested.resolve("data"), new byte[]{1, 2, 3}); | ||
| return src; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.