Skip to content

refactor: improve overlayFS errors #17808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: improve overlayFS errors
  • Loading branch information
aslilac committed May 13, 2025
commit a1f2c0de5e89d7f95ccbc7884e4b0f8eeb66c3de
63 changes: 24 additions & 39 deletions coderd/files/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"golang.org/x/xerrors"
)

// overlayFS allows you to "join" together the template files tar file fs.FS
// with the Terraform modules tar file fs.FS. We could potentially turn this
// into something more parameterized/configurable, but the requirements here are
// a _bit_ odd, because every file in the modulesFS includes the
// .terraform/modules/ folder at the beginning of it's path.
// overlayFS allows you to "join" together multiple fs.FS. Files in any specific
// overlay will only be accessible if their path starts with the base path
// provided for the overlay. eg. An overlay at the path .terraform/modules
// should contain files with paths inside the .terraform/modules folder.
type overlayFS struct {
baseFS fs.FS
overlays []Overlay
Expand All @@ -23,64 +22,50 @@ type Overlay struct {
fs.FS
}

func NewOverlayFS(baseFS fs.FS, overlays []Overlay) (fs.FS, error) {
if err := valid(baseFS); err != nil {
return nil, xerrors.Errorf("baseFS: %w", err)
}

for _, overlay := range overlays {
if err := valid(overlay.FS); err != nil {
return nil, xerrors.Errorf("overlayFS: %w", err)
}
}

func NewOverlayFS(baseFS fs.FS, overlays []Overlay) fs.FS {
return overlayFS{
baseFS: baseFS,
overlays: overlays,
}, nil
}
}

func (f overlayFS) Open(p string) (fs.File, error) {
target := f.baseFS
for _, overlay := range f.overlays {
if strings.HasPrefix(path.Clean(p), overlay.Path) {
return overlay.FS.Open(p)
target = overlay.FS
break
}
}
return f.baseFS.Open(p)
return target.Open(p)
}

func (f overlayFS) ReadDir(p string) ([]fs.DirEntry, error) {
target := f.baseFS
for _, overlay := range f.overlays {
if strings.HasPrefix(path.Clean(p), overlay.Path) {
//nolint:forcetypeassert
return overlay.FS.(fs.ReadDirFS).ReadDir(p)
target = overlay.FS
break
}
}
//nolint:forcetypeassert
return f.baseFS.(fs.ReadDirFS).ReadDir(p)
it, ok := target.(fs.ReadDirFS)
if !ok {
return nil, xerrors.New("provided fs.FS does not implement fs.ReadDirFS")
}
return it.ReadDir(p)
}

func (f overlayFS) ReadFile(p string) ([]byte, error) {
target := f.baseFS
for _, overlay := range f.overlays {
if strings.HasPrefix(path.Clean(p), overlay.Path) {
//nolint:forcetypeassert
return overlay.FS.(fs.ReadFileFS).ReadFile(p)
target = overlay.FS
break
}
}
//nolint:forcetypeassert
return f.baseFS.(fs.ReadFileFS).ReadFile(p)
}

// valid checks that the fs.FS implements the required interfaces.
// The fs.FS interface is not sufficient.
func valid(fsys fs.FS) error {
_, ok := fsys.(fs.ReadDirFS)
if !ok {
return xerrors.New("overlayFS does not implement ReadDirFS")
}
_, ok = fsys.(fs.ReadFileFS)
it, ok := target.(fs.ReadFileFS)
if !ok {
return xerrors.New("overlayFS does not implement ReadFileFS")
return nil, xerrors.New("provided fs.FS does not implement fs.ReadFileFS")
}
return nil
return it.ReadFile(p)
}
Loading