Skip to content

Commit c1fffc6

Browse files
committed
Added support for java7 setAttribtues
1 parent ffbe4ff commit c1fffc6

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/main/java/org/codehaus/plexus/archiver/util/ArchiveEntryUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package org.codehaus.plexus.archiver.util;
22

33
import org.codehaus.plexus.archiver.ArchiverException;
4+
import org.codehaus.plexus.components.io.attributes.Java7AttributeUtils;
5+
import org.codehaus.plexus.components.io.attributes.Java7Reflector;
46
import org.codehaus.plexus.logging.Logger;
57
import org.codehaus.plexus.util.Os;
68
import org.codehaus.plexus.util.cli.CommandLineException;
79
import org.codehaus.plexus.util.cli.CommandLineUtils;
810
import org.codehaus.plexus.util.cli.Commandline;
911

1012
import java.io.File;
13+
import java.io.IOException;
1114
import java.lang.reflect.Method;
1215

1316
@SuppressWarnings("JavaDoc")
@@ -50,6 +53,18 @@ public static void chmod( final File file, final int mode, final Logger logger,
5053
return;
5154
}
5255

56+
if (Java7Reflector.isJava7())
57+
{
58+
try
59+
{
60+
Java7AttributeUtils.chmod(file, mode);
61+
return;
62+
} catch (IOException e)
63+
{
64+
throw new ArchiverException("Failed setting file attributes with java7+", e);
65+
}
66+
}
67+
5368
final String m = Integer.toOctalString( mode & 0xfff );
5469

5570
if ( useJvmChmod && !jvmFilePermAvailable )

src/test/java/org/codehaus/plexus/archiver/tar/TarFileAttributesTest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.codehaus.plexus.archiver.Archiver;
1111
import org.codehaus.plexus.archiver.UnArchiver;
1212
import org.codehaus.plexus.archiver.util.DefaultArchivedFileSet;
13+
import org.codehaus.plexus.components.io.attributes.Java7Reflector;
1314
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributeUtils;
1415
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
1516
import org.codehaus.plexus.util.FileUtils;
@@ -138,8 +139,10 @@ public void testUseAttributesFromTarArchiveInputInTarArchiverOutput()
138139
PlexusIoResourceAttributes fileAttributes =
139140
PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) );
140141

141-
assertEquals( "This test will fail if your umask is not X2X (or more)",
142-
"644", fileAttributes.getOctalModeString() );
142+
final String expected = Java7Reflector.isJava7() ? "660" : "644";
143+
144+
assertEquals( "This test will fail if your umask is not X2X (or more)",
145+
expected, fileAttributes.getOctalModeString() );
143146
}
144147

145148
public void testUseDetectedFileAttributes()
@@ -199,8 +202,10 @@ public void testUseDetectedFileAttributes()
199202

200203
fileAttributes = PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) );
201204

202-
assertEquals( "This test will fail if your umask is not X2X (or more)",
203-
"444", fileAttributes.getOctalModeString() );
205+
final String expected = Java7Reflector.isJava7() ? "440" : "444";
206+
207+
assertEquals( "This test will fail if your umask is not X2X (or more)",
208+
expected, fileAttributes.getOctalModeString() );
204209
}
205210

206211
private boolean checkForWindows()
@@ -262,8 +267,10 @@ public void testOverrideDetectedFileAttributes()
262267
PlexusIoResourceAttributes fileAttributes =
263268
PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) );
264269

265-
assertEquals( "This test will fail if your umask is not X2X (or more)",
266-
"644", fileAttributes.getOctalModeString() );
270+
final String expected = Java7Reflector.isJava7() ? "660" : "644";
271+
272+
assertEquals( "This test will fail if your umask is not X2X (or more)",
273+
expected, fileAttributes.getOctalModeString() );
267274
}
268275

269276
public void testOverrideDetectedFileAttributesUsingFileMode()
@@ -320,8 +327,9 @@ public void testOverrideDetectedFileAttributesUsingFileMode()
320327
PlexusIoResourceAttributes fileAttributes =
321328
PlexusIoResourceAttributeUtils.getFileAttributes( new File( tempTarDir, tempFile.getName() ) );
322329

323-
assertEquals( "This test will fail if your umask is not X2X (or more)",
324-
"644", fileAttributes.getOctalModeString() );
330+
final String expected = Java7Reflector.isJava7() ? "660" : "644";
331+
assertEquals( "This test will fail if your umask is not X2X (or more)",
332+
expected, fileAttributes.getOctalModeString() );
325333
}
326334

327335
}
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.codehaus.plexus.archiver.util;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.util.HashMap;
56

67
import org.codehaus.plexus.components.io.attributes.Java7FileAttributes;
8+
import org.codehaus.plexus.components.io.attributes.Java7Reflector;
79
import org.codehaus.plexus.logging.console.ConsoleLogger;
810

911
import junit.framework.TestCase;
@@ -14,18 +16,28 @@ public void testChmodForFileWithDollarPLXCOMP164() throws Exception
1416
{
1517

1618
File temp = File.createTempFile("A$A", "BB$");
17-
1819
ArchiveEntryUtils.chmod( temp, 0770, new ConsoleLogger( org.codehaus.plexus.logging.Logger.LEVEL_DEBUG, "foo" ), false);
20+
assert0770(temp);
21+
}
1922

20-
Java7FileAttributes j7 = new Java7FileAttributes(temp, new HashMap<Integer, String>(), new HashMap<Integer, String>());
23+
public void testChmodWithJava7() throws Exception
24+
{
25+
if (!Java7Reflector.isJava7()) return; // Require at least java7
2126

27+
File temp = File.createTempFile("D$D", "BB$");
28+
ArchiveEntryUtils.chmod( temp, 0770, new ConsoleLogger( org.codehaus.plexus.logging.Logger.LEVEL_DEBUG, "foo" ));
29+
assert0770(temp);
30+
}
31+
32+
private void assert0770(File temp) throws IOException {
33+
Java7FileAttributes j7 = new Java7FileAttributes(temp, new HashMap<Integer, String>(), new HashMap<Integer, String>());
2234
assertTrue(j7.isGroupExecutable());
2335
assertTrue(j7.isGroupReadable());
2436
assertTrue(j7.isGroupWritable());
2537

2638
assertFalse(j7.isWorldExecutable());
2739
assertFalse(j7.isWorldReadable());
2840
assertFalse(j7.isWorldWritable());
29-
3041
}
31-
}
42+
43+
}

0 commit comments

Comments
 (0)