Compare commits

..

No commits in common. "0fa66c1125e94e23035e91cd1b13e691862f9034" and "39f15e7e1e65627c677599fe320419eed1705e0f" have entirely different histories.

8 changed files with 167 additions and 691 deletions

1
.gitignore vendored
View file

@ -1,2 +1 @@
.idea
/bin

View file

@ -1,23 +0,0 @@
DESTDIR ?= /usr
BUILDDIR ?= dist
CGO_ENABLED ?= 0
all: cmd
cmd ./cmd/eon/main.go:
mkdir -p $(BUILDDIR)/bin
go build -o $(BUILDDIR)/bin/eon ./cmd/eon -ldflags "-s -w"
install:
install -d $(DESTDIR)/bin
install -m 755 $(BUILDDIR)/bin/eon $(DESTDIR)/bin/eon
uninstall:
rm -f $(DESTDIR)/bin/eon
test:
clean:
rm -rf $(BUILDDIR)

View file

@ -1,13 +0,0 @@
#!/bin/sh
# Create the bin directory
mkdir -p bin
# Build the CLI
echo "Building the CLI..."
CGO_ENABLED=0 go build -ldflags "-s -w" -o ./bin/eon ./cmd/main.go
# Install the CLI
echo "Installing the CLI..."
sudo install ./bin/eon /bin
echo "eon installed successfully!"

BIN
cmd/cmd Executable file

Binary file not shown.

View file

@ -10,9 +10,9 @@ import (
"os"
"regexp"
"sort"
"strconv"
"strings"
"math/big"
"net/http"
"net/url"
@ -64,7 +64,7 @@ func main() {
fmt.Println("Removes unused dependencies.")
case "repo":
fmt.Println("Usage: eon repo (add/del/list)")
fmt.Println("- add <packageUrl>: adds a repository.")
fmt.Println("- add <url>: adds a repository.")
fmt.Println("- del <name>: removes a repository.")
fmt.Println("- list: lists repositories.")
case "help":
@ -89,8 +89,8 @@ func main() {
for _, pkg := range os.Args[2:] {
if !strings.HasPrefix(pkg, "-") {
fileInfo, err := os.Stat(pkg)
if err != nil || fileInfo.IsDir() {
if os.IsNotExist(err) || fileInfo.IsDir() {
if err != nil {
if os.IsNotExist(err) {
// This unholy regex is a package name validator. It validates repository-name/package-name/1.0.0-prerelease.1+meta.1
// The regex is so unholy it breaks JetBrains' regex parser, so I had to disable it.
//goland:noinspection RegExpUnnecessaryNonCapturingGroup
@ -105,9 +105,6 @@ func main() {
} else {
repoPackageList = append(repoPackageList, pkg)
}
} else {
fmt.Println(color.RedString("Failed to get file information: " + err.Error()))
os.Exit(1)
}
} else {
localPackageList = append(localPackageList, pkg)
@ -164,7 +161,7 @@ func main() {
var vagueErr error
var epkBytes bytes.Buffer
if !inMemoryMode {
preMap, err, vagueErr = lib.PreMapEPK(lib.StreamOrBytes{FileStream: epkFile, IsFileStream: true}, uint64(fileInfos[pkg].Size()))
preMap, err, vagueErr = lib.PreMapEPK(lib.StreamOrBytes{FileStream: epkFile, IsFileStream: true}, fileInfos[pkg].Size())
} else {
_, err = io.Copy(bufio.NewWriter(&epkBytes), epkFile)
if err != nil {
@ -172,11 +169,11 @@ func main() {
", have you tried disabling in-memory mode?"))
os.Exit(1)
}
err = epkFile.Close()
err := epkFile.Close()
if err != nil {
fmt.Println(color.HiYellowString("Failed to close package file: " + err.Error() + ", memory leak possible."))
}
preMap, err, vagueErr = lib.PreMapEPK(lib.StreamOrBytes{Bytes: epkBytes.Bytes()}, uint64(fileInfos[pkg].Size()))
preMap, err, vagueErr = lib.PreMapEPK(lib.StreamOrBytes{Bytes: epkBytes.Bytes()}, fileInfos[pkg].Size())
}
if err != nil || vagueErr != nil {
common.PreMapEPKHandleError(err, vagueErr, logger)
@ -192,14 +189,10 @@ func main() {
}
// Check if the architecture is supported.
if preMap.DisplayData.Architecture != strings.TrimRight(string(uts.Machine[:]), "\x00") {
if preMap.DisplayData.Architecture != "noarch" {
if preMap.DisplayData.Architecture != "noarch" || preMap.DisplayData.Architecture != string(uts.Machine[:]) {
fmt.Println(color.RedString("Package architecture not supported: " + preMap.DisplayData.Architecture))
os.Exit(1)
}
}
preMap.DisplayData.IsDependency = false
var installPackage common.InstallPackage
installPackage.EPKPreMap = &preMap
@ -229,13 +222,13 @@ func main() {
if exists {
if version.Compare(&preMap.DisplayData.Version) != -1 {
if !forceMode {
skipEpk := []string{preMap.DisplayData.Name, preMap.DisplayData.Architecture, preMap.DisplayData.Version.String(), "Local file", humanize.Bytes(preMap.DisplayData.DecompressedSize), "Skip"}
skipEpk := []string{preMap.DisplayData.Name, preMap.DisplayData.Architecture, preMap.DisplayData.Version.String(), "Local file", humanize.BigIBytes(preMap.DisplayData.DecompressedSize), "Skip"}
skipEpkList = append(skipEpkList, skipEpk)
} else {
installPackage.IsForced = true
var emptyList []string
var addedDeps int
epkList, addedDeps, err = common.HandleDependencies(dependencies, installPackage, 0, []lib.RemoteEPK{}, common.DefaultListRemotePackagesInDB, common.DefaultListEPKsInDB, epkList, &emptyList, logger)
epkList, addedDeps, err = common.HandleDependencies(dependencies, installPackage, 0, []lib.RemoteEPK{}, common.DefaultListRemotePackagesInDB, epkList, &emptyList, logger)
if err != nil {
fmt.Println(color.RedString("Failed to handle dependencies: " + err.Error()))
os.Exit(1)
@ -247,7 +240,7 @@ func main() {
} else {
var emptyList []string
var addedDeps int
epkList, addedDeps, err = common.HandleDependencies(dependencies, installPackage, 0, []lib.RemoteEPK{}, common.DefaultListRemotePackagesInDB, common.DefaultListEPKsInDB, epkList, &emptyList, logger)
epkList, addedDeps, err = common.HandleDependencies(dependencies, installPackage, 0, []lib.RemoteEPK{}, common.DefaultListRemotePackagesInDB, epkList, &emptyList, logger)
if err != nil {
fmt.Println(color.RedString("Failed to handle dependencies: " + err.Error()))
os.Exit(1)
@ -260,7 +253,7 @@ func main() {
preMap.IsUpgrade = false
var emptyList []string
var addedDeps int
epkList, addedDeps, err = common.HandleDependencies(dependencies, installPackage, 0, []lib.RemoteEPK{}, common.DefaultListRemotePackagesInDB, common.DefaultListEPKsInDB, epkList, &emptyList, logger)
epkList, addedDeps, err = common.HandleDependencies(dependencies, installPackage, 0, []lib.RemoteEPK{}, common.DefaultListRemotePackagesInDB, epkList, &emptyList, logger)
if err != nil {
fmt.Println(color.RedString("Failed to handle dependencies: " + err.Error()))
os.Exit(1)
@ -325,8 +318,6 @@ func main() {
os.Exit(1)
}
preMap.DisplayData.IsDependency = false
// Map the data we have
epkEntry.Priority = 0
epkEntry.EPKPreMap = &preMap
@ -351,14 +342,11 @@ func main() {
}
// Calculate the total size of the package
contentLength, err := strconv.ParseUint(epkBytes.Header.Get("Content-Length"), 10, 64)
if err != nil {
fmt.Println(color.RedString("Failed to get content length: " + err.Error()))
os.Exit(1)
}
contentLength := new(big.Int)
contentLength.SetString(epkBytes.Header.Get("Content-Length"), 10)
// Print that we are downloading the package
fmt.Println("\nDownloading package: " + preMap.DisplayData.Name + " (" + humanize.Bytes(contentLength) + ")")
fmt.Println("\nDownloading package: " + preMap.DisplayData.Name + " (" + humanize.BigIBytes(contentLength) + ")")
// Hide the cursor for reasons explained below.
fmt.Print("\033[?25l")
@ -378,8 +366,8 @@ func main() {
// Set the progress to 100%
logger.LogFunc(lib.Log{
Level: "PROGRESS",
Progress: 1,
Total: 1,
Progress: big.NewInt(1),
Total: big.NewInt(1),
})
// Show the cursor again because we're done with the progress bar.
@ -405,14 +393,14 @@ func main() {
if version.Compare(&preMap.DisplayData.Version) != -1 {
// If the version is the same or newer, skip the package.
if !forceMode {
skipEpkList = append(skipEpkList, []string{preMap.DisplayData.Name, preMap.DisplayData.Architecture, preMap.DisplayData.Version.String(), remoteEPK.Repository.Name, humanize.Bytes(preMap.DisplayData.DecompressedSize), "Skip"})
skipEpkList = append(skipEpkList, []string{preMap.DisplayData.Name, preMap.DisplayData.Architecture, preMap.DisplayData.Version.String(), remoteEPK.Repository.Name, humanize.BigIBytes(preMap.DisplayData.DecompressedSize), "Skip"})
} else {
// If the version is the same or newer, but the user wants to force the installation, install it.
epkEntry.IsForced = true
// We can let it use our remoteEPKList to save a SQL query.
var emptyList []string
var addedDeps int
epkList, addedDeps, err = common.HandleDependencies(dependencies, epkEntry, 0, remoteEpkList, common.DefaultListRemotePackagesInDB, common.DefaultListEPKsInDB, epkList, &emptyList, logger)
epkList, addedDeps, err = common.HandleDependencies(dependencies, epkEntry, 0, remoteEpkList, common.DefaultListRemotePackagesInDB, epkList, &emptyList, logger)
if err != nil {
fmt.Println(color.RedString("Failed to handle dependencies: " + err.Error()))
os.Exit(1)
@ -426,7 +414,7 @@ func main() {
// If the version is older, install it.
var emptyList []string
var addedDeps int
epkList, addedDeps, err = common.HandleDependencies(dependencies, epkEntry, 0, remoteEpkList, common.DefaultListRemotePackagesInDB, common.DefaultListEPKsInDB, epkList, &emptyList, logger)
epkList, addedDeps, err = common.HandleDependencies(dependencies, epkEntry, 0, remoteEpkList, common.DefaultListRemotePackagesInDB, epkList, &emptyList, logger)
if err != nil {
fmt.Println(color.RedString("Failed to handle dependencies: " + err.Error()))
os.Exit(1)
@ -439,7 +427,7 @@ func main() {
// If the package is not installed, install it.
var emptyList []string
var addedDeps int
epkList, addedDeps, err = common.HandleDependencies(dependencies, epkEntry, 0, remoteEpkList, common.DefaultListRemotePackagesInDB, common.DefaultListEPKsInDB, epkList, &emptyList, logger)
epkList, addedDeps, err = common.HandleDependencies(dependencies, epkEntry, 0, remoteEpkList, common.DefaultListRemotePackagesInDB, epkList, &emptyList, logger)
if err != nil {
fmt.Println(color.RedString("Failed to handle dependencies: " + err.Error()))
os.Exit(1)
@ -500,7 +488,7 @@ func main() {
} else {
finalisedList[3] = pkg.Repository.Name
}
finalisedList[4] = humanize.Bytes(pkg.EPKPreMap.DisplayData.DecompressedSize)
finalisedList[4] = humanize.BigIBytes(pkg.EPKPreMap.DisplayData.DecompressedSize)
if pkg.IsForced {
finalisedList[5] = "Forced installation"
} else if pkg.EPKPreMap.IsUpgrade {
@ -519,8 +507,8 @@ func main() {
}
fmt.Println("Transaction Summary")
fmt.Println("\nInstalling " + humanize.Comma(int64(len(epkList))) + " packages, of which " + humanize.Comma(int64(dependencies)) + " are dependencies.")
fmt.Println("Total download size: " + humanize.Bytes(common.GetTotalSize(epkList)))
fmt.Println("Total installed size: " + humanize.Bytes(common.GetTotalInstalledSize(epkList)) + "\n")
fmt.Println("Total download size: " + humanize.BigIBytes(common.GetTotalSize(epkList)))
fmt.Println("Total installed size: " + humanize.BigIBytes(common.GetTotalInstalledSize(epkList)) + "\n")
var response string
if !yesMode {
response = logger.LogFunc(lib.Log{
@ -560,12 +548,8 @@ func main() {
}
// Start streaming the package into a buffer
contentLength, err := strconv.ParseUint(epkBytes.Header.Get("Content-Length"), 10, 64)
if err != nil {
fmt.Println(color.RedString("Failed to get content length: " + err.Error()))
os.Exit(1)
}
fmt.Println("\nDownloading package: " + installPackage.EPKPreMap.DisplayData.Name + " (" + humanize.Bytes(contentLength) + ")")
contentLength := new(big.Int).SetBytes([]byte(epkBytes.Header.Get("Content-Length")))
fmt.Println("\nDownloading package: " + installPackage.EPKPreMap.DisplayData.Name + " (" + humanize.BigIBytes(contentLength) + ")")
var buffer bytes.Buffer
_, err = io.Copy(&lib.ProgressWriter{
Logger: logger,
@ -580,8 +564,8 @@ func main() {
// Set the progress to 100%
logger.LogFunc(lib.Log{
Level: "PROGRESS",
Progress: 1,
Total: 1,
Progress: big.NewInt(1),
Total: big.NewInt(1),
})
// Close the response body
@ -653,7 +637,7 @@ func main() {
switch os.Args[2] {
case "add":
if len(os.Args) < 4 {
fmt.Println("Usage: eon repo add <packageUrl>")
fmt.Println("Usage: eon repo add <url>")
os.Exit(1)
} else {
err := common.EstablishDBConnection(logger)
@ -801,7 +785,7 @@ func main() {
finalisedList[1] = pkg.DisplayData.Architecture
finalisedList[2] = pkg.DisplayData.Version.String()
finalisedList[3] = repositoryMap[pkg.Name].Name
finalisedList[4] = humanize.Bytes(decompressedSizeMap[pkg.Name])
finalisedList[4] = humanize.BigIBytes(decompressedSizeMap[pkg.Name])
finalisedList[5] = "Remove"
for _, item := range finalisedList {
common.PrintWithEvenPadding(item, maxSize)
@ -812,11 +796,11 @@ func main() {
}
fmt.Println("Transaction Summary")
fmt.Println("\nRemoving " + humanize.Comma(int64(len(packageRemoveList))) + " packages.")
var space uint64
space := new(big.Int)
for _, pkg := range packageRemoveList {
space += decompressedSizeMap[pkg.Name]
space = new(big.Int).Add(space, decompressedSizeMap[pkg.Name])
}
fmt.Println("Total reclaimed space: " + humanize.Bytes(space) + "\n")
fmt.Println("Total reclaimed space: " + humanize.BigIBytes(space) + "\n")
response := logger.LogFunc(lib.Log{
Level: "INFO",
Content: "Proceed with removal (y/n)?",
@ -848,31 +832,8 @@ func main() {
os.Exit(1)
}
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
fmt.Println(color.RedString("Failed to get terminal width: " + err.Error()))
os.Exit(1)
}
if width < 15 {
fmt.Println(color.RedString("Terminal too small. Minimum required width: 15 characters."))
os.Exit(1)
}
maxWidth := width / 3
switch os.Args[2] {
case "remote":
// Refresh the package list.
err = common.RefreshPackageList(common.DefaultListRepositoriesInDB, common.DefaultAddRemotePackageToDB,
common.DefaultGetFingerprintFromDB, common.DefaultAddFingerprintToDB, common.DefaultCheckRepositoryInDB,
common.DefaultAddRepositoryToDB, common.DefaultListRemotePackagesInDB, common.DefaultRemoveRemotePackageFromDB,
logger)
if err != nil {
fmt.Println(color.RedString("Failed to refresh package list: " + err.Error()))
os.Exit(1)
}
remoteEpkList, err := common.DefaultListRemotePackagesInDB()
if err != nil {
fmt.Println(color.RedString("Failed to list remote packages: " + err.Error()))
@ -881,337 +842,18 @@ func main() {
if len(remoteEpkList) == 0 {
fmt.Println("No remote packages.")
} else {
fmt.Println("\nRemote packages:")
for range width {
fmt.Print("=")
}
for _, item := range []string{"Name", "Version", "Repository"} {
common.PrintWithEvenPadding(item, maxWidth)
}
for range width {
fmt.Print("=")
}
fmt.Println("Remote packages:")
for _, epk := range remoteEpkList {
var printMap []string
printMap = append(printMap, epk.Name, epk.Version.String(), epk.Repository.Name)
for _, item := range printMap {
common.PrintWithEvenPadding(item, maxWidth)
fmt.Println("\n" + epk.Name + ":\n" +
"- " + epk.Description +
"\n- Author: " + epk.Author +
"\n- Size to download: " + humanize.Bytes(uint64(epk.CompressedSize)) +
"\n- Version: " + epk.Version.String() +
"\n- Architecture: " + epk.Architecture +
"\n- Repository: " + epk.Repository.Name)
}
fmt.Println()
}
for range width {
fmt.Print("=")
}
}
case "local":
epkMap, _, repositoryMap, _, err := common.DefaultListEPKsInDB()
if err != nil {
fmt.Println(color.RedString("Failed to list installed packages: " + err.Error()))
os.Exit(1)
}
if len(epkMap) == 0 {
fmt.Println("No local packages.")
} else {
// Sort the map by name.
var epkList []string
for epk := range epkMap {
epkList = append(epkList, epk)
}
sort.Strings(epkList)
fmt.Println("Local packages:")
for range width {
fmt.Print("=")
}
for _, item := range []string{"Name", "Version", "Repository"} {
common.PrintWithEvenPadding(item, maxWidth)
}
for range width {
fmt.Print("=")
}
for _, epk := range epkList {
version := epkMap[epk].Version
var printMap []string
printMap = append(printMap, epk, version.String(), repositoryMap[epk].Name)
for _, item := range printMap {
common.PrintWithEvenPadding(item, maxWidth)
}
fmt.Println()
}
for range width {
fmt.Print("=")
}
}
}
case "info":
if len(os.Args) < 3 {
fmt.Println("Usage: eon info <package>")
os.Exit(1)
}
err := common.EstablishDBConnection(logger)
if err != nil {
fmt.Println(color.RedString("Failed to establish a connection to the database: " + err.Error()))
os.Exit(1)
}
err = common.RefreshPackageList(common.DefaultListRepositoriesInDB, common.DefaultAddRemotePackageToDB,
common.DefaultGetFingerprintFromDB, common.DefaultAddFingerprintToDB, common.DefaultCheckRepositoryInDB,
common.DefaultAddRepositoryToDB, common.DefaultListRemotePackagesInDB, common.DefaultRemoveRemotePackageFromDB,
logger)
if err != nil {
fmt.Println(color.RedString("Failed to refresh package list: " + err.Error()))
}
remoteEpks, err := common.DefaultListRemotePackagesInDB()
if err != nil {
fmt.Println(color.RedString("Failed to list installed packages: " + err.Error()))
os.Exit(1)
}
var remoteEpk lib.RemoteEPK
for _, epk := range remoteEpks {
if epk.Name == os.Args[2] {
remoteEpk = epk
break
}
}
if remoteEpk.Name == "" {
fmt.Println(color.RedString("Package not found: " + os.Args[2]))
os.Exit(1)
}
epkPreMap, err, vagueErr := lib.PreMapRemoteEPK(remoteEpk, logger)
if err != nil || vagueErr != nil {
common.PreMapRemoteEPKHandleError(err, vagueErr, logger)
}
packageUrl, err := url.JoinPath(remoteEpk.Repository.URL, remoteEpk.Path)
if err != nil {
fmt.Println(color.RedString("Failed to join URL: " + err.Error()))
os.Exit(1)
}
metadata, err, vagueErr := lib.FullyMapMetadata(lib.StreamOrBytes{
IsURL: true,
URL: packageUrl,
}, &epkPreMap, common.DefaultGetFingerprintFromDB, common.DefaultAddFingerprintToDB, func(*lib.Logger) {
logger.LogFunc(lib.Log{
Level: "WARN",
Content: "This server does not support range requests. Please use the -O flag to " +
"enable in memory mode, or contact the server administrator to use a web server " +
"that supports range requests, such as Apache or Nginx. The speed of download " +
"is considerably slowed, as we need to discard bytes since we can only read " +
"sequentially.",
Prompt: false,
})
}, logger)
if err != nil || vagueErr != nil {
common.FullyMapMetadataHandleError(err, vagueErr, logger)
}
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
fmt.Println(color.RedString("Failed to get terminal width: " + err.Error()))
os.Exit(1)
}
if width < 11 {
fmt.Println(color.RedString("Terminal too small. Minimum required width: 11 characters."))
os.Exit(1)
}
fmt.Println()
for range width {
fmt.Print("=")
}
common.PrintWithEvenPadding("Information", width/2)
common.PrintWithEvenPadding("Content", width/2)
for range width {
fmt.Print("=")
}
common.PrintWithEvenPadding("Name", width/2)
common.PrintWithEvenPadding(epkPreMap.DisplayData.Name, width/2)
common.PrintWithEvenPadding("Version", width/2)
common.PrintWithEvenPadding(metadata.Version.String(), width/2)
common.PrintWithEvenPadding("Description", width/2)
common.PrintWithEvenPadding(metadata.Description, width/2)
common.PrintWithEvenPadding("License", width/2)
common.PrintWithEvenPadding(metadata.License, width/2)
common.PrintWithEvenPadding("Architecture", width/2)
common.PrintWithEvenPadding(metadata.Architecture, width/2)
common.PrintWithEvenPadding("Installed Size", width/2)
common.PrintWithEvenPadding(humanize.Bytes(metadata.DecompressedSize), width/2)
common.PrintWithEvenPadding("Download Size", width/2)
common.PrintWithEvenPadding(humanize.Bytes(metadata.Size), width/2)
common.PrintWithEvenPadding("Repository", width/2)
common.PrintWithEvenPadding(remoteEpk.Repository.Name, width/2)
common.PrintWithEvenPadding("Dependencies", width/2)
if len(metadata.Dependencies) == 0 {
common.PrintWithEvenPadding("None", width/2)
} else {
common.PrintWithEvenPadding(strings.Join(metadata.Dependencies, ", "), width/2)
}
// We don't print the long description - it's meant for GUIs.
for range width {
fmt.Print("=")
}
case "clean":
err := common.EstablishDBConnection(logger)
if err != nil {
fmt.Println(color.RedString("Failed to establish a connection to the database: " + err.Error()))
os.Exit(1)
}
packages, _, repositoryMap, _, err := common.DefaultListEPKsInDB()
if err != nil {
fmt.Println(color.RedString("Failed to list installed packages: " + err.Error()))
os.Exit(1)
}
if len(packages) == 0 {
fmt.Println("No packages to clean.")
os.Exit(0)
} else {
var removeList []common.RemovePackage
for _, pkg := range packages {
// Check if it's a dependency of another package.
if pkg.IsDependency {
// Add it to list of candidates for removal.
var removePackage common.RemovePackage
removePackage.Name = pkg.Name
removePackage.DisplayData = pkg
removeList = append(removeList, removePackage)
}
}
for _, pkg := range removeList {
// Check if it's parent is still installed.
for _, epk := range packages {
for _, dep := range epk.Dependencies {
if dep == pkg.Name {
// First, check if it's in the list of packages to remove.
var found bool
for _, epkName := range removeList {
if epkName.Name == epk.Name {
found = true
break
}
}
if !found {
fmt.Println(color.RedString("Package " + pkg.Name + " is a dependency of another package (" + epk.Name + ") and cannot be removed."))
os.Exit(1)
} else {
// It is in the list of packages to remove, we need to set this package's priority higher.
pkg.Priority++
}
}
}
}
}
if len(removeList) > 0 {
// First, re-order the packageRemoveList by priority, then alphabetically.
sort.Slice(removeList, func(i, j int) bool {
if removeList[i].Priority == removeList[j].Priority {
return removeList[i].Name < removeList[j].Name
}
return removeList[i].Priority > removeList[j].Priority
})
// Print the packages that will be removed.
fmt.Println("The following packages will be removed:")
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
fmt.Println(color.RedString("Failed to get terminal width: " + err.Error()))
os.Exit(1)
}
if width < 42 {
fmt.Println(color.RedString("Terminal too small. Minimum required width: 42 characters."))
os.Exit(1)
}
for range width {
fmt.Print("=")
}
tableList := []string{"Package", "Architecture", "Version", "Repository", "Installed Size", "Action"}
maxSize := width / 6
for _, item := range tableList {
common.PrintWithEvenPadding(item, maxSize)
}
fmt.Println()
for range width {
fmt.Print("=")
}
for _, pkg := range removeList {
finalisedList := make([]string, 6)
finalisedList[0] = pkg.Name
finalisedList[1] = pkg.DisplayData.Architecture
finalisedList[2] = pkg.DisplayData.Version.String()
finalisedList[3] = repositoryMap[pkg.Name].Name
finalisedList[4] = humanize.Bytes(pkg.DisplayData.DecompressedSize)
finalisedList[5] = "Remove"
for _, item := range finalisedList {
common.PrintWithEvenPadding(item, maxSize)
}
fmt.Println()
}
for range width {
fmt.Print("=")
}
fmt.Println("Transaction Summary")
fmt.Println("\nRemoving " + humanize.Comma(int64(len(removeList))) + " packages.")
var space uint64
for _, pkg := range removeList {
space += pkg.DisplayData.DecompressedSize
}
fmt.Println("Total reclaimed space: " + humanize.Bytes(space) + "\n")
response := logger.LogFunc(lib.Log{
Level: "INFO",
Content: "Proceed with removal (y/n)?",
Prompt: true,
})
if strings.ToLower(response) == "y" {
for _, pkg := range removeList {
err, vagueErr := lib.RemoveEPK(pkg.Name, common.DefaultRemoveEPKFromDB, common.DefaultGetEPKRemoveInfoFromDB, logger)
if err != nil || vagueErr != nil {
common.RemoveEPKHandleError(err, vagueErr, logger)
fmt.Println(color.RedString("Failed to remove package: " + vagueErr.Error() + err.Error()))
}
fmt.Println("Removed package: " + pkg.Name)
}
} else {
fmt.Println("Removal cancelled.")
os.Exit(1)
}
} else {
fmt.Println("No packages to clean.")
os.Exit(0)
}
}
default:
fmt.Println(color.RedString("Unknown or unimplemented command: " + os.Args[1]))

View file

@ -19,6 +19,7 @@ import (
"crypto/ed25519"
"database/sql"
"encoding/binary"
"math/big"
"net/http"
"net/url"
@ -60,9 +61,7 @@ type Plugin struct {
}
var conn *sql.DB
// Database is no longer compatible with versions below 3.0.0.
var dbVersion = semver.MustParse("3.0.0")
var dbVersion = semver.MustParse("1.0.0-beta.4")
var DefaultLogger = lib.Logger{
LogFunc: func(log lib.Log) string {
@ -81,11 +80,14 @@ var DefaultLogger = lib.Logger{
os.Exit(1)
case "PROGRESS":
// Maths time! We need to calculate the percentage of the progress bar.
// Convert the total and progress to floats so we can do division.
var floatTotal = float64(log.Total)
var floatProgress = float64(log.Progress)
// Calculate the percentage.
var percentage = floatProgress / floatTotal
// Convert the total and progress to big.Floats so we can do division.
floatTotal := new(big.Float)
floatProgress := new(big.Float)
floatTotal.SetInt(log.Total)
floatProgress.SetInt(log.Progress)
// Calculate the fraction we need to multiply by 100 to get the percentage.
percentageBig := new(big.Float).Quo(floatProgress, floatTotal)
percentage, _ := percentageBig.Float64()
// Get the terminal width so we can calculate the width of the progress bar.
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
@ -191,7 +193,7 @@ var TimeMagnitudes = []humanize.RelTimeMagnitude{
// End of Expat / MIT licensed code
}
func HandleDependencies(previousDeps int, targetEPK InstallPackage, parentPriority int, epkList []lib.RemoteEPK, listRemotePackagesInDB func() ([]lib.RemoteEPK, error), listEPKsInDB func() (map[string]lib.DisplayData, map[string]uint64, map[string]lib.Repository, map[string]uint64, error), InstallPackageList []InstallPackage, previousPackages *[]string, logger *lib.Logger) ([]InstallPackage, int, error) {
func HandleDependencies(previousDeps int, targetEPK InstallPackage, parentPriority int, epkList []lib.RemoteEPK, ListRemotePackagesInDB func() ([]lib.RemoteEPK, error), InstallPackageList []InstallPackage, previousPackages *[]string, logger *lib.Logger) ([]InstallPackage, int, error) {
// Iterate through the dependencies of the target EPK.
dependencyLoop:
for _, dependency := range targetEPK.EPKPreMap.DisplayData.Dependencies {
@ -219,7 +221,7 @@ dependencyLoop:
// Add the dependency to the list of previous packages.
*previousPackages = append(*previousPackages, targetEPK.EPKPreMap.DisplayData.Name)
// Recursively handle dependencies.
installedPackage, addedDeps, err := HandleDependencies(previousDeps, epk, parentPriority+1, epkList, listRemotePackagesInDB, listEPKsInDB, InstallPackageList, previousPackages, logger)
installedPackage, addedDeps, err := HandleDependencies(previousDeps, epk, parentPriority+1, epkList, ListRemotePackagesInDB, InstallPackageList, previousPackages, logger)
InstallPackageList = installedPackage
if err != nil {
return nil, 0, err
@ -238,7 +240,7 @@ dependencyLoop:
// Check if we already have a list of remote EPKs to use, if so use it.
if len(epkList) == 0 || epkList == nil {
var err error
epkList, err = listRemotePackagesInDB()
epkList, err = ListRemotePackagesInDB()
if err != nil {
return nil, 0, err
}
@ -263,7 +265,7 @@ dependencyLoop:
} else {
// Check if the installed version is outdated.
if version.LessThan(&epk.Version) {
// Yes it is: add it to the list of EPKs to install.
// Yes it is: Install the new version as an upgrade.
remoteEPK = epk
break
} else {
@ -273,27 +275,10 @@ dependencyLoop:
}
}
}
if !dependencyExists {
// Iterate through the list of installed EPKs to find the dependency.
installedEPKs, _, _, _, err := listEPKsInDB()
if err != nil {
return nil, 0, err
}
for installedEPK := range installedEPKs {
if installedEPK == dependency {
// The dependency is installed. Exit the loop.
continue dependencyLoop
}
}
// If the dependency still doesn't exist, crash.
// If the dependency doesn't exist, crash.
if !dependencyExists {
return nil, 0, errors.New("dependency " + dependency + " does not exist")
}
}
// Increase the dependency's priority.
epkEntry.Priority = parentPriority + 1
// Add the dependency to the list of EPKs to install.
@ -313,7 +298,6 @@ dependencyLoop:
if err != nil || vagueErr != nil {
return nil, 0, err
}
epkPreMap.DisplayData.IsDependency = true
// Set the EPKs display data.
epkEntry.EPKPreMap = &epkPreMap
if exists {
@ -340,7 +324,7 @@ dependencyLoop:
// Add the dependency to the list of previous packages.
*previousPackages = append(*previousPackages, epkEntry.EPKPreMap.DisplayData.Name)
// Recursively handle dependencies.
installPackages, addedDeps, err := HandleDependencies(previousDeps, epkEntry, epkEntry.Priority+1, epkList, listRemotePackagesInDB, listEPKsInDB, InstallPackageList, previousPackages, logger)
installPackages, addedDeps, err := HandleDependencies(previousDeps, epkEntry, epkEntry.Priority+1, epkList, ListRemotePackagesInDB, InstallPackageList, previousPackages, logger)
InstallPackageList = installPackages
if err != nil {
return nil, 0, err
@ -359,19 +343,19 @@ dependencyLoop:
return InstallPackageList, previousDeps, nil
}
func GetTotalSize(InstallPackageList []InstallPackage) uint64 {
var totalSize uint64
func GetTotalSize(InstallPackageList []InstallPackage) *big.Int {
totalSize := new(big.Int)
for _, epk := range InstallPackageList {
totalSize += epk.EPKPreMap.DisplayData.Size
totalSize.Add(totalSize, big.NewInt(epk.EPKPreMap.DisplayData.Size))
}
return totalSize
}
func GetTotalInstalledSize(InstallPackageList []InstallPackage) uint64 {
var totalSize uint64
func GetTotalInstalledSize(InstallPackageList []InstallPackage) *big.Int {
totalSize := new(big.Int)
for _, epk := range InstallPackageList {
if !epk.IsRemote {
totalSize += epk.EPKPreMap.DisplayData.DecompressedSize
totalSize.Add(totalSize, epk.EPKPreMap.DisplayData.DecompressedSize)
}
}
return totalSize
@ -517,7 +501,7 @@ func InitDB(conn *sql.DB) error {
// Huge query moment. This is way too big to read comfortably, but too bad! It breaks the Jetbrains SQL formatter if
// I break it into multiple lines.
_, err = conn.Exec("CREATE TABLE packages (name TEXT NOT NULL UNIQUE, description TEXT NOT NULL, longDescription TEXT NOT NULL, version TEXT NOT NULL, author TEXT NOT NULL, license TEXT NOT NULL, architecture TEXT NOT NULL, size INTEGER NOT NULL, dependencies TEXT NOT NULL, removeScript TEXT NOT NULL, hasRemoveScript BOOLEAN NOT NULL, isDependency BOOLEAN NOT NULL DEFAULT false, repository TEXT, installedPaths TEXT, decompressedSize INTEGER NOT NULL)")
_, err = conn.Exec("CREATE TABLE packages (name TEXT NOT NULL UNIQUE, description TEXT NOT NULL, longDescription TEXT NOT NULL, version TEXT NOT NULL, author TEXT NOT NULL, license TEXT NOT NULL, architecture TEXT NOT NULL, size INTEGER NOT NULL, dependencies TEXT NOT NULL, removeScript TEXT NOT NULL, hasRemoveScript BOOLEAN NOT NULL, isDependency BOOLEAN NOT NULL DEFAULT false, repository TEXT, installedPaths TEXT)")
if err != nil {
return err
}
@ -528,21 +512,23 @@ func InitDB(conn *sql.DB) error {
return err
}
_, err = conn.Exec("CREATE TABLE repositories (url TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE, owner TEXT NOT NULL, description TEXT NOT NULL, ignore BOOLEAN NOT NULL DEFAULT false)")
if err != nil {
return err
}
_, err = conn.Exec("INSERT INTO repositories (url, name, owner, description, ignore) VALUES ('None', 'Local file', 'eon', 'This is a placeholder repository for local files to be registered to. Do not attempt to use this repository.', true)")
_, err = conn.Exec("CREATE TABLE repositories (url TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE, owner TEXT NOT NULL, description TEXT NOT NULL)")
if err != nil {
return err
}
// Way to big. Blame IntelliJ.
_, err = conn.Exec("CREATE TABLE remotePackages (name TEXT NOT NULL UNIQUE, author TEXT NOT NULL, description TEXT NOT NULL, version TEXT NOT NULL, architecture TEXT NOT NULL, size BLOB NOT NULL, dependencies TEXT NOT NULL, path TEXT NOT NULL, arch TEXT NOT NULL, hash TEXT NOT NULL, repository TEXT NOT NULL)")
if err != nil {
return err
}
// Having exists here makes sure that there is only one global row in the table. This is a bit of a hack,
// but it's the only way to make sure that the version and endian-ness are always part of the database.
// Note that this is the only hack in this program (so far)! I feel somewhat proud, especially since it's less of
// a hack and more not utilizing the full power of SQL.
// - Arzumify
_, err = conn.Exec("CREATE TABLE global (version TEXT NOT NULL, uniquenessCheck BOOLEAN NOT NULL UNIQUE CHECK (uniquenessCheck = true) DEFAULT true)")
if err != nil {
return err
@ -668,7 +654,7 @@ func EstablishDBConnection(logger *lib.Logger) error {
return nil
}
func DefaultAddEPKToDB(metadata *lib.Metadata, installedPaths []string, removeScript []byte, dependency bool, hasRemoveScript bool, size uint64, decompressedSize uint64, repository string) error {
func DefaultAddEPKToDB(metadata *lib.Metadata, installedPaths []string, removeScript []byte, dependency bool, hasRemoveScript bool, size int64, repository ...string) error {
// If it already exists, delete it. This may happen in a force-install scenario.
_, err := conn.Exec("DELETE FROM packages WHERE name = ?", metadata.Name)
if err != nil {
@ -700,10 +686,17 @@ func DefaultAddEPKToDB(metadata *lib.Metadata, installedPaths []string, removeSc
// Not that I'm complaining or anything.
// - Arzumify
_, err = conn.Exec("INSERT INTO packages (name, description, longDescription, version, author, license, architecture, size, dependencies, removeScript, hasRemoveScript, isDependency, repository, installedPaths, decompressedSize) VALUES (?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
if len(repository) > 0 {
_, err = conn.Exec("INSERT INTO packages (name, description, longDescription, version, author, license, architecture, size, dependencies, removeScript, hasRemoveScript, isDependency, repository, installedPaths) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
metadata.Name, metadata.Description, metadata.LongDescription, metadata.Version.String(), metadata.Author,
metadata.License, metadata.Architecture, size, dependencies, string(removeScript), hasRemoveScript, dependency,
repository, installedPathsString, decompressedSize)
repository[0], installedPathsString)
} else {
_, err = conn.Exec("INSERT INTO packages (name, description, longDescription, version, author, license, architecture, size, dependencies, removeScript, hasRemoveScript, isDependency, installedPaths) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
metadata.Name, metadata.Description, metadata.LongDescription, metadata.Version.String(), metadata.Author,
metadata.License, metadata.Architecture, size, dependencies, string(removeScript), hasRemoveScript, dependency,
installedPathsString)
}
if err != nil {
return err
}
@ -746,10 +739,11 @@ func DefaultGetEPKFromDB(name string) (lib.Metadata, string, bool, bool, int64,
var repository string
var installedPaths string
var size int64
err := conn.QueryRow("SELECT description, longDescription, version, author, license, architecture, size, dependencies, removeScript, hasRemoveScript, isDependency, repository, installedPaths, decompressedSize FROM packages WHERE name = ?",
err := conn.QueryRow("SELECT description, longDescription, version, author, license, architecture, size, "+
"dependencies, removeScript, hasRemoveScript, isDependency, repository, installedPaths FROM packages WHERE name = ?",
name).Scan(&metadata.Description, &metadata.LongDescription, &versionString, &metadata.Author,
&metadata.License, &metadata.Architecture, &size, &dependencies, &removeScript, &hasRemoveScript,
&dependency, &repository, &installedPaths, &metadata.DecompressedSize)
&dependency, &repository, &installedPaths)
if err != nil {
return lib.Metadata{}, "", false, false, 0, "", nil, err
}
@ -777,8 +771,8 @@ func DefaultRemoveEPKFromDB(name string) error {
return nil
}
func DefaultListEPKsInDB() (map[string]lib.DisplayData, map[string]uint64, map[string]lib.Repository, map[string]uint64, error) {
rows, err := conn.Query("SELECT name, description, version, author, architecture, size, dependencies, repository, isDependency, decompressedSize FROM packages")
func DefaultListEPKsInDB() (map[string]lib.DisplayData, map[string]*big.Int, map[string]lib.Repository, map[string]int64, error) {
rows, err := conn.Query("SELECT name, description, version, author, architecture, size, dependencies, repository FROM packages")
if err != nil {
return nil, nil, nil, nil, err
}
@ -786,21 +780,19 @@ func DefaultListEPKsInDB() (map[string]lib.DisplayData, map[string]uint64, map[s
repositoryMap := make(map[string]lib.Repository)
repoNameToRepo := make(map[string]lib.Repository)
metadataMap := make(map[string]lib.DisplayData)
sizes := make(map[string]uint64)
compressedSizes := make(map[string]uint64)
sizes := make(map[string]*big.Int)
compressedSizes := make(map[string]int64)
for rows.Next() {
var metadata lib.DisplayData
var size uint64
var size int64
var dependencies string
var repository string
var version string
var isDependency bool
err := rows.Scan(&metadata.Name, &metadata.Description, &version, &metadata.Author, &metadata.Architecture,
&size, &dependencies, &repository, &isDependency, &metadata.DecompressedSize)
err := rows.Scan(&metadata.Name, &metadata.Description, &version,
&metadata.Author, &metadata.Architecture, &size, &dependencies, &repository)
if err != nil {
return nil, nil, nil, nil, err
}
metadata.IsDependency = isDependency
semVer, err := semver.NewVersion(version)
if err != nil {
return nil, nil, nil, nil, err
@ -809,26 +801,21 @@ func DefaultListEPKsInDB() (map[string]lib.DisplayData, map[string]uint64, map[s
metadata.Version = *semVer
metadata.Dependencies = strings.Split(strings.TrimSuffix(strings.TrimPrefix(dependencies, "["), "]"), ", ")
metadataMap[metadata.Name] = metadata
sizes[metadata.Name] = size
sizes[metadata.Name] = big.NewInt(size)
compressedSizes[metadata.Name] = size
// Check if the repository is in the repoNameToRepo map
var repo lib.Repository
repo, ok := repoNameToRepo[repository]
_, ok := repoNameToRepo[repository]
if !ok {
// If it's not, find the repository and add it to the map
repo.Name = repository
var repo lib.Repository
err := conn.QueryRow("SELECT url, owner, description FROM repositories WHERE name = ?", repository).Scan(&repo.URL, &repo.Owner, &repo.Description)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil, nil, nil, errors.New("repository " + repository + " not found")
} else {
return nil, nil, nil, nil, err
}
}
repoNameToRepo[repository] = repo
repositoryMap[repository] = repo
}
// Add the repository to the repository map
repositoryMap[metadata.Name] = repo
repositoryMap[metadata.Name] = repoNameToRepo[repository]
}
return metadataMap, sizes, repositoryMap, compressedSizes, nil
@ -930,7 +917,7 @@ func DefaultCheckRepositoryInDB(repository string) (bool, error) {
}
func DefaultListRepositoriesInDB() ([]lib.Repository, error) {
rows, err := conn.Query("SELECT url, name, owner, description FROM repositories WHERE ignore = false")
rows, err := conn.Query("SELECT url, name, owner, description FROM repositories")
if err != nil {
return []lib.Repository{}, err
}
@ -995,7 +982,7 @@ func DefaultListRemotePackagesInDB() ([]lib.RemoteEPK, error) {
for rows.Next() {
var name, author, description, version, architecture, dependencies, path, arch, repository string
var hashBytes []byte
var size uint64
var size int64
err := rows.Scan(&name, &author, &description, &version, &architecture, &size, &dependencies, &path, &arch,
&hashBytes, &repository)
if err != nil {

View file

@ -15,6 +15,7 @@ import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"math/big"
"net/http"
"net/url"
"os/exec"
@ -34,7 +35,7 @@ type RemoteEPK struct {
Description string
Version semver.Version
Architecture string
CompressedSize uint64
CompressedSize int64
Dependencies []string
Path string
Arch string
@ -66,8 +67,8 @@ type Metadata struct {
Architecture string
Dependencies []string
SpecialFiles SpecialFiles
Size uint64
DecompressedSize uint64
Size int64
DecompressedSize *big.Int
}
// EPKPreMap is a struct that contains the metadata of the EPK
@ -76,7 +77,7 @@ type EPKPreMap struct {
MetadataMap map[string]interface{}
IsLittleEndian bool
IsUpgrade bool
TarOffset uint64
TarOffset int64
}
// DisplayData is a struct that contains the display data of the EPK
@ -86,10 +87,9 @@ type DisplayData struct {
Architecture string
Description string
Version semver.Version
Size uint64
DecompressedSize uint64
Size int64
DecompressedSize *big.Int
Dependencies []string
IsDependency bool
}
// PotentiallyNullEPKPreMap is a EPKPreMap that can be nil
@ -104,8 +104,8 @@ type Log struct {
Content string
Prompt bool
PlaySound bool
Progress uint64
Total uint64
Progress *big.Int
Total *big.Int
Overwrite bool
}
@ -202,12 +202,10 @@ func MkdirAllWithPaths(path string, perm os.FileMode) ([]string, error) {
return createdDirs, nil
}
func preMapEpkFromBytes(metaDataBytes []byte, littleEndian bool, size uint64, offset uint64) (EPKPreMap, error) {
func preMapEpkFromBytes(metaDataBytes []byte, littleEndian bool, size int64, offset int64) (EPKPreMap, error) {
// Unmarshal the JSON
var displayDataMap map[string]interface{}
decoder := json.NewDecoder(bytes.NewReader(metaDataBytes))
decoder.UseNumber()
err := decoder.Decode(&displayDataMap)
err := json.Unmarshal(metaDataBytes, &displayDataMap)
if err != nil {
return EPKPreMap{}, errors.New("metadata is not valid JSON")
}
@ -224,14 +222,12 @@ func preMapEpkFromBytes(metaDataBytes []byte, littleEndian bool, size uint64, of
// Map the display data
var ok bool
// Set the size
sizeJSON, ok := displayDataMap["size"].(json.Number)
sizeBigInt, ok := displayDataMap["size"].(string)
if !ok {
return EPKPreMap{}, errors.New("size is not a number")
}
parsedDisplayData.DisplayData.DecompressedSize, err = strconv.ParseUint(sizeJSON.String(), 10, 64)
if err != nil {
return EPKPreMap{}, err
return EPKPreMap{}, errors.New("size is not a string")
}
parsedDisplayData.DisplayData.DecompressedSize = new(big.Int)
parsedDisplayData.DisplayData.DecompressedSize.SetString(sizeBigInt, 10)
// Set the name, author, version, arch, and dependencies
parsedDisplayData.DisplayData.Name, ok = displayDataMap["name"].(string)
if !ok {
@ -267,7 +263,7 @@ func preMapEpkFromBytes(metaDataBytes []byte, littleEndian bool, size uint64, of
}
// ConstMapEPKMetadataOffset is the offset of the metadata in the EPK: 3 magic bytes, 1 endian byte, 8 offset bytes, 64 signature bytes, and 32 public key bytes
var ConstMapEPKMetadataOffset uint64 = 108
var ConstMapEPKMetadataOffset int64 = 108
var ErrPreMapEPKCouldNotRead = errors.New("could not read EPK")
var ErrPreMapEPKHasNetworkStream = errors.New("network streams are not supported")
@ -276,7 +272,7 @@ var ErrPreMapEPKHasInvalidEndian = errors.New("has invalid endian")
var ErrPreMapEPKCouldNotMapJSON = errors.New("could not map metadata")
// PreMapEPK maps enough data to create the display summary of an EPK
func PreMapEPK(epkBytes StreamOrBytes, epkSize uint64) (EPKPreMap, error, error) {
func PreMapEPK(epkBytes StreamOrBytes, epkSize int64) (EPKPreMap, error, error) {
// Say that we don't support network streams
if epkBytes.IsURL {
return EPKPreMap{}, nil, ErrPreMapEPKHasNetworkStream
@ -324,7 +320,7 @@ func PreMapEPK(epkBytes StreamOrBytes, epkSize uint64) (EPKPreMap, error, error)
}
// Now we can get the offsets of the tar archive
var tarArchiveOffset uint64
var tarArchiveOffset int64
if epkBytes.IsFileStream {
var tarArchiveOffsetBytes = make([]byte, 8)
_, err := epkBytes.FileStream.ReadAt(tarArchiveOffsetBytes, 4)
@ -332,15 +328,15 @@ func PreMapEPK(epkBytes StreamOrBytes, epkSize uint64) (EPKPreMap, error, error)
return EPKPreMap{}, err, ErrPreMapEPKCouldNotRead
}
if littleEndian {
tarArchiveOffset = binary.LittleEndian.Uint64(tarArchiveOffsetBytes)
tarArchiveOffset = int64(binary.LittleEndian.Uint64(tarArchiveOffsetBytes))
} else {
tarArchiveOffset = binary.BigEndian.Uint64(tarArchiveOffsetBytes)
tarArchiveOffset = int64(binary.BigEndian.Uint64(tarArchiveOffsetBytes))
}
} else {
if littleEndian {
tarArchiveOffset = binary.LittleEndian.Uint64(epkBytes.Bytes[4:12])
tarArchiveOffset = int64(binary.LittleEndian.Uint64(epkBytes.Bytes[4:12]))
} else {
tarArchiveOffset = binary.BigEndian.Uint64(epkBytes.Bytes[4:12])
tarArchiveOffset = int64(binary.BigEndian.Uint64(epkBytes.Bytes[4:12]))
}
}
@ -349,14 +345,11 @@ func PreMapEPK(epkBytes StreamOrBytes, epkSize uint64) (EPKPreMap, error, error)
var preMapEpk EPKPreMap
if epkBytes.IsFileStream {
var metadataBuffer = make([]byte, tarArchiveOffset-ConstMapEPKMetadataOffset)
_, err := epkBytes.FileStream.ReadAt(metadataBuffer, int64(ConstMapEPKMetadataOffset))
_, err := epkBytes.FileStream.ReadAt(metadataBuffer, ConstMapEPKMetadataOffset)
if err != nil {
return EPKPreMap{}, err, ErrPreMapEPKCouldNotRead
}
preMapEpk, err = preMapEpkFromBytes(metadataBuffer, littleEndian, epkSize, tarArchiveOffset)
if err != nil {
return EPKPreMap{}, err, ErrPreMapEPKCouldNotMapJSON
}
} else {
var err error
preMapEpk, err = preMapEpkFromBytes(epkBytes.Bytes[ConstMapEPKMetadataOffset:tarArchiveOffset], littleEndian, epkSize, tarArchiveOffset)
@ -430,9 +423,6 @@ func PreMapRemoteEPK(remoteEPK RemoteEPK, logger *Logger) (EPKPreMap, error, err
if err != nil {
return EPKPreMap{}, err, ErrPreMapRemoteEPKCouldNotCloseConnection
}
} else if resp.StatusCode == 404 {
// Repository not found
return EPKPreMap{}, errors.New("repository not found: " + strconv.Itoa(resp.StatusCode)), ErrPreMapRemoteEPKUnexpectedStatusCode
} else {
// Something went wrong
return EPKPreMap{}, errors.New("unexpected status code: " + strconv.Itoa(resp.StatusCode)), ErrPreMapRemoteEPKUnexpectedStatusCode
@ -454,11 +444,11 @@ func PreMapRemoteEPK(remoteEPK RemoteEPK, logger *Logger) (EPKPreMap, error, err
}
// Now we can get the offsets of the tar archive
var tarArchiveOffset uint64
var tarArchiveOffset int64
if littleEndian {
tarArchiveOffset = binary.LittleEndian.Uint64(epkHeaderBytes[4:12])
tarArchiveOffset = int64(binary.LittleEndian.Uint64(epkHeaderBytes[4:12]))
} else {
tarArchiveOffset = binary.BigEndian.Uint64(epkHeaderBytes[4:12])
tarArchiveOffset = int64(binary.BigEndian.Uint64(epkHeaderBytes[4:12]))
}
// No signature verification for you
@ -467,7 +457,7 @@ func PreMapRemoteEPK(remoteEPK RemoteEPK, logger *Logger) (EPKPreMap, error, err
displayDataBytes := make([]byte, tarArchiveOffset-ConstMapEPKMetadataOffset)
if rangeSupported {
// Send another request to fetch the display data
req.Header.Set("Range", "bytes=108-"+strconv.FormatUint(tarArchiveOffset-1, 10))
req.Header.Set("Range", "bytes=108-"+strconv.FormatInt(tarArchiveOffset-1, 10))
resp, err = http.DefaultClient.Do(req)
if err != nil {
return EPKPreMap{}, err, ErrPreMapRemoteEPKCouldNotSendRequest
@ -699,7 +689,7 @@ func FullyMapMetadata(epkBytes StreamOrBytes, preMap *EPKPreMap, checkFingerprin
if epkBytes.IsFileStream {
// Now we can verify the signature. First, we need to take the checksum of the metadata
// Seeking is better than using ReadAt because it allows us to not have to load the entire file into memory
_, err = epkBytes.FileStream.Seek(int64(ConstMapEPKMetadataOffset), io.SeekStart)
_, err = epkBytes.FileStream.Seek(ConstMapEPKMetadataOffset, io.SeekStart)
if err != nil {
return &Metadata{}, err, ErrFullyMapMetadataCouldNotJump
}
@ -798,14 +788,12 @@ func FullyMapMetadata(epkBytes StreamOrBytes, preMap *EPKPreMap, checkFingerprin
if !ok {
return &Metadata{}, errors.New("license is not a string"), ErrFullyMapMetadataCouldNotMapJSON
}
decompressedSizeJSON, ok := preMap.MetadataMap["size"].(json.Number)
decompressedSizeString, ok := preMap.MetadataMap["size"].(string)
if !ok {
return &Metadata{}, errors.New("size is not a number"), ErrFullyMapMetadataCouldNotMapJSON
}
parsedMetadata.DecompressedSize, err = strconv.ParseUint(decompressedSizeJSON.String(), 10, 64)
if err != nil {
return &Metadata{}, err, ErrFullyMapMetadataCouldNotMapJSON
return &Metadata{}, errors.New("size is not a string"), ErrFullyMapMetadataCouldNotMapJSON
}
parsedMetadata.DecompressedSize = new(big.Int)
parsedMetadata.DecompressedSize.SetString(decompressedSizeString, 10)
return &parsedMetadata, nil, nil
}
@ -826,15 +814,15 @@ var ErrInstallEPKCouldNotRemoveTempDir = errors.New("could not remove temporary
// ProgressWriter implements a writer that intercepts writes in order to log progress
type ProgressWriter struct {
Logger *Logger
Total uint64
Total *big.Int
Writer io.Writer
}
// Write writes to the ProgressWriter
func (writer *ProgressWriter) Write(p []byte) (n int, err error) {
var byteCount uint64
byteCount := new(big.Int)
for range p {
byteCount++
byteCount.Add(byteCount, big.NewInt(1))
}
if writer.Logger.ProgressSupported {
writer.Logger.LogFunc(Log{
@ -846,7 +834,7 @@ func (writer *ProgressWriter) Write(p []byte) (n int, err error) {
} else {
writer.Logger.LogFunc(Log{
Level: "INFO",
Content: "Written " + humanize.Bytes(byteCount) + " out of " + humanize.Bytes(writer.Total),
Content: "Written " + humanize.BigIBytes(byteCount) + " out of " + humanize.BigIBytes(writer.Total),
Prompt: false,
})
}
@ -860,7 +848,7 @@ func (writer *ProgressWriter) Write(p []byte) (n int, err error) {
}
// InstallEPK installs an EPK file
func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, addEPKToDB func(*Metadata, []string, []byte, bool, bool, uint64, uint64, string) error, logger *Logger) (string, error, error) {
func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, addEPKToDB func(*Metadata, []string, []byte, bool, bool, int64, ...string) error, logger *Logger) (string, error, error) {
// Create the temporary directory
tempDir, err := os.MkdirTemp("/tmp", "eon-install-")
if err != nil {
@ -871,7 +859,7 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
var connection io.ReadCloser
if epkBytes.IsFileStream {
// Seek to the correct position in the EPK
_, err = epkBytes.FileStream.Seek(int64(preMap.TarOffset), io.SeekStart)
_, err = epkBytes.FileStream.Seek(preMap.TarOffset, io.SeekStart)
if err != nil {
return "", err, nil
}
@ -888,7 +876,7 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
return tempDir, err, ErrInstallEPKCouldNotCreateZStandardReader
}
// Set the range header
req.Header.Set("Range", "bytes="+strconv.FormatUint(preMap.TarOffset, 10)+"-")
req.Header.Set("Range", "bytes="+strconv.FormatInt(preMap.TarOffset, 10)+"-")
// Send the request
resp, err := http.DefaultClient.Do(req)
if err != nil {
@ -903,9 +891,9 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
// God this is painful. Let's give the user a progress bar to make it less painful
_, err := io.CopyN(&ProgressWriter{
Logger: logger,
Total: preMap.TarOffset,
Total: big.NewInt(preMap.TarOffset),
Writer: io.Discard,
}, connection, int64(preMap.TarOffset))
}, connection, preMap.TarOffset)
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotDecompressTarArchive
}
@ -930,7 +918,7 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
tarReader := tar.NewReader(zStandardReader)
// Create a goroutine to see how much of the decompressed size we have decompressed
var written uint64
written := new(big.Int)
stop := make(chan bool)
go func() {
for {
@ -948,7 +936,7 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
} else {
logger.LogFunc(Log{
Level: "INFO",
Content: "Decompressed " + humanize.Bytes(written) + " of " + humanize.Bytes(metadata.DecompressedSize),
Content: "Decompressed " + humanize.Bytes(uint64(written.Int64())) + " of " + humanize.Bytes(uint64(metadata.DecompressedSize.Int64())),
Prompt: false,
})
time.Sleep(1 * time.Second)
@ -1078,65 +1066,11 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
return tempDir, err, ErrInstallEPKCouldNotDecompressTarArchive
}
written += uint64(writtenFile)
written.Add(written, big.NewInt(writtenFile))
err = file.Close()
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotCloseTarReader
} else {
if !isHook {
// Add the file to the installed files
installedFiles = append(installedFiles, target)
}
}
} else {
return tempDir, err, ErrInstallEPKCouldNotStatFile
}
} else {
// See if it's an upgrade or not
if preMap.IsUpgrade {
// Check if it's a special file
for _, file := range metadata.SpecialFiles.NoReplace {
if strings.TrimSuffix(target, "/") == strings.TrimSuffix(file, "/") {
// This file is a special file and should not be replaced
continue
}
}
}
// It's not special, so we can replace it
file, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.FileMode(header.Mode))
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotCreateFile
}
writtenFile, err := io.Copy(file, tarReader)
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotDecompressTarArchive
}
written += uint64(writtenFile)
err = file.Close()
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotCloseTarReader
} else {
if !isHook {
// Add the file to the installed files
installedFiles = append(installedFiles, target)
}
}
}
case tar.TypeSymlink:
// Check if the symlink has anywhere to go
_, err := os.Stat(filepath.Dir(target))
if err != nil {
// No, it doesn't. Create the directory
if errors.Is(err, os.ErrNotExist) {
// We assume 0755 for directories
paths, err := MkdirAllWithPaths(filepath.Dir(target), 0755)
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotCreateDir
} else {
// Check if the files are in noDelete
for _, file := range metadata.SpecialFiles.NoDelete {
@ -1146,32 +1080,7 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
}
}
if !isHook {
// Add the directory to the installed files
installedFiles = append(installedFiles, filepath.Dir(target))
// Add the paths to the installed files
if paths != nil {
installedFiles = append(installedFiles, paths...)
}
}
}
} else {
return tempDir, err, ErrInstallEPKCouldNotStatDir
}
}
// Check if the symlink already exists
_, err = os.Lstat(target)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// Great, the symlink does not exist. Let's create it.
err = os.Symlink(header.Linkname, target)
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotCreateFile
} else {
if !isHook {
// Add the symlink to the installed files
// Add the file to the installed files
installedFiles = append(installedFiles, target)
}
}
@ -1189,22 +1098,6 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
}
}
}
// It's not special, so we can replace it
err = os.Remove(target)
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotCreateFile
}
err = os.Symlink(header.Linkname, target)
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotCreateFile
} else {
if !isHook {
// Add the symlink to the installed files
installedFiles = append(installedFiles, target)
}
}
}
}
}
@ -1296,24 +1189,24 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
file, err := os.ReadFile(filepath.Join(tempDir, "hooks", "remove.sh"))
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return tempDir, err, ErrInstallEPKCouldNotAddEPKToDB
} else {
var err error
if !epkBytes.IsRemote {
err = addEPKToDB(metadata, installedFiles, []byte{}, preMap.DisplayData.IsDependency, false, metadata.Size, metadata.DecompressedSize, "Local file")
err = addEPKToDB(metadata, installedFiles, []byte{}, false, false, metadata.Size)
} else {
err = addEPKToDB(metadata, installedFiles, []byte{}, preMap.DisplayData.IsDependency, false, metadata.Size, metadata.DecompressedSize, epkBytes.RepositoryName)
err = addEPKToDB(metadata, installedFiles, []byte{}, false, false, metadata.Size, epkBytes.RepositoryName)
}
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotAddEPKToDB
}
} else {
return tempDir, err, ErrInstallEPKCouldNotAddEPKToDB
}
} else {
var err error
if !epkBytes.IsRemote {
err = addEPKToDB(metadata, installedFiles, file, preMap.DisplayData.IsDependency, true, metadata.Size, metadata.DecompressedSize, "Local file")
err = addEPKToDB(metadata, installedFiles, file, false, true, metadata.Size)
} else {
err = addEPKToDB(metadata, installedFiles, file, preMap.DisplayData.IsDependency, true, metadata.Size, metadata.DecompressedSize, epkBytes.RepositoryName)
err = addEPKToDB(metadata, installedFiles, file, false, true, metadata.Size, epkBytes.RepositoryName)
}
if err != nil {
return tempDir, err, ErrInstallEPKCouldNotAddEPKToDB
@ -1329,8 +1222,8 @@ func InstallEPK(epkBytes StreamOrBytes, metadata *Metadata, preMap *EPKPreMap, a
stop <- true
logger.LogFunc(Log{
Level: "PROGRESS",
Progress: 1,
Total: 1,
Progress: big.NewInt(1),
Total: big.NewInt(1),
Overwrite: true,
})
@ -1520,12 +1413,12 @@ func AddRepository(url string, addRepositoryToDB func(Repository, bool) error, g
return "", errors.New("package version is not a valid semver version"), ErrAddRepositoryHasInvalidMetadata
}
sizeString, ok := epk["size"].(json.Number)
sizeString, ok := epk["size"].(string)
if !ok {
return "", errors.New("package size is not a number"), ErrAddRepositoryHasInvalidMetadata
return "", errors.New("package size is not a string"), ErrAddRepositoryHasInvalidMetadata
}
size, err := strconv.ParseUint(sizeString.String(), 10, 64)
size, err := strconv.ParseInt(sizeString, 10, 64)
if err != nil {
return "", errors.New("package size is not a number"), ErrAddRepositoryHasInvalidMetadata
}

View file

@ -1,9 +0,0 @@
package main
import (
"fmt"
)
func main() {
fmt.Println("Run the makefile to build the project, not go build")
}