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

paths.go « localrepo « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c65aa43e8642a9fef2a47f429e5754937aff605e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package localrepo

import (
	"os"
	"path/filepath"
	"strings"

	"gitlab.com/gitlab-org/gitaly/internal/gitaly/storage"
	"gitlab.com/gitlab-org/gitaly/internal/helper"
)

// Path returns the on-disk path of the repository.
func (repo *Repo) Path() (string, error) {
	return repo.locator.GetRepoPath(repo)
}

// ObjectDirectoryPath returns the full path of the object directory. The errors returned are gRPC
// errors with relevant error codes and should be passed back to gRPC without further decoration.
func (repo *Repo) ObjectDirectoryPath() (string, error) {
	repoPath, err := repo.Path()
	if err != nil {
		return "", err
	}

	objectDirectoryPath := repo.GetGitObjectDirectory()
	if objectDirectoryPath == "" {
		return "", helper.ErrInvalidArgumentf("object directory path is not set")
	}

	// We need to check whether the relative object directory as given by the repository is
	// a valid path. This may either be a path in the Git repository itself, where it may either
	// point to the main object directory storage or to an object quarantine directory as
	// created by git-receive-pack(1). Alternatively, if that is not the case, then it may be a
	// manual object quarantine directory located in the storage's temporary directory. These
	// have a repository-specific prefix which we must check in order to determine whether the
	// quarantine directory does in fact belong to the repo at hand.
	if _, origError := storage.ValidateRelativePath(repoPath, objectDirectoryPath); origError != nil {
		tempDir, err := repo.locator.TempDir(repo.GetStorageName())
		if err != nil {
			return "", helper.ErrInvalidArgumentf("getting storage's temporary directory: %s", err)
		}

		expectedQuarantinePrefix := filepath.Join(tempDir, storage.QuarantineDirectoryPrefix(repo))
		absoluteObjectDirectoryPath := filepath.Join(repoPath, objectDirectoryPath)

		if !strings.HasPrefix(absoluteObjectDirectoryPath, expectedQuarantinePrefix) {
			return "", helper.ErrInvalidArgumentf("not a valid relative path: %s", origError)
		}
	}

	fullPath := filepath.Join(repoPath, objectDirectoryPath)
	if _, err := os.Stat(fullPath); os.IsNotExist(err) {
		return "", helper.ErrNotFoundf("object directory does not exist: %q", fullPath)
	}

	return fullPath, nil
}

// InfoAlternatesPath returns the full path of the alternates file.
func (repo *Repo) InfoAlternatesPath() (string, error) {
	repoPath, err := repo.Path()
	if err != nil {
		return "", err
	}

	return filepath.Join(repoPath, "objects", "info", "alternates"), nil
}