From 88aa395b83d7db421d118b43f81e648c1145a9b6 Mon Sep 17 00:00:00 2001 From: Miguel Molina Date: Wed, 20 Mar 2019 10:26:51 +0100 Subject: [PATCH] command: skip a directory if gitbase has no permission to read it Fixes #718 When a child of one of the paths given to the gitbase server is not readable because of permissions, the repo finding process just stopped and no repository was added to the pool. This skips a directory if there are no permissions to read it, so the process can continue adding repositories to the pool as usual. Signed-off-by: Miguel Molina --- _testdata/not-permission/.gitkeep | 0 cmd/gitbase/command/server.go | 3 +++ cmd/gitbase/command/server_test.go | 11 +++++++++++ 3 files changed, 14 insertions(+) create mode 100644 _testdata/not-permission/.gitkeep diff --git a/_testdata/not-permission/.gitkeep b/_testdata/not-permission/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/cmd/gitbase/command/server.go b/cmd/gitbase/command/server.go index a0f2ddb35..e49f6b810 100644 --- a/cmd/gitbase/command/server.go +++ b/cmd/gitbase/command/server.go @@ -309,6 +309,9 @@ func (c *Server) addMatch(match string) error { initDepth := strings.Count(root, string(os.PathSeparator)) return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { + if os.IsPermission(err) { + return filepath.SkipDir + } return err } diff --git a/cmd/gitbase/command/server_test.go b/cmd/gitbase/command/server_test.go index afbd87ab8..25dbd09f0 100644 --- a/cmd/gitbase/command/server_test.go +++ b/cmd/gitbase/command/server_test.go @@ -1,6 +1,7 @@ package command import ( + "os" "testing" "github.com/src-d/gitbase" @@ -10,12 +11,22 @@ import ( func TestAddMatch(t *testing.T) { require := require.New(t) + notPermissionDir := "../../../_testdata/not-permission/" + fi, err := os.Stat(notPermissionDir) + require.NoError(err) + + require.NoError(os.Chmod(notPermissionDir, 0)) + defer func() { + require.NoError(os.Chmod(notPermissionDir, fi.Mode())) + }() + expected := []struct { path string err func(error, ...interface{}) }{ {"../../../_testdata/repositories/", require.NoError}, {"../../../_testdata/repositories-link/", require.NoError}, + {notPermissionDir, require.NoError}, {"../../../_testdata/repositories-not-exist/", require.Error}, } c := &Server{pool: gitbase.NewRepositoryPool(0)}