diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/VersionMetadata.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/VersionMetadata.java index 0723fda97a0..afa07cbf84b 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/VersionMetadata.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/core/VersionMetadata.java @@ -67,7 +67,14 @@ public void writeToDirectory(final Path dataDir) throws IOException { } private static File getDefaultMetadataFile(final Path dataDir) { - return dataDir.resolve(METADATA_FILENAME).toFile(); + File metaDataFile = dataDir.resolve(METADATA_FILENAME).toFile(); + + // Create the data dir here if it doesn't exist yet + if (!metaDataFile.getParentFile().exists()) { + LOG.info("Data directory {} does not exist - creating it", dataDir); + metaDataFile.getParentFile().mkdirs(); + } + return metaDataFile; } private static VersionMetadata resolveVersionMetadata(final File metadataFile) diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/VersionMetadataTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/VersionMetadataTest.java index a506be214a9..0cebcd4bda8 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/VersionMetadataTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/core/VersionMetadataTest.java @@ -49,6 +49,17 @@ void metaFileShouldContain() throws Exception { assertThat(versionMetadata.getBesuVersion()).isEqualTo("23.10.3"); } + @Test + void dataDirShouldBeCreatedIfNotPresent() throws Exception { + Files.deleteIfExists(temporaryFolder); + assertThat(Files.exists(temporaryFolder)).isFalse(); + + final VersionMetadata versionMetadata = VersionMetadata.lookUpFrom(temporaryFolder); + assertThat(versionMetadata).isNotNull(); + + assertThat(Files.exists(temporaryFolder)).isTrue(); + } + @Test void compatibilityCheckShouldThrowExceptionIfEnabled() throws Exception { // The version file says the last version to start was 23.10.3