From 0179a0ca5cbd425475448c18fdca69a372020024 Mon Sep 17 00:00:00 2001 From: Clayton Craft Date: Mon, 20 Feb 2023 21:56:21 -0800 Subject: [PATCH] misc: remove functions now in osutils, update references in project --- cmd/mkinitfs/main.go | 3 +- internal/archive/archive.go | 4 +- internal/filelist/modules/modules.go | 3 +- internal/misc/getfiles.go | 4 +- internal/misc/misc.go | 68 ---------------------------- 5 files changed, 9 insertions(+), 73 deletions(-) diff --git a/cmd/mkinitfs/main.go b/cmd/mkinitfs/main.go index 62b4a69..c386b1b 100644 --- a/cmd/mkinitfs/main.go +++ b/cmd/mkinitfs/main.go @@ -22,6 +22,7 @@ import ( "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist/modules" "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist/osksdl" "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc" + "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/osutil" "gitlab.com/postmarketOS/postmarketos-mkinitfs/pkgs/deviceinfo" ) @@ -63,7 +64,7 @@ func main() { defer misc.TimeFunc(time.Now(), "mkinitfs") - kernVer, err := misc.GetKernelVersion() + kernVer, err := osutil.GetKernelVersion() if err != nil { log.Println(err) retCode = 1 diff --git a/internal/archive/archive.go b/internal/archive/archive.go index ef79307..f3f21e2 100644 --- a/internal/archive/archive.go +++ b/internal/archive/archive.go @@ -19,7 +19,7 @@ import ( "github.com/cavaliercoder/go-cpio" "github.com/klauspost/pgzip" "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist" - "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc" + "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/osutil" ) type Archive struct { @@ -191,7 +191,7 @@ func (archive *Archive) addFile(source string, dest string) error { } // make sure target is an absolute path if !filepath.IsAbs(target) { - target, err = misc.RelativeSymlinkTargetToDir(target, filepath.Dir(source)) + target, err = osutil.RelativeSymlinkTargetToDir(target, filepath.Dir(source)) if err != nil { return err } diff --git a/internal/filelist/modules/modules.go b/internal/filelist/modules/modules.go index 9a2e6ce..884c6bd 100644 --- a/internal/filelist/modules/modules.go +++ b/internal/filelist/modules/modules.go @@ -12,6 +12,7 @@ import ( "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/filelist" "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/misc" + "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/osutil" ) type Modules struct { @@ -31,7 +32,7 @@ func New(modulesList []string, modulesListPath string) *Modules { func (m *Modules) List() (*filelist.FileList, error) { log.Println("- Including kernel modules") - kernVer, err := misc.GetKernelVersion() + kernVer, err := osutil.GetKernelVersion() if err != nil { return nil, err } diff --git a/internal/misc/getfiles.go b/internal/misc/getfiles.go index 996cf21..681c078 100644 --- a/internal/misc/getfiles.go +++ b/internal/misc/getfiles.go @@ -5,6 +5,8 @@ import ( "fmt" "os" "path/filepath" + + "gitlab.com/postmarketOS/postmarketos-mkinitfs/internal/osutil" ) func GetFiles(list []string, required bool) (files []string, err error) { @@ -150,7 +152,7 @@ func getBinaryDeps(file string) ([]string, error) { return nil, fmt.Errorf("getBinaryDeps: unable to read symlink %q: %w", file, err) } if !filepath.IsAbs(target) { - target, err = RelativeSymlinkTargetToDir(target, filepath.Dir(file)) + target, err = osutil.RelativeSymlinkTargetToDir(target, filepath.Dir(file)) if err != nil { return nil, err } diff --git a/internal/misc/misc.go b/internal/misc/misc.go index fa7136f..34e9066 100644 --- a/internal/misc/misc.go +++ b/internal/misc/misc.go @@ -4,53 +4,11 @@ package misc import ( - "fmt" "log" "os" - "path/filepath" - "strings" "time" - - "golang.org/x/sys/unix" ) -// Converts a relative symlink target path (e.g. ../../lib/foo.so), that is -// absolute path -func RelativeSymlinkTargetToDir(symPath string, dir string) (string, error) { - var path string - - oldWd, err := os.Getwd() - if err != nil { - log.Print("Unable to get current working dir") - return path, err - } - - if err := os.Chdir(dir); err != nil { - log.Print("Unable to change to working dir: ", dir) - return path, err - } - - path, err = filepath.Abs(symPath) - if err != nil { - log.Print("Unable to resolve abs path to: ", symPath) - return path, err - } - - if err := os.Chdir(oldWd); err != nil { - log.Print("Unable to change to old working dir") - return path, err - } - - return path, nil -} - -func FreeSpace(path string) (uint64, error) { - var stat unix.Statfs_t - unix.Statfs(path, &stat) - size := stat.Bavail * uint64(stat.Bsize) - return size, nil -} - // Merge the contents of "b" into "a", overwriting any previously existing keys // in "a" func Merge(a map[string]string, b map[string]string) { @@ -89,32 +47,6 @@ func TimeFunc(start time.Time, name string) { log.Printf("%s completed in: %s", name, elapsed) } -func getKernelReleaseFile() (string, error) { - files, _ := filepath.Glob("/usr/share/kernel/*/kernel.release") - // only one kernel flavor supported - if len(files) != 1 { - return "", fmt.Errorf("only one kernel release/flavor is supported, found: %q", files) - } - - return files[0], nil -} - -func GetKernelVersion() (string, error) { - var version string - - releaseFile, err := getKernelReleaseFile() - if err != nil { - return version, err - } - - contents, err := os.ReadFile(releaseFile) - if err != nil { - return version, err - } - - return strings.TrimSpace(string(contents)), nil -} - // Exists tests if the given file/dir exists or not func Exists(file string) bool { if _, err := os.Stat(file); err == nil {