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/>.
32 meta4ra "go.stargrave.org/meta4ra/internal"
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")
45 fmt.Fprintf(flag.CommandLine.Output(),
46 "Usage: %s [options] [FILE ...]\n", os.Args[0])
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.
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.
62 fmt.Println(meta4ra.Version())
66 fmt.Println(meta4ra.Warranty)
70 data, err := os.ReadFile(*metaPath)
74 var meta meta4ra.Metalink
75 err = xml.Unmarshal(data, &meta)
80 toCheck := make(map[string]string)
81 for _, fn := range flag.Args() {
82 toCheck[path.Base(fn)] = fn
87 if *pipe && len(toCheck) != 1 {
88 log.Fatalln("exactly single FILE must be specified when using -pipe")
92 for _, f := range meta.Files {
93 for _, sig := range f.Signature {
98 switch sig.MediaType {
99 case meta4ra.SigMediaTypePGP:
101 case meta4ra.SigMediaTypeSSH:
107 if err = os.WriteFile(
109 []byte(strings.TrimPrefix(sig.Signature, "\n")),
112 log.Println("Error:", f.Name, "can not save signature:", err)
117 fullPath := toCheck[f.Name]
118 delete(toCheck, f.Name)
119 if !(len(toCheck) == 0 || fullPath != "") {
126 s, err := os.Stat(fullPath)
132 if uint64(s.Size()) != f.Size {
133 log.Println("size mismatch",
134 f.Name, "our:", s.Size(), "their:", f.Size)
140 hasher, err := meta4ra.NewHasher(*hashes)
142 log.Println(f.Name, err)
151 for i, name := range hasher.Names {
152 for _, h := range f.Hashes {
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])
164 log.Println("no common hashes found for:", f.Name)
169 var src io.ReadCloser
172 src, err = os.Open(fullPath)
174 log.Println("Error:", f.Name, err)
180 var resp *http.Response
181 resp, err = http.Get(f.URLs[*dl].URL)
183 log.Println("Error:", f.Name, err)
187 log.Println("HTTP response:")
188 for k := range resp.Header {
189 log.Println("\t"+k+":", resp.Header.Get(k))
191 if resp.StatusCode != http.StatusOK {
192 log.Println("Bad status code:", f.Name, resp.Status)
204 log.Println("Error:", f.Name, err)
210 w = io.MultiWriter(os.Stdout, hasher)
215 bar := Progress{w: w, now: time.Now(), total: f.Size}
216 bar.next = bar.now.Add(ProgressPeriod)
219 _, err = io.Copy(w, bufio.NewReaderSize(src, meta4ra.BufLen))
220 if !*pipe || *dl != -1 {
225 log.Println("Error:", f.Name, err)
229 sums, err := hasher.Sums()
232 log.Println("Error:", f.Name, err)
237 hashesOur := make(map[string]string, len(sums))
238 for _, h := range sums {
239 hashesOur[h.Type] = h.Hash
241 for _, h := range f.Hashes {
242 hashOur := hashesOur[h.Type]
243 if h.Hash == hashOur {
245 fmt.Fprintln(os.Stderr, f.Name, h.Type, "good")
249 "hash mismatch:", f.Name, h.Type,
257 hashOur := sums[0].Hash
258 if hashOur == hashTheir {
260 fmt.Fprintln(os.Stderr, f.Name, hashName, "good")
264 "hash mismatch:", f.Name, hashName,
273 if len(toCheck) != 0 {
274 if _, ok := toCheck["."]; !(len(toCheck) == 1 && ok) {
275 log.Println("not all FILEs met")