Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Vosmaer (GitLab) <jacob@gitlab.com>2018-01-16 17:22:06 +0300
committerJacob Vosmaer (GitLab) <jacob@gitlab.com>2018-01-16 17:22:06 +0300
commit5fbbfe6da4faea8e69118f53680cacda3f5a753c (patch)
tree86bcbfcd38670c2650e027bcbbf26baabb694ab7
parent51b0dd5ed1f4d2963672dff04cac039c658d62c1 (diff)
Add tempdir package
-rw-r--r--CHANGELOG.md4
-rw-r--r--internal/tempdir/tempdir.go33
-rw-r--r--internal/tempdir/tempdir_test.go31
-rw-r--r--internal/tempdir/testhelper_test.go20
4 files changed, 88 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b3b229bf..d82c7da5c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
# Gitaly changelog
+UNRELEASED
+
+- Add tempdir package
+ https://gitlab.com/gitlab-org/gitaly/merge_requests/538
v0.70.0
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()
+}