filelist/*: implement FileLister in existing types

This commit is contained in:
Clayton Craft
2023-02-18 11:17:18 -08:00
parent e5002f5750
commit 6c2f7b972b
4 changed files with 51 additions and 25 deletions

View File

@@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
)
@@ -22,13 +23,13 @@ func New(filePath string) *HookFiles {
}
}
func (h *HookFiles) List() ([]string, error) {
func (h *HookFiles) List() (*filelist.FileList, error) {
log.Println("- Including files")
fileInfo, err := os.ReadDir(h.filePath)
if err != nil {
return nil, fmt.Errorf("getHookFiles: unable to read hook file dir: %w", err)
}
files := []string{}
files := filelist.NewFileList()
for _, file := range fileInfo {
path := filepath.Join(h.filePath, file.Name())
f, err := os.Open(path)
@@ -40,10 +41,12 @@ func (h *HookFiles) List() ([]string, error) {
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 {
if fFiles, 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...)
for _, file := range fFiles {
files.Add(file, file)
}
}
}
if err := s.Err(); err != nil {

View File

@@ -5,6 +5,8 @@ import (
"log"
"os"
"path/filepath"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist"
)
type HookScripts struct {
@@ -19,8 +21,8 @@ func New(scriptsDir string) *HookScripts {
}
}
func (h *HookScripts) List() ([]string, error) {
files := []string{}
func (h *HookScripts) List() (*filelist.FileList, error) {
files := filelist.NewFileList()
log.Println("- Including hook scripts")
fileInfo, err := os.ReadDir(h.scriptsDir)
@@ -29,7 +31,7 @@ func (h *HookScripts) List() ([]string, error) {
}
for _, file := range fileInfo {
path := filepath.Join(h.scriptsDir, file.Name())
files = append(files, path)
files.Add(path, path)
}
return files, nil
}

View File

@@ -10,6 +10,7 @@ import (
"regexp"
"strings"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
)
@@ -25,7 +26,7 @@ func New(modules []string) *Modules {
}
}
func (m *Modules) List() ([]string, error) {
func (m *Modules) List() (*filelist.FileList, error) {
log.Println("- Including kernel modules")
kernVer, err := misc.GetKernelVersion()
@@ -33,7 +34,7 @@ func (m *Modules) List() ([]string, error) {
return nil, err
}
files := []string{}
files := filelist.NewFileList()
modDir := filepath.Join("/lib/modules", kernVer)
if !misc.Exists(modDir) {
@@ -44,14 +45,18 @@ func (m *Modules) List() ([]string, error) {
// modules.* required by modprobe
modprobeFiles, _ := filepath.Glob(filepath.Join(modDir, "modules.*"))
files = append(files, modprobeFiles...)
for _, file := range modprobeFiles {
files.Add(file, file)
}
// deviceinfo modules
for _, module := range m.modules {
if filelist, err := getModule(module, modDir); err != nil {
if modFilelist, err := getModule(module, modDir); err != nil {
return nil, fmt.Errorf("getInitfsModules: unable to get modules from deviceinfo: %w", err)
} else {
files = append(files, filelist...)
for _, file := range modFilelist {
files.Add(file, file)
}
}
}
@@ -65,10 +70,12 @@ func (m *Modules) List() ([]string, error) {
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
if filelist, err := getModule(s.Text(), modDir); err != nil {
if modFilelist, err := getModule(s.Text(), modDir); err != nil {
return nil, fmt.Errorf("getInitfsModules: unable to get module file %q: %w", s.Text(), err)
} else {
files = append(files, filelist...)
for _, file := range modFilelist {
files.Add(file, file)
}
}
}
}

View File

@@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist"
"gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc"
)
@@ -25,12 +26,14 @@ func New(mesaDriverName string) *OskSdl {
// Get a list of files and their dependencies related to supporting rootfs full
// disk (d)encryption
func (s *OskSdl) List() ([]string, error) {
func (s *OskSdl) List() (*filelist.FileList, error) {
if !misc.Exists("/usr/bin/osk-sdl") {
return nil, nil
}
log.Println("- Including osk-sdl support")
files := filelist.NewFileList()
confFiles := []string{
"/etc/osk.conf",
"/etc/ts.conf",
@@ -38,10 +41,13 @@ func (s *OskSdl) List() ([]string, error) {
"/etc/fb.modes",
"/etc/directfbrc",
}
files, err := misc.GetFiles(confFiles, false)
confFileList, err := misc.GetFiles(confFiles, false)
if err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
}
for _, file := range confFileList {
files.Add(file, file)
}
// osk-sdl
oskFiles := []string{
@@ -49,17 +55,19 @@ func (s *OskSdl) List() ([]string, error) {
"/sbin/cryptsetup",
"/usr/lib/libGL.so.1",
}
if filelist, err := misc.GetFiles(oskFiles, true); err != nil {
if oskFileList, err := misc.GetFiles(oskFiles, true); err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
} else {
files = append(files, filelist...)
for _, file := range oskFileList {
files.Add(file, file)
}
}
fontFile, err := getOskConfFontPath("/etc/osk.conf")
if err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add file %q: %w", fontFile, err)
}
files = append(files, fontFile)
files.Add(fontFile, fontFile)
// Directfb
dfbFiles := []string{}
@@ -72,10 +80,12 @@ func (s *OskSdl) List() ([]string, error) {
if err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add file %w", err)
}
if filelist, err := misc.GetFiles(dfbFiles, true); err != nil {
if dfbFileList, err := misc.GetFiles(dfbFiles, true); err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
} else {
files = append(files, filelist...)
for _, file := range dfbFileList {
files.Add(file, file)
}
}
// tslib
@@ -91,10 +101,12 @@ func (s *OskSdl) List() ([]string, error) {
}
libts, _ := filepath.Glob("/usr/lib/libts*")
tslibFiles = append(tslibFiles, libts...)
if filelist, err := misc.GetFiles(tslibFiles, true); err != nil {
if tslibFileList, err := misc.GetFiles(tslibFiles, true); err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
} else {
files = append(files, filelist...)
for _, file := range tslibFileList {
files.Add(file, file)
}
}
// mesa hw accel
@@ -106,10 +118,12 @@ func (s *OskSdl) List() ([]string, error) {
"/usr/lib/libudev.so.1",
"/usr/lib/xorg/modules/dri/" + s.mesaDriver + "_dri.so",
}
if filelist, err := misc.GetFiles(mesaFiles, true); err != nil {
if mesaFileList, err := misc.GetFiles(mesaFiles, true); err != nil {
return nil, fmt.Errorf("getFdeFiles: failed to add files: %w", err)
} else {
files = append(files, filelist...)
for _, file := range mesaFileList {
files.Add(file, file)
}
}
}