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
30 "go.stargrave.org/meta4ra"
34 hashes := flag.String("hashes", strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
35 extractSig := flag.Bool("extract-sig", false, "Extract signature files")
36 log.SetFlags(log.Lshortfile)
39 for _, metaPath := range flag.Args() {
40 data, err := os.ReadFile(metaPath)
44 var meta meta4ra.Metalink
45 err = xml.Unmarshal(data, &meta)
49 for _, f := range meta.Files {
50 for _, sig := range f.Signature {
55 switch sig.MediaType {
56 case meta4ra.SigMediaTypePGP:
58 case meta4ra.SigMediaTypeSSH:
64 if err = os.WriteFile(
66 []byte(strings.TrimPrefix(sig.Signature, "\n")),
69 fmt.Println("Error:", f.Name, "can not save signature:", err)
73 hasher := meta4ra.NewHasher(*hashes)
76 for i, name := range hasher.Names {
77 for _, h := range f.Hashes {
79 hasher.Names = []string{name}
80 hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
81 hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
82 hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
89 log.Fatalln("no common hashes found for:", f.Name)
91 fd, err := os.Open(f.Name)
96 _, err = io.Copy(hasher, bufio.NewReaderSize(fd, 1<<20))
100 fmt.Println("Error:", f.Name, err)
104 hashOur := sums[0].Hash
105 if hashOur == hashTheir {
106 fmt.Println(f.Name, hashName, "good")
109 "Hash does not match:", f.Name, hashName,