rpicamera: check libcamera architecture by using the ELF header (#1940) (#1997)

This commit is contained in:
Alessandro Ros 2023-06-28 16:46:32 +02:00 committed by GitHub
parent 059ca76fad
commit 418f4a90b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@
package rpicamera
import (
"debug/elf"
_ "embed"
"encoding/base64"
"fmt"
@ -92,9 +93,6 @@ func findLibrary(name string) (string, error) {
for _, line := range strings.Split(string(byts), "\n") {
f := strings.Split(line, " => ")
if len(f) == 2 && strings.Contains(f[1], name+".so") {
if runtime.GOARCH == "arm" && !strings.Contains(f[1], "arm-linux-gnueabihf") {
return "", fmt.Errorf("libcamera is 64-bit, you need the 64-bit server version")
}
return f[1], nil
}
}
@ -103,12 +101,39 @@ func findLibrary(name string) (string, error) {
return "", fmt.Errorf("library '%s' not found", name)
}
func check64bit(fpath string) error {
f, err := os.Open(fpath)
if err != nil {
return err
}
defer f.Close()
ef, err := elf.NewFile(f)
if err != nil {
return err
}
defer ef.Close()
if ef.FileHeader.Class == elf.ELFCLASS64 {
return fmt.Errorf("libcamera is 64-bit, you need the 64-bit server version")
}
return nil
}
func setupSymlink(name string) error {
lib, err := findLibrary(name)
if err != nil {
return err
}
if runtime.GOARCH == "arm" {
err := check64bit(lib)
if err != nil {
return err
}
}
os.Remove("/dev/shm/" + name + ".so.x.x.x")
return os.Symlink(lib, "/dev/shm/"+name+".so.x.x.x")
}