Add 'GetPathOwners' function

This commit is contained in:
2026-03-24 18:09:54 +02:00
parent 1d81690bff
commit 248359c02f
2 changed files with 45 additions and 45 deletions
+9 -45
View File
@@ -152,7 +152,7 @@ func main() {
currentFlagSet.StringP("root", "R", "/", "Operate on specified root directory") currentFlagSet.StringP("root", "R", "/", "Operate on specified root directory")
setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("bpm %s <options>", subcommand), "Show what packages own the specified paths", os.Args[2:]) setupFlagsAndHelp(currentFlagSet, fmt.Sprintf("bpm %s <options>", subcommand), "Show what packages own the specified paths", os.Args[2:])
getFileOwner() getPathOwners()
case "c", "compile": case "c", "compile":
// Setup flags and help // Setup flags and help
currentFlagSet = flag.NewFlagSet("compile", flag.ExitOnError) currentFlagSet = flag.NewFlagSet("compile", flag.ExitOnError)
@@ -1134,7 +1134,7 @@ func updatePackages() {
} }
} }
func getFileOwner() { func getPathOwners() {
// Get flags // Get flags
rootDir, _ := currentFlagSet.GetString("root") rootDir, _ := currentFlagSet.GetString("root")
@@ -1157,7 +1157,7 @@ func getFileOwner() {
// Ensure file exists // Ensure file exists
stat, err := os.Lstat(path) stat, err := os.Lstat(path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
log.Printf("Error: file (%s) does not exist!\n", path) log.Printf("Error: %s", err)
exitCode = 1 exitCode = 1
return return
} }
@@ -1170,57 +1170,21 @@ func getFileOwner() {
pathType = "Symlink" pathType = "Symlink"
} }
// Get absolte path to path pathOwners, err := bpmlib.GetPathOwners(path, rootDir)
absPath, err := filepath.Abs(path)
if err != nil { if err != nil {
log.Printf("Error: could not get absolute path of file (%s)\n", path) log.Printf("Error: %s", err)
exitCode = 1 exitCode = 1
return return
} }
// Get path relative to rootDir
if !strings.HasPrefix(absPath, rootDir) {
log.Printf("Error: could not get path of file (%s) relative to root path", absPath)
exitCode = 1
return
}
absPath, err = filepath.Rel(rootDir, absPath)
if err != nil {
log.Printf("Error: could not get path of file (%s) relative to root path", absPath)
exitCode = 1
return
}
// Trim leading and trailing slashes
absPath = strings.TrimLeft(absPath, "/")
absPath = strings.TrimRight(absPath, "/")
// Get installed packages
pkgs, err := bpmlib.GetInstalledPackages(rootDir)
if err != nil {
log.Printf("Error: could not get installed packages: %s\n", err.Error())
exitCode = 1
return
}
// Add packages that own path to list
var pkgList []string
for _, pkg := range pkgs {
if slices.ContainsFunc(bpmlib.GetPackage(pkg, rootDir).PkgFiles, func(entry *bpmlib.PackageFileEntry) bool {
return entry.Path == absPath
}) {
pkgList = append(pkgList, pkg)
}
}
// Print packages // Print packages
if len(pkgList) == 0 { if len(pathOwners) == 0 {
fmt.Printf("%s (%s) is not owned by any packages!\n", absPath, pathType) fmt.Printf("%s (%s) is not owned by any packages!\n", path, pathType)
exitCode = 1 exitCode = 1
return return
} else { } else {
fmt.Printf("%s (%s) is owned by the following packages:\n", absPath, pathType) fmt.Printf("%s (%s) is owned by the following packages:\n", path, pathType)
for _, pkg := range pkgList { for _, pkg := range pathOwners {
fmt.Println("- " + pkg) fmt.Println("- " + pkg)
} }
} }
+36
View File
@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath"
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
@@ -103,6 +104,41 @@ func GetInstalledPackages(rootDir string) (ret []string, err error) {
return ret, nil return ret, nil
} }
func GetPathOwners(path, rootDir string) (ret []string, err error) {
// Get absolte path to path
path, err = filepath.Abs(path)
if err != nil {
return
}
path, err = filepath.Rel(rootDir, path)
if err != nil {
return
}
// Trim leading and trailing slashes
path = strings.TrimLeft(path, "/")
path = strings.TrimRight(path, "/")
// Get installed packages
pkgs, err := GetInstalledPackages(rootDir)
if err != nil {
return
}
// Add packages that own path to list
for _, pkg := range pkgs {
pkgFiles := getPackageFiles(pkg, rootDir)
if slices.ContainsFunc(pkgFiles, func(entry *PackageFileEntry) bool {
return entry.Path == path
}) {
ret = append(ret, pkg)
}
}
return ret, nil
}
func IsPackageInstalled(pkg, rootDir string) bool { func IsPackageInstalled(pkg, rootDir string) bool {
// Initialize local package information // Initialize local package information
err := InitializeLocalPackageInformation(rootDir) err := InitializeLocalPackageInformation(rootDir)