diff options
author | Andrey Loskutov <loskutov@gmx.de> | 2014-08-15 17:27:15 +0200 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2014-11-22 23:55:47 +0100 |
commit | d81529029a887cbc2356d6fc8d0168bd2f031ca2 (patch) | |
tree | 43f2fc139084a9e4285ab06b24db6676de66e1d4 /org.eclipse.jgit.java7.test/src | |
parent | 749dfeffc8bed6caf39dfae196a98537bc36a326 (diff) | |
download | jgit-d81529029a887cbc2356d6fc8d0168bd2f031ca2.tar.gz jgit-d81529029a887cbc2356d6fc8d0168bd2f031ca2.zip |
Set permission bits for "executable" attribute according to the umask
Bug: 424395
Change-Id: I3f5c55dd4c084529af2319029305ba2e174e0636
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit.java7.test/src')
-rw-r--r-- | org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/FSJava7Test.java | 78 |
1 files changed, 76 insertions, 2 deletions
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<PosixFilePermission> 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<PosixFilePermission> readPermissions(File f) throws IOException { + return Files + .getFileAttributeView(f.toPath(), PosixFileAttributeView.class) + .readAttributes().permissions(); + } + } |