Added BPM operation structs which make handling package installation/removal easier and fixed multiple bugs

This commit is contained in:
2024-10-15 10:03:06 +03:00
parent 1bd57110a0
commit 7816d0072c
4 changed files with 261 additions and 110 deletions
+96
View File
@@ -0,0 +1,96 @@
package utils
type BPMOperation struct {
Actions []OperationAction
}
func (operation *BPMOperation) ActionsContainPackage(pkg string) bool {
for _, action := range operation.Actions {
if action.GetActionType() == "install" {
return action.(*InstallPackageAction).BpmPackage.PkgInfo.Name == pkg
} else if action.GetActionType() == "fetch" {
return action.(*FetchPackageAction).RepositoryEntry.Info.Name == pkg
} else if action.GetActionType() == "remove" {
return action.(*RemovePackageAction).BpmPackage.PkgInfo.Name == pkg
}
}
return false
}
func (operation *BPMOperation) InsertActionAt(index int, action OperationAction) {
if len(operation.Actions) == index { // nil or empty slice or after last element
operation.Actions = append(operation.Actions, action)
}
operation.Actions = append(operation.Actions[:index+1], operation.Actions[index:]...) // index < len(a)
operation.Actions[index] = action
}
func (operation *BPMOperation) GetTotalDownloadSize() uint64 {
var ret uint64 = 0
for _, action := range operation.Actions {
if action.GetActionType() == "fetch" {
ret += action.(*FetchPackageAction).RepositoryEntry.DownloadSize
}
}
return ret
}
func (operation *BPMOperation) GetTotalInstalledSize() uint64 {
var ret uint64 = 0
for _, action := range operation.Actions {
if action.GetActionType() == "install" {
ret += action.(*InstallPackageAction).BpmPackage.GetInstalledSize()
} else if action.GetActionType() == "fetch" {
ret += action.(*FetchPackageAction).RepositoryEntry.InstalledSize
}
}
return ret
}
func (operation *BPMOperation) GetFinalActionSize(rootDir string) uint64 {
var ret uint64 = 0
for _, action := range operation.Actions {
if action.GetActionType() == "install" {
ret += action.(*InstallPackageAction).BpmPackage.GetInstalledSize()
if IsPackageInstalled(action.(*InstallPackageAction).BpmPackage.PkgInfo.Name, rootDir) {
ret -= GetPackage(action.(*InstallPackageAction).BpmPackage.PkgInfo.Name, rootDir).GetInstalledSize()
}
} else if action.GetActionType() == "fetch" {
ret += action.(*FetchPackageAction).RepositoryEntry.InstalledSize
} else if action.GetActionType() == "remove" {
ret -= action.(*RemovePackageAction).BpmPackage.GetInstalledSize()
}
}
return ret
}
type OperationAction interface {
GetActionType() string
}
type InstallPackageAction struct {
File string
IsDependency bool
BpmPackage *BPMPackage
}
func (action *InstallPackageAction) GetActionType() string {
return "install"
}
type FetchPackageAction struct {
IsDependency bool
RepositoryEntry *RepositoryEntry
}
func (action *FetchPackageAction) GetActionType() string {
return "fetch"
}
type RemovePackageAction struct {
BpmPackage *BPMPackage
}
func (action *RemovePackageAction) GetActionType() string {
return "remove"
}
+68 -29
View File
@@ -2,7 +2,6 @@ package utils
import (
"archive/tar"
"bufio"
"bytes"
"compress/gzip"
"errors"
@@ -180,23 +179,23 @@ func ReadPackage(filename string) (*BPMPackage, error) {
continue
}
stringEntry := strings.Split(strings.TrimSpace(line), " ")
if len(stringEntry) != 4 {
if len(stringEntry) < 4 {
return nil, errors.New("pkg.files is not formatted correctly")
}
uid, err := strconv.ParseInt(stringEntry[1], 0, 32)
uid, err := strconv.ParseInt(stringEntry[len(stringEntry)-3], 0, 32)
if err != nil {
return nil, err
}
gid, err := strconv.ParseInt(stringEntry[2], 0, 32)
gid, err := strconv.ParseInt(stringEntry[len(stringEntry)-2], 0, 32)
if err != nil {
return nil, err
}
size, err := strconv.ParseUint(stringEntry[3], 0, 64)
size, err := strconv.ParseUint(stringEntry[len(stringEntry)-1], 0, 64)
if err != nil {
return nil, err
}
pkgFiles = append(pkgFiles, &PackageFileEntry{
Path: stringEntry[0],
Path: strings.Join(stringEntry[:len(stringEntry)-3], " "),
UserID: int(uid),
GroupID: int(gid),
SizeInBytes: size,
@@ -501,8 +500,7 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string)
if err != nil {
return err, nil
}
trimmedName := strings.TrimPrefix(header.Name, "./")
extractFilename := path.Join(rootDir, trimmedName)
extractFilename := path.Join(rootDir, header.Name)
switch header.Typeflag {
case tar.TypeDir:
files = append(files, strings.TrimPrefix(header.Name, "./"))
@@ -520,7 +518,7 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string)
if _, err := os.Stat(extractFilename); err == nil {
for _, k := range bpmpkg.PkgInfo.Keep {
if strings.HasSuffix(k, "/") {
if strings.HasPrefix(trimmedName, k) {
if strings.HasPrefix(header.Name, k) {
if verbose {
fmt.Println("Skipping File: " + extractFilename + " (Containing directory is set to be kept during installs/updates)")
}
@@ -529,7 +527,7 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string)
continue
}
} else {
if trimmedName == k {
if header.Name == k {
if verbose {
fmt.Println("Skipping File: " + extractFilename + " (File is configured to be kept during installs/updates)")
}
@@ -1061,15 +1059,15 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc,
}
packageInstalled := IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir)
if packageInstalled {
oldFiles = GetPackageFiles(bpmpkg.PkgInfo.Name, rootDir)
fileEntries := GetPackageFiles(bpmpkg.PkgInfo.Name, rootDir)
for _, entry := range fileEntries {
files = append(files, entry.Path)
}
}
if !force {
if bpmpkg.PkgInfo.Arch != "any" && bpmpkg.PkgInfo.Arch != GetArch() {
return errors.New("cannot install a package with a different architecture")
}
if unresolved := bpmpkg.PkgInfo.CheckDependencies(bpmpkg.PkgInfo.Type == "source", true, rootDir); len(unresolved) != 0 {
return errors.New("the following dependencies are not installed: " + strings.Join(unresolved, ", "))
}
}
if bpmpkg.PkgInfo.Type == "binary" {
err, i := extractPackage(bpmpkg, verbose, filename, rootDir)
@@ -1115,12 +1113,11 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc,
if err != nil {
return err
}
for _, line := range files {
_, err := f.WriteString(line + "\n")
if err != nil {
return err
}
bs, err := ReadTarballContent(filename, "pkg.files")
if err != nil {
return err
}
_, err = f.Write(bs)
err = f.Close()
if err != nil {
return err
@@ -1375,8 +1372,8 @@ func GetInstalledPackages(rootDir string) ([]string, error) {
return ret, nil
}
func GetPackageFiles(pkg, rootDir string) []string {
var ret []string
func GetPackageFiles(pkg, rootDir string) []*PackageFileEntry {
var pkgFiles []*PackageFileEntry
installedDir := path.Join(rootDir, "var/lib/bpm/installed/")
pkgDir := path.Join(installedDir, pkg)
files := path.Join(pkgDir, "files")
@@ -1386,15 +1383,46 @@ func GetPackageFiles(pkg, rootDir string) []string {
if _, err := os.Stat(pkgDir); os.IsNotExist(err) {
return nil
}
file, err := os.Open(files)
bs, err := os.ReadFile(files)
if err != nil {
return nil
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ret = append(ret, scanner.Text())
for _, line := range strings.Split(string(bs), "\n") {
if strings.TrimSpace(line) == "" {
continue
}
stringEntry := strings.Split(strings.TrimSpace(line), " ")
if len(stringEntry) < 4 {
pkgFiles = append(pkgFiles, &PackageFileEntry{
Path: line,
UserID: 0,
GroupID: 0,
SizeInBytes: 0,
})
continue
}
uid, err := strconv.ParseInt(stringEntry[len(stringEntry)-3], 0, 32)
if err != nil {
return nil
}
gid, err := strconv.ParseInt(stringEntry[len(stringEntry)-2], 0, 32)
if err != nil {
return nil
}
size, err := strconv.ParseUint(stringEntry[len(stringEntry)-1], 0, 64)
if err != nil {
return nil
}
pkgFiles = append(pkgFiles, &PackageFileEntry{
Path: strings.Join(stringEntry[:len(stringEntry)-3], " "),
UserID: int(uid),
GroupID: int(gid),
SizeInBytes: size,
})
}
return ret
return pkgFiles
}
func GetPackageInfo(pkg, rootDir string) *PackageInfo {
@@ -1422,6 +1450,17 @@ func GetPackageInfo(pkg, rootDir string) *PackageInfo {
return info
}
func GetPackage(pkg, rootDir string) *BPMPackage {
if !IsPackageInstalled(pkg, rootDir) {
return nil
}
return &BPMPackage{
PkgInfo: GetPackageInfo(pkg, rootDir),
PkgFiles: GetPackageFiles(pkg, rootDir),
}
}
func RemovePackage(pkg string, verbose bool, rootDir string) error {
installedDir := path.Join(rootDir, "var/lib/bpm/installed/")
pkgDir := path.Join(installedDir, pkg)
@@ -1429,10 +1468,10 @@ func RemovePackage(pkg string, verbose bool, rootDir string) error {
if pkgInfo == nil {
return errors.New("could not get package info")
}
files := GetPackageFiles(pkg, rootDir)
fileEntries := GetPackageFiles(pkg, rootDir)
var symlinks []string
for _, file := range files {
file = path.Join(rootDir, file)
for _, entry := range fileEntries {
file := path.Join(rootDir, entry.Path)
lstat, err := os.Lstat(file)
if os.IsNotExist(err) {
continue
+9
View File
@@ -22,7 +22,10 @@ type Repository struct {
type RepositoryEntry struct {
Info *PackageInfo `yaml:"info"`
Download string `yaml:"download"`
DownloadSize uint64 `yaml:"download_size"`
InstalledSize uint64 `yaml:"installed_size"`
IsVirtualPackage bool `yaml:"-"`
Repository *Repository
}
func (repo *Repository) ContainsPackage(pkg string) bool {
@@ -63,7 +66,10 @@ func (repo *Repository) ReadLocalDatabase() error {
Provides: make([]string, 0),
},
Download: "",
DownloadSize: 0,
InstalledSize: 0,
IsVirtualPackage: false,
Repository: repo,
}
err := yaml.Unmarshal([]byte(b), &entry)
if err != nil {
@@ -84,7 +90,10 @@ func (repo *Repository) ReadLocalDatabase() error {
entry := RepositoryEntry{
Info: repo.Entries[value[0]].Info,
Download: repo.Entries[value[0]].Download,
DownloadSize: repo.Entries[value[0]].DownloadSize,
InstalledSize: repo.Entries[value[0]].InstalledSize,
IsVirtualPackage: true,
Repository: repo,
}
repo.Entries[key] = &entry
}