diff --git a/src/it/test19-intermediate-directories-collect/verify.groovy b/src/it/test19-intermediate-directories-collect/verify.groovy index 7fd6bb6..9477ac4 100644 --- a/src/it/test19-intermediate-directories-collect/verify.groovy +++ b/src/it/test19-intermediate-directories-collect/verify.groovy @@ -1,24 +1,25 @@ -def dump() { - Process proc = ('rpm -q --dump -p ' + basedir + "/target/*.rpm").execute() +def verifyFileInodes() { + Process proc = ['rpm', '-q', '--queryformat', '[%{FILEINODES} %{FILEMODES:perms} %{FILEUSERNAME} %{FILEGROUPNAME} %{FILENAMES}\n]', basedir.path + '/target/*.rpm'].execute() | 'sort -n'.execute() + proc.waitFor() return proc.in.getText().trim() } -def actual = dump() -println "Dump:\n" + actual +def actual = verifyFileInodes() +println "Verify file inodes:\n" + actual def expected = """\ - /opt/mycompany/myapp 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/a 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/a/b 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/a/b/x/y/foobar 0 1230807600 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0100555 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/c 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/c/d 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/c/d/x 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/c/d/x/y 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/c/d/x/y/foobar 0 1230807600 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0100555 myuser mygroup 0 0 0 X""".stripIndent() + 1 drwxrwxrwx myuser mygroup /opt/mycompany/myapp + 2 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/a + 3 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/a/b + 4 -r-xr-xr-x myuser mygroup /opt/mycompany/myapp/a/b/x/y/foobar + 5 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/c + 6 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/c/d + 7 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/c/d/x + 8 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/c/d/x/y + 9 -r-xr-xr-x myuser mygroup /opt/mycompany/myapp/c/d/x/y/foobar""".stripIndent() if (actual != expected) { - System.out.format("RPM dump doesn't match - actual:%n%s%nexpected:%n%s%n", actual, expected); + System.out.format("RPM file inodes doesn't match - actual:%n%s%nexpected:%n%s%n", actual, expected); return false; } diff --git a/src/it/test19-intermediate-directories/pom.xml b/src/it/test19-intermediate-directories/pom.xml index b2fcb50..f2edaf6 100644 --- a/src/it/test19-intermediate-directories/pom.xml +++ b/src/it/test19-intermediate-directories/pom.xml @@ -98,13 +98,13 @@ - /opt/mycompany/myapp/a/b/c/foobar - ${project.basedir}/src/main/data/foobar + /opt/mycompany/myapp + true my-default - /opt/mycompany/myapp - true + /opt/mycompany/myapp/a/b/c/foobar + ${project.basedir}/src/main/data/foobar my-default diff --git a/src/it/test19-intermediate-directories/verify.groovy b/src/it/test19-intermediate-directories/verify.groovy index 37f0515..ec14324 100644 --- a/src/it/test19-intermediate-directories/verify.groovy +++ b/src/it/test19-intermediate-directories/verify.groovy @@ -1,23 +1,24 @@ -def dump() { - Process proc = ('rpm -q --dump -p ' + basedir + "/target/*.rpm").execute() +def verifyFileInodes() { + Process proc = ['rpm', '-q', '--queryformat', '[%{FILEINODES} %{FILEMODES:perms} %-13{FILEUSERNAME} %-14{FILEGROUPNAME} %{FILENAMES}\n]', basedir.path + '/target/*.rpm'].execute() | 'sort -n'.execute() + proc.waitFor() return proc.in.getText().trim() } -def actual = dump() -println "Dump:\n" + actual +def actual = verifyFileInodes() +println "Verify file inodes:\n" + actual def expected = """\ - /etc/mycompany/myapp 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040755 root root 0 0 0 X - /etc/mycompany/myapp/defaults 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040755 root root 0 0 0 X - /opt/mycompany/myapp 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/a 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/a/b 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/a/b/c 0 1230807600 0000000000000000000000000000000000000000000000000000000000000000 040777 myuser mygroup 0 0 0 X - /opt/mycompany/myapp/a/b/c/foobar 0 1230807600 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0100555 myuser mygroup 0 0 0 X - /opt/mycompany/otherapp/a/b/c/foobar 0 1230807600 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 0100111 mygeneraluser mygeneralgroup 0 0 0 X""".stripIndent() + 1 drwxrwxrwx myuser mygroup /opt/mycompany/myapp + 2 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/a + 3 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/a/b + 4 drwxrwxrwx myuser mygroup /opt/mycompany/myapp/a/b/c + 5 -r-xr-xr-x myuser mygroup /opt/mycompany/myapp/a/b/c/foobar + 6 drwxr-xr-x root root /etc/mycompany/myapp + 7 drwxr-xr-x root root /etc/mycompany/myapp/defaults + 8 ---x--x--x mygeneraluser mygeneralgroup /opt/mycompany/otherapp/a/b/c/foobar""".stripIndent() if (actual != expected) { - System.out.format("RPM dump doesn't match - actual:%n%s%nexpected:%n%s%n", actual, expected); + System.out.format("RPM file inodes doesn't match - actual:%n%s%nexpected:%n%s%n", actual, expected); return false; } diff --git a/src/main/java/de/dentrassi/rpm/builder/ListenableBuilderContext.java b/src/main/java/de/dentrassi/rpm/builder/ListenableBuilderContext.java deleted file mode 100644 index 47aeb77..0000000 --- a/src/main/java/de/dentrassi/rpm/builder/ListenableBuilderContext.java +++ /dev/null @@ -1,83 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2023 Fraunhofer-Institut fuer Optronik, Systemtechnik und Bildauswertung IOSB and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-2.0 - * - * Contributors: - * Fraunhofer-Institut fuer Optronik, Systemtechnik und Bildauswertung IOSB - initial API and implementation - *******************************************************************************/ -package de.dentrassi.rpm.builder; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; -import java.nio.file.Path; -import java.util.HashSet; -import java.util.Set; - -import org.eclipse.packager.rpm.build.BuilderContext; -import org.eclipse.packager.rpm.build.FileInformationProvider; - -public class ListenableBuilderContext implements BuilderContext { - - private final BuilderContext builderContext; - private final Set listeners; - - public ListenableBuilderContext(BuilderContext builderContext) { - this.builderContext = builderContext; - this.listeners = new HashSet<>(); - } - - @Override - public void setDefaultInformationProvider(FileInformationProvider provider) { - builderContext.setDefaultInformationProvider(provider); - } - - @Override - public FileInformationProvider getDefaultInformationProvider() { - return builderContext.getDefaultInformationProvider(); - } - - @Override - public void addFile(String targetName, Path source, FileInformationProvider provider) throws IOException { - builderContext.addFile(targetName, source, provider); - listeners.forEach(listener -> listener.notifyFileAdded(targetName, (MojoFileInformationProvider) provider)); - } - - @Override - public void addFile(String targetName, InputStream source, FileInformationProvider provider) throws IOException { - builderContext.addFile(targetName, source, provider); - listeners.forEach(listener -> listener.notifyFileAdded(targetName, provider)); - } - - @Override - public void addFile(String targetName, ByteBuffer source, FileInformationProvider provider) throws IOException { - builderContext.addFile(targetName, source, provider); - listeners.forEach(listener -> listener.notifyFileAdded(targetName, provider)); - } - - @Override - public void addDirectory(String targetName, FileInformationProvider provider) throws IOException { - builderContext.addDirectory(targetName, provider); - listeners.forEach(listener -> listener.notifyDirectoryAdded(targetName, (MojoFileInformationProvider) provider)); - } - - @Override - public void addSymbolicLink(String targetName, String linkTo, FileInformationProvider provider) throws IOException { - builderContext.addSymbolicLink(targetName, linkTo, provider); - listeners.forEach(listener -> listener.notifySymbolicLinkAdded(targetName, (MojoFileInformationProvider) provider)); - } - - public void registerListener(BuilderContextListener listener) { - if (listener != null) { - listeners.add(listener); - } - } - - public void removeListener(BuilderContextListener listener) { - listeners.remove(listener); - } - -} diff --git a/src/main/java/de/dentrassi/rpm/builder/MissingDirectoryTracker.java b/src/main/java/de/dentrassi/rpm/builder/MissingDirectoryGeneratorInterceptor.java similarity index 61% rename from src/main/java/de/dentrassi/rpm/builder/MissingDirectoryTracker.java rename to src/main/java/de/dentrassi/rpm/builder/MissingDirectoryGeneratorInterceptor.java index c3d5789..867d7bc 100644 --- a/src/main/java/de/dentrassi/rpm/builder/MissingDirectoryTracker.java +++ b/src/main/java/de/dentrassi/rpm/builder/MissingDirectoryGeneratorInterceptor.java @@ -11,51 +11,78 @@ package de.dentrassi.rpm.builder; import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import org.eclipse.packager.rpm.build.BuilderContext; import org.eclipse.packager.rpm.build.FileInformationProvider; -public class MissingDirectoryTracker implements BuilderContextListener { +public class MissingDirectoryGeneratorInterceptor implements BuilderContext { + + private final BuilderContext builderContext; - // keep track of explicit added directories - private final Set explicitAddedDirectories; - // keep track of all missing directories which should be added after processing (without explicit added directories) - private final Map> missingDirectories; // base directories (prefixes) for which the intermediate directories should be generated private final List baseDirectories; + // keep track of explicit added directories + private final Set explicitAddedDirectories; + // keep track of all additionally generated and added directories + private final Set generatedDirectories; - public MissingDirectoryTracker(List baseDirectories) { - this.explicitAddedDirectories = new HashSet<>(); - this.missingDirectories = new HashMap<>(); + public MissingDirectoryGeneratorInterceptor(BuilderContext builderContext, List baseDirectories) { + this.builderContext = builderContext; this.baseDirectories = baseDirectories; + explicitAddedDirectories = new HashSet<>(); + generatedDirectories = new HashSet<>(); + } + + @Override + public void setDefaultInformationProvider(FileInformationProvider provider) { + builderContext.setDefaultInformationProvider(provider); + } + + @Override + public FileInformationProvider getDefaultInformationProvider() { + return builderContext.getDefaultInformationProvider(); } @Override - public void notifyFileAdded(String targetName, FileInformationProvider provider) { + public void addFile(String targetName, Path source, FileInformationProvider provider) throws IOException { addMissingDirectoriesFromPath(targetName, provider); + builderContext.addFile(targetName, source, provider); } @Override - public void notifyDirectoryAdded(String targetName, FileInformationProvider provider) { - explicitAddedDirectories.add(targetName); - missingDirectories.remove(targetName); + public void addFile(String targetName, InputStream source, FileInformationProvider provider) throws IOException { addMissingDirectoriesFromPath(targetName, provider); + builderContext.addFile(targetName, source, provider); } @Override - public void notifySymbolicLinkAdded(String targetName, FileInformationProvider provider) { + public void addFile(String targetName, ByteBuffer source, FileInformationProvider provider) throws IOException { addMissingDirectoriesFromPath(targetName, provider); + builderContext.addFile(targetName, source, provider); } - private void addMissingDirectoriesFromPath(String targetName, FileInformationProvider provider) { + @Override + public void addDirectory(String targetName, FileInformationProvider provider) throws IOException { + addMissingDirectoriesFromPath(targetName, provider); + builderContext.addDirectory(targetName, provider); + explicitAddedDirectories.add(targetName); + } + + @Override + public void addSymbolicLink(String targetName, String linkTo, FileInformationProvider provider) throws IOException { + addMissingDirectoriesFromPath(targetName, provider); + builderContext.addSymbolicLink(targetName, linkTo, provider); + } + + private void addMissingDirectoriesFromPath(String targetName, FileInformationProvider provider) throws IOException { if (provider instanceof MojoFileInformationProvider) { MojoFileInformationProvider mojoProvider = (MojoFileInformationProvider) provider; @@ -76,15 +103,17 @@ private void addMissingDirectoriesFromPath(String targetName, FileInformationPro } } - private void addIfIsMissingDirectory(String intermediateDirectory, MojoFileInformationProvider mojoProvider) { - if (startsPathWithPrefix(intermediateDirectory) && !explicitAddedDirectories.contains(intermediateDirectory)) { - missingDirectories.computeIfAbsent(intermediateDirectory, - (String directory) -> new MojoFileInformationProvider( + private void addIfIsMissingDirectory(String intermediateDirectory, MojoFileInformationProvider mojoProvider) throws IOException { + if (startsPathWithPrefix(intermediateDirectory) + && !explicitAddedDirectories.contains(intermediateDirectory) + && !generatedDirectories.contains(intermediateDirectory)) { + builderContext.addDirectory(intermediateDirectory, new MojoFileInformationProvider( mojoProvider.getRulesetEval(), mojoProvider.getRuleId(), null, mojoProvider.getLogger(), mojoProvider.getTimestamp())); + generatedDirectories.add(intermediateDirectory); } } @@ -96,12 +125,6 @@ private boolean startsPathWithPrefix(String directory) { return baseDirectories.stream().anyMatch(directory::startsWith); } - public void addMissingIntermediateDirectoriesToContext(BuilderContext ctx) throws IOException { - for (Map.Entry> missingEntry : missingDirectories.entrySet()) { - ctx.addDirectory(missingEntry.getKey(), missingEntry.getValue()); - } - } - private List getIntermediateDirectories(String targetName) { ArrayList intermediateDirectories = new ArrayList<>(); Path path = Paths.get(targetName); diff --git a/src/main/java/de/dentrassi/rpm/builder/RpmMojo.java b/src/main/java/de/dentrassi/rpm/builder/RpmMojo.java index 8415818..2b87c5b 100644 --- a/src/main/java/de/dentrassi/rpm/builder/RpmMojo.java +++ b/src/main/java/de/dentrassi/rpm/builder/RpmMojo.java @@ -1104,9 +1104,12 @@ protected void fillPayload(final RpmBuilder builder) throws MojoFailureException return; } - final ListenableBuilderContext ctx = new ListenableBuilderContext(builder.newContext()); - final MissingDirectoryTracker missingDirectoryTracker = new MissingDirectoryTracker(this.generateIntermediateDirectories); - ctx.registerListener(missingDirectoryTracker); + final BuilderContext ctx; + if (this.generateIntermediateDirectories.isEmpty()) { + ctx = builder.newContext(); + } else { + ctx = new MissingDirectoryGeneratorInterceptor(builder.newContext(), this.generateIntermediateDirectories);; + } this.logger.debug("Building payload:"); @@ -1121,11 +1124,6 @@ protected void fillPayload(final RpmBuilder builder) throws MojoFailureException fillFromEntry(ctx, entry); } } - - ctx.removeListener(missingDirectoryTracker); - if (!generateIntermediateDirectories.isEmpty()) { - missingDirectoryTracker.addMissingIntermediateDirectoriesToContext(ctx); - } } private void customizeHeader(final RpmBuilder builder) {