This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-mvnd.git
commit 5b7a02f09542b5532b5b28d97c9d6bfdcaeca298 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Fri Nov 6 22:27:17 2026 +0100 Write a magic and version header in the daemon registry and read tokens fully A registry without the header, as written by an older mvnd, is reset once and the daemons register again. --- .../org/mvndaemon/mvnd/common/DaemonRegistry.java | 20 ++++++- .../mvndaemon/mvnd/common/DaemonRegistryTest.java | 63 ++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java b/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java index 0443dcdd..7be5a09e 100644 --- a/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java +++ b/common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java @@ -62,6 +62,10 @@ public class DaemonRegistry implements AutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(DaemonRegistry.class); private static final long LOCK_TIMEOUT_MS = 1000 * 20; + /** Start of a non-empty registry file: the magic number "MVND" followed by the int format version. */ + private static final int MAGIC = 0x4D564E44; + + private static final int FORMAT_VERSION = 1; private static final Map<Path, Object> locks = new ConcurrentHashMap<>(); private final Path registryFile; private final Object lck; @@ -212,7 +216,17 @@ public class DaemonRegistry implements AutoCloseable { channel.position(0); DataInputStream is = new DataInputStream(new BufferedInputStream(Channels.newInputStream(channel))); infosMap.clear(); - int nb = is.available() < 4 ? 0 : is.readInt(); + int nb = 0; + if (is.available() > 0) { + // a file without the header, as written by an older mvnd, is reset like a corrupt one + int magic = is.readInt(); + int version = is.readInt(); + if (magic != MAGIC || version != FORMAT_VERSION) { + throw new IOException( + String.format("Unsupported registry header 0x%08X, version %d", magic, version)); + } + nb = is.readInt(); + } for (int i = 0; i < nb; i++) { String daemonId = is.readUTF(); String javaHome = is.readUTF(); @@ -221,7 +235,7 @@ public class DaemonRegistry implements AutoCloseable { String address = is.readUTF(); byte[] token = new byte[DaemonInfo.TOKEN_SIZE]; - is.read(token); + is.readFully(token); String locale = is.readUTF(); List<String> opts = new ArrayList<>(); @@ -254,6 +268,8 @@ public class DaemonRegistry implements AutoCloseable { channel.truncate(0); DataOutputStream os = new DataOutputStream(new BufferedOutputStream(Channels.newOutputStream(channel))); + os.writeInt(MAGIC); + os.writeInt(FORMAT_VERSION); os.writeInt(infosMap.size()); for (DaemonInfo di : infosMap.values()) { String id = di.getId(); diff --git a/common/src/test/java/org/mvndaemon/mvnd/common/DaemonRegistryTest.java b/common/src/test/java/org/mvndaemon/mvnd/common/DaemonRegistryTest.java index cb0d9ad3..74a20555 100644 --- a/common/src/test/java/org/mvndaemon/mvnd/common/DaemonRegistryTest.java +++ b/common/src/test/java/org/mvndaemon/mvnd/common/DaemonRegistryTest.java @@ -19,6 +19,7 @@ package org.mvndaemon.mvnd.common; import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; import java.io.File; import java.io.IOException; import java.io.PrintStream; @@ -347,4 +348,66 @@ public class DaemonRegistryTest { System.currentTimeMillis(), System.currentTimeMillis()); } + + @Test + public void testRegistryStartsWithHeader() throws IOException { + Path file = Files.createTempDirectory("reg").resolve("registry.bin"); + try (DaemonRegistry reg = new DaemonRegistry(file)) { + reg.store(newDaemonInfo("12345678")); + } + ByteArrayOutputStream expected = new ByteArrayOutputStream(); + try (DataOutputStream os = new DataOutputStream(expected)) { + os.writeInt(0x4D564E44); + os.writeInt(1); + os.writeInt(1); + } + byte[] actual = Files.readAllBytes(file); + assertArrayEquals(expected.toByteArray(), Arrays.copyOf(actual, 12)); + } + + @Test + public void testRegistryWithoutOrWithOtherHeaderIsReset() throws IOException { + Path dir = Files.createTempDirectory("reg"); + Path file = dir.resolve("registry.bin"); + + // the format written before the header was added: the daemon count comes first + ByteArrayOutputStream old = new ByteArrayOutputStream(); + try (DataOutputStream os = new DataOutputStream(old)) { + os.writeInt(0); + os.writeInt(0); + } + Files.write(file, old.toByteArray()); + try (DaemonRegistry reg = new DaemonRegistry(file)) { + assertEquals(0, reg.getAll().size()); + assertArrayEquals(old.toByteArray(), Files.readAllBytes(dir.resolve("registry.bin.corrupt"))); + assertEquals(0, Files.size(file)); + reg.store(newDaemonInfo("12345678")); + assertEquals(1, reg.getAll().size()); + } + + // a file written by another format version + ByteArrayOutputStream other = new ByteArrayOutputStream(); + try (DataOutputStream os = new DataOutputStream(other)) { + os.writeInt(0x4D564E44); + os.writeInt(2); + os.writeInt(0); + os.writeInt(0); + } + Files.write(file, other.toByteArray()); + try (DaemonRegistry reg = new DaemonRegistry(file)) { + assertEquals(0, reg.getAll().size()); + assertArrayEquals(other.toByteArray(), Files.readAllBytes(dir.resolve("registry.bin.corrupt"))); + } + } + + @Test + public void testEmptyFileIsValidRegistry() throws IOException { + Path dir = Files.createTempDirectory("reg"); + Path file = dir.resolve("registry.bin"); + try (DaemonRegistry reg = new DaemonRegistry(file)) { + assertEquals(0, reg.getAll().size()); + } + assertEquals(0, Files.size(file)); + assertTrue(Files.notExists(dir.resolve("registry.bin.corrupt"))); + } }
