diff options
author | Jacob Vosmaer (GitLab) <jacob@gitlab.com> | 2018-01-16 17:22:06 +0300 |
---|---|---|
committer | Jacob Vosmaer (GitLab) <jacob@gitlab.com> | 2018-01-16 17:22:06 +0300 |
commit | 5fbbfe6da4faea8e69118f53680cacda3f5a753c (patch) | |
tree | 86bcbfcd38670c2650e027bcbbf26baabb694ab7 /internal/tempdir | |
parent | 51b0dd5ed1f4d2963672dff04cac039c658d62c1 (diff) |
Add tempdir package
Diffstat (limited to 'internal/tempdir')
-rw-r--r-- | internal/tempdir/tempdir.go | 33 | ||||
-rw-r--r-- | internal/tempdir/tempdir_test.go | 31 | ||||
-rw-r--r-- | internal/tempdir/testhelper_test.go | 20 |
3 files changed, 84 insertions, 0 deletions
diff --git a/internal/tempdir/tempdir.go b/internal/tempdir/tempdir.go new file mode 100644 index 000000000..44e00570e --- /dev/null +++ b/internal/tempdir/tempdir.go @@ -0,0 +1,33 @@ +package tempdir + +import ( + "io/ioutil" + "os" + "path" + + pb "gitlab.com/gitlab-org/gitaly-proto/go" + "gitlab.com/gitlab-org/gitaly/internal/helper" +) + +const ( + // We need to be careful that this path does not clash with any + // directory name that could be provided by a user. The '+' character is + // not allowed in GitLab namespaces or repositories. + tmpRoot = "+gitaly/tmp" +) + +// New returns the path of a new temporary directory for use with the +// repository. The caller must os.RemoveAll the directory when done. +func New(repo *pb.Repository) (string, error) { + storageDir, err := helper.GetStorageByName(repo.StorageName) + if err != nil { + return "", err + } + + root := path.Join(storageDir, tmpRoot) + if err := os.MkdirAll(root, 0700); err != nil { + return "", err + } + + return ioutil.TempDir(root, "repo") +} diff --git a/internal/tempdir/tempdir_test.go b/internal/tempdir/tempdir_test.go new file mode 100644 index 000000000..3762a8883 --- /dev/null +++ b/internal/tempdir/tempdir_test.go @@ -0,0 +1,31 @@ +package tempdir + +import ( + "io/ioutil" + "os" + "path" + "testing" + + pb "gitlab.com/gitlab-org/gitaly-proto/go" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" + + "github.com/stretchr/testify/require" +) + +func TestNewSuccess(t *testing.T) { + repo := testhelper.TestRepository() + + tempDir, err := New(repo) + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + err = ioutil.WriteFile(path.Join(tempDir, "test"), []byte("hello"), 0644) + require.NoError(t, err, "write file in tempdir") + + require.NoError(t, os.RemoveAll(tempDir), "remove tempdir") +} + +func TestNewFailStorageUnknown(t *testing.T) { + _, err := New(&pb.Repository{StorageName: "does-not-exist", RelativePath: "foobar.git"}) + require.Error(t, err) +} diff --git a/internal/tempdir/testhelper_test.go b/internal/tempdir/testhelper_test.go new file mode 100644 index 000000000..0cb5eb875 --- /dev/null +++ b/internal/tempdir/testhelper_test.go @@ -0,0 +1,20 @@ +package tempdir + +import ( + "os" + "testing" + + "gitlab.com/gitlab-org/gitaly/internal/testhelper" +) + +func TestMain(m *testing.M) { + os.Exit(testMain(m)) +} + +func testMain(m *testing.M) int { + defer testhelper.MustHaveNoChildProcess() + + testhelper.ConfigureTestStorage() + + return m.Run() +} |