summaryrefslogtreecommitdiff
path: root/doVerify.go
blob: 99175f0319cde6c6507fb373de3c5b81e619983c (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
package main

import (
	"errors"
	"os"
	"path/filepath"
	"strings"

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

func doVerify() (string, error) {
	if err := os.Chdir(me.pb.BaseDir); err != nil {
		return "no '" + me.pb.BaseDir + "' directory", err
	}

	if !shell.IsDir("pool/") {
		return "no " + filepath.Join(me.pb.BaseDir, "pool") + " directory", errors.New("mount -a ? missing wit/pool/")
	}
	log.Info("Processing dir", filepath.Join(me.pb.BaseDir, "pool"))

	var counter int
	filemap := make(map[string]*zoopb.Package)
	for p := range me.pb.IterAll() {
		// VERIFY FILENME FIRST
		pdump := log.Sprintf("%v", p)
		if !shell.Exists(p.Filename) {
			log.Printf("no file Exists()  %-130.130s\n", p.Filename)
			me.pb.Delete(p)
			counter += 1
			continue
		}
		if p.DebInfo == nil {
			fullname := filepath.Join(me.pb.BaseDir, p.Filename)
			log.Printf("Running:   dpkg -I %s\n", fullname)
			populateDebInfo(p)
			counter += 1
			// if counter > 10 {
			// 	break
			// }
			continue
		}
		if p.Filename != strings.TrimSpace(p.Filename) {
			log.Printf("%-16.16s %-130.130s\n", "garbage in filename", pdump)
			counter += 1
			continue
		}
		if p.Filename == "" {
			log.Printf("%-16.16s %-130.130s\n", "filename is blank", pdump)
			counter += 1
			continue
		}
		// looks for duplicate entries by filename. probably shouldn't deprecate this.
		fullname := filepath.Join(me.pb.BaseDir, p.Filename)
		if dupname, ok := filemap[p.Filename]; ok {
			dupdump := log.Sprintf("%v", dupname)
			log.Printf("dup filename 1    %-130.130s\n", pdump)
			log.Printf("dup filename 2    %-130.130s\n", dupdump)
			counter += 1
			continue
		}
		if strings.Contains(p.Filename, "dirty") {
			log.Printf("dirty .deb build  %-130.130s\n", fullname)
			counter += 1
			continue
		}
		// VERIFY FILENME END

		// make sure the fields are valid
		if p.Package == "" {
			me.pb.Delete(p)
			log.Printf("%-16.16s %-130.130s\n", "Package is blank", pdump)
			counter += 1
			continue
		}
		if (p.DebInfo.SHA256 == "") || (p.DebInfo.MD5SUM == "") {
			log.Printf("%-16.16s %-130.130s\n", "missing SHA256", pdump)
			me.pb.Delete(p)
			counter += 1
			continue
		}
		if p.Architecture == "" {
			log.Printf("%-16.16s %-130.130s\n", "arch is blank", pdump)
			counter += 1
			continue
		}
		if p.Architecture != "amd64" {
			log.Printf("%-16.16s %-130.130s\n", "arch is not x64", pdump)
			continue
		}
		if p.DebInfo.Architecture != "" {
			if p.Architecture != p.DebInfo.Architecture {
				log.Printf("%-16.16s %-130.130s %s\n", "arch mismatch", p.Architecture, p.DebInfo.Architecture)
				counter += 1
				continue
			}
		}
		if p.Version == "" {
			log.Printf("%-16.16s %-130.130s\n", "version is blank", pdump)
			counter += 1
			continue
		}
		if p.DebInfo.Size == "" {
			log.Printf("%-16.16s %-130.130s\n", "Size is blank", pdump)
			// todo: fix this
			p.DebInfo.Size = p.DebInfo.InstalledSize
			counter += 1
			continue
		}
		/*
			// verify constructed filename
			constructedFilename := p.Package + "_" + p.Version + "_" + p.Architecture + ".deb"
			_, fname := filepath.Split(p.Filename)
			if constructedFilename != fname {
				log.Info("filename mismatch", constructedFilename, fname, cobol.Since(p.Ctime))
				counter += 1
				moveToBroken(p)
				continue
			}
			// verify constructed filepath
			firstletter := fmt.Sprintf("%1.1s", p.Package)
			fpath := filepath.Join("pool", "main", firstletter, p.Package)
			constructedFullname := filepath.Join(fpath, constructedFilename)
			if p.Filename != constructedFullname {
				log.Info("bad fullname", p.Filename)
				log.Info("new fullname", constructedFullname)
				counter += 1
				moveToBroken(p)
				continue
			}
		*/

		// deprecate this and Delete(p) instead?
		// log.Info("Package", p.Package)
		if p.Ctime == nil {
			log.Printf("ctime is nil      %-130.130s\n", fullname)
			stat, err := os.Stat(fullname)
			if err != nil {
				log.Printf("stat error        %-130.130s %v\n", fullname, err)
				continue
			}
			p.Ctime = timestamppb.New(stat.ModTime())
			counter += 1
			continue
		}
	}

	log.Info("there were", counter, "errors")
	if counter != 0 {
		me.pb.Save()
	}

	return "verified ok", nil
}

func moveToBroken(p *zoopb.Package) (string, error) {
	if err := os.Chdir(me.pb.BaseDir); err != nil {
		return "no '" + me.pb.BaseDir + "' directory", err
	}
	brokendir := filepath.Join(me.pb.BaseDir, "broken")
	if err := os.MkdirAll(brokendir, 0755); err != nil {
		return "mkdir " + brokendir, err
	}
	_, fname := filepath.Split(p.Filename)
	newname := filepath.Join(brokendir, fname)
	if err := os.Rename(p.Filename, newname); err != nil {
		return "rename failed " + newname, err
	}
	// panic("rename worked?")
	return "rename worked " + newname, nil
}