From 7f3f47c4c827e55bcdf85b9c29b3ec29fd8b9b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Tue, 16 Jun 2020 11:50:14 +0200 Subject: [PATCH] Close file lists Though we could not reproduce an error even with real big file structures (100 directories, containing each another 100 directories with 100 files each), it may be safer to make sure the streams will really be closed in the end and recommended in the javadoc description for Files#list (https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#list-java.nio.file.Path-) --- CHANGELOG.md | 1 + .../scm/update/repository/BaseMigrationStrategy.java | 9 +++++---- .../scm/update/repository/CopyMigrationStrategy.java | 8 +++++--- .../scm/update/repository/InlineMigrationStrategy.java | 9 ++++++--- .../scm/update/repository/MoveMigrationStrategy.java | 9 ++++++--- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e1db72664..d16d7df439 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixes configuration of jetty listener address with system property `jetty.host` ([#1173](https://github.com/scm-manager/scm-manager/pull/1173), [#1174](https://github.com/scm-manager/scm-manager/pull/1174)) - Fixes loading plugin bundles with context path `/` ([#1182](https://github.com/scm-manager/scm-manager/pull/1182/files), [#1181](https://github.com/scm-manager/scm-manager/issues/1181)) - Sets the new plugin center URL once ([#1184](https://github.com/scm-manager/scm-manager/pull/1184)) +- Close file lists in migration ([#1191](https://github.com/scm-manager/scm-manager/pull/1191)) ## [2.0.0] - 2020-06-04 ### Added diff --git a/scm-webapp/src/main/java/sonia/scm/update/repository/BaseMigrationStrategy.java b/scm-webapp/src/main/java/sonia/scm/update/repository/BaseMigrationStrategy.java index 27d8732b17..ef2624eadd 100644 --- a/scm-webapp/src/main/java/sonia/scm/update/repository/BaseMigrationStrategy.java +++ b/scm-webapp/src/main/java/sonia/scm/update/repository/BaseMigrationStrategy.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.update.repository; import sonia.scm.SCMContextProvider; @@ -31,6 +31,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; +import java.util.function.Consumer; import java.util.stream.Stream; abstract class BaseMigrationStrategy implements MigrationStrategy.Instance { @@ -50,9 +51,9 @@ abstract class BaseMigrationStrategy implements MigrationStrategy.Instance { return contextProvider.getBaseDirectory().toPath().resolve("repositories").resolve(type); } - Stream listSourceDirectory(Path sourceDirectory) { - try { - return Files.list(sourceDirectory); + void listSourceDirectory(Path sourceDirectory, Consumer> pathConsumer) { + try (Stream paths = Files.list(sourceDirectory)) { + pathConsumer.accept(paths); } catch (IOException e) { throw new UpdateException("could not read original directory", e); } diff --git a/scm-webapp/src/main/java/sonia/scm/update/repository/CopyMigrationStrategy.java b/scm-webapp/src/main/java/sonia/scm/update/repository/CopyMigrationStrategy.java index 0032238729..9702677513 100644 --- a/scm-webapp/src/main/java/sonia/scm/update/repository/CopyMigrationStrategy.java +++ b/scm-webapp/src/main/java/sonia/scm/update/repository/CopyMigrationStrategy.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.update.repository; import org.slf4j.Logger; @@ -62,15 +62,17 @@ class CopyMigrationStrategy extends BaseMigrationStrategy { private void copyData(Path sourceDirectory, Path targetDirectory) { createDataDirectory(targetDirectory); - listSourceDirectory(sourceDirectory).forEach( + listSourceDirectory(sourceDirectory, paths -> paths.forEach( sourceFile -> { Path targetFile = targetDirectory.resolve(sourceFile.getFileName()); if (Files.isDirectory(sourceFile)) { + LOG.trace("traversing down into sub directory {}", sourceFile); copyData(sourceFile, targetFile); } else { + LOG.trace("copying file {} to {}", sourceFile, targetFile); copyFile(sourceFile, targetFile); } } - ); + )); } } diff --git a/scm-webapp/src/main/java/sonia/scm/update/repository/InlineMigrationStrategy.java b/scm-webapp/src/main/java/sonia/scm/update/repository/InlineMigrationStrategy.java index 4d8a2a987f..6c5d7afeed 100644 --- a/scm-webapp/src/main/java/sonia/scm/update/repository/InlineMigrationStrategy.java +++ b/scm-webapp/src/main/java/sonia/scm/update/repository/InlineMigrationStrategy.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.update.repository; import org.slf4j.Logger; @@ -67,20 +67,23 @@ class InlineMigrationStrategy extends BaseMigrationStrategy { private void moveData(Path sourceDirectory, Path targetDirectory, boolean deleteDirectory) { createDataDirectory(targetDirectory); - listSourceDirectory(sourceDirectory) + listSourceDirectory(sourceDirectory, paths -> paths .filter(sourceFile -> !targetDirectory.equals(sourceFile)) .forEach( sourceFile -> { Path targetFile = targetDirectory.resolve(sourceFile.getFileName()); if (Files.isDirectory(sourceFile)) { + LOG.trace("traversing down into sub directory {}", sourceFile); moveData(sourceFile, targetFile, true); } else { + LOG.trace("moving file {} to {}", sourceFile, targetFile); moveFile(sourceFile, targetFile); } } - ); + )); if (deleteDirectory) { try { + LOG.trace("deleting source directory {}", sourceDirectory); Files.delete(sourceDirectory); } catch (IOException e) { LOG.warn("could not delete source repository directory {}", sourceDirectory); diff --git a/scm-webapp/src/main/java/sonia/scm/update/repository/MoveMigrationStrategy.java b/scm-webapp/src/main/java/sonia/scm/update/repository/MoveMigrationStrategy.java index 08b859481d..c5115a0637 100644 --- a/scm-webapp/src/main/java/sonia/scm/update/repository/MoveMigrationStrategy.java +++ b/scm-webapp/src/main/java/sonia/scm/update/repository/MoveMigrationStrategy.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.update.repository; import org.slf4j.Logger; @@ -83,17 +83,20 @@ class MoveMigrationStrategy extends BaseMigrationStrategy { private void moveData(Path sourceDirectory, Path targetDirectory) { createDataDirectory(targetDirectory); - listSourceDirectory(sourceDirectory).forEach( + listSourceDirectory(sourceDirectory, paths -> paths.forEach( sourceFile -> { Path targetFile = targetDirectory.resolve(sourceFile.getFileName()); if (Files.isDirectory(sourceFile)) { + LOG.trace("traversing down into sub directory {}", sourceFile); moveData(sourceFile, targetFile); } else { + LOG.trace("moving file {} to {}", sourceFile, targetFile); moveFile(sourceFile, targetFile); } } - ); + )); try { + LOG.trace("deleting source directory {}", sourceDirectory); Files.delete(sourceDirectory); } catch (IOException e) { LOG.warn("could not delete source repository directory {}", sourceDirectory);