]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4ra/check.go
Convenient -pipe instead of -stdin
[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         pipe := flag.Bool("pipe", false, "Verify file data from stdin, copy to stdout")
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 *pipe && len(toCheck) != 1 {
77                 log.Fatalln("exactly single FILE must be specified when using -pipe")
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                                 log.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 !*pipe {
115                         s, err := os.Stat(fullPath)
116                         if err != nil {
117                                 log.Println(err)
118                                 bad = true
119                                 continue
120                         }
121                         if uint64(s.Size()) != f.Size {
122                                 log.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                         log.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                 log.Println("no common hashes found for:", f.Name)
154                 bad = true
155                 continue
156         HashFound:
157
158                 fd := os.Stdin
159                 if !*pipe {
160                         fd, err = os.Open(fullPath)
161                         if err != nil {
162                                 log.Println("Error:", f.Name, err)
163                                 bad = true
164                                 continue
165                         }
166                 }
167                 err = hasher.Start()
168                 if err != nil {
169                         if !*pipe {
170                                 fd.Close()
171                         }
172                         hasher.Stop()
173                         log.Println("Error:", f.Name, err)
174                         bad = true
175                         continue
176                 }
177                 var w io.Writer
178                 if *pipe {
179                         w = io.MultiWriter(os.Stdout, hasher)
180                 } else {
181                         w = hasher
182                 }
183                 _, err = io.Copy(w, bufio.NewReaderSize(fd, meta4ra.BufLen))
184                 if !*pipe {
185                         fd.Close()
186                 }
187                 if err != nil {
188                         hasher.Stop()
189                         log.Println("Error:", f.Name, err)
190                         bad = true
191                         continue
192                 }
193                 sums, err := hasher.Sums()
194                 if err != nil {
195                         hasher.Stop()
196                         log.Println("Error:", f.Name, err)
197                         bad = true
198                         continue
199                 }
200                 if *allHashes {
201                         hashesOur := make(map[string]string, len(sums))
202                         for _, h := range sums {
203                                 hashesOur[h.Type] = h.Hash
204                         }
205                         for _, h := range f.Hashes {
206                                 hashOur := hashesOur[h.Type]
207                                 if h.Hash == hashOur {
208                                         if !*pipe {
209                                                 fmt.Fprintln(os.Stderr, f.Name, h.Type, "good")
210                                         }
211                                 } else {
212                                         log.Println(
213                                                 "hash mismatch:", f.Name, h.Type,
214                                                 "our:", hashOur,
215                                                 "their:", h.Hash,
216                                         )
217                                         bad = true
218                                 }
219                         }
220                 } else {
221                         hashOur := sums[0].Hash
222                         if hashOur == hashTheir {
223                                 if !*pipe {
224                                         fmt.Fprintln(os.Stderr, f.Name, hashName, "good")
225                                 }
226                         } else {
227                                 log.Println(
228                                         "hash mismatch:", f.Name, hashName,
229                                         "our:", hashOur,
230                                         "their:", hashTheir,
231                                 )
232                                 bad = true
233                                 continue
234                         }
235                 }
236         }
237         if len(toCheck) != 0 {
238                 if _, ok := toCheck["."]; !(len(toCheck) == 1 && ok) {
239                         log.Println("not all FILEs met")
240                         bad = true
241                 }
242         }
243         if bad {
244                 os.Exit(1)
245         }
246 }