Skip to content

Instantly share code, notes, and snippets.

@rewbs
Created January 23, 2011 12:47
Show Gist options
  • Save rewbs/792062 to your computer and use it in GitHub Desktop.
Save rewbs/792062 to your computer and use it in GitHub Desktop.
### Eclipse Workspace Patch 1.0
#P antTrunk
Index: src/main/org/apache/tools/ant/util/PermissionsUtils.java
===================================================================
--- src/main/org/apache/tools/ant/util/PermissionsUtils.java (revision 0)
+++ src/main/org/apache/tools/ant/util/PermissionsUtils.java (revision 0)
@@ -0,0 +1,89 @@
+package org.apache.tools.ant.util;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.PrintStream;
+
+import org.jruby.ext.posix.POSIX;
+import org.jruby.ext.posix.POSIX.ERRORS;
+import org.jruby.ext.posix.POSIXFactory;
+import org.jruby.ext.posix.POSIXHandler;
+
+/**
+ * Provides convenience methods for manipulating file permissions.
+ */
+public class PermissionsUtils
+{
+ private static POSIX posix = POSIXFactory.getPOSIX(new AntPOSIXHandler(), true);
+
+ public static int getPermissions(File file)
+ {
+ return posix.stat(file.getAbsolutePath()).mode() & 0777;
+ }
+
+ public static void setPermissions(File file, int perms)
+ {
+ posix.chmod(file.getAbsolutePath(), perms);
+ }
+
+ /**
+ * Minimal POSIX handler for Ant tasks. There's scope for improvement here, like redirecting warnings through the Ant
+ * log, failing on errors, etc...
+ */
+ public static class AntPOSIXHandler implements POSIXHandler
+ {
+
+ public File getCurrentWorkingDirectory()
+ {
+ return new File(".");
+ }
+
+ public String[] getEnv()
+ {
+ return new String[]{};
+ }
+
+ public PrintStream getErrorStream()
+ {
+ return System.err;
+ }
+
+ public InputStream getInputStream()
+ {
+ return System.in;
+ }
+
+ public PrintStream getOutputStream()
+ {
+ return System.out;
+ }
+
+ public int getPID()
+ {
+ return 0;
+ }
+
+ public boolean isVerbose()
+ {
+ return false;
+ }
+
+ public void unimplementedError(String message)
+ {
+ throw new RuntimeException(message);
+ }
+
+ public void warn(WARNING_ID arg0, String arg1, Object[] arg2)
+ {
+ System.err.println(arg0 + ": " + String.format(arg1, arg2));
+ }
+
+
+ public void error(ERRORS error, String extraData)
+ {
+ System.err.println(error + ": " + extraData);
+ }
+
+ }
+
+}
\ No newline at end of file
Index: fetch.xml
===================================================================
--- fetch.xml (revision 1062099)
+++ fetch.xml (working copy)
@@ -258,6 +258,19 @@
<f2 project="bsf" />
</target>
+
+ <target name="jna"
+ description="load jna library"
+ depends="init">
+ <f2 project="com.sun.jna" archive="jna"/>
+ </target>
+
+ <target name="jna-posix"
+ description="load jna-posix library"
+ depends="jna">
+ <f2 project="org.jruby.ext.posix" archive="jna-posix"/>
+ </target>
+
<target name="jruby"
description="load jruby"
depends="bsf">
Index: src/main/org/apache/tools/ant/taskdefs/Copy.java
===================================================================
--- src/main/org/apache/tools/ant/taskdefs/Copy.java (revision 1062099)
+++ src/main/org/apache/tools/ant/taskdefs/Copy.java (working copy)
@@ -47,6 +47,7 @@
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.IdentityMapper;
import org.apache.tools.ant.util.LinkedHashtable;
+import org.apache.tools.ant.util.PermissionsUtils;
import org.apache.tools.ant.util.ResourceUtils;
import org.apache.tools.ant.util.SourceFileScanner;
import org.apache.tools.ant.util.FlatFileNameMapper;
@@ -106,7 +107,9 @@
// used to store the single non-file resource to copy when the
// tofile attribute has been used
private Resource singleResource = null;
-
+
+ private boolean preservePermissions = true;
+
/**
* Copy task constructor.
*/
@@ -315,6 +318,22 @@
public void setFailOnError(boolean failonerror) {
this.failonerror = failonerror;
}
+
+
+ /**
+ * Set whether to attempt to preserve Unix file permissions when copying files.
+ * @param preservePermissions true or false.
+ */
+ public void setPreservePermissions(boolean preservePermissions) {
+ this.preservePermissions = preservePermissions;
+ }
+
+ /**
+ * @return true if this task is configured to preserve file system permissions.
+ */
+ public boolean isPreservePermissions() {
+ return preservePermissions;
+ }
/**
* Add a set of files to copy.
@@ -879,6 +898,12 @@
/* append: */ false, inputEncoding,
outputEncoding, getProject(),
getForce());
+
+ if (preservePermissions) {
+ int perms = PermissionsUtils.getPermissions(new File(fromFile));
+ PermissionsUtils.setPermissions(new File(toFile), perms);
+ }
+
} catch (IOException ioe) {
String msg = "Failed to copy " + fromFile + " to " + toFile
+ " due to " + getDueTo(ioe);
Index: lib/libraries.properties
===================================================================
--- lib/libraries.properties (revision 1062099)
+++ lib/libraries.properties (working copy)
@@ -44,6 +44,8 @@
jasper-compiler.version=4.1.36
jasper-runtime.version=${jasper-compiler.version}
jdepend.version=2.9.1
+jna.version=3.0.9
+jna-posix.version=1.0.3
jruby.version=0.9.8
junit.version=4.8.1
jsch.version=0.1.42
Index: src/tests/junit/org/apache/tools/ant/util/PermissionsUtilsTest.java
===================================================================
--- src/tests/junit/org/apache/tools/ant/util/PermissionsUtilsTest.java (revision 0)
+++ src/tests/junit/org/apache/tools/ant/util/PermissionsUtilsTest.java (revision 0)
@@ -0,0 +1,77 @@
+package org.apache.tools.ant.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+public class PermissionsUtilsTest extends TestCase {
+
+ private boolean isWindows() {
+ return System.getProperty("os.name").toLowerCase(Locale.ENGLISH)
+ .startsWith("windows");
+ }
+
+ public void testgetPermissions744() throws IOException {
+ if (isWindows()) {
+ return;
+ }
+
+ File file = File.createTempFile("mode", ".tmp");
+ try {
+ file.setExecutable(true, true);
+ file.setWritable(true, true);
+ file.setReadable(true, false);
+
+ assertEquals(0744, PermissionsUtils.getPermissions(file));
+ } finally {
+ file.delete();
+ }
+ }
+
+ public void testgetPermissions444() throws IOException {
+ if (isWindows()) {
+ return;
+ }
+
+ File file = File.createTempFile("mode", ".tmp");
+ try {
+ file.setExecutable(false, false);
+ file.setWritable(false, false);
+ file.setReadable(true, true);
+
+ assertEquals(0444, PermissionsUtils.getPermissions(file));
+ } finally {
+ file.delete();
+ }
+ }
+
+ public void testsetPermissions444() throws IOException {
+ if (isWindows()) {
+ return;
+ }
+
+ File file = File.createTempFile("mode", ".tmp");
+ try {
+ PermissionsUtils.setPermissions(file, 0444);
+ assertEquals(0444, PermissionsUtils.getPermissions(file));
+ } finally {
+ file.delete();
+ }
+ }
+
+ public void testsetPermissions754() throws IOException {
+ if (isWindows()) {
+ return;
+ }
+
+ File file = File.createTempFile("mode", ".tmp");
+ try {
+ PermissionsUtils.setPermissions(file, 0754);
+ assertEquals(0754, PermissionsUtils.getPermissions(file));
+ } finally {
+ file.delete();
+ }
+ }
+}
Index: src/main/org/apache/tools/ant/taskdefs/Zip.java
===================================================================
--- src/main/org/apache/tools/ant/taskdefs/Zip.java (revision 1062099)
+++ src/main/org/apache/tools/ant/taskdefs/Zip.java (working copy)
@@ -61,6 +61,7 @@
import org.apache.tools.ant.util.GlobPatternMapper;
import org.apache.tools.ant.util.IdentityMapper;
import org.apache.tools.ant.util.MergingMapper;
+import org.apache.tools.ant.util.PermissionsUtils;
import org.apache.tools.ant.util.ResourceUtils;
import org.apache.tools.zip.UnixStat;
import org.apache.tools.zip.ZipEntry;
@@ -221,7 +222,24 @@
*/
private boolean fallBackToUTF8 = false;
+ private boolean preservePermissions = true;
+
/**
+ * Set whether to attempt to preserve Unix file permissions when copying files.
+ * @param preservePermissions true or false.
+ */
+ public void setPreservePermissions(boolean preservePermissions) {
+ this.preservePermissions = preservePermissions;
+ }
+
+ /**
+ * @return true if this task is configured to preserve file system permissions.
+ */
+ public boolean isPreservePermissions() {
+ return preservePermissions;
+ }
+
+ /**
* This is the name/location of where to
* create the .zip file.
* @param zipFile the path of the zipFile
@@ -941,6 +959,14 @@
if (dealingWithFiles) {
File f = FILE_UTILS.resolveFile(base,
resources[i].getName());
+
+ if (preservePermissions) {
+ int perms = PermissionsUtils.getPermissions(f);
+ // Keep top 3 octals or original fileMode, which are
+ // the flags that aren't related to permissions.
+ fileMode = (fileMode & 0777000) | perms;
+ }
+
zipFile(f, zOut, prefix + name, fileMode);
} else {
int thisFileMode =
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment