Skip to content

Commit

Permalink
Close file lists
Browse files Browse the repository at this point in the history
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-)
  • Loading branch information
pfeuffer committed Jun 16, 2020
1 parent b304067 commit c441028
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
Expand Up @@ -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;
Expand All @@ -31,6 +31,7 @@
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 {
Expand All @@ -50,9 +51,9 @@ Path getTypeDependentPath(String type) {
return contextProvider.getBaseDirectory().toPath().resolve("repositories").resolve(type);
}

Stream<Path> listSourceDirectory(Path sourceDirectory) {
try {
return Files.list(sourceDirectory);
void listSourceDirectory(Path sourceDirectory, Consumer<Stream<Path>> pathConsumer) {
try (Stream<Path> paths = Files.list(sourceDirectory)) {
pathConsumer.accept(paths);
} catch (IOException e) {
throw new UpdateException("could not read original directory", e);
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -62,15 +62,17 @@ public Optional<Path> migrate(String id, String name, String type) {

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);
}
}
);
));
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -67,20 +67,23 @@ private void moveData(Path sourceDirectory, Path targetDirectory) {

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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -83,17 +83,20 @@ private void delete(Path rootPath, List<String> directories) {

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);
Expand Down

0 comments on commit c441028

Please sign in to comment.