From b25c9bd390ec2bf6db0722c2eb9f8e49da9054a8 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Fri, 17 Feb 2023 13:25:45 -0800 Subject: [PATCH] filelist/hookfiles: add new implementation --- internal/filelist/hookfiles/hookfiles.go | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 internal/filelist/hookfiles/hookfiles.go diff --git a/internal/filelist/hookfiles/hookfiles.go b/internal/filelist/hookfiles/hookfiles.go new file mode 100644 index 0000000..8e06959 --- /dev/null +++ b/internal/filelist/hookfiles/hookfiles.go @@ -0,0 +1,53 @@ +package hookfiles + +import ( + "bufio" + "fmt" + "log" + "os" + "path/filepath" + + "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc" +) + +type HookFiles struct { + filePath string +} + +// New returns a new HookFiles that will use the given path to provide a list +// of files + any binary dependencies they might have. +func New(filePath string) *HookFiles { + return &HookFiles{ + filePath: filePath, + } +} + +func (h *HookFiles) List() ([]string, error) { + fileInfo, err := os.ReadDir(h.filePath) + if err != nil { + return nil, fmt.Errorf("getHookFiles: unable to read hook file dir: %w", err) + } + files := []string{} + for _, file := range fileInfo { + path := filepath.Join(h.filePath, file.Name()) + f, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("getHookFiles: unable to open hook file: %w", err) + + } + defer f.Close() + log.Printf("-- Including files from: %s\n", path) + s := bufio.NewScanner(f) + for s.Scan() { + if filelist, err := misc.GetFiles([]string{s.Text()}, true); err != nil { + return nil, fmt.Errorf("getHookFiles: unable to add file %q required by %q: %w", s.Text(), path, err) + } else { + files = append(files, filelist...) + } + } + if err := s.Err(); err != nil { + return nil, fmt.Errorf("getHookFiles: uname to process hook file %q: %w", path, err) + } + } + return files, nil +}