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 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")
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.
53 data, err := os.ReadFile(*metaPath)
57 var meta meta4ra.Metalink
58 err = xml.Unmarshal(data, &meta)
63 toCheck := make(map[string]string)
64 for _, fn := range flag.Args() {
65 toCheck[path.Base(fn)] = fn
67 if *stdin && len(toCheck) != 1 {
68 log.Fatalln("exactly single FILE must be specified when using -stdin")
72 for _, f := range meta.Files {
73 for _, sig := range f.Signature {
78 switch sig.MediaType {
79 case meta4ra.SigMediaTypePGP:
81 case meta4ra.SigMediaTypeSSH:
87 if err = os.WriteFile(
89 []byte(strings.TrimPrefix(sig.Signature, "\n")),
92 fmt.Println("Error:", f.Name, "can not save signature:", err)
97 fullPath := toCheck[f.Name]
98 delete(toCheck, f.Name)
99 if !(len(toCheck) == 0 || fullPath != "") {
106 s, err := os.Stat(fullPath)
112 if uint64(s.Size()) != f.Size {
113 fmt.Println("size mismatch",
114 f.Name, "our:", s.Size(), "their:", f.Size)
120 hasher, err := meta4ra.NewHasher(*hashes)
122 fmt.Println(f.Name, err)
131 for i, name := range hasher.Names {
132 for _, h := range f.Hashes {
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])
144 fmt.Println("no common hashes found for:", f.Name)
151 fd, err = os.Open(fullPath)
153 fmt.Println("Error:", f.Name, err)
164 fmt.Println("Error:", f.Name, err)
168 _, err = io.Copy(hasher, bufio.NewReaderSize(fd, meta4ra.BufLen))
174 fmt.Println("Error:", f.Name, err)
178 sums, err := hasher.Sums()
181 fmt.Println("Error:", f.Name, err)
186 hashesOur := make(map[string]string, len(sums))
187 for _, h := range sums {
188 hashesOur[h.Type] = h.Hash
190 for _, h := range f.Hashes {
191 hashOur := hashesOur[h.Type]
192 if h.Hash == hashOur {
193 fmt.Println(f.Name, h.Type, "good")
196 "hash mismatch:", f.Name, h.Type,
204 hashOur := sums[0].Hash
205 if hashOur == hashTheir {
206 fmt.Println(f.Name, hashName, "good")
209 "hash mismatch:", f.Name, hashName,
218 if len(toCheck) != 0 {
219 fmt.Println("not all FILEs met")