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

namespace.go « namespace « service « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6d49342a591ebbb89796aa35587b79ab3b0b0d80 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package namespace

import (
	"os"
	"path"

	"golang.org/x/net/context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
	"gitlab.com/gitlab-org/gitaly/internal/helper"
)

var noNameError = status.Errorf(codes.InvalidArgument, "Name: cannot be empty")

func (s *server) NamespaceExists(ctx context.Context, in *gitalypb.NamespaceExistsRequest) (*gitalypb.NamespaceExistsResponse, error) {
	storagePath, err := helper.GetStorageByName(in.GetStorageName())
	if err != nil {
		return nil, err
	}

	// This case should return an error, as else we'd actually say the path exists as the
	// storage exists
	if in.GetName() == "" {
		return nil, noNameError
	}

	if fi, err := os.Stat(namespacePath(storagePath, in.GetName())); os.IsNotExist(err) {
		return &gitalypb.NamespaceExistsResponse{Exists: false}, nil
	} else if err != nil {
		return nil, status.Errorf(codes.Internal, "could not stat the directory: %v", err)
	} else {
		return &gitalypb.NamespaceExistsResponse{Exists: fi.IsDir()}, nil
	}
}

func (s *server) AddNamespace(ctx context.Context, in *gitalypb.AddNamespaceRequest) (*gitalypb.AddNamespaceResponse, error) {
	storagePath, err := helper.GetStorageByName(in.GetStorageName())
	if err != nil {
		return nil, err
	}

	name := in.GetName()
	if len(name) == 0 {
		return nil, noNameError
	}

	if err = os.MkdirAll(namespacePath(storagePath, name), 0770); err != nil {
		return nil, status.Errorf(codes.Internal, "create directory: %v", err)
	}

	return &gitalypb.AddNamespaceResponse{}, nil
}

func (s *server) RenameNamespace(ctx context.Context, in *gitalypb.RenameNamespaceRequest) (*gitalypb.RenameNamespaceResponse, error) {
	storagePath, err := helper.GetStorageByName(in.GetStorageName())
	if err != nil {
		return nil, err
	}

	if in.GetFrom() == "" || in.GetTo() == "" {
		return nil, status.Errorf(codes.InvalidArgument, "from and to cannot be empty")
	}

	// No need to check if the from path exists, if it doesn't, we'd later get an
	// os.LinkError
	toExistsCheck := &gitalypb.NamespaceExistsRequest{StorageName: in.StorageName, Name: in.GetTo()}
	if exists, err := s.NamespaceExists(ctx, toExistsCheck); err != nil {
		return nil, err
	} else if exists.Exists {
		return nil, status.Errorf(codes.InvalidArgument, "to directory %s already exists", in.GetTo())
	}

	err = os.Rename(namespacePath(storagePath, in.GetFrom()), namespacePath(storagePath, in.GetTo()))
	if _, ok := err.(*os.LinkError); ok {
		return nil, status.Errorf(codes.InvalidArgument, "from directory %s not found", in.GetFrom())
	} else if err != nil {
		return nil, status.Errorf(codes.Internal, "rename: %v", err)
	}

	return &gitalypb.RenameNamespaceResponse{}, nil
}

func (s *server) RemoveNamespace(ctx context.Context, in *gitalypb.RemoveNamespaceRequest) (*gitalypb.RemoveNamespaceResponse, error) {
	storagePath, err := helper.GetStorageByName(in.GetStorageName())
	if err != nil {
		return nil, err
	}

	// Needed as else we might destroy the whole storage
	if in.GetName() == "" {
		return nil, noNameError
	}

	// os.RemoveAll is idempotent by itself
	// No need to check if the directory exists, or not
	if err = os.RemoveAll(namespacePath(storagePath, in.GetName())); err != nil {
		return nil, status.Errorf(codes.Internal, "removal: %v", err)
	}
	return &gitalypb.RemoveNamespaceResponse{}, nil
}

func namespacePath(storage, ns string) string {
	return path.Join(storage, ns)
}