Skip to content

Commit

Permalink
Handle intermediate directories of collect entries in MissingDirector…
Browse files Browse the repository at this point in the history
…yTracker
  • Loading branch information
moedan committed Feb 28, 2024
1 parent 2e22458 commit ed46fc4
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 10 deletions.
136 changes: 136 additions & 0 deletions src/it/test19-intermediate-directories-collect/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.dentrassi.maven.rpm.test</groupId>
<artifactId>test19</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Test Package #19</name>
<description>
Test explicitly adding and applying rules for implicitly created intermediate directories with collect entries.
</description>

<url>http://dentrassi.de</url>

<organization>
<name>Jens Reimann</name>
<url>http://dentrassi.de</url>
</organization>

<licenses>
<license>
<name>Eclipse Public License - v 1.0</name>
<distribution>repo</distribution>
<url>https://www.eclipse.org/legal/epl-v10.html</url>
</license>
</licenses>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<skipSigning>true</skipSigning>
<!-- use a predictable timestamp -->
<project.build.outputTimestamp>2009-01-01T12:00:00+01:00</project.build.outputTimestamp>
</properties>

<build>

<plugins>
<plugin>
<groupId>de.dentrassi.maven</groupId>
<artifactId>rpm</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>rpm</goal>
</goals>
<configuration>
<attach>false</attach>
<group>Application/Misc</group>

<signature>
<keyId>${keyId}</keyId>
<keyringFile>${user.home}/.gnupg/secring.gpg</keyringFile>
<passphrase>${passphrase}</passphrase>
<hashAlgorithm>SHA1</hashAlgorithm>
<skip>${skipSigning}</skip>
</signature>

<generateIntermediateDirectories>
<baseDirecotry>/opt/mycompany/myapp</baseDirecotry>
</generateIntermediateDirectories>

<rulesets>
<ruleset>
<id>my-default</id>
<rules>
<rule>
<when>
<prefix>/opt/mycompany</prefix>
</when>
<user>mygeneraluser</user>
<group>mygeneralgroup</group>
<mode>0111</mode>
</rule>
<rule>
<when>
<prefix>/opt/mycompany/myapp</prefix>
</when>
<user>myuser</user>
<group>mygroup</group>
<mode>0555</mode>
</rule>
<rule>
<when>
<prefix>/opt/mycompany/myapp</prefix>
<type>directory</type>
</when>
<mode>0777</mode>
</rule>
</rules>
</ruleset>
</rulesets>

<entries>
<entry>
<name>/opt/mycompany/myapp/a/b</name>
<collect>
<from>${project.basedir}/src/main/data</from>
<directories>false</directories> <!-- make explicit directories -->
</collect>
<ruleset>my-default</ruleset>
</entry>
<entry>
<name>/opt/mycompany/myapp/c/d</name>
<collect>
<from>${project.basedir}/src/main/data</from>
<directories>true</directories> <!-- make explicit directories -->
</collect>
<ruleset>my-default</ruleset>
</entry>
</entries>

</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>sign</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<skipSigning>false</skipSigning>
</properties>
</profile>
</profiles>

</project>
Empty file.
25 changes: 25 additions & 0 deletions src/it/test19-intermediate-directories-collect/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def dump() {
Process proc = ('rpm -q --dump -p ' + basedir + "/target/*.rpm").execute()
return proc.in.getText().trim()
}

def actual = dump()
println "Dump:\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()

if (actual != expected) {
System.out.format("RPM dump doesn't match - actual:%n%s%nexpected:%n%s%n", actual, expected);
return false;
}

return true;
46 changes: 36 additions & 10 deletions src/main/java/de/dentrassi/rpm/builder/MissingDirectoryTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,39 @@ private void addMissingDirectoriesFromPath(String targetName, FileInformationPro
if (provider instanceof MojoFileInformationProvider) {
MojoFileInformationProvider mojoProvider = (MojoFileInformationProvider) provider;

for (String intermediateDirectory : getIntermediateDirectories(targetName)) {
if (startsPathWithPrefix(intermediateDirectory) && !explicitAddedDirectories.contains(intermediateDirectory)) {
missingDirectories.computeIfAbsent(intermediateDirectory,
(String directory) -> new MojoFileInformationProvider(
mojoProvider.getRulesetEval(),
mojoProvider.getRuleId(),
null,
mojoProvider.getLogger(),
mojoProvider.getTimestamp()));
}
List<String> intermediateDirectories;
if (containsCollectEntry(mojoProvider)) {
// collect handles intermediate directories by themselves via flag 'directories'
// -> only use base directory (entry name of collect) as target path to calculate
// intermediate directories
targetName = mojoProvider.getEntry().getName();
intermediateDirectories = getIntermediateDirectoriesIncludingTarget(targetName);
} else {
intermediateDirectories = getIntermediateDirectories(targetName);
}

for (String intermediateDirectory : intermediateDirectories) {
addIfIsMissingDirectory(intermediateDirectory, mojoProvider);
}
}
}

private void addIfIsMissingDirectory(String intermediateDirectory, MojoFileInformationProvider mojoProvider) {
if (startsPathWithPrefix(intermediateDirectory) && !explicitAddedDirectories.contains(intermediateDirectory)) {
missingDirectories.computeIfAbsent(intermediateDirectory,
(String directory) -> new MojoFileInformationProvider(
mojoProvider.getRulesetEval(),
mojoProvider.getRuleId(),
null,
mojoProvider.getLogger(),
mojoProvider.getTimestamp()));
}
}

private boolean containsCollectEntry(MojoFileInformationProvider mojoProvider) {
return mojoProvider.getEntry().getCollect() != null;
}

private boolean startsPathWithPrefix(String directory) {
return baseDirectories.stream().anyMatch(directory::startsWith);
}
Expand All @@ -95,4 +114,11 @@ private List<String> getIntermediateDirectories(String targetName) {
return intermediateDirectories;
}

private List<String> getIntermediateDirectoriesIncludingTarget(String targetName) {
List<String> intermediateDirectories = getIntermediateDirectories(targetName);
intermediateDirectories.add(targetName);

return intermediateDirectories;
}

}

0 comments on commit ed46fc4

Please sign in to comment.