1 // meta4ra -- Metalink 4.0 utilities
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // Metalink 4.0 checker
31 "go.stargrave.org/meta4ra"
35 hashes := flag.String("hashes",
36 strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
37 extractSig := flag.Bool("extract-sig", false, "Extract signature files")
38 metaPath := flag.String("meta4", "file.meta4", "Metalink file")
40 fmt.Fprintf(flag.CommandLine.Output(),
41 "Usage: %s [options] [FILE ...]\n", os.Args[0])
43 fmt.Fprint(flag.CommandLine.Output(), `
44 If no FILEs are specified, then all <file>s from metalink are searched and
45 verified if they exist. Otherwise only specified FILEs are checked. If you
46 want to skip any <file> verification (for example only to validate the format
47 and -extract-sig, then you can just specify empty ("") FILE.
51 log.SetFlags(log.Lshortfile)
53 data, err := os.ReadFile(*metaPath)
57 var meta meta4ra.Metalink
58 err = xml.Unmarshal(data, &meta)
63 toCheck := make(map[string]string)
64 for _, fn := range flag.Args() {
65 toCheck[path.Base(fn)] = fn
69 for _, f := range meta.Files {
70 for _, sig := range f.Signature {
75 switch sig.MediaType {
76 case meta4ra.SigMediaTypePGP:
78 case meta4ra.SigMediaTypeSSH:
84 if err = os.WriteFile(
86 []byte(strings.TrimPrefix(sig.Signature, "\n")),
89 fmt.Println("Error:", f.Name, "can not save signature:", err)
93 hasher := meta4ra.NewHasher(*hashes)
96 for i, name := range hasher.Names {
97 for _, h := range f.Hashes {
99 hasher.Names = []string{name}
100 hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
101 hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
102 hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
109 log.Fatalln("no common hashes found for:", f.Name)
111 fullPath := toCheck[f.Name]
112 if !(len(toCheck) == 0 || fullPath != "") {
118 fd, err := os.Open(fullPath)
120 fmt.Println("Error:", f.Name, err)
125 _, err = io.Copy(hasher, bufio.NewReaderSize(fd, 1<<20))
127 sums := hasher.Sums()
129 fmt.Println("Error:", f.Name, err)
133 hashOur := sums[0].Hash
134 if hashOur == hashTheir {
135 fmt.Println(f.Name, hashName, "good")
138 "Hash does not match:", f.Name, hashName,