]> Sergey Matveev's repositories - meta4ra.git/blob - cmd/meta4-create/main.go
Use external utilities for hash calculation
[meta4ra.git] / cmd / meta4-create / main.go
1 /*
2 meta4a -- Metalink 4.0 creator
3 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Metalink 4.0 creator
19 package main
20
21 import (
22         "bufio"
23         "bytes"
24         "encoding/xml"
25         "flag"
26         "io"
27         "log"
28         "os"
29         "os/exec"
30         "path"
31         "strings"
32         "sync"
33         "time"
34
35         "go.stargrave.org/meta4ra"
36 )
37
38 type Hasher struct {
39         names []string
40         cmds  []*exec.Cmd
41         ins   []io.WriteCloser
42         outs  []io.ReadCloser
43         wg    sync.WaitGroup
44 }
45
46 func NewHasher(hashes string) *Hasher {
47         h := Hasher{}
48         for _, hc := range strings.Split(hashes, ",") {
49                 cols := strings.SplitN(hc, ":", 2)
50                 name, cmdline := cols[0], cols[1]
51                 cmd := exec.Command(cmdline)
52                 in, err := cmd.StdinPipe()
53                 if err != nil {
54                         log.Fatalln(err)
55                 }
56                 out, err := cmd.StdoutPipe()
57                 if err != nil {
58                         log.Fatalln(err)
59                 }
60                 if err = cmd.Start(); err != nil {
61                         log.Fatalln(err)
62                 }
63                 h.names = append(h.names, name)
64                 h.ins = append(h.ins, in)
65                 h.outs = append(h.outs, out)
66                 h.cmds = append(h.cmds, cmd)
67         }
68         return &h
69 }
70
71 func (h *Hasher) Write(p []byte) (n int, err error) {
72         h.wg.Add(len(h.names))
73         for _, in := range h.ins {
74                 go func(in io.WriteCloser) {
75                         if _, err := io.Copy(in, bytes.NewReader(p)); err != nil {
76                                 log.Fatalln(err)
77                         }
78                         h.wg.Done()
79                 }(in)
80         }
81         h.wg.Wait()
82         return len(p), nil
83 }
84
85 func (h *Hasher) Sums() []meta4ra.Hash {
86         sums := make([]meta4ra.Hash, 0, len(h.names))
87         for i, name := range h.names {
88                 if err := h.ins[i].Close(); err != nil {
89                         log.Fatalln(err)
90                 }
91                 dgst, err := io.ReadAll(h.outs[i])
92                 if err != nil {
93                         log.Fatalln(err)
94                 }
95                 sums = append(sums, meta4ra.Hash{Type: name, Hash: string(dgst[:len(dgst)-1])})
96                 if err = h.cmds[i].Wait(); err != nil {
97                         log.Fatalln(err)
98                 }
99         }
100         return sums
101 }
102
103 func main() {
104         fn := flag.String("fn", "", "Filename")
105         mtime := flag.String("mtime", "", "Take that file's mtime as a Published date")
106         desc := flag.String("desc", "", "Description")
107         sig := flag.String("sig", "", "Path to signature file")
108         hashesDef := []string{
109                 "sha-256:sha256",
110                 "sha-512:sha512",
111                 "shake128:shake128sum",
112                 "shake256:shake256sum",
113                 "streebog-256:streebog256sum",
114                 "streebog-512:streebog512sum",
115                 "skein-256:skein256",
116                 "skein-512:skein512",
117                 "blake3-256:b3sum",
118         }
119         hashes := flag.String("hashes", strings.Join(hashesDef, ","), "hash-name:command-s")
120         torrent := flag.String("torrent", "", "Torrent URL")
121         log.SetFlags(log.Lshortfile)
122         flag.Parse()
123         if *fn == "" {
124                 log.Fatalln("empty -fn")
125         }
126         urls := make([]meta4ra.URL, 0, len(flag.Args()))
127         for _, u := range flag.Args() {
128                 urls = append(urls, meta4ra.URL{URL: u})
129         }
130         h := NewHasher(*hashes)
131         br := bufio.NewReaderSize(os.Stdin, 1<<20)
132         buf := make([]byte, 1<<20)
133         size, err := io.CopyBuffer(h, br, buf)
134         if err != nil {
135                 log.Fatalln(err)
136         }
137         f := meta4ra.File{
138                 Name:        path.Base(*fn),
139                 Description: *desc,
140                 Size:        uint64(size),
141                 URLs:        urls,
142                 Hashes:      h.Sums(),
143         }
144         if *sig != "" {
145                 sigData, err := os.ReadFile(*sig)
146                 if err != nil {
147                         log.Fatalln(err)
148                 }
149                 f.Signature = &meta4ra.Signature{
150                         MediaType: meta4ra.GPGSigMediaType,
151                         Signature: "\n" + string(sigData),
152                 }
153         }
154         if *torrent != "" {
155                 f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}
156         }
157         var published time.Time
158         if *mtime == "" {
159                 published = time.Now()
160         } else {
161                 fi, err := os.Stat(*mtime)
162                 if err != nil {
163                         log.Fatalln(err)
164                 }
165                 published = fi.ModTime()
166         }
167         published = published.UTC().Truncate(time.Second)
168         m := meta4ra.Metalink{
169                 Files:     []meta4ra.File{f},
170                 Generator: meta4ra.Generator,
171                 Published: published,
172         }
173         out, err := xml.MarshalIndent(&m, "", "  ")
174         if err != nil {
175                 log.Fatalln(err)
176         }
177         os.Stdout.Write([]byte(xml.Header))
178         os.Stdout.Write(out)
179         os.Stdout.Write([]byte("\n"))
180 }