Skip to content

Commit

Permalink
🐛 Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cookiengineer committed Sep 8, 2024
1 parent 4aaddbf commit 1c0c9f4
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 11 deletions.
2 changes: 2 additions & 0 deletions source/actions/Cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ func Cleanup(pkgs_folder string) bool {

var result bool

// TODO: Implement cleanup method, which deletes all old versions of packages, per architecture

return result

}
88 changes: 85 additions & 3 deletions source/actions/Upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,102 @@ package actions
import "pacman-backup/console"
import "pacman-backup/pacman"
import "os"
import "strconv"
import "strings"

func Upgrade(sync_folder string, pkgs_folder string) bool {
func isIgnored(config *pacman.Config, name_and_version string) bool {

var result bool = false

name := name_and_version[0:strings.Index(name_and_version, "@")]

if result == false {

for i := 0; i < len(config.Options.HoldPkg); i++ {

if config.Options.HoldPkg[i] == name {
result = true
break
}

}

}

if result == false {

for i := 0; i < len(config.Options.IgnoreGroup); i++ {

if config.Options.IgnoreGroup[i] == name {
result = true
break
}

}

}

if result == false {

for i := 0; i < len(config.Options.IgnorePkg); i++ {

if config.Options.IgnorePkg[i] == name {
result = true
break
}

}

}

return result

}

func Upgrade(mirror_url string, sync_folder string, pkgs_folder string) bool {

console.Group("Upgrade")

var result bool

config := pacman.NewConfig("", sync_folder, pkgs_folder)
local_config := pacman.InitConfig()
config := pacman.NewConfig(mirror_url, sync_folder, pkgs_folder)
config.Options.SyncFirst = []string{"archlinux-keyring"}

if len(local_config.Options.IgnoreGroup) > 0 {

for i := 0; i < len(local_config.Options.IgnoreGroup); i++ {
config.Options.IgnoreGroup = append(config.Options.IgnoreGroup, local_config.Options.IgnoreGroup[i])
}

}

if len(local_config.Options.IgnorePkg) > 0 {

for i := 0; i < len(local_config.Options.IgnorePkg); i++ {
config.Options.IgnorePkg = append(config.Options.IgnorePkg, local_config.Options.IgnorePkg[i])
}

}

if len(local_config.Options.HoldPkg) > 0 {

for h := 0; h < len(local_config.Options.HoldPkg); h++ {
config.Options.HoldPkg = append(config.Options.HoldPkg, local_config.Options.HoldPkg[h])
}

}

err1 := os.WriteFile("/tmp/pacman-backup.conf", []byte(config.String()), 0666)

if err1 == nil {

files := pacman.CollectFiles("/tmp/pacman-backup.conf", pkgs_folder)
updates := pacman.CollectUpdates("/tmp/pacman-backup.conf")

console.Log("Found " + strconv.Itoa(len(updates)) + " available updates")
console.Log("Found " + strconv.Itoa(len(files)) + " cached updates")

cache := make(map[string]bool, 0)
verified := true

Expand All @@ -40,7 +122,7 @@ func Upgrade(sync_folder string, pkgs_folder string) bool {

for name, is_cached := range cache {

if is_cached == false {
if is_cached == false && !isIgnored(&config, name) {
console.Error("-> Package " + name + " not available")
verified = false
}
Expand Down
8 changes: 6 additions & 2 deletions source/cmds/pacman-backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ func main() {
// pacman-backup upgrade /mnt/usb-drive
if isFolder(os.Args[2]) {

config := pacman.InitConfig()
mirror := config.ToMirror()

if !isFolder(os.Args[2] + "/sync") {
makeFolder(os.Args[2] + "/sync")
}
Expand All @@ -230,7 +233,7 @@ func main() {
makeFolder(os.Args[2] + "/pkgs")
}

actions.Upgrade(os.Args[2] + "/sync", os.Args[2] + "/pkgs")
actions.Upgrade(mirror, os.Args[2] + "/sync", os.Args[2] + "/pkgs")

}

Expand Down Expand Up @@ -276,9 +279,10 @@ func main() {
} else if action == "upgrade" {

config := pacman.InitConfig()
mirror := config.ToMirror()

if isFolder(config.Options.CacheDir) {
actions.Upgrade(config.Options.DBPath + "/sync", config.Options.CacheDir)
actions.Upgrade(mirror, config.Options.DBPath + "/sync", config.Options.CacheDir)
}

} else {
Expand Down
3 changes: 3 additions & 0 deletions source/pacman/CollectFile.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pacman

import "pacman-backup/console"
import "pacman-backup/structs"
import "os/exec"

Expand All @@ -12,6 +13,8 @@ func CollectFile(config string, filepath string) structs.Package {

if err == nil {
ParsePackage(string(buffer), &result)
} else {
console.Error(err.Error())
}

return result
Expand Down
3 changes: 3 additions & 0 deletions source/pacman/CollectUpdate.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pacman

import "pacman-backup/console"
import "pacman-backup/structs"
import "os"
import "os/exec"
Expand All @@ -16,6 +17,8 @@ func CollectUpdate(config string, name string) structs.Package {

if err == nil {
ParsePackage(string(buffer), &result)
} else {
console.Error(err.Error())
}

return result
Expand Down
53 changes: 47 additions & 6 deletions source/pacman/CollectUpdates.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
package pacman

import "pacman-backup/console"
import "pacman-backup/structs"
import "os"
import "os/exec"
import "strings"

func CollectUpdates(config string) []structs.Package {

update_index := make(map[string]bool, 0)

os.Setenv("TZ", "Europe/Greenwich")
os.Setenv("LC_TIME", "en_US")

cmd := exec.Command("pacman", "-Qui", "--noconfirm", "--config", config)
buffer, err := cmd.Output()
cmd1 := exec.Command("pacman", "-Qu", "--noconfirm", "--config", config)
buffer1, err1 := cmd1.Output()

if err1 == nil {

lines := strings.Split(strings.TrimSpace(string(buffer1)), "\n")

for l := 0; l < len(lines); l++ {

line := strings.TrimSpace(lines[l])

if strings.HasSuffix(line, "[ignored]") {
line = strings.TrimSpace(line[0 : len(line)-9])
}

if strings.Contains(line, " ") && strings.Contains(line, " -> ") {

// "package 1.2.3 -> 1.2.4"

name := line[0:strings.Index(line, " ")]
update_index[name] = true

}

}

} else {
console.Error(err1.Error())
}

cmd := exec.Command("pacman", "-Si", "--noconfirm", "--config", config)
buffer, err2 := cmd.Output()

result := make([]structs.Package, 0)

if err == nil {
if err2 == nil {

blocks := strings.Split("\n\n"+strings.TrimSpace(string(buffer)), "\n\nName")
blocks := strings.Split("\n\n"+strings.TrimSpace(string(buffer)), "\n\nRepository")

for b := 0; b < len(blocks); b++ {

Expand All @@ -26,16 +59,24 @@ func CollectUpdates(config string) []structs.Package {
if block != "" {

update := structs.NewPackage("pacman")
ParsePackage("Name "+block, &update)
ParsePackage("Repository "+block, &update)

if update.Name != "" && update.Version.IsValid() {
result = append(result, update)

_, ok := update_index[update.Name]

if ok == true {
result = append(result, update)
}

}

}

}

} else {
console.Error(err2.Error())
}

return result
Expand Down
3 changes: 3 additions & 0 deletions source/pacman/Download.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pacman

import "pacman-backup/console"
import "os/exec"

func Download(config string, name string) bool {
Expand All @@ -11,6 +12,8 @@ func Download(config string, name string) bool {

if err == nil {
result = true
} else {
console.Error(err.Error())
}

return result
Expand Down
3 changes: 3 additions & 0 deletions source/pacman/Sync.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pacman

import "pacman-backup/console"
import "os/exec"

func Sync(config string) bool {
Expand All @@ -11,6 +12,8 @@ func Sync(config string) bool {

if err == nil {
result = true
} else {
console.Error(err.Error())
}

return result
Expand Down
3 changes: 3 additions & 0 deletions source/pacman/Upgrade.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pacman

import "pacman-backup/console"
import "os/exec"

func Upgrade(config string) bool {
Expand All @@ -11,6 +12,8 @@ func Upgrade(config string) bool {

if err == nil {
result = true
} else {
console.Error(err.Error())
}

return result
Expand Down

0 comments on commit 1c0c9f4

Please sign in to comment.