]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4ra/check.go
Fix various lint warnings and suggestions
[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         "net/http"
27         "os"
28         "path"
29         "strings"
30         "time"
31
32         meta4ra "go.stargrave.org/meta4ra/internal"
33 )
34
35 func runCheck() {
36         pipe := flag.Bool("pipe", false, "Verify file data from stdin, copy to stdout")
37         progress := flag.Bool("progress", false, "Show progress of piping/downloading")
38         allHashes := flag.Bool("all-hashes", false, "Check all hashes, not the first common one")
39         hashes := flag.String("hashes", meta4ra.HashesDefault,
40                 "hash-name:commandline[,...]")
41         extractSig := flag.Bool("extract-sig", false, "Extract signature files")
42         metaPath := flag.String("meta4", "file.meta4", "Metalink file")
43         dl := flag.Int("dl", -1, "URL index to download, instead of reading from stdin")
44         flag.Usage = func() {
45                 fmt.Fprintf(flag.CommandLine.Output(),
46                         "Usage: %s [options] [FILE ...]\n", os.Args[0])
47                 flag.PrintDefaults()
48                 fmt.Fprint(flag.CommandLine.Output(), `
49 If no FILEs are specified, then all <file>s from metalink are searched and
50 verified if they exist. Otherwise only specified FILEs are checked. If you
51 want to skip any <file> verification (for example only to validate the format
52 and -extract-sig, then you can just specify an empty ("") FILE.
53
54 If -dl (> 0) is specified, then it automatically implies -pipe, but
55 downloads data by specified URLs index, instead of reading from stdin.
56 That can be used as a downloading utility.
57 `)
58         }
59         flag.Parse()
60
61         if *showVersion {
62                 fmt.Println(meta4ra.Version())
63                 return
64         }
65         if *showWarranty {
66                 fmt.Println(meta4ra.Warranty)
67                 return
68         }
69
70         data, err := os.ReadFile(*metaPath)
71         if err != nil {
72                 log.Fatalln(err)
73         }
74         var meta meta4ra.Metalink
75         err = xml.Unmarshal(data, &meta)
76         if err != nil {
77                 log.Fatalln(err)
78         }
79
80         toCheck := make(map[string]string)
81         for _, fn := range flag.Args() {
82                 toCheck[path.Base(fn)] = fn
83         }
84         if *dl != -1 {
85                 *pipe = true
86         }
87         if *pipe && len(toCheck) != 1 {
88                 log.Fatalln("exactly single FILE must be specified when using -pipe")
89         }
90
91         bad := false
92         for _, f := range meta.Files {
93                 for _, sig := range f.Signature {
94                         if !*extractSig {
95                                 continue
96                         }
97                         var fn string
98                         switch sig.MediaType {
99                         case meta4ra.SigMediaTypePGP:
100                                 fn = f.Name + ".asc"
101                         case meta4ra.SigMediaTypeSSH:
102                                 fn = f.Name + ".sig"
103                         }
104                         if fn == "" {
105                                 continue
106                         }
107                         if err = os.WriteFile(
108                                 fn,
109                                 []byte(strings.TrimPrefix(sig.Signature, "\n")),
110                                 fs.FileMode(0o666),
111                         ); err != nil {
112                                 log.Println("Error:", f.Name, "can not save signature:", err)
113                                 bad = true
114                         }
115                 }
116
117                 fullPath := toCheck[f.Name]
118                 delete(toCheck, f.Name)
119                 if !(len(toCheck) == 0 || fullPath != "") {
120                         continue
121                 }
122                 if fullPath == "" {
123                         fullPath = f.Name
124                 }
125                 if !*pipe {
126                         s, err := os.Stat(fullPath)
127                         if err != nil {
128                                 log.Println(err)
129                                 bad = true
130                                 continue
131                         }
132                         if uint64(s.Size()) != f.Size {
133                                 log.Println("size mismatch",
134                                         f.Name, "our:", s.Size(), "their:", f.Size)
135                                 bad = true
136                                 continue
137                         }
138                 }
139
140                 hasher, err := meta4ra.NewHasher(*hashes)
141                 if err != nil {
142                         log.Println(f.Name, err)
143                         bad = true
144                         continue
145                 }
146                 var hashTheir string
147                 var hashName string
148                 if *allHashes {
149                         goto HashFound
150                 }
151                 for i, name := range hasher.Names {
152                         for _, h := range f.Hashes {
153                                 if h.Type == name {
154                                         hasher.Names = []string{name}
155                                         hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
156                                         hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
157                                         hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
158                                         hashName = name
159                                         hashTheir = h.Hash
160                                         goto HashFound
161                                 }
162                         }
163                 }
164                 log.Println("no common hashes found for:", f.Name)
165                 bad = true
166                 continue
167         HashFound:
168
169                 var src io.ReadCloser
170                 src = os.Stdin
171                 if !*pipe {
172                         src, err = os.Open(fullPath)
173                         if err != nil {
174                                 log.Println("Error:", f.Name, err)
175                                 bad = true
176                                 continue
177                         }
178                 }
179                 if *dl != -1 {
180                         var resp *http.Response
181                         resp, err = http.Get(f.URLs[*dl].URL)
182                         if err != nil {
183                                 log.Println("Error:", f.Name, err)
184                                 bad = true
185                                 continue
186                         }
187                         log.Println("HTTP response:")
188                         for k := range resp.Header {
189                                 log.Println("\t"+k+":", resp.Header.Get(k))
190                         }
191                         if resp.StatusCode != http.StatusOK {
192                                 log.Println("Bad status code:", f.Name, resp.Status)
193                                 bad = true
194                                 continue
195                         }
196                         src = resp.Body
197                 }
198                 err = hasher.Start()
199                 if err != nil {
200                         if !*pipe {
201                                 src.Close()
202                         }
203                         hasher.Stop()
204                         log.Println("Error:", f.Name, err)
205                         bad = true
206                         continue
207                 }
208                 var w io.Writer
209                 if *pipe {
210                         w = io.MultiWriter(os.Stdout, hasher)
211                 } else {
212                         w = hasher
213                 }
214                 if *progress {
215                         bar := Progress{w: w, now: time.Now(), total: f.Size}
216                         bar.next = bar.now.Add(ProgressPeriod)
217                         w = &bar
218                 }
219                 _, err = io.Copy(w, bufio.NewReaderSize(src, meta4ra.BufLen))
220                 if !*pipe || *dl != -1 {
221                         src.Close()
222                 }
223                 if err != nil {
224                         hasher.Stop()
225                         log.Println("Error:", f.Name, err)
226                         bad = true
227                         continue
228                 }
229                 sums, err := hasher.Sums()
230                 if err != nil {
231                         hasher.Stop()
232                         log.Println("Error:", f.Name, err)
233                         bad = true
234                         continue
235                 }
236                 if *allHashes {
237                         hashesOur := make(map[string]string, len(sums))
238                         for _, h := range sums {
239                                 hashesOur[h.Type] = h.Hash
240                         }
241                         for _, h := range f.Hashes {
242                                 hashOur := hashesOur[h.Type]
243                                 if h.Hash == hashOur {
244                                         if !*pipe {
245                                                 fmt.Fprintln(os.Stderr, f.Name, h.Type, "good")
246                                         }
247                                 } else {
248                                         log.Println(
249                                                 "hash mismatch:", f.Name, h.Type,
250                                                 "our:", hashOur,
251                                                 "their:", h.Hash,
252                                         )
253                                         bad = true
254                                 }
255                         }
256                 } else {
257                         hashOur := sums[0].Hash
258                         if hashOur == hashTheir {
259                                 if !*pipe {
260                                         fmt.Fprintln(os.Stderr, f.Name, hashName, "good")
261                                 }
262                         } else {
263                                 log.Println(
264                                         "hash mismatch:", f.Name, hashName,
265                                         "our:", hashOur,
266                                         "their:", hashTheir,
267                                 )
268                                 bad = true
269                                 continue
270                         }
271                 }
272         }
273         if len(toCheck) != 0 {
274                 if _, ok := toCheck["."]; !(len(toCheck) == 1 && ok) {
275                         log.Println("not all FILEs met")
276                         bad = true
277                 }
278         }
279         if bad {
280                 os.Exit(1)
281         }
282 }