Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
731a805a9e | ||
|
b90624d7dd | ||
|
2a75cf9b4e | ||
|
d52cc16c88 | ||
|
112b572dc2 | ||
|
0c0a85f3bb |
139
main.go
139
main.go
@@ -165,7 +165,7 @@ func getHookFiles(filesdir string) (files []string, err error) {
|
|||||||
s := bufio.NewScanner(f)
|
s := bufio.NewScanner(f)
|
||||||
for s.Scan() {
|
for s.Scan() {
|
||||||
if filelist, err := getFiles([]string{s.Text()}, true); err != nil {
|
if filelist, err := getFiles([]string{s.Text()}, true); err != nil {
|
||||||
return nil, fmt.Errorf("getHookFiles: unable to find file %q required by %q", s.Text(), path)
|
return nil, fmt.Errorf("getHookFiles: unable to add file %q required by %q: %w", s.Text(), path, err)
|
||||||
} else {
|
} else {
|
||||||
files = append(files, filelist...)
|
files = append(files, filelist...)
|
||||||
}
|
}
|
||||||
@@ -177,8 +177,62 @@ func getHookFiles(filesdir string) (files []string, err error) {
|
|||||||
return files, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getDeps(file string, parents map[string]struct{}) (files []string, err error) {
|
||||||
|
|
||||||
|
if _, found := parents[file]; found {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get dependencies for binaries
|
||||||
|
fd, err := elf.Open(file)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("getDeps: unable to open elf binary %q: %w", file, err)
|
||||||
|
}
|
||||||
|
libs, _ := fd.ImportedLibraries()
|
||||||
|
fd.Close()
|
||||||
|
files = append(files, file)
|
||||||
|
parents[file] = struct{}{}
|
||||||
|
|
||||||
|
if len(libs) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// we don't recursively search these paths for performance reasons
|
||||||
|
libdirGlobs := []string{
|
||||||
|
"/usr/lib",
|
||||||
|
"/lib",
|
||||||
|
"/usr/lib/expect*",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lib := range libs {
|
||||||
|
found := false
|
||||||
|
findDepLoop:
|
||||||
|
for _, libdirGlob := range libdirGlobs {
|
||||||
|
libdirs, _ := filepath.Glob(libdirGlob)
|
||||||
|
for _, libdir := range libdirs {
|
||||||
|
path := filepath.Join(libdir, lib)
|
||||||
|
if _, err := os.Stat(path); err == nil {
|
||||||
|
binaryDepFiles, err := getDeps(path, parents)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
files = append(files, binaryDepFiles...)
|
||||||
|
files = append(files, path)
|
||||||
|
found = true
|
||||||
|
break findDepLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return nil, fmt.Errorf("getDeps: unable to locate dependency for %q: %s", file, lib)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Recursively list all dependencies for a given ELF binary
|
// Recursively list all dependencies for a given ELF binary
|
||||||
func getBinaryDeps(file string) (files []string, err error) {
|
func getBinaryDeps(file string) ([]string, error) {
|
||||||
// if file is a symlink, resolve dependencies for target
|
// if file is a symlink, resolve dependencies for target
|
||||||
fileStat, err := os.Lstat(file)
|
fileStat, err := os.Lstat(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -194,52 +248,14 @@ func getBinaryDeps(file string) (files []string, err error) {
|
|||||||
if !filepath.IsAbs(target) {
|
if !filepath.IsAbs(target) {
|
||||||
target, err = misc.RelativeSymlinkTargetToDir(target, filepath.Dir(file))
|
target, err = misc.RelativeSymlinkTargetToDir(target, filepath.Dir(file))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return files, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
binaryDepFiles, err := getBinaryDeps(target)
|
file = target
|
||||||
if err != nil {
|
|
||||||
return files, err
|
|
||||||
}
|
|
||||||
files = append(files, binaryDepFiles...)
|
|
||||||
return files, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get dependencies for binaries
|
return getDeps(file, make(map[string]struct{}))
|
||||||
fd, err := elf.Open(file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("getBinaryDeps: unable to open elf binary %q: %w", file, err)
|
|
||||||
}
|
|
||||||
libs, _ := fd.ImportedLibraries()
|
|
||||||
fd.Close()
|
|
||||||
files = append(files, file)
|
|
||||||
|
|
||||||
if len(libs) == 0 {
|
|
||||||
return files, err
|
|
||||||
}
|
|
||||||
|
|
||||||
libdirs := []string{"/usr/lib", "/lib"}
|
|
||||||
for _, lib := range libs {
|
|
||||||
found := false
|
|
||||||
for _, libdir := range libdirs {
|
|
||||||
path := filepath.Join(libdir, lib)
|
|
||||||
if _, err := os.Stat(path); err == nil {
|
|
||||||
binaryDepFiles, err := getBinaryDeps(path)
|
|
||||||
if err != nil {
|
|
||||||
return files, err
|
|
||||||
}
|
|
||||||
files = append(files, binaryDepFiles...)
|
|
||||||
files = append(files, path)
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return nil, fmt.Errorf("getBinaryDeps: unable to locate dependency for %q: %s", file, lib)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFiles(list []string, required bool) (files []string, err error) {
|
func getFiles(list []string, required bool) (files []string, err error) {
|
||||||
@@ -275,7 +291,7 @@ func getFile(file string, required bool) (files []string, err error) {
|
|||||||
fileInfo, err := os.Stat(file)
|
fileInfo, err := os.Stat(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if required {
|
if required {
|
||||||
return files, errors.New("getFile: File does not exist :" + file)
|
return files, fmt.Errorf("getFile: failed to stat file %q: %w", file, err)
|
||||||
}
|
}
|
||||||
return files, nil
|
return files, nil
|
||||||
}
|
}
|
||||||
@@ -426,11 +442,16 @@ func getFdeFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getHookScripts() (files []string) {
|
func getHookScripts(scriptsdir string) (files []string, err error) {
|
||||||
scripts, _ := filepath.Glob("/etc/postmarketos-mkinitfs/hooks/*.sh")
|
fileInfo, err := os.ReadDir(scriptsdir)
|
||||||
files = append(files, scripts...)
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("getHookScripts: unable to read hook script dir: %w", err)
|
||||||
return
|
}
|
||||||
|
for _, file := range fileInfo {
|
||||||
|
path := filepath.Join(scriptsdir, file.Name())
|
||||||
|
files = append(files, path)
|
||||||
|
}
|
||||||
|
return files, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getInitfsExtraFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
|
func getInitfsExtraFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
|
||||||
@@ -466,6 +487,15 @@ func getInitfsExtraFiles(devinfo deviceinfo.DeviceInfo) (files []string, err err
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if exists("/etc/postmarketos-mkinitfs/hooks-extra") {
|
||||||
|
log.Println("- Including extra hook scripts")
|
||||||
|
if hookScripts, err := getHookScripts("/etc/postmarketos-mkinitfs/hooks-extra"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
files = append(files, hookScripts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if exists("/usr/bin/osk-sdl") {
|
if exists("/usr/bin/osk-sdl") {
|
||||||
log.Println("- Including FDE support")
|
log.Println("- Including FDE support")
|
||||||
if fdeFiles, err := getFdeFiles(devinfo); err != nil {
|
if fdeFiles, err := getFdeFiles(devinfo); err != nil {
|
||||||
@@ -506,9 +536,14 @@ func getInitfsFiles(devinfo deviceinfo.DeviceInfo) (files []string, err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("- Including hook scripts")
|
if exists("/etc/postmarketos-mkinitfs/hooks") {
|
||||||
hookScripts := getHookScripts()
|
log.Println("- Including hook scripts")
|
||||||
files = append(files, hookScripts...)
|
if hookScripts, err := getHookScripts("/etc/postmarketos-mkinitfs/hooks"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
files = append(files, hookScripts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Println("- Including required binaries")
|
log.Println("- Including required binaries")
|
||||||
if filelist, err := getFiles(requiredFiles, true); err != nil {
|
if filelist, err := getFiles(requiredFiles, true); err != nil {
|
||||||
|
Reference in New Issue
Block a user