From 852cc145b7311c7b03e90ed06269dba214a4058a Mon Sep 17 00:00:00 2001 From: Alex Kuzmin Date: Tue, 10 Dec 2024 11:12:02 -0800 Subject: [PATCH] fix: fixed file leak in listing directory ServicesMain (cherry-pick) (#16825) (#16997) Signed-off-by: Alex Kuzmin --- .../state/snapshot/SignedStateFilePath.java | 68 ++++++++++--------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/snapshot/SignedStateFilePath.java b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/snapshot/SignedStateFilePath.java index dcb6b40ad134..a151805958bd 100644 --- a/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/snapshot/SignedStateFilePath.java +++ b/platform-sdk/swirlds-platform-core/src/main/java/com/swirlds/platform/state/snapshot/SignedStateFilePath.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.List; import java.util.TreeMap; +import java.util.stream.Stream; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -194,43 +195,48 @@ public List getSavedStateFiles( return List.of(); } - final List dirs = Files.list(dir).filter(Files::isDirectory).toList(); + try (final Stream list = Files.list(dir)) { - final TreeMap savedStates = new TreeMap<>(); - for (final Path subDir : dirs) { - try { - final long round = Long.parseLong(subDir.getFileName().toString()); - final Path stateFile = subDir.resolve(SIGNED_STATE_FILE_NAME); - if (!exists(stateFile)) { - logger.warn( - EXCEPTION.getMarker(), - "Saved state file ({}) not found, but directory exists '{}'", - stateFile.getFileName(), - subDir.toAbsolutePath()); - continue; - } + final List dirs = list.filter(Files::isDirectory).toList(); - final Path metdataPath = subDir.resolve(SavedStateMetadata.FILE_NAME); - final SavedStateMetadata metadata; + final TreeMap savedStates = new TreeMap<>(); + for (final Path subDir : dirs) { try { - metadata = SavedStateMetadata.parse(metdataPath); - } catch (final IOException e) { - logger.error( - EXCEPTION.getMarker(), "Unable to read saved state metadata file '{}'", metdataPath); - continue; + final long round = Long.parseLong(subDir.getFileName().toString()); + final Path stateFile = subDir.resolve(SIGNED_STATE_FILE_NAME); + if (!exists(stateFile)) { + logger.warn( + EXCEPTION.getMarker(), + "Saved state file ({}) not found, but directory exists '{}'", + stateFile.getFileName(), + subDir.toAbsolutePath()); + continue; + } + + final Path metdataPath = subDir.resolve(SavedStateMetadata.FILE_NAME); + final SavedStateMetadata metadata; + try { + metadata = SavedStateMetadata.parse(metdataPath); + } catch (final IOException e) { + logger.error( + EXCEPTION.getMarker(), + "Unable to read saved state metadata file '{}'", + metdataPath); + continue; + } + + savedStates.put(round, new SavedStateInfo(stateFile, metadata)); + + } catch (final NumberFormatException e) { + logger.warn( + EXCEPTION.getMarker(), + "Unexpected directory '{}' in '{}'", + subDir.getFileName(), + dir.toAbsolutePath()); } - - savedStates.put(round, new SavedStateInfo(stateFile, metadata)); - - } catch (final NumberFormatException e) { - logger.warn( - EXCEPTION.getMarker(), - "Unexpected directory '{}' in '{}'", - subDir.getFileName(), - dir.toAbsolutePath()); } + return new ArrayList<>(savedStates.descendingMap().values()); } - return new ArrayList<>(savedStates.descendingMap().values()); } catch (final IOException e) { throw new UncheckedIOException(e); }