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/>.
31 meta4ra "go.stargrave.org/meta4ra/internal"
35 pipe := flag.Bool("pipe", false, "Verify file data from stdin, copy to stdout")
36 allHashes := flag.Bool("all-hashes", false, "Check all hashes, not the first common one")
37 hashes := flag.String("hashes", meta4ra.HashesDefault,
38 "hash-name:commandline[,...]")
39 extractSig := flag.Bool("extract-sig", false, "Extract signature files")
40 metaPath := flag.String("meta4", "file.meta4", "Metalink file")
41 dl := flag.Int("dl", -1, "URL index to download, instead of reading from stdin")
43 fmt.Fprintf(flag.CommandLine.Output(),
44 "Usage: %s [options] [FILE ...]\n", os.Args[0])
46 fmt.Fprint(flag.CommandLine.Output(), `
47 If no FILEs are specified, then all <file>s from metalink are searched and
48 verified if they exist. Otherwise only specified FILEs are checked. If you
49 want to skip any <file> verification (for example only to validate the format
50 and -extract-sig, then you can just specify an empty ("") FILE.
52 If -dl (> 0) is specified, then it automatically implies -pipe, but
53 downloads data by specified URLs index, instead of reading from stdin.
54 That can be used as a downloading utility.
60 fmt.Println(meta4ra.Version())
64 fmt.Println(meta4ra.Warranty)
68 data, err := os.ReadFile(*metaPath)
72 var meta meta4ra.Metalink
73 err = xml.Unmarshal(data, &meta)
78 toCheck := make(map[string]string)
79 for _, fn := range flag.Args() {
80 toCheck[path.Base(fn)] = fn
85 if *pipe && len(toCheck) != 1 {
86 log.Fatalln("exactly single FILE must be specified when using -pipe")
90 for _, f := range meta.Files {
91 for _, sig := range f.Signature {
96 switch sig.MediaType {
97 case meta4ra.SigMediaTypePGP:
99 case meta4ra.SigMediaTypeSSH:
105 if err = os.WriteFile(
107 []byte(strings.TrimPrefix(sig.Signature, "\n")),
110 log.Println("Error:", f.Name, "can not save signature:", err)
115 fullPath := toCheck[f.Name]
116 delete(toCheck, f.Name)
117 if !(len(toCheck) == 0 || fullPath != "") {
124 s, err := os.Stat(fullPath)
130 if uint64(s.Size()) != f.Size {
131 log.Println("size mismatch",
132 f.Name, "our:", s.Size(), "their:", f.Size)
138 hasher, err := meta4ra.NewHasher(*hashes)
140 log.Println(f.Name, err)
149 for i, name := range hasher.Names {
150 for _, h := range f.Hashes {
152 hasher.Names = []string{name}
153 hasher.Cmds = append(hasher.Cmds[:0], hasher.Cmds[i])
154 hasher.Ins = append(hasher.Ins[:0], hasher.Ins[i])
155 hasher.Outs = append(hasher.Outs[:0], hasher.Outs[i])
162 log.Println("no common hashes found for:", f.Name)
167 var src io.ReadCloser
170 src, err = os.Open(fullPath)
172 log.Println("Error:", f.Name, err)
178 resp, err := http.Get(f.URLs[*dl].URL)
180 log.Println("Error:", f.Name, err)
184 log.Println("HTTP response:")
185 for k := range resp.Header {
186 log.Println("\t"+k+":", resp.Header.Get(k))
188 if resp.StatusCode != http.StatusOK {
189 log.Println("Bad status code:", f.Name, resp.Status)
201 log.Println("Error:", f.Name, err)
207 w = io.MultiWriter(os.Stdout, hasher)
211 _, err = io.Copy(w, bufio.NewReaderSize(src, meta4ra.BufLen))
212 if !*pipe || *dl != -1 {
217 log.Println("Error:", f.Name, err)
221 sums, err := hasher.Sums()
224 log.Println("Error:", f.Name, err)
229 hashesOur := make(map[string]string, len(sums))
230 for _, h := range sums {
231 hashesOur[h.Type] = h.Hash
233 for _, h := range f.Hashes {
234 hashOur := hashesOur[h.Type]
235 if h.Hash == hashOur {
237 fmt.Fprintln(os.Stderr, f.Name, h.Type, "good")
241 "hash mismatch:", f.Name, h.Type,
249 hashOur := sums[0].Hash
250 if hashOur == hashTheir {
252 fmt.Fprintln(os.Stderr, f.Name, hashName, "good")
256 "hash mismatch:", f.Name, hashName,
265 if len(toCheck) != 0 {
266 if _, ok := toCheck["."]; !(len(toCheck) == 1 && ok) {
267 log.Println("not all FILEs met")