-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_properties_value.go
231 lines (205 loc) · 7.64 KB
/
file_properties_value.go
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package analysis
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"github.com/buildbarn/bb-storage/pkg/filesystem/path"
"github.com/buildbarn/bonanza/pkg/evaluation"
model_core "github.com/buildbarn/bonanza/pkg/model/core"
model_filesystem "github.com/buildbarn/bonanza/pkg/model/filesystem"
model_parser "github.com/buildbarn/bonanza/pkg/model/parser"
model_analysis_pb "github.com/buildbarn/bonanza/pkg/proto/model/analysis"
model_core_pb "github.com/buildbarn/bonanza/pkg/proto/model/core"
model_filesystem_pb "github.com/buildbarn/bonanza/pkg/proto/model/filesystem"
"github.com/buildbarn/bonanza/pkg/storage/object"
)
// reposFilePropertiesResolver resolves the properties of a file
// contained in a repo. All repos are placed in a fictive root
// directory, which allows symbolic links with targets of shape
// "../${repo}/${file}" to resolve properly.
type reposFilePropertiesResolver[TReference object.BasicReference] struct {
context context.Context
directoryReader model_parser.ParsedObjectReader[TReference, model_core.Message[*model_filesystem_pb.Directory, TReference]]
leavesReader model_parser.ParsedObjectReader[TReference, model_core.Message[*model_filesystem_pb.Leaves, TReference]]
environment FilePropertiesEnvironment[TReference]
currentDirectoryReference model_core.Message[*model_core_pb.Reference, TReference]
stack []model_core.Message[*model_filesystem_pb.Directory, TReference]
fileProperties model_core.Message[*model_filesystem_pb.FileProperties, TReference]
gotTerminal bool
}
var _ path.ComponentWalker = (*reposFilePropertiesResolver[object.LocalReference])(nil)
func (r *reposFilePropertiesResolver[TReference]) getCurrentLeaves() (model_core.Message[*model_filesystem_pb.Leaves, TReference], error) {
return model_filesystem.DirectoryGetLeaves(r.context, r.leavesReader, r.stack[len(r.stack)-1])
}
func (r *reposFilePropertiesResolver[TReference]) dereferenceCurrentDirectory() error {
if r.currentDirectoryReference.IsSet() {
d, err := model_parser.Dereference(r.context, r.directoryReader, r.currentDirectoryReference)
if err != nil {
return err
}
r.stack = append(r.stack, d)
r.currentDirectoryReference.Clear()
}
return nil
}
func (r *reposFilePropertiesResolver[TReference]) OnDirectory(name path.Component) (path.GotDirectoryOrSymlink, error) {
if err := r.dereferenceCurrentDirectory(); err != nil {
return nil, err
}
if len(r.stack) == 0 {
// Currently within the root directory. Enter the repo
// corresponding to the provided directory name.
repoValue := r.environment.GetRepoValue(&model_analysis_pb.Repo_Key{
CanonicalRepo: name.String(),
})
if !repoValue.IsSet() {
return nil, evaluation.ErrMissingDependency
}
r.currentDirectoryReference = model_core.NewNestedMessage(repoValue, repoValue.Message.RootDirectoryReference.GetReference())
return path.GotDirectory{
Child: r,
IsReversible: true,
}, nil
}
d := r.stack[len(r.stack)-1]
n := name.String()
directories := d.Message.Directories
if i, ok := sort.Find(
len(directories),
func(i int) int { return strings.Compare(n, directories[i].Name) },
); ok {
switch contents := directories[i].Contents.(type) {
case *model_filesystem_pb.DirectoryNode_ContentsExternal:
r.currentDirectoryReference = model_core.NewNestedMessage(d, contents.ContentsExternal.Reference)
case *model_filesystem_pb.DirectoryNode_ContentsInline:
r.stack = append(r.stack, model_core.NewNestedMessage(d, contents.ContentsInline))
default:
return nil, errors.New("unknown directory contents type")
}
return path.GotDirectory{
Child: r,
IsReversible: true,
}, nil
}
leaves, err := r.getCurrentLeaves()
if err != nil {
return nil, err
}
files := leaves.Message.Files
if _, ok := sort.Find(
len(files),
func(i int) int { return strings.Compare(n, files[i].Name) },
); ok {
return nil, errors.New("path resolves to a regular file, while a directory was expected")
}
symlinks := leaves.Message.Symlinks
if i, ok := sort.Find(
len(symlinks),
func(i int) int { return strings.Compare(n, symlinks[i].Name) },
); ok {
return path.GotSymlink{
Parent: path.NewRelativeScopeWalker(r),
Target: path.UNIXFormat.NewParser(symlinks[i].Target),
}, nil
}
return nil, errors.New("path does not exist")
}
func (r *reposFilePropertiesResolver[TReference]) OnTerminal(name path.Component) (*path.GotSymlink, error) {
if err := r.dereferenceCurrentDirectory(); err != nil {
return nil, err
}
if len(r.stack) == 0 {
return path.OnTerminalViaOnDirectory(r, name)
}
d := r.stack[len(r.stack)-1]
n := name.String()
directories := d.Message.Directories
if _, ok := sort.Find(
len(directories),
func(i int) int { return strings.Compare(n, directories[i].Name) },
); ok {
return nil, errors.New("path resolves to a directory, while a file was expected")
}
leaves, err := r.getCurrentLeaves()
if err != nil {
return nil, err
}
files := leaves.Message.Files
if i, ok := sort.Find(
len(files),
func(i int) int { return strings.Compare(n, files[i].Name) },
); ok {
properties := files[i].Properties
if properties == nil {
return nil, errors.New("path resolves to file that does not have any properties")
}
r.fileProperties = model_core.NewNestedMessage(leaves, properties)
r.gotTerminal = true
return nil, nil
}
symlinks := leaves.Message.Symlinks
if i, ok := sort.Find(
len(symlinks),
func(i int) int { return strings.Compare(n, symlinks[i].Name) },
); ok {
return &path.GotSymlink{
Parent: path.NewRelativeScopeWalker(r),
Target: path.UNIXFormat.NewParser(symlinks[i].Target),
}, nil
}
r.gotTerminal = true
return nil, nil
}
func (r *reposFilePropertiesResolver[TReference]) OnUp() (path.ComponentWalker, error) {
if r.currentDirectoryReference.IsSet() {
r.currentDirectoryReference.Clear()
} else if len(r.stack) == 0 {
return nil, errors.New("path escapes repositories directory")
} else {
r.stack = r.stack[:len(r.stack)-1]
}
return r, nil
}
func (c *baseComputer[TReference, TMetadata]) ComputeFilePropertiesValue(ctx context.Context, key *model_analysis_pb.FileProperties_Key, e FilePropertiesEnvironment[TReference]) (PatchedFilePropertiesValue, error) {
directoryReaders, gotDirectoryReaders := e.GetDirectoryReadersValue(&model_analysis_pb.DirectoryReaders_Key{})
if !gotDirectoryReaders {
return PatchedFilePropertiesValue{}, evaluation.ErrMissingDependency
}
resolver := reposFilePropertiesResolver[TReference]{
context: ctx,
directoryReader: directoryReaders.Directory,
leavesReader: directoryReaders.Leaves,
environment: e,
}
canonicalRepo, ok := path.NewComponent(key.CanonicalRepo)
if !ok {
return PatchedFilePropertiesValue{}, fmt.Errorf("canonical repo is not a valid pathname component")
}
repoDirectory, err := resolver.OnDirectory(canonicalRepo)
if err != nil {
return PatchedFilePropertiesValue{}, fmt.Errorf("failed to resolve canonical repo directory: %w", err)
}
if err := path.Resolve(
path.UNIXFormat.NewParser(key.Path),
path.NewLoopDetectingScopeWalker(
path.NewRelativeScopeWalker(repoDirectory.(path.GotDirectory).Child),
),
); err != nil {
return PatchedFilePropertiesValue{}, fmt.Errorf("failed to resolve path: %w", err)
}
if !resolver.gotTerminal {
return PatchedFilePropertiesValue{}, errors.New("path resolves to a directory")
}
patchedFileProperties := model_core.NewPatchedMessageFromExistingCaptured(
c.objectCapturer,
resolver.fileProperties,
)
return model_core.NewPatchedMessage(
&model_analysis_pb.FileProperties_Value{
Exists: patchedFileProperties.Message,
},
model_core.MapReferenceMetadataToWalkers(patchedFileProperties.Patcher),
), nil
}