-/*
-meta4a -- Metalink 4.0 creator
-Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program. If not, see <http://www.gnu.org/licenses/>.
-*/
+// meta4ra -- Metalink 4.0 utilities
+// Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Metalink 4.0 creator
package main
import (
"bufio"
- "bytes"
"encoding/xml"
"flag"
+ "fmt"
"io"
"log"
"os"
- "os/exec"
"path"
"strings"
- "sync"
"time"
"go.stargrave.org/meta4ra"
)
-type Hasher struct {
- names []string
- cmds []*exec.Cmd
- ins []io.WriteCloser
- outs []io.ReadCloser
- wg sync.WaitGroup
-}
-
-func NewHasher(hashes string) *Hasher {
- h := Hasher{}
- for _, hc := range strings.Split(hashes, ",") {
- cols := strings.SplitN(hc, ":", 2)
- name, cmdline := cols[0], cols[1]
- cmd := exec.Command(cmdline)
- in, err := cmd.StdinPipe()
- if err != nil {
- log.Fatalln(err)
- }
- out, err := cmd.StdoutPipe()
- if err != nil {
- log.Fatalln(err)
- }
- if err = cmd.Start(); err != nil {
- log.Fatalln(err)
- }
- h.names = append(h.names, name)
- h.ins = append(h.ins, in)
- h.outs = append(h.outs, out)
- h.cmds = append(h.cmds, cmd)
- }
- return &h
-}
-
-func (h *Hasher) Write(p []byte) (n int, err error) {
- h.wg.Add(len(h.names))
- for _, in := range h.ins {
- go func(in io.WriteCloser) {
- if _, err := io.Copy(in, bytes.NewReader(p)); err != nil {
- log.Fatalln(err)
- }
- h.wg.Done()
- }(in)
- }
- h.wg.Wait()
- return len(p), nil
-}
-
-func (h *Hasher) Sums() []meta4ra.Hash {
- sums := make([]meta4ra.Hash, 0, len(h.names))
- for i, name := range h.names {
- if err := h.ins[i].Close(); err != nil {
- log.Fatalln(err)
- }
- dgst, err := io.ReadAll(h.outs[i])
- if err != nil {
- log.Fatalln(err)
- }
- sums = append(sums, meta4ra.Hash{Type: name, Hash: string(dgst[:len(dgst)-1])})
- if err = h.cmds[i].Wait(); err != nil {
- log.Fatalln(err)
- }
- }
- return sums
-}
-
func main() {
fn := flag.String("fn", "", "Filename")
mtime := flag.String("mtime", "", "Take that file's mtime as a Published date")
desc := flag.String("desc", "", "Description")
- sig := flag.String("sig", "", "Path to signature file")
- hashesDef := []string{
- "sha-256:sha256",
- "sha-512:sha512",
- "shake128:shake128sum",
- "shake256:shake256sum",
- "streebog-256:streebog256sum",
- "streebog-512:streebog512sum",
- "skein-256:skein256",
- "skein-512:skein512",
- "blake3-256:b3sum",
- }
- hashes := flag.String("hashes", strings.Join(hashesDef, ","), "hash-name:command-s")
+ sigPGP := flag.String("sig-pgp", "",
+ "Path to OpenPGP .asc signature file for inclusion")
+ sigSSH := flag.String("sig-ssh", "",
+ "Path to OpenSSH .sig signature file for inclusion")
+ hashes := flag.String("hashes",
+ strings.Join(meta4ra.HashesDefault, ","), "hash-name:command-s")
+ noPublished := flag.Bool("no-published", false,
+ "Do not include Published field")
+ noGenerator := flag.Bool("no-generator", false,
+ "Do not include Generator field")
torrent := flag.String("torrent", "", "Torrent URL")
- log.SetFlags(log.Lshortfile)
+ flag.Usage = func() {
+ fmt.Fprintf(flag.CommandLine.Output(),
+ "Usage: %s [options] [URL ...] < DATA > XXX.meta4\n", os.Args[0])
+ flag.PrintDefaults()
+ }
flag.Parse()
+ log.SetFlags(log.Lshortfile)
if *fn == "" {
log.Fatalln("empty -fn")
}
for _, u := range flag.Args() {
urls = append(urls, meta4ra.URL{URL: u})
}
- h := NewHasher(*hashes)
+ h := meta4ra.NewHasher(*hashes)
+ h.Start()
br := bufio.NewReaderSize(os.Stdin, 1<<20)
buf := make([]byte, 1<<20)
size, err := io.CopyBuffer(h, br, buf)
URLs: urls,
Hashes: h.Sums(),
}
- if *sig != "" {
- sigData, err := os.ReadFile(*sig)
+ if *sigPGP != "" {
+ sigData, err := os.ReadFile(*sigPGP)
if err != nil {
log.Fatalln(err)
}
- f.Signature = &meta4ra.Signature{
- MediaType: meta4ra.GPGSigMediaType,
+ f.Signature = append(f.Signature, meta4ra.Signature{
+ MediaType: meta4ra.SigMediaTypePGP,
Signature: "\n" + string(sigData),
+ })
+ }
+ if *sigSSH != "" {
+ sigData, err := os.ReadFile(*sigSSH)
+ if err != nil {
+ log.Fatalln(err)
}
+ f.Signature = append(f.Signature, meta4ra.Signature{
+ MediaType: meta4ra.SigMediaTypeSSH,
+ Signature: "\n" + string(sigData),
+ })
}
if *torrent != "" {
f.MetaURLs = []meta4ra.MetaURL{{MediaType: "torrent", URL: *torrent}}
}
published = fi.ModTime()
}
- published = published.UTC().Truncate(time.Second)
- m := meta4ra.Metalink{
- Files: []meta4ra.File{f},
- Generator: meta4ra.Generator,
- Published: published,
+ m := meta4ra.Metalink{Files: []meta4ra.File{f}}
+ if !*noPublished {
+ t := published.UTC().Truncate(time.Second)
+ m.Published = &t
+ }
+ if !*noGenerator {
+ m.Generator = meta4ra.Generator
}
out, err := xml.MarshalIndent(&m, "", " ")
if err != nil {