aboutsummaryrefslogtreecommitdiff
path: root/internal/slackware_com/checksums.go
blob: 643e20495de7a78ebcf3b81523b4c9bd88420bac (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
package slackware_com

import (
	"bufio"
	"bytes"
	"crypto/md5"
	"errors"
	"fmt"
	"hash"
	"io"
	"os"
	"path"
	"strings"
)

var checksumsHeader = strings.TrimSpace(`
These are the MD5 message digests for the files in this directory.
If you want to test your files, use 'md5sum' and compare the values to
the ones listed here.

To test all these files, use this command:

tail +13 CHECKSUMS.md5 | md5sum -c --quiet - | less

'md5sum' can be found in the GNU coreutils package on ftp.gnu.org in
/pub/gnu, or at any GNU mirror site.

MD5 message digest                Filename
`)

type ChecksumsEntry struct {
	MD5      string
	Filename string
}

type Checksums struct {
	Entries []ChecksumsEntry
}

func OpenChecksums(filePath string) (*Checksums, error) {
	f, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}

	buf, err := io.ReadAll(f)
	if err != nil {
		return nil, err
	}

	var checksums Checksums
	err = checksums.UnmarshalText(buf)
	if err != nil {
		return nil, err
	}

	return &checksums, nil
}

func (c *Checksums) Hash() hash.Hash {
	return md5.New()
}

func (c *Checksums) UnmarshalText(buf []byte) error {
	r := bufio.NewReader(bytes.NewReader(buf))

	for _, headerLine := range strings.Split(checksumsHeader, "\n") {
		line, _, err := r.ReadLine()
		if err != nil {
			return err
		}

		if string(line) != headerLine {
			println(string(line))
			println(headerLine)
			return errors.New("unexpected format")
		}
	}

	for {
		line, _, err := r.ReadLine()
		if err == io.EOF {
			break
		} else if err != nil {
			return err
		}

		fields := strings.Split(string(line), "  ")
		if len(fields) != 2 {
			return errors.New("unexpected format")
		}
		c.Entries = append(c.Entries, ChecksumsEntry{
			MD5:      fields[0],
			Filename: fields[1],
		})
	}

	return nil
}

func (c *Checksums) HasFilePath(filePath string) bool {
	filePath = path.Clean(filePath)
	for _, entry := range c.Entries {
		if entry.Filename == "./"+filePath {
			return true
		}
	}
	return false
}

func (c *Checksums) Files() []string {
	var entries []string

	for _, entry := range c.Entries {
		entries = append(entries, entry.Filename)
	}

	return entries
}

func (c *Checksums) String() string {
	str := strings.TrimSpace(checksumsHeader)

	for _, entry := range c.Entries {
		line := fmt.Sprintf("%s  %s", entry.MD5, entry.Filename)
		str = str + "\n" + line
	}

	return str
}

func (c *Checksums) Verify(fileName string, sum []byte) (bool, error) {
	fileName = path.Clean(fileName)
	sumStr := fmt.Sprintf("%x", sum)

	for _, entry := range c.Entries {
		if entry.Filename == "./"+fileName {
			return entry.MD5 == sumStr, nil
		}
	}

	return false, errors.New("not found")
}