]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4-check/main.go
f83d8d3e79598f2fc4da500634a334dbff893e0f
[meta4ra.git] / cmd / meta4-check / main.go
1 // meta4ra -- Metalink 4.0 utilities
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
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.
7 //
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.
12 //
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/>.
15
16 // Metalink 4.0 checker
17 package main
18
19 import (
20         "bufio"
21         "encoding/xml"
22         "flag"
23         "fmt"
24         "io"
25         "io/fs"
26         "log"
27         "os"
28         "strings"
29
30         "go.stargrave.org/meta4ra"
31 )
32
33 func main() {
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)
37         flag.Parse()
38         bad := false
39         for _, metaPath := range flag.Args() {
40                 data, err := os.ReadFile(metaPath)
41                 if err != nil {
42                         log.Fatalln(err)
43                 }
44                 var meta meta4ra.Metalink
45                 err = xml.Unmarshal(data, &meta)
46                 if err != nil {
47                         log.Fatalln(err)
48                 }
49                 for _, f := range meta.Files {
50                         for _, sig := range f.Signature {
51                                 if !*extractSig {
52                                         continue
53                                 }
54                                 var fn string
55                                 switch sig.MediaType {
56                                 case meta4ra.SigMediaTypePGP:
57                                         fn = f.Name + ".asc"
58                                 case meta4ra.SigMediaTypeSSH:
59                                         fn = f.Name + ".sig"
60                                 }
61                                 if fn == "" {
62                                         continue
63                                 }
64                                 if err = os.WriteFile(
65                                         fn,
66                                         []byte(strings.TrimPrefix(sig.Signature, "\n")),
67                                         fs.FileMode(0666),
68                                 ); err != nil {
69                                         fmt.Println("Error:", f.Name, "can not save signature:", err)
70                                         bad = true
71                                 }
72                         }
73                         hasher := meta4ra.NewHasher(*hashes)
74                         var hashTheir string
75                         var hashName string
76                         for i, name := range hasher.Names {
77                                 for _, h := range f.Hashes {
78                                         if h.Type == name {
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])
83                                                 hashName = name
84                                                 hashTheir = h.Hash
85                                                 goto HashFound
86                                         }
87                                 }
88                         }
89                         log.Fatalln("no common hashes found for:", f.Name)
90                 HashFound:
91                         fd, err := os.Open(f.Name)
92                         if err != nil {
93                                 continue
94                         }
95                         hasher.Start()
96                         _, err = io.Copy(hasher, bufio.NewReaderSize(fd, 1<<20))
97                         fd.Close()
98                         sums := hasher.Sums()
99                         if err != nil {
100                                 fmt.Println("Error:", f.Name, err)
101                                 bad = true
102                                 continue
103                         }
104                         hashOur := sums[0].Hash
105                         if hashOur == hashTheir {
106                                 fmt.Println(f.Name, hashName, "good")
107                         } else {
108                                 fmt.Println(
109                                         "Hash does not match:", f.Name, hashName,
110                                         "our:", hashOur,
111                                         "their:", hashTheir,
112                                 )
113                                 bad = true
114                                 continue
115                         }
116                 }
117         }
118         if bad {
119                 os.Exit(1)
120         }
121 }