Skip to content

Commit 7a78061

Browse files
authored
Bugfix: Overlay accessor failed to wrap LStat output (#4685)
Fixes: #4610
1 parent ed6e91f commit 7a78061

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

accessors/api.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,21 @@ func (self *OSPath) TrimComponents(components ...string) *OSPath {
226226
return result
227227
}
228228

229+
// Does the path has the required prefix?
230+
func (self *OSPath) HasPrefix(components ...string) bool {
231+
if len(self.Components) > len(components) {
232+
return false
233+
}
234+
235+
for idx, c := range components {
236+
if !self.Manipulator.ComponentEqual(c, self.Components[idx]) {
237+
return false
238+
}
239+
}
240+
241+
return true
242+
}
243+
229244
// Produce a human readable string - this is a one way conversion: It
230245
// is not possible to go back to a proper OSPath from this.
231246
func (self *OSPath) HumanString(scope types.Scope) string {
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"/file1.txt": "Hello",
3-
"/file2.txt": "Hello Two",
4-
"/subdir/file2.txt": "Hello Subdir"
2+
"file1.txt": "Hello",
3+
"file1.txt Stat": "file1.txt",
4+
"file2.txt": "Hello Two",
5+
"file2.txt Stat": "file2.txt",
6+
"subdir:file2.txt": "Hello Subdir",
7+
"subdir:file2.txt Stat": "subdir:file2.txt"
58
}

accessors/overlay/overlay.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
type OverlayFileSystemAccessorArgs struct {
19-
Paths []*accessors.OSPath `vfilter:"required,field=paths,doc=A list of paths to try to resolve each path."`
19+
Paths []*accessors.OSPath `vfilter:"required,field=paths,doc=A list of paths to try to resolve."`
2020
Accessor string `vfilter:"optional,field=accessor,doc=File accessor"`
2121
}
2222

@@ -74,17 +74,19 @@ func (self OverlayFileSystemAccessor) ReadDirWithOSPath(
7474
return nil, err
7575
}
7676

77-
base, _ := accessors.NewGenericOSPath("/")
78-
7977
for _, basepath := range overlayer.Paths {
8078
delegate_path := basepath.Append(path.Components...)
8179
delegate_dir, err := accessor.ReadDirWithOSPath(delegate_path)
8280
if err != nil {
8381
continue
8482
}
83+
84+
base := basepath.TrimComponents(basepath.Components...)
85+
8586
for _, fsinfo := range delegate_dir {
86-
res = append(res, accessors.NewFileInfoWrapper(
87-
fsinfo, base, basepath.Copy()))
87+
item := accessors.NewFileInfoWrapper(
88+
fsinfo, base, basepath.Copy())
89+
res = append(res, item)
8890
}
8991
}
9092

@@ -158,6 +160,8 @@ func (self OverlayFileSystemAccessor) LstatWithOSPath(
158160
basepath.Append(path.Components...))
159161
// Return the first successful opened file
160162
if res_err == nil {
163+
base := basepath.TrimComponents(basepath.Components...)
164+
res = accessors.NewFileInfoWrapper(res, base, basepath.Copy())
161165
break
162166
}
163167
}

accessors/overlay/overlay_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ func (self *OverlayAccessorTestSuite) TestOverlay() {
9090
assert.NoError(self.T(), err)
9191
fd.Close()
9292

93-
golden.Set(f.OSPath().String(), string(data))
93+
components := strings.Join(f.OSPath().Components, ":")
94+
95+
golden.Set(components, string(data))
96+
97+
stat, err := accessor.LstatWithOSPath(f.OSPath())
98+
assert.NoError(self.T(), err)
99+
100+
golden.Set(components+" Stat",
101+
strings.Join(stat.OSPath().Components, ":"))
94102
}
95103
}
96104
check_dir("/")

0 commit comments

Comments
 (0)