Guest User

Untitled

a guest
Jul 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.92 KB | None | 0 0
  1. ### Eclipse Workspace Patch 1.0
  2. #P antTrunk
  3. Index: src/main/org/apache/tools/ant/util/PermissionsUtils.java
  4. ===================================================================
  5. --- src/main/org/apache/tools/ant/util/PermissionsUtils.java (revision 0)
  6. +++ src/main/org/apache/tools/ant/util/PermissionsUtils.java (revision 0)
  7. @@ -0,0 +1,89 @@
  8. +package org.apache.tools.ant.util;
  9. +
  10. +import java.io.File;
  11. +import java.io.InputStream;
  12. +import java.io.PrintStream;
  13. +
  14. +import org.jruby.ext.posix.POSIX;
  15. +import org.jruby.ext.posix.POSIX.ERRORS;
  16. +import org.jruby.ext.posix.POSIXFactory;
  17. +import org.jruby.ext.posix.POSIXHandler;
  18. +
  19. +/**
  20. + * Provides convenience methods for manipulating file permissions.
  21. + */
  22. +public class PermissionsUtils
  23. +{
  24. + private static POSIX posix = POSIXFactory.getPOSIX(new AntPOSIXHandler(), true);
  25. +
  26. + public static int getPermissions(File file)
  27. + {
  28. + return posix.stat(file.getAbsolutePath()).mode() & 0777;
  29. + }
  30. +
  31. + public static void setPermissions(File file, int perms)
  32. + {
  33. + posix.chmod(file.getAbsolutePath(), perms);
  34. + }
  35. +
  36. + /**
  37. + * Minimal POSIX handler for Ant tasks. There's scope for improvement here, like redirecting warnings through the Ant
  38. + * log, failing on errors, etc...
  39. + */
  40. + public static class AntPOSIXHandler implements POSIXHandler
  41. + {
  42. +
  43. + public File getCurrentWorkingDirectory()
  44. + {
  45. + return new File(".");
  46. + }
  47. +
  48. + public String[] getEnv()
  49. + {
  50. + return new String[]{};
  51. + }
  52. +
  53. + public PrintStream getErrorStream()
  54. + {
  55. + return System.err;
  56. + }
  57. +
  58. + public InputStream getInputStream()
  59. + {
  60. + return System.in;
  61. + }
  62. +
  63. + public PrintStream getOutputStream()
  64. + {
  65. + return System.out;
  66. + }
  67. +
  68. + public int getPID()
  69. + {
  70. + return 0;
  71. + }
  72. +
  73. + public boolean isVerbose()
  74. + {
  75. + return false;
  76. + }
  77. +
  78. + public void unimplementedError(String message)
  79. + {
  80. + throw new RuntimeException(message);
  81. + }
  82. +
  83. + public void warn(WARNING_ID arg0, String arg1, Object[] arg2)
  84. + {
  85. + System.err.println(arg0 + ": " + String.format(arg1, arg2));
  86. + }
  87. +
  88. +
  89. + public void error(ERRORS error, String extraData)
  90. + {
  91. + System.err.println(error + ": " + extraData);
  92. + }
  93. +
  94. + }
  95. +
  96. +}
  97. \ No newline at end of file
  98. Index: fetch.xml
  99. ===================================================================
  100. --- fetch.xml (revision 1062099)
  101. +++ fetch.xml (working copy)
  102. @@ -258,6 +258,19 @@
  103. <f2 project="bsf" />
  104. </target>
  105.  
  106. +
  107. + <target name="jna"
  108. + description="load jna library"
  109. + depends="init">
  110. + <f2 project="com.sun.jna" archive="jna"/>
  111. + </target>
  112. +
  113. + <target name="jna-posix"
  114. + description="load jna-posix library"
  115. + depends="jna">
  116. + <f2 project="org.jruby.ext.posix" archive="jna-posix"/>
  117. + </target>
  118. +
  119. <target name="jruby"
  120. description="load jruby"
  121. depends="bsf">
  122. Index: src/main/org/apache/tools/ant/taskdefs/Copy.java
  123. ===================================================================
  124. --- src/main/org/apache/tools/ant/taskdefs/Copy.java (revision 1062099)
  125. +++ src/main/org/apache/tools/ant/taskdefs/Copy.java (working copy)
  126. @@ -47,6 +47,7 @@
  127. import org.apache.tools.ant.util.FileNameMapper;
  128. import org.apache.tools.ant.util.IdentityMapper;
  129. import org.apache.tools.ant.util.LinkedHashtable;
  130. +import org.apache.tools.ant.util.PermissionsUtils;
  131. import org.apache.tools.ant.util.ResourceUtils;
  132. import org.apache.tools.ant.util.SourceFileScanner;
  133. import org.apache.tools.ant.util.FlatFileNameMapper;
  134. @@ -106,7 +107,9 @@
  135. // used to store the single non-file resource to copy when the
  136. // tofile attribute has been used
  137. private Resource singleResource = null;
  138. -
  139. +
  140. + private boolean preservePermissions = true;
  141. +
  142. /**
  143. * Copy task constructor.
  144. */
  145. @@ -315,6 +318,22 @@
  146. public void setFailOnError(boolean failonerror) {
  147. this.failonerror = failonerror;
  148. }
  149. +
  150. +
  151. + /**
  152. + * Set whether to attempt to preserve Unix file permissions when copying files.
  153. + * @param preservePermissions true or false.
  154. + */
  155. + public void setPreservePermissions(boolean preservePermissions) {
  156. + this.preservePermissions = preservePermissions;
  157. + }
  158. +
  159. + /**
  160. + * @return true if this task is configured to preserve file system permissions.
  161. + */
  162. + public boolean isPreservePermissions() {
  163. + return preservePermissions;
  164. + }
  165.  
  166. /**
  167. * Add a set of files to copy.
  168. @@ -879,6 +898,12 @@
  169. /* append: */ false, inputEncoding,
  170. outputEncoding, getProject(),
  171. getForce());
  172. +
  173. + if (preservePermissions) {
  174. + int perms = PermissionsUtils.getPermissions(new File(fromFile));
  175. + PermissionsUtils.setPermissions(new File(toFile), perms);
  176. + }
  177. +
  178. } catch (IOException ioe) {
  179. String msg = "Failed to copy " + fromFile + " to " + toFile
  180. + " due to " + getDueTo(ioe);
  181. Index: lib/libraries.properties
  182. ===================================================================
  183. --- lib/libraries.properties (revision 1062099)
  184. +++ lib/libraries.properties (working copy)
  185. @@ -44,6 +44,8 @@
  186. jasper-compiler.version=4.1.36
  187. jasper-runtime.version=${jasper-compiler.version}
  188. jdepend.version=2.9.1
  189. +jna.version=3.0.9
  190. +jna-posix.version=1.0.3
  191. jruby.version=0.9.8
  192. junit.version=4.8.1
  193. jsch.version=0.1.42
  194. Index: src/tests/junit/org/apache/tools/ant/util/PermissionsUtilsTest.java
  195. ===================================================================
  196. --- src/tests/junit/org/apache/tools/ant/util/PermissionsUtilsTest.java (revision 0)
  197. +++ src/tests/junit/org/apache/tools/ant/util/PermissionsUtilsTest.java (revision 0)
  198. @@ -0,0 +1,77 @@
  199. +package org.apache.tools.ant.util;
  200. +
  201. +import java.io.File;
  202. +import java.io.IOException;
  203. +import java.util.Locale;
  204. +
  205. +import junit.framework.TestCase;
  206. +
  207. +public class PermissionsUtilsTest extends TestCase {
  208. +
  209. + private boolean isWindows() {
  210. + return System.getProperty("os.name").toLowerCase(Locale.ENGLISH)
  211. + .startsWith("windows");
  212. + }
  213. +
  214. + public void testgetPermissions744() throws IOException {
  215. + if (isWindows()) {
  216. + return;
  217. + }
  218. +
  219. + File file = File.createTempFile("mode", ".tmp");
  220. + try {
  221. + file.setExecutable(true, true);
  222. + file.setWritable(true, true);
  223. + file.setReadable(true, false);
  224. +
  225. + assertEquals(0744, PermissionsUtils.getPermissions(file));
  226. + } finally {
  227. + file.delete();
  228. + }
  229. + }
  230. +
  231. + public void testgetPermissions444() throws IOException {
  232. + if (isWindows()) {
  233. + return;
  234. + }
  235. +
  236. + File file = File.createTempFile("mode", ".tmp");
  237. + try {
  238. + file.setExecutable(false, false);
  239. + file.setWritable(false, false);
  240. + file.setReadable(true, true);
  241. +
  242. + assertEquals(0444, PermissionsUtils.getPermissions(file));
  243. + } finally {
  244. + file.delete();
  245. + }
  246. + }
  247. +
  248. + public void testsetPermissions444() throws IOException {
  249. + if (isWindows()) {
  250. + return;
  251. + }
  252. +
  253. + File file = File.createTempFile("mode", ".tmp");
  254. + try {
  255. + PermissionsUtils.setPermissions(file, 0444);
  256. + assertEquals(0444, PermissionsUtils.getPermissions(file));
  257. + } finally {
  258. + file.delete();
  259. + }
  260. + }
  261. +
  262. + public void testsetPermissions754() throws IOException {
  263. + if (isWindows()) {
  264. + return;
  265. + }
  266. +
  267. + File file = File.createTempFile("mode", ".tmp");
  268. + try {
  269. + PermissionsUtils.setPermissions(file, 0754);
  270. + assertEquals(0754, PermissionsUtils.getPermissions(file));
  271. + } finally {
  272. + file.delete();
  273. + }
  274. + }
  275. +}
  276. Index: src/main/org/apache/tools/ant/taskdefs/Zip.java
  277. ===================================================================
  278. --- src/main/org/apache/tools/ant/taskdefs/Zip.java (revision 1062099)
  279. +++ src/main/org/apache/tools/ant/taskdefs/Zip.java (working copy)
  280. @@ -61,6 +61,7 @@
  281. import org.apache.tools.ant.util.GlobPatternMapper;
  282. import org.apache.tools.ant.util.IdentityMapper;
  283. import org.apache.tools.ant.util.MergingMapper;
  284. +import org.apache.tools.ant.util.PermissionsUtils;
  285. import org.apache.tools.ant.util.ResourceUtils;
  286. import org.apache.tools.zip.UnixStat;
  287. import org.apache.tools.zip.ZipEntry;
  288. @@ -221,7 +222,24 @@
  289. */
  290. private boolean fallBackToUTF8 = false;
  291.  
  292. + private boolean preservePermissions = true;
  293. +
  294. /**
  295. + * Set whether to attempt to preserve Unix file permissions when copying files.
  296. + * @param preservePermissions true or false.
  297. + */
  298. + public void setPreservePermissions(boolean preservePermissions) {
  299. + this.preservePermissions = preservePermissions;
  300. + }
  301. +
  302. + /**
  303. + * @return true if this task is configured to preserve file system permissions.
  304. + */
  305. + public boolean isPreservePermissions() {
  306. + return preservePermissions;
  307. + }
  308. +
  309. + /**
  310. * This is the name/location of where to
  311. * create the .zip file.
  312. * @param zipFile the path of the zipFile
  313. @@ -941,6 +959,14 @@
  314. if (dealingWithFiles) {
  315. File f = FILE_UTILS.resolveFile(base,
  316. resources[i].getName());
  317. +
  318. + if (preservePermissions) {
  319. + int perms = PermissionsUtils.getPermissions(f);
  320. + // Keep top 3 octals or original fileMode, which are
  321. + // the flags that aren't related to permissions.
  322. + fileMode = (fileMode & 0777000) | perms;
  323. + }
  324. +
  325. zipFile(f, zOut, prefix + name, fileMode);
  326. } else {
  327. int thisFileMode =
Add Comment
Please, Sign In to add comment