1 // meta4ra -- Metalink 4.0 utilities
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
30 meta4ra "go.stargrave.org/meta4ra/internal"
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")
41 fmt.Fprintf(flag.CommandLine.Output(),
42 "Usage: %s [options] [FILE ...]\n", os.Args[0])
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.
54 fmt.Println(meta4ra.Version())
58 fmt.Println(meta4ra.Warranty)
62 data, err := os.ReadFile(*metaPath)
66 var meta meta4ra.Metalink
67 err = xml.Unmarshal(data, &meta)
72 toCheck := make(map[string]string)
73 for _, fn := range flag.Args() {
74 toCheck[path.Base(fn)] = fn
76 if *pipe && len(toCheck) != 1 {
77 log.Fatalln("exactly single FILE must be specified when using -pipe")
81 for _, f := range meta.Files {
82 for _, sig := range f.Signature {
87 switch sig.MediaType {
88 case meta4ra.SigMediaTypePGP:
90 case meta4ra.SigMediaTypeSSH:
96 if err = os.WriteFile(
98 []byte(strings.TrimPrefix(sig.Signature, "\n")),
101 log.Println("Error:", f.Name, "can not save signature:", err)
106 fullPath := toCheck[f.Name]
107 delete(toCheck, f.Name)
108 if !(len(toCheck) == 0 || fullPath != "") {
115 s, err := os.Stat(fullPath)
121 if uint64(s.Size()) != f.Size {
122 log.Println("size mismatch",
123 f.Name, "our:", s.Size(), "their:", f.Size)
129 hasher, err := meta4ra.NewHasher(*hashes)
131 log.Println(f.Name, err)
140 for i, name := range hasher.Names {
141 for _, h := range f.Hashes {
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])
153 log.Println("no common hashes found for:", f.Name)
160 fd, err = os.Open(fullPath)
162 log.Println("Error:", f.Name, err)
173 log.Println("Error:", f.Name, err)
179 w = io.MultiWriter(os.Stdout, hasher)
183 _, err = io.Copy(w, bufio.NewReaderSize(fd, meta4ra.BufLen))
189 log.Println("Error:", f.Name, err)
193 sums, err := hasher.Sums()
196 log.Println("Error:", f.Name, err)
201 hashesOur := make(map[string]string, len(sums))
202 for _, h := range sums {
203 hashesOur[h.Type] = h.Hash
205 for _, h := range f.Hashes {
206 hashOur := hashesOur[h.Type]
207 if h.Hash == hashOur {
209 fmt.Fprintln(os.Stderr, f.Name, h.Type, "good")
213 "hash mismatch:", f.Name, h.Type,
221 hashOur := sums[0].Hash
222 if hashOur == hashTheir {
224 fmt.Fprintln(os.Stderr, f.Name, hashName, "good")
228 "hash mismatch:", f.Name, hashName,
237 if len(toCheck) != 0 {
238 if _, ok := toCheck["."]; !(len(toCheck) == 1 && ok) {
239 log.Println("not all FILEs met")