bpm-package: improve old package removal

This commit is contained in:
2026-06-21 19:38:39 +03:00
parent 8eb771111c
commit d781c30146
+98 -34
View File
@@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
"io/fs"
"log"
"maps"
"os"
@@ -143,22 +144,12 @@ func createArchive() string {
// Remove old package from source dir
if repo := bpmutilsshared.GetRepository(); repo != "" {
if database, err := bpmutilsshared.ReadDatabase(path.Join(repo, "source/database.bpmdb")); err == nil {
if entry, ok := database.Entries[pkgInfo.Name]; ok {
pkgFilepath := path.Join(repo, "source", entry.Filepath)
err := os.Remove(pkgFilepath)
if err != nil {
log.Printf("Warning: could not remove old source package (%s): %s", pkgFilepath, err)
}
// Remove package signature
if _, err := os.Stat(pkgFilepath + ".sig"); err == nil {
err := os.Remove(pkgFilepath + ".sig")
if err != nil {
log.Printf("Warning: could not remove old source package signature (%s): %s", pkgFilepath+".str", err)
}
}
}
removedFiles, err := RemoveSourcePackage(repo, pkgInfo.Name)
for _, file := range removedFiles {
fmt.Println("Removed " + file)
}
if err != nil {
log.Printf("Warning: could not remove old source packages: %s", err)
}
}
@@ -270,31 +261,22 @@ func compilePackage(archive string) {
for _, line := range strings.Split(strings.TrimSpace(string(cmdOutput)), "\n") {
// Read generated package info
pkgInfo, err := bpmutilsshared.ReadPacakgeInfoFromTarball(line)
if err != nil {
log.Fatalf("Error: could not read package info: %s", err)
}
if repo := bpmutilsshared.GetRepository(); repo != "" {
// Remove old package from binary dir
if database, err := bpmutilsshared.ReadDatabase(path.Join(repo, "binary/database.bpmdb")); err == nil {
if entry, ok := database.Entries[pkgInfo.Name]; ok {
pkgFilepath := path.Join(repo, "binary", entry.Filepath)
err := os.Remove(pkgFilepath)
if err != nil {
log.Printf("Warning: could not remove old binary package (%s): %s", pkgFilepath, err)
}
// Remove package signature
if _, err := os.Stat(pkgFilepath + ".sig"); err == nil {
err := os.Remove(pkgFilepath + ".sig")
if err != nil {
log.Printf("Warning: could not remove old binary package signature (%s): %s", pkgFilepath+".str", err)
}
}
}
removedFiles, err := RemoveBinaryPackage(repo, pkgInfo.Name)
for _, file := range removedFiles {
fmt.Println("Removed " + file)
}
if err != nil {
log.Printf("Warning: could not remove old binary packages: %s", err)
}
// Move package to binary dir
if err != nil {
log.Fatalf("Error: could not read package info: %s", err)
}
newPath := path.Join(repo, "binary", pkgInfo.Arch, path.Base(line))
os.MkdirAll(path.Dir(newPath), 0755)
os.Rename(line, newPath)
@@ -364,3 +346,85 @@ func setupFlagsAndHelp(usage, desc string) {
}
flag.Parse()
}
func RemoveSourcePackage(repo, pkgName string) (removedFiles []string, err error) {
if _, err := os.Stat(path.Join(repo, "source")); err != nil {
return removedFiles, nil
}
err = filepath.Walk(path.Join(repo, "source"), func(packagePath string, info fs.FileInfo, err error) error {
if !strings.HasSuffix(packagePath, ".bpm") {
return nil
}
// Get package info
pkgInfo, err := bpmutilsshared.ReadPacakgeInfoFromTarball(packagePath)
if err != nil {
return err
}
if pkgInfo.Name != pkgName {
return nil
}
// Remove package and its signature
err = os.Remove(packagePath)
if err != nil {
return err
}
if relPath, err := filepath.Rel(repo, packagePath); err != nil {
removedFiles = append(removedFiles, packagePath)
} else {
removedFiles = append(removedFiles, relPath)
}
err = os.Remove(packagePath + ".sig")
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
})
return removedFiles, err
}
func RemoveBinaryPackage(repo, pkgName string) (removedFiles []string, err error) {
if _, err := os.Stat(path.Join(repo, "binary")); err != nil {
return removedFiles, nil
}
err = filepath.Walk(path.Join(repo, "binary"), func(packagePath string, info fs.FileInfo, err error) error {
if !strings.HasSuffix(packagePath, ".bpm") {
return nil
}
// Get package info
pkgInfo, err := bpmutilsshared.ReadPacakgeInfoFromTarball(packagePath)
if err != nil {
return err
}
if pkgInfo.Name != pkgName {
return nil
}
// Remove package and its signature
err = os.Remove(packagePath)
if err != nil {
return err
}
if relPath, err := filepath.Rel(repo, packagePath); err != nil {
removedFiles = append(removedFiles, packagePath)
} else {
removedFiles = append(removedFiles, relPath)
}
err = os.Remove(packagePath + ".sig")
if err != nil && !os.IsNotExist(err) {
return err
}
return nil
})
return removedFiles, err
}