125 lines
2.7 KiB
Go
125 lines
2.7 KiB
Go
// Copyright 2022 Clayton Craft <clayton@craftyguy.net>
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
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) {
|
|
for k, v := range b {
|
|
a[k] = v
|
|
}
|
|
}
|
|
|
|
// Removes duplicate entries from the given string slice and returns a slice
|
|
// with the unique values
|
|
func RemoveDuplicates(in []string) (out []string) {
|
|
// use a map to "remove" duplicates. the value in the map is totally
|
|
// irrelevant
|
|
outMap := make(map[string]bool)
|
|
for _, s := range in {
|
|
if ok := outMap[s]; !ok {
|
|
outMap[s] = true
|
|
}
|
|
}
|
|
|
|
out = make([]string, 0, len(outMap))
|
|
for k := range outMap {
|
|
out = append(out, k)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Prints the execution time of a function, not meant to be very
|
|
// sensitive/accurate, but good enough to gauge rough run times.
|
|
// Meant to be called as:
|
|
//
|
|
// defer misc.TimeFunc(time.Now(), "foo")
|
|
func TimeFunc(start time.Time, name string) {
|
|
elapsed := time.Since(start)
|
|
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 {
|
|
return true
|
|
}
|
|
return false
|
|
}
|