2 meta4a -- Metalink 4.0 creator
3 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
18 // Metalink 4.0 creator
35 "go.stargrave.org/meta4ra"
46 func NewHasher(hashes string) *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()
56 out, err := cmd.StdoutPipe()
60 if err = cmd.Start(); err != nil {
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)
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 {
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 {
91 dgst, err := io.ReadAll(h.outs[i])
95 sums = append(sums, meta4ra.Hash{Type: name, Hash: string(dgst[:len(dgst)-1])})
96 if err = h.cmds[i].Wait(); err != nil {
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{
111 "shake128:shake128sum",
112 "shake256:shake256sum",
113 "streebog-256:streebog256sum",
114 "streebog-512:streebog512sum",
115 "skein-256:skein256",
116 "skein-512:skein512",
119 hashes := flag.String("hashes", strings.Join(hashesDef, ","), "hash-name:command-s")
120 torrent := flag.String("torrent", "", "Torrent URL")
121 log.SetFlags(log.Lshortfile)
124 log.Fatalln("empty -fn")
126 urls := make([]meta4ra.URL, 0, len(flag.Args()))
127 for _, u := range flag.Args() {
128 urls = append(urls, meta4ra.URL{URL: u})
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)
138 Name: path.Base(*fn),
145 sigData, err := os.ReadFile(*sig)
149 f.Signature = &meta4ra.Signature{
150 MediaType: meta4ra.GPGSigMediaType,
151 Signature: "\n" + string(sigData),
155 f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}
157 var published time.Time
159 published = time.Now()
161 fi, err := os.Stat(*mtime)
165 published = fi.ModTime()
167 published = published.UTC().Truncate(time.Second)
168 m := meta4ra.Metalink{
169 Files: []meta4ra.File{f},
170 Generator: meta4ra.Generator,
171 Published: published,
173 out, err := xml.MarshalIndent(&m, "", " ")
177 os.Stdout.Write([]byte(xml.Header))
179 os.Stdout.Write([]byte("\n"))