-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathbase_symlink_factory.go
More file actions
109 lines (95 loc) · 3.14 KB
/
base_symlink_factory.go
File metadata and controls
109 lines (95 loc) · 3.14 KB
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
107
108
109
package virtual
import (
"context"
"unicode/utf8"
remoteexecution "github.com/bazelbuild/remote-apis/build/bazel/remote/execution/v2"
"github.com/buildbarn/bb-remote-execution/pkg/proto/bazeloutputservice"
"github.com/buildbarn/bb-storage/pkg/filesystem"
"github.com/buildbarn/bb-storage/pkg/filesystem/path"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type symlinkFactory struct {
defaultAttributesSetter DefaultAttributesSetter
}
func (f *symlinkFactory) LookupSymlink(target []byte) LinkableLeaf {
return symlink{
defaultAttributesSetter: f.defaultAttributesSetter,
target: target,
}
}
// NewBaseSymlinkFactory creates a SymlinkFactory that can be used to create
// simple immutable symlink nodes.
func NewBaseSymlinkFactory(defaultAttributesSetter DefaultAttributesSetter) SymlinkFactory {
return &symlinkFactory{
defaultAttributesSetter: defaultAttributesSetter,
}
}
type symlink struct {
placeholderFile
defaultAttributesSetter DefaultAttributesSetter
target []byte
}
func (f symlink) readlinkParser() (path.Parser, error) {
if !utf8.Valid(f.target) {
return nil, status.Error(codes.InvalidArgument, "Symbolic link contents are not valid UTF-8")
}
return path.UNIXFormat.NewParser(string(f.target)), nil
}
func (f symlink) readlinkString() (string, error) {
targetParser, err := f.readlinkParser()
if err != nil {
return "", err
}
targetPath, scopeWalker := path.EmptyBuilder.Join(path.VoidScopeWalker)
if err := path.Resolve(targetParser, scopeWalker); err != nil {
return "", err
}
return targetPath.GetUNIXString(), nil
}
func (f symlink) VirtualGetAttributes(ctx context.Context, requested AttributesMask, attributes *Attributes) {
f.defaultAttributesSetter(requested, attributes)
attributes.SetChangeID(0)
attributes.SetFileType(filesystem.FileTypeSymlink)
attributes.SetHasNamedAttributes(false)
attributes.SetPermissions(PermissionsRead | PermissionsWrite | PermissionsExecute)
attributes.SetSizeBytes(uint64(len(f.target)))
}
func (f symlink) VirtualReadlink(ctx context.Context) ([]byte, Status) {
return f.target, StatusOK
}
func (f symlink) VirtualSetAttributes(ctx context.Context, in *Attributes, requested AttributesMask, out *Attributes) Status {
if _, ok := in.GetSizeBytes(); ok {
return StatusErrInval
}
f.VirtualGetAttributes(ctx, requested, out)
return StatusOK
}
func (f symlink) VirtualApply(data any) bool {
switch p := data.(type) {
case *ApplyReadlink:
p.Target, p.Err = f.readlinkParser()
case *ApplyGetBazelOutputServiceStat:
if target, err := f.readlinkString(); err == nil {
p.Stat = &bazeloutputservice.BatchStatResponse_Stat{
Type: &bazeloutputservice.BatchStatResponse_Stat_Symlink_{
Symlink: &bazeloutputservice.BatchStatResponse_Stat_Symlink{
Target: target,
},
},
}
} else {
p.Err = err
}
case *ApplyAppendOutputPathPersistencyDirectoryNode:
if target, err := f.readlinkString(); err == nil {
p.Directory.Symlinks = append(p.Directory.Symlinks, &remoteexecution.SymlinkNode{
Name: p.Name.String(),
Target: target,
})
}
default:
return f.placeholderFile.VirtualApply(data)
}
return true
}