Skip to content

Commit 1c0c9f4

Browse files
committed
🐛 Fixes
1 parent 4aaddbf commit 1c0c9f4

File tree

9 files changed

+155
-11
lines changed

9 files changed

+155
-11
lines changed

source/actions/Cleanup.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ func Cleanup(pkgs_folder string) bool {
77

88
var result bool
99

10+
// TODO: Implement cleanup method, which deletes all old versions of packages, per architecture
11+
1012
return result
1113

1214
}

source/actions/Upgrade.go

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,102 @@ package actions
33
import "pacman-backup/console"
44
import "pacman-backup/pacman"
55
import "os"
6+
import "strconv"
7+
import "strings"
68

7-
func Upgrade(sync_folder string, pkgs_folder string) bool {
9+
func isIgnored(config *pacman.Config, name_and_version string) bool {
10+
11+
var result bool = false
12+
13+
name := name_and_version[0:strings.Index(name_and_version, "@")]
14+
15+
if result == false {
16+
17+
for i := 0; i < len(config.Options.HoldPkg); i++ {
18+
19+
if config.Options.HoldPkg[i] == name {
20+
result = true
21+
break
22+
}
23+
24+
}
25+
26+
}
27+
28+
if result == false {
29+
30+
for i := 0; i < len(config.Options.IgnoreGroup); i++ {
31+
32+
if config.Options.IgnoreGroup[i] == name {
33+
result = true
34+
break
35+
}
36+
37+
}
38+
39+
}
40+
41+
if result == false {
42+
43+
for i := 0; i < len(config.Options.IgnorePkg); i++ {
44+
45+
if config.Options.IgnorePkg[i] == name {
46+
result = true
47+
break
48+
}
49+
50+
}
51+
52+
}
53+
54+
return result
55+
56+
}
57+
58+
func Upgrade(mirror_url string, sync_folder string, pkgs_folder string) bool {
859

960
console.Group("Upgrade")
1061

1162
var result bool
1263

13-
config := pacman.NewConfig("", sync_folder, pkgs_folder)
64+
local_config := pacman.InitConfig()
65+
config := pacman.NewConfig(mirror_url, sync_folder, pkgs_folder)
66+
config.Options.SyncFirst = []string{"archlinux-keyring"}
67+
68+
if len(local_config.Options.IgnoreGroup) > 0 {
69+
70+
for i := 0; i < len(local_config.Options.IgnoreGroup); i++ {
71+
config.Options.IgnoreGroup = append(config.Options.IgnoreGroup, local_config.Options.IgnoreGroup[i])
72+
}
73+
74+
}
75+
76+
if len(local_config.Options.IgnorePkg) > 0 {
77+
78+
for i := 0; i < len(local_config.Options.IgnorePkg); i++ {
79+
config.Options.IgnorePkg = append(config.Options.IgnorePkg, local_config.Options.IgnorePkg[i])
80+
}
81+
82+
}
83+
84+
if len(local_config.Options.HoldPkg) > 0 {
85+
86+
for h := 0; h < len(local_config.Options.HoldPkg); h++ {
87+
config.Options.HoldPkg = append(config.Options.HoldPkg, local_config.Options.HoldPkg[h])
88+
}
89+
90+
}
91+
1492
err1 := os.WriteFile("/tmp/pacman-backup.conf", []byte(config.String()), 0666)
1593

1694
if err1 == nil {
1795

1896
files := pacman.CollectFiles("/tmp/pacman-backup.conf", pkgs_folder)
1997
updates := pacman.CollectUpdates("/tmp/pacman-backup.conf")
98+
99+
console.Log("Found " + strconv.Itoa(len(updates)) + " available updates")
100+
console.Log("Found " + strconv.Itoa(len(files)) + " cached updates")
101+
20102
cache := make(map[string]bool, 0)
21103
verified := true
22104

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

41123
for name, is_cached := range cache {
42124

43-
if is_cached == false {
125+
if is_cached == false && !isIgnored(&config, name) {
44126
console.Error("-> Package " + name + " not available")
45127
verified = false
46128
}

source/cmds/pacman-backup/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ func main() {
222222
// pacman-backup upgrade /mnt/usb-drive
223223
if isFolder(os.Args[2]) {
224224

225+
config := pacman.InitConfig()
226+
mirror := config.ToMirror()
227+
225228
if !isFolder(os.Args[2] + "/sync") {
226229
makeFolder(os.Args[2] + "/sync")
227230
}
@@ -230,7 +233,7 @@ func main() {
230233
makeFolder(os.Args[2] + "/pkgs")
231234
}
232235

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

235238
}
236239

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

278281
config := pacman.InitConfig()
282+
mirror := config.ToMirror()
279283

280284
if isFolder(config.Options.CacheDir) {
281-
actions.Upgrade(config.Options.DBPath + "/sync", config.Options.CacheDir)
285+
actions.Upgrade(mirror, config.Options.DBPath + "/sync", config.Options.CacheDir)
282286
}
283287

284288
} else {

source/pacman/CollectFile.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pacman
22

3+
import "pacman-backup/console"
34
import "pacman-backup/structs"
45
import "os/exec"
56

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

1314
if err == nil {
1415
ParsePackage(string(buffer), &result)
16+
} else {
17+
console.Error(err.Error())
1518
}
1619

1720
return result

source/pacman/CollectUpdate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pacman
22

3+
import "pacman-backup/console"
34
import "pacman-backup/structs"
45
import "os"
56
import "os/exec"
@@ -16,6 +17,8 @@ func CollectUpdate(config string, name string) structs.Package {
1617

1718
if err == nil {
1819
ParsePackage(string(buffer), &result)
20+
} else {
21+
console.Error(err.Error())
1922
}
2023

2124
return result

source/pacman/CollectUpdates.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,56 @@
11
package pacman
22

3+
import "pacman-backup/console"
34
import "pacman-backup/structs"
45
import "os"
56
import "os/exec"
67
import "strings"
78

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

11+
update_index := make(map[string]bool, 0)
12+
1013
os.Setenv("TZ", "Europe/Greenwich")
1114
os.Setenv("LC_TIME", "en_US")
1215

13-
cmd := exec.Command("pacman", "-Qui", "--noconfirm", "--config", config)
14-
buffer, err := cmd.Output()
16+
cmd1 := exec.Command("pacman", "-Qu", "--noconfirm", "--config", config)
17+
buffer1, err1 := cmd1.Output()
18+
19+
if err1 == nil {
20+
21+
lines := strings.Split(strings.TrimSpace(string(buffer1)), "\n")
22+
23+
for l := 0; l < len(lines); l++ {
24+
25+
line := strings.TrimSpace(lines[l])
26+
27+
if strings.HasSuffix(line, "[ignored]") {
28+
line = strings.TrimSpace(line[0 : len(line)-9])
29+
}
30+
31+
if strings.Contains(line, " ") && strings.Contains(line, " -> ") {
32+
33+
// "package 1.2.3 -> 1.2.4"
34+
35+
name := line[0:strings.Index(line, " ")]
36+
update_index[name] = true
37+
38+
}
39+
40+
}
41+
42+
} else {
43+
console.Error(err1.Error())
44+
}
45+
46+
cmd := exec.Command("pacman", "-Si", "--noconfirm", "--config", config)
47+
buffer, err2 := cmd.Output()
1548

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

18-
if err == nil {
51+
if err2 == nil {
1952

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

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

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

2861
update := structs.NewPackage("pacman")
29-
ParsePackage("Name "+block, &update)
62+
ParsePackage("Repository "+block, &update)
3063

3164
if update.Name != "" && update.Version.IsValid() {
32-
result = append(result, update)
65+
66+
_, ok := update_index[update.Name]
67+
68+
if ok == true {
69+
result = append(result, update)
70+
}
71+
3372
}
3473

3574
}
3675

3776
}
3877

78+
} else {
79+
console.Error(err2.Error())
3980
}
4081

4182
return result

source/pacman/Download.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pacman
22

3+
import "pacman-backup/console"
34
import "os/exec"
45

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

1213
if err == nil {
1314
result = true
15+
} else {
16+
console.Error(err.Error())
1417
}
1518

1619
return result

source/pacman/Sync.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pacman
22

3+
import "pacman-backup/console"
34
import "os/exec"
45

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

1213
if err == nil {
1314
result = true
15+
} else {
16+
console.Error(err.Error())
1417
}
1518

1619
return result

source/pacman/Upgrade.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pacman
22

3+
import "pacman-backup/console"
34
import "os/exec"
45

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

1213
if err == nil {
1314
result = true
15+
} else {
16+
console.Error(err.Error())
1417
}
1518

1619
return result

0 commit comments

Comments
 (0)