summaryrefslogtreecommitdiff
path: root/makePackagesFile.go
blob: 025771244d795cc23d4b13dc1422ff5a6ecb73cb (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main

import (
	"bufio"
	"crypto/md5"
	"crypto/sha256"
	"errors"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/lib/cobol"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/zoopb"
	"go.wit.com/log"
	"google.golang.org/protobuf/types/known/timestamppb"
)

func populateDebInfo(p *zoopb.Package) error {
	// SIMPLE SANITY CHECKS
	if p.DebInfo != nil {
		// already added p.DebInfo
		return nil
	}
	fullname := filepath.Join(me.pb.BaseDir, p.Filename)
	cmd := []string{"dpkg", "-I", fullname}
	r := shell.Run(cmd)
	if r.Error != nil {
		return r.Error
	}
	if r.Exit != 0 {
		return errors.New("dpkg returned -1")
	}
	stat, err := os.Stat(fullname)
	if err != nil {
		return err
	}
	filedata, err := os.Open(fullname)
	if err != nil {
		return err
	}
	defer filedata.Close()
	// SIMPLE SANITY CHECKS END

	// SHA256 HASH
	p.DebInfo = new(zoopb.DebInfo)

	hSHA256 := sha256.New()
	hMD5 := md5.New() // probably deprecate, but love md5sum too much
	// hSHA1 := sha1.New() // deprecated

	// TeeReader allows writing to multiple hashers at once
	// multiWriter := io.MultiWriter(hMD5, hSHA1, hSHA256)
	multiWriter := io.MultiWriter(hSHA256, hMD5)
	if _, err := io.Copy(multiWriter, filedata); err != nil {
		return err
	}
	p.DebInfo.SHA256 = fmt.Sprintf("%x", hSHA256.Sum(nil)) // should be the standard now
	p.DebInfo.MD5SUM = fmt.Sprintf("%x", hMD5.Sum(nil))    // probably deprecate
	// p.DebInfo.SHA1 = fmt.Sprintf("%x", hSHA1.Sum(nil))     // deprecated
	// SHA256 HASH END

	// set file create time
	p.Ctime = timestamppb.New(stat.ModTime())

	// PARSE "dpkg -I <p.Filename>", then exit as we are done
	starting := true
	all := strings.Join(r.Stdout, "\n")
	scanner := bufio.NewScanner(strings.NewReader(all))
	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.Fields(line)
		if starting {
			if parts[0] == "new" {
				if argv.Verbose {
					log.Printf("new: %v\n", parts)
				}
				// skip the first dpkg -I line
				continue
			}
			if parts[0] == "size" {
				if argv.Verbose {
					log.Printf("size: %v\n", parts)
				}
				p.DebInfo.InstalledSize = parts[1]
				// scan all the entries from size
				for scanner.Scan() {
					line = scanner.Text()
					parts = strings.Fields(line)
					if strings.HasPrefix(line, "  ") {
						if argv.Verbose {
							log.Printf("sizeline: %v\n", parts)
						}
						continue
					}
					starting = false
					break
				}
			}
			if starting {
				if argv.Verbose {
					log.Printf("parts: %v\n", parts)
				}
				continue
			}
		}
		varname := strings.TrimSuffix(parts[0], ":")
		varval := strings.Join(parts[1:], " ")
		// log.Printf("varname:%s varval:%s\n", varname, varval)
		switch varname {
		case "Package":
			p.Package = varval
		case "Filename":
			log.Printf("Filename: varname:%s varval:%s\n", varname, varval)
			// p.Package = varval
		case "Version":
			p.Version = varval
		case "Architecture":
			p.Architecture = varval
		case "GoPath":
			p.Namespace = varval
		case "Maintainer":
			p.DebInfo.Maintainer = varval
		case "Packager":
			p.DebInfo.Packager = varval
		case "Depends":
			p.DebInfo.Depends = varval
		case "Source":
			p.DebInfo.Source = varval
		case "URL":
			p.DebInfo.URL = varval
		case "Build-Depends":
			p.DebInfo.BuildDepends = varval
		case "Installed-Size:":
		case "Installed-Size":
			p.DebInfo.InstalledSize = varval
		case "Homepage":
			p.DebInfo.URL = varval
		case "Conflicts":
			p.DebInfo.Conflicts = varval
		case "Source-Date":
			t, err := cobol.GetTime(varval)
			if err == nil {
				p.GitDate = timestamppb.New(t)
			} else {
				if argv.Verbose {
					log.Info("FIXME: Package-Build-Date", varval, err)
				}
			}
		case "Build-Date":
		case "Package-Build-Date":
			t, err := cobol.GetTime(varval)
			if err == nil {
				p.BuildDate = timestamppb.New(t)
			} else {
				if argv.Verbose {
					log.Info("FIXME: Package-Build-Date", varval, err)
				}
			}
		case "Git-Tag-Date":
			if argv.Verbose {
				log.Info("FIXME: Git-Tag-Date", varval)
			}
		case "Description":
			description := varval
			for scanner.Scan() {
				line := scanner.Text()
				if strings.HasPrefix(line, "  ") {
					line = strings.TrimSpace(line)
					description += line + "\n"
					continue
				}
				break
			}
			p.DebInfo.Description = description
		default:
			// This forces me(it could be you!) to fix this parser
			varname2 := strings.TrimSuffix(varname, ":")
			log.Printf("UNKNOWN: varname:%s varval:%s varname2=%s\n", varname, varval, varname2)
			panic("fix mirrors populateDebInfo()")
		}
	}
	// pb.DebInfo should be correctly populated. You can verify these with "mirrors --verify"
	return nil
}

/*
UNKNOWN: varname:Depends varval:protobuf-compiler, protoc-gen-go | protoc-gen-go-wit
UNKNOWN: varname:Build-Depends varval:golang
UNKNOWN: varname:URL varval:https://go.wit.com/
UNKNOWN: varname:Package-Build-Date varval:2025/10/05 07:40:30 UTC
UNKNOWN: varname:Git-Tag-Date varval:todo: get from repo
UNKNOWN: varname:Description varval:autogen protobuf marshal and sort files
UNKNOWN: varname:this varval:is needed for go.wit.com/lib/protobuf/ GO packages
*/

func getDebianControlFile(p *zoopb.Package, varname string) (string, string) {
	switch varname {
	case "Package":
		return "Package", p.Package
	case "Filename":
		return "Filename", p.Filename
	case "Version":
		return "Version", p.Version
	case "Architecture":
		return "Architecture", p.Architecture
	case "Maintainer":
		return "Maintainer", p.DebInfo.Maintainer
		// return "Maintainer", p.Author
	case "Source":
		if argv.Verbose {
			log.Info("skipping from controlfile", varname)
			// return "Source", p.DebInfo.Source
		}
		return "Source", ""
	case "Conflicts":
		return "Conflicts", p.DebInfo.Conflicts
	case "Packager":
		return "Packager", p.DebInfo.Packager
		// return "Packager", p.Packager
	case "Depends":
		return "Depends", p.DebInfo.Depends
	case "BuildDepends":
		return "Build-Depends", p.DebInfo.BuildDepends
	case "InstalledSize":
		return "Installed-Size", "222222222"
	case "URL":
		return "Homepage", p.DebInfo.URL
	case "Size":
		return "Size", "22222222"
	case "BuildDate":
		return "Build-Date", cobol.Time(p.BuildDate)
	// case "SHA1":
	// 	return "SHA1", p.DebInfo.SHA1
	case "MD5SUM":
		return "MD5Sum", p.DebInfo.MD5SUM
	case "SHA256":
		return "SHA256", p.DebInfo.SHA256
	case "SHA512":
		return "SHA512", "" // todo: implement or totally rediculously overkill ?
	}
	log.Info("DebInfo sent a field we didn't have. fix the code above", varname)
	return "", ""
}

// make a list of the newest .deb files
func doMakePackagesFile(all *zoopb.Packages) string {
	var pfile string
	for p := range all.IterAll() {
		var controlfile string
		parts, err := zoopb.GetDebInfoFields(p)
		if err != nil {
			log.Info(err)
		}
		for _, varname := range parts {
			varname, varval := getDebianControlFile(p, varname)
			varval = strings.TrimSpace(varval)
			if varval == "" {
				continue
			}
			controlfile += log.Sprintf("%s: %s\n", varname, varval)
		}
		controlfile += log.Sprintf("\n")
		pfile += controlfile
	}
	return pfile
}

/*
	log.Printf("Scanning for .deb files in %s\n", poolDir)
	debInfos, err := scanDebs(poolDir, 200)
	if err != nil {
		log.Printf("Failed to scan .deb files: %v\n", err)
		me.sh.BadExit("scan pool scan dir", err)
	}

	var counter int
	for _, deb := range debInfos {
		newdeb := new(zoopb.Package)
		newdeb.DebInfo = new(zoopb.DebInfo)
		newdeb.Filename = deb.Filename
		newdeb.DebInfo.Filename = deb.Filename
		newdeb.DebInfo.MD5SUM = deb.MD5Sum
		// newdeb.DebInfo.SHA1 = deb.SHA1Sum
		newdeb.DebInfo.SHA256 = deb.SHA256Sum

		// log.Info("len(CONTROLDATA)", len(deb.ControlData))
		// log.Sprintf("VAR='%s' VAL='%s'\n", varname, varvalue)
		// log.Info("%v", deb.ControlData)
		for varname, varvalue := range deb.ControlData {
			switch varname {
			case "Package":
				newdeb.Package = varvalue
			case "Version":
				newdeb.Version = varvalue
			case "Architecture":
				newdeb.Architecture = varvalue
			case "Git-Tag-Date":
				if argv.Verbose {
					log.Info("CONVERT THIS TO TIME", varname, varvalue)
				}
			case "Depends":
				newdeb.DebInfo.Depends = varvalue
			case "Build-Depends":
				newdeb.DebInfo.BuildDepends = varvalue
			case "Packager":
				newdeb.DebInfo.Packager = varvalue
			case "Package-Build-Date":
				varname = "PackageBuildDate"
				const layout = "2006/01/02 15:04:05 MST"
				parsedTime, err := time.Parse(layout, varvalue)
				if err != nil {
					log.Info("CONVERT TO TIME failed", varname, varvalue, err)
				}
				newdeb.BuildDate = timestamppb.New(parsedTime)
			case "URL":
				newdeb.DebInfo.Homepage = varvalue
			default:
				if err := debian.SetDebInfoString(newdeb, varname, varvalue); err == nil {
					if argv.Verbose {
						log.Printf("Searching for Sugarman WORKED: VAR='%-30s' VAL='%s'\n", varname, varvalue)
					}
				} else {
					log.Printf("Searching for Sugarman (unknwon var): VAR='%-30s' VAL='%s' err=%v\n", varname, varvalue, err)
				}
				// todo: add to protomap
			}
		}
*/