From 76efa42bcf6a20cbcd8a5557e47414a6d9f98723 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Thu, 10 Oct 2024 12:35:39 +0300 Subject: [PATCH 01/11] Started implementation on the new bpm file structure --- main.go | 71 +++++--- utils/extract_utils.go | 40 +++++ utils/general_utils.go | 13 ++ utils/package_utils.go | 382 +++++++++++++++++++++++------------------ 4 files changed, 312 insertions(+), 194 deletions(-) create mode 100644 utils/extract_utils.go diff --git a/main.go b/main.go index 739dd31..5a3f764 100644 --- a/main.go +++ b/main.go @@ -178,26 +178,26 @@ func resolveCommand() { bpmFile string isDependency bool shouldFetch bool - pkgInfo *utils.PackageInfo + bpmpkg *utils.BPMPackage }]() unresolvedDepends := make([]string, 0) // Search for packages for _, pkg := range pkgs { if stat, err := os.Stat(pkg); err == nil && !stat.IsDir() { - pkgInfo, err := utils.ReadPackage(pkg) + bpmpkg, err := utils.ReadPackage(pkg) if err != nil { log.Fatalf("Error: could not read package: %s\n", err) } - if !reinstall && utils.IsPackageInstalled(pkgInfo.Name, rootDir) && utils.GetPackageInfo(pkgInfo.Name, rootDir, true).GetFullVersion() == pkgInfo.GetFullVersion() { + if !reinstall && utils.IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) && utils.GetPackageInfo(bpmpkg.PkgInfo.Name, rootDir, true).GetFullVersion() == bpmpkg.PkgInfo.GetFullVersion() { continue } - pkgsToInstall.Set(pkgInfo.Name, &struct { + pkgsToInstall.Set(bpmpkg.PkgInfo.Name, &struct { bpmFile string isDependency bool shouldFetch bool - pkgInfo *utils.PackageInfo - }{bpmFile: pkg, isDependency: false, shouldFetch: false, pkgInfo: pkgInfo}) + bpmpkg *utils.BPMPackage + }{bpmFile: pkg, isDependency: false, shouldFetch: false, bpmpkg: bpmpkg}) } else { entry, _, err := utils.GetRepositoryEntry(pkg) if err != nil { @@ -210,8 +210,11 @@ func resolveCommand() { bpmFile string isDependency bool shouldFetch bool - pkgInfo *utils.PackageInfo - }{bpmFile: "", isDependency: false, shouldFetch: true, pkgInfo: entry.Info}) + bpmpkg *utils.BPMPackage + }{bpmFile: "", isDependency: false, shouldFetch: true, bpmpkg: &utils.BPMPackage{ + PkgInfo: entry.Info, + PkgFiles: nil, + }}) } } @@ -220,14 +223,14 @@ func resolveCommand() { bpmFile string isDependency bool shouldFetch bool - pkgInfo *utils.PackageInfo + bpmpkg *utils.BPMPackage }]() for _, pkg := range clone.Keys() { value, _ := clone.Get(pkg) - resolved, unresolved := value.pkgInfo.ResolveAll(&[]string{}, &[]string{}, value.pkgInfo.Type == "source", !noOptional, !reinstall, verbose, rootDir) + resolved, unresolved := value.bpmpkg.PkgInfo.ResolveAll(&[]string{}, &[]string{}, value.bpmpkg.PkgInfo.Type == "source", !noOptional, !reinstall, verbose, rootDir) unresolvedDepends = append(unresolvedDepends, unresolved...) for _, depend := range resolved { - if _, ok := pkgsToInstall.Get(depend); !ok && depend != value.pkgInfo.Name { + if _, ok := pkgsToInstall.Get(depend); !ok && depend != value.bpmpkg.PkgInfo.Name { if !reinstallAll && utils.IsPackageInstalled(depend, rootDir) { continue } @@ -239,8 +242,11 @@ func resolveCommand() { bpmFile string isDependency bool shouldFetch bool - pkgInfo *utils.PackageInfo - }{bpmFile: "", isDependency: true, shouldFetch: true, pkgInfo: entry.Info}) + bpmpkg *utils.BPMPackage + }{bpmFile: "", isDependency: true, shouldFetch: true, bpmpkg: &utils.BPMPackage{ + PkgInfo: entry.Info, + PkgFiles: nil, + }}) } } pkgsToInstall.Set(pkg, value) @@ -259,12 +265,13 @@ func resolveCommand() { os.Exit(0) } + var totalOperationSize uint64 = 0 for _, pkg := range pkgsToInstall.Keys() { value, _ := pkgsToInstall.Get(pkg) - pkgInfo := value.pkgInfo - installedInfo := utils.GetPackageInfo(pkgInfo.Name, rootDir, false) + bpmpkg := value.bpmpkg + installedInfo := utils.GetPackageInfo(bpmpkg.PkgInfo.Name, rootDir, false) sourceInfo := "" - if pkgInfo.Type == "source" { + if bpmpkg.PkgInfo.Type == "source" { if rootDir != "/" && !force { log.Fatalf("Error: cannot compile and install source packages to a different root directory") } @@ -272,21 +279,29 @@ func resolveCommand() { } if installedInfo == nil { - fmt.Printf("%s: %s (Install) %s\n", pkgInfo.Name, pkgInfo.GetFullVersion(), sourceInfo) + fmt.Printf("%s: %s (Install) %s\n", bpmpkg.PkgInfo.Name, bpmpkg.PkgInfo.GetFullVersion(), sourceInfo) + totalOperationSize += bpmpkg.GetInstalledSize() } else { - comparison := utils.ComparePackageVersions(*pkgInfo, *installedInfo) + comparison := utils.ComparePackageVersions(*bpmpkg.PkgInfo, *installedInfo) if comparison < 0 { - fmt.Printf("%s: %s -> %s (Downgrade) %s\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), sourceInfo) + fmt.Printf("%s: %s -> %s (Downgrade) %s\n", bpmpkg.PkgInfo.Name, installedInfo.GetFullVersion(), bpmpkg.PkgInfo.GetFullVersion(), sourceInfo) } else if comparison > 0 { - fmt.Printf("%s: %s -> %s (Upgrade) %s\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), sourceInfo) + fmt.Printf("%s: %s -> %s (Upgrade) %s\n", bpmpkg.PkgInfo.Name, installedInfo.GetFullVersion(), bpmpkg.PkgInfo.GetFullVersion(), sourceInfo) } else { - fmt.Printf("%s: %s (Reinstall) %s\n", pkgInfo.Name, pkgInfo.GetFullVersion(), sourceInfo) + fmt.Printf("%s: %s (Reinstall) %s\n", bpmpkg.PkgInfo.Name, bpmpkg.PkgInfo.GetFullVersion(), sourceInfo) } + totalOperationSize += bpmpkg.GetInstalledSize() } } if rootDir != "/" { fmt.Println("Warning: Operating in " + rootDir) } + if totalOperationSize >= 0 { + fmt.Printf("A total of %s will be used to complete this operation\n", utils.BytesToHumanReadable(totalOperationSize)) + } else { + fmt.Printf("A total of %s will be freed by this operation\n", utils.BytesToHumanReadable(totalOperationSize)) + } + if !yesAll { reader := bufio.NewReader(os.Stdin) if pkgsToInstall.Len() == 1 { @@ -317,7 +332,7 @@ func resolveCommand() { if err != nil { log.Fatalf("Error: could not fetch package (%s): %s\n", pkg, err) } - fmt.Printf("Package (%s) was successfully fetched!\n", value.pkgInfo.Name) + fmt.Printf("Package (%s) was successfully fetched!\n", value.bpmpkg.PkgInfo.Name) value.bpmFile = fetchedPackage pkgsToInstall.Set(pkg, value) } @@ -325,7 +340,7 @@ func resolveCommand() { // Install fetched packages for _, pkg := range pkgsToInstall.Keys() { value, _ := pkgsToInstall.Get(pkg) - pkgInfo := value.pkgInfo + bpmpkg := value.bpmpkg var err error if value.isDependency { err = utils.InstallPackage(value.bpmFile, rootDir, verbose, true, buildSource, skipCheck, keepTempDir) @@ -334,19 +349,19 @@ func resolveCommand() { } if err != nil { - if pkgInfo.Type == "source" && keepTempDir { - fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) + if bpmpkg.PkgInfo.Type == "source" && keepTempDir { + fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + bpmpkg.PkgInfo.Name) } log.Fatalf("Error: could not install package (%s): %s\n", pkg, err) } - fmt.Printf("Package (%s) was successfully installed\n", pkgInfo.Name) + fmt.Printf("Package (%s) was successfully installed\n", bpmpkg.PkgInfo.Name) if value.isDependency { - err := utils.SetInstallationReason(pkgInfo.Name, utils.Dependency, rootDir) + err := utils.SetInstallationReason(bpmpkg.PkgInfo.Name, utils.Dependency, rootDir) if err != nil { log.Fatalf("Error: could not set installation reason for package: %s\n", err) } } - if pkgInfo.Type == "source" && keepTempDir { + if bpmpkg.PkgInfo.Type == "source" && keepTempDir { fmt.Println("** It is recommended you delete the temporary bpm folder in /var/tmp **") } } diff --git a/utils/extract_utils.go b/utils/extract_utils.go new file mode 100644 index 0000000..636fde1 --- /dev/null +++ b/utils/extract_utils.go @@ -0,0 +1,40 @@ +package utils + +import ( + "archive/tar" + "errors" + "io" + "os" +) + +func ReadTarballContent(tarballPath, fileToExtract string) ([]byte, error) { + file, err := os.Open(tarballPath) + if err != nil { + return nil, err + } + defer file.Close() + + tr := tar.NewReader(file) + for { + header, err := tr.Next() + if err == io.EOF { + break + } + if err != nil { + return nil, err + } + if header.Name == fileToExtract { + if header.Typeflag != tar.TypeReg { + return nil, errors.New("file to extract must be a regular file") + } + + bytes, err := io.ReadAll(tr) + if err != nil { + return nil, err + } + return bytes, nil + } + } + + return nil, errors.New("could not file in tarball") +} diff --git a/utils/general_utils.go b/utils/general_utils.go index ca568c1..6dbc29a 100644 --- a/utils/general_utils.go +++ b/utils/general_utils.go @@ -1,7 +1,9 @@ package utils import ( + "fmt" "io" + "math" "os" "os/exec" "strings" @@ -56,3 +58,14 @@ func stringSliceRemoveEmpty(s []string) []string { } return r } + +func BytesToHumanReadable(b uint64) string { + bf := float64(b) + for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} { + if math.Abs(bf) < 1024.0 { + return fmt.Sprintf("%3.1f%sB", bf, unit) + } + bf /= 1024.0 + } + return fmt.Sprintf("%.1fYiB", bf) +} diff --git a/utils/package_utils.go b/utils/package_utils.go index 04b4435..89153a8 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -3,6 +3,7 @@ package utils import ( "archive/tar" "bufio" + "bytes" "compress/gzip" "errors" "fmt" @@ -20,6 +21,11 @@ import ( "syscall" ) +type BPMPackage struct { + PkgInfo *PackageInfo + PkgFiles []*PackageFileEntry +} + type PackageInfo struct { Name string `yaml:"name,omitempty"` Description string `yaml:"description,omitempty"` @@ -37,6 +43,29 @@ type PackageInfo struct { Provides []string `yaml:"provides,omitempty"` } +type PackageFileEntry struct { + Path string + UserID int + GroupID int + SizeInBytes uint64 +} + +func (pkg *BPMPackage) GetInstalledSize() uint64 { + var totalSize uint64 = 0 + for _, entry := range pkg.PkgFiles { + totalSize += entry.SizeInBytes + } + return totalSize +} + +func (pkg *BPMPackage) ConvertFilesToString() string { + str := "" + for _, file := range pkg.PkgFiles { + str += fmt.Sprintf("%s %d %d %d\n", file.Path, file.UserID, file.GroupID, file.SizeInBytes) + } + return str +} + func (pkgInfo *PackageInfo) GetFullVersion() string { return pkgInfo.Version + "-" + strconv.Itoa(pkgInfo.Revision) } @@ -93,11 +122,8 @@ func GetPackageInfoRaw(filename string) (string, error) { if err != nil { return "", err } - archive, err := gzip.NewReader(file) - if err != nil { - return "", err - } - tr := tar.NewReader(archive) + + tr := tar.NewReader(file) for { header, err := tr.Next() if err == io.EOF { @@ -118,19 +144,21 @@ func GetPackageInfoRaw(filename string) (string, error) { return "", errors.New("pkg.info not found in archive") } -func ReadPackage(filename string) (*PackageInfo, error) { +func ReadPackage(filename string) (*BPMPackage, error) { + var pkgInfo *PackageInfo + var pkgFiles []*PackageFileEntry + if _, err := os.Stat(filename); os.IsNotExist(err) { return nil, err } + file, err := os.Open(filename) + defer file.Close() if err != nil { return nil, err } - archive, err := gzip.NewReader(file) - if err != nil { - return nil, err - } - tr := tar.NewReader(archive) + + tr := tar.NewReader(file) for { header, err := tr.Next() if err == io.EOF { @@ -141,18 +169,49 @@ func ReadPackage(filename string) (*PackageInfo, error) { } if header.Name == "pkg.info" { bs, _ := io.ReadAll(tr) - err := file.Close() + pkgInfo, err = ReadPackageInfo(string(bs)) if err != nil { return nil, err } - pkgInfo, err := ReadPackageInfo(string(bs)) - if err != nil { - return nil, err + } else if header.Name == "pkg.files" { + bs, _ := io.ReadAll(tr) + for _, line := range strings.Split(string(bs), "\n") { + if strings.TrimSpace(line) == "" { + continue + } + stringEntry := strings.Split(strings.TrimSpace(line), " ") + if len(stringEntry) != 4 { + return nil, errors.New("pkg.files is not formatted correctly") + } + uid, err := strconv.ParseInt(stringEntry[1], 0, 32) + if err != nil { + return nil, err + } + gid, err := strconv.ParseInt(stringEntry[2], 0, 32) + if err != nil { + return nil, err + } + size, err := strconv.ParseUint(stringEntry[3], 0, 64) + if err != nil { + return nil, err + } + pkgFiles = append(pkgFiles, &PackageFileEntry{ + Path: stringEntry[0], + UserID: int(uid), + GroupID: int(gid), + SizeInBytes: size, + }) } - return pkgInfo, nil } } - return nil, errors.New("pkg.info not found in archive") + + if pkgInfo == nil { + return nil, errors.New("pkg.info not found in archive") + } + return &BPMPackage{ + PkgInfo: pkgInfo, + PkgFiles: pkgFiles, + }, nil } func ReadPackageScripts(filename string) (map[string]string, error) { @@ -163,11 +222,8 @@ func ReadPackageScripts(filename string) (map[string]string, error) { if err != nil { return nil, err } - archive, err := gzip.NewReader(file) - if err != nil { - return nil, err - } - tr := tar.NewReader(archive) + + tr := tar.NewReader(file) ret := make(map[string]string) for { header, err := tr.Next() @@ -209,10 +265,7 @@ func ReadPackageScripts(filename string) (map[string]string, error) { ret[header.Name] = string(bs) } } - err = archive.Close() - if err != nil { - return nil, err - } + err = file.Close() if err != nil { return nil, err @@ -257,25 +310,25 @@ func ExecutePackageScripts(filename, rootDir string, operation Operation, postOp cmd.Dir = rootDir cmd.Env = os.Environ() cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_ROOT=%s", rootDir)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_NAME=%s", pkgInfo.Name)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_DESC=%s", pkgInfo.Description)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_VERSION=%s", pkgInfo.Version)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_NAME=%s", pkgInfo.PkgInfo.Name)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_DESC=%s", pkgInfo.PkgInfo.Description)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_VERSION=%s", pkgInfo.PkgInfo.Version)) if operation != Install { - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_OLD_VERSION=%s", GetPackageInfo(pkgInfo.Name, rootDir, true).Version)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_OLD_VERSION=%s", GetPackageInfo(pkgInfo.PkgInfo.Name, rootDir, true).Version)) } - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_REVISION=%d", pkgInfo.Revision)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_REVISION=%d", pkgInfo.PkgInfo.Revision)) if operation != Install { - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_OLD_REVISION=%d", GetPackageInfo(pkgInfo.Name, rootDir, true).Revision)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_OLD_REVISION=%d", GetPackageInfo(pkgInfo.PkgInfo.Name, rootDir, true).Revision)) } - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_URL=%s", pkgInfo.Url)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_ARCH=%s", pkgInfo.Arch)) - depends := make([]string, len(pkgInfo.Depends)) - copy(depends, pkgInfo.Depends) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_URL=%s", pkgInfo.PkgInfo.Url)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_ARCH=%s", pkgInfo.PkgInfo.Arch)) + depends := make([]string, len(pkgInfo.PkgInfo.Depends)) + copy(depends, pkgInfo.PkgInfo.Depends) for i := 0; i < len(depends); i++ { depends[i] = fmt.Sprintf("\"%s\"", depends[i]) } - makeDepends := make([]string, len(pkgInfo.MakeDepends)) - copy(makeDepends, pkgInfo.MakeDepends) + makeDepends := make([]string, len(pkgInfo.PkgInfo.MakeDepends)) + copy(makeDepends, pkgInfo.PkgInfo.MakeDepends) for i := 0; i < len(makeDepends); i++ { makeDepends[i] = fmt.Sprintf("\"%s\"", makeDepends[i]) } @@ -411,9 +464,9 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations bool, p return strings.Join(ret, "\n") } -func extractPackage(pkgInfo *PackageInfo, verbose bool, filename, rootDir string) (error, []string) { +func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) (error, []string) { var files []string - if !IsPackageInstalled(pkgInfo.Name, rootDir) { + if !IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) { err := ExecutePackageScripts(filename, rootDir, Install, false) if err != nil { return err, nil @@ -429,7 +482,13 @@ func extractPackage(pkgInfo *PackageInfo, verbose bool, filename, rootDir string if err != nil { return err, nil } - archive, err := gzip.NewReader(file) + + content, err := ReadTarballContent(filename, "files.tar.gz") + if err != nil { + return err, nil + } + + archive, err := gzip.NewReader(bytes.NewReader(content)) if err != nil { return err, nil } @@ -442,97 +501,95 @@ func extractPackage(pkgInfo *PackageInfo, verbose bool, filename, rootDir string if err != nil { return err, nil } - if strings.HasPrefix(header.Name, "files/") && header.Name != "files/" { - trimmedName := strings.TrimPrefix(header.Name, "files/") - extractFilename := path.Join(rootDir, trimmedName) - switch header.Typeflag { - case tar.TypeDir: - files = append(files, strings.TrimPrefix(header.Name, "files/")) - if err := os.Mkdir(extractFilename, 0755); err != nil { - if !os.IsExist(err) { - return err, nil - } - } else { - if verbose { - fmt.Println("Creating Directory: " + extractFilename) - } + trimmedName := strings.TrimPrefix(header.Name, "./") + extractFilename := path.Join(rootDir, trimmedName) + switch header.Typeflag { + case tar.TypeDir: + files = append(files, strings.TrimPrefix(header.Name, "./")) + if err := os.Mkdir(extractFilename, 0755); err != nil { + if !os.IsExist(err) { + return err, nil } - case tar.TypeReg: - skip := false - if _, err := os.Stat(extractFilename); err == nil { - for _, k := range pkgInfo.Keep { - if strings.HasSuffix(k, "/") { - if strings.HasPrefix(trimmedName, k) { - if verbose { - fmt.Println("Skipping File: " + extractFilename + " (Containing directory is set to be kept during installs/updates)") - } - files = append(files, strings.TrimPrefix(header.Name, "files/")) - skip = true - continue + } else { + if verbose { + fmt.Println("Creating Directory: " + extractFilename) + } + } + case tar.TypeReg: + skip := false + if _, err := os.Stat(extractFilename); err == nil { + for _, k := range bpmpkg.PkgInfo.Keep { + if strings.HasSuffix(k, "/") { + if strings.HasPrefix(trimmedName, k) { + if verbose { + fmt.Println("Skipping File: " + extractFilename + " (Containing directory is set to be kept during installs/updates)") } - } else { - if trimmedName == k { - if verbose { - fmt.Println("Skipping File: " + extractFilename + " (File is configured to be kept during installs/updates)") - } - files = append(files, strings.TrimPrefix(header.Name, "files/")) - skip = true - continue + files = append(files, strings.TrimPrefix(header.Name, "./")) + skip = true + continue + } + } else { + if trimmedName == k { + if verbose { + fmt.Println("Skipping File: " + extractFilename + " (File is configured to be kept during installs/updates)") } + files = append(files, strings.TrimPrefix(header.Name, "./")) + skip = true + continue } } } - if skip { - continue - } - err := os.Remove(extractFilename) - if err != nil && !os.IsNotExist(err) { - return err, nil - } - outFile, err := os.Create(extractFilename) - if verbose { - fmt.Println("Creating File: " + extractFilename) - } - files = append(files, strings.TrimPrefix(header.Name, "files/")) - if err != nil { - return err, nil - } - if _, err := io.Copy(outFile, tr); err != nil { - return err, nil - } - if err := os.Chmod(extractFilename, header.FileInfo().Mode()); err != nil { - return err, nil - } - err = outFile.Close() - if err != nil { - return err, nil - } - case tar.TypeSymlink: - if verbose { - fmt.Println("Creating Symlink: " + extractFilename + " -> " + header.Linkname) - } - files = append(files, strings.TrimPrefix(header.Name, "files/")) - err := os.Remove(extractFilename) - if err != nil && !os.IsNotExist(err) { - return err, nil - } - err = os.Symlink(header.Linkname, extractFilename) - if err != nil { - return err, nil - } - case tar.TypeLink: - if verbose { - fmt.Println("Detected Hard Link: " + extractFilename + " -> " + path.Join(rootDir, strings.TrimPrefix(header.Linkname, "files/"))) - } - files = append(files, strings.TrimPrefix(header.Name, "files/")) - seenHardlinks[extractFilename] = path.Join(strings.TrimPrefix(header.Linkname, "files/")) - err := os.Remove(extractFilename) - if err != nil && !os.IsNotExist(err) { - return err, nil - } - default: - return errors.New("unknown type (" + strconv.Itoa(int(header.Typeflag)) + ") in " + extractFilename), nil } + if skip { + continue + } + err := os.Remove(extractFilename) + if err != nil && !os.IsNotExist(err) { + return err, nil + } + outFile, err := os.Create(extractFilename) + if verbose { + fmt.Println("Creating File: " + extractFilename) + } + files = append(files, strings.TrimPrefix(header.Name, "./")) + if err != nil { + return err, nil + } + if _, err := io.Copy(outFile, tr); err != nil { + return err, nil + } + if err := os.Chmod(extractFilename, header.FileInfo().Mode()); err != nil { + return err, nil + } + err = outFile.Close() + if err != nil { + return err, nil + } + case tar.TypeSymlink: + if verbose { + fmt.Println("Creating Symlink: " + extractFilename + " -> " + header.Linkname) + } + files = append(files, strings.TrimPrefix(header.Name, "./")) + err := os.Remove(extractFilename) + if err != nil && !os.IsNotExist(err) { + return err, nil + } + err = os.Symlink(header.Linkname, extractFilename) + if err != nil { + return err, nil + } + case tar.TypeLink: + if verbose { + fmt.Println("Detected Hard Link: " + extractFilename + " -> " + path.Join(rootDir, strings.TrimPrefix(header.Linkname, "files/"))) + } + files = append(files, strings.TrimPrefix(header.Name, "./")) + seenHardlinks[extractFilename] = path.Join(strings.TrimPrefix(header.Linkname, "files/")) + err := os.Remove(extractFilename) + if err != nil && !os.IsNotExist(err) { + return err, nil + } + default: + return errors.New("unknown type (" + strconv.Itoa(int(header.Typeflag)) + ") in " + extractFilename), nil } } for extractFilename, destination := range seenHardlinks { @@ -554,7 +611,7 @@ func isSplitPackage(filename string) bool { if err != nil { return false } - if pkgInfo.Type != "source" { + if pkgInfo.PkgInfo.Type != "source" { return false } cmd := exec.Command("/bin/bash", "-c", fmt.Sprintf("test $(tar -tf %s | grep '^pkg.info' | wc -l) -eq 1", filename)) @@ -564,9 +621,9 @@ func isSplitPackage(filename string) bool { return true } -func compilePackage(pkgInfo *PackageInfo, filename, rootDir string, verbose, binaryPkgFromSrc, skipCheck, keepTempDir bool) (error, []string) { +func compilePackage(bpmpkg *BPMPackage, filename, rootDir string, verbose, binaryPkgFromSrc, skipCheck, keepTempDir bool) (error, []string) { var files []string - if !IsPackageInstalled(pkgInfo.Name, rootDir) { + if !IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) { err := ExecutePackageScripts(filename, rootDir, Install, false) if err != nil { return err, nil @@ -582,13 +639,10 @@ func compilePackage(pkgInfo *PackageInfo, filename, rootDir string, verbose, bin if err != nil { return err, nil } - archive, err := gzip.NewReader(file) - if err != nil { - return err, nil - } - tr := tar.NewReader(archive) - temp := path.Join(BPMConfig.CompilationDir, "bpm_source-"+pkgInfo.Name) + tr := tar.NewReader(file) + + temp := path.Join(BPMConfig.CompilationDir, "bpm_source-"+bpmpkg.PkgInfo.Name) err = os.RemoveAll(temp) if err != nil { return err, nil @@ -690,7 +744,7 @@ func compilePackage(pkgInfo *PackageInfo, filename, rootDir string, verbose, bin return err, nil } fmt.Println("Running source.sh file...") - if !IsPackageInstalled(pkgInfo.Name, rootDir) { + if !IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) { err = ExecutePackageScripts(filename, rootDir, Install, false) if err != nil { return err, nil @@ -800,19 +854,19 @@ fi cmd.Env = append(cmd.Env, "SKIPCHECK="+strconv.FormatBool(skipCheck)) cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_ROOT=%s", rootDir)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_NAME=%s", pkgInfo.Name)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_DESC=%s", pkgInfo.Description)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_VERSION=%s", pkgInfo.Version)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_REVISION=%d", pkgInfo.Revision)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_URL=%s", pkgInfo.Url)) - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_ARCH=%s", pkgInfo.Arch)) - depends := make([]string, len(pkgInfo.Depends)) - copy(depends, pkgInfo.Depends) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_NAME=%s", bpmpkg.PkgInfo.Name)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_DESC=%s", bpmpkg.PkgInfo.Description)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_VERSION=%s", bpmpkg.PkgInfo.Version)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_REVISION=%d", bpmpkg.PkgInfo.Revision)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_URL=%s", bpmpkg.PkgInfo.Url)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_ARCH=%s", bpmpkg.PkgInfo.Arch)) + depends := make([]string, len(bpmpkg.PkgInfo.Depends)) + copy(depends, bpmpkg.PkgInfo.Depends) for i := 0; i < len(depends); i++ { depends[i] = fmt.Sprintf("\"%s\"", depends[i]) } - makeDepends := make([]string, len(pkgInfo.MakeDepends)) - copy(makeDepends, pkgInfo.MakeDepends) + makeDepends := make([]string, len(bpmpkg.PkgInfo.MakeDepends)) + copy(makeDepends, bpmpkg.PkgInfo.MakeDepends) for i := 0; i < len(makeDepends); i++ { makeDepends[i] = fmt.Sprintf("\"%s\"", makeDepends[i]) } @@ -864,7 +918,7 @@ fi } } else if d.Type().IsRegular() { if _, err := os.Stat(extractFilename); err == nil { - if slices.Contains(pkgInfo.Keep, relFilename) { + if slices.Contains(bpmpkg.PkgInfo.Keep, relFilename) { if verbose { fmt.Println("Skipping File: " + extractFilename + "(File is configured to be kept during installs/updates)") } @@ -933,7 +987,7 @@ fi compiledDir := path.Join(BPMConfig.BinaryOutputDir) err = os.MkdirAll(compiledDir, 0755) compiledInfo := PackageInfo{} - compiledInfo = *pkgInfo + compiledInfo = *bpmpkg.PkgInfo compiledInfo.Type = "binary" compiledInfo.Arch = GetArch() err = os.WriteFile(path.Join(temp, "pkg.info"), []byte(CreateInfoFile(&compiledInfo)), 0644) @@ -991,7 +1045,6 @@ fi return errors.New("no output files for source package. Cancelling package installation"), nil } - defer archive.Close() defer file.Close() return nil, files } @@ -1002,39 +1055,39 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, } var oldFiles []string var files []string - pkgInfo, err := ReadPackage(filename) + bpmpkg, err := ReadPackage(filename) if err != nil { return err } - packageInstalled := IsPackageInstalled(pkgInfo.Name, rootDir) + packageInstalled := IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) if packageInstalled { - oldFiles = GetPackageFiles(pkgInfo.Name, rootDir) + oldFiles = GetPackageFiles(bpmpkg.PkgInfo.Name, rootDir) } if !force { - if pkgInfo.Arch != "any" && pkgInfo.Arch != GetArch() { + if bpmpkg.PkgInfo.Arch != "any" && bpmpkg.PkgInfo.Arch != GetArch() { return errors.New("cannot install a package with a different architecture") } - if unresolved := pkgInfo.CheckDependencies(pkgInfo.Type == "source", true, rootDir); len(unresolved) != 0 { + 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 pkgInfo.Type == "binary" { - err, i := extractPackage(pkgInfo, verbose, filename, rootDir) + if bpmpkg.PkgInfo.Type == "binary" { + err, i := extractPackage(bpmpkg, verbose, filename, rootDir) if err != nil { return err } files = i - } else if pkgInfo.Type == "source" { + } else if bpmpkg.PkgInfo.Type == "source" { if isSplitPackage(filename) { return errors.New("BPM is unable to install split source packages") } - err, i := compilePackage(pkgInfo, filename, rootDir, verbose, binaryPkgFromSrc, skipCheck, keepTempDir) + err, i := compilePackage(bpmpkg, filename, rootDir, verbose, binaryPkgFromSrc, skipCheck, keepTempDir) if err != nil { return err } files = i } else { - return errors.New("unknown package type: " + pkgInfo.Type) + return errors.New("unknown package type: " + bpmpkg.PkgInfo.Type) } slices.Sort(files) slices.Reverse(files) @@ -1048,7 +1101,7 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, if err != nil { return err } - pkgDir := path.Join(installedDir, pkgInfo.Name) + pkgDir := path.Join(installedDir, bpmpkg.PkgInfo.Name) err = os.RemoveAll(pkgDir) if err != nil { return err @@ -1201,25 +1254,22 @@ func GetSourceScript(filename string) (string, error) { if err != nil { return "", err } - if pkgInfo.Type != "source" { + if pkgInfo.PkgInfo.Type != "source" { return "", errors.New("package not of source type") } file, err := os.Open(filename) if err != nil { return "", err } - archive, err := gzip.NewReader(file) - if err != nil { - return "", err - } - tr := tar.NewReader(archive) + + tr := tar.NewReader(file) for { header, err := tr.Next() if err == io.EOF { break } if header.Name == "source.sh" { - err := archive.Close() + err := file.Close() if err != nil { return "", err } From 1bd57110a08ef453df8fddf2a9ab44adf7429998 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Thu, 10 Oct 2024 12:44:06 +0300 Subject: [PATCH 02/11] Removed some unused functions and renamed some variables --- main.go | 16 ++++++------ utils/general_utils.go | 10 -------- utils/package_utils.go | 56 +++++++----------------------------------- 3 files changed, 17 insertions(+), 65 deletions(-) diff --git a/main.go b/main.go index 5a3f764..4bd077f 100644 --- a/main.go +++ b/main.go @@ -97,7 +97,7 @@ func resolveCommand() { } for n, pkg := range packages { var info *utils.PackageInfo - info = utils.GetPackageInfo(pkg, rootDir, false) + info = utils.GetPackageInfo(pkg, rootDir) if info == nil { log.Fatalf("Error: package (%s) is not installed\n", pkg) } @@ -125,7 +125,7 @@ func resolveCommand() { return } for n, pkg := range packages { - info := utils.GetPackageInfo(pkg, rootDir, false) + info := utils.GetPackageInfo(pkg, rootDir) if info == nil { fmt.Printf("Package (%s) could not be found\n", pkg) continue @@ -189,7 +189,7 @@ func resolveCommand() { if err != nil { log.Fatalf("Error: could not read package: %s\n", err) } - if !reinstall && utils.IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) && utils.GetPackageInfo(bpmpkg.PkgInfo.Name, rootDir, true).GetFullVersion() == bpmpkg.PkgInfo.GetFullVersion() { + if !reinstall && utils.IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) && utils.GetPackageInfo(bpmpkg.PkgInfo.Name, rootDir).GetFullVersion() == bpmpkg.PkgInfo.GetFullVersion() { continue } pkgsToInstall.Set(bpmpkg.PkgInfo.Name, &struct { @@ -203,7 +203,7 @@ func resolveCommand() { if err != nil { log.Fatalf("Error: could not find package (%s) in any repository\n", pkg) } - if !reinstall && utils.IsPackageInstalled(entry.Info.Name, rootDir) && utils.GetPackageInfo(entry.Info.Name, rootDir, true).GetFullVersion() == entry.Info.GetFullVersion() { + if !reinstall && utils.IsPackageInstalled(entry.Info.Name, rootDir) && utils.GetPackageInfo(entry.Info.Name, rootDir).GetFullVersion() == entry.Info.GetFullVersion() { continue } pkgsToInstall.Set(entry.Info.Name, &struct { @@ -269,7 +269,7 @@ func resolveCommand() { for _, pkg := range pkgsToInstall.Keys() { value, _ := pkgsToInstall.Get(pkg) bpmpkg := value.bpmpkg - installedInfo := utils.GetPackageInfo(bpmpkg.PkgInfo.Name, rootDir, false) + installedInfo := utils.GetPackageInfo(bpmpkg.PkgInfo.Name, rootDir) sourceInfo := "" if bpmpkg.PkgInfo.Type == "source" { if rootDir != "/" && !force { @@ -398,7 +398,7 @@ func resolveCommand() { if err != nil { continue } - installedInfo := utils.GetPackageInfo(pkg, rootDir, true) + installedInfo := utils.GetPackageInfo(pkg, rootDir) if installedInfo == nil { log.Fatalf("Error: could not get package info for (%s)\n", pkg) } else { @@ -452,7 +452,7 @@ func resolveCommand() { for _, key := range toUpdate.Keys() { value, _ := toUpdate.Get(key) - installedInfo := utils.GetPackageInfo(value.entry.Info.Name, rootDir, true) + installedInfo := utils.GetPackageInfo(value.entry.Info.Name, rootDir) sourceInfo := "" if value.entry.Info.Type == "source" { sourceInfo = "(From Source)" @@ -560,7 +560,7 @@ func resolveCommand() { return } for _, pkg := range packages { - pkgInfo := utils.GetPackageInfo(pkg, rootDir, false) + pkgInfo := utils.GetPackageInfo(pkg, rootDir) if pkgInfo == nil { fmt.Printf("Package (%s) could not be found\n", pkg) continue diff --git a/utils/general_utils.go b/utils/general_utils.go index 6dbc29a..51ce30b 100644 --- a/utils/general_utils.go +++ b/utils/general_utils.go @@ -49,16 +49,6 @@ func stringSliceRemove(s []string, r string) []string { return s } -func stringSliceRemoveEmpty(s []string) []string { - var r []string - for _, str := range s { - if str != "" { - r = append(r, str) - } - } - return r -} - func BytesToHumanReadable(b uint64) string { bf := float64(b) for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} { diff --git a/utils/package_utils.go b/utils/package_utils.go index 89153a8..0459bc0 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -91,11 +91,11 @@ func GetInstallationReason(pkg, rootDir string) InstallationReason { if stat, err := os.Stat(path.Join(pkgDir, "installation_reason")); err != nil || stat.IsDir() { return Manual } - bytes, err := os.ReadFile(path.Join(pkgDir, "installation_reason")) + b, err := os.ReadFile(path.Join(pkgDir, "installation_reason")) if err != nil { return Unknown } - reason := string(bytes) + reason := string(b) if reason == "manual" { return Manual } else if reason == "dependency" { @@ -314,11 +314,11 @@ func ExecutePackageScripts(filename, rootDir string, operation Operation, postOp cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_DESC=%s", pkgInfo.PkgInfo.Description)) cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_VERSION=%s", pkgInfo.PkgInfo.Version)) if operation != Install { - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_OLD_VERSION=%s", GetPackageInfo(pkgInfo.PkgInfo.Name, rootDir, true).Version)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_OLD_VERSION=%s", GetPackageInfo(pkgInfo.PkgInfo.Name, rootDir).Version)) } cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_REVISION=%d", pkgInfo.PkgInfo.Revision)) if operation != Install { - cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_OLD_REVISION=%d", GetPackageInfo(pkgInfo.PkgInfo.Name, rootDir, true).Revision)) + cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_OLD_REVISION=%d", GetPackageInfo(pkgInfo.PkgInfo.Name, rootDir).Revision)) } cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_URL=%s", pkgInfo.PkgInfo.Url)) cmd.Env = append(cmd.Env, fmt.Sprintf("BPM_PKG_ARCH=%s", pkgInfo.PkgInfo.Arch)) @@ -426,11 +426,11 @@ func ReadPackageInfo(contents string) (*PackageInfo, error) { } func CreateInfoFile(pkgInfo *PackageInfo) string { - bytes, err := yaml.Marshal(&pkgInfo) + b, err := yaml.Marshal(&pkgInfo) if err != nil { return "" } - return string(bytes) + return string(b) } func CreateReadableInfo(showArchitecture, showType, showPackageRelations bool, pkgInfo *PackageInfo, rootDir string) string { @@ -1249,44 +1249,6 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, return nil } -func GetSourceScript(filename string) (string, error) { - pkgInfo, err := ReadPackage(filename) - if err != nil { - return "", err - } - if pkgInfo.PkgInfo.Type != "source" { - return "", errors.New("package not of source type") - } - file, err := os.Open(filename) - if err != nil { - return "", err - } - - tr := tar.NewReader(file) - for { - header, err := tr.Next() - if err == io.EOF { - break - } - if header.Name == "source.sh" { - err := file.Close() - if err != nil { - return "", err - } - err = file.Close() - if err != nil { - return "", err - } - bs, err := io.ReadAll(tr) - if err != nil { - return "", err - } - return string(bs), nil - } - } - return "", errors.New("package does not contain a source.sh file") -} - func (pkgInfo *PackageInfo) GetAllDependencies(checkMake, checkOptional bool) []string { allDepends := make([]string, 0) allDepends = append(allDepends, pkgInfo.Depends...) @@ -1386,7 +1348,7 @@ func IsPackageProvided(pkg, rootDir string) bool { if p == pkg { return true } - i := GetPackageInfo(p, rootDir, true) + i := GetPackageInfo(p, rootDir) if i == nil { continue } @@ -1435,7 +1397,7 @@ func GetPackageFiles(pkg, rootDir string) []string { return ret } -func GetPackageInfo(pkg, rootDir string, defaultValues bool) *PackageInfo { +func GetPackageInfo(pkg, rootDir string) *PackageInfo { installedDir := path.Join(rootDir, "var/lib/bpm/installed/") pkgDir := path.Join(installedDir, pkg) files := path.Join(pkgDir, "info") @@ -1463,7 +1425,7 @@ func GetPackageInfo(pkg, rootDir string, defaultValues bool) *PackageInfo { func RemovePackage(pkg string, verbose bool, rootDir string) error { installedDir := path.Join(rootDir, "var/lib/bpm/installed/") pkgDir := path.Join(installedDir, pkg) - pkgInfo := GetPackageInfo(pkg, rootDir, false) + pkgInfo := GetPackageInfo(pkg, rootDir) if pkgInfo == nil { return errors.New("could not get package info") } From 7816d0072cbee8e571f233654a6213e77449f371 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 15 Oct 2024 10:03:06 +0300 Subject: [PATCH 03/11] Added BPM operation structs which make handling package installation/removal easier and fixed multiple bugs --- main.go | 169 +++++++++++++++++++++-------------------- utils/operations.go | 96 +++++++++++++++++++++++ utils/package_utils.go | 97 ++++++++++++++++------- utils/repo_utils.go | 9 +++ 4 files changed, 261 insertions(+), 110 deletions(-) create mode 100644 utils/operations.go diff --git a/main.go b/main.go index 4bd077f..301a550 100644 --- a/main.go +++ b/main.go @@ -174,12 +174,7 @@ func resolveCommand() { return } - pkgsToInstall := orderedmap.NewOrderedMap[string, *struct { - bpmFile string - isDependency bool - shouldFetch bool - bpmpkg *utils.BPMPackage - }]() + operation := utils.BPMOperation{Actions: make([]utils.OperationAction, 0)} unresolvedDepends := make([]string, 0) // Search for packages @@ -192,12 +187,11 @@ func resolveCommand() { if !reinstall && utils.IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) && utils.GetPackageInfo(bpmpkg.PkgInfo.Name, rootDir).GetFullVersion() == bpmpkg.PkgInfo.GetFullVersion() { continue } - pkgsToInstall.Set(bpmpkg.PkgInfo.Name, &struct { - bpmFile string - isDependency bool - shouldFetch bool - bpmpkg *utils.BPMPackage - }{bpmFile: pkg, isDependency: false, shouldFetch: false, bpmpkg: bpmpkg}) + operation.Actions = append(operation.Actions, &utils.InstallPackageAction{ + File: pkg, + IsDependency: false, + BpmPackage: bpmpkg, + }) } else { entry, _, err := utils.GetRepositoryEntry(pkg) if err != nil { @@ -206,50 +200,44 @@ func resolveCommand() { if !reinstall && utils.IsPackageInstalled(entry.Info.Name, rootDir) && utils.GetPackageInfo(entry.Info.Name, rootDir).GetFullVersion() == entry.Info.GetFullVersion() { continue } - pkgsToInstall.Set(entry.Info.Name, &struct { - bpmFile string - isDependency bool - shouldFetch bool - bpmpkg *utils.BPMPackage - }{bpmFile: "", isDependency: false, shouldFetch: true, bpmpkg: &utils.BPMPackage{ - PkgInfo: entry.Info, - PkgFiles: nil, - }}) + operation.Actions = append(operation.Actions, &utils.FetchPackageAction{ + IsDependency: false, + RepositoryEntry: entry, + }) } } + pos := 0 + for _, value := range slices.Clone(operation.Actions) { + var resolved, unresolved []string + var pkgInfo *utils.PackageInfo + if value.GetActionType() == "install" { + action := value.(*utils.InstallPackageAction) + pkgInfo = action.BpmPackage.PkgInfo + } else if value.GetActionType() == "fetch" { + action := value.(*utils.FetchPackageAction) + pkgInfo = action.RepositoryEntry.Info + } + + resolved, unresolved = pkgInfo.ResolveAll(&[]string{}, &[]string{}, pkgInfo.Type == "source", !noOptional, !reinstallAll, verbose, rootDir) - clone := pkgsToInstall.Copy() - pkgsToInstall = orderedmap.NewOrderedMap[string, *struct { - bpmFile string - isDependency bool - shouldFetch bool - bpmpkg *utils.BPMPackage - }]() - for _, pkg := range clone.Keys() { - value, _ := clone.Get(pkg) - resolved, unresolved := value.bpmpkg.PkgInfo.ResolveAll(&[]string{}, &[]string{}, value.bpmpkg.PkgInfo.Type == "source", !noOptional, !reinstall, verbose, rootDir) unresolvedDepends = append(unresolvedDepends, unresolved...) for _, depend := range resolved { - if _, ok := pkgsToInstall.Get(depend); !ok && depend != value.bpmpkg.PkgInfo.Name { + if !operation.ActionsContainPackage(depend) && depend != pkgInfo.Name { if !reinstallAll && utils.IsPackageInstalled(depend, rootDir) { continue } entry, _, err := utils.GetRepositoryEntry(depend) if err != nil { - log.Fatalf("Error: could not find package (%s) in any repository\n", pkg) + log.Fatalf("Error: could not find package (%s) in any repository\n", pkgInfo.Name) } - pkgsToInstall.Set(depend, &struct { - bpmFile string - isDependency bool - shouldFetch bool - bpmpkg *utils.BPMPackage - }{bpmFile: "", isDependency: true, shouldFetch: true, bpmpkg: &utils.BPMPackage{ - PkgInfo: entry.Info, - PkgFiles: nil, - }}) + operation.InsertActionAt(pos, &utils.FetchPackageAction{ + IsDependency: true, + RepositoryEntry: entry, + }) + pos++ } } - pkgsToInstall.Set(pkg, value) + pos++ } // Show summary @@ -260,18 +248,25 @@ func resolveCommand() { log.Println("Warning: The following dependencies could not be found in any repositories: " + strings.Join(unresolvedDepends, ", ")) } } - if pkgsToInstall.Len() == 0 { + if len(operation.Actions) == 0 { fmt.Println("All packages are up to date!") os.Exit(0) } - var totalOperationSize uint64 = 0 - for _, pkg := range pkgsToInstall.Keys() { - value, _ := pkgsToInstall.Get(pkg) - bpmpkg := value.bpmpkg - installedInfo := utils.GetPackageInfo(bpmpkg.PkgInfo.Name, rootDir) + for _, value := range operation.Actions { + var pkgInfo *utils.PackageInfo + if value.GetActionType() == "install" { + pkgInfo = value.(*utils.InstallPackageAction).BpmPackage.PkgInfo + } else if value.GetActionType() == "fetch" { + pkgInfo = value.(*utils.FetchPackageAction).RepositoryEntry.Info + } else { + pkgInfo = value.(*utils.RemovePackageAction).BpmPackage.PkgInfo + fmt.Printf("%s: %s (Remove)\n", pkgInfo.Name, pkgInfo.GetFullVersion()) + } + + installedInfo := utils.GetPackageInfo(pkgInfo.Name, rootDir) sourceInfo := "" - if bpmpkg.PkgInfo.Type == "source" { + if pkgInfo.Type == "source" { if rootDir != "/" && !force { log.Fatalf("Error: cannot compile and install source packages to a different root directory") } @@ -279,35 +274,39 @@ func resolveCommand() { } if installedInfo == nil { - fmt.Printf("%s: %s (Install) %s\n", bpmpkg.PkgInfo.Name, bpmpkg.PkgInfo.GetFullVersion(), sourceInfo) - totalOperationSize += bpmpkg.GetInstalledSize() + fmt.Printf("%s: %s (Install) %s\n", pkgInfo.Name, pkgInfo.GetFullVersion(), sourceInfo) } else { - comparison := utils.ComparePackageVersions(*bpmpkg.PkgInfo, *installedInfo) + comparison := utils.ComparePackageVersions(*pkgInfo, *installedInfo) if comparison < 0 { - fmt.Printf("%s: %s -> %s (Downgrade) %s\n", bpmpkg.PkgInfo.Name, installedInfo.GetFullVersion(), bpmpkg.PkgInfo.GetFullVersion(), sourceInfo) + fmt.Printf("%s: %s -> %s (Downgrade) %s\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), sourceInfo) } else if comparison > 0 { - fmt.Printf("%s: %s -> %s (Upgrade) %s\n", bpmpkg.PkgInfo.Name, installedInfo.GetFullVersion(), bpmpkg.PkgInfo.GetFullVersion(), sourceInfo) + fmt.Printf("%s: %s -> %s (Upgrade) %s\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), sourceInfo) } else { - fmt.Printf("%s: %s (Reinstall) %s\n", bpmpkg.PkgInfo.Name, bpmpkg.PkgInfo.GetFullVersion(), sourceInfo) + fmt.Printf("%s: %s (Reinstall) %s\n", pkgInfo.Name, pkgInfo.GetFullVersion(), sourceInfo) } - totalOperationSize += bpmpkg.GetInstalledSize() } } + if rootDir != "/" { fmt.Println("Warning: Operating in " + rootDir) } - if totalOperationSize >= 0 { - fmt.Printf("A total of %s will be used to complete this operation\n", utils.BytesToHumanReadable(totalOperationSize)) + if operation.GetTotalDownloadSize() > 0 { + fmt.Printf("%s will be downloaded to complete this operation\n", utils.BytesToHumanReadable(operation.GetTotalDownloadSize())) + } + if operation.GetFinalActionSize(rootDir) > 0 { + fmt.Printf("A total of %s will be installed after the operation finishes\n", utils.BytesToHumanReadable(operation.GetFinalActionSize(rootDir))) + } else if operation.GetFinalActionSize(rootDir) < 0 { + fmt.Printf("A total of %s will be freed after the operation finishes\n", utils.BytesToHumanReadable(operation.GetFinalActionSize(rootDir))) } else { - fmt.Printf("A total of %s will be freed by this operation\n", utils.BytesToHumanReadable(totalOperationSize)) + fmt.Println() } if !yesAll { reader := bufio.NewReader(os.Stdin) - if pkgsToInstall.Len() == 1 { + if len(operation.Actions) == 1 { fmt.Printf("Do you wish to install this package? [y\\N] ") } else { - fmt.Printf("Do you wish to install these %d packages? [y\\N] ", pkgsToInstall.Len()) + fmt.Printf("Do you wish to install these %d packages? [y\\N] ", len(operation.Actions)) } text, _ := reader.ReadString('\n') @@ -319,43 +318,49 @@ func resolveCommand() { // Fetch packages from repositories fmt.Println("Fetching packages from available repositories...") - for _, pkg := range pkgsToInstall.Keys() { - value, _ := pkgsToInstall.Get(pkg) - if !value.shouldFetch { + for i, action := range operation.Actions { + if action.GetActionType() != "fetch" { continue } - entry, repo, err := utils.GetRepositoryEntry(pkg) + entry := action.(*utils.FetchPackageAction).RepositoryEntry + fetchedPackage, err := entry.Repository.FetchPackage(entry.Info.Name) if err != nil { - log.Fatalf("Error: could not find package (%s) in any repository\n", pkg) + log.Fatalf("Error: could not fetch package (%s): %s\n", entry.Info.Name, err) } - fetchedPackage, err := repo.FetchPackage(entry.Info.Name) + bpmpkg, err := utils.ReadPackage(fetchedPackage) if err != nil { - log.Fatalf("Error: could not fetch package (%s): %s\n", pkg, err) + log.Fatalf("Error: could not fetch package (%s): %s\n", entry.Info.Name, err) + } + fmt.Printf("Package (%s) was successfully fetched!\n", bpmpkg.PkgInfo.Name) + operation.Actions[i] = &utils.InstallPackageAction{ + File: fetchedPackage, + IsDependency: action.(*utils.FetchPackageAction).IsDependency, + BpmPackage: bpmpkg, } - fmt.Printf("Package (%s) was successfully fetched!\n", value.bpmpkg.PkgInfo.Name) - value.bpmFile = fetchedPackage - pkgsToInstall.Set(pkg, value) } // Install fetched packages - for _, pkg := range pkgsToInstall.Keys() { - value, _ := pkgsToInstall.Get(pkg) - bpmpkg := value.bpmpkg + for _, action := range operation.Actions { + if action.GetActionType() != "install" { + continue + } + value := action.(*utils.InstallPackageAction) + bpmpkg := value.BpmPackage var err error - if value.isDependency { - err = utils.InstallPackage(value.bpmFile, rootDir, verbose, true, buildSource, skipCheck, keepTempDir) + if value.IsDependency { + err = utils.InstallPackage(value.File, rootDir, verbose, true, buildSource, skipCheck, keepTempDir) } else { - err = utils.InstallPackage(value.bpmFile, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) + err = utils.InstallPackage(value.File, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) } if err != nil { if bpmpkg.PkgInfo.Type == "source" && keepTempDir { fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + bpmpkg.PkgInfo.Name) } - log.Fatalf("Error: could not install package (%s): %s\n", pkg, err) + log.Fatalf("Error: could not install package (%s): %s\n", bpmpkg.PkgInfo.Name, err) } fmt.Printf("Package (%s) was successfully installed\n", bpmpkg.PkgInfo.Name) - if value.isDependency { + if value.IsDependency { err := utils.SetInstallationReason(bpmpkg.PkgInfo.Name, utils.Dependency, rootDir) if err != nil { log.Fatalf("Error: could not set installation reason for package: %s\n", err) @@ -620,7 +625,9 @@ func resolveCommand() { var pkgList []string for _, pkg := range pkgs { - if slices.Contains(utils.GetPackageFiles(pkg, rootDir), absFile) { + if slices.ContainsFunc(utils.GetPackageFiles(pkg, rootDir), func(entry *utils.PackageFileEntry) bool { + return entry.Path == absFile + }) { pkgList = append(pkgList, pkg) } } diff --git a/utils/operations.go b/utils/operations.go new file mode 100644 index 0000000..7a00ad1 --- /dev/null +++ b/utils/operations.go @@ -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" +} diff --git a/utils/package_utils.go b/utils/package_utils.go index 0459bc0..e5d5afe 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -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 diff --git a/utils/repo_utils.go b/utils/repo_utils.go index d28ae00..28fdaeb 100644 --- a/utils/repo_utils.go +++ b/utils/repo_utils.go @@ -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 } From 1aec288013caa346dc7ba2b0d35cdccd0843a4d7 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 15 Oct 2024 10:12:49 +0300 Subject: [PATCH 04/11] Added octal permissions to BPMFileEntry struct --- utils/package_utils.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/utils/package_utils.go b/utils/package_utils.go index e5d5afe..a775c27 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -44,6 +44,7 @@ type PackageInfo struct { type PackageFileEntry struct { Path string + OctalPerms uint32 UserID int GroupID int SizeInBytes uint64 @@ -179,9 +180,13 @@ func ReadPackage(filename string) (*BPMPackage, error) { continue } stringEntry := strings.Split(strings.TrimSpace(line), " ") - if len(stringEntry) < 4 { + if len(stringEntry) < 5 { return nil, errors.New("pkg.files is not formatted correctly") } + octalPerms, err := strconv.ParseUint(stringEntry[len(stringEntry)-4], 8, 32) + if err != nil { + return nil, err + } uid, err := strconv.ParseInt(stringEntry[len(stringEntry)-3], 0, 32) if err != nil { return nil, err @@ -195,7 +200,8 @@ func ReadPackage(filename string) (*BPMPackage, error) { return nil, err } pkgFiles = append(pkgFiles, &PackageFileEntry{ - Path: strings.Join(stringEntry[:len(stringEntry)-3], " "), + Path: strings.Join(stringEntry[:len(stringEntry)-4], " "), + OctalPerms: uint32(octalPerms), UserID: int(uid), GroupID: int(gid), SizeInBytes: size, @@ -1393,16 +1399,21 @@ func GetPackageFiles(pkg, rootDir string) []*PackageFileEntry { continue } stringEntry := strings.Split(strings.TrimSpace(line), " ") - if len(stringEntry) < 4 { + if len(stringEntry) < 5 { pkgFiles = append(pkgFiles, &PackageFileEntry{ Path: line, + OctalPerms: 0, UserID: 0, GroupID: 0, SizeInBytes: 0, }) continue } - uid, err := strconv.ParseInt(stringEntry[len(stringEntry)-3], 0, 32) + uid, err := strconv.ParseInt(stringEntry[len(stringEntry)-4], 0, 32) + if err != nil { + return nil + } + octalPerms, err := strconv.ParseInt(stringEntry[len(stringEntry)-3], 0, 32) if err != nil { return nil } @@ -1415,7 +1426,8 @@ func GetPackageFiles(pkg, rootDir string) []*PackageFileEntry { return nil } pkgFiles = append(pkgFiles, &PackageFileEntry{ - Path: strings.Join(stringEntry[:len(stringEntry)-3], " "), + Path: strings.Join(stringEntry[:len(stringEntry)-4], " "), + OctalPerms: uint32(octalPerms), UserID: int(uid), GroupID: int(gid), SizeInBytes: size, From ef16fa8195c66c00ea59d0db0fbef7331315b39b Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 15 Oct 2024 14:34:01 +0300 Subject: [PATCH 05/11] Improve tarball single file reading --- utils/extract_utils.go | 17 ++++++++++------- utils/package_utils.go | 20 +++++++++++--------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/utils/extract_utils.go b/utils/extract_utils.go index 636fde1..6c51928 100644 --- a/utils/extract_utils.go +++ b/utils/extract_utils.go @@ -7,12 +7,16 @@ import ( "os" ) -func ReadTarballContent(tarballPath, fileToExtract string) ([]byte, error) { +type TarballFileReader struct { + tarReader *tar.Reader + file *os.File +} + +func ReadTarballContent(tarballPath, fileToExtract string) (*TarballFileReader, error) { file, err := os.Open(tarballPath) if err != nil { return nil, err } - defer file.Close() tr := tar.NewReader(file) for { @@ -28,11 +32,10 @@ func ReadTarballContent(tarballPath, fileToExtract string) ([]byte, error) { return nil, errors.New("file to extract must be a regular file") } - bytes, err := io.ReadAll(tr) - if err != nil { - return nil, err - } - return bytes, nil + return &TarballFileReader{ + tarReader: tr, + file: file, + }, nil } } diff --git a/utils/package_utils.go b/utils/package_utils.go index a775c27..4ec43f6 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -2,7 +2,6 @@ package utils import ( "archive/tar" - "bytes" "compress/gzip" "errors" "fmt" @@ -488,18 +487,19 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) return err, nil } - content, err := ReadTarballContent(filename, "files.tar.gz") + tarballFile, err := ReadTarballContent(filename, "files.tar.gz") if err != nil { return err, nil } + defer tarballFile.file.Close() - archive, err := gzip.NewReader(bytes.NewReader(content)) + archive, err := gzip.NewReader(tarballFile.tarReader) if err != nil { return err, nil } - tr := tar.NewReader(archive) + packageFilesReader := tar.NewReader(archive) for { - header, err := tr.Next() + header, err := packageFilesReader.Next() if err == io.EOF { break } @@ -559,7 +559,7 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) if err != nil { return err, nil } - if _, err := io.Copy(outFile, tr); err != nil { + if _, err := io.Copy(outFile, packageFilesReader); err != nil { return err, nil } if err := os.Chmod(extractFilename, header.FileInfo().Mode()); err != nil { @@ -1119,12 +1119,14 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, if err != nil { return err } - bs, err := ReadTarballContent(filename, "pkg.files") + + tarballFile, err := ReadTarballContent(filename, "pkg.files") if err != nil { return err } - _, err = f.Write(bs) - err = f.Close() + defer tarballFile.file.Close() + + _, err = io.Copy(f, tarballFile.tarReader) if err != nil { return err } From a7b025d92dc2fd69689210262969a4f4984c71de Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 15 Oct 2024 14:34:32 +0300 Subject: [PATCH 06/11] Removed additional empty line in output --- main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.go b/main.go index 301a550..4ce677b 100644 --- a/main.go +++ b/main.go @@ -297,8 +297,6 @@ func resolveCommand() { fmt.Printf("A total of %s will be installed after the operation finishes\n", utils.BytesToHumanReadable(operation.GetFinalActionSize(rootDir))) } else if operation.GetFinalActionSize(rootDir) < 0 { fmt.Printf("A total of %s will be freed after the operation finishes\n", utils.BytesToHumanReadable(operation.GetFinalActionSize(rootDir))) - } else { - fmt.Println() } if !yesAll { From 918ff984ca350c28dfd3d8ab6b6619d071006ed6 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Thu, 17 Oct 2024 11:54:32 +0300 Subject: [PATCH 07/11] Moved package fetching, installation and removal functionality to operations.go --- main.go | 357 ++++++++++------------------------------- utils/general_utils.go | 13 +- utils/operations.go | 201 ++++++++++++++++++++++- utils/package_utils.go | 17 +- 4 files changed, 294 insertions(+), 294 deletions(-) diff --git a/main.go b/main.go index 4ce677b..471ccf0 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "bufio" "flag" "fmt" - "github.com/elliotchance/orderedmap/v2" "gitlab.com/bubble-package-manager/bpm/utils" "log" "os" @@ -174,8 +173,11 @@ func resolveCommand() { return } - operation := utils.BPMOperation{Actions: make([]utils.OperationAction, 0)} - unresolvedDepends := make([]string, 0) + operation := utils.BPMOperation{ + Actions: make([]utils.OperationAction, 0), + UnresolvedDepends: make([]string, 0), + RootDir: rootDir, + } // Search for packages for _, pkg := range pkgs { @@ -206,99 +208,24 @@ func resolveCommand() { }) } } - pos := 0 - for _, value := range slices.Clone(operation.Actions) { - var resolved, unresolved []string - var pkgInfo *utils.PackageInfo - if value.GetActionType() == "install" { - action := value.(*utils.InstallPackageAction) - pkgInfo = action.BpmPackage.PkgInfo - } else if value.GetActionType() == "fetch" { - action := value.(*utils.FetchPackageAction) - pkgInfo = action.RepositoryEntry.Info - } - resolved, unresolved = pkgInfo.ResolveAll(&[]string{}, &[]string{}, pkgInfo.Type == "source", !noOptional, !reinstallAll, verbose, rootDir) - - unresolvedDepends = append(unresolvedDepends, unresolved...) - for _, depend := range resolved { - if !operation.ActionsContainPackage(depend) && depend != pkgInfo.Name { - if !reinstallAll && utils.IsPackageInstalled(depend, rootDir) { - continue - } - entry, _, err := utils.GetRepositoryEntry(depend) - if err != nil { - log.Fatalf("Error: could not find package (%s) in any repository\n", pkgInfo.Name) - } - operation.InsertActionAt(pos, &utils.FetchPackageAction{ - IsDependency: true, - RepositoryEntry: entry, - }) - pos++ - } - } - pos++ + // Resolve dependencies + err := operation.ResolveDependencies(reinstallAll, !noOptional, verbose) + if err != nil { + log.Fatalf("Error: could not resolve dependencies: %s\n", err) } - - // Show summary - if len(unresolvedDepends) != 0 { + if len(operation.UnresolvedDepends) != 0 { if !force { - log.Fatalf("Error: the following dependencies could not be found in any repositories: %s\n", strings.Join(unresolvedDepends, ", ")) + log.Fatalf("Error: the following dependencies could not be found in any repositories: %s\n", strings.Join(operation.UnresolvedDepends, ", ")) } else { - log.Println("Warning: The following dependencies could not be found in any repositories: " + strings.Join(unresolvedDepends, ", ")) - } - } - if len(operation.Actions) == 0 { - fmt.Println("All packages are up to date!") - os.Exit(0) - } - - for _, value := range operation.Actions { - var pkgInfo *utils.PackageInfo - if value.GetActionType() == "install" { - pkgInfo = value.(*utils.InstallPackageAction).BpmPackage.PkgInfo - } else if value.GetActionType() == "fetch" { - pkgInfo = value.(*utils.FetchPackageAction).RepositoryEntry.Info - } else { - pkgInfo = value.(*utils.RemovePackageAction).BpmPackage.PkgInfo - fmt.Printf("%s: %s (Remove)\n", pkgInfo.Name, pkgInfo.GetFullVersion()) - } - - installedInfo := utils.GetPackageInfo(pkgInfo.Name, rootDir) - sourceInfo := "" - if pkgInfo.Type == "source" { - if rootDir != "/" && !force { - log.Fatalf("Error: cannot compile and install source packages to a different root directory") - } - sourceInfo = "(From Source)" - } - - if installedInfo == nil { - fmt.Printf("%s: %s (Install) %s\n", pkgInfo.Name, pkgInfo.GetFullVersion(), sourceInfo) - } else { - comparison := utils.ComparePackageVersions(*pkgInfo, *installedInfo) - if comparison < 0 { - fmt.Printf("%s: %s -> %s (Downgrade) %s\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), sourceInfo) - } else if comparison > 0 { - fmt.Printf("%s: %s -> %s (Upgrade) %s\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), sourceInfo) - } else { - fmt.Printf("%s: %s (Reinstall) %s\n", pkgInfo.Name, pkgInfo.GetFullVersion(), sourceInfo) - } + log.Println("Warning: The following dependencies could not be found in any repositories: " + strings.Join(operation.UnresolvedDepends, ", ")) } } - if rootDir != "/" { - fmt.Println("Warning: Operating in " + rootDir) - } - if operation.GetTotalDownloadSize() > 0 { - fmt.Printf("%s will be downloaded to complete this operation\n", utils.BytesToHumanReadable(operation.GetTotalDownloadSize())) - } - if operation.GetFinalActionSize(rootDir) > 0 { - fmt.Printf("A total of %s will be installed after the operation finishes\n", utils.BytesToHumanReadable(operation.GetFinalActionSize(rootDir))) - } else if operation.GetFinalActionSize(rootDir) < 0 { - fmt.Printf("A total of %s will be freed after the operation finishes\n", utils.BytesToHumanReadable(operation.GetFinalActionSize(rootDir))) - } + // Show operation summary + operation.ShowOperationSummary() + // Confirmation Prompt if !yesAll { reader := bufio.NewReader(os.Stdin) if len(operation.Actions) == 1 { @@ -309,64 +236,15 @@ func resolveCommand() { text, _ := reader.ReadString('\n') if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { - fmt.Println("Cancelling...") + fmt.Println("Cancelling package installation...") os.Exit(1) } } - // Fetch packages from repositories - fmt.Println("Fetching packages from available repositories...") - for i, action := range operation.Actions { - if action.GetActionType() != "fetch" { - continue - } - entry := action.(*utils.FetchPackageAction).RepositoryEntry - fetchedPackage, err := entry.Repository.FetchPackage(entry.Info.Name) - if err != nil { - log.Fatalf("Error: could not fetch package (%s): %s\n", entry.Info.Name, err) - } - bpmpkg, err := utils.ReadPackage(fetchedPackage) - if err != nil { - log.Fatalf("Error: could not fetch package (%s): %s\n", entry.Info.Name, err) - } - fmt.Printf("Package (%s) was successfully fetched!\n", bpmpkg.PkgInfo.Name) - operation.Actions[i] = &utils.InstallPackageAction{ - File: fetchedPackage, - IsDependency: action.(*utils.FetchPackageAction).IsDependency, - BpmPackage: bpmpkg, - } - } - - // Install fetched packages - for _, action := range operation.Actions { - if action.GetActionType() != "install" { - continue - } - value := action.(*utils.InstallPackageAction) - bpmpkg := value.BpmPackage - var err error - if value.IsDependency { - err = utils.InstallPackage(value.File, rootDir, verbose, true, buildSource, skipCheck, keepTempDir) - } else { - err = utils.InstallPackage(value.File, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) - } - - if err != nil { - if bpmpkg.PkgInfo.Type == "source" && keepTempDir { - fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + bpmpkg.PkgInfo.Name) - } - log.Fatalf("Error: could not install package (%s): %s\n", bpmpkg.PkgInfo.Name, err) - } - fmt.Printf("Package (%s) was successfully installed\n", bpmpkg.PkgInfo.Name) - if value.IsDependency { - err := utils.SetInstallationReason(bpmpkg.PkgInfo.Name, utils.Dependency, rootDir) - if err != nil { - log.Fatalf("Error: could not set installation reason for package: %s\n", err) - } - } - if bpmpkg.PkgInfo.Type == "source" && keepTempDir { - fmt.Println("** It is recommended you delete the temporary bpm folder in /var/tmp **") - } + // Execute operation + err = operation.Execute(verbose, force) + if err != nil { + log.Fatalf("Error: could not complete operation: %s\n", err) } case update: if os.Getuid() != 0 { @@ -392,10 +270,14 @@ func resolveCommand() { if err != nil { log.Fatalf("Error: could not get installed packages: %s\n", err) } - toUpdate := orderedmap.NewOrderedMap[string, *struct { - isDependency bool - entry *utils.RepositoryEntry - }]() + + operation := utils.BPMOperation{ + Actions: make([]utils.OperationAction, 0), + UnresolvedDepends: make([]string, 0), + RootDir: rootDir, + } + + // Search for packages for _, pkg := range pkgs { entry, _, err := utils.GetRepositoryEntry(pkg) if err != nil { @@ -406,131 +288,46 @@ func resolveCommand() { log.Fatalf("Error: could not get package info for (%s)\n", pkg) } else { comparison := utils.ComparePackageVersions(*entry.Info, *installedInfo) - if comparison > 0 { - toUpdate.Set(entry.Info.Name, &struct { - isDependency bool - entry *utils.RepositoryEntry - }{isDependency: false, entry: entry}) - } else if reinstall { - toUpdate.Set(entry.Info.Name, &struct { - isDependency bool - entry *utils.RepositoryEntry - }{isDependency: false, entry: entry}) + if comparison > 0 || reinstall { + operation.Actions = append(operation.Actions, &utils.FetchPackageAction{ + IsDependency: false, + RepositoryEntry: entry, + }) } } } - if toUpdate.Len() == 0 { - fmt.Println("All packages are up to date!") - os.Exit(0) - } // Check for new dependencies in updated packages - unresolved := make([]string, 0) - clone := toUpdate.Copy() - for _, key := range clone.Keys() { - pkg, _ := clone.Get(key) - r, u := pkg.entry.Info.ResolveAll(&[]string{}, &[]string{}, pkg.entry.Info.Type == "source", !noOptional, true, verbose, rootDir) - unresolved = append(unresolved, u...) - for _, depend := range r { - if _, ok := toUpdate.Get(depend); !ok { - entry, _, err := utils.GetRepositoryEntry(depend) - if err != nil { - log.Fatalf("Error: could not find package (%s) in any repository\n", depend) - } - toUpdate.Set(depend, &struct { - isDependency bool - entry *utils.RepositoryEntry - }{isDependency: true, entry: entry}) - } - } + err = operation.ResolveDependencies(reinstallAll, !noOptional, verbose) + if err != nil { + log.Fatalf("Error: could not resolve dependencies: %s\n", err) } - - if len(unresolved) != 0 { + if len(operation.UnresolvedDepends) != 0 { if !force { - log.Fatalf("Error: the following dependencies could not be found in any repositories: %s\n", strings.Join(unresolved, ", ")) + log.Fatalf("Error: the following dependencies could not be found in any repositories: %s\n", strings.Join(operation.UnresolvedDepends, ", ")) } else { - log.Printf("Warning: the following dependencies could not be found in any repositories: %s\n", strings.Join(unresolved, ", ")) + log.Println("Warning: The following dependencies could not be found in any repositories: " + strings.Join(operation.UnresolvedDepends, ", ")) } } - for _, key := range toUpdate.Keys() { - value, _ := toUpdate.Get(key) - installedInfo := utils.GetPackageInfo(value.entry.Info.Name, rootDir) - sourceInfo := "" - if value.entry.Info.Type == "source" { - sourceInfo = "(From Source)" - } - if installedInfo == nil { - fmt.Printf("%s: %s (Install) %s\n", value.entry.Info.Name, value.entry.Info.GetFullVersion(), sourceInfo) - continue - } else { - comparison := utils.ComparePackageVersions(*value.entry.Info, *installedInfo) - if comparison > 0 { - fmt.Printf("%s: %s -> %s (Upgrade) %s\n", value.entry.Info.Name, installedInfo.GetFullVersion(), value.entry.Info.GetFullVersion(), sourceInfo) - } else if reinstall { - fmt.Printf("%s: %s -> %s (Reinstall) %s\n", value.entry.Info.Name, installedInfo.GetFullVersion(), value.entry.Info.GetFullVersion(), sourceInfo) - } - } - } + // Show operation summary + operation.ShowOperationSummary() - // Update confirmation prompt + // Confirmation Prompt if !yesAll { - fmt.Printf("Are you sure you wish to update all %d packages? [y\\N] ", toUpdate.Len()) + fmt.Printf("Are you sure you wish to update all %d packages? [y\\N] ", len(operation.Actions)) reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString('\n') if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { - fmt.Println("Cancelling update...") + fmt.Println("Cancelling package update...") os.Exit(1) } } - // Fetch packages - pkgsToInstall := orderedmap.NewOrderedMap[string, *struct { - isDependency bool - entry *utils.RepositoryEntry - }]() - fmt.Println("Fetching packages from available repositories...") - for _, pkg := range toUpdate.Keys() { - value, _ := toUpdate.Get(pkg) - entry, repo, err := utils.GetRepositoryEntry(pkg) - if err != nil { - log.Fatalf("Error: could not find package (%s) in any repository\n", pkg) - } - fetchedPackage, err := repo.FetchPackage(entry.Info.Name) - if err != nil { - log.Fatalf("Error: could not fetch package (%s): %s\n", pkg, err) - } - fmt.Printf("Package (%s) was successfully fetched!\n", value.entry.Info.Name) - pkgsToInstall.Set(fetchedPackage, value) - } - - // Install fetched packages - for _, pkg := range pkgsToInstall.Keys() { - value, _ := pkgsToInstall.Get(pkg) - pkgInfo := value.entry.Info - var err error - if value.isDependency { - err = utils.InstallPackage(pkg, rootDir, verbose, true, buildSource, skipCheck, keepTempDir) - } else { - err = utils.InstallPackage(pkg, rootDir, verbose, force, buildSource, skipCheck, keepTempDir) - } - - if err != nil { - if pkgInfo.Type == "source" && keepTempDir { - fmt.Println("BPM temp directory was created at /var/tmp/bpm_source-" + pkgInfo.Name) - } - log.Fatalf("Error: could not install package (%s): %s\n", pkg, err) - } - fmt.Printf("Package (%s) was successfully installed!\n", pkgInfo.Name) - if value.isDependency { - err := utils.SetInstallationReason(pkgInfo.Name, utils.Dependency, rootDir) - if err != nil { - log.Fatalf("Error: could not set installation reason for package: %s\n", err) - } - } - if pkgInfo.Type == "source" && keepTempDir { - fmt.Println("** It is recommended you delete the temporary bpm folder in /var/tmp **") - } + // Execute operation + err = operation.Execute(verbose, force) + if err != nil { + log.Fatalf("Error: could not complete operation: %s\n", err) } case sync: if os.Getuid() != 0 { @@ -541,7 +338,7 @@ func resolveCommand() { reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString('\n') if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { - fmt.Println("Cancelling sync...") + fmt.Println("Cancelling database synchronization...") os.Exit(1) } } @@ -562,32 +359,40 @@ func resolveCommand() { fmt.Println("No packages were given") return } - for _, pkg := range packages { - pkgInfo := utils.GetPackageInfo(pkg, rootDir) - if pkgInfo == nil { - fmt.Printf("Package (%s) could not be found\n", pkg) - continue - } - fmt.Println("----------------\n" + utils.CreateReadableInfo(false, false, false, pkgInfo, rootDir)) - fmt.Println("----------------") - if rootDir != "/" { - fmt.Println("Warning: Operating in " + rootDir) - } - if !yesAll { - reader := bufio.NewReader(os.Stdin) - fmt.Print("Do you wish to remove this package? [y\\N] ") - text, _ := reader.ReadString('\n') - if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { - fmt.Printf("Skipping package (%s)...\n", pkgInfo.Name) - continue - } - } - err := utils.RemovePackage(pkg, verbose, rootDir) - if err != nil { - log.Fatalf("Error: could not remove package: %s\n", err) + operation := &utils.BPMOperation{ + Actions: make([]utils.OperationAction, 0), + UnresolvedDepends: make([]string, 0), + RootDir: rootDir, + } + + // Search for packages + for _, pkg := range packages { + bpmpkg := utils.GetPackage(pkg, rootDir) + if bpmpkg == nil { + log.Fatalf("Error: package (%s) could not be found\n", pkg) } - fmt.Printf("Package (%s) was successfully removed!\n", pkgInfo.Name) + operation.Actions = append(operation.Actions, &utils.RemovePackageAction{BpmPackage: bpmpkg}) + } + + // Show operation summary + operation.ShowOperationSummary() + + // Confirmation Prompt + if !yesAll { + fmt.Printf("Are you sure you wish to remove all %d packages? [y\\N] ", len(operation.Actions)) + reader := bufio.NewReader(os.Stdin) + text, _ := reader.ReadString('\n') + if strings.TrimSpace(strings.ToLower(text)) != "y" && strings.TrimSpace(strings.ToLower(text)) != "yes" { + fmt.Println("Cancelling package removal...") + os.Exit(1) + } + } + + // Execute operation + err := operation.Execute(verbose, force) + if err != nil { + log.Fatalf("Error: could not complete operation: %s\n", err) } case file: files := subcommandArgs diff --git a/utils/general_utils.go b/utils/general_utils.go index 51ce30b..18fd718 100644 --- a/utils/general_utils.go +++ b/utils/general_utils.go @@ -49,7 +49,18 @@ func stringSliceRemove(s []string, r string) []string { return s } -func BytesToHumanReadable(b uint64) string { +func UnsignedBytesToHumanReadable(b uint64) string { + bf := float64(b) + for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} { + if math.Abs(bf) < 1024.0 { + return fmt.Sprintf("%3.1f%sB", bf, unit) + } + bf /= 1024.0 + } + return fmt.Sprintf("%.1fYiB", bf) +} + +func BytesToHumanReadable(b int64) string { bf := float64(b) for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} { if math.Abs(bf) < 1024.0 { diff --git a/utils/operations.go b/utils/operations.go index 7a00ad1..021c060 100644 --- a/utils/operations.go +++ b/utils/operations.go @@ -1,7 +1,18 @@ package utils +import ( + "errors" + "fmt" + "log" + "os" + "slices" + "strings" +) + type BPMOperation struct { - Actions []OperationAction + Actions []OperationAction + UnresolvedDepends []string + RootDir string } func (operation *BPMOperation) ActionsContainPackage(pkg string) bool { @@ -47,23 +58,199 @@ func (operation *BPMOperation) GetTotalInstalledSize() uint64 { return ret } -func (operation *BPMOperation) GetFinalActionSize(rootDir string) uint64 { - var ret uint64 = 0 +func (operation *BPMOperation) GetFinalActionSize(rootDir string) int64 { + var ret int64 = 0 for _, action := range operation.Actions { if action.GetActionType() == "install" { - ret += action.(*InstallPackageAction).BpmPackage.GetInstalledSize() + ret += int64(action.(*InstallPackageAction).BpmPackage.GetInstalledSize()) if IsPackageInstalled(action.(*InstallPackageAction).BpmPackage.PkgInfo.Name, rootDir) { - ret -= GetPackage(action.(*InstallPackageAction).BpmPackage.PkgInfo.Name, rootDir).GetInstalledSize() + ret -= int64(GetPackage(action.(*InstallPackageAction).BpmPackage.PkgInfo.Name, rootDir).GetInstalledSize()) } } else if action.GetActionType() == "fetch" { - ret += action.(*FetchPackageAction).RepositoryEntry.InstalledSize + ret += int64(action.(*FetchPackageAction).RepositoryEntry.InstalledSize) } else if action.GetActionType() == "remove" { - ret -= action.(*RemovePackageAction).BpmPackage.GetInstalledSize() + ret -= int64(action.(*RemovePackageAction).BpmPackage.GetInstalledSize()) } } return ret } +func (operation *BPMOperation) ResolveDependencies(reinstallDependencies, installOptionalDependencies, verbose bool) error { + pos := 0 + for _, value := range slices.Clone(operation.Actions) { + var pkgInfo *PackageInfo + if value.GetActionType() == "install" { + action := value.(*InstallPackageAction) + pkgInfo = action.BpmPackage.PkgInfo + } else if value.GetActionType() == "fetch" { + action := value.(*FetchPackageAction) + pkgInfo = action.RepositoryEntry.Info + } else { + pos++ + continue + } + + resolved, unresolved := pkgInfo.ResolveDependencies(&[]string{}, &[]string{}, pkgInfo.Type == "source", installOptionalDependencies, !reinstallDependencies, verbose, operation.RootDir) + + operation.UnresolvedDepends = append(operation.UnresolvedDepends, unresolved...) + + for _, depend := range resolved { + if !operation.ActionsContainPackage(depend) && depend != pkgInfo.Name { + if !reinstallDependencies && IsPackageInstalled(depend, operation.RootDir) { + continue + } + entry, _, err := GetRepositoryEntry(depend) + if err != nil { + return errors.New("could not get repository entry for package (" + depend + ")") + } + operation.InsertActionAt(pos, &FetchPackageAction{ + IsDependency: true, + RepositoryEntry: entry, + }) + pos++ + } + } + pos++ + } + + return nil +} + +func (operation *BPMOperation) ShowOperationSummary() { + if len(operation.Actions) == 0 { + fmt.Println("All packages are up to date!") + os.Exit(0) + } + + for _, value := range operation.Actions { + var pkgInfo *PackageInfo + if value.GetActionType() == "install" { + pkgInfo = value.(*InstallPackageAction).BpmPackage.PkgInfo + } else if value.GetActionType() == "fetch" { + pkgInfo = value.(*FetchPackageAction).RepositoryEntry.Info + } else { + pkgInfo = value.(*RemovePackageAction).BpmPackage.PkgInfo + fmt.Printf("%s: %s (Remove)\n", pkgInfo.Name, pkgInfo.GetFullVersion()) + continue + } + + installedInfo := GetPackageInfo(pkgInfo.Name, operation.RootDir) + sourceInfo := "" + if pkgInfo.Type == "source" { + if operation.RootDir != "/" { + log.Fatalf("cannot compile and install source packages to a different root directory") + } + sourceInfo = "(From Source)" + } + + if installedInfo == nil { + fmt.Printf("%s: %s (Install) %s\n", pkgInfo.Name, pkgInfo.GetFullVersion(), sourceInfo) + } else { + comparison := ComparePackageVersions(*pkgInfo, *installedInfo) + if comparison < 0 { + fmt.Printf("%s: %s -> %s (Downgrade) %s\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), sourceInfo) + } else if comparison > 0 { + fmt.Printf("%s: %s -> %s (Upgrade) %s\n", pkgInfo.Name, installedInfo.GetFullVersion(), pkgInfo.GetFullVersion(), sourceInfo) + } else { + fmt.Printf("%s: %s (Reinstall) %s\n", pkgInfo.Name, pkgInfo.GetFullVersion(), sourceInfo) + } + } + } + + if operation.RootDir != "/" { + fmt.Println("Warning: Operating in " + operation.RootDir) + } + if operation.GetTotalDownloadSize() > 0 { + fmt.Printf("%s will be downloaded to complete this operation\n", UnsignedBytesToHumanReadable(operation.GetTotalDownloadSize())) + } + if operation.GetFinalActionSize(operation.RootDir) > 0 { + fmt.Printf("A total of %s will be installed after the operation finishes\n", BytesToHumanReadable(operation.GetFinalActionSize(operation.RootDir))) + } else if operation.GetFinalActionSize(operation.RootDir) < 0 { + fmt.Printf("A total of %s will be freed after the operation finishes\n", strings.TrimPrefix(BytesToHumanReadable(operation.GetFinalActionSize(operation.RootDir)), "-")) + } +} + +func (operation *BPMOperation) Execute(verbose, force bool) error { + // Fetch packages from repositories + if slices.ContainsFunc(operation.Actions, func(action OperationAction) bool { + return action.GetActionType() == "fetch" + }) { + fmt.Println("Fetching packages from available repositories...") + for i, action := range operation.Actions { + if action.GetActionType() != "fetch" { + continue + } + entry := action.(*FetchPackageAction).RepositoryEntry + fetchedPackage, err := entry.Repository.FetchPackage(entry.Info.Name) + if err != nil { + return errors.New(fmt.Sprintf("could not fetch package (%s): %s\n", entry.Info.Name, err)) + } + bpmpkg, err := ReadPackage(fetchedPackage) + if err != nil { + return errors.New(fmt.Sprintf("could not fetch package (%s): %s\n", entry.Info.Name, err)) + } + fmt.Printf("Package (%s) was successfully fetched!\n", bpmpkg.PkgInfo.Name) + operation.Actions[i] = &InstallPackageAction{ + File: fetchedPackage, + IsDependency: action.(*FetchPackageAction).IsDependency, + BpmPackage: bpmpkg, + } + } + } + + // Determine words to be used for the following message + words := make([]string, 0) + if slices.ContainsFunc(operation.Actions, func(action OperationAction) bool { + return action.GetActionType() == "install" + }) { + words = append(words, "Installing") + } + + if slices.ContainsFunc(operation.Actions, func(action OperationAction) bool { + return action.GetActionType() == "remove" + }) { + words = append(words, "Removing") + } + + if len(words) == 0 { + return nil + } + fmt.Printf("%s packages...\n", strings.Join(words, "/")) + + // Installing/Removing packages from system + for _, action := range operation.Actions { + if action.GetActionType() == "remove" { + pkgInfo := action.(*RemovePackageAction).BpmPackage.PkgInfo + err := RemovePackage(pkgInfo.Name, verbose, operation.RootDir) + if err != nil { + return errors.New(fmt.Sprintf("could not remove package (%s): %s\n", pkgInfo.Name, err)) + } + } else if action.GetActionType() == "install" { + value := action.(*InstallPackageAction) + bpmpkg := value.BpmPackage + var err error + if value.IsDependency { + err = InstallPackage(value.File, operation.RootDir, verbose, true, false, false, false) + } else { + err = InstallPackage(value.File, operation.RootDir, verbose, force, false, false, false) + } + if err != nil { + return errors.New(fmt.Sprintf("could not install package (%s): %s\n", bpmpkg.PkgInfo.Name, err)) + } + fmt.Printf("Package (%s) was successfully installed\n", bpmpkg.PkgInfo.Name) + if value.IsDependency { + err := SetInstallationReason(bpmpkg.PkgInfo.Name, Dependency, operation.RootDir) + if err != nil { + return errors.New(fmt.Sprintf("could not set installation reason for package (%s): %s\n", value.BpmPackage.PkgInfo.Name, err)) + } + } + } + } + fmt.Println("Operation complete!") + + return nil +} + type OperationAction interface { GetActionType() string } diff --git a/utils/package_utils.go b/utils/package_utils.go index 4ec43f6..f9f811d 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -238,31 +238,31 @@ func ReadPackageScripts(filename string) (map[string]string, error) { return nil, err } if header.Name == "pre_install.sh" { - bs, _ := io.ReadAll(tr) + bs, err := io.ReadAll(tr) if err != nil { return nil, err } ret[header.Name] = string(bs) } else if header.Name == "post_install.sh" { - bs, _ := io.ReadAll(tr) + bs, err := io.ReadAll(tr) if err != nil { return nil, err } ret[header.Name] = string(bs) } else if header.Name == "pre_update.sh" { - bs, _ := io.ReadAll(tr) + bs, err := io.ReadAll(tr) if err != nil { return nil, err } ret[header.Name] = string(bs) } else if header.Name == "post_update.sh" { - bs, _ := io.ReadAll(tr) + bs, err := io.ReadAll(tr) if err != nil { return nil, err } ret[header.Name] = string(bs) } else if header.Name == "post_remove.sh" { - bs, _ := io.ReadAll(tr) + bs, err := io.ReadAll(tr) if err != nil { return nil, err } @@ -744,9 +744,6 @@ func compilePackage(bpmpkg *BPMPackage, filename, rootDir string, verbose, binar if _, err := os.Stat(path.Join(temp, "source.sh")); os.IsNotExist(err) { return errors.New("source.sh file could not be found in the temporary build directory"), nil } - if err != nil { - return err, nil - } fmt.Println("Running source.sh file...") if !IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) { err = ExecutePackageScripts(filename, rootDir, Install, false) @@ -1301,7 +1298,7 @@ func (pkgInfo *PackageInfo) CheckConflicts(rootDir string) []string { return ret } -func (pkgInfo *PackageInfo) ResolveAll(resolved, unresolved *[]string, checkMake, checkOptional, ignoreInstalled, verbose bool, rootDir string) ([]string, []string) { +func (pkgInfo *PackageInfo) ResolveDependencies(resolved, unresolved *[]string, checkMake, checkOptional, ignoreInstalled, verbose bool, rootDir string) ([]string, []string) { *unresolved = append(*unresolved, pkgInfo.Name) for _, depend := range pkgInfo.GetAllDependencies(checkMake, checkOptional) { depend = strings.TrimSpace(depend) @@ -1325,7 +1322,7 @@ func (pkgInfo *PackageInfo) ResolveAll(resolved, unresolved *[]string, checkMake } continue } - entry.Info.ResolveAll(resolved, unresolved, checkMake, checkOptional, ignoreInstalled, verbose, rootDir) + entry.Info.ResolveDependencies(resolved, unresolved, checkMake, checkOptional, ignoreInstalled, verbose, rootDir) } } if !slices.Contains(*resolved, pkgInfo.Name) { From a054717b230a65fd09b53d4d0aa1437d852f18fe Mon Sep 17 00:00:00 2001 From: EnumDev Date: Mon, 21 Oct 2024 21:51:03 +0300 Subject: [PATCH 08/11] Improved package removal and fixed bugs related to it --- utils/package_utils.go | 348 +++++++++++++++++++++++------------------ 1 file changed, 193 insertions(+), 155 deletions(-) diff --git a/utils/package_utils.go b/utils/package_utils.go index f9f811d..1ef7f16 100644 --- a/utils/package_utils.go +++ b/utils/package_utils.go @@ -14,6 +14,7 @@ import ( "path" "path/filepath" "slices" + "sort" "strconv" "strings" "syscall" @@ -148,6 +149,7 @@ func ReadPackage(filename string) (*BPMPackage, error) { var pkgFiles []*PackageFileEntry if _, err := os.Stat(filename); os.IsNotExist(err) { + fmt.Println("a") return nil, err } @@ -468,34 +470,33 @@ func CreateReadableInfo(showArchitecture, showType, showPackageRelations bool, p return strings.Join(ret, "\n") } -func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) (error, []string) { - var files []string +func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) error { if !IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) { err := ExecutePackageScripts(filename, rootDir, Install, false) if err != nil { - return err, nil + return err } } else { err := ExecutePackageScripts(filename, rootDir, Update, false) if err != nil { - return err, nil + return err } } seenHardlinks := make(map[string]string) file, err := os.Open(filename) if err != nil { - return err, nil + return err } tarballFile, err := ReadTarballContent(filename, "files.tar.gz") if err != nil { - return err, nil + return err } defer tarballFile.file.Close() archive, err := gzip.NewReader(tarballFile.tarReader) if err != nil { - return err, nil + return err } packageFilesReader := tar.NewReader(archive) for { @@ -504,15 +505,14 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) break } if err != nil { - return err, nil + return err } extractFilename := path.Join(rootDir, header.Name) switch header.Typeflag { case tar.TypeDir: - files = append(files, strings.TrimPrefix(header.Name, "./")) if err := os.Mkdir(extractFilename, 0755); err != nil { if !os.IsExist(err) { - return err, nil + return err } } else { if verbose { @@ -526,18 +526,16 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) if strings.HasSuffix(k, "/") { if strings.HasPrefix(header.Name, k) { if verbose { - fmt.Println("Skipping File: " + extractFilename + " (Containing directory is set to be kept during installs/updates)") + fmt.Println("Skipping File: " + extractFilename + " (Containing directory is set to be kept during reinstalls/updates)") } - files = append(files, strings.TrimPrefix(header.Name, "./")) skip = true continue } } else { if header.Name == k { if verbose { - fmt.Println("Skipping File: " + extractFilename + " (File is configured to be kept during installs/updates)") + fmt.Println("Skipping File: " + extractFilename + " (File is configured to be kept during reinstalls/updates)") } - files = append(files, strings.TrimPrefix(header.Name, "./")) skip = true continue } @@ -549,51 +547,48 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) } err := os.Remove(extractFilename) if err != nil && !os.IsNotExist(err) { - return err, nil + return err } outFile, err := os.Create(extractFilename) if verbose { fmt.Println("Creating File: " + extractFilename) } - files = append(files, strings.TrimPrefix(header.Name, "./")) if err != nil { - return err, nil + return err } if _, err := io.Copy(outFile, packageFilesReader); err != nil { - return err, nil + return err } if err := os.Chmod(extractFilename, header.FileInfo().Mode()); err != nil { - return err, nil + return err } err = outFile.Close() if err != nil { - return err, nil + return err } case tar.TypeSymlink: if verbose { fmt.Println("Creating Symlink: " + extractFilename + " -> " + header.Linkname) } - files = append(files, strings.TrimPrefix(header.Name, "./")) err := os.Remove(extractFilename) if err != nil && !os.IsNotExist(err) { - return err, nil + return err } err = os.Symlink(header.Linkname, extractFilename) if err != nil { - return err, nil + return err } case tar.TypeLink: if verbose { fmt.Println("Detected Hard Link: " + extractFilename + " -> " + path.Join(rootDir, strings.TrimPrefix(header.Linkname, "files/"))) } - files = append(files, strings.TrimPrefix(header.Name, "./")) seenHardlinks[extractFilename] = path.Join(strings.TrimPrefix(header.Linkname, "files/")) err := os.Remove(extractFilename) if err != nil && !os.IsNotExist(err) { - return err, nil + return err } default: - return errors.New("unknown type (" + strconv.Itoa(int(header.Typeflag)) + ") in " + extractFilename), nil + return errors.New("unknown type (" + strconv.Itoa(int(header.Typeflag)) + ") in " + extractFilename) } } for extractFilename, destination := range seenHardlinks { @@ -602,12 +597,12 @@ func extractPackage(bpmpkg *BPMPackage, verbose bool, filename, rootDir string) } err := os.Link(path.Join(rootDir, destination), extractFilename) if err != nil { - return err, nil + return err } } defer archive.Close() defer file.Close() - return nil, files + return nil } func isSplitPackage(filename string) bool { @@ -1054,17 +1049,103 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, if _, err := os.Stat(filename); os.IsNotExist(err) { return err } - var oldFiles []string - var files []string bpmpkg, err := ReadPackage(filename) if err != nil { return err } packageInstalled := IsPackageInstalled(bpmpkg.PkgInfo.Name, rootDir) + // Check if package is installed and remove current files if packageInstalled { + // Fetching and reversing package file entry list fileEntries := GetPackageFiles(bpmpkg.PkgInfo.Name, rootDir) + sort.Slice(fileEntries, func(i, j int) bool { + return fileEntries[i].Path < fileEntries[j].Path + }) + slices.Reverse(fileEntries) + files, err := GetAllPackageFiles(rootDir, bpmpkg.PkgInfo.Name) + if err != nil { + return err + } + + // Removing old package files + if verbose { + fmt.Printf("Removing old files for package (%s)...\n", bpmpkg.PkgInfo.Name) + } for _, entry := range fileEntries { - files = append(files, entry.Path) + file := path.Join(rootDir, entry.Path) + stat, err := os.Lstat(file) + if os.IsNotExist(err) { + continue + } + if err != nil { + return err + } + if len(files[entry.Path]) != 0 { + if verbose { + fmt.Println("Skipping path: " + file + " (Path is managed by multiple packages)") + } + continue + } + shouldContinue := false + for _, value := range bpmpkg.PkgInfo.Keep { + if strings.HasSuffix(value, "/") { + if strings.HasPrefix(entry.Path, value) || entry.Path == strings.TrimSuffix(value, "/") { + if verbose { + fmt.Println("Skipping path: " + file + " (Path is set to be kept during reinstalls/updates)") + } + shouldContinue = true + continue + } + } else { + if entry.Path == value { + if verbose { + fmt.Println("Skipping path: " + file + " (Path is set to be kept during reinstalls/updates)") + } + shouldContinue = true + continue + } + } + } + if shouldContinue { + continue + } + if stat.Mode()&os.ModeSymlink != 0 { + if verbose { + fmt.Println("Removing: " + file) + } + err := os.Remove(file) + if err != nil { + return err + } + continue + } + if stat.IsDir() { + dir, err := os.ReadDir(file) + if err != nil { + return err + } + if len(dir) != 0 { + if verbose { + fmt.Println("Skipping non-empty directory: " + file) + } + continue + } + if verbose { + fmt.Println("Removing: " + file) + } + err = os.Remove(file) + if err != nil { + return err + } + } else { + if verbose { + fmt.Println("Removing: " + file) + } + err := os.Remove(file) + if err != nil { + return err + } + } } } if !force { @@ -1072,30 +1153,27 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, return errors.New("cannot install a package with a different architecture") } } + + if verbose { + fmt.Printf("Extracting files for package (%s)...\n", bpmpkg.PkgInfo.Name) + } + if bpmpkg.PkgInfo.Type == "binary" { - err, i := extractPackage(bpmpkg, verbose, filename, rootDir) + err := extractPackage(bpmpkg, verbose, filename, rootDir) if err != nil { return err } - files = i } else if bpmpkg.PkgInfo.Type == "source" { if isSplitPackage(filename) { return errors.New("BPM is unable to install split source packages") } - err, i := compilePackage(bpmpkg, filename, rootDir, verbose, binaryPkgFromSrc, skipCheck, keepTempDir) + err, _ := compilePackage(bpmpkg, filename, rootDir, verbose, binaryPkgFromSrc, skipCheck, keepTempDir) if err != nil { return err } - files = i } else { return errors.New("unknown package type: " + bpmpkg.PkgInfo.Type) } - slices.Sort(files) - slices.Reverse(files) - - filesDiff := slices.DeleteFunc(oldFiles, func(f string) bool { - return slices.Contains(files, f) - }) installedDir := path.Join(rootDir, "var/lib/bpm/installed/") err = os.MkdirAll(installedDir, 0755) @@ -1164,79 +1242,6 @@ func InstallPackage(filename, rootDir string, verbose, force, binaryPkgFromSrc, } } - if len(filesDiff) != 0 { - fmt.Println("Removing obsolete files...") - var symlinks []string - for _, f := range filesDiff { - f = path.Join(rootDir, f) - lstat, err := os.Lstat(f) - if os.IsNotExist(err) { - continue - } else if err != nil { - return err - } - if lstat.Mode()&os.ModeSymlink != 0 { - symlinks = append(symlinks, f) - continue - } - stat, err := os.Stat(f) - if os.IsNotExist(err) { - continue - } else if err != nil { - return err - } - if stat.IsDir() { - dir, err := os.ReadDir(f) - if err != nil { - return err - } - if len(dir) == 0 { - if verbose { - fmt.Println("Removing: " + f) - } - err := os.Remove(f) - if err != nil { - return err - } - } - } else { - if verbose { - fmt.Println("Removing: " + f) - } - err := os.Remove(f) - if err != nil { - return err - } - } - } - removals := -1 - for len(symlinks) > 0 && removals != 0 { - removals = 0 - for i := len(symlinks) - 1; i >= 0; i-- { - f := symlinks[i] - f = path.Join(rootDir, f) - _, err := os.Lstat(f) - if os.IsNotExist(err) { - continue - } else if err != nil { - return err - } - _, err = filepath.EvalSymlinks(f) - if os.IsNotExist(err) { - err := os.Remove(f) - if err != nil { - return err - } - removals++ - if verbose { - fmt.Println("Removing: " + f) - } - } else if err != nil { - return err - } - } - } - } if !packageInstalled { err = ExecutePackageScripts(filename, rootDir, Install, true) if err != nil { @@ -1400,7 +1405,7 @@ func GetPackageFiles(pkg, rootDir string) []*PackageFileEntry { stringEntry := strings.Split(strings.TrimSpace(line), " ") if len(stringEntry) < 5 { pkgFiles = append(pkgFiles, &PackageFileEntry{ - Path: line, + Path: strings.TrimSuffix(line, "/"), OctalPerms: 0, UserID: 0, GroupID: 0, @@ -1425,7 +1430,7 @@ func GetPackageFiles(pkg, rootDir string) []*PackageFileEntry { return nil } pkgFiles = append(pkgFiles, &PackageFileEntry{ - Path: strings.Join(stringEntry[:len(stringEntry)-4], " "), + Path: strings.TrimSuffix(strings.Join(stringEntry[:len(stringEntry)-4], " "), "/"), OctalPerms: uint32(octalPerms), UserID: int(uid), GroupID: int(gid), @@ -1472,6 +1477,34 @@ func GetPackage(pkg, rootDir string) *BPMPackage { } } +func GetAllPackageFiles(rootDir string, excludePackages ...string) (map[string][]*BPMPackage, error) { + ret := make(map[string][]*BPMPackage) + + pkgNames, err := GetInstalledPackages(rootDir) + if err != nil { + return nil, err + } + + for _, pkgName := range pkgNames { + if slices.Contains(excludePackages, pkgName) { + continue + } + bpmpkg := GetPackage(pkgName, rootDir) + if bpmpkg == nil { + return nil, errors.New(fmt.Sprintf("could not get BPM package (%s)", pkgName)) + } + for _, entry := range bpmpkg.PkgFiles { + if _, ok := ret[entry.Path]; ok { + ret[entry.Path] = append(ret[entry.Path], bpmpkg) + } else { + ret[entry.Path] = []*BPMPackage{bpmpkg} + } + } + } + + return ret, nil +} + func RemovePackage(pkg string, verbose bool, rootDir string) error { installedDir := path.Join(rootDir, "var/lib/bpm/installed/") pkgDir := path.Join(installedDir, pkg) @@ -1479,8 +1512,19 @@ func RemovePackage(pkg string, verbose bool, rootDir string) error { if pkgInfo == nil { return errors.New("could not get package info") } + + // Fetching and reversing package file entry list fileEntries := GetPackageFiles(pkg, rootDir) - var symlinks []string + sort.Slice(fileEntries, func(i, j int) bool { + return fileEntries[i].Path < fileEntries[j].Path + }) + slices.Reverse(fileEntries) + files, err := GetAllPackageFiles(rootDir, pkg) + if err != nil { + return err + } + + // Removing package files for _, entry := range fileEntries { file := path.Join(rootDir, entry.Path) lstat, err := os.Lstat(file) @@ -1490,8 +1534,20 @@ func RemovePackage(pkg string, verbose bool, rootDir string) error { if err != nil { return err } + if len(files[entry.Path]) != 0 { + if verbose { + fmt.Println("Skipping path: " + file + "(Path is managed by multiple packages)") + } + continue + } if lstat.Mode()&os.ModeSymlink != 0 { - symlinks = append(symlinks, file) + if verbose { + fmt.Println("Removing: " + file) + } + err := os.Remove(file) + if err != nil { + return err + } continue } stat, err := os.Stat(file) @@ -1506,14 +1562,18 @@ func RemovePackage(pkg string, verbose bool, rootDir string) error { if err != nil { return err } - if len(dir) == 0 { + if len(dir) != 0 { if verbose { - fmt.Println("Removing: " + file) - } - err := os.Remove(file) - if err != nil { - return err + fmt.Println("Skipping non-empty directory: " + file) } + continue + } + if verbose { + fmt.Println("Removing: " + file) + } + err = os.Remove(file) + if err != nil { + return err } } else { if verbose { @@ -1525,33 +1585,8 @@ func RemovePackage(pkg string, verbose bool, rootDir string) error { } } } - removals := -1 - for len(symlinks) > 0 && removals != 0 { - removals = 0 - for i := len(symlinks) - 1; i >= 0; i-- { - file := symlinks[i] - file = path.Join(rootDir, file) - _, err := os.Lstat(file) - if os.IsNotExist(err) { - continue - } else if err != nil { - return err - } - _, err = filepath.EvalSymlinks(file) - if os.IsNotExist(err) { - err := os.Remove(file) - if err != nil { - return err - } - removals++ - if verbose { - fmt.Println("Removing: " + file) - } - } else if err != nil { - return err - } - } - } + + // Executing post_remove script if _, err := os.Stat(path.Join(pkgDir, "post_remove.sh")); err == nil { cmd := exec.Command("/bin/bash", path.Join(pkgDir, "post_remove.sh")) if !BPMConfig.SilentCompilation { @@ -1586,12 +1621,15 @@ func RemovePackage(pkg string, verbose bool, rootDir string) error { return err } } - err := os.RemoveAll(pkgDir) - if err != nil { - return err - } + + // Removing package directory if verbose { fmt.Println("Removing: " + pkgDir) } + err = os.RemoveAll(pkgDir) + if err != nil { + return err + } + return nil } From 4aed0fe5b045a2713d65cd705233a3c113da8a5e Mon Sep 17 00:00:00 2001 From: EnumDev Date: Mon, 21 Oct 2024 21:55:39 +0300 Subject: [PATCH 09/11] Made GetArch() function run independently of the 'uname' command --- utils/general_utils.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/utils/general_utils.go b/utils/general_utils.go index 18fd718..dd1e812 100644 --- a/utils/general_utils.go +++ b/utils/general_utils.go @@ -5,16 +5,22 @@ import ( "io" "math" "os" - "os/exec" - "strings" + "syscall" ) func GetArch() string { - output, err := exec.Command("/usr/bin/uname", "-m").Output() + uname := syscall.Utsname{} + err := syscall.Uname(&uname) if err != nil { return "" } - return strings.TrimSpace(string(output)) + + var byteString [65]byte + var indexLength int + for ; uname.Machine[indexLength] != 0; indexLength++ { + byteString[indexLength] = uint8(uname.Machine[indexLength]) + } + return string(byteString[:indexLength]) } func copyFileContents(src, dst string) (err error) { From 0b439bcfed7f10130500798981562259524ef00c Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 22 Oct 2024 15:37:16 +0300 Subject: [PATCH 10/11] Changed BPM Version to 0.5.0 --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 471ccf0..c5b69d8 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( /* A simple-to-use package manager */ /* ------------------------------------------------------- */ -var bpmVer = "0.4.2" +var bpmVer = "0.5.0" var subcommand = "help" var subcommandArgs []string From fe928dbbd6fe5d7b2be7f9880dcbbe5eeeb1196e Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 22 Oct 2024 17:43:05 +0300 Subject: [PATCH 11/11] Fixed ActionsContainPackage BPMOperation receiver function incorrectly returning false --- utils/operations.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/utils/operations.go b/utils/operations.go index 021c060..57772d3 100644 --- a/utils/operations.go +++ b/utils/operations.go @@ -18,11 +18,17 @@ type BPMOperation struct { func (operation *BPMOperation) ActionsContainPackage(pkg string) bool { for _, action := range operation.Actions { if action.GetActionType() == "install" { - return action.(*InstallPackageAction).BpmPackage.PkgInfo.Name == pkg + if action.(*InstallPackageAction).BpmPackage.PkgInfo.Name == pkg { + return true + } } else if action.GetActionType() == "fetch" { - return action.(*FetchPackageAction).RepositoryEntry.Info.Name == pkg + if action.(*FetchPackageAction).RepositoryEntry.Info.Name == pkg { + return true + } } else if action.GetActionType() == "remove" { - return action.(*RemovePackageAction).BpmPackage.PkgInfo.Name == pkg + if action.(*RemovePackageAction).BpmPackage.PkgInfo.Name == pkg { + return true + } } } return false