1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
package managers
import (
"fmt"
"os"
"path/filepath"
"runtime"
"slices"
"github.com/lxc/distrobuilder/shared"
)
type pacman struct {
common
}
func (m *pacman) load() error {
err := m.setMirrorlist()
if err != nil {
return fmt.Errorf("Failed to set mirrorlist: %w", err)
}
err = m.setupTrustedKeys()
if err != nil {
return fmt.Errorf("Failed to setup trusted keys: %w", err)
}
m.commands = managerCommands{
clean: "pacman",
install: "pacman",
refresh: "pacman",
remove: "pacman",
update: "pacman",
}
m.flags = managerFlags{
clean: []string{
"-Sc",
},
global: []string{
"--noconfirm",
},
install: []string{
"-S", "--needed",
},
remove: []string{
"-Rcs",
},
refresh: []string{
"-Syy",
},
update: []string{
"-Su",
},
}
m.hooks = managerHooks{
clean: func() error {
path := "/var/cache/pacman/pkg"
// List all entries.
entries, err := os.ReadDir(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("Failed to list directory '%s': %w", path, err)
}
// Individually wipe all entries.
for _, entry := range entries {
entryPath := filepath.Join(path, entry.Name())
err := os.RemoveAll(entryPath)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("Failed to remove '%s': %w", entryPath, err)
}
}
return nil
},
}
return nil
}
func (m *pacman) setupTrustedKeys() error {
var err error
_, err = os.Stat("/etc/pacman.d/gnupg")
if err == nil {
return nil
}
err = shared.RunCommand(m.ctx, nil, nil, "pacman-key", "--init")
if err != nil {
return fmt.Errorf("Error initializing with pacman-key: %w", err)
}
var keyring string
if slices.Contains([]string{"arm", "arm64"}, runtime.GOARCH) {
keyring = "archlinuxarm"
} else {
keyring = "archlinux"
}
err = shared.RunCommand(m.ctx, nil, nil, "pacman-key", "--populate", keyring)
if err != nil {
return fmt.Errorf("Error populating with pacman-key: %w", err)
}
return nil
}
func (m *pacman) setMirrorlist() error {
f, err := os.Create(filepath.Join("etc", "pacman.d", "mirrorlist"))
if err != nil {
return fmt.Errorf("Failed to create file %q: %w", filepath.Join("etc", "pacman.d", "mirrorlist"), err)
}
defer f.Close()
var mirror string
if slices.Contains([]string{"arm", "arm64"}, runtime.GOARCH) {
mirror = "Server = http://mirror.archlinuxarm.org/$arch/$repo"
} else if slices.Contains([]string{"riscv64"}, runtime.GOARCH) {
mirror = "Server = https://archriscv.felixc.at/repo/$repo"
} else {
mirror = "Server = http://mirrors.kernel.org/archlinux/$repo/os/$arch"
}
_, err = f.WriteString(mirror)
if err != nil {
return fmt.Errorf("Failed to write to %q: %w", filepath.Join("etc", "pacman.d", "mirrorlist"), err)
}
return nil
}
|