deb-simple/apt.go

126 lines
2.3 KiB
Go
Raw Normal View History

2018-10-14 08:17:04 +00:00
package main
2017-06-11 06:14:13 +00:00
import (
"os"
"fmt"
"io"
"io/ioutil"
"strings"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"golang.org/x/crypto/openpgp"
"time"
2018-10-14 08:18:57 +00:00
"gitea.meow.tf/tyler/deb-simple/deb/release"
"path"
2017-06-11 06:14:13 +00:00
)
2017-09-11 02:58:50 +00:00
func createRelease(config Conf, distro string) error {
outfile, err := os.Create(path.Join(config.DistPath(distro), "Release"))
2017-06-11 06:14:13 +00:00
if err != nil {
return fmt.Errorf("failed to create Release: %s", err)
}
2017-06-11 06:14:13 +00:00
defer outfile.Close()
2017-09-11 02:58:50 +00:00
r := &release.Release{
Suite: distro,
Architectures: config.Repo.ArchitectureNames(),
Components: []string{"main"},
Date: time.Now(),
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
for _, arch := range config.Repo.ArchitectureNames() {
absolutePath := path.Join(config.DistPath(distro), "main", "binary-" + arch)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
list, err := ioutil.ReadDir(absolutePath)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if err != nil {
continue
}
2017-06-11 06:14:13 +00:00
basePath := path.Join("main", "binary-" + arch)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
for _, file := range list {
filePath := path.Join(absolutePath, file.Name())
2017-06-11 06:14:13 +00:00
fileLocalPath := path.Join(basePath, file.Name())
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
fileLocalPath = strings.Replace(fileLocalPath, "\\", "/", -1)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
f, err := os.Open(filePath)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if err != nil {
return err
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
var size int64 = file.Size()
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if size == 0 {
if stat, err := os.Stat(filePath); err == nil {
size = stat.Size()
}
2017-06-11 06:14:13 +00:00
}
2017-09-11 02:58:50 +00:00
var (
md5hash = md5.New()
sha1hash = sha1.New()
sha256hash = sha256.New()
)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
w := io.MultiWriter(md5hash, sha1hash, sha256hash)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
io.Copy(w, f)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
repoFile := &release.File{
Name: fileLocalPath,
Size: size,
MD5Sum: hex.EncodeToString(md5hash.Sum(nil)),
SHA1: hex.EncodeToString(sha1hash.Sum(nil)),
SHA256: hex.EncodeToString(sha256hash.Sum(nil)),
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
r.Append(repoFile)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
f.Close()
f = nil
2017-06-11 06:14:13 +00:00
}
2017-09-11 02:58:50 +00:00
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
r.Write(outfile)
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if pgpEntity != nil {
return signRelease(config, distro)
2017-06-11 06:14:13 +00:00
}
2017-09-11 02:58:50 +00:00
return nil
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
func signRelease(config Conf, distro string) error {
distPath := config.DistPath(distro)
2017-06-11 06:14:13 +00:00
f, err := os.Open(path.Join(distPath, "Release"))
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
if err != nil {
return fmt.Errorf("failed to read Release: %s", err)
}
2017-06-11 06:14:13 +00:00
2017-09-11 02:58:50 +00:00
defer f.Close()
gpgfile, err := os.Create(path.Join(distPath, "Release.gpg"))
2017-09-11 02:58:50 +00:00
if err != nil {
return fmt.Errorf("failed to create Release.gpg: %s", err)
}
defer gpgfile.Close()
if err := openpgp.ArmoredDetachSignText(gpgfile, pgpEntity, f, nil); err != nil {
return fmt.Errorf("failed to sign Release: %s", err)
2017-06-11 06:14:13 +00:00
}
return nil
}