]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4ra/check.go
c53ccb9f0e7c7768ada5c1ff2d8f92eeb49a39da
[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         if *showVersion {
54                 fmt.Println(meta4ra.Version())
55                 return
56         }
57         if *showWarranty {
58                 fmt.Println(meta4ra.Warranty)
59                 return
60         }
61
62         data, err := os.ReadFile(*metaPath)
63         if err != nil {
64                 log.Fatalln(err)
65         }
66         var meta meta4ra.Metalink
67         err = xml.Unmarshal(data, &meta)
68         if err != nil {
69                 log.Fatalln(err)
70         }
71
72         toCheck := make(map[string]string)
73         for _, fn := range flag.Args() {
74                 toCheck[path.Base(fn)] = fn
75         }
76         if *stdin && len(toCheck) != 1 {
77                 log.Fatalln("exactly single FILE must be specified when using -stdin")
78         }
79
80         bad := false
81         for _, f := range meta.Files {
82                 for _, sig := range f.Signature {
83                         if !*extractSig {
84                                 continue
85                         }
86                         var fn string
87                         switch sig.MediaType {
88                         case meta4ra.SigMediaTypePGP:
89                                 fn = f.Name + ".asc"
90                         case meta4ra.SigMediaTypeSSH:
91                                 fn = f.Name + ".sig"
92                         }
93                         if fn == "" {
94                                 continue
95                         }
96                         if err = os.WriteFile(
97                                 fn,
98                                 []byte(strings.TrimPrefix(sig.Signature, "\n")),
99                                 fs.FileMode(0666),
100                         ); err != nil {
101                                 fmt.Println("Error:", f.Name, "can not save signature:", err)
102                                 bad = true
103                         }
104                 }
105
106                 fullPath := toCheck[f.Name]
107                 delete(toCheck, f.Name)
108                 if !(len(toCheck) == 0 || fullPath != "") {
109                         continue
110                 }
111                 if fullPath == "" {
112                         fullPath = f.Name
113                 }
114                 if !*stdin {
115                         s, err := os.Stat(fullPath)
116                         if err != nil {
117                                 fmt.Println(err)
118                                 bad = true
119                                 continue
120                         }
121                         if uint64(s.Size()) != f.Size {
122                                 fmt.Println("size mismatch",
123                                         f.Name, "our:", s.Size(), "their:", f.Size)
124                                 bad = true
125                                 continue
126                         }
127                 }
128
129                 hasher, err := meta4ra.NewHasher(*hashes)
130                 if err != nil {
131                         fmt.Println(f.Name, err)
132                         bad = true
133                         continue
134                 }
135                 var hashTheir string
136                 var hashName string
137                 if *allHashes {
138                         goto HashFound
139                 }
140                 for i, name := range hasher.Names {
141                         for _, h := range f.Hashes {
142                                 if h.Type == name {
143                                         hasher.Names = []string{name}
144                                         hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
145                                         hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
146                                         hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
147                                         hashName = name
148                                         hashTheir = h.Hash
149                                         goto HashFound
150                                 }
151                         }
152                 }
153                 fmt.Println("no common hashes found for:", f.Name)
154                 bad = true
155                 continue
156         HashFound:
157
158                 fd := os.Stdin
159                 if !*stdin {
160                         fd, err = os.Open(fullPath)
161                         if err != nil {
162                                 fmt.Println("Error:", f.Name, err)
163                                 bad = true
164                                 continue
165                         }
166                 }
167                 err = hasher.Start()
168                 if err != nil {
169                         if !*stdin {
170                                 fd.Close()
171                         }
172                         hasher.Stop()
173                         fmt.Println("Error:", f.Name, err)
174                         bad = true
175                         continue
176                 }
177                 _, err = io.Copy(hasher, bufio.NewReaderSize(fd, meta4ra.BufLen))
178                 if !*stdin {
179                         fd.Close()
180                 }
181                 if err != nil {
182                         hasher.Stop()
183                         fmt.Println("Error:", f.Name, err)
184                         bad = true
185                         continue
186                 }
187                 sums, err := hasher.Sums()
188                 if err != nil {
189                         hasher.Stop()
190                         fmt.Println("Error:", f.Name, err)
191                         bad = true
192                         continue
193                 }
194                 if *allHashes {
195                         hashesOur := make(map[string]string, len(sums))
196                         for _, h := range sums {
197                                 hashesOur[h.Type] = h.Hash
198                         }
199                         for _, h := range f.Hashes {
200                                 hashOur := hashesOur[h.Type]
201                                 if h.Hash == hashOur {
202                                         fmt.Println(f.Name, h.Type, "good")
203                                 } else {
204                                         fmt.Println(
205                                                 "hash mismatch:", f.Name, h.Type,
206                                                 "our:", hashOur,
207                                                 "their:", h.Hash,
208                                         )
209                                         bad = true
210                                 }
211                         }
212                 } else {
213                         hashOur := sums[0].Hash
214                         if hashOur == hashTheir {
215                                 fmt.Println(f.Name, hashName, "good")
216                         } else {
217                                 fmt.Println(
218                                         "hash mismatch:", f.Name, hashName,
219                                         "our:", hashOur,
220                                         "their:", hashTheir,
221                                 )
222                                 bad = true
223                                 continue
224                         }
225                 }
226         }
227         if len(toCheck) != 0 {
228                 if _, ok := toCheck["."]; !(len(toCheck) == 1 && ok) {
229                         fmt.Println("not all FILEs met")
230                         bad = true
231                 }
232         }
233         if bad {
234                 os.Exit(1)
235         }
236 }