From d81529029a887cbc2356d6fc8d0168bd2f031ca2 Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Fri, 15 Aug 2014 17:27:15 +0200 Subject: Set permission bits for "executable" attribute according to the umask Bug: 424395 Change-Id: I3f5c55dd4c084529af2319029305ba2e174e0636 Signed-off-by: Andrey Loskutov Signed-off-by: Matthias Sohn --- .../src/org/eclipse/jgit/util/FSJava7Test.java | 78 +++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) (limited to 'org.eclipse.jgit.java7.test') diff --git a/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/FSJava7Test.java b/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/FSJava7Test.java index 70eaef231a..91555f371a 100644 --- a/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/FSJava7Test.java +++ b/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/FSJava7Test.java @@ -46,13 +46,21 @@ package org.eclipse.jgit.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeFalse; +import static org.junit.Assume.assumeNotNull; +import static org.junit.Assume.assumeTrue; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.PosixFilePermission; +import java.util.Set; import org.eclipse.jgit.junit.RepositoryTestCase; -import org.eclipse.jgit.util.FS; -import org.eclipse.jgit.util.FileUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -112,4 +120,70 @@ public class FSJava7Test { assertTrue(fs.canExecute(target)); } + @Test + public void testExecutableAttributes() throws Exception { + FS fs = FS.DETECTED; + // If this assumption fails the test is halted and ignored. + assumeTrue(fs instanceof FS_POSIX_Java7); + + File f = new File(trash, "bla"); + assertTrue(f.createNewFile()); + assertFalse(fs.canExecute(f)); + + String umask = readUmask(); + assumeNotNull(umask); + + char others = umask.charAt(umask.length() - 1); + + boolean badUmask; + if (others != '0' && others != '2' && others != '4' && others != '6') { + // umask is set in the way that "others" can not "execute" => git + // CLI will not set "execute" attribute for "others", so we also + // don't care + badUmask = true; + } else { + badUmask = false; + } + + Set permissions = readPermissions(f); + assertTrue(!permissions.contains(PosixFilePermission.OTHERS_EXECUTE)); + assertTrue(!permissions.contains(PosixFilePermission.GROUP_EXECUTE)); + assertTrue(!permissions.contains(PosixFilePermission.OWNER_EXECUTE)); + + fs.setExecute(f, true); + + permissions = readPermissions(f); + assertTrue("'owner' execute permission not set", + permissions.contains(PosixFilePermission.OWNER_EXECUTE)); + assertTrue("'group' execute permission not set", + permissions.contains(PosixFilePermission.GROUP_EXECUTE)); + if (badUmask) { + assertFalse("'others' execute permission set", + permissions.contains(PosixFilePermission.OTHERS_EXECUTE)); + System.err.println("WARNING: your system's umask: \"" + umask + + "\" doesn't allow FSJava7Test to test if setting posix" + + " permissions for \"others\" works properly"); + assumeFalse(badUmask); + } else { + assertTrue("'others' execute permission not set", + permissions.contains(PosixFilePermission.OTHERS_EXECUTE)); + } + } + + private String readUmask() throws Exception { + Process p = Runtime.getRuntime().exec( + new String[] { "sh", "-c", "umask" }, null, null); + final BufferedReader lineRead = new BufferedReader( + new InputStreamReader(p.getInputStream(), Charset + .defaultCharset().name())); + p.waitFor(); + return lineRead.readLine(); + } + + private Set readPermissions(File f) throws IOException { + return Files + .getFileAttributeView(f.toPath(), PosixFileAttributeView.class) + .readAttributes().permissions(); + } + } -- cgit v1.2.3