summaryrefslogtreecommitdiff
path: root/mirrorsSupport.go
blob: aade2b81d3c20f2c08d4ae77f98516a02df86423 (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package zoopb

// makes dists/ directores for 'apt update'

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"os"
	"path"
	"path/filepath"
	"strings"

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
	"google.golang.org/protobuf/encoding/prototext"
)

var verbose bool

// remove and return the pb varname
func (pb *Package) DeleteString(varname string) string {
	return "todo"
}

func (pb *Package) Print() {
	shell.RunVerbose([]string{"dpkg", "-I", pb.Filename})
	log.Info("\nNEW PB START")
	log.Info(strings.TrimSpace(prototext.Format(pb)))
	log.Info("NEW PB END\n")

	log.Info("Attempt to walk pb.DebInfo")
	if err := printDebInfoStrings(pb, true); err != nil {
		log.Info("pb.walk error:", err)
	}

}

// makes /home/mirrors/debian/dists/ for use by 'apt update'
func (pb *Packages) MakeDists() (string, error) {
	log.Printf("Attempting to make dists/ in %s with %d .deb files\n", pb.BaseDir, pb.Len())
	os.Chdir(pb.BaseDir)

	var s string
	var err error
	var archpb *Packages
	archpb, s, err = pb.doNormalArch("all")
	log.Printf("arch all     found %5d packages. %s  err=(%v)\n", archpb.Len(), filepath.Join(pb.BaseDir, s), err)
	archpb, s, err = pb.doNormalArch("riscv64")
	log.Printf("arch riscv64 found %5d packages. %s  err=(%v)\n", archpb.Len(), filepath.Join(pb.BaseDir, s), err)

	//	archpb, s, err = pb.doNormalArch("amd64")
	//	log.Printf("arch amd64   found %5d packages. %s  err=(%v)\n", archpb.Len(), filepath.Join(pb.BaseDir, s), err)

	// archpb, s, err = pb.doNormalArch("arm64")
	// log.Printf("arch arm64   found %5d not as cool as riscv packages. msg='%s'  err=(%v)\n", archpb.Len(), s, err)

	return s, nil
}

func (pb *Packages) doNormalArch(arch string) (*Packages, string, error) {
	packageDir := filepath.Join("dists", "sid", "main", "binary-"+arch)
	archpb, s, err := pb.MakePackageFile(packageDir, arch)
	if err != nil {
		log.Info("MakePackageFile() failed", s, err)
	} else {
		if verbose {
			log.Info("MakePackageFile() for", arch, "worked")
		}
	}

	/*
		// 5. Generate and sign the Release file
		log.Println("Generating and signing Release file...")
		if err := generateAndSignReleaseFile(distPath); err != nil {
			log.Printf("Failed to generate or sign Release file: %v\n", err)
			return err
		}
	*/
	return archpb, filepath.Join(packageDir, "Packages"), err
}

// similar to "dpkg -I moonbuggy.deb"
// destroys the pb so send a copy
func (pb *Package) writeDebInfo(w io.Writer) {
	fmt.Fprintf(w, "Package: %s\n", pb.Package)
	fmt.Fprintf(w, "Filename: %s\n", pb.Filename)
	fmt.Fprintf(w, "MD5sum: %s\n", pb.DeleteString("md5sum"))
	fmt.Fprintf(w, "SHA1: %s\n", pb.DeleteString("SHA1"))
	fmt.Fprintf(w, "SHA256: %s\n", pb.DeleteString("sha256"))
}

// makes the file "dists/sid/main/binary-riscv64/Packages"
// then compresses it as .gz & .bz2 files
func (pb *Packages) MakePackageFile(packageDir string, arch string) (*Packages, string, error) {
	if verbose {
		log.Printf("Attempting to make Package file for arch=%s\n", arch)
	}
	if err := os.Chdir(pb.BaseDir); err != nil {
		return nil, "", err
	}

	fullpath := path.Join(pb.BaseDir, packageDir)
	if err := os.MkdirAll(fullpath, 0755); err != nil {
		return nil, "", err
	}

	// made a new PB in memory of just the matching riscv64.deb files
	matcharch := NewPackages()

	// goes through each .deb file and writes out the entry in the Packages file
	for debPB := range pb.IterAll() {
		if verbose {
			log.Info("Adding package", debPB.Architecture, debPB.Filename)
		}
		if debPB.Architecture != arch {
			continue
		}
		matcharch.Append(debPB)
	}

	if matcharch.Len() == 0 {
		return matcharch, "found no files", errors.New("no " + arch + " files found")
	}

	var data bytes.Buffer
	for debPB := range matcharch.IterByFilename() {
		if verbose {
			log.Info("Adding package", debPB.Filename)
		}
		debPB.writeDebInfo(&data)
		fmt.Fprintf(&data, "\n")
	}

	// write out the "Packages" file
	filename := filepath.Join(packageDir, "Package")
	fullname := filepath.Join(pb.BaseDir, filename)
	if len(data.String()) == 0 {
		return matcharch, fullname, errors.New("something is wrong. Package file would be empty")
	}
	log.Info("Going to write file", fullname, "with string length", len(data.String()))
	if err := os.WriteFile(fullname, []byte(data.String()), 0644); err != nil {
		return matcharch, fullname, err
	}

	log.Info(data.String())

	/*
		// Create "Packages.gz"
		if err := compressFile(filepath.Join(fullfile, "Package.gz"); err != nil {
			return err
		}

		// Create "Packages.gz"
		if err := compressFile(filepath.Join(fullfile, "Package.bz2"); err != nil {
			return err
		}
	*/
	s := log.Sprintf("Created %s", fullname)
	return matcharch, s, nil
}