]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4ra/check.go
c030f8b8417ec376d2008ab8aed377efa43ac15e
[meta4ra.git] / cmd / meta4ra / check.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 package main
17
18 import (
19         "bufio"
20         "encoding/xml"
21         "flag"
22         "fmt"
23         "io"
24         "io/fs"
25         "log"
26         "os"
27         "path"
28         "strings"
29
30         meta4ra "go.stargrave.org/meta4ra/internal"
31 )
32
33 func runCheck() {
34         stdin := flag.Bool("stdin", false, "Compare data of single file taken from stdin")
35         allHashes := flag.Bool("all-hashes", false, "Check all hashes, not the first common one")
36         hashes := flag.String("hashes", meta4ra.HashesDefault,
37                 "hash-name:commandline[,...]")
38         extractSig := flag.Bool("extract-sig", false, "Extract signature files")
39         metaPath := flag.String("meta4", "file.meta4", "Metalink file")
40         flag.Usage = func() {
41                 fmt.Fprintf(flag.CommandLine.Output(),
42                         "Usage: %s [options] [FILE ...]\n", os.Args[0])
43                 flag.PrintDefaults()
44                 fmt.Fprint(flag.CommandLine.Output(), `
45 If no FILEs are specified, then all <file>s from metalink are searched and
46 verified if they exist. Otherwise only specified FILEs are checked. If you
47 want to skip any <file> verification (for example only to validate the format
48 and -extract-sig, then you can just specify an empty ("") FILE.
49 `)
50         }
51         flag.Parse()
52
53         data, err := os.ReadFile(*metaPath)
54         if err != nil {
55                 log.Fatalln(err)
56         }
57         var meta meta4ra.Metalink
58         err = xml.Unmarshal(data, &meta)
59         if err != nil {
60                 log.Fatalln(err)
61         }
62
63         toCheck := make(map[string]string)
64         for _, fn := range flag.Args() {
65                 toCheck[path.Base(fn)] = fn
66         }
67         if *stdin && len(toCheck) != 1 {
68                 log.Fatalln("exactly single FILE must be specified when using -stdin")
69         }
70
71         bad := false
72         for _, f := range meta.Files {
73                 for _, sig := range f.Signature {
74                         if !*extractSig {
75                                 continue
76                         }
77                         var fn string
78                         switch sig.MediaType {
79                         case meta4ra.SigMediaTypePGP:
80                                 fn = f.Name + ".asc"
81                         case meta4ra.SigMediaTypeSSH:
82                                 fn = f.Name + ".sig"
83                         }
84                         if fn == "" {
85                                 continue
86                         }
87                         if err = os.WriteFile(
88                                 fn,
89                                 []byte(strings.TrimPrefix(sig.Signature, "\n")),
90                                 fs.FileMode(0666),
91                         ); err != nil {
92                                 fmt.Println("Error:", f.Name, "can not save signature:", err)
93                                 bad = true
94                         }
95                 }
96
97                 fullPath := toCheck[f.Name]
98                 delete(toCheck, f.Name)
99                 if !(len(toCheck) == 0 || fullPath != "") {
100                         continue
101                 }
102                 if fullPath == "" {
103                         fullPath = f.Name
104                 }
105                 if !*stdin {
106                         s, err := os.Stat(fullPath)
107                         if err != nil {
108                                 fmt.Println(err)
109                                 bad = true
110                                 continue
111                         }
112                         if uint64(s.Size()) != f.Size {
113                                 fmt.Println("size mismatch",
114                                         f.Name, "our:", s.Size(), "their:", f.Size)
115                                 bad = true
116                                 continue
117                         }
118                 }
119
120                 hasher, err := meta4ra.NewHasher(*hashes)
121                 if err != nil {
122                         fmt.Println(f.Name, err)
123                         bad = true
124                         continue
125                 }
126                 var hashTheir string
127                 var hashName string
128                 if *allHashes {
129                         goto HashFound
130                 }
131                 for i, name := range hasher.Names {
132                         for _, h := range f.Hashes {
133                                 if h.Type == name {
134                                         hasher.Names = []string{name}
135                                         hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
136                                         hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
137                                         hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
138                                         hashName = name
139                                         hashTheir = h.Hash
140                                         goto HashFound
141                                 }
142                         }
143                 }
144                 fmt.Println("no common hashes found for:", f.Name)
145                 bad = true
146                 continue
147         HashFound:
148
149                 fd := os.Stdin
150                 if !*stdin {
151                         fd, err = os.Open(fullPath)
152                         if err != nil {
153                                 fmt.Println("Error:", f.Name, err)
154                                 bad = true
155                                 continue
156                         }
157                 }
158                 err = hasher.Start()
159                 if err != nil {
160                         if !*stdin {
161                                 fd.Close()
162                         }
163                         hasher.Stop()
164                         fmt.Println("Error:", f.Name, err)
165                         bad = true
166                         continue
167                 }
168                 _, err = io.Copy(hasher, bufio.NewReaderSize(fd, meta4ra.BufLen))
169                 if !*stdin {
170                         fd.Close()
171                 }
172                 if err != nil {
173                         hasher.Stop()
174                         fmt.Println("Error:", f.Name, err)
175                         bad = true
176                         continue
177                 }
178                 sums, err := hasher.Sums()
179                 if err != nil {
180                         hasher.Stop()
181                         fmt.Println("Error:", f.Name, err)
182                         bad = true
183                         continue
184                 }
185                 if *allHashes {
186                         hashesOur := make(map[string]string, len(sums))
187                         for _, h := range sums {
188                                 hashesOur[h.Type] = h.Hash
189                         }
190                         for _, h := range f.Hashes {
191                                 hashOur := hashesOur[h.Type]
192                                 if h.Hash == hashOur {
193                                         fmt.Println(f.Name, h.Type, "good")
194                                 } else {
195                                         fmt.Println(
196                                                 "hash mismatch:", f.Name, h.Type,
197                                                 "our:", hashOur,
198                                                 "their:", h.Hash,
199                                         )
200                                         bad = true
201                                 }
202                         }
203                 } else {
204                         hashOur := sums[0].Hash
205                         if hashOur == hashTheir {
206                                 fmt.Println(f.Name, hashName, "good")
207                         } else {
208                                 fmt.Println(
209                                         "hash mismatch:", f.Name, hashName,
210                                         "our:", hashOur,
211                                         "their:", hashTheir,
212                                 )
213                                 bad = true
214                                 continue
215                         }
216                 }
217         }
218         if len(toCheck) != 0 {
219                 fmt.Println("not all FILEs met")
220                 bad = true
221         }
222         if bad {
223                 os.Exit(1)
224         }
225 }