]> source.dussan.org Git - gitblit.git/commitdiff
Implement adjusting file permissions for shared repositories.
authorFlorian Zschocke <florian.zschocke@cycos.com>
Tue, 20 Aug 2013 08:58:51 +0000 (10:58 +0200)
committerFlorian Zschocke <florian.zschocke@cycos.com>
Mon, 26 Aug 2013 10:39:57 +0000 (12:39 +0200)
Implement adjusting file and directory permissions for shared
repositories in JGitUtils.

src/main/java/com/gitblit/utils/JGitUtils.java
src/test/java/com/gitblit/tests/JGitUtilsTest.java

index 03b54ee91c2d3e8479d5cee41332a3b4265b6074..49b3ad70ba1a8ff94488d5eeab57eb037dc95326 100644 (file)
@@ -32,6 +32,7 @@ import java.util.Map;
 import java.util.Map.Entry;\r
 import java.util.regex.Pattern;\r
 \r
+import org.apache.commons.io.filefilter.TrueFileFilter;\r
 import org.eclipse.jgit.api.CloneCommand;\r
 import org.eclipse.jgit.api.FetchCommand;\r
 import org.eclipse.jgit.api.Git;\r
@@ -260,12 +261,7 @@ public class JGitUtils {
         * @return Repository\r
         */\r
        public static Repository createRepository(File repositoriesFolder, String name) {\r
-               try {\r
-                       Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call();\r
-                       return git.getRepository();\r
-               } catch (GitAPIException e) {\r
-                       throw new RuntimeException(e);\r
-               }\r
+               return createRepository(repositoriesFolder, name, "FALSE");\r
        }\r
 \r
        /**\r
@@ -279,7 +275,13 @@ public class JGitUtils {
         */\r
        public static Repository createRepository(File repositoriesFolder, String name, String shared) {\r
                try {\r
-                       Repository repo = createRepository(repositoriesFolder, name);\r
+                       Repository repo = null;\r
+                       try {\r
+                               Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call();\r
+                               repo = git.getRepository();\r
+                       } catch (GitAPIException e) {\r
+                               throw new RuntimeException(e);\r
+                       }\r
 \r
                        GitConfigSharedRepository sharedRepository = new GitConfigSharedRepository(shared);\r
                        if (sharedRepository.isShared()) {\r
@@ -289,8 +291,12 @@ public class JGitUtils {
                                config.save();\r
 \r
                                if (! JnaUtils.isWindows()) {\r
-\r
-                                       //libc.chmod("/path/to/file", 0755);\r
+                                       Iterator<File> iter = org.apache.commons.io.FileUtils.iterateFilesAndDirs(repo.getDirectory(),\r
+                                                       TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);\r
+                                       // Adjust permissions on file/directory\r
+                                       while (iter.hasNext()) {\r
+                                               adjustSharedPerm(iter.next(), sharedRepository);\r
+                                       }\r
                                }\r
                        }\r
 \r
@@ -299,7 +305,9 @@ public class JGitUtils {
                        throw new RuntimeException(e);\r
                }\r
        }\r
-       private enum GitConfigSharedRepositoryValue {\r
+\r
+       private enum GitConfigSharedRepositoryValue\r
+       {\r
                UMASK("0", 0), FALSE("0", 0), OFF("0", 0), NO("0", 0),\r
                GROUP("1", 0660), TRUE("1", 0660), ON("1", 0660), YES("1", 0660),\r
                ALL("2", 0664), WORLD("2", 0664), EVERYBODY("2", 0664),\r
@@ -313,13 +321,13 @@ public class JGitUtils {
                public int getPerm() { return permValue; };\r
 \r
        }\r
+\r
        private static class GitConfigSharedRepository\r
        {\r
                private int intValue;\r
-               GitConfigSharedRepositoryValue enumValue;\r
+               private GitConfigSharedRepositoryValue enumValue;\r
 \r
-               GitConfigSharedRepository(String s)\r
-               {\r
+               GitConfigSharedRepository(String s) {\r
                        if ( s == null || s.trim().isEmpty() ) {\r
                                enumValue = GitConfigSharedRepositoryValue.GROUP;\r
                        }\r
@@ -344,25 +352,61 @@ public class JGitUtils {
                        }\r
                }\r
 \r
-               String getValue()\r
-               {\r
+               String getValue() {\r
                        if ( enumValue == GitConfigSharedRepositoryValue.Oxxx ) return Integer.toOctalString(intValue);\r
                        return enumValue.getConfigValue();\r
                }\r
 \r
-               int getPerm()\r
-               {\r
+               int getPerm() {\r
                        if ( enumValue == GitConfigSharedRepositoryValue.Oxxx ) return intValue;\r
                        return enumValue.getPerm();\r
                }\r
 \r
-               boolean isShared()\r
-               {\r
+               boolean isCustom() {\r
+                       return enumValue == GitConfigSharedRepositoryValue.Oxxx;\r
+               }\r
+\r
+               boolean isShared() {\r
                        return (enumValue.getPerm() > 0) || enumValue == GitConfigSharedRepositoryValue.Oxxx;\r
                }\r
        }\r
 \r
 \r
+       public static int adjustSharedPerm(File path, String configShared) {\r
+               return adjustSharedPerm(path, new GitConfigSharedRepository(configShared));\r
+       }\r
+\r
+\r
+       public static int adjustSharedPerm(File path, GitConfigSharedRepository configShared) {\r
+               if (! configShared.isShared()) return 0;\r
+\r
+               int perm = configShared.getPerm();\r
+               int mode = JnaUtils.getFilemode(path);\r
+               if (mode < 0) return -1;\r
+\r
+               // If the owner has no write access, delete it from group and other, too.\r
+               if ((mode & JnaUtils.S_IWUSR) == 0) perm &= ~0222;\r
+               // If the owner has execute access, set it for all blocks that have read access.\r
+               if ((mode & JnaUtils.S_IXUSR) == JnaUtils.S_IXUSR) perm |= (perm & 0444) >> 2;\r
+\r
+               if (configShared.isCustom()) {\r
+                       // Use the custom value for access permissions.\r
+                       mode |= (mode & ~0777) | perm;\r
+               }\r
+               else {\r
+                       // Just add necessary bits to existing permissions.\r
+                       mode |= perm;\r
+               }\r
+\r
+               if (path.isDirectory()) {\r
+                       mode |= (mode & 0444) >> 2;\r
+                       mode |= JnaUtils.S_ISGID;\r
+               }\r
+\r
+               return JnaUtils.setFilemode(path, mode);\r
+       }\r
+\r
+\r
        /**\r
         * Returns a list of repository names in the specified folder.\r
         * \r
index 4affca2d9a1926c4f215d4103cf1c9bab4df1ffd..6f86b9f7e40dbf8908be573f6f1f1e4806d41632 100644 (file)
@@ -52,6 +52,7 @@ import com.gitblit.models.PathModel.PathChangeModel;
 import com.gitblit.models.RefModel;\r
 import com.gitblit.utils.CompressionUtils;\r
 import com.gitblit.utils.JGitUtils;\r
+import com.gitblit.utils.JnaUtils;\r
 import com.gitblit.utils.StringUtils;\r
 \r
 public class JGitUtilsTest {\r
@@ -150,7 +151,7 @@ public class JGitUtilsTest {
 \r
        @Test\r
        public void testCreateRepositoryShared() throws Exception {\r
-               String[] repositories = { "NewTestRepository.git", "NewTestRepository" };\r
+               String[] repositories = { "NewSharedTestRepository.git" };\r
                for (String repositoryName : repositories) {\r
                        Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES,\r
                                        repositoryName, "group");\r
@@ -159,13 +160,21 @@ public class JGitUtilsTest {
                        assertNotNull(repository);\r
                        assertFalse(JGitUtils.hasCommits(repository));\r
                        assertNull(JGitUtils.getFirstCommit(repository, null));\r
-                       assertEquals(folder.lastModified(), JGitUtils.getFirstChange(repository, null)\r
-                                       .getTime());\r
-                       assertEquals(folder.lastModified(), JGitUtils.getLastChange(repository).when.getTime());\r
-                       assertNull(JGitUtils.getCommit(repository, null));\r
+\r
+                       assertTrue(folder.exists());\r
+                       int mode = JnaUtils.getFilemode(folder);\r
+                       assertEquals(JnaUtils.S_ISGID, mode & JnaUtils.S_ISGID);\r
+                       assertEquals(JnaUtils.S_IRWXG, mode & JnaUtils.S_IRWXG);\r
+\r
+                       mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/HEAD");\r
+                       assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);\r
+\r
+                       mode = JnaUtils.getFilemode(folder.getAbsolutePath() + "/config");\r
+                       assertEquals(JnaUtils.S_IRGRP | JnaUtils.S_IWGRP, mode & JnaUtils.S_IRWXG);\r
+\r
                        repository.close();\r
                        RepositoryCache.close(repository);\r
-//                     FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);\r
+                       FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);\r
                }\r
        }\r
 \r