deb-simple/apt.go

126 lines
2.3 KiB
Go

package main
import (
"os"
"fmt"
"io"
"io/ioutil"
"strings"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"golang.org/x/crypto/openpgp"
"time"
"gitea.meow.tf/tyler/deb-simple/deb/release"
"path"
)
func createRelease(config Conf, distro string) error {
outfile, err := os.Create(path.Join(config.DistPath(distro), "Release"))
if err != nil {
return fmt.Errorf("failed to create Release: %s", err)
}
defer outfile.Close()
r := &release.Release{
Suite: distro,
Architectures: config.Repo.ArchitectureNames(),
Components: []string{"main"},
Date: time.Now(),
}
for _, arch := range config.Repo.ArchitectureNames() {
absolutePath := path.Join(config.DistPath(distro), "main", "binary-" + arch)
list, err := ioutil.ReadDir(absolutePath)
if err != nil {
continue
}
basePath := path.Join("main", "binary-" + arch)
for _, file := range list {
filePath := path.Join(absolutePath, file.Name())
fileLocalPath := path.Join(basePath, file.Name())
fileLocalPath = strings.Replace(fileLocalPath, "\\", "/", -1)
f, err := os.Open(filePath)
if err != nil {
return err
}
var size int64 = file.Size()
if size == 0 {
if stat, err := os.Stat(filePath); err == nil {
size = stat.Size()
}
}
var (
md5hash = md5.New()
sha1hash = sha1.New()
sha256hash = sha256.New()
)
w := io.MultiWriter(md5hash, sha1hash, sha256hash)
io.Copy(w, f)
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)),
}
r.Append(repoFile)
f.Close()
f = nil
}
}
r.Write(outfile)
if pgpEntity != nil {
return signRelease(config, distro)
}
return nil
}
func signRelease(config Conf, distro string) error {
distPath := config.DistPath(distro)
f, err := os.Open(path.Join(distPath, "Release"))
if err != nil {
return fmt.Errorf("failed to read Release: %s", err)
}
defer f.Close()
gpgfile, err := os.Create(path.Join(distPath, "Release.gpg"))
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)
}
return nil
}