filelist/initramfs: cache list of files (MR 34)

Allows subsequent calls to List() to return quickly
This commit is contained in:
Clayton Craft
2023-03-19 22:21:02 -07:00
parent 8fac3004a6
commit 67ce1a9c2e

View File

@@ -9,6 +9,7 @@ import (
// combining the output from them. // combining the output from them.
type Initramfs struct { type Initramfs struct {
features []filelist.FileLister features []filelist.FileLister
files *filelist.FileList
} }
// New returns a new Initramfs that generate a list of files based on the given // New returns a new Initramfs that generate a list of files based on the given
@@ -20,15 +21,18 @@ func New(features []filelist.FileLister) *Initramfs {
} }
func (i *Initramfs) List() (*filelist.FileList, error) { func (i *Initramfs) List() (*filelist.FileList, error) {
files := filelist.NewFileList() if i.files != nil {
return i.files, nil
}
i.files = filelist.NewFileList()
for _, f := range i.features { for _, f := range i.features {
list, err := f.List() list, err := f.List()
if err != nil { if err != nil {
return nil, err return nil, err
} }
files.Import(list) i.files.Import(list)
} }
return files, nil return i.files, nil
} }