This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat-jakartaee-migration.git
commit d0e548a87efb456443745011dbc5194709b1fec4 Author: Mark Thomas <[email protected]> AuthorDate: Wed Nov 12 12:07:51 2025 +0000 When src == dest, only write dest if the file has changed Based on PR #78 by Semiao Marco --- CHANGES.md | 3 ++ .../org/apache/tomcat/jakartaee/Migration.java | 36 +++++++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 7e93e47..f9e8912 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,8 @@ # Tomcat Migration Tool for Jakarta EE - Changelog +## 1.0.10 +- When migrating files in place, don't replace the original file if no conversion has taken place. Based on PR[#78] by Semiao Marco. + ## 1.0.9 - Update the JaCoCo Maven plugin to 0.8.12. (dependabot/markt) - Update Commons BCEL to 6.10.0. (dependabot/markt) diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java b/src/main/java/org/apache/tomcat/jakartaee/Migration.java index 45190a5..21b6e97 100644 --- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java +++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java @@ -283,13 +283,17 @@ public class Migration { if (!inplace) { try (InputStream is = new FileInputStream(src); OutputStream os = new FileOutputStream(dest)) { - migrateStream(src.getAbsolutePath(), is, os); + converted = migrateStream(src.getAbsolutePath(), is, os); } } else { ByteArrayOutputStream buffer = new ByteArrayOutputStream((int) (src.length() * 1.05)); try (InputStream is = new FileInputStream(src)) { - migrateStream(src.getAbsolutePath(), is, buffer); + if (migrateStream(src.getAbsolutePath(), is, buffer)) { + converted = true; + } else { + return; + } } try (OutputStream os = new FileOutputStream(dest)) { @@ -299,12 +303,14 @@ public class Migration { } - private void migrateArchiveStreaming(InputStream src, OutputStream dest) throws IOException { + private boolean migrateArchiveStreaming(InputStream src, OutputStream dest) throws IOException { + boolean convertedArchive = false; try (ZipArchiveInputStream srcZipStream = new ZipArchiveInputStream(CloseShieldInputStream.wrap(src)); ZipArchiveOutputStream destZipStream = new ZipArchiveOutputStream(CloseShieldOutputStream.wrap(dest))) { ZipArchiveEntry srcZipEntry; CRC32 crc32 = new CRC32(); while ((srcZipEntry = srcZipStream.getNextEntry()) != null) { + boolean convertedStream = false; String srcName = srcZipEntry.getName(); if (isSignatureFile(srcName)) { logger.log(Level.WARNING, sm.getString("migration.skipSignatureFile", srcName)); @@ -322,7 +328,7 @@ public class Migration { String destName = profile.convert(srcName); if (srcZipEntry.getMethod() == ZipEntry.STORED) { ByteArrayOutputStream tempBuffer = new ByteArrayOutputStream((int) (srcZipEntry.getSize() * 1.05)); - migrateStream(srcName, srcZipStream, tempBuffer); + convertedStream = migrateStream(srcName, srcZipStream, tempBuffer); crc32.update(tempBuffer.toByteArray(), 0, tempBuffer.size()); MigrationZipArchiveEntry destZipEntry = new MigrationZipArchiveEntry(srcZipEntry); destZipEntry.setName(destName); @@ -336,15 +342,18 @@ public class Migration { MigrationZipArchiveEntry destZipEntry = new MigrationZipArchiveEntry(srcZipEntry); destZipEntry.setName(destName); destZipStream.putArchiveEntry(destZipEntry); - migrateStream(srcName, srcZipStream, destZipStream); + convertedStream = migrateStream(srcName, srcZipStream, destZipStream); destZipStream.closeArchiveEntry(); } + convertedArchive = convertedArchive || convertedStream; } } + return convertedArchive; } - private void migrateArchiveInMemory(InputStream src, OutputStream dest) throws IOException { + private boolean migrateArchiveInMemory(InputStream src, OutputStream dest) throws IOException { + boolean convertedArchive = false; // Read the source into memory ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(src, baos); @@ -367,7 +376,8 @@ public class Migration { MigrationZipArchiveEntry destZipEntry = new MigrationZipArchiveEntry(srcZipEntry); destZipEntry.setName(destName); destZipStream.putArchiveEntry(destZipEntry); - migrateStream(srcName, srcZipFile.getInputStream(srcZipEntry), destZipStream); + boolean convertedStream = migrateStream(srcName, srcZipFile.getInputStream(srcZipEntry), destZipStream); + convertedArchive = convertedArchive || convertedStream; destZipStream.closeArchiveEntry(); } } @@ -375,6 +385,8 @@ public class Migration { // Write the destination back to the stream ByteArrayInputStream bais = new ByteArrayInputStream(destByteChannel.array(), 0, (int) destByteChannel.size()); IOUtils.copy(bais, dest); + + return convertedArchive; } @@ -388,28 +400,30 @@ public class Migration { } - private void migrateStream(String name, InputStream src, OutputStream dest) throws IOException { + private boolean migrateStream(String name, InputStream src, OutputStream dest) throws IOException { + boolean convertedStream = false; if (isExcluded(name)) { Util.copy(src, dest); logger.log(Level.INFO, sm.getString("migration.skip", name)); } else if (isArchive(name)) { if (zipInMemory) { logger.log(Level.INFO, sm.getString("migration.archive.memory", name)); - migrateArchiveInMemory(src, dest); + convertedStream = migrateArchiveInMemory(src, dest); logger.log(Level.INFO, sm.getString("migration.archive.complete", name)); } else { logger.log(Level.INFO, sm.getString("migration.archive.stream", name)); - migrateArchiveStreaming(src, dest); + convertedStream = migrateArchiveStreaming(src, dest); logger.log(Level.INFO, sm.getString("migration.archive.complete", name)); } } else { for (Converter converter : converters) { if (converter.accepts(name)) { - converted = converted | converter.convert(name, src, dest, profile); + convertedStream = converter.convert(name, src, dest, profile); break; } } } + return convertedStream; } private boolean isArchive(String fileName) { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
