]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4-check/main.go
meta4-check workability again
[meta4ra.git] / cmd / meta4-check / main.go
1 /*
2 meta4a -- Metalink 4.0 checker
3 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Metalink 4.0 checker
19 package main
20
21 import (
22         "bufio"
23         "encoding/xml"
24         "flag"
25         "fmt"
26         "io"
27         "io/fs"
28         "log"
29         "os"
30         "strings"
31
32         "go.stargrave.org/meta4ra"
33 )
34
35 func main() {
36         hashes := flag.String("hashes", strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
37         extractSig := flag.Bool("extract-sig", false, "Extract signature files")
38         log.SetFlags(log.Lshortfile)
39         flag.Parse()
40         bad := false
41         for _, metaPath := range flag.Args() {
42                 data, err := os.ReadFile(metaPath)
43                 if err != nil {
44                         log.Fatalln(err)
45                 }
46                 var meta meta4ra.Metalink
47                 err = xml.Unmarshal(data, &meta)
48                 if err != nil {
49                         log.Fatalln(err)
50                 }
51                 for _, f := range meta.Files {
52                         if f.Signature != nil && *extractSig {
53                                 if err = os.WriteFile(
54                                         f.Name+".asc",
55                                         []byte(f.Signature.Signature),
56                                         fs.FileMode(0666),
57                                 ); err != nil {
58                                         fmt.Println("Error:", f.Name, "can not save signature:", err)
59                                         bad = true
60                                 }
61                         }
62                         hasher := meta4ra.NewHasher(*hashes)
63                         var hashTheir string
64                         var hashName string
65                         for i, name := range hasher.Names {
66                                 for _, h := range f.Hashes {
67                                         if h.Type == name {
68                                                 hasher.Names = []string{name}
69                                                 hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
70                                                 hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
71                                                 hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
72                                                 hashName = name
73                                                 hashTheir = h.Hash
74                                                 goto HashFound
75                                         }
76                                 }
77                         }
78                         log.Fatalln("no common hashes found for:", f.Name)
79                 HashFound:
80                         fd, err := os.Open(f.Name)
81                         if err != nil {
82                                 continue
83                         }
84                         hasher.Start()
85                         _, err = io.Copy(hasher, bufio.NewReaderSize(fd, 1<<20))
86                         fd.Close()
87                         sums := hasher.Sums()
88                         if err != nil {
89                                 fmt.Println("Error:", f.Name, err)
90                                 bad = true
91                                 continue
92                         }
93                         hashOur := sums[0].Hash
94                         if hashOur == hashTheir {
95                                 fmt.Println(f.Name, hashName, "good")
96                         } else {
97                                 fmt.Println(
98                                         "Hash does not match:", f.Name, hashName,
99                                         "our:", hashOur,
100                                         "their:", hashTheir,
101                                 )
102                                 bad = true
103                                 continue
104                         }
105                 }
106         }
107         if bad {
108                 os.Exit(1)
109         }
110 }